blob: 41acbfa9dab98c42e2b7809f658bd73e4df1ce2c [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
Brian Curtineb24d742010-04-12 17:16:38 +000012#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
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000040#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000041#define NSIG 12
42#include <process.h>
43#endif
44
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG _NSIG /* For BSD/SysV */
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 QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000054# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000055#endif
56
57
Guido van Rossumbb4ba121994-06-23 11:25:45 +000058/*
59 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
60
61 When threads are supported, we want the following semantics:
62
63 - only the main thread can set a signal handler
64 - any thread can get a signal handler
65 - signals are only delivered to the main thread
66
67 I.e. we don't support "synchronous signals" like SIGFPE (catching
68 this doesn't make much sense in Python anyway) nor do we support
69 signals as a means of inter-thread communication, since not all
70 thread implementations support that (at least our thread library
71 doesn't).
72
73 We still have the problem that in some implementations signals
74 generated by the keyboard (e.g. SIGINT) are delivered to all
75 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
76 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000077 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000078 a working implementation that works in all three cases -- the
79 handler ignores signals if getpid() isn't the same as in the main
80 thread. XXX This is a hack.
81
Guido van Rossum9e8181b2000-09-19 00:46:46 +000082 GNU pth is a user-space threading library, and as such, all threads
83 run within the same process. In this case, if the currently running
84 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000085*/
86
87#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000088#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000089#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000090static long main_thread;
91static pid_t main_pid;
92#endif
93
Victor Stinner2ec6b172011-05-15 10:21:59 +020094static volatile struct {
95 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000097} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000098
Victor Stinner2ec6b172011-05-15 10:21:59 +020099static volatile sig_atomic_t wakeup_fd = -1;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000100
Christian Heimesb76922a2007-12-11 01:06:40 +0000101/* Speed up sigcheck() when none tripped */
102static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000103
Barry Warsaw92971171997-01-03 00:14:25 +0000104static PyObject *DefaultHandler;
105static PyObject *IgnoreHandler;
106static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000107
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000108/* On Solaris 8, gcc will produce a warning that the function
109 declaration is not a prototype. This is caused by the definition of
110 SIG_DFL as (void (*)())0; the correct declaration would have been
111 (void (*)(int))0. */
112
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000113static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000114
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100115#ifdef MS_WINDOWS
116static HANDLE sigint_event = NULL;
117#endif
118
Martin v. Löwis823725e2008-03-24 13:39:54 +0000119#ifdef HAVE_GETITIMER
120static PyObject *ItimerError;
121
122/* auxiliary functions for setitimer/getitimer */
123static void
124timeval_from_double(double d, struct timeval *tv)
125{
126 tv->tv_sec = floor(d);
127 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
128}
129
Christian Heimes1a8501c2008-10-02 19:56:01 +0000130Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000131double_from_timeval(struct timeval *tv)
132{
133 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
134}
135
136static PyObject *
137itimer_retval(struct itimerval *iv)
138{
139 PyObject *r, *v;
140
141 r = PyTuple_New(2);
142 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000144
145 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 Py_DECREF(r);
147 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000148 }
149
150 PyTuple_SET_ITEM(r, 0, v);
151
152 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 Py_DECREF(r);
154 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000155 }
156
157 PyTuple_SET_ITEM(r, 1, v);
158
159 return r;
160}
161#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000162
Guido van Rossume4485b01994-09-07 14:32:49 +0000163static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000164signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 PyErr_SetNone(PyExc_KeyboardInterrupt);
167 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000168}
169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000170PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000171"default_int_handler(...)\n\
172\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000173The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000174It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000175
Thomas Wouters0796b002000-07-22 23:49:30 +0000176
177static int
178checksignals_witharg(void * unused)
179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000181}
182
Tim Peters4f1b2082000-07-23 21:18:09 +0000183static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200184trip_signal(int sig_num)
185{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200186 unsigned char byte;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200187
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200188 Handlers[sig_num].tripped = 1;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200189 if (wakeup_fd != -1) {
190 byte = (unsigned char)sig_num;
191 write(wakeup_fd, &byte, 1);
192 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200193 if (is_tripped)
194 return;
195 /* Set is_tripped after setting .tripped, as it gets
196 cleared in PyErr_CheckSignals() before .tripped. */
197 is_tripped = 1;
198 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200199}
200
201static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000202signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000203{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000204 int save_errno = errno;
205
206#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (PyThread_get_thread_ident() != main_thread) {
208 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000210 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000211#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000212 {
213#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
236 /* Issue #10311: asynchronously executing signal handlers should not
237 mutate errno under the feet of unsuspecting C code. */
238 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100239
240#ifdef MS_WINDOWS
241 if (sig_num == SIGINT)
242 SetEvent(sigint_event);
243#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000244}
Guido van Rossume4485b01994-09-07 14:32:49 +0000245
Guido van Rossum06d511d1995-03-10 15:13:48 +0000246
Guido van Rossum1171ee61997-08-22 20:42:00 +0000247#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000248static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000249signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 int t;
252 if (!PyArg_ParseTuple(args, "i:alarm", &t))
253 return NULL;
254 /* alarm() returns the number of seconds remaining */
255 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000256}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000259"alarm(seconds)\n\
260\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000261Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000262#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossum1171ee61997-08-22 20:42:00 +0000264#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000265static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000266signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 Py_BEGIN_ALLOW_THREADS
269 (void)pause();
270 Py_END_ALLOW_THREADS
271 /* make sure that any exceptions that got raised are propagated
272 * back into Python
273 */
274 if (PyErr_CheckSignals())
275 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_INCREF(Py_None);
278 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000279}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000280PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000281"pause()\n\
282\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000283Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000284
Guido van Rossum06d511d1995-03-10 15:13:48 +0000285#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000286
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000287
Guido van Rossume4485b01994-09-07 14:32:49 +0000288static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000289signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyObject *obj;
292 int sig_num;
293 PyObject *old_handler;
294 void (*func)(int);
295 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
296 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000297#ifdef MS_WINDOWS
298 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000299 switch (sig_num) {
300 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000301#ifdef SIGBREAK
302 /* Issue #10003: SIGBREAK is not documented as permitted, but works
303 and corresponds to CTRL_BREAK_EVENT. */
304 case SIGBREAK: break;
305#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000306 case SIGFPE: break;
307 case SIGILL: break;
308 case SIGINT: break;
309 case SIGSEGV: break;
310 case SIGTERM: break;
311 default:
312 PyErr_SetString(PyExc_ValueError, "invalid signal value");
313 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000314 }
315#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000316#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 if (PyThread_get_thread_ident() != main_thread) {
318 PyErr_SetString(PyExc_ValueError,
319 "signal only works in main thread");
320 return NULL;
321 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000322#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (sig_num < 1 || sig_num >= NSIG) {
324 PyErr_SetString(PyExc_ValueError,
325 "signal number out of range");
326 return NULL;
327 }
328 if (obj == IgnoreHandler)
329 func = SIG_IGN;
330 else if (obj == DefaultHandler)
331 func = SIG_DFL;
332 else if (!PyCallable_Check(obj)) {
333 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000334"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 return NULL;
336 }
337 else
338 func = signal_handler;
339 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200340 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return NULL;
342 }
343 old_handler = Handlers[sig_num].func;
344 Handlers[sig_num].tripped = 0;
345 Py_INCREF(obj);
346 Handlers[sig_num].func = obj;
347 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348}
349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000351"signal(sig, action) -> action\n\
352\n\
353Set the action for the given signal. The action can be SIG_DFL,\n\
354SIG_IGN, or a callable Python object. The previous action is\n\
355returned. See getsignal() for possible return values.\n\
356\n\
357*** IMPORTANT NOTICE ***\n\
358A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000360
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000361
Guido van Rossume4485b01994-09-07 14:32:49 +0000362static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000363signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 int sig_num;
366 PyObject *old_handler;
367 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
368 return NULL;
369 if (sig_num < 1 || sig_num >= NSIG) {
370 PyErr_SetString(PyExc_ValueError,
371 "signal number out of range");
372 return NULL;
373 }
374 old_handler = Handlers[sig_num].func;
375 Py_INCREF(old_handler);
376 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000377}
378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000380"getsignal(sig) -> action\n\
381\n\
382Return the current action for the given signal. The return value can be:\n\
383SIG_IGN -- if the signal is being ignored\n\
384SIG_DFL -- if the default action for the signal is in effect\n\
385None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000386anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000387
Christian Heimes8640e742008-02-23 16:23:06 +0000388#ifdef HAVE_SIGINTERRUPT
389PyDoc_STRVAR(siginterrupt_doc,
390"siginterrupt(sig, flag) -> None\n\
391change system call restart behaviour: if flag is False, system calls\n\
392will be restarted when interrupted by signal sig, else system calls\n\
393will be interrupted.");
394
395static PyObject *
396signal_siginterrupt(PyObject *self, PyObject *args)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 int sig_num;
399 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
402 return NULL;
403 if (sig_num < 1 || sig_num >= NSIG) {
404 PyErr_SetString(PyExc_ValueError,
405 "signal number out of range");
406 return NULL;
407 }
408 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200409 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return NULL;
411 }
Christian Heimes8640e742008-02-23 16:23:06 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 Py_INCREF(Py_None);
414 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000415}
416
417#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000418
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000419static PyObject *
420signal_set_wakeup_fd(PyObject *self, PyObject *args)
421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 struct stat buf;
423 int fd, old_fd;
424 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
425 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000426#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (PyThread_get_thread_ident() != main_thread) {
428 PyErr_SetString(PyExc_ValueError,
429 "set_wakeup_fd only works in main thread");
430 return NULL;
431 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000432#endif
Benjamin Petersonc68a4a02013-01-18 00:10:24 -0500433 if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 PyErr_SetString(PyExc_ValueError, "invalid fd");
435 return NULL;
436 }
437 old_fd = wakeup_fd;
438 wakeup_fd = fd;
439 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000440}
441
442PyDoc_STRVAR(set_wakeup_fd_doc,
443"set_wakeup_fd(fd) -> fd\n\
444\n\
445Sets the fd to be written to (with '\\0') when a signal\n\
446comes in. A library can use this to wakeup select or poll.\n\
447The previous fd is returned.\n\
448\n\
449The fd must be non-blocking.");
450
451/* C API for the same, without all the error checking */
452int
453PySignal_SetWakeupFd(int fd)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 int old_fd = wakeup_fd;
456 if (fd < 0)
457 fd = -1;
458 wakeup_fd = fd;
459 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000460}
461
462
Martin v. Löwis823725e2008-03-24 13:39:54 +0000463#ifdef HAVE_SETITIMER
464static PyObject *
465signal_setitimer(PyObject *self, PyObject *args)
466{
467 double first;
468 double interval = 0;
469 int which;
470 struct itimerval new, old;
471
472 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000474
475 timeval_from_double(first, &new.it_value);
476 timeval_from_double(interval, &new.it_interval);
477 /* Let OS check "which" value */
478 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 PyErr_SetFromErrno(ItimerError);
480 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000481 }
482
483 return itimer_retval(&old);
484}
485
486PyDoc_STRVAR(setitimer_doc,
487"setitimer(which, seconds[, interval])\n\
488\n\
489Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
490or ITIMER_PROF) to fire after value seconds and after\n\
491that every interval seconds.\n\
492The itimer can be cleared by setting seconds to zero.\n\
493\n\
494Returns old values as a tuple: (delay, interval).");
495#endif
496
497
498#ifdef HAVE_GETITIMER
499static PyObject *
500signal_getitimer(PyObject *self, PyObject *args)
501{
502 int which;
503 struct itimerval old;
504
505 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000507
508 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 PyErr_SetFromErrno(ItimerError);
510 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000511 }
512
513 return itimer_retval(&old);
514}
515
516PyDoc_STRVAR(getitimer_doc,
517"getitimer(which)\n\
518\n\
519Returns current value of given itimer.");
520#endif
521
Ross Lagerwallbc808222011-06-25 12:13:40 +0200522#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
523 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200524/* Convert an iterable to a sigset.
525 Return 0 on success, return -1 and raise an exception on error. */
526
527static int
528iterable_to_sigset(PyObject *iterable, sigset_t *mask)
529{
530 int result = -1;
531 PyObject *iterator, *item;
532 long signum;
533 int err;
534
535 sigemptyset(mask);
536
537 iterator = PyObject_GetIter(iterable);
538 if (iterator == NULL)
539 goto error;
540
541 while (1)
542 {
543 item = PyIter_Next(iterator);
544 if (item == NULL) {
545 if (PyErr_Occurred())
546 goto error;
547 else
548 break;
549 }
550
551 signum = PyLong_AsLong(item);
552 Py_DECREF(item);
553 if (signum == -1 && PyErr_Occurred())
554 goto error;
555 if (0 < signum && signum < NSIG)
556 err = sigaddset(mask, (int)signum);
557 else
558 err = 1;
559 if (err) {
560 PyErr_Format(PyExc_ValueError,
561 "signal number %ld out of range", signum);
562 goto error;
563 }
564 }
565 result = 0;
566
567error:
568 Py_XDECREF(iterator);
569 return result;
570}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200571#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200572
Victor Stinnerb3e72192011-05-08 01:46:11 +0200573#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200574static PyObject*
575sigset_to_set(sigset_t mask)
576{
577 PyObject *signum, *result;
578 int sig;
579
580 result = PySet_New(0);
581 if (result == NULL)
582 return NULL;
583
584 for (sig = 1; sig < NSIG; sig++) {
585 if (sigismember(&mask, sig) != 1)
586 continue;
587
588 /* Handle the case where it is a member by adding the signal to
589 the result list. Ignore the other cases because they mean the
590 signal isn't a member of the mask or the signal was invalid,
591 and an invalid signal must have been our fault in constructing
592 the loop boundaries. */
593 signum = PyLong_FromLong(sig);
594 if (signum == NULL) {
595 Py_DECREF(result);
596 return NULL;
597 }
598 if (PySet_Add(result, signum) == -1) {
599 Py_DECREF(signum);
600 Py_DECREF(result);
601 return NULL;
602 }
603 Py_DECREF(signum);
604 }
605 return result;
606}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200607#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200608
Victor Stinnerb3e72192011-05-08 01:46:11 +0200609#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200610static PyObject *
611signal_pthread_sigmask(PyObject *self, PyObject *args)
612{
Victor Stinner35b300c2011-05-04 13:20:35 +0200613 int how;
614 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200615 sigset_t mask, previous;
616 int err;
617
618 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
619 return NULL;
620
621 if (iterable_to_sigset(signals, &mask))
622 return NULL;
623
624 err = pthread_sigmask(how, &mask, &previous);
625 if (err != 0) {
626 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200627 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200628 return NULL;
629 }
630
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200631 /* if signals was unblocked, signal handlers have been called */
632 if (PyErr_CheckSignals())
633 return NULL;
634
Victor Stinner35b300c2011-05-04 13:20:35 +0200635 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200636}
637
638PyDoc_STRVAR(signal_pthread_sigmask_doc,
639"pthread_sigmask(how, mask) -> old mask\n\
640\n\
641Fetch and/or change the signal mask of the calling thread.");
642#endif /* #ifdef PYPTHREAD_SIGMASK */
643
Martin v. Löwis823725e2008-03-24 13:39:54 +0000644
Victor Stinnerb3e72192011-05-08 01:46:11 +0200645#ifdef HAVE_SIGPENDING
646static PyObject *
647signal_sigpending(PyObject *self)
648{
649 int err;
650 sigset_t mask;
651 err = sigpending(&mask);
652 if (err)
653 return PyErr_SetFromErrno(PyExc_OSError);
654 return sigset_to_set(mask);
655}
656
657PyDoc_STRVAR(signal_sigpending_doc,
658"sigpending() -> list\n\
659\n\
660Examine pending signals.");
661#endif /* #ifdef HAVE_SIGPENDING */
662
663
664#ifdef HAVE_SIGWAIT
665static PyObject *
666signal_sigwait(PyObject *self, PyObject *args)
667{
668 PyObject *signals;
669 sigset_t set;
670 int err, signum;
671
672 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
673 return NULL;
674
675 if (iterable_to_sigset(signals, &set))
676 return NULL;
677
Victor Stinner10c30d62011-06-10 01:39:53 +0200678 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200679 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200680 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200681 if (err) {
682 errno = err;
683 return PyErr_SetFromErrno(PyExc_OSError);
684 }
685
686 return PyLong_FromLong(signum);
687}
688
689PyDoc_STRVAR(signal_sigwait_doc,
690"sigwait(sigset) -> signum\n\
691\n\
692Wait a signal.");
693#endif /* #ifdef HAVE_SIGPENDING */
694
Ross Lagerwallbc808222011-06-25 12:13:40 +0200695#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
696static int initialized;
697static PyStructSequence_Field struct_siginfo_fields[] = {
698 {"si_signo", "signal number"},
699 {"si_code", "signal code"},
700 {"si_errno", "errno associated with this signal"},
701 {"si_pid", "sending process ID"},
702 {"si_uid", "real user ID of sending process"},
703 {"si_status", "exit value or signal"},
704 {"si_band", "band event for SIGPOLL"},
705 {0}
706};
707
708PyDoc_STRVAR(struct_siginfo__doc__,
709"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
710This object may be accessed either as a tuple of\n\
711(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
712or via the attributes si_signo, si_code, and so on.");
713
714static PyStructSequence_Desc struct_siginfo_desc = {
715 "signal.struct_siginfo", /* name */
716 struct_siginfo__doc__, /* doc */
717 struct_siginfo_fields, /* fields */
718 7 /* n_in_sequence */
719};
720
721static PyTypeObject SiginfoType;
722
723static PyObject *
724fill_siginfo(siginfo_t *si)
725{
726 PyObject *result = PyStructSequence_New(&SiginfoType);
727 if (!result)
728 return NULL;
729
730 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
731 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
732 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
733 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200734 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200735 PyStructSequence_SET_ITEM(result, 5,
736 PyLong_FromLong((long)(si->si_status)));
737 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
738 if (PyErr_Occurred()) {
739 Py_DECREF(result);
740 return NULL;
741 }
742
743 return result;
744}
745#endif
746
747#ifdef HAVE_SIGWAITINFO
748static PyObject *
749signal_sigwaitinfo(PyObject *self, PyObject *args)
750{
751 PyObject *signals;
752 sigset_t set;
753 siginfo_t si;
754 int err;
755
756 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
757 return NULL;
758
759 if (iterable_to_sigset(signals, &set))
760 return NULL;
761
762 Py_BEGIN_ALLOW_THREADS
763 err = sigwaitinfo(&set, &si);
764 Py_END_ALLOW_THREADS
765 if (err == -1)
766 return PyErr_SetFromErrno(PyExc_OSError);
767
768 return fill_siginfo(&si);
769}
770
771PyDoc_STRVAR(signal_sigwaitinfo_doc,
772"sigwaitinfo(sigset) -> struct_siginfo\n\
773\n\
774Wait synchronously for a signal until one of the signals in *sigset* is\n\
775delivered.\n\
776Returns a struct_siginfo containing information about the signal.");
777#endif /* #ifdef HAVE_SIGWAITINFO */
778
779#ifdef HAVE_SIGTIMEDWAIT
780static PyObject *
781signal_sigtimedwait(PyObject *self, PyObject *args)
782{
783 PyObject *signals, *timeout;
784 struct timespec buf;
785 sigset_t set;
786 siginfo_t si;
787 int err;
788
Victor Stinner643cd682012-03-02 22:54:03 +0100789 if (!PyArg_ParseTuple(args, "OO:sigtimedwait",
790 &signals, &timeout))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200791 return NULL;
792
Victor Stinner643cd682012-03-02 22:54:03 +0100793 if (_PyTime_ObjectToTimespec(timeout, &buf.tv_sec, &buf.tv_nsec) == -1)
Ross Lagerwallbc808222011-06-25 12:13:40 +0200794 return NULL;
795
796 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
797 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
798 return NULL;
799 }
800
801 if (iterable_to_sigset(signals, &set))
802 return NULL;
803
804 Py_BEGIN_ALLOW_THREADS
805 err = sigtimedwait(&set, &si, &buf);
806 Py_END_ALLOW_THREADS
807 if (err == -1) {
808 if (errno == EAGAIN)
809 Py_RETURN_NONE;
810 else
811 return PyErr_SetFromErrno(PyExc_OSError);
812 }
813
814 return fill_siginfo(&si);
815}
816
817PyDoc_STRVAR(signal_sigtimedwait_doc,
818"sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
819\n\
820Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
821nanoseconds).");
822#endif /* #ifdef HAVE_SIGTIMEDWAIT */
823
Victor Stinnerb3e72192011-05-08 01:46:11 +0200824
825#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
826static PyObject *
827signal_pthread_kill(PyObject *self, PyObject *args)
828{
829 long tid;
830 int signum;
831 int err;
832
833 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
834 return NULL;
835
Victor Stinner86e104a2011-05-09 14:45:38 +0200836 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200837 if (err != 0) {
838 errno = err;
839 PyErr_SetFromErrno(PyExc_OSError);
840 return NULL;
841 }
842
843 /* the signal may have been send to the current thread */
844 if (PyErr_CheckSignals())
845 return NULL;
846
847 Py_RETURN_NONE;
848}
849
850PyDoc_STRVAR(signal_pthread_kill_doc,
851"pthread_kill(thread_id, signum)\n\
852\n\
853Send a signal to a thread.");
854#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
855
856
857
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000858/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000859static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000860#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000862#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863#ifdef HAVE_SETITIMER
864 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
865#endif
866#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000868#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 {"signal", signal_signal, METH_VARARGS, signal_doc},
870 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
871 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000872#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000874#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000875#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200877 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000878#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 {"default_int_handler", signal_default_int_handler,
880 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200881#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
882 {"pthread_kill", (PyCFunction)signal_pthread_kill,
883 METH_VARARGS, signal_pthread_kill_doc},
884#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200885#ifdef PYPTHREAD_SIGMASK
886 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
887 METH_VARARGS, signal_pthread_sigmask_doc},
888#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200889#ifdef HAVE_SIGPENDING
890 {"sigpending", (PyCFunction)signal_sigpending,
891 METH_NOARGS, signal_sigpending_doc},
892#endif
893#ifdef HAVE_SIGWAIT
894 {"sigwait", (PyCFunction)signal_sigwait,
895 METH_VARARGS, signal_sigwait_doc},
896#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200897#ifdef HAVE_SIGWAITINFO
898 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
899 METH_VARARGS, signal_sigwaitinfo_doc},
900#endif
901#ifdef HAVE_SIGTIMEDWAIT
902 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
903 METH_VARARGS, signal_sigtimedwait_doc},
904#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000906};
907
Barry Warsaw92971171997-01-03 00:14:25 +0000908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000910"This module provides mechanisms to use signal handlers in Python.\n\
911\n\
912Functions:\n\
913\n\
914alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000915setitimer() -- cause a signal (described below) after a specified\n\
916 float time and the timer may restart then [Unix only]\n\
917getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000918signal() -- set the action for a given signal\n\
919getsignal() -- get the signal action for a given signal\n\
920pause() -- wait until a signal arrives [Unix only]\n\
921default_int_handler() -- default SIGINT handler\n\
922\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000923signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000924SIG_DFL -- used to refer to the system default handler\n\
925SIG_IGN -- used to ignore the signal\n\
926NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000927SIGINT, SIGTERM, etc. -- signal numbers\n\
928\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000929itimer constants:\n\
930ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
931 expiration\n\
932ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
933 and delivers SIGVTALRM upon expiration\n\
934ITIMER_PROF -- decrements both when the process is executing and\n\
935 when the system is executing on behalf of the process.\n\
936 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
937 used to profile the time spent by the application\n\
938 in user and kernel space. SIGPROF is delivered upon\n\
939 expiration.\n\
940\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000941*** IMPORTANT NOTICE ***\n\
942A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000943the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000944
Martin v. Löwis1a214512008-06-11 05:26:20 +0000945static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 PyModuleDef_HEAD_INIT,
947 "signal",
948 module_doc,
949 -1,
950 signal_methods,
951 NULL,
952 NULL,
953 NULL,
954 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000955};
956
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000957PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000958PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyObject *m, *d, *x;
961 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000962
963#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 main_thread = PyThread_get_thread_ident();
965 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000966#endif
967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* Create the module and add the functions */
969 m = PyModule_Create(&signalmodule);
970 if (m == NULL)
971 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000972
Ross Lagerwallbc808222011-06-25 12:13:40 +0200973#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
974 if (!initialized)
975 PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
976
977 Py_INCREF((PyObject*) &SiginfoType);
978 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
979 initialized = 1;
980#endif
981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* Add some symbolic constants to the module */
983 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
986 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
987 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
990 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
991 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 x = PyLong_FromLong((long)NSIG);
994 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
995 goto finally;
996 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000997
Victor Stinnera9293352011-04-30 15:21:58 +0200998#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200999 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1000 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001001#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001002#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001003 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1004 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001005#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001006#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001007 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1008 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001009#endif
1010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1012 if (!x)
1013 goto finally;
1014 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 Handlers[0].tripped = 0;
1017 for (i = 1; i < NSIG; i++) {
1018 void (*t)(int);
1019 t = PyOS_getsig(i);
1020 Handlers[i].tripped = 0;
1021 if (t == SIG_DFL)
1022 Handlers[i].func = DefaultHandler;
1023 else if (t == SIG_IGN)
1024 Handlers[i].func = IgnoreHandler;
1025 else
1026 Handlers[i].func = Py_None; /* None of our business */
1027 Py_INCREF(Handlers[i].func);
1028 }
1029 if (Handlers[SIGINT].func == DefaultHandler) {
1030 /* Install default int handler */
1031 Py_INCREF(IntHandler);
1032 Py_DECREF(Handlers[SIGINT].func);
1033 Handlers[SIGINT].func = IntHandler;
1034 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1035 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001036
1037#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 x = PyLong_FromLong(SIGHUP);
1039 PyDict_SetItemString(d, "SIGHUP", x);
1040 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001041#endif
1042#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 x = PyLong_FromLong(SIGINT);
1044 PyDict_SetItemString(d, "SIGINT", x);
1045 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001046#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001047#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 x = PyLong_FromLong(SIGBREAK);
1049 PyDict_SetItemString(d, "SIGBREAK", x);
1050 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001051#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001052#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 x = PyLong_FromLong(SIGQUIT);
1054 PyDict_SetItemString(d, "SIGQUIT", x);
1055 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001056#endif
1057#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 x = PyLong_FromLong(SIGILL);
1059 PyDict_SetItemString(d, "SIGILL", x);
1060 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001061#endif
1062#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 x = PyLong_FromLong(SIGTRAP);
1064 PyDict_SetItemString(d, "SIGTRAP", x);
1065 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001066#endif
1067#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 x = PyLong_FromLong(SIGIOT);
1069 PyDict_SetItemString(d, "SIGIOT", x);
1070 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001071#endif
1072#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 x = PyLong_FromLong(SIGABRT);
1074 PyDict_SetItemString(d, "SIGABRT", x);
1075 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001076#endif
1077#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 x = PyLong_FromLong(SIGEMT);
1079 PyDict_SetItemString(d, "SIGEMT", x);
1080 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001081#endif
1082#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 x = PyLong_FromLong(SIGFPE);
1084 PyDict_SetItemString(d, "SIGFPE", x);
1085 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001086#endif
1087#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 x = PyLong_FromLong(SIGKILL);
1089 PyDict_SetItemString(d, "SIGKILL", x);
1090 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001091#endif
1092#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 x = PyLong_FromLong(SIGBUS);
1094 PyDict_SetItemString(d, "SIGBUS", x);
1095 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001096#endif
1097#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 x = PyLong_FromLong(SIGSEGV);
1099 PyDict_SetItemString(d, "SIGSEGV", x);
1100 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001101#endif
1102#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 x = PyLong_FromLong(SIGSYS);
1104 PyDict_SetItemString(d, "SIGSYS", x);
1105 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001106#endif
1107#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 x = PyLong_FromLong(SIGPIPE);
1109 PyDict_SetItemString(d, "SIGPIPE", x);
1110 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001111#endif
1112#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 x = PyLong_FromLong(SIGALRM);
1114 PyDict_SetItemString(d, "SIGALRM", x);
1115 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001116#endif
1117#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 x = PyLong_FromLong(SIGTERM);
1119 PyDict_SetItemString(d, "SIGTERM", x);
1120 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001121#endif
1122#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 x = PyLong_FromLong(SIGUSR1);
1124 PyDict_SetItemString(d, "SIGUSR1", x);
1125 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001126#endif
1127#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 x = PyLong_FromLong(SIGUSR2);
1129 PyDict_SetItemString(d, "SIGUSR2", x);
1130 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001131#endif
1132#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 x = PyLong_FromLong(SIGCLD);
1134 PyDict_SetItemString(d, "SIGCLD", x);
1135 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001136#endif
1137#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 x = PyLong_FromLong(SIGCHLD);
1139 PyDict_SetItemString(d, "SIGCHLD", x);
1140 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001141#endif
1142#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 x = PyLong_FromLong(SIGPWR);
1144 PyDict_SetItemString(d, "SIGPWR", x);
1145 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001146#endif
1147#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 x = PyLong_FromLong(SIGIO);
1149 PyDict_SetItemString(d, "SIGIO", x);
1150 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001151#endif
1152#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 x = PyLong_FromLong(SIGURG);
1154 PyDict_SetItemString(d, "SIGURG", x);
1155 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001156#endif
1157#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 x = PyLong_FromLong(SIGWINCH);
1159 PyDict_SetItemString(d, "SIGWINCH", x);
1160 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001161#endif
1162#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 x = PyLong_FromLong(SIGPOLL);
1164 PyDict_SetItemString(d, "SIGPOLL", x);
1165 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001166#endif
1167#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 x = PyLong_FromLong(SIGSTOP);
1169 PyDict_SetItemString(d, "SIGSTOP", x);
1170 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001171#endif
1172#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 x = PyLong_FromLong(SIGTSTP);
1174 PyDict_SetItemString(d, "SIGTSTP", x);
1175 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001176#endif
1177#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 x = PyLong_FromLong(SIGCONT);
1179 PyDict_SetItemString(d, "SIGCONT", x);
1180 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001181#endif
1182#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 x = PyLong_FromLong(SIGTTIN);
1184 PyDict_SetItemString(d, "SIGTTIN", x);
1185 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001186#endif
1187#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 x = PyLong_FromLong(SIGTTOU);
1189 PyDict_SetItemString(d, "SIGTTOU", x);
1190 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001191#endif
1192#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 x = PyLong_FromLong(SIGVTALRM);
1194 PyDict_SetItemString(d, "SIGVTALRM", x);
1195 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001196#endif
1197#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 x = PyLong_FromLong(SIGPROF);
1199 PyDict_SetItemString(d, "SIGPROF", x);
1200 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001201#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001202#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 x = PyLong_FromLong(SIGXCPU);
1204 PyDict_SetItemString(d, "SIGXCPU", x);
1205 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001206#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001207#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 x = PyLong_FromLong(SIGXFSZ);
1209 PyDict_SetItemString(d, "SIGXFSZ", x);
1210 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001211#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001212#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 x = PyLong_FromLong(SIGRTMIN);
1214 PyDict_SetItemString(d, "SIGRTMIN", x);
1215 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001216#endif
1217#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 x = PyLong_FromLong(SIGRTMAX);
1219 PyDict_SetItemString(d, "SIGRTMAX", x);
1220 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001221#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001222#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 x = PyLong_FromLong(SIGINFO);
1224 PyDict_SetItemString(d, "SIGINFO", x);
1225 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001226#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001227
1228#ifdef ITIMER_REAL
1229 x = PyLong_FromLong(ITIMER_REAL);
1230 PyDict_SetItemString(d, "ITIMER_REAL", x);
1231 Py_DECREF(x);
1232#endif
1233#ifdef ITIMER_VIRTUAL
1234 x = PyLong_FromLong(ITIMER_VIRTUAL);
1235 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1236 Py_DECREF(x);
1237#endif
1238#ifdef ITIMER_PROF
1239 x = PyLong_FromLong(ITIMER_PROF);
1240 PyDict_SetItemString(d, "ITIMER_PROF", x);
1241 Py_DECREF(x);
1242#endif
1243
1244#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 ItimerError = PyErr_NewException("signal.ItimerError",
1246 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001247 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001249#endif
1250
Brian Curtineb24d742010-04-12 17:16:38 +00001251#ifdef CTRL_C_EVENT
1252 x = PyLong_FromLong(CTRL_C_EVENT);
1253 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1254 Py_DECREF(x);
1255#endif
1256
1257#ifdef CTRL_BREAK_EVENT
1258 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1259 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1260 Py_DECREF(x);
1261#endif
1262
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001263#ifdef MS_WINDOWS
1264 /* Create manual-reset event, initially unset */
1265 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1266#endif
1267
Martin v. Löwis1a214512008-06-11 05:26:20 +00001268 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 Py_DECREF(m);
1270 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001271 }
Barry Warsaw92971171997-01-03 00:14:25 +00001272
Barry Warsaw92971171997-01-03 00:14:25 +00001273 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001274 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001275}
1276
1277static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001278finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 int i;
1281 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 PyOS_setsig(SIGINT, old_siginthandler);
1284 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 for (i = 1; i < NSIG; i++) {
1287 func = Handlers[i].func;
1288 Handlers[i].tripped = 0;
1289 Handlers[i].func = NULL;
1290 if (i != SIGINT && func != NULL && func != Py_None &&
1291 func != DefaultHandler && func != IgnoreHandler)
1292 PyOS_setsig(i, SIG_DFL);
1293 Py_XDECREF(func);
1294 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 Py_XDECREF(IntHandler);
1297 IntHandler = NULL;
1298 Py_XDECREF(DefaultHandler);
1299 DefaultHandler = NULL;
1300 Py_XDECREF(IgnoreHandler);
1301 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001302}
1303
Barry Warsaw92971171997-01-03 00:14:25 +00001304
Barry Warsaw92971171997-01-03 00:14:25 +00001305/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001306int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001307PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 int i;
1310 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (!is_tripped)
1313 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001314
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001315#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 if (PyThread_get_thread_ident() != main_thread)
1317 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001318#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 /*
1321 * The is_tripped variable is meant to speed up the calls to
1322 * PyErr_CheckSignals (both directly or via pending calls) when no
1323 * signal has arrived. This variable is set to 1 when a signal arrives
1324 * and it is set to 0 here, when we know some signals arrived. This way
1325 * we can run the registered handlers with no signals blocked.
1326 *
1327 * NOTE: with this approach we can have a situation where is_tripped is
1328 * 1 but we have no more signals to handle (Handlers[i].tripped
1329 * is 0 for every signal i). This won't do us any harm (except
1330 * we're gonna spent some cycles for nothing). This happens when
1331 * we receive a signal i after we zero is_tripped and before we
1332 * check Handlers[i].tripped.
1333 */
1334 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (!(f = (PyObject *)PyEval_GetFrame()))
1337 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 for (i = 1; i < NSIG; i++) {
1340 if (Handlers[i].tripped) {
1341 PyObject *result = NULL;
1342 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1343 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (arglist) {
1346 result = PyEval_CallObject(Handlers[i].func,
1347 arglist);
1348 Py_DECREF(arglist);
1349 }
1350 if (!result)
1351 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 Py_DECREF(result);
1354 }
1355 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001358}
1359
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001360
Barry Warsaw92971171997-01-03 00:14:25 +00001361/* Replacements for intrcheck.c functionality
1362 * Declared in pyerrors.h
1363 */
1364void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001365PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001366{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001367 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001368}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369
1370void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001371PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001372{
Brett Cannon0ecd30b2013-02-01 14:04:12 -05001373 PyObject *m = PyImport_ImportModule("signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 Py_DECREF(m);
1376 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001377}
1378
1379void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001380PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383}
1384
1385int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001386PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001389#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (PyThread_get_thread_ident() != main_thread)
1391 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001392#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 Handlers[SIGINT].tripped = 0;
1394 return 1;
1395 }
1396 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001397}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001398
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001399static void
1400_clear_pending_signals(void)
1401{
1402 int i;
1403 if (!is_tripped)
1404 return;
1405 is_tripped = 0;
1406 for (i = 1; i < NSIG; ++i) {
1407 Handlers[i].tripped = 0;
1408 }
1409}
1410
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001411void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001412PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001413{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001414 /* Clear the signal flags after forking so that they aren't handled
1415 * in both processes if they came in just before the fork() but before
1416 * the interpreter had an opportunity to call the handlers. issue9535. */
1417 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001418#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001419 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1420 * can be called safely. */
1421 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001422 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 PyEval_ReInitThreads();
1424 main_thread = PyThread_get_thread_ident();
1425 main_pid = getpid();
1426 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001427#endif
1428}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001429
1430int
1431_PyOS_IsMainThread(void)
1432{
1433#ifdef WITH_THREAD
1434 return PyThread_get_thread_ident() == main_thread;
1435#else
1436 return 1;
1437#endif
1438}
1439
1440#ifdef MS_WINDOWS
1441void *_PyOS_SigintEvent(void)
1442{
1443 /* Returns a manual-reset event which gets tripped whenever
1444 SIGINT is received.
1445
1446 Python.h does not include windows.h so we do cannot use HANDLE
1447 as the return type of this function. We use void* instead. */
1448 return sigint_event;
1449}
1450#endif