Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | #if defined(HAVE_ALARM) |
| 6 | |
| 7 | PyDoc_STRVAR(signal_alarm__doc__, |
| 8 | "alarm($module, seconds, /)\n" |
| 9 | "--\n" |
| 10 | "\n" |
| 11 | "Arrange for SIGALRM to arrive after the given number of seconds."); |
| 12 | |
| 13 | #define SIGNAL_ALARM_METHODDEF \ |
| 14 | {"alarm", (PyCFunction)signal_alarm, METH_O, signal_alarm__doc__}, |
| 15 | |
| 16 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 17 | signal_alarm_impl(PyObject *module, int seconds); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 18 | |
| 19 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 20 | signal_alarm(PyObject *module, PyObject *arg) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 21 | { |
| 22 | PyObject *return_value = NULL; |
| 23 | int seconds; |
| 24 | long _return_value; |
| 25 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 26 | if (PyFloat_Check(arg)) { |
| 27 | PyErr_SetString(PyExc_TypeError, |
| 28 | "integer argument expected, got float" ); |
| 29 | goto exit; |
| 30 | } |
| 31 | seconds = _PyLong_AsInt(arg); |
| 32 | if (seconds == -1 && PyErr_Occurred()) { |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 33 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 34 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 35 | _return_value = signal_alarm_impl(module, seconds); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 36 | if ((_return_value == -1) && PyErr_Occurred()) { |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 37 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 38 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 39 | return_value = PyLong_FromLong(_return_value); |
| 40 | |
| 41 | exit: |
| 42 | return return_value; |
| 43 | } |
| 44 | |
| 45 | #endif /* defined(HAVE_ALARM) */ |
| 46 | |
| 47 | #if defined(HAVE_PAUSE) |
| 48 | |
| 49 | PyDoc_STRVAR(signal_pause__doc__, |
| 50 | "pause($module, /)\n" |
| 51 | "--\n" |
| 52 | "\n" |
| 53 | "Wait until a signal arrives."); |
| 54 | |
| 55 | #define SIGNAL_PAUSE_METHODDEF \ |
| 56 | {"pause", (PyCFunction)signal_pause, METH_NOARGS, signal_pause__doc__}, |
| 57 | |
| 58 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 59 | signal_pause_impl(PyObject *module); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 60 | |
| 61 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 62 | signal_pause(PyObject *module, PyObject *Py_UNUSED(ignored)) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 63 | { |
| 64 | return signal_pause_impl(module); |
| 65 | } |
| 66 | |
| 67 | #endif /* defined(HAVE_PAUSE) */ |
| 68 | |
Vladimir Matveev | c24c6c2 | 2019-01-08 01:58:25 -0800 | [diff] [blame] | 69 | PyDoc_STRVAR(signal_raise_signal__doc__, |
| 70 | "raise_signal($module, signalnum, /)\n" |
| 71 | "--\n" |
| 72 | "\n" |
| 73 | "Send a signal to the executing process."); |
| 74 | |
| 75 | #define SIGNAL_RAISE_SIGNAL_METHODDEF \ |
| 76 | {"raise_signal", (PyCFunction)signal_raise_signal, METH_O, signal_raise_signal__doc__}, |
| 77 | |
| 78 | static PyObject * |
| 79 | signal_raise_signal_impl(PyObject *module, int signalnum); |
| 80 | |
| 81 | static PyObject * |
| 82 | signal_raise_signal(PyObject *module, PyObject *arg) |
| 83 | { |
| 84 | PyObject *return_value = NULL; |
| 85 | int signalnum; |
| 86 | |
| 87 | if (PyFloat_Check(arg)) { |
| 88 | PyErr_SetString(PyExc_TypeError, |
| 89 | "integer argument expected, got float" ); |
| 90 | goto exit; |
| 91 | } |
| 92 | signalnum = _PyLong_AsInt(arg); |
| 93 | if (signalnum == -1 && PyErr_Occurred()) { |
| 94 | goto exit; |
| 95 | } |
| 96 | return_value = signal_raise_signal_impl(module, signalnum); |
| 97 | |
| 98 | exit: |
| 99 | return return_value; |
| 100 | } |
| 101 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 102 | PyDoc_STRVAR(signal_signal__doc__, |
| 103 | "signal($module, signalnum, handler, /)\n" |
| 104 | "--\n" |
| 105 | "\n" |
| 106 | "Set the action for the given signal.\n" |
| 107 | "\n" |
| 108 | "The action can be SIG_DFL, SIG_IGN, or a callable Python object.\n" |
| 109 | "The previous action is returned. See getsignal() for possible return values.\n" |
| 110 | "\n" |
| 111 | "*** IMPORTANT NOTICE ***\n" |
| 112 | "A signal handler function is called with two arguments:\n" |
| 113 | "the first is the signal number, the second is the interrupted stack frame."); |
| 114 | |
| 115 | #define SIGNAL_SIGNAL_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 116 | {"signal", (PyCFunction)(void(*)(void))signal_signal, METH_FASTCALL, signal_signal__doc__}, |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 117 | |
| 118 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 119 | signal_signal_impl(PyObject *module, int signalnum, PyObject *handler); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 120 | |
| 121 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 122 | signal_signal(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 123 | { |
| 124 | PyObject *return_value = NULL; |
| 125 | int signalnum; |
| 126 | PyObject *handler; |
| 127 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 128 | if (!_PyArg_CheckPositional("signal", nargs, 2, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 129 | goto exit; |
| 130 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 131 | if (PyFloat_Check(args[0])) { |
| 132 | PyErr_SetString(PyExc_TypeError, |
| 133 | "integer argument expected, got float" ); |
| 134 | goto exit; |
| 135 | } |
| 136 | signalnum = _PyLong_AsInt(args[0]); |
| 137 | if (signalnum == -1 && PyErr_Occurred()) { |
| 138 | goto exit; |
| 139 | } |
| 140 | handler = args[1]; |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 141 | return_value = signal_signal_impl(module, signalnum, handler); |
| 142 | |
| 143 | exit: |
| 144 | return return_value; |
| 145 | } |
| 146 | |
| 147 | PyDoc_STRVAR(signal_getsignal__doc__, |
| 148 | "getsignal($module, signalnum, /)\n" |
| 149 | "--\n" |
| 150 | "\n" |
| 151 | "Return the current action for the given signal.\n" |
| 152 | "\n" |
| 153 | "The return value can be:\n" |
| 154 | " SIG_IGN -- if the signal is being ignored\n" |
| 155 | " SIG_DFL -- if the default action for the signal is in effect\n" |
| 156 | " None -- if an unknown handler is in effect\n" |
| 157 | " anything else -- the callable Python object used as a handler"); |
| 158 | |
| 159 | #define SIGNAL_GETSIGNAL_METHODDEF \ |
| 160 | {"getsignal", (PyCFunction)signal_getsignal, METH_O, signal_getsignal__doc__}, |
| 161 | |
| 162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 163 | signal_getsignal_impl(PyObject *module, int signalnum); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 164 | |
| 165 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 166 | signal_getsignal(PyObject *module, PyObject *arg) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 167 | { |
| 168 | PyObject *return_value = NULL; |
| 169 | int signalnum; |
| 170 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 171 | if (PyFloat_Check(arg)) { |
| 172 | PyErr_SetString(PyExc_TypeError, |
| 173 | "integer argument expected, got float" ); |
| 174 | goto exit; |
| 175 | } |
| 176 | signalnum = _PyLong_AsInt(arg); |
| 177 | if (signalnum == -1 && PyErr_Occurred()) { |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 178 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 179 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 180 | return_value = signal_getsignal_impl(module, signalnum); |
| 181 | |
| 182 | exit: |
| 183 | return return_value; |
| 184 | } |
| 185 | |
Antoine Pietri | 5d2a27d | 2018-03-12 14:42:34 +0100 | [diff] [blame] | 186 | PyDoc_STRVAR(signal_strsignal__doc__, |
| 187 | "strsignal($module, signalnum, /)\n" |
| 188 | "--\n" |
| 189 | "\n" |
| 190 | "Return the system description of the given signal.\n" |
| 191 | "\n" |
| 192 | "The return values can be such as \"Interrupt\", \"Segmentation fault\", etc.\n" |
| 193 | "Returns None if the signal is not recognized."); |
| 194 | |
| 195 | #define SIGNAL_STRSIGNAL_METHODDEF \ |
| 196 | {"strsignal", (PyCFunction)signal_strsignal, METH_O, signal_strsignal__doc__}, |
| 197 | |
| 198 | static PyObject * |
| 199 | signal_strsignal_impl(PyObject *module, int signalnum); |
| 200 | |
| 201 | static PyObject * |
| 202 | signal_strsignal(PyObject *module, PyObject *arg) |
| 203 | { |
| 204 | PyObject *return_value = NULL; |
| 205 | int signalnum; |
| 206 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 207 | if (PyFloat_Check(arg)) { |
| 208 | PyErr_SetString(PyExc_TypeError, |
| 209 | "integer argument expected, got float" ); |
| 210 | goto exit; |
| 211 | } |
| 212 | signalnum = _PyLong_AsInt(arg); |
| 213 | if (signalnum == -1 && PyErr_Occurred()) { |
Antoine Pietri | 5d2a27d | 2018-03-12 14:42:34 +0100 | [diff] [blame] | 214 | goto exit; |
| 215 | } |
| 216 | return_value = signal_strsignal_impl(module, signalnum); |
| 217 | |
| 218 | exit: |
| 219 | return return_value; |
| 220 | } |
| 221 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 222 | #if defined(HAVE_SIGINTERRUPT) |
| 223 | |
| 224 | PyDoc_STRVAR(signal_siginterrupt__doc__, |
| 225 | "siginterrupt($module, signalnum, flag, /)\n" |
| 226 | "--\n" |
| 227 | "\n" |
| 228 | "Change system call restart behaviour.\n" |
| 229 | "\n" |
| 230 | "If flag is False, system calls will be restarted when interrupted by\n" |
| 231 | "signal sig, else system calls will be interrupted."); |
| 232 | |
| 233 | #define SIGNAL_SIGINTERRUPT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 234 | {"siginterrupt", (PyCFunction)(void(*)(void))signal_siginterrupt, METH_FASTCALL, signal_siginterrupt__doc__}, |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 235 | |
| 236 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 237 | signal_siginterrupt_impl(PyObject *module, int signalnum, int flag); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 238 | |
| 239 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 240 | signal_siginterrupt(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 241 | { |
| 242 | PyObject *return_value = NULL; |
| 243 | int signalnum; |
| 244 | int flag; |
| 245 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 246 | if (!_PyArg_CheckPositional("siginterrupt", nargs, 2, 2)) { |
| 247 | goto exit; |
| 248 | } |
| 249 | if (PyFloat_Check(args[0])) { |
| 250 | PyErr_SetString(PyExc_TypeError, |
| 251 | "integer argument expected, got float" ); |
| 252 | goto exit; |
| 253 | } |
| 254 | signalnum = _PyLong_AsInt(args[0]); |
| 255 | if (signalnum == -1 && PyErr_Occurred()) { |
| 256 | goto exit; |
| 257 | } |
| 258 | if (PyFloat_Check(args[1])) { |
| 259 | PyErr_SetString(PyExc_TypeError, |
| 260 | "integer argument expected, got float" ); |
| 261 | goto exit; |
| 262 | } |
| 263 | flag = _PyLong_AsInt(args[1]); |
| 264 | if (flag == -1 && PyErr_Occurred()) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 265 | goto exit; |
| 266 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 267 | return_value = signal_siginterrupt_impl(module, signalnum, flag); |
| 268 | |
| 269 | exit: |
| 270 | return return_value; |
| 271 | } |
| 272 | |
| 273 | #endif /* defined(HAVE_SIGINTERRUPT) */ |
| 274 | |
| 275 | #if defined(HAVE_SETITIMER) |
| 276 | |
| 277 | PyDoc_STRVAR(signal_setitimer__doc__, |
| 278 | "setitimer($module, which, seconds, interval=0.0, /)\n" |
| 279 | "--\n" |
| 280 | "\n" |
| 281 | "Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).\n" |
| 282 | "\n" |
| 283 | "The timer will fire after value seconds and after that every interval seconds.\n" |
| 284 | "The itimer can be cleared by setting seconds to zero.\n" |
| 285 | "\n" |
| 286 | "Returns old values as a tuple: (delay, interval)."); |
| 287 | |
| 288 | #define SIGNAL_SETITIMER_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 289 | {"setitimer", (PyCFunction)(void(*)(void))signal_setitimer, METH_FASTCALL, signal_setitimer__doc__}, |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 290 | |
| 291 | static PyObject * |
Victor Stinner | ef611c9 | 2017-10-13 13:49:43 -0700 | [diff] [blame] | 292 | signal_setitimer_impl(PyObject *module, int which, PyObject *seconds, |
| 293 | PyObject *interval); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 294 | |
| 295 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 296 | signal_setitimer(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 297 | { |
| 298 | PyObject *return_value = NULL; |
| 299 | int which; |
Victor Stinner | ef611c9 | 2017-10-13 13:49:43 -0700 | [diff] [blame] | 300 | PyObject *seconds; |
| 301 | PyObject *interval = NULL; |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 302 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 303 | if (!_PyArg_CheckPositional("setitimer", nargs, 2, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 304 | goto exit; |
| 305 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 306 | if (PyFloat_Check(args[0])) { |
| 307 | PyErr_SetString(PyExc_TypeError, |
| 308 | "integer argument expected, got float" ); |
| 309 | goto exit; |
| 310 | } |
| 311 | which = _PyLong_AsInt(args[0]); |
| 312 | if (which == -1 && PyErr_Occurred()) { |
| 313 | goto exit; |
| 314 | } |
| 315 | seconds = args[1]; |
| 316 | if (nargs < 3) { |
| 317 | goto skip_optional; |
| 318 | } |
| 319 | interval = args[2]; |
| 320 | skip_optional: |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 321 | return_value = signal_setitimer_impl(module, which, seconds, interval); |
| 322 | |
| 323 | exit: |
| 324 | return return_value; |
| 325 | } |
| 326 | |
| 327 | #endif /* defined(HAVE_SETITIMER) */ |
| 328 | |
| 329 | #if defined(HAVE_GETITIMER) |
| 330 | |
| 331 | PyDoc_STRVAR(signal_getitimer__doc__, |
| 332 | "getitimer($module, which, /)\n" |
| 333 | "--\n" |
| 334 | "\n" |
| 335 | "Returns current value of given itimer."); |
| 336 | |
| 337 | #define SIGNAL_GETITIMER_METHODDEF \ |
| 338 | {"getitimer", (PyCFunction)signal_getitimer, METH_O, signal_getitimer__doc__}, |
| 339 | |
| 340 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 341 | signal_getitimer_impl(PyObject *module, int which); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 342 | |
| 343 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 344 | signal_getitimer(PyObject *module, PyObject *arg) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 345 | { |
| 346 | PyObject *return_value = NULL; |
| 347 | int which; |
| 348 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 349 | if (PyFloat_Check(arg)) { |
| 350 | PyErr_SetString(PyExc_TypeError, |
| 351 | "integer argument expected, got float" ); |
| 352 | goto exit; |
| 353 | } |
| 354 | which = _PyLong_AsInt(arg); |
| 355 | if (which == -1 && PyErr_Occurred()) { |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 356 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 357 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 358 | return_value = signal_getitimer_impl(module, which); |
| 359 | |
| 360 | exit: |
| 361 | return return_value; |
| 362 | } |
| 363 | |
| 364 | #endif /* defined(HAVE_GETITIMER) */ |
| 365 | |
| 366 | #if defined(PYPTHREAD_SIGMASK) |
| 367 | |
| 368 | PyDoc_STRVAR(signal_pthread_sigmask__doc__, |
| 369 | "pthread_sigmask($module, how, mask, /)\n" |
| 370 | "--\n" |
| 371 | "\n" |
| 372 | "Fetch and/or change the signal mask of the calling thread."); |
| 373 | |
| 374 | #define SIGNAL_PTHREAD_SIGMASK_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 375 | {"pthread_sigmask", (PyCFunction)(void(*)(void))signal_pthread_sigmask, METH_FASTCALL, signal_pthread_sigmask__doc__}, |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 376 | |
| 377 | static PyObject * |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 378 | signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 379 | |
| 380 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 381 | signal_pthread_sigmask(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 382 | { |
| 383 | PyObject *return_value = NULL; |
| 384 | int how; |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 385 | sigset_t mask; |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 386 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 387 | if (!_PyArg_CheckPositional("pthread_sigmask", nargs, 2, 2)) { |
| 388 | goto exit; |
| 389 | } |
| 390 | if (PyFloat_Check(args[0])) { |
| 391 | PyErr_SetString(PyExc_TypeError, |
| 392 | "integer argument expected, got float" ); |
| 393 | goto exit; |
| 394 | } |
| 395 | how = _PyLong_AsInt(args[0]); |
| 396 | if (how == -1 && PyErr_Occurred()) { |
| 397 | goto exit; |
| 398 | } |
| 399 | if (!_Py_Sigset_Converter(args[1], &mask)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 400 | goto exit; |
| 401 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 402 | return_value = signal_pthread_sigmask_impl(module, how, mask); |
| 403 | |
| 404 | exit: |
| 405 | return return_value; |
| 406 | } |
| 407 | |
| 408 | #endif /* defined(PYPTHREAD_SIGMASK) */ |
| 409 | |
| 410 | #if defined(HAVE_SIGPENDING) |
| 411 | |
| 412 | PyDoc_STRVAR(signal_sigpending__doc__, |
| 413 | "sigpending($module, /)\n" |
| 414 | "--\n" |
| 415 | "\n" |
| 416 | "Examine pending signals.\n" |
| 417 | "\n" |
| 418 | "Returns a set of signal numbers that are pending for delivery to\n" |
| 419 | "the calling thread."); |
| 420 | |
| 421 | #define SIGNAL_SIGPENDING_METHODDEF \ |
| 422 | {"sigpending", (PyCFunction)signal_sigpending, METH_NOARGS, signal_sigpending__doc__}, |
| 423 | |
| 424 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 425 | signal_sigpending_impl(PyObject *module); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 426 | |
| 427 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 428 | signal_sigpending(PyObject *module, PyObject *Py_UNUSED(ignored)) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 429 | { |
| 430 | return signal_sigpending_impl(module); |
| 431 | } |
| 432 | |
| 433 | #endif /* defined(HAVE_SIGPENDING) */ |
| 434 | |
| 435 | #if defined(HAVE_SIGWAIT) |
| 436 | |
| 437 | PyDoc_STRVAR(signal_sigwait__doc__, |
| 438 | "sigwait($module, sigset, /)\n" |
| 439 | "--\n" |
| 440 | "\n" |
| 441 | "Wait for a signal.\n" |
| 442 | "\n" |
| 443 | "Suspend execution of the calling thread until the delivery of one of the\n" |
| 444 | "signals specified in the signal set sigset. The function accepts the signal\n" |
| 445 | "and returns the signal number."); |
| 446 | |
| 447 | #define SIGNAL_SIGWAIT_METHODDEF \ |
| 448 | {"sigwait", (PyCFunction)signal_sigwait, METH_O, signal_sigwait__doc__}, |
| 449 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 450 | static PyObject * |
| 451 | signal_sigwait_impl(PyObject *module, sigset_t sigset); |
| 452 | |
| 453 | static PyObject * |
| 454 | signal_sigwait(PyObject *module, PyObject *arg) |
| 455 | { |
| 456 | PyObject *return_value = NULL; |
| 457 | sigset_t sigset; |
| 458 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 459 | if (!_Py_Sigset_Converter(arg, &sigset)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 460 | goto exit; |
| 461 | } |
| 462 | return_value = signal_sigwait_impl(module, sigset); |
| 463 | |
| 464 | exit: |
| 465 | return return_value; |
| 466 | } |
| 467 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 468 | #endif /* defined(HAVE_SIGWAIT) */ |
| 469 | |
Antoine Pitrou | 9d3627e | 2018-05-04 13:00:50 +0200 | [diff] [blame] | 470 | #if (defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)) |
| 471 | |
| 472 | PyDoc_STRVAR(signal_valid_signals__doc__, |
| 473 | "valid_signals($module, /)\n" |
| 474 | "--\n" |
| 475 | "\n" |
| 476 | "Return a set of valid signal numbers on this platform.\n" |
| 477 | "\n" |
| 478 | "The signal numbers returned by this function can be safely passed to\n" |
| 479 | "functions like `pthread_sigmask`."); |
| 480 | |
| 481 | #define SIGNAL_VALID_SIGNALS_METHODDEF \ |
| 482 | {"valid_signals", (PyCFunction)signal_valid_signals, METH_NOARGS, signal_valid_signals__doc__}, |
| 483 | |
| 484 | static PyObject * |
| 485 | signal_valid_signals_impl(PyObject *module); |
| 486 | |
| 487 | static PyObject * |
| 488 | signal_valid_signals(PyObject *module, PyObject *Py_UNUSED(ignored)) |
| 489 | { |
| 490 | return signal_valid_signals_impl(module); |
| 491 | } |
| 492 | |
| 493 | #endif /* (defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)) */ |
| 494 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 495 | #if defined(HAVE_SIGWAITINFO) |
| 496 | |
| 497 | PyDoc_STRVAR(signal_sigwaitinfo__doc__, |
| 498 | "sigwaitinfo($module, sigset, /)\n" |
| 499 | "--\n" |
| 500 | "\n" |
| 501 | "Wait synchronously until one of the signals in *sigset* is delivered.\n" |
| 502 | "\n" |
| 503 | "Returns a struct_siginfo containing information about the signal."); |
| 504 | |
| 505 | #define SIGNAL_SIGWAITINFO_METHODDEF \ |
| 506 | {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo, METH_O, signal_sigwaitinfo__doc__}, |
| 507 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 508 | static PyObject * |
| 509 | signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset); |
| 510 | |
| 511 | static PyObject * |
| 512 | signal_sigwaitinfo(PyObject *module, PyObject *arg) |
| 513 | { |
| 514 | PyObject *return_value = NULL; |
| 515 | sigset_t sigset; |
| 516 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 517 | if (!_Py_Sigset_Converter(arg, &sigset)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 518 | goto exit; |
| 519 | } |
| 520 | return_value = signal_sigwaitinfo_impl(module, sigset); |
| 521 | |
| 522 | exit: |
| 523 | return return_value; |
| 524 | } |
| 525 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 526 | #endif /* defined(HAVE_SIGWAITINFO) */ |
| 527 | |
| 528 | #if defined(HAVE_SIGTIMEDWAIT) |
| 529 | |
| 530 | PyDoc_STRVAR(signal_sigtimedwait__doc__, |
| 531 | "sigtimedwait($module, sigset, timeout, /)\n" |
| 532 | "--\n" |
| 533 | "\n" |
| 534 | "Like sigwaitinfo(), but with a timeout.\n" |
| 535 | "\n" |
| 536 | "The timeout is specified in seconds, with floating point numbers allowed."); |
| 537 | |
| 538 | #define SIGNAL_SIGTIMEDWAIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 539 | {"sigtimedwait", (PyCFunction)(void(*)(void))signal_sigtimedwait, METH_FASTCALL, signal_sigtimedwait__doc__}, |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 540 | |
| 541 | static PyObject * |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 542 | signal_sigtimedwait_impl(PyObject *module, sigset_t sigset, |
Serhiy Storchaka | 6b680cd | 2015-05-16 15:57:56 +0300 | [diff] [blame] | 543 | PyObject *timeout_obj); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 544 | |
| 545 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 546 | signal_sigtimedwait(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 547 | { |
| 548 | PyObject *return_value = NULL; |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 549 | sigset_t sigset; |
Serhiy Storchaka | 6b680cd | 2015-05-16 15:57:56 +0300 | [diff] [blame] | 550 | PyObject *timeout_obj; |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 551 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 552 | if (!_PyArg_CheckPositional("sigtimedwait", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 553 | goto exit; |
| 554 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 555 | if (!_Py_Sigset_Converter(args[0], &sigset)) { |
| 556 | goto exit; |
| 557 | } |
| 558 | timeout_obj = args[1]; |
Serhiy Storchaka | 6b680cd | 2015-05-16 15:57:56 +0300 | [diff] [blame] | 559 | return_value = signal_sigtimedwait_impl(module, sigset, timeout_obj); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 560 | |
| 561 | exit: |
| 562 | return return_value; |
| 563 | } |
| 564 | |
| 565 | #endif /* defined(HAVE_SIGTIMEDWAIT) */ |
| 566 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 567 | #if defined(HAVE_PTHREAD_KILL) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 568 | |
| 569 | PyDoc_STRVAR(signal_pthread_kill__doc__, |
| 570 | "pthread_kill($module, thread_id, signalnum, /)\n" |
| 571 | "--\n" |
| 572 | "\n" |
| 573 | "Send a signal to a thread."); |
| 574 | |
| 575 | #define SIGNAL_PTHREAD_KILL_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 576 | {"pthread_kill", (PyCFunction)(void(*)(void))signal_pthread_kill, METH_FASTCALL, signal_pthread_kill__doc__}, |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 577 | |
| 578 | static PyObject * |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 579 | signal_pthread_kill_impl(PyObject *module, unsigned long thread_id, |
| 580 | int signalnum); |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 581 | |
| 582 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 583 | signal_pthread_kill(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 584 | { |
| 585 | PyObject *return_value = NULL; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 586 | unsigned long thread_id; |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 587 | int signalnum; |
| 588 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 589 | if (!_PyArg_CheckPositional("pthread_kill", nargs, 2, 2)) { |
| 590 | goto exit; |
| 591 | } |
| 592 | if (!PyLong_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 593 | _PyArg_BadArgument("pthread_kill", "argument 1", "int", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 594 | goto exit; |
| 595 | } |
| 596 | thread_id = PyLong_AsUnsignedLongMask(args[0]); |
| 597 | if (PyFloat_Check(args[1])) { |
| 598 | PyErr_SetString(PyExc_TypeError, |
| 599 | "integer argument expected, got float" ); |
| 600 | goto exit; |
| 601 | } |
| 602 | signalnum = _PyLong_AsInt(args[1]); |
| 603 | if (signalnum == -1 && PyErr_Occurred()) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 604 | goto exit; |
| 605 | } |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 606 | return_value = signal_pthread_kill_impl(module, thread_id, signalnum); |
| 607 | |
| 608 | exit: |
| 609 | return return_value; |
| 610 | } |
| 611 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 612 | #endif /* defined(HAVE_PTHREAD_KILL) */ |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 613 | |
Benjamin Peterson | 7483451 | 2019-11-19 20:39:14 -0800 | [diff] [blame] | 614 | #if (defined(__linux__) && defined(__NR_pidfd_send_signal)) |
| 615 | |
| 616 | PyDoc_STRVAR(signal_pidfd_send_signal__doc__, |
| 617 | "pidfd_send_signal($module, pidfd, signalnum, siginfo=None, flags=0, /)\n" |
| 618 | "--\n" |
| 619 | "\n" |
| 620 | "Send a signal to a process referred to by a pid file descriptor."); |
| 621 | |
| 622 | #define SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF \ |
| 623 | {"pidfd_send_signal", (PyCFunction)(void(*)(void))signal_pidfd_send_signal, METH_FASTCALL, signal_pidfd_send_signal__doc__}, |
| 624 | |
| 625 | static PyObject * |
| 626 | signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum, |
| 627 | PyObject *siginfo, int flags); |
| 628 | |
| 629 | static PyObject * |
| 630 | signal_pidfd_send_signal(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 631 | { |
| 632 | PyObject *return_value = NULL; |
| 633 | int pidfd; |
| 634 | int signalnum; |
| 635 | PyObject *siginfo = Py_None; |
| 636 | int flags = 0; |
| 637 | |
| 638 | if (!_PyArg_CheckPositional("pidfd_send_signal", nargs, 2, 4)) { |
| 639 | goto exit; |
| 640 | } |
| 641 | if (PyFloat_Check(args[0])) { |
| 642 | PyErr_SetString(PyExc_TypeError, |
| 643 | "integer argument expected, got float" ); |
| 644 | goto exit; |
| 645 | } |
| 646 | pidfd = _PyLong_AsInt(args[0]); |
| 647 | if (pidfd == -1 && PyErr_Occurred()) { |
| 648 | goto exit; |
| 649 | } |
| 650 | if (PyFloat_Check(args[1])) { |
| 651 | PyErr_SetString(PyExc_TypeError, |
| 652 | "integer argument expected, got float" ); |
| 653 | goto exit; |
| 654 | } |
| 655 | signalnum = _PyLong_AsInt(args[1]); |
| 656 | if (signalnum == -1 && PyErr_Occurred()) { |
| 657 | goto exit; |
| 658 | } |
| 659 | if (nargs < 3) { |
| 660 | goto skip_optional; |
| 661 | } |
| 662 | siginfo = args[2]; |
| 663 | if (nargs < 4) { |
| 664 | goto skip_optional; |
| 665 | } |
| 666 | if (PyFloat_Check(args[3])) { |
| 667 | PyErr_SetString(PyExc_TypeError, |
| 668 | "integer argument expected, got float" ); |
| 669 | goto exit; |
| 670 | } |
| 671 | flags = _PyLong_AsInt(args[3]); |
| 672 | if (flags == -1 && PyErr_Occurred()) { |
| 673 | goto exit; |
| 674 | } |
| 675 | skip_optional: |
| 676 | return_value = signal_pidfd_send_signal_impl(module, pidfd, signalnum, siginfo, flags); |
| 677 | |
| 678 | exit: |
| 679 | return return_value; |
| 680 | } |
| 681 | |
| 682 | #endif /* (defined(__linux__) && defined(__NR_pidfd_send_signal)) */ |
| 683 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 684 | #ifndef SIGNAL_ALARM_METHODDEF |
| 685 | #define SIGNAL_ALARM_METHODDEF |
| 686 | #endif /* !defined(SIGNAL_ALARM_METHODDEF) */ |
| 687 | |
| 688 | #ifndef SIGNAL_PAUSE_METHODDEF |
| 689 | #define SIGNAL_PAUSE_METHODDEF |
| 690 | #endif /* !defined(SIGNAL_PAUSE_METHODDEF) */ |
| 691 | |
| 692 | #ifndef SIGNAL_SIGINTERRUPT_METHODDEF |
| 693 | #define SIGNAL_SIGINTERRUPT_METHODDEF |
| 694 | #endif /* !defined(SIGNAL_SIGINTERRUPT_METHODDEF) */ |
| 695 | |
| 696 | #ifndef SIGNAL_SETITIMER_METHODDEF |
| 697 | #define SIGNAL_SETITIMER_METHODDEF |
| 698 | #endif /* !defined(SIGNAL_SETITIMER_METHODDEF) */ |
| 699 | |
| 700 | #ifndef SIGNAL_GETITIMER_METHODDEF |
| 701 | #define SIGNAL_GETITIMER_METHODDEF |
| 702 | #endif /* !defined(SIGNAL_GETITIMER_METHODDEF) */ |
| 703 | |
| 704 | #ifndef SIGNAL_PTHREAD_SIGMASK_METHODDEF |
| 705 | #define SIGNAL_PTHREAD_SIGMASK_METHODDEF |
| 706 | #endif /* !defined(SIGNAL_PTHREAD_SIGMASK_METHODDEF) */ |
| 707 | |
| 708 | #ifndef SIGNAL_SIGPENDING_METHODDEF |
| 709 | #define SIGNAL_SIGPENDING_METHODDEF |
| 710 | #endif /* !defined(SIGNAL_SIGPENDING_METHODDEF) */ |
| 711 | |
| 712 | #ifndef SIGNAL_SIGWAIT_METHODDEF |
| 713 | #define SIGNAL_SIGWAIT_METHODDEF |
| 714 | #endif /* !defined(SIGNAL_SIGWAIT_METHODDEF) */ |
| 715 | |
Antoine Pitrou | 9d3627e | 2018-05-04 13:00:50 +0200 | [diff] [blame] | 716 | #ifndef SIGNAL_VALID_SIGNALS_METHODDEF |
| 717 | #define SIGNAL_VALID_SIGNALS_METHODDEF |
| 718 | #endif /* !defined(SIGNAL_VALID_SIGNALS_METHODDEF) */ |
| 719 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 720 | #ifndef SIGNAL_SIGWAITINFO_METHODDEF |
| 721 | #define SIGNAL_SIGWAITINFO_METHODDEF |
| 722 | #endif /* !defined(SIGNAL_SIGWAITINFO_METHODDEF) */ |
| 723 | |
| 724 | #ifndef SIGNAL_SIGTIMEDWAIT_METHODDEF |
| 725 | #define SIGNAL_SIGTIMEDWAIT_METHODDEF |
| 726 | #endif /* !defined(SIGNAL_SIGTIMEDWAIT_METHODDEF) */ |
| 727 | |
| 728 | #ifndef SIGNAL_PTHREAD_KILL_METHODDEF |
| 729 | #define SIGNAL_PTHREAD_KILL_METHODDEF |
| 730 | #endif /* !defined(SIGNAL_PTHREAD_KILL_METHODDEF) */ |
Benjamin Peterson | 7483451 | 2019-11-19 20:39:14 -0800 | [diff] [blame] | 731 | |
| 732 | #ifndef SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF |
| 733 | #define SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF |
| 734 | #endif /* !defined(SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF) */ |
| 735 | /*[clinic end generated code: output=b41b4b6bd9ad4da2 input=a9049054013a1b77]*/ |