Repository navigation
Expand file tree
/
Copy pathEnumerateProcesses.cpp
More file actions
85 lines (69 loc) · 1.55 KB
/
Copy pathEnumerateProcesses.cpp
File metadata and controls
85 lines (69 loc) · 1.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
//#include <experimental/filesystem>
#include <boost/filesystem.hpp>
#include <sstream>
#include "NativeCore.hpp"
//using fs = std::experimental::filesystem;
//using sys = std;
/*using fs = boost::filesystem;
using sys = boost::system;*/
enum class Platform
{
Unknown,
X86,
X64
};
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (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;
}
extern "C" void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
{
//using namespace std::experimental::filesystem;
//using namespace std;
using namespace boost::filesystem;
using namespace boost::system;
if (callbackProcess == nullptr)
{
return;
}
path proc("/proc");
if (is_directory(proc))
{
for (auto& p : directory_iterator(proc))
{
if (is_directory(p))
{
auto processPath = p.path();
auto name = processPath.filename().string();
if (is_number(name))
{
size_t pid = parse_type<size_t>(name);
auto exeSymLink = processPath / "exe";
if (is_symlink(symlink_status(exeSymLink)))
{
error_code ec;
auto e = read_symlink(exeSymLink, ec);
if (!ec)
{
//auto elfHeader = processPath / "";
EnumerateProcessData data = {};
data.Id = pid;
MultiByteToUnicode(e.string().c_str(), data.ModulePath, PATH_MAXIMUM_LENGTH);
callbackProcess(&data);
}
}
}
}
}
}
}