-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
176 lines (161 loc) · 4 KB
/
process.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Sigstoped
* Copyright (C) 2020 Carl Klemm
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <iostream>
#include <fstream>
#include <filesystem>
#include <signal.h>
#include "process.h"
#include "split.h"
#include "debug.h"
bool Process::operator==(const Process& in)
{
return pid_ == in.pid_;
}
bool Process::operator!=(const Process& in)
{
return pid_ != in.pid_;
}
pid_t Process::getPid()
{
return pid_;
}
void Process::stop(bool children)
{
if(pid_ > 0)
{
kill(pid_, SIGSTOP);
if(children)
{
std::vector<Process> children = getChildren();
for(auto& child : children) child.stop(true);
}
}
}
void Process::resume(bool children)
{
kill(pid_, SIGCONT);
if(children)
{
std::vector<Process> children = getChildren();
for(auto& child : children) child.resume(true);
}
}
bool Process::getStoped()
{
return stoped_;
}
std::vector<std::string> Process::openStatus()
{
std::fstream statusFile;
std::vector<std::string> lines;
if(pid_ > 0)
{
statusFile.open(std::string("/proc/") + std::to_string(pid_)+ "/status", std::fstream::in);
if(statusFile.is_open())
{
std::string statusString((std::istreambuf_iterator<char>(statusFile)),
std::istreambuf_iterator<char>());
lines = split(statusString);
statusFile.close();
}
else
{
std::cout<<"cant open "<<"/proc/" + std::to_string(pid_)+ "/status"<<std::endl;
}
}
return lines;
}
std::vector<Process> Process::getChildren()
{
std::vector<Process> ret;
std::vector<pid_t> processes = getAllProcessPids();
for(const auto & pid : processes)
{
Process process(pid);
if(process.getPPid() == pid_) ret.push_back(process);
}
return ret;
}
std::vector<pid_t> Process::getAllProcessPids()
{
std::vector<pid_t> processes;
for(const auto & entry : std::filesystem::directory_iterator("/proc"))
{
pid_t pid = convertPid(entry.path().stem());
if(pid > 0) processes.push_back(pid);
}
return processes;
}
std::string Process::getName()
{
if(name_.size() == 0)
{
std::vector<std::string> lines = openStatus();
if(lines.size() > 0)
{
std::vector<std::string> tokens = split(lines[0], '\t');
if(tokens.size() > 1)
{
name_ = tokens[1];
}
}
}
return name_;
}
std::vector<Process> Process::byName(const std::string& name)
{
std::vector<pid_t> procs = getAllProcessPids();
std::vector<Process> retProcs;
for(const auto & pid : procs )
{
Process proc(pid);
if(name == proc.getName()) retProcs.push_back(proc);
}
return retProcs;
}
pid_t Process::convertPid(const std::string& pid)
{
pid_t ret;
try
{
ret = std::stol(pid);
}
catch(const std::invalid_argument& exception)
{
debug(exception.what());
ret = -1;
}
return ret;
}
pid_t Process::getPPid()
{
if(ppid_ < 0)
{
std::vector<std::string> lines = openStatus();
if(lines.size() > 7)
{
std::vector<std::string> tokens = split(lines[6] , '\t');
if(tokens.size() > 1) ppid_ = convertPid(tokens[1]);
}
}
return ppid_;
}
Process::Process(pid_t pidIn): pid_(pidIn)
{
}