1997-08-14 21:04:24 -03:00
|
|
|
|
1996-12-18 14:37:27 -04:00
|
|
|
#include "Python.h"
|
2002-04-08 18:28:20 -03:00
|
|
|
#include "structseq.h"
|
1996-12-18 14:37:27 -04:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2003-03-30 13:23:49 -04:00
|
|
|
/* for sysconf */
|
|
|
|
#if defined(HAVE_UNISTD_H)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1996-12-18 14:37:27 -04:00
|
|
|
|
1997-08-17 13:24:30 -03: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 14:37:27 -04:00
|
|
|
|
|
|
|
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
|
|
|
|
|
|
|
|
static PyObject *ResourceError;
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(struct_rusage__doc__,
|
|
|
|
"struct_rusage: Result from getrusage.\n\n"
|
|
|
|
"This object may be accessed either as a tuple of\n"
|
|
|
|
" (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
|
|
|
|
" nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
|
|
|
|
"or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
|
2002-04-08 18:28:20 -03:00
|
|
|
|
|
|
|
static PyStructSequence_Field struct_rusage_fields[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"ru_utime", "user time used"},
|
|
|
|
{"ru_stime", "system time used"},
|
|
|
|
{"ru_maxrss", "max. resident set size"},
|
|
|
|
{"ru_ixrss", "shared memory size"},
|
|
|
|
{"ru_idrss", "unshared data size"},
|
|
|
|
{"ru_isrss", "unshared stack size"},
|
|
|
|
{"ru_minflt", "page faults not requiring I/O"},
|
|
|
|
{"ru_majflt", "page faults requiring I/O"},
|
|
|
|
{"ru_nswap", "number of swap outs"},
|
|
|
|
{"ru_inblock", "block input operations"},
|
|
|
|
{"ru_oublock", "block output operations"},
|
|
|
|
{"ru_msgsnd", "IPC messages sent"},
|
|
|
|
{"ru_msgrcv", "IPC messages received"},
|
|
|
|
{"ru_nsignals", "signals received"},
|
|
|
|
{"ru_nvcsw", "voluntary context switches"},
|
|
|
|
{"ru_nivcsw", "involuntary context switches"},
|
|
|
|
{0}
|
2002-04-08 18:28:20 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyStructSequence_Desc struct_rusage_desc = {
|
2010-05-09 11:46:46 -03:00
|
|
|
"resource.struct_rusage", /* name */
|
|
|
|
struct_rusage__doc__, /* doc */
|
|
|
|
struct_rusage_fields, /* fields */
|
|
|
|
16 /* n_in_sequence */
|
2002-04-08 18:28:20 -03:00
|
|
|
};
|
|
|
|
|
2006-04-16 15:55:50 -03:00
|
|
|
static int initialized;
|
2002-04-08 18:28:20 -03:00
|
|
|
static PyTypeObject StructRUsageType;
|
|
|
|
|
1996-12-18 14:37:27 -04:00
|
|
|
static PyObject *
|
2000-07-10 09:15:54 -03:00
|
|
|
resource_getrusage(PyObject *self, PyObject *args)
|
1996-12-18 14:37:27 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int who;
|
|
|
|
struct rusage ru;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "i:getrusage", &who))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (getrusage(who, &ru) == -1) {
|
|
|
|
if (errno == EINVAL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid who parameter");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyErr_SetFromErrno(ResourceError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = PyStructSequence_New(&StructRUsageType);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
PyStructSequence_SET_ITEM(result, 0,
|
|
|
|
PyFloat_FromDouble(doubletime(ru.ru_utime)));
|
|
|
|
PyStructSequence_SET_ITEM(result, 1,
|
|
|
|
PyFloat_FromDouble(doubletime(ru.ru_stime)));
|
|
|
|
PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
|
|
|
|
PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
|
|
|
|
PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
|
|
|
|
PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));
|
|
|
|
PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));
|
|
|
|
PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));
|
|
|
|
PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));
|
|
|
|
PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));
|
|
|
|
PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));
|
|
|
|
PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));
|
|
|
|
PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));
|
|
|
|
PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
|
|
|
|
PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
|
|
|
|
PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1996-12-18 14:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 09:15:54 -03:00
|
|
|
resource_getrlimit(PyObject *self, PyObject *args)
|
1996-12-18 14:37:27 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
struct rlimit rl;
|
|
|
|
int resource;
|
1996-12-18 14:37:27 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
|
|
|
|
return NULL;
|
1996-12-18 14:37:27 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (resource < 0 || resource >= RLIM_NLIMITS) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid resource specified");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-18 14:37:27 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (getrlimit(resource, &rl) == -1) {
|
|
|
|
PyErr_SetFromErrno(ResourceError);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-01-06 14:44:57 -04:00
|
|
|
|
|
|
|
#if defined(HAVE_LONG_LONG)
|
2010-05-09 11:46:46 -03:00
|
|
|
if (sizeof(rl.rlim_cur) > sizeof(long)) {
|
|
|
|
return Py_BuildValue("LL",
|
|
|
|
(PY_LONG_LONG) rl.rlim_cur,
|
|
|
|
(PY_LONG_LONG) rl.rlim_max);
|
|
|
|
}
|
1999-01-06 14:44:57 -04:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
|
1996-12-18 14:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 09:15:54 -03:00
|
|
|
resource_setrlimit(PyObject *self, PyObject *args)
|
1996-12-18 14:37:27 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
struct rlimit rl;
|
|
|
|
int resource;
|
2013-02-04 06:45:46 -04:00
|
|
|
PyObject *limits, *curobj, *maxobj;
|
1996-12-18 14:37:27 -04:00
|
|
|
|
2013-02-04 06:45:46 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
1996-12-18 14:37:27 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (resource < 0 || resource >= RLIM_NLIMITS) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid resource specified");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-18 14:37:27 -04:00
|
|
|
|
2013-02-04 06:45:46 -04:00
|
|
|
limits = PySequence_Tuple(limits);
|
|
|
|
if (!limits)
|
|
|
|
/* Here limits is a borrowed reference */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (PyTuple_GET_SIZE(limits) != 2) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"expected a tuple of 2 integers");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
curobj = PyTuple_GET_ITEM(limits, 0);
|
|
|
|
maxobj = PyTuple_GET_ITEM(limits, 1);
|
|
|
|
|
1999-01-06 14:44:57 -04:00
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
2010-05-09 11:46:46 -03:00
|
|
|
rl.rlim_cur = PyInt_AsLong(curobj);
|
|
|
|
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
|
2013-02-04 06:45:46 -04:00
|
|
|
goto error;
|
2010-05-09 11:46:46 -03:00
|
|
|
rl.rlim_max = PyInt_AsLong(maxobj);
|
|
|
|
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
|
2013-02-04 06:45:46 -04:00
|
|
|
goto error;
|
1999-01-06 14:44:57 -04:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
/* The limits are probably bigger than a long */
|
|
|
|
rl.rlim_cur = PyLong_Check(curobj) ?
|
|
|
|
PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
|
|
|
|
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
|
2013-02-04 06:45:46 -04:00
|
|
|
goto error;
|
2010-05-09 11:46:46 -03:00
|
|
|
rl.rlim_max = PyLong_Check(maxobj) ?
|
|
|
|
PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
|
|
|
|
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
|
2013-02-04 06:45:46 -04:00
|
|
|
goto error;
|
1999-01-06 14:44:57 -04:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 11:46:46 -03: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);
|
2013-02-04 06:45:46 -04:00
|
|
|
goto error;
|
2010-05-09 11:46:46 -03:00
|
|
|
}
|
2013-02-04 06:45:46 -04:00
|
|
|
Py_DECREF(limits);
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2013-02-04 06:45:46 -04:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(limits);
|
|
|
|
return NULL;
|
1996-12-18 14:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2006-05-29 18:04:52 -03:00
|
|
|
resource_getpagesize(PyObject *self, PyObject *unused)
|
1996-12-18 14:37:27 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
long pagesize = 0;
|
2003-03-30 13:23:49 -04:00
|
|
|
#if defined(HAVE_GETPAGESIZE)
|
2010-05-09 11:46:46 -03:00
|
|
|
pagesize = getpagesize();
|
2003-03-30 13:23:49 -04:00
|
|
|
#elif defined(HAVE_SYSCONF)
|
2004-08-12 10:26:56 -03:00
|
|
|
#if defined(_SC_PAGE_SIZE)
|
2010-05-09 11:46:46 -03:00
|
|
|
pagesize = sysconf(_SC_PAGE_SIZE);
|
2004-08-12 10:26:56 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
|
|
|
|
pagesize = sysconf(_SC_PAGESIZE);
|
2004-08-12 10:26:56 -03:00
|
|
|
#endif
|
2003-03-30 13:23:49 -04:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
return Py_BuildValue("i", pagesize);
|
2003-03-30 13:23:49 -04:00
|
|
|
|
1996-12-18 14:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List of functions */
|
|
|
|
|
|
|
|
static struct PyMethodDef
|
|
|
|
resource_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"getrusage", resource_getrusage, METH_VARARGS},
|
|
|
|
{"getrlimit", resource_getrlimit, METH_VARARGS},
|
|
|
|
{"setrlimit", resource_setrlimit, METH_VARARGS},
|
|
|
|
{"getpagesize", resource_getpagesize, METH_NOARGS},
|
|
|
|
{NULL, NULL} /* sentinel */
|
1996-12-18 14:37:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Module initialization */
|
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2001-09-17 13:19:16 -03:00
|
|
|
initresource(void)
|
1996-12-18 14:37:27 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m, *v;
|
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule("resource", resource_methods);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
if (ResourceError == NULL) {
|
|
|
|
ResourceError = PyErr_NewException("resource.error",
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
Py_INCREF(ResourceError);
|
|
|
|
PyModule_AddObject(m, "error", ResourceError);
|
|
|
|
if (!initialized)
|
|
|
|
PyStructSequence_InitType(&StructRUsageType,
|
|
|
|
&struct_rusage_desc);
|
|
|
|
Py_INCREF(&StructRUsageType);
|
|
|
|
PyModule_AddObject(m, "struct_rusage",
|
|
|
|
(PyObject*) &StructRUsageType);
|
|
|
|
|
|
|
|
/* insert constants */
|
1996-12-18 14:37:27 -04:00
|
|
|
#ifdef RLIMIT_CPU
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_FSIZE
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_DATA
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_STACK
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_CORE
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_NOFILE
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_OFILE
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_VMEM
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_RSS
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_NPROC
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RLIMIT_MEMLOCK
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
2006-09-16 14:36:37 -03:00
|
|
|
#ifdef RLIMIT_SBSIZE
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);
|
2006-09-16 14:36:37 -03:00
|
|
|
#endif
|
|
|
|
|
1996-12-18 14:37:27 -04:00
|
|
|
#ifdef RUSAGE_SELF
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
1997-12-09 15:35:48 -04:00
|
|
|
#ifdef RUSAGE_CHILDREN
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RUSAGE_BOTH
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
|
1996-12-18 14:37:27 -04:00
|
|
|
#endif
|
2002-03-24 18:27:39 -04:00
|
|
|
|
|
|
|
#if defined(HAVE_LONG_LONG)
|
2010-05-09 11:46:46 -03:00
|
|
|
if (sizeof(RLIM_INFINITY) > sizeof(long)) {
|
|
|
|
v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
|
|
|
|
} else
|
2002-03-24 18:27:39 -04:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
{
|
|
|
|
v = PyInt_FromLong((long) RLIM_INFINITY);
|
|
|
|
}
|
|
|
|
if (v) {
|
|
|
|
PyModule_AddObject(m, "RLIM_INFINITY", v);
|
|
|
|
}
|
|
|
|
initialized = 1;
|
1996-12-18 14:37:27 -04:00
|
|
|
}
|