-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathEnumerateProcesses.cpp
More file actions
193 lines (166 loc) · 4.55 KB
/
EnumerateProcesses.cpp
File metadata and controls
193 lines (166 loc) · 4.55 KB
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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#ifdef __linux__
#include <experimental/filesystem>
#elif __APPLE__
#include <sys/proc_info.h>
#include <libproc.h>
#endif
#include "NativeCore.hpp"
#ifdef __linux__
namespace fs = std::experimental::filesystem;
#endif
// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
#ifdef __linux__
#define USE_CUSTOM_READ_SYMLINK
#ifdef USE_CUSTOM_READ_SYMLINK
#include <unistd.h>
fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
{
fs::path symlink_path;
std::string temp(64, '\0');
for (;; temp.resize(temp.size() * 2))
{
ssize_t result;
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
{
ec.assign(errno, std::system_category());
break;
}
else
{
if (result != static_cast<ssize_t>(temp.size()))
{
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));
ec.clear();
break;
}
}
}
return symlink_path;
}
#endif
#endif
enum class Platform
{
Unknown,
X86,
X64
};
Platform GetProcessPlatform(const std::string& auxvPath)
{
auto platform = Platform::Unknown;
std::ifstream file(auxvPath);
if (file)
{
char buffer[16];
while (true)
{
file.read(buffer, 16);
if (!file)
{
return Platform::X64;
}
if (buffer[4] != 0 || buffer[5] != 0 || buffer[6] != 0 || buffer[7] != 0)
{
return Platform::X86;
}
}
}
return platform;
}
extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callbackProcess)
{
if (callbackProcess == nullptr)
{
return;
}
#ifdef __linux__
fs::path procPath("/proc");
if (fs::is_directory(procPath))
{
for (auto& d : fs::directory_iterator(procPath))
{
if (fs::is_directory(d))
{
auto processPath = d.path();
auto name = processPath.filename().string();
if (is_number(name))
{
auto exeSymLink = processPath / "exe";
if (fs::is_symlink(fs::symlink_status(exeSymLink)))
{
std::error_code ec;
auto linkPath =
#ifdef USE_CUSTOM_READ_SYMLINK
my_read_symlink
#else
read_symlink
#endif
(exeSymLink, ec).string();
if (!ec)
{
auto auxvPath = processPath / "auxv";
auto platform = GetProcessPlatform(auxvPath.string());
#ifdef RECLASSNET64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
EnumerateProcessData data = {};
data.Id = parse_type<size_t>(name);
MultiByteToUnicode(linkPath.c_str(), data.Path, PATH_MAXIMUM_LENGTH);
const auto name = fs::path(data.Path).filename().u16string();
str16cpy(data.Name, name.c_str(), std::min<int>(name.length(), PATH_MAXIMUM_LENGTH - 1));
callbackProcess(&data);
}
}
}
}
}
}
}
#elif __APPLE__
int procCnt = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
pid_t pids[1024];
memset(pids, 0, sizeof pids);
proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
for (int i = 0; i < procCnt; i++)
{
if (!pids[i]) continue;
char curPath[PROC_PIDPATHINFO_MAXSIZE];
char curName[PROC_PIDPATHINFO_MAXSIZE];
memset(curPath, 0, sizeof curPath);
proc_pidpath(pids[i], curPath, sizeof curPath);
int len = strlen(curPath);
if (len)
{
int pos = len;
while (pos && curPath[pos] != '/') --pos;
strcpy(curName, curPath + pos + 1);
struct proc_bsdinfo bsd_info;
int error = proc_pidinfo (pids[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
if (error == 0)
continue;
auto platform = Platform::X86;
if (bsd_info.pbi_flags & PROC_FLAG_LP64)
platform = Platform::X64;
#ifdef RECLASSNET64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
EnumerateProcessData data = {};
data.Id = (size_t)pids[i];
MultiByteToUnicode(curPath, data.Path, PATH_MAXIMUM_LENGTH);
MultiByteToUnicode(curName, data.Name, PATH_MAXIMUM_LENGTH);
callbackProcess(&data);
}
}
}
#endif
}