Skip to content

Commit

Permalink
Refactor sample application
Browse files Browse the repository at this point in the history
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
dtrugman committed Mar 6, 2021
1 parent c797da0 commit d8dfe8a
Show file tree
Hide file tree
Showing 12 changed files with 684 additions and 327 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ if (CMAKE_BUILD_TYPE MATCHES Debug)
target_link_libraries (pfs PUBLIC -lasan ${SANITIZE_FLAGS})
endif ()

add_executable (sample sample/sample.cpp)
aux_source_directory (sample SAMPLE_SOURCES)
add_executable (sample ${SAMPLE_SOURCES})
target_link_libraries (sample PRIVATE pfs)

set (UNITTEST_SOURCE_DIR test)
Expand Down
28 changes: 28 additions & 0 deletions sample/enum.hpp
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
115 changes: 115 additions & 0 deletions sample/enum_fd.cpp
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;
}
79 changes: 79 additions & 0 deletions sample/enum_net.cpp
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;
}
66 changes: 66 additions & 0 deletions sample/enum_system.cpp
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;
}
Loading

0 comments on commit d8dfe8a

Please sign in to comment.