blob: d470727fb629dc06c1dba62c72bf2f5a068738b3 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007#ifndef MS_WINDOWS
8#include "posixmodule.h"
9#endif
Victor Stinner11517102014-07-29 23:31:34 +020010#ifdef MS_WINDOWS
11#include "socketmodule.h" /* needed for SOCKET_T */
12#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000014#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020015#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000017#include <process.h>
18#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000019#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000020
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000022#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
24#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000025#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000027#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000028#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000030
Victor Stinnera9293352011-04-30 15:21:58 +020031#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
32# define PYPTHREAD_SIGMASK
33#endif
34
35#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
36# include <pthread.h>
37#endif
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000040#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#endif
42
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000043#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#endif
54
Tal Einatc7027b72015-05-16 14:14:49 +030055#include "clinic/signalmodule.c.h"
56
57/*[clinic input]
58module signal
59[clinic start generated code]*/
60/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
61
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000062
Guido van Rossumbb4ba121994-06-23 11:25:45 +000063/*
64 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
65
66 When threads are supported, we want the following semantics:
67
68 - only the main thread can set a signal handler
69 - any thread can get a signal handler
70 - signals are only delivered to the main thread
71
72 I.e. we don't support "synchronous signals" like SIGFPE (catching
73 this doesn't make much sense in Python anyway) nor do we support
74 signals as a means of inter-thread communication, since not all
75 thread implementations support that (at least our thread library
76 doesn't).
77
78 We still have the problem that in some implementations signals
79 generated by the keyboard (e.g. SIGINT) are delivered to all
80 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
81 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000082 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000083 a working implementation that works in all three cases -- the
84 handler ignores signals if getpid() isn't the same as in the main
85 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000086*/
87
Guido van Rossum295b8e51997-06-06 21:16:41 +000088#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000089#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020090static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091static pid_t main_pid;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000092
Victor Stinner2ec6b172011-05-15 10:21:59 +020093static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +020094 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000096} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Victor Stinner11517102014-07-29 23:31:34 +020098#ifdef MS_WINDOWS
99#define INVALID_FD ((SOCKET_T)-1)
100
101static volatile struct {
102 SOCKET_T fd;
103 int use_send;
104 int send_err_set;
105 int send_errno;
106 int send_win_error;
107} wakeup = {INVALID_FD, 0, 0};
108#else
109#define INVALID_FD (-1)
Victor Stinner2ec6b172011-05-15 10:21:59 +0200110static volatile sig_atomic_t wakeup_fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200111#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000112
Christian Heimesb76922a2007-12-11 01:06:40 +0000113/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200114static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115
Barry Warsaw92971171997-01-03 00:14:25 +0000116static PyObject *DefaultHandler;
117static PyObject *IgnoreHandler;
118static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000119
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000120/* On Solaris 8, gcc will produce a warning that the function
121 declaration is not a prototype. This is caused by the definition of
122 SIG_DFL as (void (*)())0; the correct declaration would have been
123 (void (*)(int))0. */
124
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000125static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000126
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100127#ifdef MS_WINDOWS
128static HANDLE sigint_event = NULL;
129#endif
130
Martin v. Löwis823725e2008-03-24 13:39:54 +0000131#ifdef HAVE_GETITIMER
132static PyObject *ItimerError;
133
Victor Stinneref611c92017-10-13 13:49:43 -0700134/* auxiliary functions for setitimer */
135static int
136timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000137{
Victor Stinneref611c92017-10-13 13:49:43 -0700138 if (obj == NULL) {
139 tv->tv_sec = 0;
140 tv->tv_usec = 0;
141 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200142 }
Victor Stinneref611c92017-10-13 13:49:43 -0700143
144 _PyTime_t t;
145 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
146 return -1;
147 }
148 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000149}
150
Christian Heimes1a8501c2008-10-02 19:56:01 +0000151Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000152double_from_timeval(struct timeval *tv)
153{
154 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
155}
156
157static PyObject *
158itimer_retval(struct itimerval *iv)
159{
160 PyObject *r, *v;
161
162 r = PyTuple_New(2);
163 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000164 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000165
166 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000167 Py_DECREF(r);
168 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000169 }
170
171 PyTuple_SET_ITEM(r, 0, v);
172
173 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000174 Py_DECREF(r);
175 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000176 }
177
178 PyTuple_SET_ITEM(r, 1, v);
179
180 return r;
181}
182#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000183
Guido van Rossume4485b01994-09-07 14:32:49 +0000184static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000185signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 PyErr_SetNone(PyExc_KeyboardInterrupt);
188 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000189}
190
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000191PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000192"default_int_handler(...)\n\
193\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000194The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000196
Thomas Wouters0796b002000-07-22 23:49:30 +0000197
198static int
Victor Stinner11517102014-07-29 23:31:34 +0200199report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200200{
201 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700202 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200203 PyErr_SetFromErrno(PyExc_OSError);
204 PySys_WriteStderr("Exception ignored when trying to write to the "
205 "signal wakeup fd:\n");
206 PyErr_WriteUnraisable(NULL);
207 errno = save_errno;
208 return 0;
209}
210
Victor Stinner11517102014-07-29 23:31:34 +0200211#ifdef MS_WINDOWS
212static int
213report_wakeup_send_error(void* Py_UNUSED(data))
214{
215 PyObject *res;
216
217 if (wakeup.send_win_error) {
218 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
219 recognizes the error codes used by both GetLastError() and
220 WSAGetLastError */
221 res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
222 }
223 else {
224 errno = wakeup.send_errno;
225 res = PyErr_SetFromErrno(PyExc_OSError);
226 }
227
228 assert(res == NULL);
229 wakeup.send_err_set = 0;
230
231 PySys_WriteStderr("Exception ignored when trying to send to the "
232 "signal wakeup fd:\n");
233 PyErr_WriteUnraisable(NULL);
234
235 return 0;
236}
237#endif /* MS_WINDOWS */
238
Tim Peters4f1b2082000-07-23 21:18:09 +0000239static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200240trip_signal(int sig_num)
241{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200242 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200243 int fd;
244 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200245
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200246 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200247
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200248 /* Set is_tripped after setting .tripped, as it gets
249 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200250 _Py_atomic_store(&is_tripped, 1);
251
252 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200253 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700254
255 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200256 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
257 and then set the flag, but this allowed the following sequence of events
258 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700259
260 - main thread blocks on select([wakeup_fd], ...)
261 - signal arrives
262 - trip_signal writes to the wakeup fd
263 - the main thread wakes up
264 - the main thread checks the signal flags, sees that they're unset
265 - the main thread empties the wakeup fd
266 - the main thread goes back to sleep
267 - trip_signal sets the flags to request the Python-level signal handler
268 be run
269 - the main thread doesn't notice, because it's asleep
270
271 See bpo-30038 for more details.
272 */
273
Victor Stinner11517102014-07-29 23:31:34 +0200274#ifdef MS_WINDOWS
275 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
276#else
277 fd = wakeup_fd;
278#endif
279
280 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200281 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200282#ifdef MS_WINDOWS
283 if (wakeup.use_send) {
284 do {
285 rc = send(fd, &byte, 1, 0);
286 } while (rc < 0 && errno == EINTR);
287
288 /* we only have a storage for one error in the wakeup structure */
289 if (rc < 0 && !wakeup.send_err_set) {
290 wakeup.send_err_set = 1;
291 wakeup.send_errno = errno;
292 wakeup.send_win_error = GetLastError();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200293 /* Py_AddPendingCall() isn't signal-safe, but we
294 still use it for this exceptional case. */
Victor Stinner11517102014-07-29 23:31:34 +0200295 Py_AddPendingCall(report_wakeup_send_error, NULL);
296 }
297 }
298 else
299#endif
300 {
301 byte = (unsigned char)sig_num;
Victor Stinnere72fe392015-04-01 18:35:22 +0200302
303 /* _Py_write_noraise() retries write() if write() is interrupted by
304 a signal (fails with EINTR). */
305 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200306
307 if (rc < 0) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200308 /* Py_AddPendingCall() isn't signal-safe, but we
309 still use it for this exceptional case. */
Victor Stinner11517102014-07-29 23:31:34 +0200310 Py_AddPendingCall(report_wakeup_write_error,
Benjamin Petersonca470632016-09-06 13:47:26 -0700311 (void *)(intptr_t)errno);
Victor Stinner11517102014-07-29 23:31:34 +0200312 }
313 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200314 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200315}
316
317static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000318signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000319{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000320 int save_errno = errno;
321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000323 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000324 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200325 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000327
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000328#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000329#ifdef SIGCHLD
330 /* To avoid infinite recursion, this signal remains
331 reset until explicit re-instated.
332 Don't clear the 'func' field as it is our pointer
333 to the Python handler... */
334 if (sig_num != SIGCHLD)
335#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000337 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 * makes this true. See also issue8354. */
339 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000340#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000341
342 /* Issue #10311: asynchronously executing signal handlers should not
343 mutate errno under the feet of unsuspecting C code. */
344 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100345
346#ifdef MS_WINDOWS
347 if (sig_num == SIGINT)
348 SetEvent(sigint_event);
349#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350}
Guido van Rossume4485b01994-09-07 14:32:49 +0000351
Guido van Rossum06d511d1995-03-10 15:13:48 +0000352
Guido van Rossum1171ee61997-08-22 20:42:00 +0000353#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300354
355/*[clinic input]
356signal.alarm -> long
357
358 seconds: int
359 /
360
361Arrange for SIGALRM to arrive after the given number of seconds.
362[clinic start generated code]*/
363
364static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300365signal_alarm_impl(PyObject *module, int seconds)
366/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300369 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000370}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000371
Guido van Rossum06d511d1995-03-10 15:13:48 +0000372#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000373
Guido van Rossum1171ee61997-08-22 20:42:00 +0000374#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300375
376/*[clinic input]
377signal.pause
378
379Wait until a signal arrives.
380[clinic start generated code]*/
381
Guido van Rossuma597dde1995-01-10 20:56:29 +0000382static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300383signal_pause_impl(PyObject *module)
384/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_BEGIN_ALLOW_THREADS
387 (void)pause();
388 Py_END_ALLOW_THREADS
389 /* make sure that any exceptions that got raised are propagated
390 * back into Python
391 */
392 if (PyErr_CheckSignals())
393 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000394
Tal Einatc7027b72015-05-16 14:14:49 +0300395 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000396}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000397
Guido van Rossum06d511d1995-03-10 15:13:48 +0000398#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000399
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000400
Tal Einatc7027b72015-05-16 14:14:49 +0300401/*[clinic input]
402signal.signal
403
404 signalnum: int
405 handler: object
406 /
407
408Set the action for the given signal.
409
410The action can be SIG_DFL, SIG_IGN, or a callable Python object.
411The previous action is returned. See getsignal() for possible return values.
412
413*** IMPORTANT NOTICE ***
414A signal handler function is called with two arguments:
415the first is the signal number, the second is the interrupted stack frame.
416[clinic start generated code]*/
417
Guido van Rossume4485b01994-09-07 14:32:49 +0000418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300419signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
420/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 PyObject *old_handler;
423 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000424#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300425 /* Validate that signalnum is one of the allowable signals */
426 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000427 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000428#ifdef SIGBREAK
429 /* Issue #10003: SIGBREAK is not documented as permitted, but works
430 and corresponds to CTRL_BREAK_EVENT. */
431 case SIGBREAK: break;
432#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000433 case SIGFPE: break;
434 case SIGILL: break;
435 case SIGINT: break;
436 case SIGSEGV: break;
437 case SIGTERM: break;
438 default:
439 PyErr_SetString(PyExc_ValueError, "invalid signal value");
440 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000441 }
442#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (PyThread_get_thread_ident() != main_thread) {
444 PyErr_SetString(PyExc_ValueError,
445 "signal only works in main thread");
446 return NULL;
447 }
Tal Einatc7027b72015-05-16 14:14:49 +0300448 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyErr_SetString(PyExc_ValueError,
450 "signal number out of range");
451 return NULL;
452 }
Tal Einatc7027b72015-05-16 14:14:49 +0300453 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300455 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300457 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000459"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 return NULL;
461 }
462 else
463 func = signal_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300464 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200465 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 return NULL;
467 }
Tal Einatc7027b72015-05-16 14:14:49 +0300468 old_handler = Handlers[signalnum].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200469 _Py_atomic_store_relaxed(&Handlers[signalnum].tripped, 0);
Tal Einatc7027b72015-05-16 14:14:49 +0300470 Py_INCREF(handler);
471 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200472 if (old_handler != NULL)
473 return old_handler;
474 else
475 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000476}
477
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000478
Tal Einatc7027b72015-05-16 14:14:49 +0300479/*[clinic input]
480signal.getsignal
481
482 signalnum: int
483 /
484
485Return the current action for the given signal.
486
487The return value can be:
488 SIG_IGN -- if the signal is being ignored
489 SIG_DFL -- if the default action for the signal is in effect
490 None -- if an unknown handler is in effect
491 anything else -- the callable Python object used as a handler
492[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000493
Guido van Rossume4485b01994-09-07 14:32:49 +0000494static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300495signal_getsignal_impl(PyObject *module, int signalnum)
496/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300499 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyErr_SetString(PyExc_ValueError,
501 "signal number out of range");
502 return NULL;
503 }
Tal Einatc7027b72015-05-16 14:14:49 +0300504 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200505 if (old_handler != NULL) {
506 Py_INCREF(old_handler);
507 return old_handler;
508 }
509 else {
510 Py_RETURN_NONE;
511 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512}
513
Christian Heimes8640e742008-02-23 16:23:06 +0000514#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300515
516/*[clinic input]
517signal.siginterrupt
518
519 signalnum: int
520 flag: int
521 /
522
523Change system call restart behaviour.
524
525If flag is False, system calls will be restarted when interrupted by
526signal sig, else system calls will be interrupted.
527[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000528
529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300530signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
531/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000532{
Tal Einatc7027b72015-05-16 14:14:49 +0300533 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyErr_SetString(PyExc_ValueError,
535 "signal number out of range");
536 return NULL;
537 }
Tal Einatc7027b72015-05-16 14:14:49 +0300538 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200539 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return NULL;
541 }
Tal Einatc7027b72015-05-16 14:14:49 +0300542 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000543}
544
545#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000546
Tal Einatc7027b72015-05-16 14:14:49 +0300547
548static PyObject*
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000549signal_set_wakeup_fd(PyObject *self, PyObject *args)
550{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200551 struct _Py_stat_struct status;
Victor Stinner11517102014-07-29 23:31:34 +0200552#ifdef MS_WINDOWS
553 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100554 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200555 int res;
556 int res_size = sizeof res;
557 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200558 int is_socket;
559
560 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
561 return NULL;
562
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100563 sockfd = PyLong_AsSocket_t(fdobj);
564 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200565 return NULL;
566#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
570 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200571#endif
572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (PyThread_get_thread_ident() != main_thread) {
574 PyErr_SetString(PyExc_ValueError,
575 "set_wakeup_fd only works in main thread");
576 return NULL;
577 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200578
Victor Stinner11517102014-07-29 23:31:34 +0200579#ifdef MS_WINDOWS
580 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100581 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200582 /* Import the _socket module to call WSAStartup() */
583 mod = PyImport_ImportModuleNoBlock("_socket");
584 if (mod == NULL)
585 return NULL;
586 Py_DECREF(mod);
587
588 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100589 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200590 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100591 int fd, err;
592
593 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200594 if (err != WSAENOTSOCK) {
595 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
596 return NULL;
597 }
598
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100599 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700600 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200601 PyErr_SetString(PyExc_ValueError, "invalid fd");
602 return NULL;
603 }
604
Victor Stinnere134a7f2015-03-30 10:09:31 +0200605 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200606 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200607
608 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200609 }
Victor Stinner38227602014-08-27 12:59:44 +0200610 else {
Victor Stinner11517102014-07-29 23:31:34 +0200611 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200612
613 /* Windows does not provide a function to test if a socket
614 is in non-blocking mode */
615 }
Victor Stinner11517102014-07-29 23:31:34 +0200616 }
617
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100618 old_sockfd = wakeup.fd;
619 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200620 wakeup.use_send = is_socket;
621
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100622 if (old_sockfd != INVALID_FD)
623 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200624 else
625 return PyLong_FromLong(-1);
626#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200627 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200628 int blocking;
629
Victor Stinnere134a7f2015-03-30 10:09:31 +0200630 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200631 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200632
633 blocking = _Py_get_blocking(fd);
634 if (blocking < 0)
635 return NULL;
636 if (blocking) {
637 PyErr_Format(PyExc_ValueError,
638 "the fd %i must be in non-blocking mode",
639 fd);
640 return NULL;
641 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 old_fd = wakeup_fd;
645 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200648#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000649}
650
651PyDoc_STRVAR(set_wakeup_fd_doc,
652"set_wakeup_fd(fd) -> fd\n\
653\n\
Victor Stinner11517102014-07-29 23:31:34 +0200654Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000655comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200656The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000657\n\
658The fd must be non-blocking.");
659
660/* C API for the same, without all the error checking */
661int
662PySignal_SetWakeupFd(int fd)
663{
Victor Stinner11517102014-07-29 23:31:34 +0200664 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (fd < 0)
666 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200667
668#ifdef MS_WINDOWS
669 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
670 wakeup.fd = fd;
671#else
672 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200674#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000676}
677
678
Martin v. Löwis823725e2008-03-24 13:39:54 +0000679#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300680
681/*[clinic input]
682signal.setitimer
683
684 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700685 seconds: object
686 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300687 /
688
689Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
690
691The timer will fire after value seconds and after that every interval seconds.
692The itimer can be cleared by setting seconds to zero.
693
694Returns old values as a tuple: (delay, interval).
695[clinic start generated code]*/
696
Martin v. Löwis823725e2008-03-24 13:39:54 +0000697static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700698signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
699 PyObject *interval)
700/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000701{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000702 struct itimerval new, old;
703
Victor Stinneref611c92017-10-13 13:49:43 -0700704 if (timeval_from_double(seconds, &new.it_value) < 0) {
705 return NULL;
706 }
707 if (timeval_from_double(interval, &new.it_interval) < 0) {
708 return NULL;
709 }
710
Martin v. Löwis823725e2008-03-24 13:39:54 +0000711 /* Let OS check "which" value */
712 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300713 PyErr_SetFromErrno(ItimerError);
714 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000715 }
716
717 return itimer_retval(&old);
718}
719
Martin v. Löwis823725e2008-03-24 13:39:54 +0000720#endif
721
722
723#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300724
725/*[clinic input]
726signal.getitimer
727
728 which: int
729 /
730
731Returns current value of given itimer.
732[clinic start generated code]*/
733
Martin v. Löwis823725e2008-03-24 13:39:54 +0000734static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300735signal_getitimer_impl(PyObject *module, int which)
736/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000737{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000738 struct itimerval old;
739
Martin v. Löwis823725e2008-03-24 13:39:54 +0000740 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300741 PyErr_SetFromErrno(ItimerError);
742 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000743 }
744
745 return itimer_retval(&old);
746}
747
Martin v. Löwis823725e2008-03-24 13:39:54 +0000748#endif
749
Ross Lagerwallbc808222011-06-25 12:13:40 +0200750#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
751 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200752/* Convert an iterable to a sigset.
753 Return 0 on success, return -1 and raise an exception on error. */
754
755static int
756iterable_to_sigset(PyObject *iterable, sigset_t *mask)
757{
758 int result = -1;
759 PyObject *iterator, *item;
760 long signum;
761 int err;
762
763 sigemptyset(mask);
764
765 iterator = PyObject_GetIter(iterable);
766 if (iterator == NULL)
767 goto error;
768
769 while (1)
770 {
771 item = PyIter_Next(iterator);
772 if (item == NULL) {
773 if (PyErr_Occurred())
774 goto error;
775 else
776 break;
777 }
778
779 signum = PyLong_AsLong(item);
780 Py_DECREF(item);
781 if (signum == -1 && PyErr_Occurred())
782 goto error;
783 if (0 < signum && signum < NSIG)
784 err = sigaddset(mask, (int)signum);
785 else
786 err = 1;
787 if (err) {
788 PyErr_Format(PyExc_ValueError,
789 "signal number %ld out of range", signum);
790 goto error;
791 }
792 }
793 result = 0;
794
795error:
796 Py_XDECREF(iterator);
797 return result;
798}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200799#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200800
Victor Stinnerb3e72192011-05-08 01:46:11 +0200801#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200802static PyObject*
803sigset_to_set(sigset_t mask)
804{
805 PyObject *signum, *result;
806 int sig;
807
808 result = PySet_New(0);
809 if (result == NULL)
810 return NULL;
811
812 for (sig = 1; sig < NSIG; sig++) {
813 if (sigismember(&mask, sig) != 1)
814 continue;
815
816 /* Handle the case where it is a member by adding the signal to
817 the result list. Ignore the other cases because they mean the
818 signal isn't a member of the mask or the signal was invalid,
819 and an invalid signal must have been our fault in constructing
820 the loop boundaries. */
821 signum = PyLong_FromLong(sig);
822 if (signum == NULL) {
823 Py_DECREF(result);
824 return NULL;
825 }
826 if (PySet_Add(result, signum) == -1) {
827 Py_DECREF(signum);
828 Py_DECREF(result);
829 return NULL;
830 }
831 Py_DECREF(signum);
832 }
833 return result;
834}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200835#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200836
Victor Stinnerb3e72192011-05-08 01:46:11 +0200837#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300838
839/*[clinic input]
840signal.pthread_sigmask
841
842 how: int
843 mask: object
844 /
845
846Fetch and/or change the signal mask of the calling thread.
847[clinic start generated code]*/
848
Victor Stinnera9293352011-04-30 15:21:58 +0200849static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300850signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
851/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200852{
Tal Einatc7027b72015-05-16 14:14:49 +0300853 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200854 int err;
855
Tal Einatc7027b72015-05-16 14:14:49 +0300856 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200857 return NULL;
858
Tal Einatc7027b72015-05-16 14:14:49 +0300859 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200860 if (err != 0) {
861 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200862 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200863 return NULL;
864 }
865
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200866 /* if signals was unblocked, signal handlers have been called */
867 if (PyErr_CheckSignals())
868 return NULL;
869
Victor Stinner35b300c2011-05-04 13:20:35 +0200870 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200871}
872
Victor Stinnera9293352011-04-30 15:21:58 +0200873#endif /* #ifdef PYPTHREAD_SIGMASK */
874
Martin v. Löwis823725e2008-03-24 13:39:54 +0000875
Victor Stinnerb3e72192011-05-08 01:46:11 +0200876#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300877
878/*[clinic input]
879signal.sigpending
880
881Examine pending signals.
882
883Returns a set of signal numbers that are pending for delivery to
884the calling thread.
885[clinic start generated code]*/
886
Victor Stinnerb3e72192011-05-08 01:46:11 +0200887static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300888signal_sigpending_impl(PyObject *module)
889/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200890{
891 int err;
892 sigset_t mask;
893 err = sigpending(&mask);
894 if (err)
895 return PyErr_SetFromErrno(PyExc_OSError);
896 return sigset_to_set(mask);
897}
898
Victor Stinnerb3e72192011-05-08 01:46:11 +0200899#endif /* #ifdef HAVE_SIGPENDING */
900
901
902#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300903
904/*[clinic input]
905signal.sigwait
906
907 sigset: object
908 /
909
910Wait for a signal.
911
912Suspend execution of the calling thread until the delivery of one of the
913signals specified in the signal set sigset. The function accepts the signal
914and returns the signal number.
915[clinic start generated code]*/
916
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300918signal_sigwait(PyObject *module, PyObject *sigset)
919/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200920{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200921 sigset_t set;
922 int err, signum;
923
Tal Einatc7027b72015-05-16 14:14:49 +0300924 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200925 return NULL;
926
Victor Stinner10c30d62011-06-10 01:39:53 +0200927 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200928 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200929 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200930 if (err) {
931 errno = err;
932 return PyErr_SetFromErrno(PyExc_OSError);
933 }
934
935 return PyLong_FromLong(signum);
936}
937
Tal Einatc7027b72015-05-16 14:14:49 +0300938#endif /* #ifdef HAVE_SIGWAIT */
939
Victor Stinnerb3e72192011-05-08 01:46:11 +0200940
Ross Lagerwallbc808222011-06-25 12:13:40 +0200941#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
942static int initialized;
943static PyStructSequence_Field struct_siginfo_fields[] = {
944 {"si_signo", "signal number"},
945 {"si_code", "signal code"},
946 {"si_errno", "errno associated with this signal"},
947 {"si_pid", "sending process ID"},
948 {"si_uid", "real user ID of sending process"},
949 {"si_status", "exit value or signal"},
950 {"si_band", "band event for SIGPOLL"},
951 {0}
952};
953
954PyDoc_STRVAR(struct_siginfo__doc__,
955"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
956This object may be accessed either as a tuple of\n\
957(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
958or via the attributes si_signo, si_code, and so on.");
959
960static PyStructSequence_Desc struct_siginfo_desc = {
961 "signal.struct_siginfo", /* name */
962 struct_siginfo__doc__, /* doc */
963 struct_siginfo_fields, /* fields */
964 7 /* n_in_sequence */
965};
966
967static PyTypeObject SiginfoType;
968
969static PyObject *
970fill_siginfo(siginfo_t *si)
971{
972 PyObject *result = PyStructSequence_New(&SiginfoType);
973 if (!result)
974 return NULL;
975
976 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
977 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
978 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
979 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200980 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200981 PyStructSequence_SET_ITEM(result, 5,
982 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500983#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +0200984 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500985#else
986 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
987#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200988 if (PyErr_Occurred()) {
989 Py_DECREF(result);
990 return NULL;
991 }
992
993 return result;
994}
995#endif
996
997#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300998
999/*[clinic input]
1000signal.sigwaitinfo
1001
1002 sigset: object
1003 /
1004
1005Wait synchronously until one of the signals in *sigset* is delivered.
1006
1007Returns a struct_siginfo containing information about the signal.
1008[clinic start generated code]*/
1009
Ross Lagerwallbc808222011-06-25 12:13:40 +02001010static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001011signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1012/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001013{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001014 sigset_t set;
1015 siginfo_t si;
1016 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001017 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001018
Tal Einatc7027b72015-05-16 14:14:49 +03001019 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001020 return NULL;
1021
Victor Stinnera453cd82015-03-20 12:54:28 +01001022 do {
1023 Py_BEGIN_ALLOW_THREADS
1024 err = sigwaitinfo(&set, &si);
1025 Py_END_ALLOW_THREADS
1026 } while (err == -1
1027 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001028 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001029 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001030
1031 return fill_siginfo(&si);
1032}
1033
Ross Lagerwallbc808222011-06-25 12:13:40 +02001034#endif /* #ifdef HAVE_SIGWAITINFO */
1035
1036#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001037
1038/*[clinic input]
1039signal.sigtimedwait
1040
1041 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001042 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001043 /
1044
1045Like sigwaitinfo(), but with a timeout.
1046
1047The timeout is specified in seconds, with floating point numbers allowed.
1048[clinic start generated code]*/
1049
Ross Lagerwallbc808222011-06-25 12:13:40 +02001050static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001051signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001052 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001053/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001054{
Victor Stinnera453cd82015-03-20 12:54:28 +01001055 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001056 sigset_t set;
1057 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001058 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001059 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001060
Victor Stinner869e1772015-03-30 03:49:14 +02001061 if (_PyTime_FromSecondsObject(&timeout,
1062 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001063 return NULL;
1064
Victor Stinnera453cd82015-03-20 12:54:28 +01001065 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001066 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1067 return NULL;
1068 }
1069
Tal Einatc7027b72015-05-16 14:14:49 +03001070 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001071 return NULL;
1072
Victor Stinner34dc0f42015-03-27 18:19:03 +01001073 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001074
1075 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001076 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1077 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001078
1079 Py_BEGIN_ALLOW_THREADS
1080 res = sigtimedwait(&set, &si, &ts);
1081 Py_END_ALLOW_THREADS
1082
1083 if (res != -1)
1084 break;
1085
1086 if (errno != EINTR) {
1087 if (errno == EAGAIN)
1088 Py_RETURN_NONE;
1089 else
1090 return PyErr_SetFromErrno(PyExc_OSError);
1091 }
1092
1093 /* sigtimedwait() was interrupted by a signal (EINTR) */
1094 if (PyErr_CheckSignals())
1095 return NULL;
1096
Victor Stinner34dc0f42015-03-27 18:19:03 +01001097 monotonic = _PyTime_GetMonotonicClock();
1098 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001099 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001100 break;
1101 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001102
1103 return fill_siginfo(&si);
1104}
1105
Ross Lagerwallbc808222011-06-25 12:13:40 +02001106#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1107
Victor Stinnerb3e72192011-05-08 01:46:11 +02001108
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001109#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001110
1111/*[clinic input]
1112signal.pthread_kill
1113
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001114 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001115 signalnum: int
1116 /
1117
1118Send a signal to a thread.
1119[clinic start generated code]*/
1120
Victor Stinnerb3e72192011-05-08 01:46:11 +02001121static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001122signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1123 int signalnum)
1124/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001125{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001126 int err;
1127
Tal Einatc7027b72015-05-16 14:14:49 +03001128 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001129 if (err != 0) {
1130 errno = err;
1131 PyErr_SetFromErrno(PyExc_OSError);
1132 return NULL;
1133 }
1134
1135 /* the signal may have been send to the current thread */
1136 if (PyErr_CheckSignals())
1137 return NULL;
1138
1139 Py_RETURN_NONE;
1140}
1141
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001142#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001143
1144
1145
Tal Einatc7027b72015-05-16 14:14:49 +03001146/* List of functions defined in the module -- some of the methoddefs are
1147 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001148static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001149 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1150 SIGNAL_ALARM_METHODDEF
1151 SIGNAL_SETITIMER_METHODDEF
1152 SIGNAL_GETITIMER_METHODDEF
1153 SIGNAL_SIGNAL_METHODDEF
1154 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001156 SIGNAL_SIGINTERRUPT_METHODDEF
1157 SIGNAL_PAUSE_METHODDEF
1158 SIGNAL_PTHREAD_KILL_METHODDEF
1159 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1160 SIGNAL_SIGPENDING_METHODDEF
1161 SIGNAL_SIGWAIT_METHODDEF
1162 SIGNAL_SIGWAITINFO_METHODDEF
1163 SIGNAL_SIGTIMEDWAIT_METHODDEF
1164 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001165};
1166
Barry Warsaw92971171997-01-03 00:14:25 +00001167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001169"This module provides mechanisms to use signal handlers in Python.\n\
1170\n\
1171Functions:\n\
1172\n\
1173alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001174setitimer() -- cause a signal (described below) after a specified\n\
1175 float time and the timer may restart then [Unix only]\n\
1176getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001177signal() -- set the action for a given signal\n\
1178getsignal() -- get the signal action for a given signal\n\
1179pause() -- wait until a signal arrives [Unix only]\n\
1180default_int_handler() -- default SIGINT handler\n\
1181\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001182signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001183SIG_DFL -- used to refer to the system default handler\n\
1184SIG_IGN -- used to ignore the signal\n\
1185NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001186SIGINT, SIGTERM, etc. -- signal numbers\n\
1187\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001188itimer constants:\n\
1189ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1190 expiration\n\
1191ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1192 and delivers SIGVTALRM upon expiration\n\
1193ITIMER_PROF -- decrements both when the process is executing and\n\
1194 when the system is executing on behalf of the process.\n\
1195 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1196 used to profile the time spent by the application\n\
1197 in user and kernel space. SIGPROF is delivered upon\n\
1198 expiration.\n\
1199\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001200*** IMPORTANT NOTICE ***\n\
1201A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001202the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001203
Martin v. Löwis1a214512008-06-11 05:26:20 +00001204static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001206 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 module_doc,
1208 -1,
1209 signal_methods,
1210 NULL,
1211 NULL,
1212 NULL,
1213 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001214};
1215
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001216PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001217PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PyObject *m, *d, *x;
1220 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 main_thread = PyThread_get_thread_ident();
1223 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 /* Create the module and add the functions */
1226 m = PyModule_Create(&signalmodule);
1227 if (m == NULL)
1228 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001229
Ross Lagerwallbc808222011-06-25 12:13:40 +02001230#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001231 if (!initialized) {
1232 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1233 return NULL;
1234 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001235 Py_INCREF((PyObject*) &SiginfoType);
1236 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1237 initialized = 1;
1238#endif
1239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 /* Add some symbolic constants to the module */
1241 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1244 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1245 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1248 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1249 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 x = PyLong_FromLong((long)NSIG);
1252 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1253 goto finally;
1254 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001255
Victor Stinnera9293352011-04-30 15:21:58 +02001256#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001257 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1258 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001259#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001260#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001261 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1262 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001263#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001264#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001265 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1266 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001267#endif
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1270 if (!x)
1271 goto finally;
1272 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001273
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001274 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 for (i = 1; i < NSIG; i++) {
1276 void (*t)(int);
1277 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001278 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (t == SIG_DFL)
1280 Handlers[i].func = DefaultHandler;
1281 else if (t == SIG_IGN)
1282 Handlers[i].func = IgnoreHandler;
1283 else
1284 Handlers[i].func = Py_None; /* None of our business */
1285 Py_INCREF(Handlers[i].func);
1286 }
1287 if (Handlers[SIGINT].func == DefaultHandler) {
1288 /* Install default int handler */
1289 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001290 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1292 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001293
1294#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001295 if (PyModule_AddIntMacro(m, SIGHUP))
1296 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001297#endif
1298#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001299 if (PyModule_AddIntMacro(m, SIGINT))
1300 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001301#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001302#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001303 if (PyModule_AddIntMacro(m, SIGBREAK))
1304 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001305#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001306#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001307 if (PyModule_AddIntMacro(m, SIGQUIT))
1308 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001309#endif
1310#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001311 if (PyModule_AddIntMacro(m, SIGILL))
1312 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001313#endif
1314#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001315 if (PyModule_AddIntMacro(m, SIGTRAP))
1316 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001317#endif
1318#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001319 if (PyModule_AddIntMacro(m, SIGIOT))
1320 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001321#endif
1322#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001323 if (PyModule_AddIntMacro(m, SIGABRT))
1324 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001325#endif
1326#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001327 if (PyModule_AddIntMacro(m, SIGEMT))
1328 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001329#endif
1330#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001331 if (PyModule_AddIntMacro(m, SIGFPE))
1332 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001333#endif
1334#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001335 if (PyModule_AddIntMacro(m, SIGKILL))
1336 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001337#endif
1338#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001339 if (PyModule_AddIntMacro(m, SIGBUS))
1340 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341#endif
1342#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001343 if (PyModule_AddIntMacro(m, SIGSEGV))
1344 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001345#endif
1346#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001347 if (PyModule_AddIntMacro(m, SIGSYS))
1348 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349#endif
1350#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001351 if (PyModule_AddIntMacro(m, SIGPIPE))
1352 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001353#endif
1354#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001355 if (PyModule_AddIntMacro(m, SIGALRM))
1356 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001357#endif
1358#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001359 if (PyModule_AddIntMacro(m, SIGTERM))
1360 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001361#endif
1362#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001363 if (PyModule_AddIntMacro(m, SIGUSR1))
1364 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001365#endif
1366#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001367 if (PyModule_AddIntMacro(m, SIGUSR2))
1368 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369#endif
1370#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001371 if (PyModule_AddIntMacro(m, SIGCLD))
1372 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373#endif
1374#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001375 if (PyModule_AddIntMacro(m, SIGCHLD))
1376 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001377#endif
1378#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001379 if (PyModule_AddIntMacro(m, SIGPWR))
1380 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381#endif
1382#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001383 if (PyModule_AddIntMacro(m, SIGIO))
1384 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001385#endif
1386#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001387 if (PyModule_AddIntMacro(m, SIGURG))
1388 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001389#endif
1390#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001391 if (PyModule_AddIntMacro(m, SIGWINCH))
1392 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#endif
1394#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001395 if (PyModule_AddIntMacro(m, SIGPOLL))
1396 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001397#endif
1398#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001399 if (PyModule_AddIntMacro(m, SIGSTOP))
1400 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401#endif
1402#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001403 if (PyModule_AddIntMacro(m, SIGTSTP))
1404 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405#endif
1406#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001407 if (PyModule_AddIntMacro(m, SIGCONT))
1408 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409#endif
1410#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGTTIN))
1412 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGTTOU))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
1418#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGVTALRM))
1420 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001421#endif
1422#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGPROF))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001426#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGXCPU))
1428 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001429#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001430#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGXFSZ))
1432 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001433#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001434#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGRTMIN))
1436 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001437#endif
1438#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGRTMAX))
1440 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001441#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001442#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001443 if (PyModule_AddIntMacro(m, SIGINFO))
1444 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001445#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001446
1447#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1449 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001450#endif
1451#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1453 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001454#endif
1455#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001456 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1457 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001458#endif
1459
1460#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001462 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001463 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001464 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001465#endif
1466
Brian Curtineb24d742010-04-12 17:16:38 +00001467#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001468 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1469 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001470#endif
1471
1472#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001473 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1474 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001475#endif
1476
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001477#ifdef MS_WINDOWS
1478 /* Create manual-reset event, initially unset */
1479 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1480#endif
1481
Martin v. Löwis1a214512008-06-11 05:26:20 +00001482 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 Py_DECREF(m);
1484 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001485 }
Barry Warsaw92971171997-01-03 00:14:25 +00001486
Barry Warsaw92971171997-01-03 00:14:25 +00001487 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001488 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001489}
1490
1491static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001492finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 int i;
1495 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 PyOS_setsig(SIGINT, old_siginthandler);
1498 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 for (i = 1; i < NSIG; i++) {
1501 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001502 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 Handlers[i].func = NULL;
1504 if (i != SIGINT && func != NULL && func != Py_None &&
1505 func != DefaultHandler && func != IgnoreHandler)
1506 PyOS_setsig(i, SIG_DFL);
1507 Py_XDECREF(func);
1508 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001509
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001510 Py_CLEAR(IntHandler);
1511 Py_CLEAR(DefaultHandler);
1512 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513}
1514
Barry Warsaw92971171997-01-03 00:14:25 +00001515
Barry Warsaw92971171997-01-03 00:14:25 +00001516/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001518PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 int i;
1521 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001522
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001523 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 if (PyThread_get_thread_ident() != main_thread)
1527 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 /*
1530 * The is_tripped variable is meant to speed up the calls to
1531 * PyErr_CheckSignals (both directly or via pending calls) when no
1532 * signal has arrived. This variable is set to 1 when a signal arrives
1533 * and it is set to 0 here, when we know some signals arrived. This way
1534 * we can run the registered handlers with no signals blocked.
1535 *
1536 * NOTE: with this approach we can have a situation where is_tripped is
1537 * 1 but we have no more signals to handle (Handlers[i].tripped
1538 * is 0 for every signal i). This won't do us any harm (except
1539 * we're gonna spent some cycles for nothing). This happens when
1540 * we receive a signal i after we zero is_tripped and before we
1541 * check Handlers[i].tripped.
1542 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001543 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 if (!(f = (PyObject *)PyEval_GetFrame()))
1546 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001549 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 PyObject *result = NULL;
1551 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001552 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (arglist) {
1555 result = PyEval_CallObject(Handlers[i].func,
1556 arglist);
1557 Py_DECREF(arglist);
1558 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001559 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001560 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001562 }
Barry Warsaw92971171997-01-03 00:14:25 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 Py_DECREF(result);
1565 }
1566 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001569}
1570
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001571
Barry Warsaw92971171997-01-03 00:14:25 +00001572/* Replacements for intrcheck.c functionality
1573 * Declared in pyerrors.h
1574 */
1575void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001576PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001577{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001578 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001579}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001580
1581void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001582PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001583{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001584 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 Py_DECREF(m);
1587 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001588}
1589
1590void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001591PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001594}
1595
1596int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001597PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001598{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001599 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (PyThread_get_thread_ident() != main_thread)
1601 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001602 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return 1;
1604 }
1605 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001606}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001607
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001608static void
1609_clear_pending_signals(void)
1610{
1611 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001612 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001613 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001614 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001615 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001616 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001617 }
1618}
1619
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001620void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001621_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001622{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001623 /* Clear the signal flags after forking so that they aren't handled
1624 * in both processes if they came in just before the fork() but before
1625 * the interpreter had an opportunity to call the handlers. issue9535. */
1626 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 main_thread = PyThread_get_thread_ident();
1628 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001629}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001630
1631int
1632_PyOS_IsMainThread(void)
1633{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001634 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001635}
1636
1637#ifdef MS_WINDOWS
1638void *_PyOS_SigintEvent(void)
1639{
1640 /* Returns a manual-reset event which gets tripped whenever
1641 SIGINT is received.
1642
1643 Python.h does not include windows.h so we do cannot use HANDLE
1644 as the return type of this function. We use void* instead. */
1645 return sigint_event;
1646}
1647#endif