| 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 |
|
| 8 |
#include <Python.h> |
| 9 |
#include <fcntl.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <sys/param.h> |
| 12 |
#include <sys/sysctl.h> |
| 13 |
#include <sys/proc.h> |
| 14 |
#include <kvm.h> |
| 15 |
|
| 16 |
#include "../../arch/all/init.h" |
| 17 |
|
| 18 |
#define PSUTIL_KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) |
| 19 |
// #define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) |
| 20 |
|
| 21 |
|
| 22 |
// ============================================================================ |
| 23 |
// Utility functions |
| 24 |
// ============================================================================ |
| 25 |
|
| 26 |
|
| 27 |
int |
| 28 |
psutil_kinfo_proc(pid_t pid, struct kinfo_proc *proc) { |
| 29 |
// Fills a kinfo_proc struct based on process pid. |
| 30 |
int ret; |
| 31 |
int mib[6]; |
| 32 |
size_t size = sizeof(struct kinfo_proc); |
| 33 |
|
| 34 |
mib[0] = CTL_KERN; |
| 35 |
mib[1] = KERN_PROC; |
| 36 |
mib[2] = KERN_PROC_PID; |
| 37 |
mib[3] = pid; |
| 38 |
mib[4] = size; |
| 39 |
mib[5] = 1; |
| 40 |
|
| 41 |
ret = sysctl((int*)mib, 6, proc, &size, NULL, 0); |
| 42 |
if (ret == -1) { |
| 43 |
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl(kinfo_proc)"); |
| 44 |
return -1; |
| 45 |
} |
| 46 |
// sysctl stores 0 in the size if we can't find the process information. |
| 47 |
if (size == 0) { |
| 48 |
NoSuchProcess("sysctl (size = 0)"); |
| 49 |
return -1; |
| 50 |
} |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
// ============================================================================ |
| 56 |
// APIS |
| 57 |
// ============================================================================ |
| 58 |
|
| 59 |
int |
| 60 |
psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount) { |
| 61 |
// Returns a list of all BSD processes on the system. This routine |
| 62 |
// allocates the list and puts it in *procList and a count of the |
| 63 |
// number of entries in *procCount. You are responsible for freeing |
| 64 |
// this list (use "free" from System framework). |
| 65 |
// On success, the function returns 0. |
| 66 |
// On error, the function returns a BSD errno value. |
| 67 |
struct kinfo_proc *result; |
| 68 |
// Declaring name as const requires us to cast it when passing it to |
| 69 |
// sysctl because the prototype doesn't include the const modifier. |
| 70 |
char errbuf[_POSIX2_LINE_MAX]; |
| 71 |
int cnt; |
| 72 |
kvm_t *kd; |
| 73 |
|
| 74 |
assert(procList != NULL); |
| 75 |
assert(*procList == NULL); |
| 76 |
assert(procCount != NULL); |
| 77 |
|
| 78 |
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); |
| 79 |
if (! kd) { |
| 80 |
convert_kvm_err("kvm_openfiles", errbuf); |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
result = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &cnt); |
| 85 |
if (result == NULL) { |
| 86 |
PyErr_Format(PyExc_RuntimeError, "kvm_getprocs syscall failed"); |
| 87 |
kvm_close(kd); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
*procCount = (size_t)cnt; |
| 92 |
|
| 93 |
size_t mlen = cnt * sizeof(struct kinfo_proc); |
| 94 |
|
| 95 |
if ((*procList = malloc(mlen)) == NULL) { |
| 96 |
PyErr_NoMemory(); |
| 97 |
kvm_close(kd); |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
memcpy(*procList, result, mlen); |
| 102 |
assert(*procList != NULL); |
| 103 |
kvm_close(kd); |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// TODO: refactor this (it's clunky) |
| 110 |
PyObject * |
| 111 |
psutil_proc_cmdline(PyObject *self, PyObject *args) { |
| 112 |
pid_t pid; |
| 113 |
int mib[4]; |
| 114 |
char *argv_buf = NULL; |
| 115 |
size_t argv_len = 0; |
| 116 |
char **argv = NULL; |
| 117 |
char **p; |
| 118 |
PyObject *py_retlist = PyList_New(0); |
| 119 |
PyObject *py_arg = NULL; |
| 120 |
|
| 121 |
if (py_retlist == NULL) |
| 122 |
return NULL; |
| 123 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 124 |
goto error; |
| 125 |
|
| 126 |
mib[0] = CTL_KERN; |
| 127 |
mib[1] = KERN_PROC_ARGS; |
| 128 |
mib[2] = pid; |
| 129 |
mib[3] = KERN_PROC_ARGV; |
| 130 |
|
| 131 |
if (psutil_sysctl_malloc(mib, 4, &argv_buf, &argv_len) == -1) |
| 132 |
goto error; |
| 133 |
|
| 134 |
argv = (char **)argv_buf; |
| 135 |
|
| 136 |
for (p = argv; *p != NULL; p++) { |
| 137 |
py_arg = PyUnicode_DecodeFSDefault(*p); |
| 138 |
if (!py_arg) |
| 139 |
goto error; |
| 140 |
if (PyList_Append(py_retlist, py_arg)) |
| 141 |
goto error; |
| 142 |
Py_DECREF(py_arg); |
| 143 |
} |
| 144 |
|
| 145 |
free(argv_buf); |
| 146 |
return py_retlist; |
| 147 |
|
| 148 |
error: |
| 149 |
if (argv_buf != NULL) |
| 150 |
free(argv_buf); |
| 151 |
Py_XDECREF(py_arg); |
| 152 |
Py_DECREF(py_retlist); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
PyObject * |
| 158 |
psutil_proc_threads(PyObject *self, PyObject *args) { |
| 159 |
// OpenBSD reference: |
| 160 |
// https://github.com/janmojzis/pstree/blob/master/proc_kvm.c |
| 161 |
// Note: this requires root access, else it will fail trying |
| 162 |
// to access /dev/kmem. |
| 163 |
pid_t pid; |
| 164 |
kvm_t *kd = NULL; |
| 165 |
int nentries, i; |
| 166 |
char errbuf[4096]; |
| 167 |
struct kinfo_proc *kp; |
| 168 |
PyObject *py_retlist = PyList_New(0); |
| 169 |
PyObject *py_tuple = NULL; |
| 170 |
|
| 171 |
if (py_retlist == NULL) |
| 172 |
return NULL; |
| 173 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 174 |
goto error; |
| 175 |
|
| 176 |
kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf); |
| 177 |
if (! kd) { |
| 178 |
// Usually fails due to EPERM against /dev/mem. We retry with |
| 179 |
// KVM_NO_FILES which apparently has the same effect. |
| 180 |
// https://stackoverflow.com/questions/22369736/ |
| 181 |
psutil_debug("kvm_openfiles(O_RDONLY) failed"); |
| 182 |
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); |
| 183 |
if (! kd) { |
| 184 |
convert_kvm_err("kvm_openfiles()", errbuf); |
| 185 |
goto error; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
kp = kvm_getprocs( |
| 190 |
kd, KERN_PROC_PID | KERN_PROC_SHOW_THREADS | KERN_PROC_KTHREAD, pid, |
| 191 |
sizeof(*kp), &nentries); |
| 192 |
if (! kp) { |
| 193 |
if (strstr(errbuf, "Permission denied") != NULL) |
| 194 |
AccessDenied("kvm_getprocs"); |
| 195 |
else |
| 196 |
PyErr_Format(PyExc_RuntimeError, "kvm_getprocs() syscall failed"); |
| 197 |
goto error; |
| 198 |
} |
| 199 |
|
| 200 |
for (i = 0; i < nentries; i++) { |
| 201 |
if (kp[i].p_tid < 0) |
| 202 |
continue; |
| 203 |
if (kp[i].p_pid == pid) { |
| 204 |
py_tuple = Py_BuildValue( |
| 205 |
_Py_PARSE_PID "dd", |
| 206 |
kp[i].p_tid, |
| 207 |
PSUTIL_KPT2DOUBLE(kp[i].p_uutime), |
| 208 |
PSUTIL_KPT2DOUBLE(kp[i].p_ustime)); |
| 209 |
if (py_tuple == NULL) |
| 210 |
goto error; |
| 211 |
if (PyList_Append(py_retlist, py_tuple)) |
| 212 |
goto error; |
| 213 |
Py_DECREF(py_tuple); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
kvm_close(kd); |
| 218 |
return py_retlist; |
| 219 |
|
| 220 |
error: |
| 221 |
Py_XDECREF(py_tuple); |
| 222 |
Py_DECREF(py_retlist); |
| 223 |
if (kd != NULL) |
| 224 |
kvm_close(kd); |
| 225 |
return NULL; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
PyObject * |
| 230 |
psutil_proc_num_fds(PyObject *self, PyObject *args) { |
| 231 |
pid_t pid; |
| 232 |
int cnt; |
| 233 |
|
| 234 |
struct kinfo_file *freep; |
| 235 |
struct kinfo_proc kipp; |
| 236 |
|
| 237 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 238 |
return NULL; |
| 239 |
|
| 240 |
if (psutil_kinfo_proc(pid, &kipp) == -1) |
| 241 |
return NULL; |
| 242 |
|
| 243 |
freep = kinfo_getfile(pid, &cnt); |
| 244 |
|
| 245 |
if (freep == NULL) { |
| 246 |
#if defined(PSUTIL_OPENBSD) |
| 247 |
if ((pid == 0) && (errno == ESRCH)) { |
| 248 |
psutil_debug( |
| 249 |
"num_fds() returned ESRCH for PID 0; forcing `return 0`" |
| 250 |
); |
| 251 |
PyErr_Clear(); |
| 252 |
return Py_BuildValue("i", 0); |
| 253 |
} |
| 254 |
#endif |
| 255 |
return NULL; |
| 256 |
} |
| 257 |
|
| 258 |
free(freep); |
| 259 |
return Py_BuildValue("i", cnt); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
PyObject * |
| 264 |
psutil_proc_cwd(PyObject *self, PyObject *args) { |
| 265 |
// Reference: |
| 266 |
// https://github.com/openbsd/src/blob/ |
| 267 |
// 588f7f8c69786211f2d16865c552afb91b1c7cba/bin/ps/print.c#L191 |
| 268 |
pid_t pid; |
| 269 |
struct kinfo_proc kp; |
| 270 |
char path[MAXPATHLEN]; |
| 271 |
size_t pathlen = sizeof path; |
| 272 |
|
| 273 |
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 274 |
return NULL; |
| 275 |
if (psutil_kinfo_proc(pid, &kp) == -1) |
| 276 |
return NULL; |
| 277 |
|
| 278 |
int name[] = { CTL_KERN, KERN_PROC_CWD, pid }; |
| 279 |
if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) { |
| 280 |
if (errno == ENOENT) { |
| 281 |
psutil_debug("sysctl(KERN_PROC_CWD) -> ENOENT converted to ''"); |
| 282 |
return Py_BuildValue("s", ""); |
| 283 |
} |
| 284 |
else { |
| 285 |
PyErr_SetFromErrno(PyExc_OSError); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
} |
| 289 |
return PyUnicode_DecodeFSDefault(path); |
| 290 |
} |