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 Class Reference

A struct with various tools that are independent or depend on operating system. More...

#include <tools.h>

Public Member Functions

void SetBufferSize (const size_t &buffer_size)
 Set buffer size of an instance.
 
int SetSigint (void(*handler)(int)) const noexcept
 Attach handler to SIGINT signal.
 
std::FILE * ShellCommand (const std::string &command) const noexcept
 Do a command in system shell.
 
std::unordered_set< pid_t > GetAllPids () const noexcept
 Get all PIDs existing in the system.
 
std::unordered_set< std::string > GetAllProcessNames () const noexcept
 Get all names of processes existing in the system.
 
std::unordered_set< pid_t > FindPidsByName (const std::string &name) const noexcept
 Get all PIDs by name of the process.
 
uint8_t PidExists (const pid_t &pid) const noexcept
 Check if a process with the given PID exists.
 
uint8_t ProcessExists (const std::string &pname) const noexcept
 Check if a process with the given name exists.
 
uint8_t DecodePermissions (const std::string &permissions) const noexcept
 Get permissions stored as uint8_t from std::string.
 
std::string EncodePermissions (const uint8_t &mode) const noexcept
 Get permissions stored as std::string from uint8_t.
 
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.
 

Detailed Description

A struct with various tools that are independent or depend on operating system.

This struct provides a set of functions that are useful in some parts of the project, but cannot be attributed to any existing category. These functions do not depend on any parts of the program. The struct includes such functionality as working with signals (SIGINT), getting terminal window size, making shell commands, comparing memory arrays and so on.

Member Function Documentation

◆ DecodePermissions()

uint8_t Tools::DecodePermissions ( const std::string & permissions) const
noexcept

Get permissions stored as uint8_t from std::string.

Parameters
[in]permissionsPermissions stored as std::string, for example, "rwxp". Some additional characters after are not prohibited.
Returns
Value where the last 4 bits represent permissions (rwxs are 1, others are 0). If the input std::string is too short or an unexpected character is found, the return value is -1 (255).

Process permissions of a memory segment from std::string to uint8_t.

◆ EncodePermissions()

std::string Tools::EncodePermissions ( const uint8_t & mode) const
noexcept

Get permissions stored as std::string from uint8_t.

Parameters
[in]modeValue, where the last 4 bits represent permissions (rwxs are 1, others are 0).
Returns
Permissions stored as std::string, for example, "rwxp".

Process permissions of a memory segment from uint8_t to std::string.

◆ FindDifferencesOfLen()

std::array< std::unique_ptr< char[]>, 2 > Tools::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.

Parameters
[in]old_strFirst "old" array of char.
[in]new_strSecond "new" array of char.
[in]str_lenLength of both arrays.
[in]lenLength of different sequences.
[out]doneAmount of bytes processed.
Returns
std::array of length of 2 containing "old" and "new" versions of a changed substring in unique_ptr containers.

Find first pair of different substrings of given length on equal positions comparing 2 given arrays. Each char of the substrings must be different. If longer substrings differ, their shorter versions are not returned.

◆ FindPidsByName()

std::unordered_set< pid_t > Tools::FindPidsByName ( const std::string & name) const
noexcept

Get all PIDs by name of the process.

Parameters
[in]nameName of the process in type of std::string.
Returns
std::unordered_set with PIDs in pid_t type.

Get all process IDs that can be found by running "pgrep -x "process_name"" shell command.

◆ GetAllPids()

std::unordered_set< pid_t > Tools::GetAllPids ( ) const
noexcept

Get all PIDs existing in the system.

Returns
std::unordered_set with all PIDs in pid_t type.

Get all process IDs that can be found by running "pgrep .+" shell command.

◆ GetAllProcessNames()

std::unordered_set< std::string > Tools::GetAllProcessNames ( ) const
noexcept

Get all names of processes existing in the system.

Returns
std::unordered_set with all names in std::string type.

Get all names of processes that can be found by running "pgrep -l .+" shell command.

◆ PidExists()

uint8_t Tools::PidExists ( const pid_t & pid) const
noexcept

Check if a process with the given PID exists.

Parameters
[in]pidPID of the process in pid_t type.
Returns
0 if process exists, 1 if process does not exist, 2 if there was an error while checking.

Check if a process with the given process ID exists in the system by checking if /proc/PID directory exists.

◆ ProcessExists()

uint8_t Tools::ProcessExists ( const std::string & pname) const
noexcept

Check if a process with the given name exists.

Parameters
[in]pnameName of the process in type of std::string.
Returns
0 if process exists, 1 if process does not exist, 2 if there was an error while checking.

Check if a process with the given process name exists in the system by running "pgrep -x "process_name"" shell command.

◆ SetBufferSize()

void Tools::SetBufferSize ( const size_t & buffer_size)
inline

Set buffer size of an instance.

Parameters
[in]buffer_sizeDesired buffer size in bytes.

Set buffer size, a number of bytes that are allocated when needed.

◆ SetSigint()

int Tools::SetSigint ( void(* handler )(int)) const
noexcept

Attach handler to SIGINT signal.

Parameters
[in]handlerPointer to handler function.
Returns
Return value of calling sigaction function (success is 0, error is other value).

Attach handler function to SIGINT, which will be called every time SIGINT is received.

◆ ShellCommand()

std::FILE * Tools::ShellCommand ( const std::string & command) const
noexcept

Do a command in system shell.

Parameters
[in]commandCommand in type of std::string.
Returns
FILE*, read-only pipe that is stdout of the done command.

Do a command in a shell by calling function "popen" and return stdout pipe of the command.


The documentation for this class was generated from the following files: