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
|
// Copyright 2020-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "HostLinux.h"
#include "mumble_positional_audio_utils.h"
#include <cstring>
#include <sstream>
#include <sys/uio.h>
HostLinux::HostLinux(const procid_t pid) : m_pid(pid) {
}
HostLinux::~HostLinux() {
}
bool HostLinux::peek(const procptr_t address, void *dst, const size_t size) const {
iovec in;
in.iov_base = reinterpret_cast< void * >(address);
in.iov_len = size;
iovec out;
out.iov_base = dst;
out.iov_len = size;
const auto ret = process_vm_readv(m_pid, &out, 1, &in, 1, 0);
return (ret != -1 && static_cast< size_t >(ret) == in.iov_len);
}
Modules HostLinux::modules() const {
std::ostringstream path;
path << "/proc/";
path << m_pid;
path << "/maps";
const auto maps = readFile(path.str());
if (maps.size() == 0) {
return {};
}
Modules modules;
std::string name;
std::stringstream stream(maps);
while (stream) {
auto ret = readUntil(stream, '-');
if (!ret.second) {
return modules;
}
MemoryRegion region;
region.address = std::stoull(ret.first, nullptr, 16);
ret = readUntil(stream, ' ');
if (!ret.second) {
return modules;
}
region.size = std::stoull(ret.first, nullptr, 16) - region.address;
ret = readUntil(stream, ' ');
if (!ret.second) {
return modules;
}
for (const auto character : ret.first) {
switch (character) {
case 'r':
region.readable = true;
break;
case 'w':
region.writable = true;
break;
case 'x':
region.executable = true;
break;
}
}
// Skip offset, dev and inode
for (uint8_t i = 0; i < 3; ++i) {
if (!skipUntil(stream, ' ', false)) {
return modules;
}
}
// Eat spaces until we're at the beginning of the path
if (!skipUntil(stream, ' ', true)) {
return modules;
}
// Throw up last eaten character
stream.unget();
ret = readUntil(stream, '\n');
if (!ret.second) {
return modules;
}
if (ret.first.size() == 0) {
// Anonymous region, let's add it to the module that owns the previous one
if (!name.empty()) {
auto iter = modules.find(name);
iter->second.addRegion(region);
}
continue;
}
// Only treat path as a real path if it starts with "/"
if (ret.first[0] != '/') {
continue;
}
const auto lastSlashPos = ret.first.find_last_of('/');
if (ret.first.size() > lastSlashPos + 1) {
name = ret.first.substr(lastSlashPos + 1);
const auto iter = modules.find(name);
if (iter != modules.cend()) {
iter->second.addRegion(region);
} else {
Module module(name);
module.addRegion(region);
modules.insert(std::make_pair(name, module));
}
}
}
return modules;
}
bool HostLinux::isWine(const procid_t id) {
std::ostringstream ss;
ss << "/proc/";
ss << id;
ss << "/exe";
char *path = realpath(ss.str().c_str(), nullptr);
if (!path) {
return false;
}
const char *filename = basename(path);
if (!filename) {
return false;
}
const auto ret = strcmp(filename, "wine-preloader") == 0 || strcmp(filename, "wine64-preloader") == 0;
// basename() returns a pointer to the basename's position in the string passed as argument.
// For that reason we cannot free 'path' before performing the check.
free(path);
return ret;
}
|