| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola', Landry Breuil. |
| 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 |
* Platform-specific module methods for NetBSD. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <Python.h> |
| 11 |
#include <sys/sysctl.h> |
| 12 |
#include <kvm.h> |
| 13 |
|
| 14 |
#include "../../arch/all/init.h" |
| 15 |
|
| 16 |
|
| 17 |
#define PSUTIL_KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) |
| 18 |
#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) |
| 19 |
|
| 20 |
|
| 21 |
// ============================================================================ |
| 22 |
// Utility functions |
| 23 |
// ============================================================================ |
| 24 |
|
| 25 |
|
| 26 |
int |
| 27 |
psutil_kinfo_proc(pid_t pid, kinfo_proc *proc) { |
| 28 |
// Fills a kinfo_proc struct based on process pid. |
| 29 |
int ret; |
| 30 |
int mib[6]; |
| 31 |
size_t size = sizeof(kinfo_proc); |
| 32 |
|
| 33 |
mib[0] = CTL_KERN; |
| 34 |
mib[1] = KERN_PROC2; |
| 35 |
mib[2] = KERN_PROC_PID; |
| 36 |
mib[3] = pid; |
| 37 |
mib[4] = size; |
| 38 |
mib[5] = 1; |
| 39 |
|
| 40 |
ret = sysctl((int*)mib, 6, proc, &size, NULL, 0); |
| 41 |
if (ret == -1) { |
| 42 |
PyErr_SetFromErrno(PyExc_OSError); |
| 43 |
return -1; |
| 44 |
} |
| 45 |
// sysctl stores 0 in the size if we can't find the process information. |
| 46 |
if (size == 0) { |
| 47 |
NoSuchProcess("sysctl (size = 0)"); |
| 48 |
return -1; |
| 49 |
} |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
PyObject * |
| 57 |
psutil_proc_cwd(PyObject *self, PyObject *args) { |
| 58 |
long pid; |
| 59 |
|
| 60 |
char path[MAXPATHLEN]; |
| 61 |
size_t pathlen = sizeof path; |
| 62 |
|
| 63 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 64 |
return NULL; |
| 65 |
|
| 66 |
#ifdef KERN_PROC_CWD |
| 67 |
int name[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_CWD}; |
| 68 |
if (sysctl(name, 4, path, &pathlen, NULL, 0) != 0) { |
| 69 |
if (errno == ENOENT) |
| 70 |
NoSuchProcess("sysctl -> ENOENT"); |
| 71 |
else |
| 72 |
PyErr_SetFromErrno(PyExc_OSError); |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
#else |
| 76 |
char *buf; |
| 77 |
if (asprintf(&buf, "/proc/%d/cwd", (int)pid) < 0) { |
| 78 |
PyErr_NoMemory(); |
| 79 |
return NULL; |
| 80 |
} |
| 81 |
|
| 82 |
ssize_t len = readlink(buf, path, sizeof(path) - 1); |
| 83 |
free(buf); |
| 84 |
if (len == -1) { |
| 85 |
if (errno == ENOENT) |
| 86 |
NoSuchProcess("readlink -> ENOENT"); |
| 87 |
else |
| 88 |
PyErr_SetFromErrno(PyExc_OSError); |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
path[len] = '\0'; |
| 92 |
#endif |
| 93 |
|
| 94 |
return PyUnicode_DecodeFSDefault(path); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// XXX: This is no longer used as per |
| 99 |
// https://github.com/giampaolo/psutil/pull/557#issuecomment-171912820 |
| 100 |
// Current implementation uses /proc instead. |
| 101 |
// Left here just in case. |
| 102 |
/* |
| 103 |
PyObject * |
| 104 |
psutil_proc_exe(PyObject *self, PyObject *args) { |
| 105 |
#if __NetBSD_Version__ >= 799000000 |
| 106 |
pid_t pid; |
| 107 |
char pathname[MAXPATHLEN]; |
| 108 |
int error; |
| 109 |
int mib[4]; |
| 110 |
int ret; |
| 111 |
size_t size; |
| 112 |
|
| 113 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 114 |
return NULL; |
| 115 |
if (pid == 0) { |
| 116 |
// else returns ENOENT |
| 117 |
return Py_BuildValue("s", ""); |
| 118 |
} |
| 119 |
|
| 120 |
mib[0] = CTL_KERN; |
| 121 |
mib[1] = KERN_PROC_ARGS; |
| 122 |
mib[2] = pid; |
| 123 |
mib[3] = KERN_PROC_PATHNAME; |
| 124 |
|
| 125 |
size = sizeof(pathname); |
| 126 |
error = sysctl(mib, 4, NULL, &size, NULL, 0); |
| 127 |
if (error == -1) { |
| 128 |
PyErr_SetFromErrno(PyExc_OSError); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
error = sysctl(mib, 4, pathname, &size, NULL, 0); |
| 133 |
if (error == -1) { |
| 134 |
PyErr_SetFromErrno(PyExc_OSError); |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
if (size == 0 || strlen(pathname) == 0) { |
| 138 |
ret = psutil_pid_exists(pid); |
| 139 |
if (ret == -1) |
| 140 |
return NULL; |
| 141 |
else if (ret == 0) |
| 142 |
return NoSuchProcess("psutil_pid_exists -> 0"); |
| 143 |
else |
| 144 |
strcpy(pathname, ""); |
| 145 |
} |
| 146 |
|
| 147 |
return PyUnicode_DecodeFSDefault(pathname); |
| 148 |
#else |
| 149 |
return Py_BuildValue("s", ""); |
| 150 |
#endif |
| 151 |
} |
| 152 |
*/ |
| 153 |
|
| 154 |
|
| 155 |
PyObject * |
| 156 |
psutil_proc_num_threads(PyObject *self, PyObject *args) { |
| 157 |
// Return number of threads used by process as a Python integer. |
| 158 |
long pid; |
| 159 |
kinfo_proc kp; |
| 160 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 161 |
return NULL; |
| 162 |
if (psutil_kinfo_proc(pid, &kp) == -1) |
| 163 |
return NULL; |
| 164 |
return Py_BuildValue("l", (long)kp.p_nlwps); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
PyObject * |
| 169 |
psutil_proc_threads(PyObject *self, PyObject *args) { |
| 170 |
pid_t pid; |
| 171 |
int mib[5]; |
| 172 |
int i, nlwps; |
| 173 |
ssize_t st; |
| 174 |
size_t size; |
| 175 |
struct kinfo_lwp *kl = NULL; |
| 176 |
PyObject *py_retlist = PyList_New(0); |
| 177 |
PyObject *py_tuple = NULL; |
| 178 |
|
| 179 |
if (py_retlist == NULL) |
| 180 |
return NULL; |
| 181 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 182 |
goto error; |
| 183 |
|
| 184 |
mib[0] = CTL_KERN; |
| 185 |
mib[1] = KERN_LWP; |
| 186 |
mib[2] = pid; |
| 187 |
mib[3] = sizeof(struct kinfo_lwp); |
| 188 |
mib[4] = 0; |
| 189 |
|
| 190 |
// first query size |
| 191 |
st = sysctl(mib, 5, NULL, &size, NULL, 0); |
| 192 |
if (st == -1) { |
| 193 |
PyErr_SetFromErrno(PyExc_OSError); |
| 194 |
goto error; |
| 195 |
} |
| 196 |
if (size == 0) { |
| 197 |
NoSuchProcess("sysctl (size = 0)"); |
| 198 |
goto error; |
| 199 |
} |
| 200 |
|
| 201 |
// set slot count for NetBSD KERN_LWP |
| 202 |
mib[4] = size / sizeof(size_t); |
| 203 |
|
| 204 |
if (psutil_sysctl_malloc( |
| 205 |
mib, __arraycount(mib), (char **)&kl, &size) != 0) { |
| 206 |
goto error; |
| 207 |
} |
| 208 |
if (size == 0) { |
| 209 |
NoSuchProcess("sysctl (size = 0)"); |
| 210 |
goto error; |
| 211 |
} |
| 212 |
|
| 213 |
nlwps = (int)(size / sizeof(struct kinfo_lwp)); |
| 214 |
for (i = 0; i < nlwps; i++) { |
| 215 |
if (kl[i].l_stat == LSIDL || kl[i].l_stat == LSZOMB) |
| 216 |
continue; |
| 217 |
// XXX: return 2 "user" times, no "system" time available |
| 218 |
py_tuple = Py_BuildValue( |
| 219 |
"idd", |
| 220 |
kl[i].l_lid, |
| 221 |
PSUTIL_KPT2DOUBLE(kl[i].l_rtime), |
| 222 |
PSUTIL_KPT2DOUBLE(kl[i].l_rtime) |
| 223 |
); |
| 224 |
if (py_tuple == NULL) |
| 225 |
goto error; |
| 226 |
if (PyList_Append(py_retlist, py_tuple)) |
| 227 |
goto error; |
| 228 |
Py_DECREF(py_tuple); |
| 229 |
} |
| 230 |
|
| 231 |
free(kl); |
| 232 |
return py_retlist; |
| 233 |
|
| 234 |
error: |
| 235 |
Py_XDECREF(py_tuple); |
| 236 |
Py_DECREF(py_retlist); |
| 237 |
if (kl != NULL) |
| 238 |
free(kl); |
| 239 |
return NULL; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
int |
| 244 |
psutil_get_proc_list(kinfo_proc **procList, size_t *procCount) { |
| 245 |
// Returns a list of all BSD processes on the system. This routine |
| 246 |
// allocates the list and puts it in *procList and a count of the |
| 247 |
// number of entries in *procCount. You are responsible for freeing |
| 248 |
// this list (use "free" from System framework). |
| 249 |
// On success, the function returns 0. |
| 250 |
// On error, the function returns a BSD errno value. |
| 251 |
kinfo_proc *result; |
| 252 |
// Declaring name as const requires us to cast it when passing it to |
| 253 |
// sysctl because the prototype doesn't include the const modifier. |
| 254 |
char errbuf[_POSIX2_LINE_MAX]; |
| 255 |
int cnt; |
| 256 |
kvm_t *kd; |
| 257 |
|
| 258 |
assert( procList != NULL); |
| 259 |
assert(*procList == NULL); |
| 260 |
assert(procCount != NULL); |
| 261 |
|
| 262 |
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); |
| 263 |
|
| 264 |
if (kd == NULL) { |
| 265 |
PyErr_Format( |
| 266 |
PyExc_RuntimeError, "kvm_openfiles() syscall failed: %s", errbuf); |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
result = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(kinfo_proc), &cnt); |
| 271 |
if (result == NULL) { |
| 272 |
PyErr_Format(PyExc_RuntimeError, "kvm_getproc2() syscall failed"); |
| 273 |
kvm_close(kd); |
| 274 |
return 1; |
| 275 |
} |
| 276 |
|
| 277 |
*procCount = (size_t)cnt; |
| 278 |
|
| 279 |
size_t mlen = cnt * sizeof(kinfo_proc); |
| 280 |
|
| 281 |
if ((*procList = malloc(mlen)) == NULL) { |
| 282 |
PyErr_NoMemory(); |
| 283 |
kvm_close(kd); |
| 284 |
return 1; |
| 285 |
} |
| 286 |
|
| 287 |
memcpy(*procList, result, mlen); |
| 288 |
assert(*procList != NULL); |
| 289 |
kvm_close(kd); |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
PyObject * |
| 296 |
psutil_proc_cmdline(PyObject *self, PyObject *args) { |
| 297 |
pid_t pid; |
| 298 |
int mib[4]; |
| 299 |
int st; |
| 300 |
int attempt = 0; |
| 301 |
int max_attempts = 50; |
| 302 |
size_t len = 0; |
| 303 |
size_t pos = 0; |
| 304 |
char *procargs = NULL; |
| 305 |
PyObject *py_retlist = PyList_New(0); |
| 306 |
PyObject *py_arg = NULL; |
| 307 |
|
| 308 |
if (py_retlist == NULL) |
| 309 |
return NULL; |
| 310 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 311 |
goto error; |
| 312 |
|
| 313 |
mib[0] = CTL_KERN; |
| 314 |
mib[1] = KERN_PROC_ARGS; |
| 315 |
mib[2] = pid; |
| 316 |
mib[3] = KERN_PROC_ARGV; |
| 317 |
|
| 318 |
while (1) { |
| 319 |
if (psutil_sysctl_malloc( |
| 320 |
mib, __arraycount(mib), (char **)&procargs, &len) != 0) { |
| 321 |
if (errno == EBUSY) { |
| 322 |
// Usually happens with TestProcess.test_long_cmdline. See: |
| 323 |
// https://github.com/giampaolo/psutil/issues/2250 |
| 324 |
attempt += 1; |
| 325 |
if (attempt < max_attempts) { |
| 326 |
psutil_debug("proc %zu cmdline(): retry on EBUSY", pid); |
| 327 |
continue; |
| 328 |
} |
| 329 |
else { |
| 330 |
psutil_debug( |
| 331 |
"proc %zu cmdline(): return [] due to EBUSY", pid |
| 332 |
); |
| 333 |
return py_retlist; |
| 334 |
} |
| 335 |
} |
| 336 |
goto error; |
| 337 |
} |
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
if (len > 0) { |
| 342 |
while (pos < len) { |
| 343 |
py_arg = PyUnicode_DecodeFSDefault(&procargs[pos]); |
| 344 |
if (!py_arg) |
| 345 |
goto error; |
| 346 |
if (PyList_Append(py_retlist, py_arg)) |
| 347 |
goto error; |
| 348 |
Py_DECREF(py_arg); |
| 349 |
pos = pos + strlen(&procargs[pos]) + 1; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
free(procargs); |
| 354 |
return py_retlist; |
| 355 |
|
| 356 |
error: |
| 357 |
Py_XDECREF(py_arg); |
| 358 |
Py_DECREF(py_retlist); |
| 359 |
if (procargs != NULL) |
| 360 |
free(procargs); |
| 361 |
return NULL; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
PyObject * |
| 366 |
psutil_proc_num_fds(PyObject *self, PyObject *args) { |
| 367 |
long pid; |
| 368 |
int cnt; |
| 369 |
|
| 370 |
struct kinfo_file *freep; |
| 371 |
|
| 372 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 373 |
return NULL; |
| 374 |
|
| 375 |
errno = 0; |
| 376 |
freep = kinfo_getfile(pid, &cnt); |
| 377 |
if (freep == NULL) { |
| 378 |
psutil_raise_for_pid(pid, "kinfo_getfile()"); |
| 379 |
return NULL; |
| 380 |
} |
| 381 |
free(freep); |
| 382 |
|
| 383 |
return Py_BuildValue("i", cnt); |
| 384 |
} |