blob: 026846b3bb607fab9727e2e3bcd9f6f4fbeb374f [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"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007#include "intrcheck.h"
8
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009#ifdef MS_WINDOWS
Brian Curtineb24d742010-04-12 17:16:38 +000010#include <Windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000011#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#endif
19#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000020#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000022#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000023#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000024#endif
Jean-Paul Calderone867c4352010-06-19 19:54:48 +000025#ifdef HAVE_SIGNALFD
26#include <sys/signalfd.h>
27#endif
28
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000029
Guido van Rossumbb4ba121994-06-23 11:25:45 +000030#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000031#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000032#endif
33
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000034#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000035#define NSIG 12
36#include <process.h>
37#endif
38
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000039#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000040# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000042# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000049#endif
50
51
Guido van Rossumbb4ba121994-06-23 11:25:45 +000052/*
53 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
54
55 When threads are supported, we want the following semantics:
56
57 - only the main thread can set a signal handler
58 - any thread can get a signal handler
59 - signals are only delivered to the main thread
60
61 I.e. we don't support "synchronous signals" like SIGFPE (catching
62 this doesn't make much sense in Python anyway) nor do we support
63 signals as a means of inter-thread communication, since not all
64 thread implementations support that (at least our thread library
65 doesn't).
66
67 We still have the problem that in some implementations signals
68 generated by the keyboard (e.g. SIGINT) are delivered to all
69 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
70 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000071 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000072 a working implementation that works in all three cases -- the
73 handler ignores signals if getpid() isn't the same as in the main
74 thread. XXX This is a hack.
75
Guido van Rossum9e8181b2000-09-19 00:46:46 +000076 GNU pth is a user-space threading library, and as such, all threads
77 run within the same process. In this case, if the currently running
78 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000079*/
80
81#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000082#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000083#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000084static long main_thread;
85static pid_t main_pid;
86#endif
87
Barry Warsaw92971171997-01-03 00:14:25 +000088static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 int tripped;
90 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000091} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000092
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000093static sig_atomic_t wakeup_fd = -1;
94
Christian Heimesb76922a2007-12-11 01:06:40 +000095/* Speed up sigcheck() when none tripped */
96static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Barry Warsaw92971171997-01-03 00:14:25 +000098static PyObject *DefaultHandler;
99static PyObject *IgnoreHandler;
100static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000101
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000102/* On Solaris 8, gcc will produce a warning that the function
103 declaration is not a prototype. This is caused by the definition of
104 SIG_DFL as (void (*)())0; the correct declaration would have been
105 (void (*)(int))0. */
106
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000107static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000108
Martin v. Löwis823725e2008-03-24 13:39:54 +0000109#ifdef HAVE_GETITIMER
110static PyObject *ItimerError;
111
112/* auxiliary functions for setitimer/getitimer */
113static void
114timeval_from_double(double d, struct timeval *tv)
115{
116 tv->tv_sec = floor(d);
117 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
118}
119
Christian Heimes1a8501c2008-10-02 19:56:01 +0000120Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000121double_from_timeval(struct timeval *tv)
122{
123 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
124}
125
126static PyObject *
127itimer_retval(struct itimerval *iv)
128{
129 PyObject *r, *v;
130
131 r = PyTuple_New(2);
132 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000134
135 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 Py_DECREF(r);
137 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000138 }
139
140 PyTuple_SET_ITEM(r, 0, v);
141
142 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 Py_DECREF(r);
144 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145 }
146
147 PyTuple_SET_ITEM(r, 1, v);
148
149 return r;
150}
151#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000152
Guido van Rossume4485b01994-09-07 14:32:49 +0000153static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000154signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 PyErr_SetNone(PyExc_KeyboardInterrupt);
157 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000158}
159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000161"default_int_handler(...)\n\
162\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000163The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000165
Thomas Wouters0796b002000-07-22 23:49:30 +0000166
167static int
168checksignals_witharg(void * unused)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000171}
172
Tim Peters4f1b2082000-07-23 21:18:09 +0000173static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000174signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000175{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000176#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000177#ifdef WITH_PTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (PyThread_get_thread_ident() != main_thread) {
179 pth_raise(*(pth_t *) main_thread, sig_num);
180 return;
181 }
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000182#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 /* See NOTES section above */
184 if (getpid() == main_pid) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000185#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 Handlers[sig_num].tripped = 1;
187 /* Set is_tripped after setting .tripped, as it gets
188 cleared in PyErr_CheckSignals() before .tripped. */
189 is_tripped = 1;
190 Py_AddPendingCall(checksignals_witharg, NULL);
191 if (wakeup_fd != -1)
192 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000193#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000195#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000196#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (sig_num == SIGCHLD) {
198 /* To avoid infinite recursion, this signal remains
199 reset until explicit re-instated.
200 Don't clear the 'func' field as it is our pointer
201 to the Python handler... */
202 return;
203 }
Guido van Rossum602099a1994-09-14 13:32:22 +0000204#endif
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000205#ifndef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 /* If the handler was not set up with sigaction, reinstall it. See
207 * Python/pythonrun.c for the implementation of PyOS_setsig which
208 * makes this true. See also issue8354. */
209 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000210#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000211}
Guido van Rossume4485b01994-09-07 14:32:49 +0000212
Guido van Rossum06d511d1995-03-10 15:13:48 +0000213
Guido van Rossum1171ee61997-08-22 20:42:00 +0000214#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000215static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000216signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 int t;
219 if (!PyArg_ParseTuple(args, "i:alarm", &t))
220 return NULL;
221 /* alarm() returns the number of seconds remaining */
222 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000223}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000225PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000226"alarm(seconds)\n\
227\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000229#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
Guido van Rossum1171ee61997-08-22 20:42:00 +0000231#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000232static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000233signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 Py_BEGIN_ALLOW_THREADS
236 (void)pause();
237 Py_END_ALLOW_THREADS
238 /* make sure that any exceptions that got raised are propagated
239 * back into Python
240 */
241 if (PyErr_CheckSignals())
242 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_INCREF(Py_None);
245 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000246}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000248"pause()\n\
249\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000250Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000251
Guido van Rossum06d511d1995-03-10 15:13:48 +0000252#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000253
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000254
Guido van Rossume4485b01994-09-07 14:32:49 +0000255static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000256signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyObject *obj;
259 int sig_num;
260 PyObject *old_handler;
261 void (*func)(int);
262 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
263 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000264#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (PyThread_get_thread_ident() != main_thread) {
266 PyErr_SetString(PyExc_ValueError,
267 "signal only works in main thread");
268 return NULL;
269 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000270#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (sig_num < 1 || sig_num >= NSIG) {
272 PyErr_SetString(PyExc_ValueError,
273 "signal number out of range");
274 return NULL;
275 }
276 if (obj == IgnoreHandler)
277 func = SIG_IGN;
278 else if (obj == DefaultHandler)
279 func = SIG_DFL;
280 else if (!PyCallable_Check(obj)) {
281 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000282"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 return NULL;
284 }
285 else
286 func = signal_handler;
287 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
288 PyErr_SetFromErrno(PyExc_RuntimeError);
289 return NULL;
290 }
291 old_handler = Handlers[sig_num].func;
292 Handlers[sig_num].tripped = 0;
293 Py_INCREF(obj);
294 Handlers[sig_num].func = obj;
295 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000296}
297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000298PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000299"signal(sig, action) -> action\n\
300\n\
301Set the action for the given signal. The action can be SIG_DFL,\n\
302SIG_IGN, or a callable Python object. The previous action is\n\
303returned. See getsignal() for possible return values.\n\
304\n\
305*** IMPORTANT NOTICE ***\n\
306A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000307the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000308
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000309
Guido van Rossume4485b01994-09-07 14:32:49 +0000310static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000311signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 int sig_num;
314 PyObject *old_handler;
315 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
316 return NULL;
317 if (sig_num < 1 || sig_num >= NSIG) {
318 PyErr_SetString(PyExc_ValueError,
319 "signal number out of range");
320 return NULL;
321 }
322 old_handler = Handlers[sig_num].func;
323 Py_INCREF(old_handler);
324 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325}
326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000328"getsignal(sig) -> action\n\
329\n\
330Return the current action for the given signal. The return value can be:\n\
331SIG_IGN -- if the signal is being ignored\n\
332SIG_DFL -- if the default action for the signal is in effect\n\
333None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335
Christian Heimes8640e742008-02-23 16:23:06 +0000336#ifdef HAVE_SIGINTERRUPT
337PyDoc_STRVAR(siginterrupt_doc,
338"siginterrupt(sig, flag) -> None\n\
339change system call restart behaviour: if flag is False, system calls\n\
340will be restarted when interrupted by signal sig, else system calls\n\
341will be interrupted.");
342
343static PyObject *
344signal_siginterrupt(PyObject *self, PyObject *args)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 int sig_num;
347 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
350 return NULL;
351 if (sig_num < 1 || sig_num >= NSIG) {
352 PyErr_SetString(PyExc_ValueError,
353 "signal number out of range");
354 return NULL;
355 }
356 if (siginterrupt(sig_num, flag)<0) {
357 PyErr_SetFromErrno(PyExc_RuntimeError);
358 return NULL;
359 }
Christian Heimes8640e742008-02-23 16:23:06 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_INCREF(Py_None);
362 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000363}
364
365#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000366
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000367static PyObject *
368signal_set_wakeup_fd(PyObject *self, PyObject *args)
369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 struct stat buf;
371 int fd, old_fd;
372 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
373 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000374#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 if (PyThread_get_thread_ident() != main_thread) {
376 PyErr_SetString(PyExc_ValueError,
377 "set_wakeup_fd only works in main thread");
378 return NULL;
379 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000380#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (fd != -1 && fstat(fd, &buf) != 0) {
382 PyErr_SetString(PyExc_ValueError, "invalid fd");
383 return NULL;
384 }
385 old_fd = wakeup_fd;
386 wakeup_fd = fd;
387 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000388}
389
390PyDoc_STRVAR(set_wakeup_fd_doc,
391"set_wakeup_fd(fd) -> fd\n\
392\n\
393Sets the fd to be written to (with '\\0') when a signal\n\
394comes in. A library can use this to wakeup select or poll.\n\
395The previous fd is returned.\n\
396\n\
397The fd must be non-blocking.");
398
399/* C API for the same, without all the error checking */
400int
401PySignal_SetWakeupFd(int fd)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 int old_fd = wakeup_fd;
404 if (fd < 0)
405 fd = -1;
406 wakeup_fd = fd;
407 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000408}
409
410
Martin v. Löwis823725e2008-03-24 13:39:54 +0000411#ifdef HAVE_SETITIMER
412static PyObject *
413signal_setitimer(PyObject *self, PyObject *args)
414{
415 double first;
416 double interval = 0;
417 int which;
418 struct itimerval new, old;
419
420 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000422
423 timeval_from_double(first, &new.it_value);
424 timeval_from_double(interval, &new.it_interval);
425 /* Let OS check "which" value */
426 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 PyErr_SetFromErrno(ItimerError);
428 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000429 }
430
431 return itimer_retval(&old);
432}
433
434PyDoc_STRVAR(setitimer_doc,
435"setitimer(which, seconds[, interval])\n\
436\n\
437Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
438or ITIMER_PROF) to fire after value seconds and after\n\
439that every interval seconds.\n\
440The itimer can be cleared by setting seconds to zero.\n\
441\n\
442Returns old values as a tuple: (delay, interval).");
443#endif
444
445
446#ifdef HAVE_GETITIMER
447static PyObject *
448signal_getitimer(PyObject *self, PyObject *args)
449{
450 int which;
451 struct itimerval old;
452
453 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000455
456 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyErr_SetFromErrno(ItimerError);
458 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000459 }
460
461 return itimer_retval(&old);
462}
463
464PyDoc_STRVAR(getitimer_doc,
465"getitimer(which)\n\
466\n\
467Returns current value of given itimer.");
468#endif
469
470
Jean-Paul Calderone867c4352010-06-19 19:54:48 +0000471static int
472_iterable_to_mask(PyObject *iterable, sigset_t *mask)
473{
474 static const char* range_format = "signal number %d out of range";
475 char range_buffer[1024];
476 int result = 0;
477
478 PyObject *item, *iterator = NULL;
479
480 sigemptyset(mask);
481
482 iterator = PyObject_GetIter(iterable);
483 if (iterator == NULL) {
484 result = -1;
485 goto error;
486 }
487
488 while ((item = PyIter_Next(iterator))) {
489 int signum = PyInt_AsLong(item);
490 Py_DECREF(item);
491 if (signum == -1 && PyErr_Occurred()) {
492 result = -1;
493 goto error;
494 }
495 if (sigaddset(mask, signum) == -1) {
496 PyOS_snprintf(range_buffer, sizeof(range_buffer), range_format, signum);
497 PyErr_SetString(PyExc_ValueError, range_buffer);
498 result = -1;
499 goto error;
500 }
501 }
502
503error:
504 Py_XDECREF(iterator);
505 return result;
506}
507
508#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
509# define PY_SIGMASK pthread_sigmask
510#elif defined(HAVE_SIGPROCMASK)
511# define PY_SIGMASK sigprocmask
512#endif
513
514#ifdef PY_SIGMASK
515static PyObject *
516signal_sigprocmask(PyObject *self, PyObject *args)
517{
518 static const char* how_format = "value specified for how (%d) invalid";
519 char how_buffer[1024];
520
521 int how, sig;
522 PyObject *signals, *result, *signum;
523 sigset_t mask, previous;
524
525 if (!PyArg_ParseTuple(args, "iO:sigprocmask", &how, &signals)) {
526 return NULL;
527 }
528
529 if (_iterable_to_mask(signals, &mask) == -1) {
530 return NULL;
531 }
532
533 if (PY_SIGMASK(how, &mask, &previous) != 0) {
534 PyOS_snprintf(how_buffer, sizeof(how_buffer), how_format, how);
535 PyErr_SetString(PyExc_ValueError, how_buffer);
536 return NULL;
537 }
538
539 result = PyList_New(0);
540 if (result == NULL) {
541 return NULL;
542 }
543
544 for (sig = 1; sig < NSIG; ++sig) {
545 if (sigismember(&previous, sig) == 1) {
546 /* Handle the case where it is a member by adding the signal to
547 the result list. Ignore the other cases because they mean the
548 signal isn't a member of the mask or the signal was invalid,
549 and an invalid signal must have been our fault in constructing
550 the loop boundaries. */
551 signum = PyInt_FromLong(sig);
552 if (signum == NULL) {
553 Py_DECREF(result);
554 return NULL;
555 }
556 if (PyList_Append(result, signum) == -1) {
557 Py_DECREF(signum);
558 Py_DECREF(result);
559 return NULL;
560 }
561 Py_DECREF(signum);
562 }
563 }
564 return result;
565}
566
567PyDoc_STRVAR(sigprocmask_doc,
568"sigprocmask(how, mask) -> old mask\n\
569\n\
570Examine and change blocked signals.");
571#endif
572
573#ifdef HAVE_SIGNALFD
574static PyObject *
575signal_signalfd(PyObject *self, PyObject *args)
576{
577 int result, flags = 0;
578 sigset_t mask;
579
580 int fd;
581 PyObject *signals;
582
583 if (!PyArg_ParseTuple(args, "iO|i:signalfd", &fd, &signals, &flags)) {
584 return NULL;
585 }
586
587 if (_iterable_to_mask(signals, &mask) == -1) {
588 return NULL;
589 }
590
591 result = signalfd(-1, &mask, flags);
592
593 if (result == -1) {
594 PyErr_SetFromErrno(PyExc_OSError);
595 return NULL;
596 }
597
598 return PyInt_FromLong(result);
599}
600
601PyDoc_STRVAR(signalfd_doc,
602"signalfd(fd, mask, flags)\n\
603\n\
604Create a file descriptor for accepting signals.");
605
606#endif
607
608
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000609/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000610static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000611#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000613#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000614#ifdef HAVE_SETITIMER
615 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
616#endif
617#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000619#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 {"signal", signal_signal, METH_VARARGS, signal_doc},
621 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
622 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Jean-Paul Calderone867c4352010-06-19 19:54:48 +0000623#ifdef PY_SIGMASK
624 {"sigprocmask", signal_sigprocmask, METH_VARARGS, sigprocmask_doc},
625/* It's no longer needed, so clean up the namespace. */
626#undef PY_SIGMASK
627#endif
628#ifdef HAVE_SIGNALFD
629 {"signalfd", signal_signalfd, METH_VARARGS, signalfd_doc},
630#endif
Christian Heimes8640e742008-02-23 16:23:06 +0000631#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000633#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000634#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 {"pause", (PyCFunction)signal_pause,
636 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000637#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 {"default_int_handler", signal_default_int_handler,
639 METH_VARARGS, default_int_handler_doc},
640 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000641};
642
Barry Warsaw92971171997-01-03 00:14:25 +0000643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000644PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000645"This module provides mechanisms to use signal handlers in Python.\n\
646\n\
647Functions:\n\
648\n\
649alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000650setitimer() -- cause a signal (described below) after a specified\n\
651 float time and the timer may restart then [Unix only]\n\
652getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000653signal() -- set the action for a given signal\n\
654getsignal() -- get the signal action for a given signal\n\
655pause() -- wait until a signal arrives [Unix only]\n\
656default_int_handler() -- default SIGINT handler\n\
657\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000658signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000659SIG_DFL -- used to refer to the system default handler\n\
660SIG_IGN -- used to ignore the signal\n\
661NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000662SIGINT, SIGTERM, etc. -- signal numbers\n\
663\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000664itimer constants:\n\
665ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
666 expiration\n\
667ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
668 and delivers SIGVTALRM upon expiration\n\
669ITIMER_PROF -- decrements both when the process is executing and\n\
670 when the system is executing on behalf of the process.\n\
671 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
672 used to profile the time spent by the application\n\
673 in user and kernel space. SIGPROF is delivered upon\n\
674 expiration.\n\
675\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000676*** IMPORTANT NOTICE ***\n\
677A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000678the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000679
Martin v. Löwis1a214512008-06-11 05:26:20 +0000680static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 PyModuleDef_HEAD_INIT,
682 "signal",
683 module_doc,
684 -1,
685 signal_methods,
686 NULL,
687 NULL,
688 NULL,
689 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000690};
691
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000692PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000693PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyObject *m, *d, *x;
696 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000697
698#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 main_thread = PyThread_get_thread_ident();
700 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000701#endif
702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 /* Create the module and add the functions */
704 m = PyModule_Create(&signalmodule);
705 if (m == NULL)
706 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 /* Add some symbolic constants to the module */
709 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
712 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
713 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
716 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
717 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 x = PyLong_FromLong((long)NSIG);
720 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
721 goto finally;
722 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
725 if (!x)
726 goto finally;
727 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 Handlers[0].tripped = 0;
730 for (i = 1; i < NSIG; i++) {
731 void (*t)(int);
732 t = PyOS_getsig(i);
733 Handlers[i].tripped = 0;
734 if (t == SIG_DFL)
735 Handlers[i].func = DefaultHandler;
736 else if (t == SIG_IGN)
737 Handlers[i].func = IgnoreHandler;
738 else
739 Handlers[i].func = Py_None; /* None of our business */
740 Py_INCREF(Handlers[i].func);
741 }
742 if (Handlers[SIGINT].func == DefaultHandler) {
743 /* Install default int handler */
744 Py_INCREF(IntHandler);
745 Py_DECREF(Handlers[SIGINT].func);
746 Handlers[SIGINT].func = IntHandler;
747 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
748 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000749
750#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 x = PyLong_FromLong(SIGHUP);
752 PyDict_SetItemString(d, "SIGHUP", x);
753 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000754#endif
755#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 x = PyLong_FromLong(SIGINT);
757 PyDict_SetItemString(d, "SIGINT", x);
758 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000759#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000760#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 x = PyLong_FromLong(SIGBREAK);
762 PyDict_SetItemString(d, "SIGBREAK", x);
763 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000764#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000765#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 x = PyLong_FromLong(SIGQUIT);
767 PyDict_SetItemString(d, "SIGQUIT", x);
768 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000769#endif
770#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 x = PyLong_FromLong(SIGILL);
772 PyDict_SetItemString(d, "SIGILL", x);
773 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000774#endif
775#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 x = PyLong_FromLong(SIGTRAP);
777 PyDict_SetItemString(d, "SIGTRAP", x);
778 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000779#endif
780#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 x = PyLong_FromLong(SIGIOT);
782 PyDict_SetItemString(d, "SIGIOT", x);
783 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000784#endif
785#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 x = PyLong_FromLong(SIGABRT);
787 PyDict_SetItemString(d, "SIGABRT", x);
788 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000789#endif
790#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 x = PyLong_FromLong(SIGEMT);
792 PyDict_SetItemString(d, "SIGEMT", x);
793 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000794#endif
795#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 x = PyLong_FromLong(SIGFPE);
797 PyDict_SetItemString(d, "SIGFPE", x);
798 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000799#endif
800#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 x = PyLong_FromLong(SIGKILL);
802 PyDict_SetItemString(d, "SIGKILL", x);
803 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000804#endif
805#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 x = PyLong_FromLong(SIGBUS);
807 PyDict_SetItemString(d, "SIGBUS", x);
808 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000809#endif
810#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 x = PyLong_FromLong(SIGSEGV);
812 PyDict_SetItemString(d, "SIGSEGV", x);
813 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000814#endif
815#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 x = PyLong_FromLong(SIGSYS);
817 PyDict_SetItemString(d, "SIGSYS", x);
818 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000819#endif
820#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 x = PyLong_FromLong(SIGPIPE);
822 PyDict_SetItemString(d, "SIGPIPE", x);
823 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000824#endif
825#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 x = PyLong_FromLong(SIGALRM);
827 PyDict_SetItemString(d, "SIGALRM", x);
828 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000829#endif
830#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 x = PyLong_FromLong(SIGTERM);
832 PyDict_SetItemString(d, "SIGTERM", x);
833 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000834#endif
835#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 x = PyLong_FromLong(SIGUSR1);
837 PyDict_SetItemString(d, "SIGUSR1", x);
838 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000839#endif
840#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 x = PyLong_FromLong(SIGUSR2);
842 PyDict_SetItemString(d, "SIGUSR2", x);
843 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000844#endif
845#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 x = PyLong_FromLong(SIGCLD);
847 PyDict_SetItemString(d, "SIGCLD", x);
848 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000849#endif
850#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 x = PyLong_FromLong(SIGCHLD);
852 PyDict_SetItemString(d, "SIGCHLD", x);
853 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000854#endif
855#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 x = PyLong_FromLong(SIGPWR);
857 PyDict_SetItemString(d, "SIGPWR", x);
858 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000859#endif
860#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 x = PyLong_FromLong(SIGIO);
862 PyDict_SetItemString(d, "SIGIO", x);
863 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000864#endif
865#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 x = PyLong_FromLong(SIGURG);
867 PyDict_SetItemString(d, "SIGURG", x);
868 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000869#endif
870#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 x = PyLong_FromLong(SIGWINCH);
872 PyDict_SetItemString(d, "SIGWINCH", x);
873 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000874#endif
875#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 x = PyLong_FromLong(SIGPOLL);
877 PyDict_SetItemString(d, "SIGPOLL", x);
878 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000879#endif
880#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 x = PyLong_FromLong(SIGSTOP);
882 PyDict_SetItemString(d, "SIGSTOP", x);
883 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000884#endif
885#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 x = PyLong_FromLong(SIGTSTP);
887 PyDict_SetItemString(d, "SIGTSTP", x);
888 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000889#endif
890#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 x = PyLong_FromLong(SIGCONT);
892 PyDict_SetItemString(d, "SIGCONT", x);
893 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000894#endif
895#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 x = PyLong_FromLong(SIGTTIN);
897 PyDict_SetItemString(d, "SIGTTIN", x);
898 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000899#endif
900#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 x = PyLong_FromLong(SIGTTOU);
902 PyDict_SetItemString(d, "SIGTTOU", x);
903 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000904#endif
905#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 x = PyLong_FromLong(SIGVTALRM);
907 PyDict_SetItemString(d, "SIGVTALRM", x);
908 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000909#endif
910#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 x = PyLong_FromLong(SIGPROF);
912 PyDict_SetItemString(d, "SIGPROF", x);
913 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000914#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000915#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 x = PyLong_FromLong(SIGXCPU);
917 PyDict_SetItemString(d, "SIGXCPU", x);
918 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000919#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000920#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 x = PyLong_FromLong(SIGXFSZ);
922 PyDict_SetItemString(d, "SIGXFSZ", x);
923 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000924#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000925#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 x = PyLong_FromLong(SIGRTMIN);
927 PyDict_SetItemString(d, "SIGRTMIN", x);
928 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000929#endif
930#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 x = PyLong_FromLong(SIGRTMAX);
932 PyDict_SetItemString(d, "SIGRTMAX", x);
933 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000934#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000935#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 x = PyLong_FromLong(SIGINFO);
937 PyDict_SetItemString(d, "SIGINFO", x);
938 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000939#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000940
941#ifdef ITIMER_REAL
942 x = PyLong_FromLong(ITIMER_REAL);
943 PyDict_SetItemString(d, "ITIMER_REAL", x);
944 Py_DECREF(x);
945#endif
946#ifdef ITIMER_VIRTUAL
947 x = PyLong_FromLong(ITIMER_VIRTUAL);
948 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
949 Py_DECREF(x);
950#endif
951#ifdef ITIMER_PROF
952 x = PyLong_FromLong(ITIMER_PROF);
953 PyDict_SetItemString(d, "ITIMER_PROF", x);
954 Py_DECREF(x);
955#endif
956
957#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 ItimerError = PyErr_NewException("signal.ItimerError",
959 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000960 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000962#endif
963
Jean-Paul Calderone867c4352010-06-19 19:54:48 +0000964#ifdef SIG_BLOCK
965 x = PyLong_FromLong(SIG_BLOCK);
966 PyDict_SetItemString(d, "SIG_BLOCK", x);
967 Py_DECREF(x);
968#endif
969
970#ifdef SIG_UNBLOCK
971 x = PyLong_FromLong(SIG_UNBLOCK);
972 PyDict_SetItemString(d, "SIG_UNBLOCK", x);
973 Py_DECREF(x);
974#endif
975
976#ifdef SIG_SETMASK
977 x = PyLong_FromLong(SIG_SETMASK);
978 PyDict_SetItemString(d, "SIG_SETMASK", x);
979 Py_DECREF(x);
980#endif
981
982#ifdef SFD_CLOEXEC
983 x = PyLong_FromLong(SFD_CLOEXEC);
984 PyDict_SetItemString(d, "SFD_CLOEXEC", x);
985 Py_DECREF(x);
986#endif
987
988#ifdef SFD_NONBLOCK
989 x = PyLong_FromLong(SFD_NONBLOCK);
990 PyDict_SetItemString(d, "SFD_NONBLOCK", x);
991 Py_DECREF(x);
992#endif
993
Brian Curtineb24d742010-04-12 17:16:38 +0000994#ifdef CTRL_C_EVENT
995 x = PyLong_FromLong(CTRL_C_EVENT);
996 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
997 Py_DECREF(x);
998#endif
999
1000#ifdef CTRL_BREAK_EVENT
1001 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1002 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1003 Py_DECREF(x);
1004#endif
1005
Martin v. Löwis1a214512008-06-11 05:26:20 +00001006 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 Py_DECREF(m);
1008 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001009 }
Barry Warsaw92971171997-01-03 00:14:25 +00001010
Barry Warsaw92971171997-01-03 00:14:25 +00001011 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001012 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001013}
1014
1015static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001016finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 int i;
1019 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 PyOS_setsig(SIGINT, old_siginthandler);
1022 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 for (i = 1; i < NSIG; i++) {
1025 func = Handlers[i].func;
1026 Handlers[i].tripped = 0;
1027 Handlers[i].func = NULL;
1028 if (i != SIGINT && func != NULL && func != Py_None &&
1029 func != DefaultHandler && func != IgnoreHandler)
1030 PyOS_setsig(i, SIG_DFL);
1031 Py_XDECREF(func);
1032 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 Py_XDECREF(IntHandler);
1035 IntHandler = NULL;
1036 Py_XDECREF(DefaultHandler);
1037 DefaultHandler = NULL;
1038 Py_XDECREF(IgnoreHandler);
1039 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001040}
1041
Barry Warsaw92971171997-01-03 00:14:25 +00001042
Barry Warsaw92971171997-01-03 00:14:25 +00001043/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001044int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001045PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 int i;
1048 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (!is_tripped)
1051 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001052
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001053#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (PyThread_get_thread_ident() != main_thread)
1055 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001056#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /*
1059 * The is_tripped variable is meant to speed up the calls to
1060 * PyErr_CheckSignals (both directly or via pending calls) when no
1061 * signal has arrived. This variable is set to 1 when a signal arrives
1062 * and it is set to 0 here, when we know some signals arrived. This way
1063 * we can run the registered handlers with no signals blocked.
1064 *
1065 * NOTE: with this approach we can have a situation where is_tripped is
1066 * 1 but we have no more signals to handle (Handlers[i].tripped
1067 * is 0 for every signal i). This won't do us any harm (except
1068 * we're gonna spent some cycles for nothing). This happens when
1069 * we receive a signal i after we zero is_tripped and before we
1070 * check Handlers[i].tripped.
1071 */
1072 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!(f = (PyObject *)PyEval_GetFrame()))
1075 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 for (i = 1; i < NSIG; i++) {
1078 if (Handlers[i].tripped) {
1079 PyObject *result = NULL;
1080 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1081 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (arglist) {
1084 result = PyEval_CallObject(Handlers[i].func,
1085 arglist);
1086 Py_DECREF(arglist);
1087 }
1088 if (!result)
1089 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 Py_DECREF(result);
1092 }
1093 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001096}
1097
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001098
Barry Warsaw92971171997-01-03 00:14:25 +00001099/* Replacements for intrcheck.c functionality
1100 * Declared in pyerrors.h
1101 */
1102void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001103PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 is_tripped = 1;
1106 Handlers[SIGINT].tripped = 1;
1107 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +00001108}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001109
1110void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001111PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 PyObject *m = PyInit_signal();
1114 if (m) {
1115 _PyImport_FixupExtension(m, "signal", "signal");
1116 Py_DECREF(m);
1117 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001118}
1119
1120void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001121PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001124}
1125
1126int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001127PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001130#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (PyThread_get_thread_ident() != main_thread)
1132 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001133#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 Handlers[SIGINT].tripped = 0;
1135 return 1;
1136 }
1137 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001138}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001139
1140void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001141PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001142{
1143#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PyEval_ReInitThreads();
1145 main_thread = PyThread_get_thread_ident();
1146 main_pid = getpid();
1147 _PyImport_ReInitLock();
1148 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001149#endif
1150}