-
Notifications
You must be signed in to change notification settings - Fork 1
/
processtools.cpp
373 lines (329 loc) · 12.2 KB
/
processtools.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (C) 2017 Lily Rivers (VioletDarkKitty)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 3
* of the License only.
*
* 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 "processtools.h"
#include <string>
#include <unistd.h>
#include <fstream>
#include <QFileInfo>
#include <proc/sysinfo.h>
#include <time.h>
#include <qdiriterator.h>
#include <QIcon>
#include <QMap>
#include <unordered_set>
#include <unordered_map>
#include "hashqstring.h"
namespace processTools {
/* from http://stackoverflow.com/questions/24581908/c-lstat-on-proc-pid-exe
* User explicitly gave allowences for using this code via email
*/
/**
* @brief exe_of Obtain the executable path a process is running
* @param pid: Process ID
* @param sizeptr: If specified, the allocated size is saved here
* @param lenptr: If specified, the path length is saved here
* @return the dynamically allocated pointer to the path, or NULL with errno set if an error occurs.
*/
char* exe_of(const pid_t pid, size_t *const sizeptr, size_t *const lenptr)
{
char *exe_path = NULL;
size_t exe_size = 1024;
ssize_t exe_used;
char path_buf[64];
unsigned int path_len;
path_len = snprintf(path_buf, sizeof path_buf, "/proc/%ld/exe", (long)pid);
if (path_len < 1 || path_len >= sizeof path_buf) {
errno = ENOMEM;
return NULL;
}
while (1) {
exe_path = (char*)malloc(exe_size);
if (!exe_path) {
errno = ENOMEM;
return NULL;
}
exe_used = readlink(path_buf, exe_path, exe_size - 1);
if (exe_used == (ssize_t)-1) {
free(exe_path);
return NULL;
}
if (exe_used < (ssize_t)1) {
/* Race condition? */
errno = ENOENT;
free(exe_path);
return NULL;
}
if (exe_used < (ssize_t)(exe_size - 1))
break;
free(exe_path);
exe_size += 1024;
}
/* Try reallocating the exe_path to minimum size.
* This is optional, and can even fail without
* any bad effects. */
{
char *temp;
temp = (char*)realloc(exe_path, exe_used + 1);
if (temp) {
exe_path = temp;
exe_size = exe_used + 1;
}
}
if (sizeptr)
*sizeptr = exe_size;
if (lenptr)
*lenptr = exe_used;
exe_path[exe_used] = '\0';
return exe_path;
}
/**
* @brief explode Explode a string based on
* @param s The string to explode
* @param c The seperator to use
* @return A vector of the exploded string
*/
const std::vector<std::string> explode(const std::string& s, const char& c)
{
std::string buff{""};
std::vector<std::string> v;
for(auto n:s)
{
if(n != c) buff+=n; else
if(n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if(buff != "") v.push_back(buff);
return v;
}
/**
* @brief getProcessNameFromCmdLine Get the name of the process from its PID
* @param pid the pid of the process to get the name for
* @return The name of the process
*/
QString getProcessNameFromCmdLine(const pid_t pid)
{
std::string cmdline = getProcessCmdline(pid).toStdString();
if (cmdline.size()<1) {
return "";
}
// maintain linux paths
std::replace(cmdline.begin(),cmdline.end(),'\\','/');
auto args = explode(cmdline,' ');
QString name = QFileInfo(QString::fromStdString(args[0])).fileName();
// replace the name of some processes with their first argument, eg, python, php, ruby etc
// QString does not support hash
static std::unordered_set<QString> nameMap({"python", "python2", "python3", "ruby", "php", "perl"});
auto pos = nameMap.find(name);
if (pos != nameMap.end()) {
return QFileInfo(QString::fromStdString(args[1])).fileName();
} else {
return name;
}
}
/**
* @brief getProcessCmdline Get the command line that the process was executed with from its PID
* @param pid The pid of the process to get
* @return The command line that the process was run from
*/
QString getProcessCmdline(pid_t pid)
{
std::string temp;
try {
std::fstream fs;
fs.open("/proc/"+std::to_string((long)pid)+"/cmdline", std::fstream::in);
std::getline(fs,temp);
fs.close();
} catch(std::ifstream::failure e) {
return "FAILED TO READ PROC";
}
// change \0 to ' '
std::replace(temp.begin(),temp.end(),'\0',' ');
if (temp.size()<1) {
return "";
}
return QString::fromStdString(temp);
}
/**
* @brief getProcessName Get the name of the process from a proc_t
* @param p The proc_t structure to use for getting the name of the process
* @return
*/
QString getProcessName(proc_t* p)
{
QString processName = "ERROR";
char* temp = NULL;//exe_of(p->tid,NULL,NULL);
// if exe_of fails here, it will be because permission is denied
if (temp!=NULL) {
processName = QFileInfo(temp).fileName();
free(temp);
} else {
// next try to read from /proc/*//*cmdline
processName = getProcessNameFromCmdLine(p->tid);
if (processName=="") {
// fallback on /proc/*//*stat program name value
// bad because it is limited to 16 chars
processName = p->cmd;
}
}
return processName;
}
/**
* @brief getTotalCpuTime Read the data from /proc/stat and get the total time the cpu has been busy
* @return The total cpu time
*/
unsigned long long getTotalCpuTime()
{
// from https://github.com/scaidermern/top-processes/blob/master/top_proc.c#L54
FILE* file = fopen("/proc/stat", "r");
if (file == NULL) {
perror("Could not open stat file");
return 0;
}
char buffer[1024];
unsigned long long user = 0, nice = 0, system = 0, idle = 0;
// added between Linux 2.5.41 and 2.6.33, see man proc(5)
unsigned long long iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guestnice = 0;
char* ret = fgets(buffer, sizeof(buffer) - 1, file);
if (ret == NULL) {
perror("Could not read stat file");
fclose(file);
return 0;
}
fclose(file);
sscanf(buffer,
"cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu",
&user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guestnice);
// sum everything up (except guest and guestnice since they are already included
// in user and nice, see http://unix.stackexchange.com/q/178045/20626)
return user + nice + system + idle + iowait + irq + softirq + steal;
}
/**
* @brief calculateCPUPercentage
* @param before - old proc_t of the process
* @param after - new proc_t of the process
* @param cpuTime - the last total cpu time measurement
* @return The cpu percentage of the process
*/
double calculateCPUPercentage(const proc_t* before, const proc_t* after, const unsigned long long &cpuTime)
{
double cpuTimeA = getTotalCpuTime() - cpuTime;
unsigned long long processcpuTime = ((after->utime + after->stime)
- (before->utime + before->stime));
/// TODO: GSM has an option to divide by # cpus
return (processcpuTime / cpuTimeA) * 100.0 * sysconf(_SC_NPROCESSORS_CONF);
}
/**
* @brief getProcessStartDate Convert a timestamp into a date in the form that is used in the current locale
* @param start_time The timestamp
* @return A date string
*/
QString getProcessStartDate(unsigned long long start_time)
{
__time_t time = getbtime() + start_time/Hertz;
struct tm *tm = localtime(&time);
char date[255];
strftime(date, sizeof(date), "%Ex %ER", tm);
return QString(date);
}
/**
* @brief getProcessStatus Get the status of a process as a long string from its proc_t
* @param p The proc_t structure of the process
* @return The long string form of the process status
*/
QString getProcessStatus(proc_t* p)
{
switch(p->state) {
case 'S':
return "Sleeping";
break;
case 'R':
return "Running";
break;
case 'Z':
return "Zombie";
break;
case 'D':
return "Uninterruptible Sleep";
break;
case 'T':
return "Stopped";
break;
default:
return "Unknown state: '" + QString(p->state) + "'";
}
}
/**
* @brief getProcessIconFromName Get the icon for a process given its name
* @param procname The name of the process
* @return The process' icon or the default executable icon if none was found
*/
QIcon getProcessIconFromName(QString procName, std::unordered_map<QString, QIcon> &processIconMapCache)
{
// check we havent already got the icon in the cache
auto pos = processIconMapCache.find(procName);
if (pos != processIconMapCache.end()) {
return pos->second;
}
// apply some corrections to the process name
// ie, sh should look for terminal icons, not anything containing sh
static std::map<QString, QString> procNameCorrections({
{"sh","terminal"}, {"bash","terminal"}, {"dconf-service", "dconf"}, {"gconfd-2", "dconf"}, {"deja-dup-monitor", "deja-dup"}
});
auto procPos = procNameCorrections.find(procName);
if (procPos != procNameCorrections.end()) {
procName = procPos->second;
}
// search /usr/share/applications for the desktop file that corresponds to the proc and get its icon
QDirIterator dir("/usr/share/applications", QDirIterator::Subdirectories);
QString desktopFile;
QIcon defaultExecutableIcon = QIcon::fromTheme("application-x-executable");
while(dir.hasNext()) {
if (dir.fileInfo().suffix() == "desktop") {
if (dir.fileName().toLower().contains(procName.toLower())) {
desktopFile = dir.filePath();
break;
}
}
dir.next();
}
if (desktopFile.size() == 0) {
return defaultExecutableIcon;
}
QIcon icon = defaultExecutableIcon;
QString iconName;
QFile in(desktopFile);
in.open(QIODevice::ReadOnly);
while(!in.atEnd()) {
iconName = in.readLine().trimmed();
if (iconName.startsWith("Icon=")) {
iconName.remove(0,5); // remove the first 5 chars
} else {
continue;
}
if (iconName.contains("/")) {
// this is probably a path to the file, use that instead of the theme icon name
icon = QIcon(iconName);
} else {
icon = QIcon::fromTheme(iconName,defaultExecutableIcon);
break;
}
}
in.close();
processIconMapCache[procName] = icon;
return icon;
}
}