-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocess.hpp
More file actions
48 lines (33 loc) · 1010 Bytes
/
Copy pathprocess.hpp
File metadata and controls
48 lines (33 loc) · 1010 Bytes
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
#ifndef PROCESS_HPP
#define PROCESS_HPP
#include <string>
#include <dirent.h>
#include "file.hpp"
namespace Process {
static pid_t name_to_pid(std::string proc_name) {
if (proc_name.length() == 0)
return 0;
//Try to open the /proc/ directory
DIR *pDir = opendir("/proc/");
if (pDir == nullptr) { return -1; } // if that fails then exit
dirent* pDirent = nullptr;
//basically loop through all the processes
while (pDirent = readdir(pDir), pDirent != nullptr) {
//Skip non processes
if (pDirent->d_type != DT_DIR) {
continue;
}
//Basically contains the process ID
std::string str_proc_id = pDirent->d_name;
//Get the name of the processes
std::string str_comm = File::str_read("/proc/" + str_proc_id + "/comm");
//Check if the currently iterated processes is the one we want
if (str_comm == proc_name) {
pid_t pid = (pid_t) atoi(str_proc_id.c_str());
return pid;
}
}
return 0;
}
}
#endif