MemoryAccessor 1
A command-line front-end for exploring virtual memory of a linux process by accessing /proc/PID/mem file.
Loading...
Searching...
No Matches
tools.h
Go to the documentation of this file.
1// MemoryAccessor - A tool for accessing /proc/PID/mem
2// Copyright (C) 2024 zloymish
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
24#ifndef MEMORYACCESSOR_SRC_TOOLS_H_
25#define MEMORYACCESSOR_SRC_TOOLS_H_
26
27#include <sys/types.h>
28
29#include <array>
30#include <cstdio>
31#include <memory>
32#include <string>
33#include <unordered_set>
34
45class Tools {
46public:
53 void SetBufferSize(const size_t &buffer_size) { buffer_size_ = buffer_size; }
54
55 int SetSigint(void (*handler)(int)) const noexcept;
56
57 std::FILE *ShellCommand(const std::string &command) const noexcept;
58 std::unordered_set<pid_t> GetAllPids() const noexcept;
59 std::unordered_set<std::string> GetAllProcessNames() const noexcept;
60 std::unordered_set<pid_t>
61 FindPidsByName(const std::string &name) const noexcept;
62 uint8_t PidExists(const pid_t &pid) const noexcept;
63 uint8_t ProcessExists(const std::string &pname) const noexcept;
64
65 uint8_t DecodePermissions(const std::string &permissions) const noexcept;
66 std::string EncodePermissions(const uint8_t &mode) const noexcept;
67
68 std::array<std::unique_ptr<char[]>, 2>
69 FindDifferencesOfLen(const char *old_str, const char *new_str, size_t str_len,
70 size_t &done, const size_t &len) const noexcept;
71
72private:
73 const std::string kModes{"rwxs"};
75 const uint8_t kModesLength{static_cast<uint8_t>(
76 kModes.length())};
77 size_t buffer_size_{
78 0x1000};
79};
80
81#endif // MEMORYACCESSOR_SRC_TOOLS_H_
A struct with various tools that are independent or depend on operating system.
Definition tools.h:45
uint8_t DecodePermissions(const std::string &permissions) const noexcept
Get permissions stored as uint8_t from std::string.
Definition tools.cc:215
uint8_t ProcessExists(const std::string &pname) const noexcept
Check if a process with the given name exists.
Definition tools.cc:194
std::string EncodePermissions(const uint8_t &mode) const noexcept
Get permissions stored as std::string from uint8_t.
Definition tools.cc:244
std::unordered_set< std::string > GetAllProcessNames() const noexcept
Get all names of processes existing in the system.
Definition tools.cc:109
int SetSigint(void(*handler)(int)) const noexcept
Attach handler to SIGINT signal.
Definition tools.cc:45
std::array< std::unique_ptr< char[]>, 2 > FindDifferencesOfLen(const char *old_str, const char *new_str, size_t str_len, size_t &done, const size_t &len) const noexcept
Find differences of given length comparing two arrays of char.
Definition tools.cc:274
std::unordered_set< pid_t > GetAllPids() const noexcept
Get all PIDs existing in the system.
Definition tools.cc:79
std::unordered_set< pid_t > FindPidsByName(const std::string &name) const noexcept
Get all PIDs by name of the process.
Definition tools.cc:143
void SetBufferSize(const size_t &buffer_size)
Set buffer size of an instance.
Definition tools.h:53
uint8_t PidExists(const pid_t &pid) const noexcept
Check if a process with the given PID exists.
Definition tools.cc:175
std::FILE * ShellCommand(const std::string &command) const noexcept
Do a command in system shell.
Definition tools.cc:66