| 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 "../../arch/all/init.h" |
| 8 |
|
| 9 |
#ifdef PSUTIL_HAS_SYSCTL |
| 10 |
#include <Python.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <sys/sysctl.h> |
| 13 |
|
| 14 |
|
| 15 |
static const int MAX_RETRIES = 10; |
| 16 |
|
| 17 |
|
| 18 |
// A thin wrapper on top of sysctl(). |
| 19 |
int |
| 20 |
psutil_sysctl(int *mib, u_int miblen, void *buf, size_t buflen) { |
| 21 |
size_t len = buflen; |
| 22 |
|
| 23 |
if (sysctl(mib, miblen, buf, &len, NULL, 0) == -1) { |
| 24 |
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl()"); |
| 25 |
return -1; |
| 26 |
} |
| 27 |
|
| 28 |
if (len != buflen) { |
| 29 |
PyErr_SetString(PyExc_RuntimeError, "sysctl() size mismatch"); |
| 30 |
return -1; |
| 31 |
} |
| 32 |
|
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
// Allocate buffer for sysctl with retry on ENOMEM or buffer size mismatch. |
| 37 |
// The caller is responsible for freeing the memory. |
| 38 |
int |
| 39 |
psutil_sysctl_malloc(int *mib, u_int miblen, char **buf, size_t *buflen) { |
| 40 |
size_t needed = 0; |
| 41 |
char *buffer = NULL; |
| 42 |
int ret; |
| 43 |
int max_retries = MAX_RETRIES; |
| 44 |
|
| 45 |
// First query to determine required size |
| 46 |
ret = sysctl(mib, miblen, NULL, &needed, NULL, 0); |
| 47 |
if (ret == -1) { |
| 48 |
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl() malloc 1/3"); |
| 49 |
return -1; |
| 50 |
} |
| 51 |
|
| 52 |
while (max_retries-- > 0) { |
| 53 |
// zero-initialize buffer to prevent uninitialized bytes |
| 54 |
buffer = calloc(1, needed); |
| 55 |
if (buffer == NULL) { |
| 56 |
PyErr_NoMemory(); |
| 57 |
return -1; |
| 58 |
} |
| 59 |
|
| 60 |
size_t len = needed; |
| 61 |
ret = sysctl(mib, miblen, buffer, &len, NULL, 0); |
| 62 |
|
| 63 |
if (ret == 0) { |
| 64 |
// Success: return buffer and length |
| 65 |
*buf = buffer; |
| 66 |
*buflen = len; |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
// Handle buffer too small |
| 71 |
if (errno == ENOMEM) { |
| 72 |
free(buffer); |
| 73 |
buffer = NULL; |
| 74 |
|
| 75 |
// Re-query needed size for next attempt |
| 76 |
if (sysctl(mib, miblen, NULL, &needed, NULL, 0) == -1) { |
| 77 |
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl() malloc 2/3"); |
| 78 |
return -1; |
| 79 |
} |
| 80 |
|
| 81 |
psutil_debug("psutil_sysctl_malloc() retry"); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
// Other errors: clean up and give up |
| 86 |
free(buffer); |
| 87 |
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl() malloc 3/3"); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
PyErr_SetString( |
| 92 |
PyExc_RuntimeError, "sysctl() buffer allocation retry limit exceeded" |
| 93 |
); |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// Get the maximum process arguments size. |
| 99 |
int |
| 100 |
psutil_sysctl_argmax() { |
| 101 |
int argmax; |
| 102 |
int mib[2] = {CTL_KERN, KERN_ARGMAX}; |
| 103 |
|
| 104 |
if (psutil_sysctl(mib, 2, &argmax, sizeof(argmax)) != 0) { |
| 105 |
PyErr_Clear(); |
| 106 |
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl(KERN_ARGMAX)"); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
return argmax; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
#ifdef PSUTIL_HAS_SYSCTLBYNAME |
| 115 |
// A thin wrapper on top of sysctlbyname(). |
| 116 |
int |
| 117 |
psutil_sysctlbyname(const char *name, void *buf, size_t buflen) { |
| 118 |
size_t len = buflen; |
| 119 |
char errbuf[256]; |
| 120 |
|
| 121 |
if (sysctlbyname(name, buf, &len, NULL, 0) == -1) { |
| 122 |
snprintf(errbuf, sizeof(errbuf), "sysctlbyname('%s')", name); |
| 123 |
psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
if (len != buflen) { |
| 128 |
snprintf( |
| 129 |
errbuf, |
| 130 |
sizeof(errbuf), |
| 131 |
"sysctlbyname('%s') size mismatch: returned %zu, expected %zu", |
| 132 |
name, |
| 133 |
len, |
| 134 |
buflen |
| 135 |
); |
| 136 |
PyErr_SetString(PyExc_RuntimeError, errbuf); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
// Allocate buffer for sysctlbyname with retry on ENOMEM or size mismatch. |
| 145 |
// The caller is responsible for freeing the memory. |
| 146 |
int |
| 147 |
psutil_sysctlbyname_malloc(const char *name, char **buf, size_t *buflen) { |
| 148 |
int ret; |
| 149 |
int max_retries = MAX_RETRIES; |
| 150 |
size_t needed = 0; |
| 151 |
size_t len = 0; |
| 152 |
char *buffer = NULL; |
| 153 |
char errbuf[256]; |
| 154 |
|
| 155 |
// First query to determine required size. |
| 156 |
ret = sysctlbyname(name, NULL, &needed, NULL, 0); |
| 157 |
if (ret == -1) { |
| 158 |
snprintf(errbuf, sizeof(errbuf), "sysctlbyname('%s') malloc 1/3", name); |
| 159 |
psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf); |
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
while (max_retries-- > 0) { |
| 164 |
// Zero-initialize buffer to prevent uninitialized bytes. |
| 165 |
buffer = calloc(1, needed); |
| 166 |
if (buffer == NULL) { |
| 167 |
PyErr_NoMemory(); |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
len = needed; |
| 172 |
ret = sysctlbyname(name, buffer, &len, NULL, 0); |
| 173 |
if (ret == 0) { |
| 174 |
// Success: return buffer and actual length. |
| 175 |
*buf = buffer; |
| 176 |
*buflen = len; |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
// Handle buffer too small. Re-query and retry. |
| 181 |
if (errno == ENOMEM) { |
| 182 |
free(buffer); |
| 183 |
buffer = NULL; |
| 184 |
|
| 185 |
if (sysctlbyname(name, NULL, &needed, NULL, 0) == -1) { |
| 186 |
snprintf( |
| 187 |
errbuf, |
| 188 |
sizeof(errbuf), |
| 189 |
"sysctlbyname('%s') malloc 2/3", |
| 190 |
name |
| 191 |
); |
| 192 |
psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf); |
| 193 |
return -1; |
| 194 |
} |
| 195 |
|
| 196 |
psutil_debug("psutil_sysctlbyname_malloc() retry"); |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
// Other errors: clean up and give up. |
| 201 |
free(buffer); |
| 202 |
snprintf( |
| 203 |
errbuf, sizeof(errbuf), "sysctlbyname('%s') malloc 3/3", name |
| 204 |
); |
| 205 |
psutil_PyErr_SetFromOSErrnoWithSyscall(errbuf); |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
snprintf( |
| 210 |
errbuf, |
| 211 |
sizeof(errbuf), |
| 212 |
"sysctlbyname('%s') buffer allocation retry limit exceeded", |
| 213 |
name |
| 214 |
); |
| 215 |
PyErr_SetString(PyExc_RuntimeError, errbuf); |
| 216 |
return -1; |
| 217 |
} |
| 218 |
#endif // PSUTIL_HAS_SYSCTLBYNAME |
| 219 |
#endif // PSUTIL_HAS_SYSCTL |