Debian Codegraph @

psutil/arch/sunos/proc.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include <Python.h>
8
9 #include <fcntl.h>
10 #include <libproc.h>
11
12 #include "../../arch/all/init.h"
13 #include "proc.h"
14 #include "environ.h"
15
16
17 // Read a file content and fills a C structure with it.
18 int
19 psutil_file_to_struct(char *path, void *fstruct, size_t size) {
20 int fd;
21 ssize_t nbytes;
22 fd = open(path, O_RDONLY);
23 if (fd == -1) {
24 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
25 return 0;
26 }
27 nbytes = read(fd, fstruct, size);
28 if (nbytes == -1) {
29 close(fd);
30 PyErr_SetFromErrno(PyExc_OSError);
31 return 0;
32 }
33 if (nbytes != (ssize_t) size) {
34 close(fd);
35 PyErr_SetString(
36 PyExc_RuntimeError, "read() file structure size mismatch");
37 return 0;
38 }
39 close(fd);
40 return nbytes;
41 }
42
43
44 /*
45 * Return process ppid, rss, vms, ctime, nice, nthreads, status and tty
46 * as a Python tuple.
47 */
48 PyObject *
49 psutil_proc_basic_info(PyObject *self, PyObject *args) {
50 int pid;
51 char path[1000];
52 psinfo_t info;
53 const char *procfs_path;
54
55 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
56 return NULL;
57
58 sprintf(path, "%s/%i/psinfo", procfs_path, pid);
59 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
60 return NULL;
61 return Py_BuildValue(
62 "ikkdiiikiiii",
63 info.pr_ppid, // parent pid
64 info.pr_rssize, // rss
65 info.pr_size, // vms
66 PSUTIL_TV2DOUBLE(info.pr_start), // create time
67 info.pr_lwp.pr_nice, // nice
68 info.pr_nlwp, // no. of threads
69 info.pr_lwp.pr_state, // status code
70 info.pr_ttydev, // tty nr
71 (int)info.pr_uid, // real user id
72 (int)info.pr_euid, // effective user id
73 (int)info.pr_gid, // real group id
74 (int)info.pr_egid // effective group id
75 );
76 }
77
78
79 /*
80 * Join array of C strings to C string with delimiter dm.
81 * Omit empty records.
82 */
83 static int
84 cstrings_array_to_string(char **joined, char ** array, size_t count, char dm) {
85 size_t i;
86 size_t total_length = 0;
87 size_t item_length = 0;
88 char *result = NULL;
89 char *last = NULL;
90
91 if (!array || !joined)
92 return 0;
93
94 for (i=0; i<count; i++) {
95 if (!array[i])
96 continue;
97
98 item_length = strlen(array[i]) + 1;
99 total_length += item_length;
100 }
101
102 if (!total_length) {
103 return 0;
104 }
105
106 result = malloc(total_length);
107 if (!result) {
108 PyErr_NoMemory();
109 return -1;
110 }
111
112 result[0] = '\0';
113 last = result;
114
115 for (i=0; i<count; i++) {
116 if (!array[i])
117 continue;
118
119 item_length = strlen(array[i]);
120 memcpy(last, array[i], item_length);
121 last[item_length] = dm;
122
123 last += item_length + 1;
124 }
125
126 result[total_length-1] = '\0';
127 *joined = result;
128
129 return total_length;
130 }
131
132 /*
133 * Return process name and args as a Python tuple.
134 */
135 PyObject *
136 psutil_proc_name_and_args(PyObject *self, PyObject *args) {
137 int pid;
138 char path[1000];
139 psinfo_t info;
140 size_t argc;
141 int joined;
142 char **argv;
143 char *argv_plain;
144 const char *procfs_path;
145 PyObject *py_name = NULL;
146 PyObject *py_args = NULL;
147 PyObject *py_retlist = NULL;
148
149 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
150 return NULL;
151 sprintf(path, "%s/%i/psinfo", procfs_path, pid);
152 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
153 return NULL;
154
155 py_name = PyUnicode_DecodeFSDefault(info.pr_fname);
156 if (!py_name)
157 goto error;
158
159 /* SunOS truncates arguments to length PRARGSZ, very likely args are truncated.
160 * The only way to retrieve full command line is to parse process memory */
161 if (info.pr_argc && strlen(info.pr_psargs) == PRARGSZ-1) {
162 argv = psutil_read_raw_args(info, procfs_path, &argc);
163 if (argv) {
164 joined = cstrings_array_to_string(&argv_plain, argv, argc, ' ');
165 if (joined > 0) {
166 py_args = PyUnicode_DecodeFSDefault(argv_plain);
167 free(argv_plain);
168 } else if (joined < 0) {
169 goto error;
170 }
171
172 psutil_free_cstrings_array(argv, argc);
173 }
174 }
175
176 /* If we can't read process memory or can't decode the result
177 * then return args from /proc. */
178 if (!py_args) {
179 PyErr_Clear();
180 py_args = PyUnicode_DecodeFSDefault(info.pr_psargs);
181 }
182
183 /* Both methods has been failed. */
184 if (!py_args)
185 goto error;
186
187 py_retlist = Py_BuildValue("OO", py_name, py_args);
188 if (!py_retlist)
189 goto error;
190
191 Py_DECREF(py_name);
192 Py_DECREF(py_args);
193 return py_retlist;
194
195 error:
196 Py_XDECREF(py_name);
197 Py_XDECREF(py_args);
198 Py_XDECREF(py_retlist);
199 return NULL;
200 }
201
202
203 /*
204 * Return process environ block.
205 */
206 PyObject *
207 psutil_proc_environ(PyObject *self, PyObject *args) {
208 int pid;
209 char path[1000];
210 psinfo_t info;
211 const char *procfs_path;
212 char **env = NULL;
213 ssize_t env_count = -1;
214 char *dm;
215 int i = 0;
216 PyObject *py_envname = NULL;
217 PyObject *py_envval = NULL;
218 PyObject *py_retdict = PyDict_New();
219
220 if (! py_retdict)
221 return PyErr_NoMemory();
222
223 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
224 return NULL;
225
226 sprintf(path, "%s/%i/psinfo", procfs_path, pid);
227 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
228 goto error;
229
230 if (! info.pr_envp) {
231 AccessDenied("/proc/pid/psinfo struct not set");
232 goto error;
233 }
234
235 env = psutil_read_raw_env(info, procfs_path, &env_count);
236 if (! env && env_count != 0)
237 goto error;
238
239 for (i=0; i<env_count; i++) {
240 if (! env[i])
241 break;
242
243 dm = strchr(env[i], '=');
244 if (! dm)
245 continue;
246
247 *dm = '\0';
248
249 py_envname = PyUnicode_DecodeFSDefault(env[i]);
250 if (! py_envname)
251 goto error;
252
253 py_envval = PyUnicode_DecodeFSDefault(dm+1);
254 if (! py_envname)
255 goto error;
256
257 if (PyDict_SetItem(py_retdict, py_envname, py_envval) < 0)
258 goto error;
259
260 Py_CLEAR(py_envname);
261 Py_CLEAR(py_envval);
262 }
263
264 psutil_free_cstrings_array(env, env_count);
265 return py_retdict;
266
267 error:
268 if (env && env_count >= 0)
269 psutil_free_cstrings_array(env, env_count);
270
271 Py_XDECREF(py_envname);
272 Py_XDECREF(py_envval);
273 Py_XDECREF(py_retdict);
274 return NULL;
275 }
276
277
278 /*
279 * Return process user and system CPU times as a Python tuple.
280 */
281 PyObject *
282 psutil_proc_cpu_times(PyObject *self, PyObject *args) {
283 int pid;
284 char path[1000];
285 pstatus_t info;
286 const char *procfs_path;
287
288 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
289 return NULL;
290 sprintf(path, "%s/%i/status", procfs_path, pid);
291 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
292 return NULL;
293 // results are more precise than os.times()
294 return Py_BuildValue(
295 "(dddd)",
296 PSUTIL_TV2DOUBLE(info.pr_utime),
297 PSUTIL_TV2DOUBLE(info.pr_stime),
298 PSUTIL_TV2DOUBLE(info.pr_cutime),
299 PSUTIL_TV2DOUBLE(info.pr_cstime)
300 );
301 }
302
303
304 /*
305 * Return what CPU the process is running on.
306 */
307 PyObject *
308 psutil_proc_cpu_num(PyObject *self, PyObject *args) {
309 int fd = -1;
310 int pid;
311 char path[1000];
312 struct prheader header;
313 struct lwpsinfo *lwp = NULL;
314 int nent;
315 int size;
316 int proc_num;
317 ssize_t nbytes;
318 const char *procfs_path;
319
320 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
321 return NULL;
322
323 sprintf(path, "%s/%i/lpsinfo", procfs_path, pid);
324 fd = open(path, O_RDONLY);
325 if (fd == -1) {
326 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
327 return NULL;
328 }
329
330 // read header
331 nbytes = pread(fd, &header, sizeof(header), 0);
332 if (nbytes == -1) {
333 PyErr_SetFromErrno(PyExc_OSError);
334 goto error;
335 }
336 if (nbytes != sizeof(header)) {
337 PyErr_SetString(
338 PyExc_RuntimeError, "read() file structure size mismatch");
339 goto error;
340 }
341
342 // malloc
343 nent = header.pr_nent;
344 size = header.pr_entsize * nent;
345 lwp = malloc(size);
346 if (lwp == NULL) {
347 PyErr_NoMemory();
348 goto error;
349 }
350
351 // read the rest
352 nbytes = pread(fd, lwp, size, sizeof(header));
353 if (nbytes == -1) {
354 PyErr_SetFromErrno(PyExc_OSError);
355 goto error;
356 }
357 if (nbytes != size) {
358 PyErr_SetString(
359 PyExc_RuntimeError, "read() file structure size mismatch");
360 goto error;
361 }
362
363 // done
364 proc_num = lwp->pr_onpro;
365 close(fd);
366 free(lwp);
367 return Py_BuildValue("i", proc_num);
368
369 error:
370 if (fd != -1)
371 close(fd);
372 free(lwp);
373 return NULL;
374 }
375
376
377 /*
378 * Return process uids/gids as a Python tuple.
379 */
380 PyObject *
381 psutil_proc_cred(PyObject *self, PyObject *args) {
382 int pid;
383 char path[1000];
384 prcred_t info;
385 const char *procfs_path;
386
387 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
388 return NULL;
389 sprintf(path, "%s/%i/cred", procfs_path, pid);
390 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
391 return NULL;
392 return Py_BuildValue("iiiiii",
393 info.pr_ruid, info.pr_euid, info.pr_suid,
394 info.pr_rgid, info.pr_egid, info.pr_sgid);
395 }
396
397
398 /*
399 * Return process voluntary and involuntary context switches as a Python tuple.
400 */
401 PyObject *
402 psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) {
403 int pid;
404 char path[1000];
405 prusage_t info;
406 const char *procfs_path;
407
408 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
409 return NULL;
410 sprintf(path, "%s/%i/usage", procfs_path, pid);
411 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
412 return NULL;
413 return Py_BuildValue("kk", info.pr_vctx, info.pr_ictx);
414 }
415
416
417 /*
418 * Process IO counters.
419 *
420 * Commented out and left here as a reminder. Apparently we cannot
421 * retrieve process IO stats because:
422 * - 'pr_ioch' is a sum of chars read and written, with no distinction
423 * - 'pr_inblk' and 'pr_oublk', which should be the number of bytes
424 * read and written, hardly increase and according to:
425 * http://www.brendangregg.com/Solaris/paper_diskubyp1.pdf
426 * ...they should be meaningless anyway.
427 *
428 PyObject*
429 proc_io_counters(PyObject* self, PyObject* args) {
430 int pid;
431 char path[1000];
432 prusage_t info;
433 const char *procfs_path;
434
435 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
436 return NULL;
437 sprintf(path, "%s/%i/usage", procfs_path, pid);
438 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
439 return NULL;
440
441 // On Solaris we only have 'pr_ioch' which accounts for bytes read
442 // *and* written.
443 // 'pr_inblk' and 'pr_oublk' should be expressed in blocks of
444 // 8KB according to:
445 // http://www.brendangregg.com/Solaris/paper_diskubyp1.pdf (pag. 8)
446 return Py_BuildValue("kkkk",
447 info.pr_ioch,
448 info.pr_ioch,
449 info.pr_inblk,
450 info.pr_oublk);
451 }
452 */
453
454
455 /*
456 * Return information about a given process thread.
457 */
458 PyObject *
459 psutil_proc_query_thread(PyObject *self, PyObject *args) {
460 int pid, tid;
461 char path[1000];
462 lwpstatus_t info;
463 const char *procfs_path;
464
465 if (! PyArg_ParseTuple(args, "iis", &pid, &tid, &procfs_path))
466 return NULL;
467 sprintf(path, "%s/%i/lwp/%i/lwpstatus", procfs_path, pid, tid);
468 if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
469 return NULL;
470 return Py_BuildValue("dd",
471 PSUTIL_TV2DOUBLE(info.pr_utime),
472 PSUTIL_TV2DOUBLE(info.pr_stime));
473 }
474
475
476 /*
477 * Return process memory mappings.
478 */
479 PyObject *
480 psutil_proc_memory_maps(PyObject *self, PyObject *args) {
481 int pid;
482 int fd = -1;
483 char path[1000];
484 char perms[10];
485 const char *name;
486 struct stat st;
487 pstatus_t status;
488
489 prxmap_t *xmap = NULL, *p;
490 off_t size;
491 size_t nread;
492 int nmap;
493 uintptr_t pr_addr_sz;
494 uintptr_t stk_base_sz, brk_base_sz;
495 const char *procfs_path;
496
497 PyObject *py_tuple = NULL;
498 PyObject *py_path = NULL;
499 PyObject *py_retlist = PyList_New(0);
500
501 if (py_retlist == NULL)
502 return NULL;
503 if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
504 goto error;
505
506 sprintf(path, "%s/%i/status", procfs_path, pid);
507 if (! psutil_file_to_struct(path, (void *)&status, sizeof(status)))
508 goto error;
509
510 sprintf(path, "%s/%i/xmap", procfs_path, pid);
511 if (stat(path, &st) == -1) {
512 PyErr_SetFromErrno(PyExc_OSError);
513 goto error;
514 }
515
516 size = st.st_size;
517
518 fd = open(path, O_RDONLY);
519 if (fd == -1) {
520 PyErr_SetFromErrno(PyExc_OSError);
521 goto error;
522 }
523
524 xmap = (prxmap_t *)malloc(size);
525 if (xmap == NULL) {
526 PyErr_NoMemory();
527 goto error;
528 }
529
530 nread = pread(fd, xmap, size, 0);
531 nmap = nread / sizeof(prxmap_t);
532 p = xmap;
533
534 while (nmap) {
535 nmap -= 1;
536 if (p == NULL) {
537 p += 1;
538 continue;
539 }
540
541 perms[0] = '\0';
542 pr_addr_sz = p->pr_vaddr + p->pr_size;
543
544 // perms
545 sprintf(perms, "%c%c%c%c", p->pr_mflags & MA_READ ? 'r' : '-',
546 p->pr_mflags & MA_WRITE ? 'w' : '-',
547 p->pr_mflags & MA_EXEC ? 'x' : '-',
548 p->pr_mflags & MA_SHARED ? 's' : '-');
549
550 // name
551 if (strlen(p->pr_mapname) > 0) {
552 name = p->pr_mapname;
553 }
554 else {
555 if ((p->pr_mflags & MA_ISM) || (p->pr_mflags & MA_SHM)) {
556 name = "[shmid]";
557 }
558 else {
559 stk_base_sz = status.pr_stkbase + status.pr_stksize;
560 brk_base_sz = status.pr_brkbase + status.pr_brksize;
561
562 if ((pr_addr_sz > status.pr_stkbase) &&
563 (p->pr_vaddr < stk_base_sz)) {
564 name = "[stack]";
565 }
566 else if ((p->pr_mflags & MA_ANON) && \
567 (pr_addr_sz > status.pr_brkbase) && \
568 (p->pr_vaddr < brk_base_sz)) {
569 name = "[heap]";
570 }
571 else {
572 name = "[anon]";
573 }
574 }
575 }
576
577 py_path = PyUnicode_DecodeFSDefault(name);
578 if (! py_path)
579 goto error;
580 py_tuple = Py_BuildValue(
581 "kksOkkk",
582 (unsigned long)p->pr_vaddr,
583 (unsigned long)pr_addr_sz,
584 perms,
585 py_path,
586 (unsigned long)p->pr_rss * p->pr_pagesize,
587 (unsigned long)p->pr_anon * p->pr_pagesize,
588 (unsigned long)p->pr_locked * p->pr_pagesize);
589 if (!py_tuple)
590 goto error;
591 if (PyList_Append(py_retlist, py_tuple))
592 goto error;
593 Py_CLEAR(py_path);
594 Py_CLEAR(py_tuple);
595
596 // increment pointer
597 p += 1;
598 }
599
600 close(fd);
601 free(xmap);
602 return py_retlist;
603
604 error:
605 if (fd != -1)
606 close(fd);
607 Py_XDECREF(py_tuple);
608 Py_XDECREF(py_path);
609 Py_DECREF(py_retlist);
610 if (xmap != NULL)
611 free(xmap);
612 return NULL;
613 }