OLD | NEW |
| (Empty) |
1 /* | |
2 * $Id: _psutil_posix.c 1142 2011-10-05 18:45:49Z g.rodola $ | |
3 * | |
4 * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 * | |
8 * Functions specific to all POSIX compliant platforms. | |
9 */ | |
10 | |
11 #include <Python.h> | |
12 #include <errno.h> | |
13 #include <stdlib.h> | |
14 #include <sys/resource.h> | |
15 | |
16 #include "_psutil_posix.h" | |
17 | |
18 | |
19 /* | |
20 * Given a PID return process priority as a Python integer. | |
21 */ | |
22 static PyObject* | |
23 posix_getpriority(PyObject* self, PyObject* args) | |
24 { | |
25 long pid; | |
26 int priority; | |
27 errno = 0; | |
28 if (! PyArg_ParseTuple(args, "l", &pid)) { | |
29 return NULL; | |
30 } | |
31 priority = getpriority(PRIO_PROCESS, pid); | |
32 if (errno != 0) { | |
33 return PyErr_SetFromErrno(PyExc_OSError); | |
34 } | |
35 return Py_BuildValue("i", priority); | |
36 } | |
37 | |
38 /* | |
39 * Given a PID and a value change process priority. | |
40 */ | |
41 static PyObject* | |
42 posix_setpriority(PyObject* self, PyObject* args) | |
43 { | |
44 long pid; | |
45 int priority; | |
46 int retval; | |
47 if (! PyArg_ParseTuple(args, "li", &pid, &priority)) { | |
48 return NULL; | |
49 } | |
50 retval = setpriority(PRIO_PROCESS, pid, priority); | |
51 if (retval == -1) { | |
52 return PyErr_SetFromErrno(PyExc_OSError); | |
53 } | |
54 Py_INCREF(Py_None); | |
55 return Py_None; | |
56 } | |
57 | |
58 | |
59 /* | |
60 * define the psutil C module methods and initialize the module. | |
61 */ | |
62 static PyMethodDef | |
63 PsutilMethods[] = | |
64 { | |
65 {"getpriority", posix_getpriority, METH_VARARGS, | |
66 "Return process priority"}, | |
67 {"setpriority", posix_setpriority, METH_VARARGS, | |
68 "Set process priority"}, | |
69 {NULL, NULL, 0, NULL} | |
70 }; | |
71 | |
72 struct module_state { | |
73 PyObject *error; | |
74 }; | |
75 | |
76 #if PY_MAJOR_VERSION >= 3 | |
77 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) | |
78 #else | |
79 #define GETSTATE(m) (&_state) | |
80 #endif | |
81 | |
82 #if PY_MAJOR_VERSION >= 3 | |
83 | |
84 static int | |
85 psutil_posix_traverse(PyObject *m, visitproc visit, void *arg) { | |
86 Py_VISIT(GETSTATE(m)->error); | |
87 return 0; | |
88 } | |
89 | |
90 static int | |
91 psutil_posix_clear(PyObject *m) { | |
92 Py_CLEAR(GETSTATE(m)->error); | |
93 return 0; | |
94 } | |
95 | |
96 static struct PyModuleDef | |
97 moduledef = { | |
98 PyModuleDef_HEAD_INIT, | |
99 "psutil_posix", | |
100 NULL, | |
101 sizeof(struct module_state), | |
102 PsutilMethods, | |
103 NULL, | |
104 psutil_posix_traverse, | |
105 psutil_posix_clear, | |
106 NULL | |
107 }; | |
108 | |
109 #define INITERROR return NULL | |
110 | |
111 PyObject * | |
112 PyInit__psutil_posix(void) | |
113 | |
114 #else | |
115 #define INITERROR return | |
116 | |
117 void init_psutil_posix(void) | |
118 #endif | |
119 { | |
120 #if PY_MAJOR_VERSION >= 3 | |
121 PyObject *module = PyModule_Create(&moduledef); | |
122 #else | |
123 PyObject *module = Py_InitModule("_psutil_posix", PsutilMethods); | |
124 #endif | |
125 if (module == NULL) { | |
126 INITERROR; | |
127 } | |
128 #if PY_MAJOR_VERSION >= 3 | |
129 return module; | |
130 #endif | |
131 } | |
132 | |
133 | |
134 | |
OLD | NEW |