Debian Codegraph @

psutil/arch/aix/common.c

raw
1 /*
2 * Copyright (c) 2017, Arnon Yaari
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include <Python.h>
9 #include <sys/core.h>
10 #include <stdlib.h>
11 #include "common.h"
12
13 /* psutil_kread() - read from kernel memory */
14 int
15 psutil_kread(
16 int Kd, /* kernel memory file descriptor */
17 KA_T addr, /* kernel memory address */
18 char *buf, /* buffer to receive data */
19 size_t len) { /* length to read */
20 int br;
21
22 if (lseek64(Kd, (off64_t)addr, L_SET) == (off64_t)-1) {
23 PyErr_SetFromErrno(PyExc_OSError);
24 return 1;
25 }
26 br = read(Kd, buf, len);
27 if (br == -1) {
28 PyErr_SetFromErrno(PyExc_OSError);
29 return 1;
30 }
31 if (br != len) {
32 PyErr_SetString(PyExc_RuntimeError,
33 "size mismatch when reading kernel memory fd");
34 return 1;
35 }
36 return 0;
37 }
38
39 struct procentry64 *
40 psutil_read_process_table(int * num) {
41 size_t msz;
42 pid32_t pid = 0;
43 struct procentry64 *processes = (struct procentry64 *)NULL;
44 struct procentry64 *p;
45 int Np = 0; /* number of processes allocated in 'processes' */
46 int np = 0; /* number of processes read into 'processes' */
47 int i; /* number of processes read in current iteration */
48
49 msz = (size_t)(PROCSIZE * PROCINFO_INCR);
50 processes = (struct procentry64 *)malloc(msz);
51 if (!processes) {
52 PyErr_NoMemory();
53 return NULL;
54 }
55 Np = PROCINFO_INCR;
56 p = processes;
57 while ((i = getprocs64(p, PROCSIZE, (struct fdsinfo64 *)NULL, 0, &pid,
58 PROCINFO_INCR))
59 == PROCINFO_INCR) {
60 np += PROCINFO_INCR;
61 if (np >= Np) {
62 msz = (size_t)(PROCSIZE * (Np + PROCINFO_INCR));
63 processes = (struct procentry64 *)realloc((char *)processes, msz);
64 if (!processes) {
65 PyErr_NoMemory();
66 return NULL;
67 }
68 Np += PROCINFO_INCR;
69 }
70 p = (struct procentry64 *)((char *)processes + (np * PROCSIZE));
71 }
72
73 /* add the number of processes read in the last iteration */
74 if (i > 0)
75 np += i;
76
77 *num = np;
78 return processes;
79 }