Python-2.7.3/Modules/resource.c

No issues found

  1 #include "Python.h"
  2 #include "structseq.h"
  3 #include <sys/resource.h>
  4 #include <sys/time.h>
  5 #include <string.h>
  6 #include <errno.h>
  7 /* for sysconf */
  8 #if defined(HAVE_UNISTD_H)
  9 #include <unistd.h>
 10 #endif
 11 
 12 /* On some systems, these aren't in any header file.
 13    On others they are, with inconsistent prototypes.
 14    We declare the (default) return type, to shut up gcc -Wall;
 15    but we can't declare the prototype, to avoid errors
 16    when the header files declare it different.
 17    Worse, on some Linuxes, getpagesize() returns a size_t... */
 18 
 19 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
 20 
 21 static PyObject *ResourceError;
 22 
 23 PyDoc_STRVAR(struct_rusage__doc__,
 24 "struct_rusage: Result from getrusage.\n\n"
 25 "This object may be accessed either as a tuple of\n"
 26 "    (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
 27 "    nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
 28 "or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
 29 
 30 static PyStructSequence_Field struct_rusage_fields[] = {
 31     {"ru_utime",        "user time used"},
 32     {"ru_stime",        "system time used"},
 33     {"ru_maxrss",       "max. resident set size"},
 34     {"ru_ixrss",        "shared memory size"},
 35     {"ru_idrss",        "unshared data size"},
 36     {"ru_isrss",        "unshared stack size"},
 37     {"ru_minflt",       "page faults not requiring I/O"},
 38     {"ru_majflt",       "page faults requiring I/O"},
 39     {"ru_nswap",        "number of swap outs"},
 40     {"ru_inblock",      "block input operations"},
 41     {"ru_oublock",      "block output operations"},
 42     {"ru_msgsnd",       "IPC messages sent"},
 43     {"ru_msgrcv",       "IPC messages received"},
 44     {"ru_nsignals",     "signals received"},
 45     {"ru_nvcsw",        "voluntary context switches"},
 46     {"ru_nivcsw",       "involuntary context switches"},
 47     {0}
 48 };
 49 
 50 static PyStructSequence_Desc struct_rusage_desc = {
 51     "resource.struct_rusage",           /* name */
 52     struct_rusage__doc__,       /* doc */
 53     struct_rusage_fields,       /* fields */
 54     16          /* n_in_sequence */
 55 };
 56 
 57 static int initialized;
 58 static PyTypeObject StructRUsageType;
 59 
 60 static PyObject *
 61 resource_getrusage(PyObject *self, PyObject *args)
 62 {
 63     int who;
 64     struct rusage ru;
 65     PyObject *result;
 66 
 67     if (!PyArg_ParseTuple(args, "i:getrusage", &who))
 68         return NULL;
 69 
 70     if (getrusage(who, &ru) == -1) {
 71         if (errno == EINVAL) {
 72             PyErr_SetString(PyExc_ValueError,
 73                             "invalid who parameter");
 74             return NULL;
 75         }
 76         PyErr_SetFromErrno(ResourceError);
 77         return NULL;
 78     }
 79 
 80     result = PyStructSequence_New(&StructRUsageType);
 81     if (!result)
 82         return NULL;
 83 
 84     PyStructSequence_SET_ITEM(result, 0,
 85                     PyFloat_FromDouble(doubletime(ru.ru_utime)));
 86     PyStructSequence_SET_ITEM(result, 1,
 87                     PyFloat_FromDouble(doubletime(ru.ru_stime)));
 88     PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
 89     PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
 90     PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
 91     PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));
 92     PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));
 93     PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));
 94     PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));
 95     PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));
 96     PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));
 97     PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));
 98     PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));
 99     PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
100     PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
101     PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
102 
103     if (PyErr_Occurred()) {
104         Py_DECREF(result);
105         return NULL;
106     }
107 
108     return result;
109 }
110 
111 
112 static PyObject *
113 resource_getrlimit(PyObject *self, PyObject *args)
114 {
115     struct rlimit rl;
116     int resource;
117 
118     if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
119         return NULL;
120 
121     if (resource < 0 || resource >= RLIM_NLIMITS) {
122         PyErr_SetString(PyExc_ValueError,
123                         "invalid resource specified");
124         return NULL;
125     }
126 
127     if (getrlimit(resource, &rl) == -1) {
128         PyErr_SetFromErrno(ResourceError);
129         return NULL;
130     }
131 
132 #if defined(HAVE_LONG_LONG)
133     if (sizeof(rl.rlim_cur) > sizeof(long)) {
134         return Py_BuildValue("LL",
135                              (PY_LONG_LONG) rl.rlim_cur,
136                              (PY_LONG_LONG) rl.rlim_max);
137     }
138 #endif
139     return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
140 }
141 
142 static PyObject *
143 resource_setrlimit(PyObject *self, PyObject *args)
144 {
145     struct rlimit rl;
146     int resource;
147     PyObject *curobj, *maxobj;
148 
149     if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
150                           &resource, &curobj, &maxobj))
151         return NULL;
152 
153     if (resource < 0 || resource >= RLIM_NLIMITS) {
154         PyErr_SetString(PyExc_ValueError,
155                         "invalid resource specified");
156         return NULL;
157     }
158 
159 #if !defined(HAVE_LARGEFILE_SUPPORT)
160     rl.rlim_cur = PyInt_AsLong(curobj);
161     if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
162         return NULL;
163     rl.rlim_max = PyInt_AsLong(maxobj);
164     if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
165         return NULL;
166 #else
167     /* The limits are probably bigger than a long */
168     rl.rlim_cur = PyLong_Check(curobj) ?
169         PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
170     if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
171         return NULL;
172     rl.rlim_max = PyLong_Check(maxobj) ?
173         PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
174     if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
175         return NULL;
176 #endif
177 
178     rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
179     rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
180     if (setrlimit(resource, &rl) == -1) {
181         if (errno == EINVAL)
182             PyErr_SetString(PyExc_ValueError,
183                             "current limit exceeds maximum limit");
184         else if (errno == EPERM)
185             PyErr_SetString(PyExc_ValueError,
186                             "not allowed to raise maximum limit");
187         else
188             PyErr_SetFromErrno(ResourceError);
189         return NULL;
190     }
191     Py_INCREF(Py_None);
192     return Py_None;
193 }
194 
195 static PyObject *
196 resource_getpagesize(PyObject *self, PyObject *unused)
197 {
198     long pagesize = 0;
199 #if defined(HAVE_GETPAGESIZE)
200     pagesize = getpagesize();
201 #elif defined(HAVE_SYSCONF)
202 #if defined(_SC_PAGE_SIZE)
203     pagesize = sysconf(_SC_PAGE_SIZE);
204 #else
205     /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
206     pagesize = sysconf(_SC_PAGESIZE);
207 #endif
208 #endif
209     return Py_BuildValue("i", pagesize);
210 
211 }
212 
213 /* List of functions */
214 
215 static struct PyMethodDef
216 resource_methods[] = {
217     {"getrusage",    resource_getrusage,   METH_VARARGS},
218     {"getrlimit",    resource_getrlimit,   METH_VARARGS},
219     {"setrlimit",    resource_setrlimit,   METH_VARARGS},
220     {"getpagesize",  resource_getpagesize, METH_NOARGS},
221     {NULL, NULL}                             /* sentinel */
222 };
223 
224 
225 /* Module initialization */
226 
227 PyMODINIT_FUNC
228 initresource(void)
229 {
230     PyObject *m, *v;
231 
232     /* Create the module and add the functions */
233     m = Py_InitModule("resource", resource_methods);
234     if (m == NULL)
235         return;
236 
237     /* Add some symbolic constants to the module */
238     if (ResourceError == NULL) {
239         ResourceError = PyErr_NewException("resource.error",
240                                            NULL, NULL);
241     }
242     Py_INCREF(ResourceError);
243     PyModule_AddObject(m, "error", ResourceError);
244     if (!initialized)
245         PyStructSequence_InitType(&StructRUsageType,
246                                   &struct_rusage_desc);
247     Py_INCREF(&StructRUsageType);
248     PyModule_AddObject(m, "struct_rusage",
249                        (PyObject*) &StructRUsageType);
250 
251     /* insert constants */
252 #ifdef RLIMIT_CPU
253     PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
254 #endif
255 
256 #ifdef RLIMIT_FSIZE
257     PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
258 #endif
259 
260 #ifdef RLIMIT_DATA
261     PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
262 #endif
263 
264 #ifdef RLIMIT_STACK
265     PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
266 #endif
267 
268 #ifdef RLIMIT_CORE
269     PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
270 #endif
271 
272 #ifdef RLIMIT_NOFILE
273     PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
274 #endif
275 
276 #ifdef RLIMIT_OFILE
277     PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
278 #endif
279 
280 #ifdef RLIMIT_VMEM
281     PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
282 #endif
283 
284 #ifdef RLIMIT_AS
285     PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
286 #endif
287 
288 #ifdef RLIMIT_RSS
289     PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
290 #endif
291 
292 #ifdef RLIMIT_NPROC
293     PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
294 #endif
295 
296 #ifdef RLIMIT_MEMLOCK
297     PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
298 #endif
299 
300 #ifdef RLIMIT_SBSIZE
301     PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);
302 #endif
303 
304 #ifdef RUSAGE_SELF
305     PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
306 #endif
307 
308 #ifdef RUSAGE_CHILDREN
309     PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
310 #endif
311 
312 #ifdef RUSAGE_BOTH
313     PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
314 #endif
315 
316 #if defined(HAVE_LONG_LONG)
317     if (sizeof(RLIM_INFINITY) > sizeof(long)) {
318         v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
319     } else
320 #endif
321     {
322         v = PyInt_FromLong((long) RLIM_INFINITY);
323     }
324     if (v) {
325         PyModule_AddObject(m, "RLIM_INFINITY", v);
326     }
327     initialized = 1;
328 }