blob: 75abc98bc4b593a66f83feb9c7655c1c83d64982 [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
88#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000089#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000090#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020091static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000092static pid_t main_pid;
93#endif
94
Victor Stinner2ec6b172011-05-15 10:21:59 +020095static volatile struct {
96 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000098} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099
Victor Stinner11517102014-07-29 23:31:34 +0200100#ifdef MS_WINDOWS
101#define INVALID_FD ((SOCKET_T)-1)
102
103static volatile struct {
104 SOCKET_T fd;
105 int use_send;
106 int send_err_set;
107 int send_errno;
108 int send_win_error;
109} wakeup = {INVALID_FD, 0, 0};
110#else
111#define INVALID_FD (-1)
Victor Stinner2ec6b172011-05-15 10:21:59 +0200112static volatile sig_atomic_t wakeup_fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200113#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000114
Christian Heimesb76922a2007-12-11 01:06:40 +0000115/* Speed up sigcheck() when none tripped */
116static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000117
Barry Warsaw92971171997-01-03 00:14:25 +0000118static PyObject *DefaultHandler;
119static PyObject *IgnoreHandler;
120static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000121
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000122/* On Solaris 8, gcc will produce a warning that the function
123 declaration is not a prototype. This is caused by the definition of
124 SIG_DFL as (void (*)())0; the correct declaration would have been
125 (void (*)(int))0. */
126
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000127static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000128
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100129#ifdef MS_WINDOWS
130static HANDLE sigint_event = NULL;
131#endif
132
Martin v. Löwis823725e2008-03-24 13:39:54 +0000133#ifdef HAVE_GETITIMER
134static PyObject *ItimerError;
135
136/* auxiliary functions for setitimer/getitimer */
137static void
138timeval_from_double(double d, struct timeval *tv)
139{
140 tv->tv_sec = floor(d);
141 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
142}
143
Christian Heimes1a8501c2008-10-02 19:56:01 +0000144Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145double_from_timeval(struct timeval *tv)
146{
147 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
148}
149
150static PyObject *
151itimer_retval(struct itimerval *iv)
152{
153 PyObject *r, *v;
154
155 r = PyTuple_New(2);
156 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000157 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158
159 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000160 Py_DECREF(r);
161 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000162 }
163
164 PyTuple_SET_ITEM(r, 0, v);
165
166 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
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, 1, v);
172
173 return r;
174}
175#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000176
Guido van Rossume4485b01994-09-07 14:32:49 +0000177static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000178signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 PyErr_SetNone(PyExc_KeyboardInterrupt);
181 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000182}
183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000184PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000185"default_int_handler(...)\n\
186\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000187The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000189
Thomas Wouters0796b002000-07-22 23:49:30 +0000190
191static int
192checksignals_witharg(void * unused)
193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000195}
196
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200197static int
Victor Stinner11517102014-07-29 23:31:34 +0200198report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200199{
200 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700201 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200202 PyErr_SetFromErrno(PyExc_OSError);
203 PySys_WriteStderr("Exception ignored when trying to write to the "
204 "signal wakeup fd:\n");
205 PyErr_WriteUnraisable(NULL);
206 errno = save_errno;
207 return 0;
208}
209
Victor Stinner11517102014-07-29 23:31:34 +0200210#ifdef MS_WINDOWS
211static int
212report_wakeup_send_error(void* Py_UNUSED(data))
213{
214 PyObject *res;
215
216 if (wakeup.send_win_error) {
217 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
218 recognizes the error codes used by both GetLastError() and
219 WSAGetLastError */
220 res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
221 }
222 else {
223 errno = wakeup.send_errno;
224 res = PyErr_SetFromErrno(PyExc_OSError);
225 }
226
227 assert(res == NULL);
228 wakeup.send_err_set = 0;
229
230 PySys_WriteStderr("Exception ignored when trying to send to the "
231 "signal wakeup fd:\n");
232 PyErr_WriteUnraisable(NULL);
233
234 return 0;
235}
236#endif /* MS_WINDOWS */
237
Tim Peters4f1b2082000-07-23 21:18:09 +0000238static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200239trip_signal(int sig_num)
240{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200241 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200242 int fd;
243 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200244
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200245 Handlers[sig_num].tripped = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200246
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700247 if (!is_tripped) {
248 /* Set is_tripped after setting .tripped, as it gets
249 cleared in PyErr_CheckSignals() before .tripped. */
250 is_tripped = 1;
251 Py_AddPendingCall(checksignals_witharg, NULL);
252 }
253
254 /* And then write to the wakeup fd *after* setting all the globals and
255 doing the Py_AddPendingCall. We used to write to the wakeup fd and then
256 set the flag, but this allowed the following sequence of events
257 (especially on windows, where trip_signal runs in a new thread):
258
259 - main thread blocks on select([wakeup_fd], ...)
260 - signal arrives
261 - trip_signal writes to the wakeup fd
262 - the main thread wakes up
263 - the main thread checks the signal flags, sees that they're unset
264 - the main thread empties the wakeup fd
265 - the main thread goes back to sleep
266 - trip_signal sets the flags to request the Python-level signal handler
267 be run
268 - the main thread doesn't notice, because it's asleep
269
270 See bpo-30038 for more details.
271 */
272
Victor Stinner11517102014-07-29 23:31:34 +0200273#ifdef MS_WINDOWS
274 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
275#else
276 fd = wakeup_fd;
277#endif
278
279 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200280 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200281#ifdef MS_WINDOWS
282 if (wakeup.use_send) {
283 do {
284 rc = send(fd, &byte, 1, 0);
285 } while (rc < 0 && errno == EINTR);
286
287 /* we only have a storage for one error in the wakeup structure */
288 if (rc < 0 && !wakeup.send_err_set) {
289 wakeup.send_err_set = 1;
290 wakeup.send_errno = errno;
291 wakeup.send_win_error = GetLastError();
292 Py_AddPendingCall(report_wakeup_send_error, NULL);
293 }
294 }
295 else
296#endif
297 {
298 byte = (unsigned char)sig_num;
Victor Stinnere72fe392015-04-01 18:35:22 +0200299
300 /* _Py_write_noraise() retries write() if write() is interrupted by
301 a signal (fails with EINTR). */
302 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200303
304 if (rc < 0) {
305 Py_AddPendingCall(report_wakeup_write_error,
Benjamin Petersonca470632016-09-06 13:47:26 -0700306 (void *)(intptr_t)errno);
Victor Stinner11517102014-07-29 23:31:34 +0200307 }
308 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200309 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200310}
311
312static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000313signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000315 int save_errno = errno;
316
Antoine Pitrou39a65912010-11-05 19:47:27 +0000317#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000320#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000321 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200322 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000324
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000325#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000326#ifdef SIGCHLD
327 /* To avoid infinite recursion, this signal remains
328 reset until explicit re-instated.
329 Don't clear the 'func' field as it is our pointer
330 to the Python handler... */
331 if (sig_num != SIGCHLD)
332#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000334 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 * makes this true. See also issue8354. */
336 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000337#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000338
339 /* Issue #10311: asynchronously executing signal handlers should not
340 mutate errno under the feet of unsuspecting C code. */
341 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100342
343#ifdef MS_WINDOWS
344 if (sig_num == SIGINT)
345 SetEvent(sigint_event);
346#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000347}
Guido van Rossume4485b01994-09-07 14:32:49 +0000348
Guido van Rossum06d511d1995-03-10 15:13:48 +0000349
Guido van Rossum1171ee61997-08-22 20:42:00 +0000350#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300351
352/*[clinic input]
353signal.alarm -> long
354
355 seconds: int
356 /
357
358Arrange for SIGALRM to arrive after the given number of seconds.
359[clinic start generated code]*/
360
361static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300362signal_alarm_impl(PyObject *module, int seconds)
363/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300366 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000367}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000368
Guido van Rossum06d511d1995-03-10 15:13:48 +0000369#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000370
Guido van Rossum1171ee61997-08-22 20:42:00 +0000371#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300372
373/*[clinic input]
374signal.pause
375
376Wait until a signal arrives.
377[clinic start generated code]*/
378
Guido van Rossuma597dde1995-01-10 20:56:29 +0000379static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300380signal_pause_impl(PyObject *module)
381/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_BEGIN_ALLOW_THREADS
384 (void)pause();
385 Py_END_ALLOW_THREADS
386 /* make sure that any exceptions that got raised are propagated
387 * back into Python
388 */
389 if (PyErr_CheckSignals())
390 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000391
Tal Einatc7027b72015-05-16 14:14:49 +0300392 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000393}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000394
Guido van Rossum06d511d1995-03-10 15:13:48 +0000395#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000396
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000397
Tal Einatc7027b72015-05-16 14:14:49 +0300398/*[clinic input]
399signal.signal
400
401 signalnum: int
402 handler: object
403 /
404
405Set the action for the given signal.
406
407The action can be SIG_DFL, SIG_IGN, or a callable Python object.
408The previous action is returned. See getsignal() for possible return values.
409
410*** IMPORTANT NOTICE ***
411A signal handler function is called with two arguments:
412the first is the signal number, the second is the interrupted stack frame.
413[clinic start generated code]*/
414
Guido van Rossume4485b01994-09-07 14:32:49 +0000415static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300416signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
417/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyObject *old_handler;
420 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000421#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300422 /* Validate that signalnum is one of the allowable signals */
423 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000424 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000425#ifdef SIGBREAK
426 /* Issue #10003: SIGBREAK is not documented as permitted, but works
427 and corresponds to CTRL_BREAK_EVENT. */
428 case SIGBREAK: break;
429#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000430 case SIGFPE: break;
431 case SIGILL: break;
432 case SIGINT: break;
433 case SIGSEGV: break;
434 case SIGTERM: break;
435 default:
436 PyErr_SetString(PyExc_ValueError, "invalid signal value");
437 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000438 }
439#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (PyThread_get_thread_ident() != main_thread) {
442 PyErr_SetString(PyExc_ValueError,
443 "signal only works in main thread");
444 return NULL;
445 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000446#endif
Tal Einatc7027b72015-05-16 14:14:49 +0300447 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyErr_SetString(PyExc_ValueError,
449 "signal number out of range");
450 return NULL;
451 }
Tal Einatc7027b72015-05-16 14:14:49 +0300452 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300454 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300456 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return NULL;
460 }
461 else
462 func = signal_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300463 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200464 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return NULL;
466 }
Tal Einatc7027b72015-05-16 14:14:49 +0300467 old_handler = Handlers[signalnum].func;
468 Handlers[signalnum].tripped = 0;
469 Py_INCREF(handler);
470 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200471 if (old_handler != NULL)
472 return old_handler;
473 else
474 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000475}
476
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000477
Tal Einatc7027b72015-05-16 14:14:49 +0300478/*[clinic input]
479signal.getsignal
480
481 signalnum: int
482 /
483
484Return the current action for the given signal.
485
486The return value can be:
487 SIG_IGN -- if the signal is being ignored
488 SIG_DFL -- if the default action for the signal is in effect
489 None -- if an unknown handler is in effect
490 anything else -- the callable Python object used as a handler
491[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000492
Guido van Rossume4485b01994-09-07 14:32:49 +0000493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300494signal_getsignal_impl(PyObject *module, int signalnum)
495/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300498 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyErr_SetString(PyExc_ValueError,
500 "signal number out of range");
501 return NULL;
502 }
Tal Einatc7027b72015-05-16 14:14:49 +0300503 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200504 if (old_handler != NULL) {
505 Py_INCREF(old_handler);
506 return old_handler;
507 }
508 else {
509 Py_RETURN_NONE;
510 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511}
512
Christian Heimes8640e742008-02-23 16:23:06 +0000513#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300514
515/*[clinic input]
516signal.siginterrupt
517
518 signalnum: int
519 flag: int
520 /
521
522Change system call restart behaviour.
523
524If flag is False, system calls will be restarted when interrupted by
525signal sig, else system calls will be interrupted.
526[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000527
528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300529signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
530/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000531{
Tal Einatc7027b72015-05-16 14:14:49 +0300532 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyErr_SetString(PyExc_ValueError,
534 "signal number out of range");
535 return NULL;
536 }
Tal Einatc7027b72015-05-16 14:14:49 +0300537 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200538 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return NULL;
540 }
Tal Einatc7027b72015-05-16 14:14:49 +0300541 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000542}
543
544#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000545
Tal Einatc7027b72015-05-16 14:14:49 +0300546
547static PyObject*
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000548signal_set_wakeup_fd(PyObject *self, PyObject *args)
549{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200550 struct _Py_stat_struct status;
Victor Stinner11517102014-07-29 23:31:34 +0200551#ifdef MS_WINDOWS
552 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100553 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200554 int res;
555 int res_size = sizeof res;
556 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200557 int is_socket;
558
559 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
560 return NULL;
561
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100562 sockfd = PyLong_AsSocket_t(fdobj);
563 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200564 return NULL;
565#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
569 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200570#endif
571
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000572#ifdef WITH_THREAD
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 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000578#endif
Victor Stinner0bffc942014-07-21 16:28:54 +0200579
Victor Stinner11517102014-07-29 23:31:34 +0200580#ifdef MS_WINDOWS
581 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100582 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200583 /* Import the _socket module to call WSAStartup() */
584 mod = PyImport_ImportModuleNoBlock("_socket");
585 if (mod == NULL)
586 return NULL;
587 Py_DECREF(mod);
588
589 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100590 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200591 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100592 int fd, err;
593
594 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200595 if (err != WSAENOTSOCK) {
596 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
597 return NULL;
598 }
599
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100600 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700601 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200602 PyErr_SetString(PyExc_ValueError, "invalid fd");
603 return NULL;
604 }
605
Victor Stinnere134a7f2015-03-30 10:09:31 +0200606 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200607 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200608
609 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200610 }
Victor Stinner38227602014-08-27 12:59:44 +0200611 else {
Victor Stinner11517102014-07-29 23:31:34 +0200612 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200613
614 /* Windows does not provide a function to test if a socket
615 is in non-blocking mode */
616 }
Victor Stinner11517102014-07-29 23:31:34 +0200617 }
618
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100619 old_sockfd = wakeup.fd;
620 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200621 wakeup.use_send = is_socket;
622
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100623 if (old_sockfd != INVALID_FD)
624 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200625 else
626 return PyLong_FromLong(-1);
627#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200628 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200629 int blocking;
630
Victor Stinnere134a7f2015-03-30 10:09:31 +0200631 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200632 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200633
634 blocking = _Py_get_blocking(fd);
635 if (blocking < 0)
636 return NULL;
637 if (blocking) {
638 PyErr_Format(PyExc_ValueError,
639 "the fd %i must be in non-blocking mode",
640 fd);
641 return NULL;
642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 old_fd = wakeup_fd;
646 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200649#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000650}
651
652PyDoc_STRVAR(set_wakeup_fd_doc,
653"set_wakeup_fd(fd) -> fd\n\
654\n\
Victor Stinner11517102014-07-29 23:31:34 +0200655Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000656comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200657The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000658\n\
659The fd must be non-blocking.");
660
661/* C API for the same, without all the error checking */
662int
663PySignal_SetWakeupFd(int fd)
664{
Victor Stinner11517102014-07-29 23:31:34 +0200665 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 if (fd < 0)
667 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200668
669#ifdef MS_WINDOWS
670 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
671 wakeup.fd = fd;
672#else
673 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200675#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000677}
678
679
Martin v. Löwis823725e2008-03-24 13:39:54 +0000680#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300681
682/*[clinic input]
683signal.setitimer
684
685 which: int
686 seconds: double
687 interval: double = 0.0
688 /
689
690Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
691
692The timer will fire after value seconds and after that every interval seconds.
693The itimer can be cleared by setting seconds to zero.
694
695Returns old values as a tuple: (delay, interval).
696[clinic start generated code]*/
697
Martin v. Löwis823725e2008-03-24 13:39:54 +0000698static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300699signal_setitimer_impl(PyObject *module, int which, double seconds,
Tal Einatc7027b72015-05-16 14:14:49 +0300700 double interval)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300701/*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000702{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000703 struct itimerval new, old;
704
Tal Einatc7027b72015-05-16 14:14:49 +0300705 timeval_from_double(seconds, &new.it_value);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000706 timeval_from_double(interval, &new.it_interval);
707 /* Let OS check "which" value */
708 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300709 PyErr_SetFromErrno(ItimerError);
710 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000711 }
712
713 return itimer_retval(&old);
714}
715
Martin v. Löwis823725e2008-03-24 13:39:54 +0000716#endif
717
718
719#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300720
721/*[clinic input]
722signal.getitimer
723
724 which: int
725 /
726
727Returns current value of given itimer.
728[clinic start generated code]*/
729
Martin v. Löwis823725e2008-03-24 13:39:54 +0000730static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300731signal_getitimer_impl(PyObject *module, int which)
732/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000733{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000734 struct itimerval old;
735
Martin v. Löwis823725e2008-03-24 13:39:54 +0000736 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300737 PyErr_SetFromErrno(ItimerError);
738 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000739 }
740
741 return itimer_retval(&old);
742}
743
Martin v. Löwis823725e2008-03-24 13:39:54 +0000744#endif
745
Ross Lagerwallbc808222011-06-25 12:13:40 +0200746#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
747 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200748/* Convert an iterable to a sigset.
749 Return 0 on success, return -1 and raise an exception on error. */
750
751static int
752iterable_to_sigset(PyObject *iterable, sigset_t *mask)
753{
754 int result = -1;
755 PyObject *iterator, *item;
756 long signum;
757 int err;
758
759 sigemptyset(mask);
760
761 iterator = PyObject_GetIter(iterable);
762 if (iterator == NULL)
763 goto error;
764
765 while (1)
766 {
767 item = PyIter_Next(iterator);
768 if (item == NULL) {
769 if (PyErr_Occurred())
770 goto error;
771 else
772 break;
773 }
774
775 signum = PyLong_AsLong(item);
776 Py_DECREF(item);
777 if (signum == -1 && PyErr_Occurred())
778 goto error;
779 if (0 < signum && signum < NSIG)
780 err = sigaddset(mask, (int)signum);
781 else
782 err = 1;
783 if (err) {
784 PyErr_Format(PyExc_ValueError,
785 "signal number %ld out of range", signum);
786 goto error;
787 }
788 }
789 result = 0;
790
791error:
792 Py_XDECREF(iterator);
793 return result;
794}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200795#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200796
Victor Stinnerb3e72192011-05-08 01:46:11 +0200797#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200798static PyObject*
799sigset_to_set(sigset_t mask)
800{
801 PyObject *signum, *result;
802 int sig;
803
804 result = PySet_New(0);
805 if (result == NULL)
806 return NULL;
807
808 for (sig = 1; sig < NSIG; sig++) {
809 if (sigismember(&mask, sig) != 1)
810 continue;
811
812 /* Handle the case where it is a member by adding the signal to
813 the result list. Ignore the other cases because they mean the
814 signal isn't a member of the mask or the signal was invalid,
815 and an invalid signal must have been our fault in constructing
816 the loop boundaries. */
817 signum = PyLong_FromLong(sig);
818 if (signum == NULL) {
819 Py_DECREF(result);
820 return NULL;
821 }
822 if (PySet_Add(result, signum) == -1) {
823 Py_DECREF(signum);
824 Py_DECREF(result);
825 return NULL;
826 }
827 Py_DECREF(signum);
828 }
829 return result;
830}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200831#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200832
Victor Stinnerb3e72192011-05-08 01:46:11 +0200833#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300834
835/*[clinic input]
836signal.pthread_sigmask
837
838 how: int
839 mask: object
840 /
841
842Fetch and/or change the signal mask of the calling thread.
843[clinic start generated code]*/
844
Victor Stinnera9293352011-04-30 15:21:58 +0200845static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300846signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
847/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200848{
Tal Einatc7027b72015-05-16 14:14:49 +0300849 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200850 int err;
851
Tal Einatc7027b72015-05-16 14:14:49 +0300852 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200853 return NULL;
854
Tal Einatc7027b72015-05-16 14:14:49 +0300855 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200856 if (err != 0) {
857 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200858 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200859 return NULL;
860 }
861
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200862 /* if signals was unblocked, signal handlers have been called */
863 if (PyErr_CheckSignals())
864 return NULL;
865
Victor Stinner35b300c2011-05-04 13:20:35 +0200866 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200867}
868
Victor Stinnera9293352011-04-30 15:21:58 +0200869#endif /* #ifdef PYPTHREAD_SIGMASK */
870
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871
Victor Stinnerb3e72192011-05-08 01:46:11 +0200872#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300873
874/*[clinic input]
875signal.sigpending
876
877Examine pending signals.
878
879Returns a set of signal numbers that are pending for delivery to
880the calling thread.
881[clinic start generated code]*/
882
Victor Stinnerb3e72192011-05-08 01:46:11 +0200883static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300884signal_sigpending_impl(PyObject *module)
885/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200886{
887 int err;
888 sigset_t mask;
889 err = sigpending(&mask);
890 if (err)
891 return PyErr_SetFromErrno(PyExc_OSError);
892 return sigset_to_set(mask);
893}
894
Victor Stinnerb3e72192011-05-08 01:46:11 +0200895#endif /* #ifdef HAVE_SIGPENDING */
896
897
898#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300899
900/*[clinic input]
901signal.sigwait
902
903 sigset: object
904 /
905
906Wait for a signal.
907
908Suspend execution of the calling thread until the delivery of one of the
909signals specified in the signal set sigset. The function accepts the signal
910and returns the signal number.
911[clinic start generated code]*/
912
Victor Stinnerb3e72192011-05-08 01:46:11 +0200913static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300914signal_sigwait(PyObject *module, PyObject *sigset)
915/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200916{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917 sigset_t set;
918 int err, signum;
919
Tal Einatc7027b72015-05-16 14:14:49 +0300920 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200921 return NULL;
922
Victor Stinner10c30d62011-06-10 01:39:53 +0200923 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200924 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200925 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200926 if (err) {
927 errno = err;
928 return PyErr_SetFromErrno(PyExc_OSError);
929 }
930
931 return PyLong_FromLong(signum);
932}
933
Tal Einatc7027b72015-05-16 14:14:49 +0300934#endif /* #ifdef HAVE_SIGWAIT */
935
Victor Stinnerb3e72192011-05-08 01:46:11 +0200936
Ross Lagerwallbc808222011-06-25 12:13:40 +0200937#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
938static int initialized;
939static PyStructSequence_Field struct_siginfo_fields[] = {
940 {"si_signo", "signal number"},
941 {"si_code", "signal code"},
942 {"si_errno", "errno associated with this signal"},
943 {"si_pid", "sending process ID"},
944 {"si_uid", "real user ID of sending process"},
945 {"si_status", "exit value or signal"},
946 {"si_band", "band event for SIGPOLL"},
947 {0}
948};
949
950PyDoc_STRVAR(struct_siginfo__doc__,
951"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
952This object may be accessed either as a tuple of\n\
953(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
954or via the attributes si_signo, si_code, and so on.");
955
956static PyStructSequence_Desc struct_siginfo_desc = {
957 "signal.struct_siginfo", /* name */
958 struct_siginfo__doc__, /* doc */
959 struct_siginfo_fields, /* fields */
960 7 /* n_in_sequence */
961};
962
963static PyTypeObject SiginfoType;
964
965static PyObject *
966fill_siginfo(siginfo_t *si)
967{
968 PyObject *result = PyStructSequence_New(&SiginfoType);
969 if (!result)
970 return NULL;
971
972 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
973 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
974 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
975 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200976 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200977 PyStructSequence_SET_ITEM(result, 5,
978 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500979#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +0200980 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500981#else
982 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
983#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200984 if (PyErr_Occurred()) {
985 Py_DECREF(result);
986 return NULL;
987 }
988
989 return result;
990}
991#endif
992
993#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300994
995/*[clinic input]
996signal.sigwaitinfo
997
998 sigset: object
999 /
1000
1001Wait synchronously until one of the signals in *sigset* is delivered.
1002
1003Returns a struct_siginfo containing information about the signal.
1004[clinic start generated code]*/
1005
Ross Lagerwallbc808222011-06-25 12:13:40 +02001006static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001007signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1008/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001009{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001010 sigset_t set;
1011 siginfo_t si;
1012 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001013 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001014
Tal Einatc7027b72015-05-16 14:14:49 +03001015 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001016 return NULL;
1017
Victor Stinnera453cd82015-03-20 12:54:28 +01001018 do {
1019 Py_BEGIN_ALLOW_THREADS
1020 err = sigwaitinfo(&set, &si);
1021 Py_END_ALLOW_THREADS
1022 } while (err == -1
1023 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001024 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001025 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001026
1027 return fill_siginfo(&si);
1028}
1029
Ross Lagerwallbc808222011-06-25 12:13:40 +02001030#endif /* #ifdef HAVE_SIGWAITINFO */
1031
1032#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001033
1034/*[clinic input]
1035signal.sigtimedwait
1036
1037 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001038 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001039 /
1040
1041Like sigwaitinfo(), but with a timeout.
1042
1043The timeout is specified in seconds, with floating point numbers allowed.
1044[clinic start generated code]*/
1045
Ross Lagerwallbc808222011-06-25 12:13:40 +02001046static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001047signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001048 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001049/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001050{
Victor Stinnera453cd82015-03-20 12:54:28 +01001051 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001052 sigset_t set;
1053 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001054 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001055 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001056
Victor Stinner869e1772015-03-30 03:49:14 +02001057 if (_PyTime_FromSecondsObject(&timeout,
1058 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001059 return NULL;
1060
Victor Stinnera453cd82015-03-20 12:54:28 +01001061 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001062 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1063 return NULL;
1064 }
1065
Tal Einatc7027b72015-05-16 14:14:49 +03001066 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001067 return NULL;
1068
Victor Stinner34dc0f42015-03-27 18:19:03 +01001069 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001070
1071 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001072 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1073 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001074
1075 Py_BEGIN_ALLOW_THREADS
1076 res = sigtimedwait(&set, &si, &ts);
1077 Py_END_ALLOW_THREADS
1078
1079 if (res != -1)
1080 break;
1081
1082 if (errno != EINTR) {
1083 if (errno == EAGAIN)
1084 Py_RETURN_NONE;
1085 else
1086 return PyErr_SetFromErrno(PyExc_OSError);
1087 }
1088
1089 /* sigtimedwait() was interrupted by a signal (EINTR) */
1090 if (PyErr_CheckSignals())
1091 return NULL;
1092
Victor Stinner34dc0f42015-03-27 18:19:03 +01001093 monotonic = _PyTime_GetMonotonicClock();
1094 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001095 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001096 break;
1097 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001098
1099 return fill_siginfo(&si);
1100}
1101
Ross Lagerwallbc808222011-06-25 12:13:40 +02001102#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1103
Victor Stinnerb3e72192011-05-08 01:46:11 +02001104
1105#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
Tal Einatc7027b72015-05-16 14:14:49 +03001106
1107/*[clinic input]
1108signal.pthread_kill
1109
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001110 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001111 signalnum: int
1112 /
1113
1114Send a signal to a thread.
1115[clinic start generated code]*/
1116
Victor Stinnerb3e72192011-05-08 01:46:11 +02001117static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001118signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1119 int signalnum)
1120/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001121{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001122 int err;
1123
Tal Einatc7027b72015-05-16 14:14:49 +03001124 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001125 if (err != 0) {
1126 errno = err;
1127 PyErr_SetFromErrno(PyExc_OSError);
1128 return NULL;
1129 }
1130
1131 /* the signal may have been send to the current thread */
1132 if (PyErr_CheckSignals())
1133 return NULL;
1134
1135 Py_RETURN_NONE;
1136}
1137
Victor Stinnerb3e72192011-05-08 01:46:11 +02001138#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1139
1140
1141
Tal Einatc7027b72015-05-16 14:14:49 +03001142/* List of functions defined in the module -- some of the methoddefs are
1143 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001144static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001145 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1146 SIGNAL_ALARM_METHODDEF
1147 SIGNAL_SETITIMER_METHODDEF
1148 SIGNAL_GETITIMER_METHODDEF
1149 SIGNAL_SIGNAL_METHODDEF
1150 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001152 SIGNAL_SIGINTERRUPT_METHODDEF
1153 SIGNAL_PAUSE_METHODDEF
1154 SIGNAL_PTHREAD_KILL_METHODDEF
1155 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1156 SIGNAL_SIGPENDING_METHODDEF
1157 SIGNAL_SIGWAIT_METHODDEF
1158 SIGNAL_SIGWAITINFO_METHODDEF
1159 SIGNAL_SIGTIMEDWAIT_METHODDEF
1160 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001161};
1162
Barry Warsaw92971171997-01-03 00:14:25 +00001163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001164PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001165"This module provides mechanisms to use signal handlers in Python.\n\
1166\n\
1167Functions:\n\
1168\n\
1169alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001170setitimer() -- cause a signal (described below) after a specified\n\
1171 float time and the timer may restart then [Unix only]\n\
1172getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001173signal() -- set the action for a given signal\n\
1174getsignal() -- get the signal action for a given signal\n\
1175pause() -- wait until a signal arrives [Unix only]\n\
1176default_int_handler() -- default SIGINT handler\n\
1177\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001178signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001179SIG_DFL -- used to refer to the system default handler\n\
1180SIG_IGN -- used to ignore the signal\n\
1181NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001182SIGINT, SIGTERM, etc. -- signal numbers\n\
1183\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001184itimer constants:\n\
1185ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1186 expiration\n\
1187ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1188 and delivers SIGVTALRM upon expiration\n\
1189ITIMER_PROF -- decrements both when the process is executing and\n\
1190 when the system is executing on behalf of the process.\n\
1191 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1192 used to profile the time spent by the application\n\
1193 in user and kernel space. SIGPROF is delivered upon\n\
1194 expiration.\n\
1195\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001196*** IMPORTANT NOTICE ***\n\
1197A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001199
Martin v. Löwis1a214512008-06-11 05:26:20 +00001200static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001202 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 module_doc,
1204 -1,
1205 signal_methods,
1206 NULL,
1207 NULL,
1208 NULL,
1209 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001210};
1211
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001212PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001213PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 PyObject *m, *d, *x;
1216 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001217
1218#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 main_thread = PyThread_get_thread_ident();
1220 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001221#endif
1222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 /* Create the module and add the functions */
1224 m = PyModule_Create(&signalmodule);
1225 if (m == NULL)
1226 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001227
Ross Lagerwallbc808222011-06-25 12:13:40 +02001228#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001229 if (!initialized) {
1230 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1231 return NULL;
1232 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001233 Py_INCREF((PyObject*) &SiginfoType);
1234 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1235 initialized = 1;
1236#endif
1237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 /* Add some symbolic constants to the module */
1239 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1242 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1243 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1246 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1247 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 x = PyLong_FromLong((long)NSIG);
1250 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1251 goto finally;
1252 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001253
Victor Stinnera9293352011-04-30 15:21:58 +02001254#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001255 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1256 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001257#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001258#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001259 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1260 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001261#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001262#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001263 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1264 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001265#endif
1266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1268 if (!x)
1269 goto finally;
1270 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 Handlers[0].tripped = 0;
1273 for (i = 1; i < NSIG; i++) {
1274 void (*t)(int);
1275 t = PyOS_getsig(i);
1276 Handlers[i].tripped = 0;
1277 if (t == SIG_DFL)
1278 Handlers[i].func = DefaultHandler;
1279 else if (t == SIG_IGN)
1280 Handlers[i].func = IgnoreHandler;
1281 else
1282 Handlers[i].func = Py_None; /* None of our business */
1283 Py_INCREF(Handlers[i].func);
1284 }
1285 if (Handlers[SIGINT].func == DefaultHandler) {
1286 /* Install default int handler */
1287 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001288 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1290 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001291
1292#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001293 if (PyModule_AddIntMacro(m, SIGHUP))
1294 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001295#endif
1296#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001297 if (PyModule_AddIntMacro(m, SIGINT))
1298 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001300#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001301 if (PyModule_AddIntMacro(m, SIGBREAK))
1302 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001303#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001304#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001305 if (PyModule_AddIntMacro(m, SIGQUIT))
1306 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001307#endif
1308#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001309 if (PyModule_AddIntMacro(m, SIGILL))
1310 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001311#endif
1312#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001313 if (PyModule_AddIntMacro(m, SIGTRAP))
1314 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001315#endif
1316#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001317 if (PyModule_AddIntMacro(m, SIGIOT))
1318 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001319#endif
1320#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001321 if (PyModule_AddIntMacro(m, SIGABRT))
1322 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001323#endif
1324#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001325 if (PyModule_AddIntMacro(m, SIGEMT))
1326 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001327#endif
1328#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001329 if (PyModule_AddIntMacro(m, SIGFPE))
1330 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001331#endif
1332#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001333 if (PyModule_AddIntMacro(m, SIGKILL))
1334 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001335#endif
1336#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001337 if (PyModule_AddIntMacro(m, SIGBUS))
1338 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339#endif
1340#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001341 if (PyModule_AddIntMacro(m, SIGSEGV))
1342 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001343#endif
1344#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001345 if (PyModule_AddIntMacro(m, SIGSYS))
1346 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001347#endif
1348#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001349 if (PyModule_AddIntMacro(m, SIGPIPE))
1350 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001351#endif
1352#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001353 if (PyModule_AddIntMacro(m, SIGALRM))
1354 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001355#endif
1356#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001357 if (PyModule_AddIntMacro(m, SIGTERM))
1358 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001359#endif
1360#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001361 if (PyModule_AddIntMacro(m, SIGUSR1))
1362 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001363#endif
1364#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001365 if (PyModule_AddIntMacro(m, SIGUSR2))
1366 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001367#endif
1368#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001369 if (PyModule_AddIntMacro(m, SIGCLD))
1370 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001371#endif
1372#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001373 if (PyModule_AddIntMacro(m, SIGCHLD))
1374 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001375#endif
1376#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001377 if (PyModule_AddIntMacro(m, SIGPWR))
1378 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001379#endif
1380#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001381 if (PyModule_AddIntMacro(m, SIGIO))
1382 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383#endif
1384#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001385 if (PyModule_AddIntMacro(m, SIGURG))
1386 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001387#endif
1388#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001389 if (PyModule_AddIntMacro(m, SIGWINCH))
1390 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001391#endif
1392#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001393 if (PyModule_AddIntMacro(m, SIGPOLL))
1394 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001395#endif
1396#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001397 if (PyModule_AddIntMacro(m, SIGSTOP))
1398 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001399#endif
1400#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001401 if (PyModule_AddIntMacro(m, SIGTSTP))
1402 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403#endif
1404#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001405 if (PyModule_AddIntMacro(m, SIGCONT))
1406 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001407#endif
1408#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001409 if (PyModule_AddIntMacro(m, SIGTTIN))
1410 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001411#endif
1412#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001413 if (PyModule_AddIntMacro(m, SIGTTOU))
1414 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001415#endif
1416#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001417 if (PyModule_AddIntMacro(m, SIGVTALRM))
1418 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001419#endif
1420#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001421 if (PyModule_AddIntMacro(m, SIGPROF))
1422 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001423#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001424#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001425 if (PyModule_AddIntMacro(m, SIGXCPU))
1426 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001427#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001428#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001429 if (PyModule_AddIntMacro(m, SIGXFSZ))
1430 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001431#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001432#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001433 if (PyModule_AddIntMacro(m, SIGRTMIN))
1434 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001435#endif
1436#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001437 if (PyModule_AddIntMacro(m, SIGRTMAX))
1438 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001439#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001440#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001441 if (PyModule_AddIntMacro(m, SIGINFO))
1442 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001443#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001444
1445#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001446 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1447 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001448#endif
1449#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001450 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1451 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001452#endif
1453#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001454 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1455 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001456#endif
1457
1458#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001460 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001461 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001462 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001463#endif
1464
Brian Curtineb24d742010-04-12 17:16:38 +00001465#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001466 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1467 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001468#endif
1469
1470#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1472 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001473#endif
1474
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001475#ifdef MS_WINDOWS
1476 /* Create manual-reset event, initially unset */
1477 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1478#endif
1479
Martin v. Löwis1a214512008-06-11 05:26:20 +00001480 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 Py_DECREF(m);
1482 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001483 }
Barry Warsaw92971171997-01-03 00:14:25 +00001484
Barry Warsaw92971171997-01-03 00:14:25 +00001485 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001486 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001487}
1488
1489static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001490finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 int i;
1493 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 PyOS_setsig(SIGINT, old_siginthandler);
1496 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 for (i = 1; i < NSIG; i++) {
1499 func = Handlers[i].func;
1500 Handlers[i].tripped = 0;
1501 Handlers[i].func = NULL;
1502 if (i != SIGINT && func != NULL && func != Py_None &&
1503 func != DefaultHandler && func != IgnoreHandler)
1504 PyOS_setsig(i, SIG_DFL);
1505 Py_XDECREF(func);
1506 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001507
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001508 Py_CLEAR(IntHandler);
1509 Py_CLEAR(DefaultHandler);
1510 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001511}
1512
Barry Warsaw92971171997-01-03 00:14:25 +00001513
Barry Warsaw92971171997-01-03 00:14:25 +00001514/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001515int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001516PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 int i;
1519 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!is_tripped)
1522 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001523
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001524#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (PyThread_get_thread_ident() != main_thread)
1526 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001527#endif
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 */
1543 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++) {
1549 if (Handlers[i].tripped) {
1550 PyObject *result = NULL;
1551 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1552 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 }
1559 if (!result)
1560 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 Py_DECREF(result);
1563 }
1564 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001567}
1568
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001569
Barry Warsaw92971171997-01-03 00:14:25 +00001570/* Replacements for intrcheck.c functionality
1571 * Declared in pyerrors.h
1572 */
1573void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001574PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001575{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001576 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001577}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001578
1579void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001580PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001581{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001582 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 Py_DECREF(m);
1585 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001586}
1587
1588void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001589PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001592}
1593
1594int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001595PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001598#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 if (PyThread_get_thread_ident() != main_thread)
1600 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001601#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 Handlers[SIGINT].tripped = 0;
1603 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;
1612 if (!is_tripped)
1613 return;
1614 is_tripped = 0;
1615 for (i = 1; i < NSIG; ++i) {
1616 Handlers[i].tripped = 0;
1617 }
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();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001627#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 main_thread = PyThread_get_thread_ident();
1629 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001630#endif
1631}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001632
1633int
1634_PyOS_IsMainThread(void)
1635{
1636#ifdef WITH_THREAD
1637 return PyThread_get_thread_ident() == main_thread;
1638#else
1639 return 1;
1640#endif
1641}
1642
1643#ifdef MS_WINDOWS
1644void *_PyOS_SigintEvent(void)
1645{
1646 /* Returns a manual-reset event which gets tripped whenever
1647 SIGINT is received.
1648
1649 Python.h does not include windows.h so we do cannot use HANDLE
1650 as the return type of this function. We use void* instead. */
1651 return sigint_event;
1652}
1653#endif