4 #include <sys/resource.h>
9 #if defined(HAVE_UNISTD_H)
13 /* On some systems, these aren't in any header file.
14 On others they are, with inconsistent prototypes.
15 We declare the (default) return type, to shut up gcc -Wall;
16 but we can't declare the prototype, to avoid errors
17 when the header files declare it different.
18 Worse, on some Linuxes, getpagesize() returns a size_t... */
20 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
22 static PyObject
*ResourceError
;
24 PyDoc_STRVAR(struct_rusage__doc__
,
25 "struct_rusage: Result from getrusage.\n\n"
26 "This object may be accessed either as a tuple of\n"
27 " (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
28 " nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
29 "or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
31 static PyStructSequence_Field struct_rusage_fields
[] = {
32 {"ru_utime", "user time used"},
33 {"ru_stime", "system time used"},
34 {"ru_maxrss", "max. resident set size"},
35 {"ru_ixrss", "shared memory size"},
36 {"ru_idrss", "unshared data size"},
37 {"ru_isrss", "unshared stack size"},
38 {"ru_minflt", "page faults not requiring I/O"},
39 {"ru_majflt", "page faults requiring I/O"},
40 {"ru_nswap", "number of swap outs"},
41 {"ru_inblock", "block input operations"},
42 {"ru_oublock", "block output operations"},
43 {"ru_msgsnd", "IPC messages sent"},
44 {"ru_msgrcv", "IPC messages received"},
45 {"ru_nsignals", "signals received"},
46 {"ru_nvcsw", "voluntary context switches"},
47 {"ru_nivcsw", "involuntary context switches"},
51 static PyStructSequence_Desc struct_rusage_desc
= {
52 "resource.struct_rusage", /* name */
53 struct_rusage__doc__
, /* doc */
54 struct_rusage_fields
, /* fields */
55 16 /* n_in_sequence */
58 static int initialized
;
59 static PyTypeObject StructRUsageType
;
62 resource_getrusage(PyObject
*self
, PyObject
*args
)
68 if (!PyArg_ParseTuple(args
, "i:getrusage", &who
))
71 if (getrusage(who
, &ru
) == -1) {
72 if (errno
== EINVAL
) {
73 PyErr_SetString(PyExc_ValueError
,
74 "invalid who parameter");
77 PyErr_SetFromErrno(ResourceError
);
81 result
= PyStructSequence_New(&StructRUsageType
);
85 PyStructSequence_SET_ITEM(result
, 0,
86 PyFloat_FromDouble(doubletime(ru
.ru_utime
)));
87 PyStructSequence_SET_ITEM(result
, 1,
88 PyFloat_FromDouble(doubletime(ru
.ru_stime
)));
89 PyStructSequence_SET_ITEM(result
, 2, PyLong_FromLong(ru
.ru_maxrss
));
90 PyStructSequence_SET_ITEM(result
, 3, PyLong_FromLong(ru
.ru_ixrss
));
91 PyStructSequence_SET_ITEM(result
, 4, PyLong_FromLong(ru
.ru_idrss
));
92 PyStructSequence_SET_ITEM(result
, 5, PyLong_FromLong(ru
.ru_isrss
));
93 PyStructSequence_SET_ITEM(result
, 6, PyLong_FromLong(ru
.ru_minflt
));
94 PyStructSequence_SET_ITEM(result
, 7, PyLong_FromLong(ru
.ru_majflt
));
95 PyStructSequence_SET_ITEM(result
, 8, PyLong_FromLong(ru
.ru_nswap
));
96 PyStructSequence_SET_ITEM(result
, 9, PyLong_FromLong(ru
.ru_inblock
));
97 PyStructSequence_SET_ITEM(result
, 10, PyLong_FromLong(ru
.ru_oublock
));
98 PyStructSequence_SET_ITEM(result
, 11, PyLong_FromLong(ru
.ru_msgsnd
));
99 PyStructSequence_SET_ITEM(result
, 12, PyLong_FromLong(ru
.ru_msgrcv
));
100 PyStructSequence_SET_ITEM(result
, 13, PyLong_FromLong(ru
.ru_nsignals
));
101 PyStructSequence_SET_ITEM(result
, 14, PyLong_FromLong(ru
.ru_nvcsw
));
102 PyStructSequence_SET_ITEM(result
, 15, PyLong_FromLong(ru
.ru_nivcsw
));
104 if (PyErr_Occurred()) {
114 resource_getrlimit(PyObject
*self
, PyObject
*args
)
119 if (!PyArg_ParseTuple(args
, "i:getrlimit", &resource
))
122 if (resource
< 0 || resource
>= RLIM_NLIMITS
) {
123 PyErr_SetString(PyExc_ValueError
,
124 "invalid resource specified");
128 if (getrlimit(resource
, &rl
) == -1) {
129 PyErr_SetFromErrno(ResourceError
);
133 #if defined(HAVE_LONG_LONG)
134 if (sizeof(rl
.rlim_cur
) > sizeof(long)) {
135 return Py_BuildValue("LL",
136 (PY_LONG_LONG
) rl
.rlim_cur
,
137 (PY_LONG_LONG
) rl
.rlim_max
);
140 return Py_BuildValue("ll", (long) rl
.rlim_cur
, (long) rl
.rlim_max
);
144 resource_setrlimit(PyObject
*self
, PyObject
*args
)
148 PyObject
*curobj
, *maxobj
;
150 if (!PyArg_ParseTuple(args
, "i(OO):setrlimit",
151 &resource
, &curobj
, &maxobj
))
154 if (resource
< 0 || resource
>= RLIM_NLIMITS
) {
155 PyErr_SetString(PyExc_ValueError
,
156 "invalid resource specified");
160 #if !defined(HAVE_LARGEFILE_SUPPORT)
161 rl
.rlim_cur
= PyLong_AsLong(curobj
);
162 if (rl
.rlim_cur
== (rlim_t
)-1 && PyErr_Occurred())
164 rl
.rlim_max
= PyLong_AsLong(maxobj
);
165 if (rl
.rlim_max
== (rlim_t
)-1 && PyErr_Occurred())
168 /* The limits are probably bigger than a long */
169 rl
.rlim_cur
= PyLong_AsLongLong(curobj
);
170 if (rl
.rlim_cur
== (rlim_t
)-1 && PyErr_Occurred())
172 rl
.rlim_max
= PyLong_AsLongLong(maxobj
);
173 if (rl
.rlim_max
== (rlim_t
)-1 && PyErr_Occurred())
177 rl
.rlim_cur
= rl
.rlim_cur
& RLIM_INFINITY
;
178 rl
.rlim_max
= rl
.rlim_max
& RLIM_INFINITY
;
179 if (setrlimit(resource
, &rl
) == -1) {
181 PyErr_SetString(PyExc_ValueError
,
182 "current limit exceeds maximum limit");
183 else if (errno
== EPERM
)
184 PyErr_SetString(PyExc_ValueError
,
185 "not allowed to raise maximum limit");
187 PyErr_SetFromErrno(ResourceError
);
195 resource_getpagesize(PyObject
*self
, PyObject
*unused
)
198 #if defined(HAVE_GETPAGESIZE)
199 pagesize
= getpagesize();
200 #elif defined(HAVE_SYSCONF)
201 #if defined(_SC_PAGE_SIZE)
202 pagesize
= sysconf(_SC_PAGE_SIZE
);
204 /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
205 pagesize
= sysconf(_SC_PAGESIZE
);
208 return Py_BuildValue("i", pagesize
);
212 /* List of functions */
214 static struct PyMethodDef
215 resource_methods
[] = {
216 {"getrusage", resource_getrusage
, METH_VARARGS
},
217 {"getrlimit", resource_getrlimit
, METH_VARARGS
},
218 {"setrlimit", resource_setrlimit
, METH_VARARGS
},
219 {"getpagesize", resource_getpagesize
, METH_NOARGS
},
220 {NULL
, NULL
} /* sentinel */
224 /* Module initialization */
227 static struct PyModuleDef resourcemodule
= {
228 PyModuleDef_HEAD_INIT
,
240 PyInit_resource(void)
244 /* Create the module and add the functions */
245 m
= PyModule_Create(&resourcemodule
);
249 /* Add some symbolic constants to the module */
250 if (ResourceError
== NULL
) {
251 ResourceError
= PyErr_NewException("resource.error",
254 Py_INCREF(ResourceError
);
255 PyModule_AddObject(m
, "error", ResourceError
);
257 PyStructSequence_InitType(&StructRUsageType
,
258 &struct_rusage_desc
);
259 Py_INCREF(&StructRUsageType
);
260 PyModule_AddObject(m
, "struct_rusage",
261 (PyObject
*) &StructRUsageType
);
263 /* insert constants */
265 PyModule_AddIntConstant(m
, "RLIMIT_CPU", RLIMIT_CPU
);
269 PyModule_AddIntConstant(m
, "RLIMIT_FSIZE", RLIMIT_FSIZE
);
273 PyModule_AddIntConstant(m
, "RLIMIT_DATA", RLIMIT_DATA
);
277 PyModule_AddIntConstant(m
, "RLIMIT_STACK", RLIMIT_STACK
);
281 PyModule_AddIntConstant(m
, "RLIMIT_CORE", RLIMIT_CORE
);
285 PyModule_AddIntConstant(m
, "RLIMIT_NOFILE", RLIMIT_NOFILE
);
289 PyModule_AddIntConstant(m
, "RLIMIT_OFILE", RLIMIT_OFILE
);
293 PyModule_AddIntConstant(m
, "RLIMIT_VMEM", RLIMIT_VMEM
);
297 PyModule_AddIntConstant(m
, "RLIMIT_AS", RLIMIT_AS
);
301 PyModule_AddIntConstant(m
, "RLIMIT_RSS", RLIMIT_RSS
);
305 PyModule_AddIntConstant(m
, "RLIMIT_NPROC", RLIMIT_NPROC
);
308 #ifdef RLIMIT_MEMLOCK
309 PyModule_AddIntConstant(m
, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK
);
313 PyModule_AddIntConstant(m
, "RLIMIT_SBSIZE", RLIMIT_SBSIZE
);
317 PyModule_AddIntConstant(m
, "RUSAGE_SELF", RUSAGE_SELF
);
320 #ifdef RUSAGE_CHILDREN
321 PyModule_AddIntConstant(m
, "RUSAGE_CHILDREN", RUSAGE_CHILDREN
);
325 PyModule_AddIntConstant(m
, "RUSAGE_BOTH", RUSAGE_BOTH
);
328 #if defined(HAVE_LONG_LONG)
329 if (sizeof(RLIM_INFINITY
) > sizeof(long)) {
330 v
= PyLong_FromLongLong((PY_LONG_LONG
) RLIM_INFINITY
);
334 v
= PyLong_FromLong((long) RLIM_INFINITY
);
337 PyModule_AddObject(m
, "RLIM_INFINITY", v
);