1997-08-15 00:04:24 +00:00
|
|
|
|
1996-12-18 18:37:27 +00:00
|
|
|
#include "Python.h"
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
1997-08-17 16:24:30 +00:00
|
|
|
/* On some systems, these aren't in any header file.
|
|
|
|
On others they are, with inconsistent prototypes.
|
|
|
|
We declare the (default) return type, to shut up gcc -Wall;
|
|
|
|
but we can't declare the prototype, to avoid errors
|
|
|
|
when the header files declare it different.
|
|
|
|
Worse, on some Linuxes, getpagesize() returns a size_t... */
|
1996-12-18 18:37:27 +00:00
|
|
|
|
|
|
|
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
|
|
|
|
|
|
|
|
static PyObject *ResourceError;
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 12:15:54 +00:00
|
|
|
resource_getrusage(PyObject *self, PyObject *args)
|
1996-12-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
int who;
|
|
|
|
struct rusage ru;
|
|
|
|
|
2000-02-29 13:59:29 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "i:getrusage", &who))
|
1996-12-18 18:37:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (getrusage(who, &ru) == -1) {
|
|
|
|
if (errno == EINVAL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid who parameter");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyErr_SetFromErrno(ResourceError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yeah, this 16-tuple is way ugly. It's probably a lot less
|
|
|
|
ugly than a dictionary with keys (or object attributes)
|
|
|
|
named things like 'ixrss'.
|
|
|
|
*/
|
|
|
|
return Py_BuildValue(
|
|
|
|
"ddiiiiiiiiiiiiii",
|
|
|
|
doubletime(ru.ru_utime), /* user time used */
|
|
|
|
doubletime(ru.ru_stime), /* system time used */
|
|
|
|
ru.ru_maxrss, /* max. resident set size */
|
|
|
|
ru.ru_ixrss, /* shared memory size */
|
|
|
|
ru.ru_idrss, /* unshared memory size */
|
|
|
|
ru.ru_isrss, /* unshared stack size */
|
|
|
|
ru.ru_minflt, /* page faults not requiring I/O*/
|
|
|
|
ru.ru_majflt, /* page faults requiring I/O */
|
|
|
|
ru.ru_nswap, /* number of swap outs */
|
|
|
|
ru.ru_inblock, /* block input operations */
|
|
|
|
ru.ru_oublock, /* block output operations */
|
|
|
|
ru.ru_msgsnd, /* messages sent */
|
|
|
|
ru.ru_msgrcv, /* messages received */
|
|
|
|
ru.ru_nsignals, /* signals received */
|
2000-07-16 12:04:32 +00:00
|
|
|
ru.ru_nvcsw, /* voluntary context switches */
|
|
|
|
ru.ru_nivcsw /* involuntary context switches */
|
1996-12-18 18:37:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 12:15:54 +00:00
|
|
|
resource_getrlimit(PyObject *self, PyObject *args)
|
1996-12-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
struct rlimit rl;
|
|
|
|
int resource;
|
|
|
|
|
2000-02-29 13:59:29 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
|
1996-12-18 18:37:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (resource < 0 || resource >= RLIM_NLIMITS) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid resource specified");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getrlimit(resource, &rl) == -1) {
|
|
|
|
PyErr_SetFromErrno(ResourceError);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-01-06 18:44:57 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_LONG_LONG)
|
|
|
|
if (sizeof(rl.rlim_cur) > sizeof(long)) {
|
|
|
|
return Py_BuildValue("LL",
|
|
|
|
(LONG_LONG) rl.rlim_cur,
|
|
|
|
(LONG_LONG) rl.rlim_max);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max);
|
1996-12-18 18:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 12:15:54 +00:00
|
|
|
resource_setrlimit(PyObject *self, PyObject *args)
|
1996-12-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
struct rlimit rl;
|
|
|
|
int resource;
|
1999-01-06 18:44:57 +00:00
|
|
|
PyObject *curobj, *maxobj;
|
1996-12-18 18:37:27 +00:00
|
|
|
|
2000-02-29 13:59:29 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj))
|
1996-12-18 18:37:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (resource < 0 || resource >= RLIM_NLIMITS) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid resource specified");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-01-06 18:44:57 +00:00
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
|
|
|
rl.rlim_cur = PyInt_AsLong(curobj);
|
|
|
|
rl.rlim_max = PyInt_AsLong(maxobj);
|
|
|
|
#else
|
|
|
|
/* The limits are probably bigger than a long */
|
|
|
|
rl.rlim_cur = PyLong_Check(curobj) ?
|
|
|
|
PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
|
|
|
|
rl.rlim_max = PyLong_Check(maxobj) ?
|
|
|
|
PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
|
|
|
|
#endif
|
|
|
|
|
1996-12-18 18:37:27 +00:00
|
|
|
rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
|
|
|
|
rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
|
|
|
|
if (setrlimit(resource, &rl) == -1) {
|
|
|
|
if (errno == EINVAL)
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"current limit exceeds maximum limit");
|
|
|
|
else if (errno == EPERM)
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"not allowed to raise maximum limit");
|
|
|
|
else
|
|
|
|
PyErr_SetFromErrno(ResourceError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 12:15:54 +00:00
|
|
|
resource_getpagesize(PyObject *self, PyObject *args)
|
1996-12-18 18:37:27 +00:00
|
|
|
{
|
2000-02-29 13:59:29 +00:00
|
|
|
if (!PyArg_ParseTuple(args, ":getpagesize"))
|
1996-12-18 18:37:27 +00:00
|
|
|
return NULL;
|
|
|
|
return Py_BuildValue("i", getpagesize());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* List of functions */
|
|
|
|
|
|
|
|
static struct PyMethodDef
|
|
|
|
resource_methods[] = {
|
2002-01-17 23:15:58 +00:00
|
|
|
{"getrusage", resource_getrusage, METH_VARARGS},
|
|
|
|
{"getrlimit", resource_getrlimit, METH_VARARGS},
|
|
|
|
{"setrlimit", resource_setrlimit, METH_VARARGS},
|
|
|
|
{"getpagesize", resource_getpagesize, METH_VARARGS},
|
1996-12-18 18:37:27 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Module initialization */
|
|
|
|
|
|
|
|
static void
|
|
|
|
ins(PyObject *dict, char *name, int value)
|
|
|
|
{
|
|
|
|
PyObject *v = PyInt_FromLong((long) value);
|
|
|
|
if (v) {
|
|
|
|
PyDict_SetItemString(dict, name, v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
|
|
|
/* errors will be checked by initresource() */
|
|
|
|
}
|
|
|
|
|
2001-09-17 16:19:16 +00:00
|
|
|
DL_EXPORT(void)
|
|
|
|
initresource(void)
|
1996-12-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
PyObject *m, *d;
|
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule("resource", resource_methods);
|
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
d = PyModule_GetDict(m);
|
1997-10-01 04:29:29 +00:00
|
|
|
ResourceError = PyErr_NewException("resource.error", NULL, NULL);
|
1996-12-18 18:37:27 +00:00
|
|
|
PyDict_SetItemString(d, "error", ResourceError);
|
|
|
|
|
|
|
|
/* insert constants */
|
|
|
|
#ifdef RLIMIT_CPU
|
|
|
|
ins(d, "RLIMIT_CPU", RLIMIT_CPU);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_FSIZE
|
|
|
|
ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_DATA
|
|
|
|
ins(d, "RLIMIT_DATA", RLIMIT_DATA);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_STACK
|
|
|
|
ins(d, "RLIMIT_STACK", RLIMIT_STACK);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_CORE
|
|
|
|
ins(d, "RLIMIT_CORE", RLIMIT_CORE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_OFILE
|
|
|
|
ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_VMEM
|
|
|
|
ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
ins(d, "RLIMIT_AS", RLIMIT_AS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_RSS
|
|
|
|
ins(d, "RLIMIT_RSS", RLIMIT_RSS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_NPROC
|
|
|
|
ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_MEMLOCK
|
|
|
|
ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RUSAGE_SELF
|
|
|
|
ins(d, "RUSAGE_SELF", RUSAGE_SELF);
|
|
|
|
#endif
|
|
|
|
|
1997-12-09 19:35:48 +00:00
|
|
|
#ifdef RUSAGE_CHILDREN
|
1996-12-18 18:37:27 +00:00
|
|
|
ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RUSAGE_BOTH
|
|
|
|
ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
|
|
|
|
#endif
|
|
|
|
}
|