forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerateProcesses.cpp
More file actions
152 lines (128 loc) · 2.72 KB
/
Copy pathEnumerateProcesses.cpp
File metadata and controls
152 lines (128 loc) · 2.72 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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <experimental/filesystem>
#include "NativeCore.hpp"
namespace fs = std::experimental::filesystem;
// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
#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 != (ssize_t)temp.size())
{
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));
ec.clear();
break;
}
}
}
return symlink_path;
}
#endif
bool is_number(const std::string& s)
{
auto it = s.begin();
for (; it != s.end() && std::isdigit(*it); ++it);
return !s.empty() && it == s.end();
}
template<typename T>
T parse_type(const std::string& s)
{
std::stringstream ss(s);
T val;
ss >> val;
return val;
}
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 EnumerateProcesses(EnumerateProcessCallback callbackProcess)
{
if (callbackProcess == nullptr)
{
return;
}
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 NATIVE_CORE_64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
EnumerateProcessData data = {};
data.Id = parse_type<size_t>(name);
MultiByteToUnicode(linkPath.c_str(), data.ModulePath, PATH_MAXIMUM_LENGTH);
callbackProcess(&data);
}
}
}
}
}
}
}
}