-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split sample into sub files and add a new category of sample applications: Re-implementation of known tools using the library. Also fix formatting and logging headers to support inclusion from multiple compilation units.
- Loading branch information
Showing
12 changed files
with
684 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef SAMPLE_ENUM_HPP | ||
#define SAMPLE_ENUM_HPP | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
int enum_system(std::vector<std::string>&& args); | ||
int enum_net(std::vector<std::string>&& args); | ||
int enum_tasks(std::vector<std::string>&& args); | ||
int enum_fds(std::vector<std::string>&& args); | ||
|
||
#endif // SAMPLE_ENUM_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "enum.hpp" | ||
#include "format.hpp" | ||
#include "log.hpp" | ||
|
||
#include "pfs/procfs.hpp" | ||
|
||
template <typename T> | ||
void add_sockets(const std::vector<T>& sockets, | ||
std::unordered_map<ino_t, std::string>& output) | ||
{ | ||
for (auto& socket : sockets) | ||
{ | ||
std::ostringstream out; | ||
out << socket; | ||
output.emplace(socket.inode, out.str()); | ||
} | ||
} | ||
|
||
std::unordered_map<ino_t, std::string> enum_sockets(const pfs::net& net) | ||
{ | ||
std::unordered_map<ino_t, std::string> sockets; | ||
add_sockets(net.get_icmp(), sockets); | ||
add_sockets(net.get_icmp6(), sockets); | ||
add_sockets(net.get_raw(), sockets); | ||
add_sockets(net.get_raw6(), sockets); | ||
add_sockets(net.get_tcp(), sockets); | ||
add_sockets(net.get_tcp6(), sockets); | ||
add_sockets(net.get_udp(), sockets); | ||
add_sockets(net.get_udp6(), sockets); | ||
add_sockets(net.get_udplite(), sockets); | ||
add_sockets(net.get_udplite6(), sockets); | ||
add_sockets(net.get_unix(), sockets); | ||
add_sockets(net.get_netlink(), sockets); | ||
return sockets; | ||
} | ||
|
||
void enum_task_fds(const pfs::task& task) | ||
{ | ||
try | ||
{ | ||
LOG("fds"); | ||
LOG("---"); | ||
|
||
auto sockets = enum_sockets(task.get_net()); | ||
|
||
for (auto& iter : task.get_fds()) | ||
{ | ||
auto num = iter.first; | ||
auto& fd = iter.second; | ||
|
||
auto st = fd.get_target_stat(); | ||
auto inode = st.st_ino; | ||
|
||
std::ostringstream out; | ||
out << "target[" << fd.get_target() << "] "; | ||
|
||
auto socket = sockets.find(inode); | ||
if (socket != sockets.end()) | ||
{ | ||
out << socket->second; | ||
} | ||
|
||
LOG(num << ": " << out.str()); | ||
} | ||
|
||
LOG(""); | ||
} | ||
catch (const std::runtime_error& ex) | ||
{ | ||
LOG("Error when printing task[" << task.id() << "] fds:"); | ||
LOG(TAB << ex.what()); | ||
} | ||
} | ||
|
||
int enum_fds(std::vector<std::string>&& args) | ||
{ | ||
pfs::procfs pfs; | ||
|
||
if (args.empty()) | ||
{ | ||
for (const auto& process : pfs.get_processes()) | ||
{ | ||
for (const auto& thread : process.get_tasks()) | ||
{ | ||
enum_task_fds(thread); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
for (const auto& task : args) | ||
{ | ||
auto id = std::stoi(task); | ||
enum_task_fds(pfs.get_task(id)); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "enum.hpp" | ||
#include "format.hpp" | ||
#include "log.hpp" | ||
|
||
#include "pfs/procfs.hpp" | ||
|
||
int enum_net(std::vector<std::string>&& args) | ||
{ | ||
(void)args; | ||
|
||
try | ||
{ | ||
LOG("========================================================="); | ||
LOG("Net"); | ||
LOG("========================================================="); | ||
|
||
pfs::procfs pfs; | ||
auto net = pfs.get_net(); | ||
|
||
auto icmp = net.get_icmp(); | ||
print(icmp); | ||
|
||
auto icmp6 = net.get_icmp6(); | ||
print(icmp6); | ||
|
||
auto raw = net.get_raw(); | ||
print(raw); | ||
|
||
auto raw6 = net.get_raw6(); | ||
print(raw6); | ||
|
||
auto tcp = net.get_tcp(); | ||
print(tcp); | ||
|
||
auto tcp6 = net.get_tcp6(); | ||
print(tcp6); | ||
|
||
auto udp = net.get_udp(); | ||
print(udp); | ||
|
||
auto udp6 = net.get_udp6(); | ||
print(udp6); | ||
|
||
auto udplite = net.get_udplite(); | ||
print(udplite); | ||
|
||
auto udplite6 = net.get_udplite6(); | ||
print(udplite6); | ||
|
||
auto unix = net.get_unix(); | ||
print(unix); | ||
|
||
auto netlink = net.get_netlink(); | ||
print(netlink); | ||
} | ||
catch (const std::runtime_error& ex) | ||
{ | ||
LOG("Error when printing net info:"); | ||
LOG(TAB << ex.what()); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "enum.hpp" | ||
#include "format.hpp" | ||
#include "log.hpp" | ||
|
||
#include "pfs/procfs.hpp" | ||
|
||
int enum_system(std::vector<std::string>&& args) | ||
{ | ||
(void)args; | ||
|
||
try | ||
{ | ||
LOG("========================================================="); | ||
LOG("System"); | ||
LOG("========================================================="); | ||
|
||
pfs::procfs pfs; | ||
|
||
auto buddyinfo = pfs.get_buddyinfo(); | ||
print(buddyinfo); | ||
|
||
auto cmdline = pfs.get_cmdline(); | ||
print(cmdline); | ||
|
||
auto modules = pfs.get_modules(); | ||
print(modules); | ||
|
||
auto filesystems = pfs.get_filesystems(); | ||
print(filesystems); | ||
|
||
auto loadavg = pfs.get_loadavg(); | ||
print(loadavg); | ||
|
||
auto meminfo = pfs.get_meminfo(); | ||
print(meminfo); | ||
|
||
auto version = pfs.get_version(); | ||
print(version); | ||
|
||
auto version_signature = pfs.get_version_signature(); | ||
print(version_signature); | ||
} | ||
catch (const std::runtime_error& ex) | ||
{ | ||
LOG("Error when printing system info:"); | ||
LOG(TAB << ex.what()); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.