blob: c4f0121644412224c47118a024491c4b9a444ecf [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
Guido van Rossum398d9fe1994-05-11 08:59:13 +000010
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020012#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000013#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000014#include <process.h>
15#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000017
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000019#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000020#endif
21#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000022#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000024#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000025#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000026#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000027
Victor Stinnera9293352011-04-30 15:21:58 +020028#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
29# define PYPTHREAD_SIGMASK
30#endif
31
32#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
33# include <pthread.h>
34#endif
35
Guido van Rossumbb4ba121994-06-23 11:25:45 +000036#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000037#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000038#endif
39
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000040#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000041# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000043# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000045# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000047# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000049# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000050#endif
51
52
Guido van Rossumbb4ba121994-06-23 11:25:45 +000053/*
54 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
55
56 When threads are supported, we want the following semantics:
57
58 - only the main thread can set a signal handler
59 - any thread can get a signal handler
60 - signals are only delivered to the main thread
61
62 I.e. we don't support "synchronous signals" like SIGFPE (catching
63 this doesn't make much sense in Python anyway) nor do we support
64 signals as a means of inter-thread communication, since not all
65 thread implementations support that (at least our thread library
66 doesn't).
67
68 We still have the problem that in some implementations signals
69 generated by the keyboard (e.g. SIGINT) are delivered to all
70 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
71 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000072 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000073 a working implementation that works in all three cases -- the
74 handler ignores signals if getpid() isn't the same as in the main
75 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000076*/
77
78#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000079#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000080#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000081static long main_thread;
82static pid_t main_pid;
83#endif
84
Victor Stinner2ec6b172011-05-15 10:21:59 +020085static volatile struct {
86 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000088} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000089
Victor Stinner2ec6b172011-05-15 10:21:59 +020090static volatile sig_atomic_t wakeup_fd = -1;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000091
Christian Heimesb76922a2007-12-11 01:06:40 +000092/* Speed up sigcheck() when none tripped */
93static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094
Barry Warsaw92971171997-01-03 00:14:25 +000095static PyObject *DefaultHandler;
96static PyObject *IgnoreHandler;
97static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000098
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000099/* On Solaris 8, gcc will produce a warning that the function
100 declaration is not a prototype. This is caused by the definition of
101 SIG_DFL as (void (*)())0; the correct declaration would have been
102 (void (*)(int))0. */
103
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000104static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000105
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100106#ifdef MS_WINDOWS
107static HANDLE sigint_event = NULL;
108#endif
109
Martin v. Löwis823725e2008-03-24 13:39:54 +0000110#ifdef HAVE_GETITIMER
111static PyObject *ItimerError;
112
113/* auxiliary functions for setitimer/getitimer */
114static void
115timeval_from_double(double d, struct timeval *tv)
116{
117 tv->tv_sec = floor(d);
118 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
119}
120
Christian Heimes1a8501c2008-10-02 19:56:01 +0000121Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000122double_from_timeval(struct timeval *tv)
123{
124 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
125}
126
127static PyObject *
128itimer_retval(struct itimerval *iv)
129{
130 PyObject *r, *v;
131
132 r = PyTuple_New(2);
133 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000135
136 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 Py_DECREF(r);
138 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000139 }
140
141 PyTuple_SET_ITEM(r, 0, v);
142
143 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 Py_DECREF(r);
145 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000146 }
147
148 PyTuple_SET_ITEM(r, 1, v);
149
150 return r;
151}
152#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000153
Guido van Rossume4485b01994-09-07 14:32:49 +0000154static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000155signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 PyErr_SetNone(PyExc_KeyboardInterrupt);
158 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000159}
160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000161PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000162"default_int_handler(...)\n\
163\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000164The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000165It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000166
Thomas Wouters0796b002000-07-22 23:49:30 +0000167
168static int
169checksignals_witharg(void * unused)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000172}
173
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200174static int
175report_wakeup_error(void *data)
176{
177 int save_errno = errno;
178 errno = (int) (Py_intptr_t) data;
179 PyErr_SetFromErrno(PyExc_OSError);
180 PySys_WriteStderr("Exception ignored when trying to write to the "
181 "signal wakeup fd:\n");
182 PyErr_WriteUnraisable(NULL);
183 errno = save_errno;
184 return 0;
185}
186
Tim Peters4f1b2082000-07-23 21:18:09 +0000187static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200188trip_signal(int sig_num)
189{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200190 unsigned char byte;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200191 int rc = 0;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200192
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200193 Handlers[sig_num].tripped = 1;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200194 if (wakeup_fd != -1) {
195 byte = (unsigned char)sig_num;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200196 while ((rc = write(wakeup_fd, &byte, 1)) == -1 && errno == EINTR);
197 if (rc == -1)
198 Py_AddPendingCall(report_wakeup_error, (void *) (Py_intptr_t) errno);
Victor Stinnerc13ef662011-05-25 02:35:58 +0200199 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200200 if (is_tripped)
201 return;
202 /* Set is_tripped after setting .tripped, as it gets
203 cleared in PyErr_CheckSignals() before .tripped. */
204 is_tripped = 1;
205 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200206}
207
208static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000209signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000211 int save_errno = errno;
212
Antoine Pitrou39a65912010-11-05 19:47:27 +0000213#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000215 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000216#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000217 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200218 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000220
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000221#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000222#ifdef SIGCHLD
223 /* To avoid infinite recursion, this signal remains
224 reset until explicit re-instated.
225 Don't clear the 'func' field as it is our pointer
226 to the Python handler... */
227 if (sig_num != SIGCHLD)
228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 /* If the handler was not set up with sigaction, reinstall it. See
230 * Python/pythonrun.c for the implementation of PyOS_setsig which
231 * makes this true. See also issue8354. */
232 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000233#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000234
235 /* Issue #10311: asynchronously executing signal handlers should not
236 mutate errno under the feet of unsuspecting C code. */
237 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100238
239#ifdef MS_WINDOWS
240 if (sig_num == SIGINT)
241 SetEvent(sigint_event);
242#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000243}
Guido van Rossume4485b01994-09-07 14:32:49 +0000244
Guido van Rossum06d511d1995-03-10 15:13:48 +0000245
Guido van Rossum1171ee61997-08-22 20:42:00 +0000246#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000247static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000248signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 int t;
251 if (!PyArg_ParseTuple(args, "i:alarm", &t))
252 return NULL;
253 /* alarm() returns the number of seconds remaining */
254 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000255}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000258"alarm(seconds)\n\
259\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000261#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262
Guido van Rossum1171ee61997-08-22 20:42:00 +0000263#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000264static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000265signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 Py_BEGIN_ALLOW_THREADS
268 (void)pause();
269 Py_END_ALLOW_THREADS
270 /* make sure that any exceptions that got raised are propagated
271 * back into Python
272 */
273 if (PyErr_CheckSignals())
274 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 Py_INCREF(Py_None);
277 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000278}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000280"pause()\n\
281\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000282Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000283
Guido van Rossum06d511d1995-03-10 15:13:48 +0000284#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000285
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000286
Guido van Rossume4485b01994-09-07 14:32:49 +0000287static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000288signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyObject *obj;
291 int sig_num;
292 PyObject *old_handler;
293 void (*func)(int);
294 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
295 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000296#ifdef MS_WINDOWS
297 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000298 switch (sig_num) {
299 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000300#ifdef SIGBREAK
301 /* Issue #10003: SIGBREAK is not documented as permitted, but works
302 and corresponds to CTRL_BREAK_EVENT. */
303 case SIGBREAK: break;
304#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000305 case SIGFPE: break;
306 case SIGILL: break;
307 case SIGINT: break;
308 case SIGSEGV: break;
309 case SIGTERM: break;
310 default:
311 PyErr_SetString(PyExc_ValueError, "invalid signal value");
312 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000313 }
314#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000315#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (PyThread_get_thread_ident() != main_thread) {
317 PyErr_SetString(PyExc_ValueError,
318 "signal only works in main thread");
319 return NULL;
320 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000321#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (sig_num < 1 || sig_num >= NSIG) {
323 PyErr_SetString(PyExc_ValueError,
324 "signal number out of range");
325 return NULL;
326 }
327 if (obj == IgnoreHandler)
328 func = SIG_IGN;
329 else if (obj == DefaultHandler)
330 func = SIG_DFL;
331 else if (!PyCallable_Check(obj)) {
332 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000333"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 return NULL;
335 }
336 else
337 func = signal_handler;
338 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200339 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 return NULL;
341 }
342 old_handler = Handlers[sig_num].func;
343 Handlers[sig_num].tripped = 0;
344 Py_INCREF(obj);
345 Handlers[sig_num].func = obj;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200346 if (old_handler != NULL)
347 return old_handler;
348 else
349 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350}
351
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000352PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000353"signal(sig, action) -> action\n\
354\n\
355Set the action for the given signal. The action can be SIG_DFL,\n\
356SIG_IGN, or a callable Python object. The previous action is\n\
357returned. See getsignal() for possible return values.\n\
358\n\
359*** IMPORTANT NOTICE ***\n\
360A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000361the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000362
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000363
Guido van Rossume4485b01994-09-07 14:32:49 +0000364static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000365signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 int sig_num;
368 PyObject *old_handler;
369 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
370 return NULL;
371 if (sig_num < 1 || sig_num >= NSIG) {
372 PyErr_SetString(PyExc_ValueError,
373 "signal number out of range");
374 return NULL;
375 }
376 old_handler = Handlers[sig_num].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200377 if (old_handler != NULL) {
378 Py_INCREF(old_handler);
379 return old_handler;
380 }
381 else {
382 Py_RETURN_NONE;
383 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384}
385
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000386PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000387"getsignal(sig) -> action\n\
388\n\
389Return the current action for the given signal. The return value can be:\n\
390SIG_IGN -- if the signal is being ignored\n\
391SIG_DFL -- if the default action for the signal is in effect\n\
392None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000393anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000394
Christian Heimes8640e742008-02-23 16:23:06 +0000395#ifdef HAVE_SIGINTERRUPT
396PyDoc_STRVAR(siginterrupt_doc,
397"siginterrupt(sig, flag) -> None\n\
398change system call restart behaviour: if flag is False, system calls\n\
399will be restarted when interrupted by signal sig, else system calls\n\
400will be interrupted.");
401
402static PyObject *
403signal_siginterrupt(PyObject *self, PyObject *args)
404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 int sig_num;
406 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
409 return NULL;
410 if (sig_num < 1 || sig_num >= NSIG) {
411 PyErr_SetString(PyExc_ValueError,
412 "signal number out of range");
413 return NULL;
414 }
415 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200416 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return NULL;
418 }
Christian Heimes8640e742008-02-23 16:23:06 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 Py_INCREF(Py_None);
421 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000422}
423
424#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000425
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000426static PyObject *
427signal_set_wakeup_fd(PyObject *self, PyObject *args)
428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 struct stat buf;
430 int fd, old_fd;
431 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
432 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000433#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (PyThread_get_thread_ident() != main_thread) {
435 PyErr_SetString(PyExc_ValueError,
436 "set_wakeup_fd only works in main thread");
437 return NULL;
438 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000439#endif
Victor Stinner0bffc942014-07-21 16:28:54 +0200440
441 if (fd != -1) {
442 if (!_PyVerify_fd(fd)) {
443 PyErr_SetString(PyExc_ValueError, "invalid fd");
444 return NULL;
445 }
446
447 if (fstat(fd, &buf) != 0)
448 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 old_fd = wakeup_fd;
452 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000455}
456
457PyDoc_STRVAR(set_wakeup_fd_doc,
458"set_wakeup_fd(fd) -> fd\n\
459\n\
460Sets the fd to be written to (with '\\0') when a signal\n\
461comes in. A library can use this to wakeup select or poll.\n\
462The previous fd is returned.\n\
463\n\
464The fd must be non-blocking.");
465
466/* C API for the same, without all the error checking */
467int
468PySignal_SetWakeupFd(int fd)
469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 int old_fd = wakeup_fd;
471 if (fd < 0)
472 fd = -1;
473 wakeup_fd = fd;
474 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000475}
476
477
Martin v. Löwis823725e2008-03-24 13:39:54 +0000478#ifdef HAVE_SETITIMER
479static PyObject *
480signal_setitimer(PyObject *self, PyObject *args)
481{
482 double first;
483 double interval = 0;
484 int which;
485 struct itimerval new, old;
486
487 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000489
490 timeval_from_double(first, &new.it_value);
491 timeval_from_double(interval, &new.it_interval);
492 /* Let OS check "which" value */
493 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyErr_SetFromErrno(ItimerError);
495 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000496 }
497
498 return itimer_retval(&old);
499}
500
501PyDoc_STRVAR(setitimer_doc,
502"setitimer(which, seconds[, interval])\n\
503\n\
504Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
505or ITIMER_PROF) to fire after value seconds and after\n\
506that every interval seconds.\n\
507The itimer can be cleared by setting seconds to zero.\n\
508\n\
509Returns old values as a tuple: (delay, interval).");
510#endif
511
512
513#ifdef HAVE_GETITIMER
514static PyObject *
515signal_getitimer(PyObject *self, PyObject *args)
516{
517 int which;
518 struct itimerval old;
519
520 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000522
523 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyErr_SetFromErrno(ItimerError);
525 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000526 }
527
528 return itimer_retval(&old);
529}
530
531PyDoc_STRVAR(getitimer_doc,
532"getitimer(which)\n\
533\n\
534Returns current value of given itimer.");
535#endif
536
Ross Lagerwallbc808222011-06-25 12:13:40 +0200537#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
538 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200539/* Convert an iterable to a sigset.
540 Return 0 on success, return -1 and raise an exception on error. */
541
542static int
543iterable_to_sigset(PyObject *iterable, sigset_t *mask)
544{
545 int result = -1;
546 PyObject *iterator, *item;
547 long signum;
548 int err;
549
550 sigemptyset(mask);
551
552 iterator = PyObject_GetIter(iterable);
553 if (iterator == NULL)
554 goto error;
555
556 while (1)
557 {
558 item = PyIter_Next(iterator);
559 if (item == NULL) {
560 if (PyErr_Occurred())
561 goto error;
562 else
563 break;
564 }
565
566 signum = PyLong_AsLong(item);
567 Py_DECREF(item);
568 if (signum == -1 && PyErr_Occurred())
569 goto error;
570 if (0 < signum && signum < NSIG)
571 err = sigaddset(mask, (int)signum);
572 else
573 err = 1;
574 if (err) {
575 PyErr_Format(PyExc_ValueError,
576 "signal number %ld out of range", signum);
577 goto error;
578 }
579 }
580 result = 0;
581
582error:
583 Py_XDECREF(iterator);
584 return result;
585}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200586#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200587
Victor Stinnerb3e72192011-05-08 01:46:11 +0200588#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200589static PyObject*
590sigset_to_set(sigset_t mask)
591{
592 PyObject *signum, *result;
593 int sig;
594
595 result = PySet_New(0);
596 if (result == NULL)
597 return NULL;
598
599 for (sig = 1; sig < NSIG; sig++) {
600 if (sigismember(&mask, sig) != 1)
601 continue;
602
603 /* Handle the case where it is a member by adding the signal to
604 the result list. Ignore the other cases because they mean the
605 signal isn't a member of the mask or the signal was invalid,
606 and an invalid signal must have been our fault in constructing
607 the loop boundaries. */
608 signum = PyLong_FromLong(sig);
609 if (signum == NULL) {
610 Py_DECREF(result);
611 return NULL;
612 }
613 if (PySet_Add(result, signum) == -1) {
614 Py_DECREF(signum);
615 Py_DECREF(result);
616 return NULL;
617 }
618 Py_DECREF(signum);
619 }
620 return result;
621}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200622#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200623
Victor Stinnerb3e72192011-05-08 01:46:11 +0200624#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200625static PyObject *
626signal_pthread_sigmask(PyObject *self, PyObject *args)
627{
Victor Stinner35b300c2011-05-04 13:20:35 +0200628 int how;
629 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200630 sigset_t mask, previous;
631 int err;
632
633 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
634 return NULL;
635
636 if (iterable_to_sigset(signals, &mask))
637 return NULL;
638
639 err = pthread_sigmask(how, &mask, &previous);
640 if (err != 0) {
641 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200642 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200643 return NULL;
644 }
645
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200646 /* if signals was unblocked, signal handlers have been called */
647 if (PyErr_CheckSignals())
648 return NULL;
649
Victor Stinner35b300c2011-05-04 13:20:35 +0200650 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200651}
652
653PyDoc_STRVAR(signal_pthread_sigmask_doc,
654"pthread_sigmask(how, mask) -> old mask\n\
655\n\
656Fetch and/or change the signal mask of the calling thread.");
657#endif /* #ifdef PYPTHREAD_SIGMASK */
658
Martin v. Löwis823725e2008-03-24 13:39:54 +0000659
Victor Stinnerb3e72192011-05-08 01:46:11 +0200660#ifdef HAVE_SIGPENDING
661static PyObject *
662signal_sigpending(PyObject *self)
663{
664 int err;
665 sigset_t mask;
666 err = sigpending(&mask);
667 if (err)
668 return PyErr_SetFromErrno(PyExc_OSError);
669 return sigset_to_set(mask);
670}
671
672PyDoc_STRVAR(signal_sigpending_doc,
673"sigpending() -> list\n\
674\n\
675Examine pending signals.");
676#endif /* #ifdef HAVE_SIGPENDING */
677
678
679#ifdef HAVE_SIGWAIT
680static PyObject *
681signal_sigwait(PyObject *self, PyObject *args)
682{
683 PyObject *signals;
684 sigset_t set;
685 int err, signum;
686
687 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
688 return NULL;
689
690 if (iterable_to_sigset(signals, &set))
691 return NULL;
692
Victor Stinner10c30d62011-06-10 01:39:53 +0200693 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200694 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200695 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200696 if (err) {
697 errno = err;
698 return PyErr_SetFromErrno(PyExc_OSError);
699 }
700
701 return PyLong_FromLong(signum);
702}
703
704PyDoc_STRVAR(signal_sigwait_doc,
705"sigwait(sigset) -> signum\n\
706\n\
707Wait a signal.");
708#endif /* #ifdef HAVE_SIGPENDING */
709
Ross Lagerwallbc808222011-06-25 12:13:40 +0200710#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
711static int initialized;
712static PyStructSequence_Field struct_siginfo_fields[] = {
713 {"si_signo", "signal number"},
714 {"si_code", "signal code"},
715 {"si_errno", "errno associated with this signal"},
716 {"si_pid", "sending process ID"},
717 {"si_uid", "real user ID of sending process"},
718 {"si_status", "exit value or signal"},
719 {"si_band", "band event for SIGPOLL"},
720 {0}
721};
722
723PyDoc_STRVAR(struct_siginfo__doc__,
724"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
725This object may be accessed either as a tuple of\n\
726(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
727or via the attributes si_signo, si_code, and so on.");
728
729static PyStructSequence_Desc struct_siginfo_desc = {
730 "signal.struct_siginfo", /* name */
731 struct_siginfo__doc__, /* doc */
732 struct_siginfo_fields, /* fields */
733 7 /* n_in_sequence */
734};
735
736static PyTypeObject SiginfoType;
737
738static PyObject *
739fill_siginfo(siginfo_t *si)
740{
741 PyObject *result = PyStructSequence_New(&SiginfoType);
742 if (!result)
743 return NULL;
744
745 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
746 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
747 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
748 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200749 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200750 PyStructSequence_SET_ITEM(result, 5,
751 PyLong_FromLong((long)(si->si_status)));
752 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
753 if (PyErr_Occurred()) {
754 Py_DECREF(result);
755 return NULL;
756 }
757
758 return result;
759}
760#endif
761
762#ifdef HAVE_SIGWAITINFO
763static PyObject *
764signal_sigwaitinfo(PyObject *self, PyObject *args)
765{
766 PyObject *signals;
767 sigset_t set;
768 siginfo_t si;
769 int err;
770
771 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
772 return NULL;
773
774 if (iterable_to_sigset(signals, &set))
775 return NULL;
776
777 Py_BEGIN_ALLOW_THREADS
778 err = sigwaitinfo(&set, &si);
779 Py_END_ALLOW_THREADS
780 if (err == -1)
781 return PyErr_SetFromErrno(PyExc_OSError);
782
783 return fill_siginfo(&si);
784}
785
786PyDoc_STRVAR(signal_sigwaitinfo_doc,
787"sigwaitinfo(sigset) -> struct_siginfo\n\
788\n\
789Wait synchronously for a signal until one of the signals in *sigset* is\n\
790delivered.\n\
791Returns a struct_siginfo containing information about the signal.");
792#endif /* #ifdef HAVE_SIGWAITINFO */
793
794#ifdef HAVE_SIGTIMEDWAIT
795static PyObject *
796signal_sigtimedwait(PyObject *self, PyObject *args)
797{
798 PyObject *signals, *timeout;
799 struct timespec buf;
800 sigset_t set;
801 siginfo_t si;
Antoine Pitroucf8a1e52013-04-17 22:06:44 +0200802 time_t tv_sec;
803 long tv_nsec;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200804 int err;
805
Victor Stinner643cd682012-03-02 22:54:03 +0100806 if (!PyArg_ParseTuple(args, "OO:sigtimedwait",
807 &signals, &timeout))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200808 return NULL;
809
Victor Stinner3c1b3792014-02-17 00:02:43 +0100810 if (_PyTime_ObjectToTimespec(timeout, &tv_sec, &tv_nsec,
811 _PyTime_ROUND_DOWN) == -1)
Ross Lagerwallbc808222011-06-25 12:13:40 +0200812 return NULL;
Antoine Pitroucf8a1e52013-04-17 22:06:44 +0200813 buf.tv_sec = tv_sec;
814 buf.tv_nsec = tv_nsec;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200815
816 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
817 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
818 return NULL;
819 }
820
821 if (iterable_to_sigset(signals, &set))
822 return NULL;
823
824 Py_BEGIN_ALLOW_THREADS
825 err = sigtimedwait(&set, &si, &buf);
826 Py_END_ALLOW_THREADS
827 if (err == -1) {
828 if (errno == EAGAIN)
829 Py_RETURN_NONE;
830 else
831 return PyErr_SetFromErrno(PyExc_OSError);
832 }
833
834 return fill_siginfo(&si);
835}
836
837PyDoc_STRVAR(signal_sigtimedwait_doc,
838"sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
839\n\
840Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
841nanoseconds).");
842#endif /* #ifdef HAVE_SIGTIMEDWAIT */
843
Victor Stinnerb3e72192011-05-08 01:46:11 +0200844
845#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
846static PyObject *
847signal_pthread_kill(PyObject *self, PyObject *args)
848{
849 long tid;
850 int signum;
851 int err;
852
853 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
854 return NULL;
855
Victor Stinner86e104a2011-05-09 14:45:38 +0200856 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200857 if (err != 0) {
858 errno = err;
859 PyErr_SetFromErrno(PyExc_OSError);
860 return NULL;
861 }
862
863 /* the signal may have been send to the current thread */
864 if (PyErr_CheckSignals())
865 return NULL;
866
867 Py_RETURN_NONE;
868}
869
870PyDoc_STRVAR(signal_pthread_kill_doc,
871"pthread_kill(thread_id, signum)\n\
872\n\
873Send a signal to a thread.");
874#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
875
876
877
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000878/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000879static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000880#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000882#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000883#ifdef HAVE_SETITIMER
884 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
885#endif
886#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000888#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 {"signal", signal_signal, METH_VARARGS, signal_doc},
890 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
891 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000892#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000894#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000895#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200897 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000898#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 {"default_int_handler", signal_default_int_handler,
900 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200901#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
902 {"pthread_kill", (PyCFunction)signal_pthread_kill,
903 METH_VARARGS, signal_pthread_kill_doc},
904#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200905#ifdef PYPTHREAD_SIGMASK
906 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
907 METH_VARARGS, signal_pthread_sigmask_doc},
908#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200909#ifdef HAVE_SIGPENDING
910 {"sigpending", (PyCFunction)signal_sigpending,
911 METH_NOARGS, signal_sigpending_doc},
912#endif
913#ifdef HAVE_SIGWAIT
914 {"sigwait", (PyCFunction)signal_sigwait,
915 METH_VARARGS, signal_sigwait_doc},
916#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200917#ifdef HAVE_SIGWAITINFO
918 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
919 METH_VARARGS, signal_sigwaitinfo_doc},
920#endif
921#ifdef HAVE_SIGTIMEDWAIT
922 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
923 METH_VARARGS, signal_sigtimedwait_doc},
924#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000926};
927
Barry Warsaw92971171997-01-03 00:14:25 +0000928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000929PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000930"This module provides mechanisms to use signal handlers in Python.\n\
931\n\
932Functions:\n\
933\n\
934alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000935setitimer() -- cause a signal (described below) after a specified\n\
936 float time and the timer may restart then [Unix only]\n\
937getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000938signal() -- set the action for a given signal\n\
939getsignal() -- get the signal action for a given signal\n\
940pause() -- wait until a signal arrives [Unix only]\n\
941default_int_handler() -- default SIGINT handler\n\
942\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000943signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000944SIG_DFL -- used to refer to the system default handler\n\
945SIG_IGN -- used to ignore the signal\n\
946NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000947SIGINT, SIGTERM, etc. -- signal numbers\n\
948\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000949itimer constants:\n\
950ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
951 expiration\n\
952ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
953 and delivers SIGVTALRM upon expiration\n\
954ITIMER_PROF -- decrements both when the process is executing and\n\
955 when the system is executing on behalf of the process.\n\
956 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
957 used to profile the time spent by the application\n\
958 in user and kernel space. SIGPROF is delivered upon\n\
959 expiration.\n\
960\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000961*** IMPORTANT NOTICE ***\n\
962A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000963the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000964
Martin v. Löwis1a214512008-06-11 05:26:20 +0000965static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -0400967 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 module_doc,
969 -1,
970 signal_methods,
971 NULL,
972 NULL,
973 NULL,
974 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000975};
976
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000977PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +0200978PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 PyObject *m, *d, *x;
981 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000982
983#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 main_thread = PyThread_get_thread_ident();
985 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000986#endif
987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* Create the module and add the functions */
989 m = PyModule_Create(&signalmodule);
990 if (m == NULL)
991 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000992
Ross Lagerwallbc808222011-06-25 12:13:40 +0200993#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +0200994 if (!initialized) {
995 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
996 return NULL;
997 }
Ross Lagerwallbc808222011-06-25 12:13:40 +0200998 Py_INCREF((PyObject*) &SiginfoType);
999 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1000 initialized = 1;
1001#endif
1002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 /* Add some symbolic constants to the module */
1004 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1007 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1008 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1011 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1012 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 x = PyLong_FromLong((long)NSIG);
1015 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1016 goto finally;
1017 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001018
Victor Stinnera9293352011-04-30 15:21:58 +02001019#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001020 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1021 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001022#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001023#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001024 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1025 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001026#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001027#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001028 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1029 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001030#endif
1031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1033 if (!x)
1034 goto finally;
1035 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Handlers[0].tripped = 0;
1038 for (i = 1; i < NSIG; i++) {
1039 void (*t)(int);
1040 t = PyOS_getsig(i);
1041 Handlers[i].tripped = 0;
1042 if (t == SIG_DFL)
1043 Handlers[i].func = DefaultHandler;
1044 else if (t == SIG_IGN)
1045 Handlers[i].func = IgnoreHandler;
1046 else
1047 Handlers[i].func = Py_None; /* None of our business */
1048 Py_INCREF(Handlers[i].func);
1049 }
1050 if (Handlers[SIGINT].func == DefaultHandler) {
1051 /* Install default int handler */
1052 Py_INCREF(IntHandler);
1053 Py_DECREF(Handlers[SIGINT].func);
1054 Handlers[SIGINT].func = IntHandler;
1055 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1056 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001057
1058#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 x = PyLong_FromLong(SIGHUP);
1060 PyDict_SetItemString(d, "SIGHUP", x);
1061 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001062#endif
1063#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 x = PyLong_FromLong(SIGINT);
1065 PyDict_SetItemString(d, "SIGINT", x);
1066 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001067#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001068#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 x = PyLong_FromLong(SIGBREAK);
1070 PyDict_SetItemString(d, "SIGBREAK", x);
1071 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001072#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001073#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 x = PyLong_FromLong(SIGQUIT);
1075 PyDict_SetItemString(d, "SIGQUIT", x);
1076 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001077#endif
1078#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 x = PyLong_FromLong(SIGILL);
1080 PyDict_SetItemString(d, "SIGILL", x);
1081 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001082#endif
1083#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 x = PyLong_FromLong(SIGTRAP);
1085 PyDict_SetItemString(d, "SIGTRAP", x);
1086 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001087#endif
1088#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 x = PyLong_FromLong(SIGIOT);
1090 PyDict_SetItemString(d, "SIGIOT", x);
1091 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001092#endif
1093#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 x = PyLong_FromLong(SIGABRT);
1095 PyDict_SetItemString(d, "SIGABRT", x);
1096 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001097#endif
1098#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 x = PyLong_FromLong(SIGEMT);
1100 PyDict_SetItemString(d, "SIGEMT", x);
1101 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001102#endif
1103#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 x = PyLong_FromLong(SIGFPE);
1105 PyDict_SetItemString(d, "SIGFPE", x);
1106 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001107#endif
1108#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 x = PyLong_FromLong(SIGKILL);
1110 PyDict_SetItemString(d, "SIGKILL", x);
1111 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001112#endif
1113#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 x = PyLong_FromLong(SIGBUS);
1115 PyDict_SetItemString(d, "SIGBUS", x);
1116 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001117#endif
1118#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 x = PyLong_FromLong(SIGSEGV);
1120 PyDict_SetItemString(d, "SIGSEGV", x);
1121 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001122#endif
1123#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 x = PyLong_FromLong(SIGSYS);
1125 PyDict_SetItemString(d, "SIGSYS", x);
1126 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001127#endif
1128#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 x = PyLong_FromLong(SIGPIPE);
1130 PyDict_SetItemString(d, "SIGPIPE", x);
1131 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001132#endif
1133#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 x = PyLong_FromLong(SIGALRM);
1135 PyDict_SetItemString(d, "SIGALRM", x);
1136 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001137#endif
1138#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 x = PyLong_FromLong(SIGTERM);
1140 PyDict_SetItemString(d, "SIGTERM", x);
1141 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001142#endif
1143#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 x = PyLong_FromLong(SIGUSR1);
1145 PyDict_SetItemString(d, "SIGUSR1", x);
1146 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001147#endif
1148#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 x = PyLong_FromLong(SIGUSR2);
1150 PyDict_SetItemString(d, "SIGUSR2", x);
1151 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001152#endif
1153#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 x = PyLong_FromLong(SIGCLD);
1155 PyDict_SetItemString(d, "SIGCLD", x);
1156 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001157#endif
1158#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 x = PyLong_FromLong(SIGCHLD);
1160 PyDict_SetItemString(d, "SIGCHLD", x);
1161 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001162#endif
1163#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 x = PyLong_FromLong(SIGPWR);
1165 PyDict_SetItemString(d, "SIGPWR", x);
1166 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001167#endif
1168#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 x = PyLong_FromLong(SIGIO);
1170 PyDict_SetItemString(d, "SIGIO", x);
1171 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001172#endif
1173#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 x = PyLong_FromLong(SIGURG);
1175 PyDict_SetItemString(d, "SIGURG", x);
1176 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001177#endif
1178#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 x = PyLong_FromLong(SIGWINCH);
1180 PyDict_SetItemString(d, "SIGWINCH", x);
1181 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001182#endif
1183#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 x = PyLong_FromLong(SIGPOLL);
1185 PyDict_SetItemString(d, "SIGPOLL", x);
1186 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001187#endif
1188#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 x = PyLong_FromLong(SIGSTOP);
1190 PyDict_SetItemString(d, "SIGSTOP", x);
1191 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001192#endif
1193#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 x = PyLong_FromLong(SIGTSTP);
1195 PyDict_SetItemString(d, "SIGTSTP", x);
1196 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001197#endif
1198#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 x = PyLong_FromLong(SIGCONT);
1200 PyDict_SetItemString(d, "SIGCONT", x);
1201 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001202#endif
1203#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 x = PyLong_FromLong(SIGTTIN);
1205 PyDict_SetItemString(d, "SIGTTIN", x);
1206 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001207#endif
1208#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 x = PyLong_FromLong(SIGTTOU);
1210 PyDict_SetItemString(d, "SIGTTOU", x);
1211 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001212#endif
1213#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 x = PyLong_FromLong(SIGVTALRM);
1215 PyDict_SetItemString(d, "SIGVTALRM", x);
1216 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001217#endif
1218#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 x = PyLong_FromLong(SIGPROF);
1220 PyDict_SetItemString(d, "SIGPROF", x);
1221 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001222#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001223#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 x = PyLong_FromLong(SIGXCPU);
1225 PyDict_SetItemString(d, "SIGXCPU", x);
1226 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001227#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001228#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 x = PyLong_FromLong(SIGXFSZ);
1230 PyDict_SetItemString(d, "SIGXFSZ", x);
1231 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001232#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001233#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 x = PyLong_FromLong(SIGRTMIN);
1235 PyDict_SetItemString(d, "SIGRTMIN", x);
1236 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001237#endif
1238#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 x = PyLong_FromLong(SIGRTMAX);
1240 PyDict_SetItemString(d, "SIGRTMAX", x);
1241 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001242#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001243#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 x = PyLong_FromLong(SIGINFO);
1245 PyDict_SetItemString(d, "SIGINFO", x);
1246 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001247#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001248
1249#ifdef ITIMER_REAL
1250 x = PyLong_FromLong(ITIMER_REAL);
1251 PyDict_SetItemString(d, "ITIMER_REAL", x);
1252 Py_DECREF(x);
1253#endif
1254#ifdef ITIMER_VIRTUAL
1255 x = PyLong_FromLong(ITIMER_VIRTUAL);
1256 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1257 Py_DECREF(x);
1258#endif
1259#ifdef ITIMER_PROF
1260 x = PyLong_FromLong(ITIMER_PROF);
1261 PyDict_SetItemString(d, "ITIMER_PROF", x);
1262 Py_DECREF(x);
1263#endif
1264
1265#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 ItimerError = PyErr_NewException("signal.ItimerError",
1267 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001268 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001270#endif
1271
Brian Curtineb24d742010-04-12 17:16:38 +00001272#ifdef CTRL_C_EVENT
1273 x = PyLong_FromLong(CTRL_C_EVENT);
1274 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1275 Py_DECREF(x);
1276#endif
1277
1278#ifdef CTRL_BREAK_EVENT
1279 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1280 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1281 Py_DECREF(x);
1282#endif
1283
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001284#ifdef MS_WINDOWS
1285 /* Create manual-reset event, initially unset */
1286 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1287#endif
1288
Martin v. Löwis1a214512008-06-11 05:26:20 +00001289 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 Py_DECREF(m);
1291 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001292 }
Barry Warsaw92971171997-01-03 00:14:25 +00001293
Barry Warsaw92971171997-01-03 00:14:25 +00001294 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001295 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001296}
1297
1298static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001299finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 int i;
1302 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 PyOS_setsig(SIGINT, old_siginthandler);
1305 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 for (i = 1; i < NSIG; i++) {
1308 func = Handlers[i].func;
1309 Handlers[i].tripped = 0;
1310 Handlers[i].func = NULL;
1311 if (i != SIGINT && func != NULL && func != Py_None &&
1312 func != DefaultHandler && func != IgnoreHandler)
1313 PyOS_setsig(i, SIG_DFL);
1314 Py_XDECREF(func);
1315 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001316
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001317 Py_CLEAR(IntHandler);
1318 Py_CLEAR(DefaultHandler);
1319 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001320}
1321
Barry Warsaw92971171997-01-03 00:14:25 +00001322
Barry Warsaw92971171997-01-03 00:14:25 +00001323/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001324int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001325PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 int i;
1328 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (!is_tripped)
1331 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001332
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001333#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (PyThread_get_thread_ident() != main_thread)
1335 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001336#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 /*
1339 * The is_tripped variable is meant to speed up the calls to
1340 * PyErr_CheckSignals (both directly or via pending calls) when no
1341 * signal has arrived. This variable is set to 1 when a signal arrives
1342 * and it is set to 0 here, when we know some signals arrived. This way
1343 * we can run the registered handlers with no signals blocked.
1344 *
1345 * NOTE: with this approach we can have a situation where is_tripped is
1346 * 1 but we have no more signals to handle (Handlers[i].tripped
1347 * is 0 for every signal i). This won't do us any harm (except
1348 * we're gonna spent some cycles for nothing). This happens when
1349 * we receive a signal i after we zero is_tripped and before we
1350 * check Handlers[i].tripped.
1351 */
1352 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (!(f = (PyObject *)PyEval_GetFrame()))
1355 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 for (i = 1; i < NSIG; i++) {
1358 if (Handlers[i].tripped) {
1359 PyObject *result = NULL;
1360 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1361 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (arglist) {
1364 result = PyEval_CallObject(Handlers[i].func,
1365 arglist);
1366 Py_DECREF(arglist);
1367 }
1368 if (!result)
1369 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 Py_DECREF(result);
1372 }
1373 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001376}
1377
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001378
Barry Warsaw92971171997-01-03 00:14:25 +00001379/* Replacements for intrcheck.c functionality
1380 * Declared in pyerrors.h
1381 */
1382void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001383PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001384{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001385 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001386}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001387
1388void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001389PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001390{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001391 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 Py_DECREF(m);
1394 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001395}
1396
1397void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001398PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401}
1402
1403int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001404PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001407#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 if (PyThread_get_thread_ident() != main_thread)
1409 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001410#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 Handlers[SIGINT].tripped = 0;
1412 return 1;
1413 }
1414 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001415}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001416
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001417static void
1418_clear_pending_signals(void)
1419{
1420 int i;
1421 if (!is_tripped)
1422 return;
1423 is_tripped = 0;
1424 for (i = 1; i < NSIG; ++i) {
1425 Handlers[i].tripped = 0;
1426 }
1427}
1428
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001429void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001430PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001431{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001432 /* Clear the signal flags after forking so that they aren't handled
1433 * in both processes if they came in just before the fork() but before
1434 * the interpreter had an opportunity to call the handlers. issue9535. */
1435 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001436#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001437 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1438 * can be called safely. */
1439 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001440 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyEval_ReInitThreads();
1442 main_thread = PyThread_get_thread_ident();
1443 main_pid = getpid();
1444 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001445#endif
1446}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001447
1448int
1449_PyOS_IsMainThread(void)
1450{
1451#ifdef WITH_THREAD
1452 return PyThread_get_thread_ident() == main_thread;
1453#else
1454 return 1;
1455#endif
1456}
1457
1458#ifdef MS_WINDOWS
1459void *_PyOS_SigintEvent(void)
1460{
1461 /* Returns a manual-reset event which gets tripped whenever
1462 SIGINT is received.
1463
1464 Python.h does not include windows.h so we do cannot use HANDLE
1465 as the return type of this function. We use void* instead. */
1466 return sigint_event;
1467}
1468#endif