-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
environ.c
405 lines (329 loc) · 9.96 KB
/
environ.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 2009, Giampaolo Rodola', Oleksii Shevchuk.
* All rights reserved. Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*
* Functions specific for Process.environ().
*/
#define _STRUCTURED_PROC 1
#include <Python.h>
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
#undef _FILE_OFFSET_BITS
#undef _LARGEFILE64_SOURCE
#endif
#include <sys/types.h>
#include <sys/procfs.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "environ.h"
#define STRING_SEARCH_BUF_SIZE 512
/*
* Open address space of specified process and return file descriptor.
* @param pid a pid of process.
* @param procfs_path a path to mounted procfs filesystem.
* @return file descriptor or -1 in case of error.
*/
static int
open_address_space(pid_t pid, const char *procfs_path) {
int fd;
char proc_path[PATH_MAX];
snprintf(proc_path, PATH_MAX, "%s/%i/as", procfs_path, pid);
fd = open(proc_path, O_RDONLY);
if (fd < 0)
PyErr_SetFromErrno(PyExc_OSError);
return fd;
}
/*
* Read chunk of data by offset to specified buffer of the same size.
* @param fd a file descriptor.
* @param offset an required offset in file.
* @param buf a buffer where to store result.
* @param buf_size a size of buffer where data will be stored.
* @return amount of bytes stored to the buffer or -1 in case of
* error.
*/
static size_t
read_offt(int fd, off_t offset, char *buf, size_t buf_size) {
size_t to_read = buf_size;
size_t stored = 0;
int r;
while (to_read) {
r = pread(fd, buf + stored, to_read, offset + stored);
if (r < 0)
goto error;
else if (r == 0)
break;
to_read -= r;
stored += r;
}
return stored;
error:
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
/*
* Read null-terminated string from file descriptor starting from
* specified offset.
* @param fd a file descriptor of opened address space.
* @param offset an offset in specified file descriptor.
* @return allocated null-terminated string or NULL in case of error.
*/
static char *
read_cstring_offt(int fd, off_t offset) {
int r;
int i = 0;
off_t end = offset;
size_t len;
char buf[STRING_SEARCH_BUF_SIZE];
char *result = NULL;
if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
// Search end of string
for (;;) {
r = read(fd, buf, sizeof(buf));
if (r == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
else if (r == 0) {
break;
}
else {
for (i=0; i<r; i++)
if (! buf[i])
goto found;
}
end += r;
}
found:
len = end + i - offset;
result = malloc(len+1);
if (! result) {
PyErr_NoMemory();
goto error;
}
if (len) {
if (read_offt(fd, offset, result, len) < 0) {
goto error;
}
}
result[len] = '\0';
return result;
error:
if (result)
free(result);
return NULL;
}
/*
* Read block of addresses by offset, dereference them one by one
* and create an array of null terminated C strings from them.
* @param fd a file descriptor of address space of interesting process.
* @param offset an offset of address block in address space.
* @param ptr_size a size of pointer. Only 4 or 8 are valid values.
* @param count amount of pointers in block.
* @return allocated array of strings dereferenced and read by offset.
* Number of elements in array are count. In case of error function
* returns NULL.
*/
static char **
read_cstrings_block(int fd, off_t offset, size_t ptr_size, size_t count) {
char **result = NULL;
char *pblock = NULL;
size_t pblock_size;
size_t i;
assert(ptr_size == 4 || ptr_size == 8);
if (!count)
goto error;
pblock_size = ptr_size * count;
pblock = malloc(pblock_size);
if (! pblock) {
PyErr_NoMemory();
goto error;
}
if (read_offt(fd, offset, pblock, pblock_size) != pblock_size)
goto error;
result = (char **) calloc(count, sizeof(char *));
if (! result) {
PyErr_NoMemory();
goto error;
}
for (i=0; i<count; i++) {
result[i] = read_cstring_offt(
fd, (ptr_size == 4?
((uint32_t *) pblock)[i]:
((uint64_t *) pblock)[i]));
if (!result[i])
goto error;
}
free(pblock);
return result;
error:
if (result)
psutil_free_cstrings_array(result, i);
if (pblock)
free(pblock);
return NULL;
}
/*
* Check that caller process can extract proper values from psinfo_t
* structure.
* @param info a pointer to process info (psinfo_t) structure of the
* interesting process.
* @return 1 in case if caller process can extract proper values from
* psinfo_t structure, or 0 otherwise.
*/
static inline int
is_ptr_dereference_possible(psinfo_t info) {
#if !defined(_LP64)
return info.pr_dmodel == PR_MODEL_ILP32;
#else
return 1;
#endif
}
/*
* Return pointer size according to psinfo_t structure
* @param info a pointer to process info (psinfo_t) structure of the
* interesting process.
* @return pointer size (4 or 8).
*/
static inline int
ptr_size_by_psinfo(psinfo_t info) {
return info.pr_dmodel == PR_MODEL_ILP32? 4 : 8;
}
/*
* Count amount of pointers in a block which ends with NULL.
* @param fd a descriptor of /proc/PID/as special file.
* @param offt an offset of block of pointers at the file.
* @param ptr_size a pointer size (allowed values: {4, 8}).
* @return amount of non-NULL pointers or -1 in case of error.
*/
static int
search_pointers_vector_size_offt(int fd, off_t offt, size_t ptr_size) {
int count = 0;
size_t r;
char buf[8];
static const char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
assert(ptr_size == 4 || ptr_size == 8);
if (lseek(fd, offt, SEEK_SET) == (off_t)-1)
goto error;
for (;; count ++) {
r = read(fd, buf, ptr_size);
if (r < 0)
goto error;
if (r == 0)
break;
if (r != ptr_size) {
PyErr_SetString(
PyExc_RuntimeError, "pointer block is truncated");
return -1;
}
if (! memcmp(buf, zeros, ptr_size))
break;
}
return count;
error:
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
/*
* Dereference and read array of strings by psinfo_t.pr_argv pointer from
* remote process.
* @param info a pointer to process info (psinfo_t) structure of the
* interesting process
* @param procfs_path a cstring with path to mounted procfs filesystem.
* @param count a pointer to variable where to store amount of elements in
* returned array. In case of error value of variable will not be
changed.
* @return allocated array of cstrings or NULL in case of error.
*/
char **
psutil_read_raw_args(psinfo_t info, const char *procfs_path, size_t *count) {
int as;
char **result;
if (! is_ptr_dereference_possible(info)) {
PyErr_SetString(
PyExc_NotImplementedError,
"can't get env of a 64 bit process from a 32 bit process");
return NULL;
}
if (! (info.pr_argv && info.pr_argc)) {
PyErr_SetString(
PyExc_RuntimeError, "process doesn't have arguments block");
return NULL;
}
as = open_address_space(info.pr_pid, procfs_path);
if (as < 0)
return NULL;
result = read_cstrings_block(
as, info.pr_argv, ptr_size_by_psinfo(info), info.pr_argc
);
if (result && count)
*count = info.pr_argc;
close(as);
return result;
}
/*
* Dereference and read array of strings by psinfo_t.pr_envp pointer
* from remote process.
* @param info a pointer to process info (psinfo_t) structure of the
* interesting process.
* @param procfs_path a cstring with path to mounted procfs filesystem.
* @param count a pointer to variable where to store amount of elements in
* returned array. In case of error value of variable will not be
* changed. To detect special case (described later) variable should be
* initialized by -1 or other negative value.
* @return allocated array of cstrings or NULL in case of error.
* Special case: count set to 0, return NULL.
* Special case means there is no error acquired, but no data
* retrieved.
* Special case exists because the nature of the process. From the
* beginning it's not clean how many pointers in envp array. Also
* situation when environment is empty is common for kernel processes.
*/
char **
psutil_read_raw_env(psinfo_t info, const char *procfs_path, ssize_t *count) {
int as;
int env_count;
int ptr_size;
char **result = NULL;
if (! is_ptr_dereference_possible(info)) {
PyErr_SetString(
PyExc_NotImplementedError,
"can't get env of a 64 bit process from a 32 bit process");
return NULL;
}
as = open_address_space(info.pr_pid, procfs_path);
if (as < 0)
return NULL;
ptr_size = ptr_size_by_psinfo(info);
env_count = search_pointers_vector_size_offt(
as, info.pr_envp, ptr_size);
if (env_count >= 0 && count)
*count = env_count;
if (env_count > 0)
result = read_cstrings_block(
as, info.pr_envp, ptr_size, env_count);
close(as);
return result;
}
/*
* Free array of cstrings.
* @param array an array of cstrings returned by psutil_read_raw_env,
* psutil_read_raw_args or any other function.
* @param count a count of strings in the passed array
*/
void
psutil_free_cstrings_array(char **array, size_t count) {
size_t i;
if (!array)
return;
for (i=0; i<count; i++) {
if (array[i]) {
free(array[i]);
}
}
free(array);
}