blob: ddbf80be9c69eb0c51802049a82de87d74246bc5 [file] [log] [blame]
Guido van Rossumf84a5391997-08-15 00:04:24 +00001
Guido van Rossum2e631391996-12-18 18:37:27 +00002#include "Python.h"
3#include <sys/resource.h>
4#include <sys/time.h>
Guido van Rossum2e631391996-12-18 18:37:27 +00005#include <string.h>
6#include <errno.h>
Martin v. Löwisf26d63b2003-03-30 17:23:49 +00007#include <unistd.h>
Guido van Rossum2e631391996-12-18 18:37:27 +00008
Guido van Rossum607b33a1997-08-17 16:24:30 +00009/* On some systems, these aren't in any header file.
10 On others they are, with inconsistent prototypes.
11 We declare the (default) return type, to shut up gcc -Wall;
12 but we can't declare the prototype, to avoid errors
13 when the header files declare it different.
14 Worse, on some Linuxes, getpagesize() returns a size_t... */
Guido van Rossum2e631391996-12-18 18:37:27 +000015
16#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
17
Serhiy Storchaka19897632017-03-12 13:08:30 +020018/*[clinic input]
19module resource
20[clinic start generated code]*/
21/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e89d38ed52609d7c]*/
22
23/*[python input]
24class pid_t_converter(CConverter):
25 type = 'pid_t'
26 format_unit = '" _Py_PARSE_PID "'
27[python start generated code]*/
28/*[python end generated code: output=da39a3ee5e6b4b0d input=0c1d19f640d57e48]*/
29
30#include "clinic/resource.c.h"
31
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000032PyDoc_STRVAR(struct_rusage__doc__,
33"struct_rusage: Result from getrusage.\n\n"
34"This object may be accessed either as a tuple of\n"
35" (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
36" nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
37"or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
Martin v. Löwis688357e2002-04-08 21:28:20 +000038
39static PyStructSequence_Field struct_rusage_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 {"ru_utime", "user time used"},
41 {"ru_stime", "system time used"},
42 {"ru_maxrss", "max. resident set size"},
43 {"ru_ixrss", "shared memory size"},
44 {"ru_idrss", "unshared data size"},
45 {"ru_isrss", "unshared stack size"},
46 {"ru_minflt", "page faults not requiring I/O"},
47 {"ru_majflt", "page faults requiring I/O"},
48 {"ru_nswap", "number of swap outs"},
49 {"ru_inblock", "block input operations"},
50 {"ru_oublock", "block output operations"},
51 {"ru_msgsnd", "IPC messages sent"},
52 {"ru_msgrcv", "IPC messages received"},
53 {"ru_nsignals", "signals received"},
54 {"ru_nvcsw", "voluntary context switches"},
55 {"ru_nivcsw", "involuntary context switches"},
56 {0}
Martin v. Löwis688357e2002-04-08 21:28:20 +000057};
58
59static PyStructSequence_Desc struct_rusage_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 "resource.struct_rusage", /* name */
61 struct_rusage__doc__, /* doc */
62 struct_rusage_fields, /* fields */
63 16 /* n_in_sequence */
Martin v. Löwis688357e2002-04-08 21:28:20 +000064};
65
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066static int initialized;
Martin v. Löwis688357e2002-04-08 21:28:20 +000067static PyTypeObject StructRUsageType;
68
Serhiy Storchaka19897632017-03-12 13:08:30 +020069/*[clinic input]
70resource.getrusage
71
72 who: int
73 /
74
75[clinic start generated code]*/
76
Guido van Rossum2e631391996-12-18 18:37:27 +000077static PyObject *
Serhiy Storchaka19897632017-03-12 13:08:30 +020078resource_getrusage_impl(PyObject *module, int who)
79/*[clinic end generated code: output=8fad2880ba6a9843 input=5c857bcc5b9ccb1b]*/
Guido van Rossum2e631391996-12-18 18:37:27 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 struct rusage ru;
82 PyObject *result;
Guido van Rossum2e631391996-12-18 18:37:27 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (getrusage(who, &ru) == -1) {
85 if (errno == EINVAL) {
86 PyErr_SetString(PyExc_ValueError,
87 "invalid who parameter");
88 return NULL;
89 }
Benjamin Peterson2122cf72011-12-10 17:50:22 -050090 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return NULL;
92 }
Guido van Rossum2e631391996-12-18 18:37:27 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 result = PyStructSequence_New(&StructRUsageType);
95 if (!result)
96 return NULL;
Martin v. Löwis688357e2002-04-08 21:28:20 +000097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 PyStructSequence_SET_ITEM(result, 0,
99 PyFloat_FromDouble(doubletime(ru.ru_utime)));
100 PyStructSequence_SET_ITEM(result, 1,
101 PyFloat_FromDouble(doubletime(ru.ru_stime)));
102 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss));
103 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss));
104 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss));
105 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss));
106 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt));
107 PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt));
108 PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap));
109 PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock));
110 PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock));
111 PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd));
112 PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv));
113 PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals));
114 PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw));
115 PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw));
Martin v. Löwis688357e2002-04-08 21:28:20 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (PyErr_Occurred()) {
118 Py_DECREF(result);
119 return NULL;
120 }
Martin v. Löwis688357e2002-04-08 21:28:20 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 return result;
Guido van Rossum2e631391996-12-18 18:37:27 +0000123}
124
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200125static int
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200126py2rlimit(PyObject *limits, struct rlimit *rl_out)
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200127{
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200128 PyObject *curobj, *maxobj;
129 limits = PySequence_Tuple(limits);
130 if (!limits)
131 /* Here limits is a borrowed reference */
132 return -1;
133
134 if (PyTuple_GET_SIZE(limits) != 2) {
135 PyErr_SetString(PyExc_ValueError,
136 "expected a tuple of 2 integers");
137 goto error;
138 }
139 curobj = PyTuple_GET_ITEM(limits, 0);
140 maxobj = PyTuple_GET_ITEM(limits, 1);
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200141#if !defined(HAVE_LARGEFILE_SUPPORT)
142 rl_out->rlim_cur = PyLong_AsLong(curobj);
143 if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200144 goto error;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200145 rl_out->rlim_max = PyLong_AsLong(maxobj);
146 if (rl_out->rlim_max == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200147 goto error;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200148#else
149 /* The limits are probably bigger than a long */
150 rl_out->rlim_cur = PyLong_AsLongLong(curobj);
151 if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200152 goto error;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200153 rl_out->rlim_max = PyLong_AsLongLong(maxobj);
154 if (rl_out->rlim_max == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200155 goto error;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200156#endif
157
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200158 Py_DECREF(limits);
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200159 rl_out->rlim_cur = rl_out->rlim_cur & RLIM_INFINITY;
160 rl_out->rlim_max = rl_out->rlim_max & RLIM_INFINITY;
161 return 0;
162
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200163error:
164 Py_DECREF(limits);
165 return -1;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200166}
167
168static PyObject*
169rlimit2py(struct rlimit rl)
170{
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200171 if (sizeof(rl.rlim_cur) > sizeof(long)) {
172 return Py_BuildValue("LL",
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700173 (long long) rl.rlim_cur,
174 (long long) rl.rlim_max);
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200175 }
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200176 return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
177}
Guido van Rossum2e631391996-12-18 18:37:27 +0000178
Serhiy Storchaka19897632017-03-12 13:08:30 +0200179/*[clinic input]
180resource.getrlimit
181
182 resource: int
183 /
184
185[clinic start generated code]*/
186
Guido van Rossum2e631391996-12-18 18:37:27 +0000187static PyObject *
Serhiy Storchaka19897632017-03-12 13:08:30 +0200188resource_getrlimit_impl(PyObject *module, int resource)
189/*[clinic end generated code: output=98327b25061ffe39 input=a697cb0004cb3c36]*/
Guido van Rossum2e631391996-12-18 18:37:27 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 struct rlimit rl;
Guido van Rossum2e631391996-12-18 18:37:27 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (resource < 0 || resource >= RLIM_NLIMITS) {
194 PyErr_SetString(PyExc_ValueError,
195 "invalid resource specified");
196 return NULL;
197 }
Guido van Rossum2e631391996-12-18 18:37:27 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 if (getrlimit(resource, &rl) == -1) {
Benjamin Peterson2122cf72011-12-10 17:50:22 -0500200 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return NULL;
202 }
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200203 return rlimit2py(rl);
Guido van Rossum2e631391996-12-18 18:37:27 +0000204}
205
Serhiy Storchaka19897632017-03-12 13:08:30 +0200206/*[clinic input]
207resource.setrlimit
208
209 resource: int
210 limits: object
211 /
212
213[clinic start generated code]*/
214
Guido van Rossum2e631391996-12-18 18:37:27 +0000215static PyObject *
Serhiy Storchaka19897632017-03-12 13:08:30 +0200216resource_setrlimit_impl(PyObject *module, int resource, PyObject *limits)
217/*[clinic end generated code: output=4e82ec3f34d013d1 input=6235a6ce23b4ca75]*/
Guido van Rossum2e631391996-12-18 18:37:27 +0000218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 struct rlimit rl;
Guido van Rossum2e631391996-12-18 18:37:27 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (resource < 0 || resource >= RLIM_NLIMITS) {
222 PyErr_SetString(PyExc_ValueError,
223 "invalid resource specified");
224 return NULL;
225 }
Guido van Rossum2e631391996-12-18 18:37:27 +0000226
Saiyang Gou7514f4f2020-02-12 23:47:42 -0800227 if (PySys_Audit("resource.setrlimit", "iO", resource,
228 limits ? limits : Py_None) < 0) {
229 return NULL;
230 }
231
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200232 if (py2rlimit(limits, &rl) < 0) {
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200233 return NULL;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200234 }
Guido van Rossum763737b1999-01-06 18:44:57 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 if (setrlimit(resource, &rl) == -1) {
237 if (errno == EINVAL)
238 PyErr_SetString(PyExc_ValueError,
239 "current limit exceeds maximum limit");
240 else if (errno == EPERM)
241 PyErr_SetString(PyExc_ValueError,
242 "not allowed to raise maximum limit");
243 else
Benjamin Peterson2122cf72011-12-10 17:50:22 -0500244 PyErr_SetFromErrno(PyExc_OSError);
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200245 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 }
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200247 Py_RETURN_NONE;
Guido van Rossum2e631391996-12-18 18:37:27 +0000248}
249
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200250#ifdef HAVE_PRLIMIT
Serhiy Storchaka19897632017-03-12 13:08:30 +0200251/*[clinic input]
252resource.prlimit
253
254 pid: pid_t
255 resource: int
256 [
257 limits: object
258 ]
259 /
260
261[clinic start generated code]*/
262
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200263static PyObject *
Serhiy Storchaka19897632017-03-12 13:08:30 +0200264resource_prlimit_impl(PyObject *module, pid_t pid, int resource,
265 int group_right_1, PyObject *limits)
266/*[clinic end generated code: output=ee976b393187a7a3 input=b77743bdccc83564]*/
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200267{
268 struct rlimit old_limit, new_limit;
Serhiy Storchaka19897632017-03-12 13:08:30 +0200269 int retval;
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200270
271 if (resource < 0 || resource >= RLIM_NLIMITS) {
272 PyErr_SetString(PyExc_ValueError,
273 "invalid resource specified");
274 return NULL;
275 }
276
Saiyang Gou7514f4f2020-02-12 23:47:42 -0800277 if (PySys_Audit("resource.prlimit", "iiO", pid, resource,
278 limits ? limits : Py_None) < 0) {
279 return NULL;
280 }
281
Serhiy Storchaka19897632017-03-12 13:08:30 +0200282 if (group_right_1) {
Serhiy Storchakab94eef22016-12-19 08:04:15 +0200283 if (py2rlimit(limits, &new_limit) < 0) {
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200284 return NULL;
285 }
286 retval = prlimit(pid, resource, &new_limit, &old_limit);
287 }
288 else {
289 retval = prlimit(pid, resource, NULL, &old_limit);
290 }
291
292 if (retval == -1) {
293 if (errno == EINVAL) {
294 PyErr_SetString(PyExc_ValueError,
295 "current limit exceeds maximum limit");
296 } else {
297 PyErr_SetFromErrno(PyExc_OSError);
298 }
299 return NULL;
300 }
301 return rlimit2py(old_limit);
302}
303#endif /* HAVE_PRLIMIT */
304
Serhiy Storchaka19897632017-03-12 13:08:30 +0200305/*[clinic input]
306resource.getpagesize -> int
307[clinic start generated code]*/
308
309static int
310resource_getpagesize_impl(PyObject *module)
311/*[clinic end generated code: output=9ba93eb0f3d6c3a9 input=546545e8c1f42085]*/
Guido van Rossum2e631391996-12-18 18:37:27 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 long pagesize = 0;
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000314#if defined(HAVE_GETPAGESIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 pagesize = getpagesize();
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000316#elif defined(HAVE_SYSCONF)
Martin v. Löwis0cb3c632004-08-12 13:26:56 +0000317#if defined(_SC_PAGE_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 pagesize = sysconf(_SC_PAGE_SIZE);
Martin v. Löwis0cb3c632004-08-12 13:26:56 +0000319#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
321 pagesize = sysconf(_SC_PAGESIZE);
Martin v. Löwis0cb3c632004-08-12 13:26:56 +0000322#endif
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000323#endif
Serhiy Storchaka19897632017-03-12 13:08:30 +0200324 return pagesize;
Guido van Rossum2e631391996-12-18 18:37:27 +0000325}
326
327/* List of functions */
328
329static struct PyMethodDef
330resource_methods[] = {
Serhiy Storchaka19897632017-03-12 13:08:30 +0200331 RESOURCE_GETRUSAGE_METHODDEF
332 RESOURCE_GETRLIMIT_METHODDEF
333 RESOURCE_PRLIMIT_METHODDEF
334 RESOURCE_SETRLIMIT_METHODDEF
335 RESOURCE_GETPAGESIZE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 {NULL, NULL} /* sentinel */
Guido van Rossum2e631391996-12-18 18:37:27 +0000337};
338
339
340/* Module initialization */
341
Martin v. Löwis1a214512008-06-11 05:26:20 +0000342
Hai Shi45f70082020-04-02 20:35:08 +0800343static int
344resource_exec(PyObject *module)
Guido van Rossum2e631391996-12-18 18:37:27 +0000345{
Hai Shi45f70082020-04-02 20:35:08 +0800346#define ADD_INT(module, value) \
347 do { \
348 if (PyModule_AddIntConstant(module, #value, value) < 0) { \
349 return -1; \
350 } \
351 } while (0)
Guido van Rossum2e631391996-12-18 18:37:27 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 /* Add some symbolic constants to the module */
Benjamin Peterson2122cf72011-12-10 17:50:22 -0500354 Py_INCREF(PyExc_OSError);
Hai Shi45f70082020-04-02 20:35:08 +0800355 if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) {
356 Py_DECREF(PyExc_OSError);
357 return -1;
358 }
Victor Stinner1c8f0592013-07-22 22:24:54 +0200359 if (!initialized) {
360 if (PyStructSequence_InitType2(&StructRUsageType,
361 &struct_rusage_desc) < 0)
Hai Shi45f70082020-04-02 20:35:08 +0800362 return -1;
Victor Stinner1c8f0592013-07-22 22:24:54 +0200363 }
364
Hai Shi45f70082020-04-02 20:35:08 +0800365 if(PyModule_AddType(module, &StructRUsageType) < 0) {
366 return -1;
367 }
Guido van Rossum2e631391996-12-18 18:37:27 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* insert constants */
Guido van Rossum2e631391996-12-18 18:37:27 +0000370#ifdef RLIMIT_CPU
Hai Shi45f70082020-04-02 20:35:08 +0800371 ADD_INT(module, RLIMIT_CPU);
Guido van Rossum2e631391996-12-18 18:37:27 +0000372#endif
373
374#ifdef RLIMIT_FSIZE
Hai Shi45f70082020-04-02 20:35:08 +0800375 ADD_INT(module, RLIMIT_FSIZE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000376#endif
377
378#ifdef RLIMIT_DATA
Hai Shi45f70082020-04-02 20:35:08 +0800379 ADD_INT(module, RLIMIT_DATA);
Guido van Rossum2e631391996-12-18 18:37:27 +0000380#endif
381
382#ifdef RLIMIT_STACK
Hai Shi45f70082020-04-02 20:35:08 +0800383 ADD_INT(module, RLIMIT_STACK);
Guido van Rossum2e631391996-12-18 18:37:27 +0000384#endif
385
386#ifdef RLIMIT_CORE
Hai Shi45f70082020-04-02 20:35:08 +0800387 ADD_INT(module, RLIMIT_CORE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000388#endif
389
390#ifdef RLIMIT_NOFILE
Hai Shi45f70082020-04-02 20:35:08 +0800391 ADD_INT(module, RLIMIT_NOFILE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000392#endif
393
394#ifdef RLIMIT_OFILE
Hai Shi45f70082020-04-02 20:35:08 +0800395 ADD_INT(module, RLIMIT_OFILE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000396#endif
397
398#ifdef RLIMIT_VMEM
Hai Shi45f70082020-04-02 20:35:08 +0800399 ADD_INT(module, RLIMIT_VMEM);
Guido van Rossum2e631391996-12-18 18:37:27 +0000400#endif
401
402#ifdef RLIMIT_AS
Hai Shi45f70082020-04-02 20:35:08 +0800403 ADD_INT(module, RLIMIT_AS);
Guido van Rossum2e631391996-12-18 18:37:27 +0000404#endif
405
406#ifdef RLIMIT_RSS
Hai Shi45f70082020-04-02 20:35:08 +0800407 ADD_INT(module, RLIMIT_RSS);
Guido van Rossum2e631391996-12-18 18:37:27 +0000408#endif
409
410#ifdef RLIMIT_NPROC
Hai Shi45f70082020-04-02 20:35:08 +0800411 ADD_INT(module, RLIMIT_NPROC);
Guido van Rossum2e631391996-12-18 18:37:27 +0000412#endif
413
414#ifdef RLIMIT_MEMLOCK
Hai Shi45f70082020-04-02 20:35:08 +0800415 ADD_INT(module, RLIMIT_MEMLOCK);
Guido van Rossum2e631391996-12-18 18:37:27 +0000416#endif
417
Thomas Wouters89f507f2006-12-13 04:49:30 +0000418#ifdef RLIMIT_SBSIZE
Hai Shi45f70082020-04-02 20:35:08 +0800419 ADD_INT(module, RLIMIT_SBSIZE);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000420#endif
421
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200422/* Linux specific */
423#ifdef RLIMIT_MSGQUEUE
Hai Shi45f70082020-04-02 20:35:08 +0800424 ADD_INT(module, RLIMIT_MSGQUEUE);
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200425#endif
426
427#ifdef RLIMIT_NICE
Hai Shi45f70082020-04-02 20:35:08 +0800428 ADD_INT(module, RLIMIT_NICE);
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200429#endif
430
431#ifdef RLIMIT_RTPRIO
Hai Shi45f70082020-04-02 20:35:08 +0800432 ADD_INT(module, RLIMIT_RTPRIO);
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200433#endif
434
435#ifdef RLIMIT_RTTIME
Hai Shi45f70082020-04-02 20:35:08 +0800436 ADD_INT(module, RLIMIT_RTTIME);
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200437#endif
438
439#ifdef RLIMIT_SIGPENDING
Hai Shi45f70082020-04-02 20:35:08 +0800440 ADD_INT(module, RLIMIT_SIGPENDING);
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200441#endif
442
443/* target */
Guido van Rossum2e631391996-12-18 18:37:27 +0000444#ifdef RUSAGE_SELF
Hai Shi45f70082020-04-02 20:35:08 +0800445 ADD_INT(module, RUSAGE_SELF);
Guido van Rossum2e631391996-12-18 18:37:27 +0000446#endif
447
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000448#ifdef RUSAGE_CHILDREN
Hai Shi45f70082020-04-02 20:35:08 +0800449 ADD_INT(module, RUSAGE_CHILDREN);
Guido van Rossum2e631391996-12-18 18:37:27 +0000450#endif
451
452#ifdef RUSAGE_BOTH
Hai Shi45f70082020-04-02 20:35:08 +0800453 ADD_INT(module, RUSAGE_BOTH);
Guido van Rossum2e631391996-12-18 18:37:27 +0000454#endif
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000455
Antoine Pitroub6d4ee52010-11-17 16:19:35 +0000456#ifdef RUSAGE_THREAD
Hai Shi45f70082020-04-02 20:35:08 +0800457 ADD_INT(module, RUSAGE_THREAD);
Antoine Pitroub6d4ee52010-11-17 16:19:35 +0000458#endif
459
Christian Heimes5bb414d2013-12-08 14:35:55 +0100460/* FreeBSD specific */
461
462#ifdef RLIMIT_SWAP
Hai Shi45f70082020-04-02 20:35:08 +0800463 ADD_INT(module, RLIMIT_SWAP);
Christian Heimes5bb414d2013-12-08 14:35:55 +0100464#endif
465
466#ifdef RLIMIT_SBSIZE
Hai Shi45f70082020-04-02 20:35:08 +0800467 ADD_INT(module, RLIMIT_SBSIZE);
Christian Heimes5bb414d2013-12-08 14:35:55 +0100468#endif
469
470#ifdef RLIMIT_NPTS
Hai Shi45f70082020-04-02 20:35:08 +0800471 ADD_INT(module, RLIMIT_NPTS);
Christian Heimes5bb414d2013-12-08 14:35:55 +0100472#endif
473
Hai Shi45f70082020-04-02 20:35:08 +0800474 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (sizeof(RLIM_INFINITY) > sizeof(long)) {
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700476 v = PyLong_FromLongLong((long long) RLIM_INFINITY);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 {
479 v = PyLong_FromLong((long) RLIM_INFINITY);
480 }
Hai Shi45f70082020-04-02 20:35:08 +0800481 if (!v) {
482 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 }
Hai Shi45f70082020-04-02 20:35:08 +0800484
485 if (PyModule_AddObject(module, "RLIM_INFINITY", v) < 0) {
486 Py_DECREF(v);
487 return -1;
488 }
489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 initialized = 1;
Hai Shi45f70082020-04-02 20:35:08 +0800491 return 0;
492
493#undef ADD_INT
494}
495
496static struct PyModuleDef_Slot resource_slots[] = {
497 {Py_mod_exec, resource_exec},
498 {0, NULL}
499};
500
501static struct PyModuleDef resourcemodule = {
502 PyModuleDef_HEAD_INIT,
503 .m_name = "resource",
504 .m_size = 0,
505 .m_methods = resource_methods,
506 .m_slots = resource_slots,
507};
508
509PyMODINIT_FUNC
510PyInit_resource(void)
511{
512 return PyModuleDef_Init(&resourcemodule);
Guido van Rossum2e631391996-12-18 18:37:27 +0000513}