| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola', Oleksii Shevchuk. |
| 3 |
* All rights reserved. Use of this source code is governed by a BSD-style |
| 4 |
* license that can be found in the LICENSE file. |
| 5 |
* |
| 6 |
* Functions specific for Process.environ(). |
| 7 |
*/ |
| 8 |
|
| 9 |
#define _STRUCTURED_PROC 1 |
| 10 |
|
| 11 |
#include <Python.h> |
| 12 |
|
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/procfs.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
#include <fcntl.h> |
| 17 |
#include <unistd.h> |
| 18 |
|
| 19 |
#include "../../arch/all/init.h" |
| 20 |
#include "environ.h" |
| 21 |
|
| 22 |
|
| 23 |
#define STRING_SEARCH_BUF_SIZE 512 |
| 24 |
|
| 25 |
|
| 26 |
/* |
| 27 |
* Open address space of specified process and return file descriptor. |
| 28 |
* @param pid a pid of process. |
| 29 |
* @param procfs_path a path to mounted procfs filesystem. |
| 30 |
* @return file descriptor or -1 in case of error. |
| 31 |
*/ |
| 32 |
static int |
| 33 |
open_address_space(pid_t pid, const char *procfs_path) { |
| 34 |
int fd; |
| 35 |
char proc_path[PATH_MAX]; |
| 36 |
|
| 37 |
snprintf(proc_path, PATH_MAX, "%s/%i/as", procfs_path, pid); |
| 38 |
fd = open(proc_path, O_RDONLY); |
| 39 |
if (fd < 0) |
| 40 |
PyErr_SetFromErrno(PyExc_OSError); |
| 41 |
|
| 42 |
return fd; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/* |
| 47 |
* Read chunk of data by offset to specified buffer of the same size. |
| 48 |
* @param fd a file descriptor. |
| 49 |
* @param offset an required offset in file. |
| 50 |
* @param buf a buffer where to store result. |
| 51 |
* @param buf_size a size of buffer where data will be stored. |
| 52 |
* @return amount of bytes stored to the buffer or -1 in case of |
| 53 |
* error. |
| 54 |
*/ |
| 55 |
static size_t |
| 56 |
read_offt(int fd, off_t offset, char *buf, size_t buf_size) { |
| 57 |
size_t to_read = buf_size; |
| 58 |
size_t stored = 0; |
| 59 |
int r; |
| 60 |
|
| 61 |
while (to_read) { |
| 62 |
r = pread(fd, buf + stored, to_read, offset + stored); |
| 63 |
if (r < 0) |
| 64 |
goto error; |
| 65 |
else if (r == 0) |
| 66 |
break; |
| 67 |
|
| 68 |
to_read -= r; |
| 69 |
stored += r; |
| 70 |
} |
| 71 |
|
| 72 |
return stored; |
| 73 |
|
| 74 |
error: |
| 75 |
PyErr_SetFromErrno(PyExc_OSError); |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/* |
| 81 |
* Read null-terminated string from file descriptor starting from |
| 82 |
* specified offset. |
| 83 |
* @param fd a file descriptor of opened address space. |
| 84 |
* @param offset an offset in specified file descriptor. |
| 85 |
* @return allocated null-terminated string or NULL in case of error. |
| 86 |
*/ |
| 87 |
static char * |
| 88 |
read_cstring_offt(int fd, off_t offset) { |
| 89 |
int r; |
| 90 |
int i = 0; |
| 91 |
off_t end = offset; |
| 92 |
size_t len; |
| 93 |
char buf[STRING_SEARCH_BUF_SIZE]; |
| 94 |
char *result = NULL; |
| 95 |
|
| 96 |
if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { |
| 97 |
PyErr_SetFromErrno(PyExc_OSError); |
| 98 |
goto error; |
| 99 |
} |
| 100 |
|
| 101 |
// Search end of string |
| 102 |
for (;;) { |
| 103 |
r = read(fd, buf, sizeof(buf)); |
| 104 |
if (r == -1) { |
| 105 |
PyErr_SetFromErrno(PyExc_OSError); |
| 106 |
goto error; |
| 107 |
} |
| 108 |
else if (r == 0) { |
| 109 |
break; |
| 110 |
} |
| 111 |
else { |
| 112 |
for (i=0; i<r; i++) |
| 113 |
if (! buf[i]) |
| 114 |
goto found; |
| 115 |
} |
| 116 |
|
| 117 |
end += r; |
| 118 |
} |
| 119 |
|
| 120 |
found: |
| 121 |
len = end + i - offset; |
| 122 |
|
| 123 |
result = malloc(len+1); |
| 124 |
if (! result) { |
| 125 |
PyErr_NoMemory(); |
| 126 |
goto error; |
| 127 |
} |
| 128 |
|
| 129 |
if (len) { |
| 130 |
if (read_offt(fd, offset, result, len) < 0) { |
| 131 |
goto error; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
result[len] = '\0'; |
| 136 |
return result; |
| 137 |
|
| 138 |
error: |
| 139 |
if (result) |
| 140 |
free(result); |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/* |
| 146 |
* Read block of addresses by offset, dereference them one by one |
| 147 |
* and create an array of null terminated C strings from them. |
| 148 |
* @param fd a file descriptor of address space of interesting process. |
| 149 |
* @param offset an offset of address block in address space. |
| 150 |
* @param ptr_size a size of pointer. Only 4 or 8 are valid values. |
| 151 |
* @param count amount of pointers in block. |
| 152 |
* @return allocated array of strings dereferenced and read by offset. |
| 153 |
* Number of elements in array are count. In case of error function |
| 154 |
* returns NULL. |
| 155 |
*/ |
| 156 |
static char ** |
| 157 |
read_cstrings_block(int fd, off_t offset, size_t ptr_size, size_t count) { |
| 158 |
char **result = NULL; |
| 159 |
char *pblock = NULL; |
| 160 |
size_t pblock_size; |
| 161 |
size_t i; |
| 162 |
|
| 163 |
assert(ptr_size == 4 || ptr_size == 8); |
| 164 |
|
| 165 |
if (!count) |
| 166 |
goto error; |
| 167 |
|
| 168 |
pblock_size = ptr_size * count; |
| 169 |
|
| 170 |
pblock = malloc(pblock_size); |
| 171 |
if (! pblock) { |
| 172 |
PyErr_NoMemory(); |
| 173 |
goto error; |
| 174 |
} |
| 175 |
|
| 176 |
if (read_offt(fd, offset, pblock, pblock_size) != pblock_size) |
| 177 |
goto error; |
| 178 |
|
| 179 |
result = (char **) calloc(count, sizeof(char *)); |
| 180 |
if (! result) { |
| 181 |
PyErr_NoMemory(); |
| 182 |
goto error; |
| 183 |
} |
| 184 |
|
| 185 |
for (i=0; i<count; i++) { |
| 186 |
result[i] = read_cstring_offt( |
| 187 |
fd, (ptr_size == 4? |
| 188 |
((uint32_t *) pblock)[i]: |
| 189 |
((uint64_t *) pblock)[i])); |
| 190 |
|
| 191 |
if (!result[i]) |
| 192 |
goto error; |
| 193 |
} |
| 194 |
|
| 195 |
free(pblock); |
| 196 |
return result; |
| 197 |
|
| 198 |
error: |
| 199 |
if (result) |
| 200 |
psutil_free_cstrings_array(result, i); |
| 201 |
if (pblock) |
| 202 |
free(pblock); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/* |
| 208 |
* Check that caller process can extract proper values from psinfo_t |
| 209 |
* structure. |
| 210 |
* @param info a pointer to process info (psinfo_t) structure of the |
| 211 |
* interesting process. |
| 212 |
* @return 1 in case if caller process can extract proper values from |
| 213 |
* psinfo_t structure, or 0 otherwise. |
| 214 |
*/ |
| 215 |
static inline int |
| 216 |
is_ptr_dereference_possible(psinfo_t info) { |
| 217 |
#if !defined(_LP64) |
| 218 |
return info.pr_dmodel == PR_MODEL_ILP32; |
| 219 |
#else |
| 220 |
return 1; |
| 221 |
#endif |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
/* |
| 226 |
* Return pointer size according to psinfo_t structure |
| 227 |
* @param info a pointer to process info (psinfo_t) structure of the |
| 228 |
* interesting process. |
| 229 |
* @return pointer size (4 or 8). |
| 230 |
*/ |
| 231 |
static inline int |
| 232 |
ptr_size_by_psinfo(psinfo_t info) { |
| 233 |
return info.pr_dmodel == PR_MODEL_ILP32? 4 : 8; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
/* |
| 238 |
* Count amount of pointers in a block which ends with NULL. |
| 239 |
* @param fd a descriptor of /proc/PID/as special file. |
| 240 |
* @param offt an offset of block of pointers at the file. |
| 241 |
* @param ptr_size a pointer size (allowed values: {4, 8}). |
| 242 |
* @return amount of non-NULL pointers or -1 in case of error. |
| 243 |
*/ |
| 244 |
static int |
| 245 |
search_pointers_vector_size_offt(int fd, off_t offt, size_t ptr_size) { |
| 246 |
int count = 0; |
| 247 |
size_t r; |
| 248 |
char buf[8]; |
| 249 |
static const char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 250 |
|
| 251 |
assert(ptr_size == 4 || ptr_size == 8); |
| 252 |
|
| 253 |
if (lseek(fd, offt, SEEK_SET) == (off_t)-1) |
| 254 |
goto error; |
| 255 |
|
| 256 |
for (;; count ++) { |
| 257 |
r = read(fd, buf, ptr_size); |
| 258 |
|
| 259 |
if (r < 0) |
| 260 |
goto error; |
| 261 |
|
| 262 |
if (r == 0) |
| 263 |
break; |
| 264 |
|
| 265 |
if (r != ptr_size) { |
| 266 |
PyErr_SetString( |
| 267 |
PyExc_RuntimeError, "pointer block is truncated"); |
| 268 |
return -1; |
| 269 |
} |
| 270 |
|
| 271 |
if (! memcmp(buf, zeros, ptr_size)) |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
return count; |
| 276 |
|
| 277 |
error: |
| 278 |
PyErr_SetFromErrno(PyExc_OSError); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/* |
| 284 |
* Dereference and read array of strings by psinfo_t.pr_argv pointer from |
| 285 |
* remote process. |
| 286 |
* @param info a pointer to process info (psinfo_t) structure of the |
| 287 |
* interesting process |
| 288 |
* @param procfs_path a cstring with path to mounted procfs filesystem. |
| 289 |
* @param count a pointer to variable where to store amount of elements in |
| 290 |
* returned array. In case of error value of variable will not be |
| 291 |
changed. |
| 292 |
* @return allocated array of cstrings or NULL in case of error. |
| 293 |
*/ |
| 294 |
char ** |
| 295 |
psutil_read_raw_args(psinfo_t info, const char *procfs_path, size_t *count) { |
| 296 |
int as; |
| 297 |
char **result; |
| 298 |
|
| 299 |
if (! is_ptr_dereference_possible(info)) { |
| 300 |
PyErr_SetString( |
| 301 |
PyExc_NotImplementedError, |
| 302 |
"can't get env of a 64 bit process from a 32 bit process"); |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
if (! (info.pr_argv && info.pr_argc)) { |
| 307 |
PyErr_SetString( |
| 308 |
PyExc_RuntimeError, "process doesn't have arguments block"); |
| 309 |
|
| 310 |
return NULL; |
| 311 |
} |
| 312 |
|
| 313 |
as = open_address_space(info.pr_pid, procfs_path); |
| 314 |
if (as < 0) |
| 315 |
return NULL; |
| 316 |
|
| 317 |
result = read_cstrings_block( |
| 318 |
as, info.pr_argv, ptr_size_by_psinfo(info), info.pr_argc |
| 319 |
); |
| 320 |
|
| 321 |
if (result && count) |
| 322 |
*count = info.pr_argc; |
| 323 |
|
| 324 |
close(as); |
| 325 |
|
| 326 |
return result; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/* |
| 331 |
* Dereference and read array of strings by psinfo_t.pr_envp pointer |
| 332 |
* from remote process. |
| 333 |
* @param info a pointer to process info (psinfo_t) structure of the |
| 334 |
* interesting process. |
| 335 |
* @param procfs_path a cstring with path to mounted procfs filesystem. |
| 336 |
* @param count a pointer to variable where to store amount of elements in |
| 337 |
* returned array. In case of error value of variable will not be |
| 338 |
* changed. To detect special case (described later) variable should be |
| 339 |
* initialized by -1 or other negative value. |
| 340 |
* @return allocated array of cstrings or NULL in case of error. |
| 341 |
* Special case: count set to 0, return NULL. |
| 342 |
* Special case means there is no error acquired, but no data |
| 343 |
* retrieved. |
| 344 |
* Special case exists because the nature of the process. From the |
| 345 |
* beginning it's not clean how many pointers in envp array. Also |
| 346 |
* situation when environment is empty is common for kernel processes. |
| 347 |
*/ |
| 348 |
char ** |
| 349 |
psutil_read_raw_env(psinfo_t info, const char *procfs_path, ssize_t *count) { |
| 350 |
int as; |
| 351 |
int env_count; |
| 352 |
int ptr_size; |
| 353 |
char **result = NULL; |
| 354 |
|
| 355 |
if (! is_ptr_dereference_possible(info)) { |
| 356 |
PyErr_SetString( |
| 357 |
PyExc_NotImplementedError, |
| 358 |
"can't get env of a 64 bit process from a 32 bit process"); |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
as = open_address_space(info.pr_pid, procfs_path); |
| 363 |
if (as < 0) |
| 364 |
return NULL; |
| 365 |
|
| 366 |
ptr_size = ptr_size_by_psinfo(info); |
| 367 |
|
| 368 |
env_count = search_pointers_vector_size_offt( |
| 369 |
as, info.pr_envp, ptr_size); |
| 370 |
|
| 371 |
if (env_count >= 0 && count) |
| 372 |
*count = env_count; |
| 373 |
|
| 374 |
if (env_count > 0) |
| 375 |
result = read_cstrings_block( |
| 376 |
as, info.pr_envp, ptr_size, env_count); |
| 377 |
|
| 378 |
close(as); |
| 379 |
return result; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
/* |
| 384 |
* Free array of cstrings. |
| 385 |
* @param array an array of cstrings returned by psutil_read_raw_env, |
| 386 |
* psutil_read_raw_args or any other function. |
| 387 |
* @param count a count of strings in the passed array |
| 388 |
*/ |
| 389 |
void |
| 390 |
psutil_free_cstrings_array(char **array, size_t count) { |
| 391 |
size_t i; |
| 392 |
|
| 393 |
if (!array) |
| 394 |
return; |
| 395 |
for (i=0; i<count; i++) { |
| 396 |
if (array[i]) { |
| 397 |
free(array[i]); |
| 398 |
} |
| 399 |
} |
| 400 |
free(array); |
| 401 |
} |