| 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 |
// Global names shared by all platforms. |
| 8 |
|
| 9 |
#include <Python.h> |
| 10 |
#ifdef PSUTIL_WINDOWS |
| 11 |
#include <windows.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#include "init.h" |
| 15 |
|
| 16 |
int PSUTIL_DEBUG = 0; |
| 17 |
int PSUTIL_CONN_NONE = 128; |
| 18 |
|
| 19 |
#ifdef Py_GIL_DISABLED |
| 20 |
PyMutex utxent_lock = {0}; |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
// Set OSError(errno=ESRCH, strerror="No such process (originated from") |
| 25 |
// Python exception. |
| 26 |
PyObject * |
| 27 |
NoSuchProcess(const char *syscall) { |
| 28 |
PyObject *exc; |
| 29 |
char msg[1024]; |
| 30 |
|
| 31 |
sprintf(msg, "assume no such process (originated from %s)", syscall); |
| 32 |
exc = PyObject_CallFunction(PyExc_OSError, "(is)", ESRCH, msg); |
| 33 |
PyErr_SetObject(PyExc_OSError, exc); |
| 34 |
Py_XDECREF(exc); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
// Set OSError(errno=EACCES, strerror="Permission denied" (originated from ...) |
| 40 |
// Python exception. |
| 41 |
PyObject * |
| 42 |
AccessDenied(const char *syscall) { |
| 43 |
PyObject *exc; |
| 44 |
char msg[1024]; |
| 45 |
|
| 46 |
sprintf(msg, "assume access denied (originated from %s)", syscall); |
| 47 |
exc = PyObject_CallFunction(PyExc_OSError, "(is)", EACCES, msg); |
| 48 |
PyErr_SetObject(PyExc_OSError, exc); |
| 49 |
Py_XDECREF(exc); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
// Same as PyErr_SetFromErrno(0) but adds the syscall to the exception |
| 55 |
// message. |
| 56 |
PyObject * |
| 57 |
psutil_PyErr_SetFromOSErrnoWithSyscall(const char *syscall) { |
| 58 |
char fullmsg[1024]; |
| 59 |
|
| 60 |
#ifdef PSUTIL_WINDOWS |
| 61 |
DWORD dwLastError = GetLastError(); |
| 62 |
sprintf(fullmsg, "(originated from %s)", syscall); |
| 63 |
PyErr_SetFromWindowsErrWithFilename(dwLastError, fullmsg); |
| 64 |
#else |
| 65 |
PyObject *exc; |
| 66 |
sprintf(fullmsg, "%s (originated from %s)", strerror(errno), syscall); |
| 67 |
exc = PyObject_CallFunction(PyExc_OSError, "(is)", errno, fullmsg); |
| 68 |
PyErr_SetObject(PyExc_OSError, exc); |
| 69 |
Py_XDECREF(exc); |
| 70 |
#endif |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
// Enable or disable PSUTIL_DEBUG messages. |
| 76 |
PyObject * |
| 77 |
psutil_set_debug(PyObject *self, PyObject *args) { |
| 78 |
PyObject *value; |
| 79 |
int x; |
| 80 |
|
| 81 |
if (!PyArg_ParseTuple(args, "O", &value)) |
| 82 |
return NULL; |
| 83 |
x = PyObject_IsTrue(value); |
| 84 |
if (x < 0) { |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
else if (x == 0) { |
| 88 |
PSUTIL_DEBUG = 0; |
| 89 |
} |
| 90 |
else { |
| 91 |
PSUTIL_DEBUG = 1; |
| 92 |
} |
| 93 |
Py_RETURN_NONE; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
// Raise OverflowError if Python int value overflowed when converting |
| 98 |
// to pid_t. Raise ValueError if Python int value is negative. |
| 99 |
// Otherwise, return None. |
| 100 |
PyObject * |
| 101 |
psutil_check_pid_range(PyObject *self, PyObject *args) { |
| 102 |
#ifdef PSUTIL_WINDOWS |
| 103 |
DWORD pid; |
| 104 |
#else |
| 105 |
pid_t pid; |
| 106 |
#endif |
| 107 |
|
| 108 |
if (!PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) |
| 109 |
return NULL; |
| 110 |
if (pid < 0) { |
| 111 |
PyErr_SetString(PyExc_ValueError, "pid must be a positive integer"); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
Py_RETURN_NONE; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
// Called on module import on all platforms. |
| 119 |
int |
| 120 |
psutil_setup(void) { |
| 121 |
if (getenv("PSUTIL_DEBUG") != NULL) |
| 122 |
PSUTIL_DEBUG = 1; |
| 123 |
return 0; |
| 124 |
} |