Guido van Rossum | f84a539 | 1997-08-15 00:04:24 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 2 | #include "Python.h" |
| 3 | #include <sys/resource.h> |
| 4 | #include <sys/time.h> |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <errno.h> |
Martin v. Löwis | f26d63b | 2003-03-30 17:23:49 +0000 | [diff] [blame] | 7 | /* for sysconf */ |
| 8 | #if defined(HAVE_UNISTD_H) |
| 9 | #include <unistd.h> |
| 10 | #endif |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 607b33a | 1997-08-17 16:24:30 +0000 | [diff] [blame] | 12 | /* On some systems, these aren't in any header file. |
| 13 | On others they are, with inconsistent prototypes. |
| 14 | We declare the (default) return type, to shut up gcc -Wall; |
| 15 | but we can't declare the prototype, to avoid errors |
| 16 | when the header files declare it different. |
| 17 | Worse, on some Linuxes, getpagesize() returns a size_t... */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 18 | |
| 19 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 20 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 21 | PyDoc_STRVAR(struct_rusage__doc__, |
| 22 | "struct_rusage: Result from getrusage.\n\n" |
| 23 | "This object may be accessed either as a tuple of\n" |
| 24 | " (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n" |
| 25 | " nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n" |
| 26 | "or via the attributes ru_utime, ru_stime, ru_maxrss, and so on."); |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 27 | |
| 28 | static PyStructSequence_Field struct_rusage_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | {"ru_utime", "user time used"}, |
| 30 | {"ru_stime", "system time used"}, |
| 31 | {"ru_maxrss", "max. resident set size"}, |
| 32 | {"ru_ixrss", "shared memory size"}, |
| 33 | {"ru_idrss", "unshared data size"}, |
| 34 | {"ru_isrss", "unshared stack size"}, |
| 35 | {"ru_minflt", "page faults not requiring I/O"}, |
| 36 | {"ru_majflt", "page faults requiring I/O"}, |
| 37 | {"ru_nswap", "number of swap outs"}, |
| 38 | {"ru_inblock", "block input operations"}, |
| 39 | {"ru_oublock", "block output operations"}, |
| 40 | {"ru_msgsnd", "IPC messages sent"}, |
| 41 | {"ru_msgrcv", "IPC messages received"}, |
| 42 | {"ru_nsignals", "signals received"}, |
| 43 | {"ru_nvcsw", "voluntary context switches"}, |
| 44 | {"ru_nivcsw", "involuntary context switches"}, |
| 45 | {0} |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static PyStructSequence_Desc struct_rusage_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | "resource.struct_rusage", /* name */ |
| 50 | struct_rusage__doc__, /* doc */ |
| 51 | struct_rusage_fields, /* fields */ |
| 52 | 16 /* n_in_sequence */ |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 55 | static int initialized; |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 56 | static PyTypeObject StructRUsageType; |
| 57 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 58 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 59 | resource_getrusage(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 60 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | int who; |
| 62 | struct rusage ru; |
| 63 | PyObject *result; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (!PyArg_ParseTuple(args, "i:getrusage", &who)) |
| 66 | return NULL; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | if (getrusage(who, &ru) == -1) { |
| 69 | if (errno == EINVAL) { |
| 70 | PyErr_SetString(PyExc_ValueError, |
| 71 | "invalid who parameter"); |
| 72 | return NULL; |
| 73 | } |
Benjamin Peterson | 2122cf7 | 2011-12-10 17:50:22 -0500 | [diff] [blame] | 74 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | result = PyStructSequence_New(&StructRUsageType); |
| 79 | if (!result) |
| 80 | return NULL; |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 81 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | PyStructSequence_SET_ITEM(result, 0, |
| 83 | PyFloat_FromDouble(doubletime(ru.ru_utime))); |
| 84 | PyStructSequence_SET_ITEM(result, 1, |
| 85 | PyFloat_FromDouble(doubletime(ru.ru_stime))); |
| 86 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss)); |
| 87 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss)); |
| 88 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss)); |
| 89 | PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss)); |
| 90 | PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt)); |
| 91 | PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt)); |
| 92 | PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap)); |
| 93 | PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock)); |
| 94 | PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock)); |
| 95 | PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd)); |
| 96 | PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv)); |
| 97 | PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals)); |
| 98 | PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw)); |
| 99 | PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw)); |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | if (PyErr_Occurred()) { |
| 102 | Py_DECREF(result); |
| 103 | return NULL; |
| 104 | } |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | return result; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 109 | static int |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 110 | py2rlimit(PyObject *limits, struct rlimit *rl_out) |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 111 | { |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 112 | PyObject *curobj, *maxobj; |
| 113 | limits = PySequence_Tuple(limits); |
| 114 | if (!limits) |
| 115 | /* Here limits is a borrowed reference */ |
| 116 | return -1; |
| 117 | |
| 118 | if (PyTuple_GET_SIZE(limits) != 2) { |
| 119 | PyErr_SetString(PyExc_ValueError, |
| 120 | "expected a tuple of 2 integers"); |
| 121 | goto error; |
| 122 | } |
| 123 | curobj = PyTuple_GET_ITEM(limits, 0); |
| 124 | maxobj = PyTuple_GET_ITEM(limits, 1); |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 125 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 126 | rl_out->rlim_cur = PyLong_AsLong(curobj); |
| 127 | if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred()) |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 128 | goto error; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 129 | rl_out->rlim_max = PyLong_AsLong(maxobj); |
| 130 | if (rl_out->rlim_max == (rlim_t)-1 && PyErr_Occurred()) |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 131 | goto error; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 132 | #else |
| 133 | /* The limits are probably bigger than a long */ |
| 134 | rl_out->rlim_cur = PyLong_AsLongLong(curobj); |
| 135 | if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred()) |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 136 | goto error; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 137 | rl_out->rlim_max = PyLong_AsLongLong(maxobj); |
| 138 | if (rl_out->rlim_max == (rlim_t)-1 && PyErr_Occurred()) |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 139 | goto error; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 140 | #endif |
| 141 | |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 142 | Py_DECREF(limits); |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 143 | rl_out->rlim_cur = rl_out->rlim_cur & RLIM_INFINITY; |
| 144 | rl_out->rlim_max = rl_out->rlim_max & RLIM_INFINITY; |
| 145 | return 0; |
| 146 | |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 147 | error: |
| 148 | Py_DECREF(limits); |
| 149 | return -1; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static PyObject* |
| 153 | rlimit2py(struct rlimit rl) |
| 154 | { |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 155 | if (sizeof(rl.rlim_cur) > sizeof(long)) { |
| 156 | return Py_BuildValue("LL", |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 157 | (long long) rl.rlim_cur, |
| 158 | (long long) rl.rlim_max); |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 159 | } |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 160 | return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max); |
| 161 | } |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 162 | |
| 163 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 164 | resource_getrlimit(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | struct rlimit rl; |
| 167 | int resource; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | if (!PyArg_ParseTuple(args, "i:getrlimit", &resource)) |
| 170 | return NULL; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 173 | PyErr_SetString(PyExc_ValueError, |
| 174 | "invalid resource specified"); |
| 175 | return NULL; |
| 176 | } |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | if (getrlimit(resource, &rl) == -1) { |
Benjamin Peterson | 2122cf7 | 2011-12-10 17:50:22 -0500 | [diff] [blame] | 179 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 182 | return rlimit2py(rl); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 186 | resource_setrlimit(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | struct rlimit rl; |
| 189 | int resource; |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 190 | PyObject *limits; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 191 | |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 192 | if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | return NULL; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 196 | PyErr_SetString(PyExc_ValueError, |
| 197 | "invalid resource specified"); |
| 198 | return NULL; |
| 199 | } |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 200 | |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 201 | if (py2rlimit(limits, &rl) < 0) { |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 202 | return NULL; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 203 | } |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | if (setrlimit(resource, &rl) == -1) { |
| 206 | if (errno == EINVAL) |
| 207 | PyErr_SetString(PyExc_ValueError, |
| 208 | "current limit exceeds maximum limit"); |
| 209 | else if (errno == EPERM) |
| 210 | PyErr_SetString(PyExc_ValueError, |
| 211 | "not allowed to raise maximum limit"); |
| 212 | else |
Benjamin Peterson | 2122cf7 | 2011-12-10 17:50:22 -0500 | [diff] [blame] | 213 | PyErr_SetFromErrno(PyExc_OSError); |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 214 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | } |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 216 | Py_RETURN_NONE; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 219 | #ifdef HAVE_PRLIMIT |
| 220 | static PyObject * |
| 221 | resource_prlimit(PyObject *self, PyObject *args) |
| 222 | { |
| 223 | struct rlimit old_limit, new_limit; |
| 224 | int resource, retval; |
| 225 | pid_t pid; |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 226 | PyObject *limits = NULL; |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 227 | |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 228 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|O:prlimit", |
| 229 | &pid, &resource, &limits)) |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 230 | return NULL; |
| 231 | |
| 232 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 233 | PyErr_SetString(PyExc_ValueError, |
| 234 | "invalid resource specified"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
Serhiy Storchaka | b94eef2 | 2016-12-19 08:04:15 +0200 | [diff] [blame] | 238 | if (limits != NULL) { |
| 239 | if (py2rlimit(limits, &new_limit) < 0) { |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 240 | return NULL; |
| 241 | } |
| 242 | retval = prlimit(pid, resource, &new_limit, &old_limit); |
| 243 | } |
| 244 | else { |
| 245 | retval = prlimit(pid, resource, NULL, &old_limit); |
| 246 | } |
| 247 | |
| 248 | if (retval == -1) { |
| 249 | if (errno == EINVAL) { |
| 250 | PyErr_SetString(PyExc_ValueError, |
| 251 | "current limit exceeds maximum limit"); |
| 252 | } else { |
| 253 | PyErr_SetFromErrno(PyExc_OSError); |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | return rlimit2py(old_limit); |
| 258 | } |
| 259 | #endif /* HAVE_PRLIMIT */ |
| 260 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 261 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 262 | resource_getpagesize(PyObject *self, PyObject *unused) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | long pagesize = 0; |
Martin v. Löwis | f26d63b | 2003-03-30 17:23:49 +0000 | [diff] [blame] | 265 | #if defined(HAVE_GETPAGESIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | pagesize = getpagesize(); |
Martin v. Löwis | f26d63b | 2003-03-30 17:23:49 +0000 | [diff] [blame] | 267 | #elif defined(HAVE_SYSCONF) |
Martin v. Löwis | 0cb3c63 | 2004-08-12 13:26:56 +0000 | [diff] [blame] | 268 | #if defined(_SC_PAGE_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | pagesize = sysconf(_SC_PAGE_SIZE); |
Martin v. Löwis | 0cb3c63 | 2004-08-12 13:26:56 +0000 | [diff] [blame] | 270 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */ |
| 272 | pagesize = sysconf(_SC_PAGESIZE); |
Martin v. Löwis | 0cb3c63 | 2004-08-12 13:26:56 +0000 | [diff] [blame] | 273 | #endif |
Martin v. Löwis | f26d63b | 2003-03-30 17:23:49 +0000 | [diff] [blame] | 274 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | return Py_BuildValue("i", pagesize); |
Martin v. Löwis | f26d63b | 2003-03-30 17:23:49 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* List of functions */ |
| 280 | |
| 281 | static struct PyMethodDef |
| 282 | resource_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | {"getrusage", resource_getrusage, METH_VARARGS}, |
| 284 | {"getrlimit", resource_getrlimit, METH_VARARGS}, |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 285 | #ifdef HAVE_PRLIMIT |
| 286 | {"prlimit", resource_prlimit, METH_VARARGS}, |
| 287 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | {"setrlimit", resource_setrlimit, METH_VARARGS}, |
| 289 | {"getpagesize", resource_getpagesize, METH_NOARGS}, |
| 290 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | |
| 294 | /* Module initialization */ |
| 295 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 296 | |
| 297 | static struct PyModuleDef resourcemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | PyModuleDef_HEAD_INIT, |
| 299 | "resource", |
| 300 | NULL, |
| 301 | -1, |
| 302 | resource_methods, |
| 303 | NULL, |
| 304 | NULL, |
| 305 | NULL, |
| 306 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 309 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 310 | PyInit_resource(void) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | PyObject *m, *v; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | /* Create the module and add the functions */ |
| 315 | m = PyModule_Create(&resourcemodule); |
| 316 | if (m == NULL) |
| 317 | return NULL; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | /* Add some symbolic constants to the module */ |
Benjamin Peterson | 2122cf7 | 2011-12-10 17:50:22 -0500 | [diff] [blame] | 320 | Py_INCREF(PyExc_OSError); |
| 321 | PyModule_AddObject(m, "error", PyExc_OSError); |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 322 | if (!initialized) { |
| 323 | if (PyStructSequence_InitType2(&StructRUsageType, |
| 324 | &struct_rusage_desc) < 0) |
| 325 | return NULL; |
| 326 | } |
| 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | Py_INCREF(&StructRUsageType); |
| 329 | PyModule_AddObject(m, "struct_rusage", |
| 330 | (PyObject*) &StructRUsageType); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | /* insert constants */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 333 | #ifdef RLIMIT_CPU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 334 | PyModule_AddIntMacro(m, RLIMIT_CPU); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 335 | #endif |
| 336 | |
| 337 | #ifdef RLIMIT_FSIZE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 338 | PyModule_AddIntMacro(m, RLIMIT_FSIZE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 339 | #endif |
| 340 | |
| 341 | #ifdef RLIMIT_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 342 | PyModule_AddIntMacro(m, RLIMIT_DATA); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 343 | #endif |
| 344 | |
| 345 | #ifdef RLIMIT_STACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 346 | PyModule_AddIntMacro(m, RLIMIT_STACK); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 347 | #endif |
| 348 | |
| 349 | #ifdef RLIMIT_CORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 350 | PyModule_AddIntMacro(m, RLIMIT_CORE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 351 | #endif |
| 352 | |
| 353 | #ifdef RLIMIT_NOFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 354 | PyModule_AddIntMacro(m, RLIMIT_NOFILE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 355 | #endif |
| 356 | |
| 357 | #ifdef RLIMIT_OFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 358 | PyModule_AddIntMacro(m, RLIMIT_OFILE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 359 | #endif |
| 360 | |
| 361 | #ifdef RLIMIT_VMEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 362 | PyModule_AddIntMacro(m, RLIMIT_VMEM); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 363 | #endif |
| 364 | |
| 365 | #ifdef RLIMIT_AS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 366 | PyModule_AddIntMacro(m, RLIMIT_AS); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
| 369 | #ifdef RLIMIT_RSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 370 | PyModule_AddIntMacro(m, RLIMIT_RSS); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
| 373 | #ifdef RLIMIT_NPROC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 374 | PyModule_AddIntMacro(m, RLIMIT_NPROC); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 375 | #endif |
| 376 | |
| 377 | #ifdef RLIMIT_MEMLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 378 | PyModule_AddIntMacro(m, RLIMIT_MEMLOCK); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 379 | #endif |
| 380 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 381 | #ifdef RLIMIT_SBSIZE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 382 | PyModule_AddIntMacro(m, RLIMIT_SBSIZE); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 383 | #endif |
| 384 | |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 385 | /* Linux specific */ |
| 386 | #ifdef RLIMIT_MSGQUEUE |
| 387 | PyModule_AddIntMacro(m, RLIMIT_MSGQUEUE); |
| 388 | #endif |
| 389 | |
| 390 | #ifdef RLIMIT_NICE |
| 391 | PyModule_AddIntMacro(m, RLIMIT_NICE); |
| 392 | #endif |
| 393 | |
| 394 | #ifdef RLIMIT_RTPRIO |
| 395 | PyModule_AddIntMacro(m, RLIMIT_RTPRIO); |
| 396 | #endif |
| 397 | |
| 398 | #ifdef RLIMIT_RTTIME |
| 399 | PyModule_AddIntMacro(m, RLIMIT_RTTIME); |
| 400 | #endif |
| 401 | |
| 402 | #ifdef RLIMIT_SIGPENDING |
| 403 | PyModule_AddIntMacro(m, RLIMIT_SIGPENDING); |
| 404 | #endif |
| 405 | |
| 406 | /* target */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 407 | #ifdef RUSAGE_SELF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 408 | PyModule_AddIntMacro(m, RUSAGE_SELF); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 409 | #endif |
| 410 | |
Guido van Rossum | ebd05eb | 1997-12-09 19:35:48 +0000 | [diff] [blame] | 411 | #ifdef RUSAGE_CHILDREN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 412 | PyModule_AddIntMacro(m, RUSAGE_CHILDREN); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 413 | #endif |
| 414 | |
| 415 | #ifdef RUSAGE_BOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 416 | PyModule_AddIntMacro(m, RUSAGE_BOTH); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 417 | #endif |
Neil Schemenauer | 29ac3cb | 2002-03-24 22:27:39 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | b6d4ee5 | 2010-11-17 16:19:35 +0000 | [diff] [blame] | 419 | #ifdef RUSAGE_THREAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 420 | PyModule_AddIntMacro(m, RUSAGE_THREAD); |
Antoine Pitrou | b6d4ee5 | 2010-11-17 16:19:35 +0000 | [diff] [blame] | 421 | #endif |
| 422 | |
Christian Heimes | 5bb414d | 2013-12-08 14:35:55 +0100 | [diff] [blame] | 423 | /* FreeBSD specific */ |
| 424 | |
| 425 | #ifdef RLIMIT_SWAP |
| 426 | PyModule_AddIntMacro(m, RLIMIT_SWAP); |
| 427 | #endif |
| 428 | |
| 429 | #ifdef RLIMIT_SBSIZE |
| 430 | PyModule_AddIntMacro(m, RLIMIT_SBSIZE); |
| 431 | #endif |
| 432 | |
| 433 | #ifdef RLIMIT_NPTS |
| 434 | PyModule_AddIntMacro(m, RLIMIT_NPTS); |
| 435 | #endif |
| 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | if (sizeof(RLIM_INFINITY) > sizeof(long)) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 438 | v = PyLong_FromLongLong((long long) RLIM_INFINITY); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | } else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | { |
| 441 | v = PyLong_FromLong((long) RLIM_INFINITY); |
| 442 | } |
| 443 | if (v) { |
| 444 | PyModule_AddObject(m, "RLIM_INFINITY", v); |
| 445 | } |
| 446 | initialized = 1; |
| 447 | return m; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 448 | } |