blob: 2d74a1fcfe9df00fc6fe42d32f37a0551e5f5c14 (
plain)
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
|
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#include <tlhelp32.h>
#endif
#include "proc.h"
pid_t getPid(const char* process_name)
{
#if defined(__linux__) || defined(__FreeBSD__)
#ifdef __FreeBSD__
// on FreeBSD /proc is not mounted by default
if (!isDir("/proc"))
return -1;
#endif
pid_t pid;
char buf[PATH_MAX];
struct dirent* ent;
DIR* proc = opendir("/proc");
FILE* stat;
if (proc)
{
while ((ent = readdir(proc)) != NULL)
{
long lpid = atol(ent->d_name);
if (!lpid) continue;
snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
stat = fopen(buf, "r");
if (stat && (fscanf(stat, "%i (%[^)])", &pid, buf)) == 2)
{
if (!strcmp(buf, process_name))
{
fclose(stat);
closedir(proc);
return pid;
}
fclose(stat);
}
}
closedir(proc);
}
#elif _WIN32
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap,&pe32);
while(Process32Next(hSnap,&pe32))
{
if (!strcmp(pe32.szExeFile, process_name))
return (long)pe32.th32ProcessID;
}
#else
#error No Implementation
#endif
return -1;
}
|