blob: 98a77efe332694696872a05229784fd04046bc8e [file] [log] [blame]
Guido van Rossumf84a5391997-08-15 00:04:24 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf84a5391997-08-15 00:04:24 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf84a5391997-08-15 00:04:24 +00009******************************************************************/
10
Guido van Rossum2e631391996-12-18 18:37:27 +000011#include "Python.h"
Guido van Rossum86aeb901997-10-31 16:55:51 +000012#include "mytime.h" /* needed for SunOS4.1 */
Guido van Rossum2e631391996-12-18 18:37:27 +000013#include <sys/resource.h>
14#include <sys/time.h>
15#include <unistd.h>
16#include <string.h>
17#include <errno.h>
18
Guido van Rossum607b33a1997-08-17 16:24:30 +000019/* On some systems, these aren't in any header file.
20 On others they are, with inconsistent prototypes.
21 We declare the (default) return type, to shut up gcc -Wall;
22 but we can't declare the prototype, to avoid errors
23 when the header files declare it different.
24 Worse, on some Linuxes, getpagesize() returns a size_t... */
25#ifndef linux
26int getrusage();
27int getpagesize();
Guido van Rossum668e4411997-08-05 02:24:57 +000028#endif
Guido van Rossum2e631391996-12-18 18:37:27 +000029
30#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
31
32static PyObject *ResourceError;
33
34static PyObject *
35resource_getrusage(self, args)
36 PyObject *self;
37 PyObject *args;
38{
39 int who;
40 struct rusage ru;
41
Guido van Rossum43713e52000-02-29 13:59:29 +000042 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
Guido van Rossum2e631391996-12-18 18:37:27 +000043 return NULL;
44
45 if (getrusage(who, &ru) == -1) {
46 if (errno == EINVAL) {
47 PyErr_SetString(PyExc_ValueError,
48 "invalid who parameter");
49 return NULL;
50 }
51 PyErr_SetFromErrno(ResourceError);
52 return NULL;
53 }
54
55 /* Yeah, this 16-tuple is way ugly. It's probably a lot less
56 ugly than a dictionary with keys (or object attributes)
57 named things like 'ixrss'.
58 */
59 return Py_BuildValue(
60 "ddiiiiiiiiiiiiii",
61 doubletime(ru.ru_utime), /* user time used */
62 doubletime(ru.ru_stime), /* system time used */
63 ru.ru_maxrss, /* max. resident set size */
64 ru.ru_ixrss, /* shared memory size */
65 ru.ru_idrss, /* unshared memory size */
66 ru.ru_isrss, /* unshared stack size */
67 ru.ru_minflt, /* page faults not requiring I/O*/
68 ru.ru_majflt, /* page faults requiring I/O */
69 ru.ru_nswap, /* number of swap outs */
70 ru.ru_inblock, /* block input operations */
71 ru.ru_oublock, /* block output operations */
72 ru.ru_msgsnd, /* messages sent */
73 ru.ru_msgrcv, /* messages received */
74 ru.ru_nsignals, /* signals received */
75 ru.ru_nvcsw, /* voluntary context switchs */
76 ru.ru_nivcsw /* involuntary context switchs */
77 );
78}
79
80
81static PyObject *
82resource_getrlimit(self, args)
83 PyObject *self;
84 PyObject *args;
85{
86 struct rlimit rl;
87 int resource;
Guido van Rossum2e631391996-12-18 18:37:27 +000088
Guido van Rossum43713e52000-02-29 13:59:29 +000089 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
Guido van Rossum2e631391996-12-18 18:37:27 +000090 return NULL;
91
92 if (resource < 0 || resource >= RLIM_NLIMITS) {
93 PyErr_SetString(PyExc_ValueError,
94 "invalid resource specified");
95 return NULL;
96 }
97
98 if (getrlimit(resource, &rl) == -1) {
99 PyErr_SetFromErrno(ResourceError);
100 return NULL;
101 }
Guido van Rossum763737b1999-01-06 18:44:57 +0000102
103#if defined(HAVE_LONG_LONG)
104 if (sizeof(rl.rlim_cur) > sizeof(long)) {
105 return Py_BuildValue("LL",
106 (LONG_LONG) rl.rlim_cur,
107 (LONG_LONG) rl.rlim_max);
108 }
109#endif
110 return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max);
Guido van Rossum2e631391996-12-18 18:37:27 +0000111}
112
113static PyObject *
114resource_setrlimit(self, args)
115 PyObject *self;
116 PyObject *args;
117{
118 struct rlimit rl;
119 int resource;
Guido van Rossum763737b1999-01-06 18:44:57 +0000120 PyObject *curobj, *maxobj;
Guido van Rossum2e631391996-12-18 18:37:27 +0000121
Guido van Rossum43713e52000-02-29 13:59:29 +0000122 if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj))
Guido van Rossum2e631391996-12-18 18:37:27 +0000123 return NULL;
124
125 if (resource < 0 || resource >= RLIM_NLIMITS) {
126 PyErr_SetString(PyExc_ValueError,
127 "invalid resource specified");
128 return NULL;
129 }
130
Guido van Rossum763737b1999-01-06 18:44:57 +0000131#if !defined(HAVE_LARGEFILE_SUPPORT)
132 rl.rlim_cur = PyInt_AsLong(curobj);
133 rl.rlim_max = PyInt_AsLong(maxobj);
134#else
135 /* The limits are probably bigger than a long */
136 rl.rlim_cur = PyLong_Check(curobj) ?
137 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
138 rl.rlim_max = PyLong_Check(maxobj) ?
139 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
140#endif
141
Guido van Rossum2e631391996-12-18 18:37:27 +0000142 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
143 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
144 if (setrlimit(resource, &rl) == -1) {
145 if (errno == EINVAL)
146 PyErr_SetString(PyExc_ValueError,
147 "current limit exceeds maximum limit");
148 else if (errno == EPERM)
149 PyErr_SetString(PyExc_ValueError,
150 "not allowed to raise maximum limit");
151 else
152 PyErr_SetFromErrno(ResourceError);
153 return NULL;
154 }
155 Py_INCREF(Py_None);
156 return Py_None;
157}
158
159static PyObject *
160resource_getpagesize(self, args)
161 PyObject *self;
162 PyObject *args;
163{
Guido van Rossum43713e52000-02-29 13:59:29 +0000164 if (!PyArg_ParseTuple(args, ":getpagesize"))
Guido van Rossum2e631391996-12-18 18:37:27 +0000165 return NULL;
166 return Py_BuildValue("i", getpagesize());
167}
168
169/* List of functions */
170
171static struct PyMethodDef
172resource_methods[] = {
173 {"getrusage", resource_getrusage, 1},
174 {"getrlimit", resource_getrlimit, 1},
175 {"setrlimit", resource_setrlimit, 1},
176 {"getpagesize", resource_getpagesize, 1},
177 {NULL, NULL} /* sentinel */
178};
179
180
181/* Module initialization */
182
183static void
184ins(PyObject *dict, char *name, int value)
185{
186 PyObject *v = PyInt_FromLong((long) value);
187 if (v) {
188 PyDict_SetItemString(dict, name, v);
189 Py_DECREF(v);
190 }
191 /* errors will be checked by initresource() */
192}
193
194void initresource()
195{
196 PyObject *m, *d;
197
198 /* Create the module and add the functions */
199 m = Py_InitModule("resource", resource_methods);
200
201 /* Add some symbolic constants to the module */
202 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000203 ResourceError = PyErr_NewException("resource.error", NULL, NULL);
Guido van Rossum2e631391996-12-18 18:37:27 +0000204 PyDict_SetItemString(d, "error", ResourceError);
205
206 /* insert constants */
207#ifdef RLIMIT_CPU
208 ins(d, "RLIMIT_CPU", RLIMIT_CPU);
209#endif
210
211#ifdef RLIMIT_FSIZE
212 ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
213#endif
214
215#ifdef RLIMIT_DATA
216 ins(d, "RLIMIT_DATA", RLIMIT_DATA);
217#endif
218
219#ifdef RLIMIT_STACK
220 ins(d, "RLIMIT_STACK", RLIMIT_STACK);
221#endif
222
223#ifdef RLIMIT_CORE
224 ins(d, "RLIMIT_CORE", RLIMIT_CORE);
225#endif
226
227#ifdef RLIMIT_NOFILE
228 ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
229#endif
230
231#ifdef RLIMIT_OFILE
232 ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
233#endif
234
235#ifdef RLIMIT_VMEM
236 ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
237#endif
238
239#ifdef RLIMIT_AS
240 ins(d, "RLIMIT_AS", RLIMIT_AS);
241#endif
242
243#ifdef RLIMIT_RSS
244 ins(d, "RLIMIT_RSS", RLIMIT_RSS);
245#endif
246
247#ifdef RLIMIT_NPROC
248 ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
249#endif
250
251#ifdef RLIMIT_MEMLOCK
252 ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
253#endif
254
255#ifdef RUSAGE_SELF
256 ins(d, "RUSAGE_SELF", RUSAGE_SELF);
257#endif
258
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000259#ifdef RUSAGE_CHILDREN
Guido van Rossum2e631391996-12-18 18:37:27 +0000260 ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
261#endif
262
263#ifdef RUSAGE_BOTH
264 ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
265#endif
Guido van Rossum2e631391996-12-18 18:37:27 +0000266}