File: sysctl.c

package info (click to toggle)
python-psutil 7.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,312 kB
  • sloc: python: 19,411; ansic: 15,378; makefile: 465; javascript: 153; sh: 54
file content (219 lines) | stat: -rw-r--r-- 5,797 bytes parent folder | download
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
/*
 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "../../arch/all/init.h"

#ifdef PSUTIL_HAS_SYSCTL
#include <Python.h>
#include <sys/types.h>
#include <sys/sysctl.h>


static const int MAX_RETRIES = 10;


// A thin wrapper on top of sysctl().
int
psutil_sysctl(int *mib, u_int miblen, void *buf, size_t buflen) {
    size_t len = buflen;

    if (sysctl(mib, miblen, buf, &len, NULL, 0) == -1) {
        psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl()");
        return -1;
    }

    if (len != buflen) {
        PyErr_SetString(PyExc_RuntimeError, "sysctl() size mismatch");
        return -1;
    }

    return 0;
}

// Allocate buffer for sysctl with retry on ENOMEM or buffer size mismatch.
// The caller is responsible for freeing the memory.
int
psutil_sysctl_malloc(int *mib, u_int miblen, char **buf, size_t *buflen) {
    size_t needed = 0;
    char *buffer = NULL;
    int ret;
    int max_retries = MAX_RETRIES;

    // First query to determine required size
    ret = sysctl(mib, miblen, NULL, &needed, NULL, 0);
    if (ret == -1) {
        psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl() malloc 1/3");
        return -1;
    }

    while (max_retries-- > 0) {
        // zero-initialize buffer to prevent uninitialized bytes
        buffer = calloc(1, needed);
        if (buffer == NULL) {
            PyErr_NoMemory();
            return -1;
        }

        size_t len = needed;
        ret = sysctl(mib, miblen, buffer, &len, NULL, 0);

        if (ret == 0) {
            // Success: return buffer and length
            *buf = buffer;
            *buflen = len;
            return 0;
        }

        // Handle buffer too small
        if (errno == ENOMEM) {
            free(buffer);
            buffer = NULL;

            // Re-query needed size for next attempt
            if (sysctl(mib, miblen, NULL, &needed, NULL, 0) == -1) {
                psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl() malloc 2/3");
                return -1;
            }

            psutil_debug("psutil_sysctl_malloc() retry");
            continue;
        }

        // Other errors: clean up and give up
        free(buffer);
        psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl() malloc 3/3");
        return -1;
    }

    PyErr_SetString(
        PyExc_RuntimeError, "sysctl() buffer allocation retry limit exceeded"
    );
    return -1;
}


// Get the maximum process arguments size.
int
psutil_sysctl_argmax() {
    int argmax;
    int mib[2] = {CTL_KERN, KERN_ARGMAX};

    if (psutil_sysctl(mib, 2, &argmax, sizeof(argmax)) != 0) {
        PyErr_Clear();
        psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl(KERN_ARGMAX)");
        return 0;
    }

    return argmax;
}


#ifdef PSUTIL_HAS_SYSCTLBYNAME
// A thin wrapper on top of sysctlbyname().
int
psutil_sysctlbyname(const char *name, void *buf, size_t buflen) {
    size_t len = buflen;
    char errbuf[256];

    if (sysctlbyname(name, buf, &len, NULL, 0) == -1) {
        snprintf(errbuf, sizeof(errbuf), "sysctlbyname('%s')", name);
        psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf);
        return -1;
    }

    if (len != buflen) {
        snprintf(
            errbuf,
            sizeof(errbuf),
            "sysctlbyname('%s') size mismatch: returned %zu, expected %zu",
            name,
            len,
            buflen
        );
        PyErr_SetString(PyExc_RuntimeError, errbuf);
        return -1;
    }

    return 0;
}


// Allocate buffer for sysctlbyname with retry on ENOMEM or size mismatch.
// The caller is responsible for freeing the memory.
int
psutil_sysctlbyname_malloc(const char *name, char **buf, size_t *buflen) {
    int ret;
    int max_retries = MAX_RETRIES;
    size_t needed = 0;
    size_t len = 0;
    char *buffer = NULL;
    char errbuf[256];

    // First query to determine required size.
    ret = sysctlbyname(name, NULL, &needed, NULL, 0);
    if (ret == -1) {
        snprintf(errbuf, sizeof(errbuf), "sysctlbyname('%s') malloc 1/3", name);
        psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf);
        return -1;
    }

    while (max_retries-- > 0) {
        // Zero-initialize buffer to prevent uninitialized bytes.
        buffer = calloc(1, needed);
        if (buffer == NULL) {
            PyErr_NoMemory();
            return -1;
        }

        len = needed;
        ret = sysctlbyname(name, buffer, &len, NULL, 0);
        if (ret == 0) {
            // Success: return buffer and actual length.
            *buf = buffer;
            *buflen = len;
            return 0;
        }

        // Handle buffer too small. Re-query and retry.
        if (errno == ENOMEM) {
            free(buffer);
            buffer = NULL;

            if (sysctlbyname(name, NULL, &needed, NULL, 0) == -1) {
                snprintf(
                    errbuf,
                    sizeof(errbuf),
                    "sysctlbyname('%s') malloc 2/3",
                    name
                );
                psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf);
                return -1;
            }

            psutil_debug("psutil_sysctlbyname_malloc() retry");
            continue;
        }

        // Other errors: clean up and give up.
        free(buffer);
        snprintf(
            errbuf, sizeof(errbuf), "sysctlbyname('%s') malloc 3/3", name
        );
        psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf);
        return -1;
    }

    snprintf(
        errbuf,
        sizeof(errbuf),
        "sysctlbyname('%s') buffer allocation retry limit exceeded",
        name
    );
    PyErr_SetString(PyExc_RuntimeError, errbuf);
    return -1;
}
#endif  // PSUTIL_HAS_SYSCTLBYNAME
#endif  // PSUTIL_HAS_SYSCTL