blob: 598bc8a34290819004ab341ac4b421dd7fd031a1 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007#ifndef MS_WINDOWS
8#include "posixmodule.h"
9#endif
Victor Stinner11517102014-07-29 23:31:34 +020010#ifdef MS_WINDOWS
11#include "socketmodule.h" /* needed for SOCKET_T */
12#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000014#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020015#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000017#include <process.h>
18#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000019#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000020
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000022#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
24#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000025#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000027#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000028#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000030
Victor Stinnera9293352011-04-30 15:21:58 +020031#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
32# define PYPTHREAD_SIGMASK
33#endif
34
35#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
36# include <pthread.h>
37#endif
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000040#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#endif
42
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000043#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#endif
54
55
Guido van Rossumbb4ba121994-06-23 11:25:45 +000056/*
57 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
58
59 When threads are supported, we want the following semantics:
60
61 - only the main thread can set a signal handler
62 - any thread can get a signal handler
63 - signals are only delivered to the main thread
64
65 I.e. we don't support "synchronous signals" like SIGFPE (catching
66 this doesn't make much sense in Python anyway) nor do we support
67 signals as a means of inter-thread communication, since not all
68 thread implementations support that (at least our thread library
69 doesn't).
70
71 We still have the problem that in some implementations signals
72 generated by the keyboard (e.g. SIGINT) are delivered to all
73 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
74 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000075 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000076 a working implementation that works in all three cases -- the
77 handler ignores signals if getpid() isn't the same as in the main
78 thread. XXX This is a hack.
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
Victor Stinner2ec6b172011-05-15 10:21:59 +020088static volatile struct {
89 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000091} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000092
Victor Stinner11517102014-07-29 23:31:34 +020093#ifdef MS_WINDOWS
94#define INVALID_FD ((SOCKET_T)-1)
95
96static volatile struct {
97 SOCKET_T fd;
98 int use_send;
99 int send_err_set;
100 int send_errno;
101 int send_win_error;
102} wakeup = {INVALID_FD, 0, 0};
103#else
104#define INVALID_FD (-1)
Victor Stinner2ec6b172011-05-15 10:21:59 +0200105static volatile sig_atomic_t wakeup_fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200106#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000107
Christian Heimesb76922a2007-12-11 01:06:40 +0000108/* Speed up sigcheck() when none tripped */
109static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000110
Barry Warsaw92971171997-01-03 00:14:25 +0000111static PyObject *DefaultHandler;
112static PyObject *IgnoreHandler;
113static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000114
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000115/* On Solaris 8, gcc will produce a warning that the function
116 declaration is not a prototype. This is caused by the definition of
117 SIG_DFL as (void (*)())0; the correct declaration would have been
118 (void (*)(int))0. */
119
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000120static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000121
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100122#ifdef MS_WINDOWS
123static HANDLE sigint_event = NULL;
124#endif
125
Martin v. Löwis823725e2008-03-24 13:39:54 +0000126#ifdef HAVE_GETITIMER
127static PyObject *ItimerError;
128
129/* auxiliary functions for setitimer/getitimer */
130static void
131timeval_from_double(double d, struct timeval *tv)
132{
133 tv->tv_sec = floor(d);
134 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
135}
136
Christian Heimes1a8501c2008-10-02 19:56:01 +0000137Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000138double_from_timeval(struct timeval *tv)
139{
140 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
141}
142
143static PyObject *
144itimer_retval(struct itimerval *iv)
145{
146 PyObject *r, *v;
147
148 r = PyTuple_New(2);
149 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000151
152 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
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, 0, v);
158
159 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 Py_DECREF(r);
161 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000162 }
163
164 PyTuple_SET_ITEM(r, 1, v);
165
166 return r;
167}
168#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000169
Guido van Rossume4485b01994-09-07 14:32:49 +0000170static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000171signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 PyErr_SetNone(PyExc_KeyboardInterrupt);
174 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000175}
176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000177PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000178"default_int_handler(...)\n\
179\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000180The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000181It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000182
Thomas Wouters0796b002000-07-22 23:49:30 +0000183
184static int
185checksignals_witharg(void * unused)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000188}
189
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200190static int
Victor Stinner11517102014-07-29 23:31:34 +0200191report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200192{
193 int save_errno = errno;
194 errno = (int) (Py_intptr_t) data;
195 PyErr_SetFromErrno(PyExc_OSError);
196 PySys_WriteStderr("Exception ignored when trying to write to the "
197 "signal wakeup fd:\n");
198 PyErr_WriteUnraisable(NULL);
199 errno = save_errno;
200 return 0;
201}
202
Victor Stinner11517102014-07-29 23:31:34 +0200203#ifdef MS_WINDOWS
204static int
205report_wakeup_send_error(void* Py_UNUSED(data))
206{
207 PyObject *res;
208
209 if (wakeup.send_win_error) {
210 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
211 recognizes the error codes used by both GetLastError() and
212 WSAGetLastError */
213 res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
214 }
215 else {
216 errno = wakeup.send_errno;
217 res = PyErr_SetFromErrno(PyExc_OSError);
218 }
219
220 assert(res == NULL);
221 wakeup.send_err_set = 0;
222
223 PySys_WriteStderr("Exception ignored when trying to send to the "
224 "signal wakeup fd:\n");
225 PyErr_WriteUnraisable(NULL);
226
227 return 0;
228}
229#endif /* MS_WINDOWS */
230
Tim Peters4f1b2082000-07-23 21:18:09 +0000231static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200232trip_signal(int sig_num)
233{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200234 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200235 int fd;
236 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200237
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200238 Handlers[sig_num].tripped = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200239
240#ifdef MS_WINDOWS
241 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
242#else
243 fd = wakeup_fd;
244#endif
245
246 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200247 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200248#ifdef MS_WINDOWS
249 if (wakeup.use_send) {
250 do {
251 rc = send(fd, &byte, 1, 0);
252 } while (rc < 0 && errno == EINTR);
253
254 /* we only have a storage for one error in the wakeup structure */
255 if (rc < 0 && !wakeup.send_err_set) {
256 wakeup.send_err_set = 1;
257 wakeup.send_errno = errno;
258 wakeup.send_win_error = GetLastError();
259 Py_AddPendingCall(report_wakeup_send_error, NULL);
260 }
261 }
262 else
263#endif
264 {
265 byte = (unsigned char)sig_num;
266 do {
267 rc = write(fd, &byte, 1);
268 } while (rc < 0 && errno == EINTR);
269
270 if (rc < 0) {
271 Py_AddPendingCall(report_wakeup_write_error,
272 (void *)(Py_intptr_t)errno);
273 }
274 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200275 }
Victor Stinner11517102014-07-29 23:31:34 +0200276
277 if (!is_tripped) {
278 /* Set is_tripped after setting .tripped, as it gets
279 cleared in PyErr_CheckSignals() before .tripped. */
280 is_tripped = 1;
281 Py_AddPendingCall(checksignals_witharg, NULL);
282 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200283}
284
285static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000286signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000287{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000288 int save_errno = errno;
289
Antoine Pitrou39a65912010-11-05 19:47:27 +0000290#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000292 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000293#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000294 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200295 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000297
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000298#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000299#ifdef SIGCHLD
300 /* To avoid infinite recursion, this signal remains
301 reset until explicit re-instated.
302 Don't clear the 'func' field as it is our pointer
303 to the Python handler... */
304 if (sig_num != SIGCHLD)
305#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000307 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 * makes this true. See also issue8354. */
309 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000310#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000311
312 /* Issue #10311: asynchronously executing signal handlers should not
313 mutate errno under the feet of unsuspecting C code. */
314 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100315
316#ifdef MS_WINDOWS
317 if (sig_num == SIGINT)
318 SetEvent(sigint_event);
319#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000320}
Guido van Rossume4485b01994-09-07 14:32:49 +0000321
Guido van Rossum06d511d1995-03-10 15:13:48 +0000322
Guido van Rossum1171ee61997-08-22 20:42:00 +0000323#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000324static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000325signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 int t;
328 if (!PyArg_ParseTuple(args, "i:alarm", &t))
329 return NULL;
330 /* alarm() returns the number of seconds remaining */
331 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000332}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000335"alarm(seconds)\n\
336\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000338#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339
Guido van Rossum1171ee61997-08-22 20:42:00 +0000340#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000341static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000342signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_BEGIN_ALLOW_THREADS
345 (void)pause();
346 Py_END_ALLOW_THREADS
347 /* make sure that any exceptions that got raised are propagated
348 * back into Python
349 */
350 if (PyErr_CheckSignals())
351 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_INCREF(Py_None);
354 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000355}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000356PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000357"pause()\n\
358\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000360
Guido van Rossum06d511d1995-03-10 15:13:48 +0000361#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000362
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000363
Guido van Rossume4485b01994-09-07 14:32:49 +0000364static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000365signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 PyObject *obj;
368 int sig_num;
369 PyObject *old_handler;
370 void (*func)(int);
371 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
372 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000373#ifdef MS_WINDOWS
374 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000375 switch (sig_num) {
376 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000377#ifdef SIGBREAK
378 /* Issue #10003: SIGBREAK is not documented as permitted, but works
379 and corresponds to CTRL_BREAK_EVENT. */
380 case SIGBREAK: break;
381#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000382 case SIGFPE: break;
383 case SIGILL: break;
384 case SIGINT: break;
385 case SIGSEGV: break;
386 case SIGTERM: break;
387 default:
388 PyErr_SetString(PyExc_ValueError, "invalid signal value");
389 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000390 }
391#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000392#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (PyThread_get_thread_ident() != main_thread) {
394 PyErr_SetString(PyExc_ValueError,
395 "signal only works in main thread");
396 return NULL;
397 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000398#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (sig_num < 1 || sig_num >= NSIG) {
400 PyErr_SetString(PyExc_ValueError,
401 "signal number out of range");
402 return NULL;
403 }
404 if (obj == IgnoreHandler)
405 func = SIG_IGN;
406 else if (obj == DefaultHandler)
407 func = SIG_DFL;
408 else if (!PyCallable_Check(obj)) {
409 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000410"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return NULL;
412 }
413 else
414 func = signal_handler;
415 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200416 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return NULL;
418 }
419 old_handler = Handlers[sig_num].func;
420 Handlers[sig_num].tripped = 0;
421 Py_INCREF(obj);
422 Handlers[sig_num].func = obj;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200423 if (old_handler != NULL)
424 return old_handler;
425 else
426 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000427}
428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000429PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000430"signal(sig, action) -> action\n\
431\n\
432Set the action for the given signal. The action can be SIG_DFL,\n\
433SIG_IGN, or a callable Python object. The previous action is\n\
434returned. See getsignal() for possible return values.\n\
435\n\
436*** IMPORTANT NOTICE ***\n\
437A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000438the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000439
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000440
Guido van Rossume4485b01994-09-07 14:32:49 +0000441static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000442signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 int sig_num;
445 PyObject *old_handler;
446 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
447 return NULL;
448 if (sig_num < 1 || sig_num >= NSIG) {
449 PyErr_SetString(PyExc_ValueError,
450 "signal number out of range");
451 return NULL;
452 }
453 old_handler = Handlers[sig_num].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200454 if (old_handler != NULL) {
455 Py_INCREF(old_handler);
456 return old_handler;
457 }
458 else {
459 Py_RETURN_NONE;
460 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000461}
462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000463PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000464"getsignal(sig) -> action\n\
465\n\
466Return the current action for the given signal. The return value can be:\n\
467SIG_IGN -- if the signal is being ignored\n\
468SIG_DFL -- if the default action for the signal is in effect\n\
469None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000471
Christian Heimes8640e742008-02-23 16:23:06 +0000472#ifdef HAVE_SIGINTERRUPT
473PyDoc_STRVAR(siginterrupt_doc,
474"siginterrupt(sig, flag) -> None\n\
475change system call restart behaviour: if flag is False, system calls\n\
476will be restarted when interrupted by signal sig, else system calls\n\
477will be interrupted.");
478
479static PyObject *
480signal_siginterrupt(PyObject *self, PyObject *args)
481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 int sig_num;
483 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
486 return NULL;
487 if (sig_num < 1 || sig_num >= NSIG) {
488 PyErr_SetString(PyExc_ValueError,
489 "signal number out of range");
490 return NULL;
491 }
492 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200493 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return NULL;
495 }
Christian Heimes8640e742008-02-23 16:23:06 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_INCREF(Py_None);
498 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000499}
500
501#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000502
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000503static PyObject *
504signal_set_wakeup_fd(PyObject *self, PyObject *args)
505{
Steve Dowerf2f373f2015-02-21 08:44:05 -0800506 struct _Py_stat_struct st;
Victor Stinner11517102014-07-29 23:31:34 +0200507#ifdef MS_WINDOWS
508 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100509 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200510 int res;
511 int res_size = sizeof res;
512 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200513 int is_socket;
514
515 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
516 return NULL;
517
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100518 sockfd = PyLong_AsSocket_t(fdobj);
519 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200520 return NULL;
521#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
525 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200526#endif
527
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000528#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (PyThread_get_thread_ident() != main_thread) {
530 PyErr_SetString(PyExc_ValueError,
531 "set_wakeup_fd only works in main thread");
532 return NULL;
533 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000534#endif
Victor Stinner0bffc942014-07-21 16:28:54 +0200535
Victor Stinner11517102014-07-29 23:31:34 +0200536#ifdef MS_WINDOWS
537 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100538 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200539 /* Import the _socket module to call WSAStartup() */
540 mod = PyImport_ImportModuleNoBlock("_socket");
541 if (mod == NULL)
542 return NULL;
543 Py_DECREF(mod);
544
545 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100546 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200547 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100548 int fd, err;
549
550 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200551 if (err != WSAENOTSOCK) {
552 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
553 return NULL;
554 }
555
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100556 fd = (int)sockfd;
557 if ((SOCKET_T)fd != sockfd || !_PyVerify_fd(fd)) {
Victor Stinner11517102014-07-29 23:31:34 +0200558 PyErr_SetString(PyExc_ValueError, "invalid fd");
559 return NULL;
560 }
561
Steve Dowerf2f373f2015-02-21 08:44:05 -0800562 if (_Py_fstat(fd, &st) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200563 PyErr_SetFromErrno(PyExc_OSError);
564 return NULL;
565 }
Victor Stinner38227602014-08-27 12:59:44 +0200566
567 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200568 }
Victor Stinner38227602014-08-27 12:59:44 +0200569 else {
Victor Stinner11517102014-07-29 23:31:34 +0200570 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200571
572 /* Windows does not provide a function to test if a socket
573 is in non-blocking mode */
574 }
Victor Stinner11517102014-07-29 23:31:34 +0200575 }
576
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100577 old_sockfd = wakeup.fd;
578 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200579 wakeup.use_send = is_socket;
580
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100581 if (old_sockfd != INVALID_FD)
582 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200583 else
584 return PyLong_FromLong(-1);
585#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200586 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200587 int blocking;
588
Victor Stinner1d8948e2014-07-24 22:51:05 +0200589 if (!_PyVerify_fd(fd)) {
590 PyErr_SetString(PyExc_ValueError, "invalid fd");
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200591 return NULL;
592 }
Victor Stinner1d8948e2014-07-24 22:51:05 +0200593
Steve Dowerf2f373f2015-02-21 08:44:05 -0800594 if (_Py_fstat(fd, &st) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200595 PyErr_SetFromErrno(PyExc_OSError);
596 return NULL;
597 }
Victor Stinner38227602014-08-27 12:59:44 +0200598
599 blocking = _Py_get_blocking(fd);
600 if (blocking < 0)
601 return NULL;
602 if (blocking) {
603 PyErr_Format(PyExc_ValueError,
604 "the fd %i must be in non-blocking mode",
605 fd);
606 return NULL;
607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 old_fd = wakeup_fd;
611 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200614#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000615}
616
617PyDoc_STRVAR(set_wakeup_fd_doc,
618"set_wakeup_fd(fd) -> fd\n\
619\n\
Victor Stinner11517102014-07-29 23:31:34 +0200620Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000621comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200622The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000623\n\
624The fd must be non-blocking.");
625
626/* C API for the same, without all the error checking */
627int
628PySignal_SetWakeupFd(int fd)
629{
Victor Stinner11517102014-07-29 23:31:34 +0200630 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 if (fd < 0)
632 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200633
634#ifdef MS_WINDOWS
635 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
636 wakeup.fd = fd;
637#else
638 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200640#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000642}
643
644
Martin v. Löwis823725e2008-03-24 13:39:54 +0000645#ifdef HAVE_SETITIMER
646static PyObject *
647signal_setitimer(PyObject *self, PyObject *args)
648{
649 double first;
650 double interval = 0;
651 int which;
652 struct itimerval new, old;
653
654 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000656
657 timeval_from_double(first, &new.it_value);
658 timeval_from_double(interval, &new.it_interval);
659 /* Let OS check "which" value */
660 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 PyErr_SetFromErrno(ItimerError);
662 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000663 }
664
665 return itimer_retval(&old);
666}
667
668PyDoc_STRVAR(setitimer_doc,
669"setitimer(which, seconds[, interval])\n\
670\n\
671Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
672or ITIMER_PROF) to fire after value seconds and after\n\
673that every interval seconds.\n\
674The itimer can be cleared by setting seconds to zero.\n\
675\n\
676Returns old values as a tuple: (delay, interval).");
677#endif
678
679
680#ifdef HAVE_GETITIMER
681static PyObject *
682signal_getitimer(PyObject *self, PyObject *args)
683{
684 int which;
685 struct itimerval old;
686
687 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000689
690 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyErr_SetFromErrno(ItimerError);
692 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000693 }
694
695 return itimer_retval(&old);
696}
697
698PyDoc_STRVAR(getitimer_doc,
699"getitimer(which)\n\
700\n\
701Returns current value of given itimer.");
702#endif
703
Ross Lagerwallbc808222011-06-25 12:13:40 +0200704#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
705 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200706/* Convert an iterable to a sigset.
707 Return 0 on success, return -1 and raise an exception on error. */
708
709static int
710iterable_to_sigset(PyObject *iterable, sigset_t *mask)
711{
712 int result = -1;
713 PyObject *iterator, *item;
714 long signum;
715 int err;
716
717 sigemptyset(mask);
718
719 iterator = PyObject_GetIter(iterable);
720 if (iterator == NULL)
721 goto error;
722
723 while (1)
724 {
725 item = PyIter_Next(iterator);
726 if (item == NULL) {
727 if (PyErr_Occurred())
728 goto error;
729 else
730 break;
731 }
732
733 signum = PyLong_AsLong(item);
734 Py_DECREF(item);
735 if (signum == -1 && PyErr_Occurred())
736 goto error;
737 if (0 < signum && signum < NSIG)
738 err = sigaddset(mask, (int)signum);
739 else
740 err = 1;
741 if (err) {
742 PyErr_Format(PyExc_ValueError,
743 "signal number %ld out of range", signum);
744 goto error;
745 }
746 }
747 result = 0;
748
749error:
750 Py_XDECREF(iterator);
751 return result;
752}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200753#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200754
Victor Stinnerb3e72192011-05-08 01:46:11 +0200755#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200756static PyObject*
757sigset_to_set(sigset_t mask)
758{
759 PyObject *signum, *result;
760 int sig;
761
762 result = PySet_New(0);
763 if (result == NULL)
764 return NULL;
765
766 for (sig = 1; sig < NSIG; sig++) {
767 if (sigismember(&mask, sig) != 1)
768 continue;
769
770 /* Handle the case where it is a member by adding the signal to
771 the result list. Ignore the other cases because they mean the
772 signal isn't a member of the mask or the signal was invalid,
773 and an invalid signal must have been our fault in constructing
774 the loop boundaries. */
775 signum = PyLong_FromLong(sig);
776 if (signum == NULL) {
777 Py_DECREF(result);
778 return NULL;
779 }
780 if (PySet_Add(result, signum) == -1) {
781 Py_DECREF(signum);
782 Py_DECREF(result);
783 return NULL;
784 }
785 Py_DECREF(signum);
786 }
787 return result;
788}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200789#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200790
Victor Stinnerb3e72192011-05-08 01:46:11 +0200791#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200792static PyObject *
793signal_pthread_sigmask(PyObject *self, PyObject *args)
794{
Victor Stinner35b300c2011-05-04 13:20:35 +0200795 int how;
796 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200797 sigset_t mask, previous;
798 int err;
799
800 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
801 return NULL;
802
803 if (iterable_to_sigset(signals, &mask))
804 return NULL;
805
806 err = pthread_sigmask(how, &mask, &previous);
807 if (err != 0) {
808 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200809 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200810 return NULL;
811 }
812
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200813 /* if signals was unblocked, signal handlers have been called */
814 if (PyErr_CheckSignals())
815 return NULL;
816
Victor Stinner35b300c2011-05-04 13:20:35 +0200817 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200818}
819
820PyDoc_STRVAR(signal_pthread_sigmask_doc,
821"pthread_sigmask(how, mask) -> old mask\n\
822\n\
823Fetch and/or change the signal mask of the calling thread.");
824#endif /* #ifdef PYPTHREAD_SIGMASK */
825
Martin v. Löwis823725e2008-03-24 13:39:54 +0000826
Victor Stinnerb3e72192011-05-08 01:46:11 +0200827#ifdef HAVE_SIGPENDING
828static PyObject *
829signal_sigpending(PyObject *self)
830{
831 int err;
832 sigset_t mask;
833 err = sigpending(&mask);
834 if (err)
835 return PyErr_SetFromErrno(PyExc_OSError);
836 return sigset_to_set(mask);
837}
838
839PyDoc_STRVAR(signal_sigpending_doc,
840"sigpending() -> list\n\
841\n\
842Examine pending signals.");
843#endif /* #ifdef HAVE_SIGPENDING */
844
845
846#ifdef HAVE_SIGWAIT
847static PyObject *
848signal_sigwait(PyObject *self, PyObject *args)
849{
850 PyObject *signals;
851 sigset_t set;
852 int err, signum;
853
854 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
855 return NULL;
856
857 if (iterable_to_sigset(signals, &set))
858 return NULL;
859
Victor Stinner10c30d62011-06-10 01:39:53 +0200860 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200861 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200862 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200863 if (err) {
864 errno = err;
865 return PyErr_SetFromErrno(PyExc_OSError);
866 }
867
868 return PyLong_FromLong(signum);
869}
870
871PyDoc_STRVAR(signal_sigwait_doc,
872"sigwait(sigset) -> signum\n\
873\n\
874Wait a signal.");
875#endif /* #ifdef HAVE_SIGPENDING */
876
Ross Lagerwallbc808222011-06-25 12:13:40 +0200877#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
878static int initialized;
879static PyStructSequence_Field struct_siginfo_fields[] = {
880 {"si_signo", "signal number"},
881 {"si_code", "signal code"},
882 {"si_errno", "errno associated with this signal"},
883 {"si_pid", "sending process ID"},
884 {"si_uid", "real user ID of sending process"},
885 {"si_status", "exit value or signal"},
886 {"si_band", "band event for SIGPOLL"},
887 {0}
888};
889
890PyDoc_STRVAR(struct_siginfo__doc__,
891"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
892This object may be accessed either as a tuple of\n\
893(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
894or via the attributes si_signo, si_code, and so on.");
895
896static PyStructSequence_Desc struct_siginfo_desc = {
897 "signal.struct_siginfo", /* name */
898 struct_siginfo__doc__, /* doc */
899 struct_siginfo_fields, /* fields */
900 7 /* n_in_sequence */
901};
902
903static PyTypeObject SiginfoType;
904
905static PyObject *
906fill_siginfo(siginfo_t *si)
907{
908 PyObject *result = PyStructSequence_New(&SiginfoType);
909 if (!result)
910 return NULL;
911
912 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
913 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
914 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
915 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200916 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200917 PyStructSequence_SET_ITEM(result, 5,
918 PyLong_FromLong((long)(si->si_status)));
919 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
920 if (PyErr_Occurred()) {
921 Py_DECREF(result);
922 return NULL;
923 }
924
925 return result;
926}
927#endif
928
929#ifdef HAVE_SIGWAITINFO
930static PyObject *
931signal_sigwaitinfo(PyObject *self, PyObject *args)
932{
933 PyObject *signals;
934 sigset_t set;
935 siginfo_t si;
936 int err;
937
938 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
939 return NULL;
940
941 if (iterable_to_sigset(signals, &set))
942 return NULL;
943
944 Py_BEGIN_ALLOW_THREADS
945 err = sigwaitinfo(&set, &si);
946 Py_END_ALLOW_THREADS
947 if (err == -1)
948 return PyErr_SetFromErrno(PyExc_OSError);
949
950 return fill_siginfo(&si);
951}
952
953PyDoc_STRVAR(signal_sigwaitinfo_doc,
954"sigwaitinfo(sigset) -> struct_siginfo\n\
955\n\
956Wait synchronously for a signal until one of the signals in *sigset* is\n\
957delivered.\n\
958Returns a struct_siginfo containing information about the signal.");
959#endif /* #ifdef HAVE_SIGWAITINFO */
960
961#ifdef HAVE_SIGTIMEDWAIT
962static PyObject *
963signal_sigtimedwait(PyObject *self, PyObject *args)
964{
965 PyObject *signals, *timeout;
966 struct timespec buf;
967 sigset_t set;
968 siginfo_t si;
Antoine Pitroucf8a1e52013-04-17 22:06:44 +0200969 time_t tv_sec;
970 long tv_nsec;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200971 int err;
972
Victor Stinner643cd682012-03-02 22:54:03 +0100973 if (!PyArg_ParseTuple(args, "OO:sigtimedwait",
974 &signals, &timeout))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200975 return NULL;
976
Victor Stinner3c1b3792014-02-17 00:02:43 +0100977 if (_PyTime_ObjectToTimespec(timeout, &tv_sec, &tv_nsec,
978 _PyTime_ROUND_DOWN) == -1)
Ross Lagerwallbc808222011-06-25 12:13:40 +0200979 return NULL;
Antoine Pitroucf8a1e52013-04-17 22:06:44 +0200980 buf.tv_sec = tv_sec;
981 buf.tv_nsec = tv_nsec;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200982
983 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
984 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
985 return NULL;
986 }
987
988 if (iterable_to_sigset(signals, &set))
989 return NULL;
990
991 Py_BEGIN_ALLOW_THREADS
992 err = sigtimedwait(&set, &si, &buf);
993 Py_END_ALLOW_THREADS
994 if (err == -1) {
995 if (errno == EAGAIN)
996 Py_RETURN_NONE;
997 else
998 return PyErr_SetFromErrno(PyExc_OSError);
999 }
1000
1001 return fill_siginfo(&si);
1002}
1003
1004PyDoc_STRVAR(signal_sigtimedwait_doc,
1005"sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
1006\n\
1007Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
1008nanoseconds).");
1009#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1010
Victor Stinnerb3e72192011-05-08 01:46:11 +02001011
1012#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
1013static PyObject *
1014signal_pthread_kill(PyObject *self, PyObject *args)
1015{
1016 long tid;
1017 int signum;
1018 int err;
1019
1020 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
1021 return NULL;
1022
Victor Stinner86e104a2011-05-09 14:45:38 +02001023 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001024 if (err != 0) {
1025 errno = err;
1026 PyErr_SetFromErrno(PyExc_OSError);
1027 return NULL;
1028 }
1029
1030 /* the signal may have been send to the current thread */
1031 if (PyErr_CheckSignals())
1032 return NULL;
1033
1034 Py_RETURN_NONE;
1035}
1036
1037PyDoc_STRVAR(signal_pthread_kill_doc,
1038"pthread_kill(thread_id, signum)\n\
1039\n\
1040Send a signal to a thread.");
1041#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1042
1043
1044
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001045/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +00001046static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +00001047#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +00001049#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001050#ifdef HAVE_SETITIMER
1051 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
1052#endif
1053#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +00001055#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 {"signal", signal_signal, METH_VARARGS, signal_doc},
1057 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
1058 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +00001059#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +00001061#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +00001062#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +02001064 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +00001065#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 {"default_int_handler", signal_default_int_handler,
1067 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +02001068#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
1069 {"pthread_kill", (PyCFunction)signal_pthread_kill,
1070 METH_VARARGS, signal_pthread_kill_doc},
1071#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001072#ifdef PYPTHREAD_SIGMASK
1073 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
1074 METH_VARARGS, signal_pthread_sigmask_doc},
1075#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +02001076#ifdef HAVE_SIGPENDING
1077 {"sigpending", (PyCFunction)signal_sigpending,
1078 METH_NOARGS, signal_sigpending_doc},
1079#endif
1080#ifdef HAVE_SIGWAIT
1081 {"sigwait", (PyCFunction)signal_sigwait,
1082 METH_VARARGS, signal_sigwait_doc},
1083#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001084#ifdef HAVE_SIGWAITINFO
1085 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
1086 METH_VARARGS, signal_sigwaitinfo_doc},
1087#endif
1088#ifdef HAVE_SIGTIMEDWAIT
1089 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
1090 METH_VARARGS, signal_sigtimedwait_doc},
1091#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001093};
1094
Barry Warsaw92971171997-01-03 00:14:25 +00001095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001097"This module provides mechanisms to use signal handlers in Python.\n\
1098\n\
1099Functions:\n\
1100\n\
1101alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001102setitimer() -- cause a signal (described below) after a specified\n\
1103 float time and the timer may restart then [Unix only]\n\
1104getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001105signal() -- set the action for a given signal\n\
1106getsignal() -- get the signal action for a given signal\n\
1107pause() -- wait until a signal arrives [Unix only]\n\
1108default_int_handler() -- default SIGINT handler\n\
1109\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001110signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001111SIG_DFL -- used to refer to the system default handler\n\
1112SIG_IGN -- used to ignore the signal\n\
1113NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001114SIGINT, SIGTERM, etc. -- signal numbers\n\
1115\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001116itimer constants:\n\
1117ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1118 expiration\n\
1119ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1120 and delivers SIGVTALRM upon expiration\n\
1121ITIMER_PROF -- decrements both when the process is executing and\n\
1122 when the system is executing on behalf of the process.\n\
1123 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1124 used to profile the time spent by the application\n\
1125 in user and kernel space. SIGPROF is delivered upon\n\
1126 expiration.\n\
1127\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001128*** IMPORTANT NOTICE ***\n\
1129A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001130the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001131
Martin v. Löwis1a214512008-06-11 05:26:20 +00001132static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001134 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 module_doc,
1136 -1,
1137 signal_methods,
1138 NULL,
1139 NULL,
1140 NULL,
1141 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001142};
1143
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001144PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001145PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 PyObject *m, *d, *x;
1148 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001149
1150#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 main_thread = PyThread_get_thread_ident();
1152 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001153#endif
1154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 /* Create the module and add the functions */
1156 m = PyModule_Create(&signalmodule);
1157 if (m == NULL)
1158 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001159
Ross Lagerwallbc808222011-06-25 12:13:40 +02001160#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001161 if (!initialized) {
1162 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1163 return NULL;
1164 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001165 Py_INCREF((PyObject*) &SiginfoType);
1166 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1167 initialized = 1;
1168#endif
1169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 /* Add some symbolic constants to the module */
1171 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1174 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1175 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1178 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1179 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 x = PyLong_FromLong((long)NSIG);
1182 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1183 goto finally;
1184 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001185
Victor Stinnera9293352011-04-30 15:21:58 +02001186#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001187 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1188 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001189#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001190#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001191 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1192 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001193#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001194#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001195 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1196 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001197#endif
1198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1200 if (!x)
1201 goto finally;
1202 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 Handlers[0].tripped = 0;
1205 for (i = 1; i < NSIG; i++) {
1206 void (*t)(int);
1207 t = PyOS_getsig(i);
1208 Handlers[i].tripped = 0;
1209 if (t == SIG_DFL)
1210 Handlers[i].func = DefaultHandler;
1211 else if (t == SIG_IGN)
1212 Handlers[i].func = IgnoreHandler;
1213 else
1214 Handlers[i].func = Py_None; /* None of our business */
1215 Py_INCREF(Handlers[i].func);
1216 }
1217 if (Handlers[SIGINT].func == DefaultHandler) {
1218 /* Install default int handler */
1219 Py_INCREF(IntHandler);
1220 Py_DECREF(Handlers[SIGINT].func);
1221 Handlers[SIGINT].func = IntHandler;
1222 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1223 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001224
1225#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 x = PyLong_FromLong(SIGHUP);
1227 PyDict_SetItemString(d, "SIGHUP", x);
1228 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001229#endif
1230#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 x = PyLong_FromLong(SIGINT);
1232 PyDict_SetItemString(d, "SIGINT", x);
1233 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001234#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001235#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 x = PyLong_FromLong(SIGBREAK);
1237 PyDict_SetItemString(d, "SIGBREAK", x);
1238 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001239#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001240#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 x = PyLong_FromLong(SIGQUIT);
1242 PyDict_SetItemString(d, "SIGQUIT", x);
1243 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001244#endif
1245#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 x = PyLong_FromLong(SIGILL);
1247 PyDict_SetItemString(d, "SIGILL", x);
1248 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001249#endif
1250#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 x = PyLong_FromLong(SIGTRAP);
1252 PyDict_SetItemString(d, "SIGTRAP", x);
1253 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001254#endif
1255#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 x = PyLong_FromLong(SIGIOT);
1257 PyDict_SetItemString(d, "SIGIOT", x);
1258 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001259#endif
1260#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 x = PyLong_FromLong(SIGABRT);
1262 PyDict_SetItemString(d, "SIGABRT", x);
1263 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001264#endif
1265#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 x = PyLong_FromLong(SIGEMT);
1267 PyDict_SetItemString(d, "SIGEMT", x);
1268 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001269#endif
1270#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 x = PyLong_FromLong(SIGFPE);
1272 PyDict_SetItemString(d, "SIGFPE", x);
1273 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001274#endif
1275#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 x = PyLong_FromLong(SIGKILL);
1277 PyDict_SetItemString(d, "SIGKILL", x);
1278 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001279#endif
1280#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 x = PyLong_FromLong(SIGBUS);
1282 PyDict_SetItemString(d, "SIGBUS", x);
1283 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001284#endif
1285#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 x = PyLong_FromLong(SIGSEGV);
1287 PyDict_SetItemString(d, "SIGSEGV", x);
1288 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001289#endif
1290#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 x = PyLong_FromLong(SIGSYS);
1292 PyDict_SetItemString(d, "SIGSYS", x);
1293 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001294#endif
1295#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 x = PyLong_FromLong(SIGPIPE);
1297 PyDict_SetItemString(d, "SIGPIPE", x);
1298 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299#endif
1300#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 x = PyLong_FromLong(SIGALRM);
1302 PyDict_SetItemString(d, "SIGALRM", x);
1303 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001304#endif
1305#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 x = PyLong_FromLong(SIGTERM);
1307 PyDict_SetItemString(d, "SIGTERM", x);
1308 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001309#endif
1310#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 x = PyLong_FromLong(SIGUSR1);
1312 PyDict_SetItemString(d, "SIGUSR1", x);
1313 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001314#endif
1315#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 x = PyLong_FromLong(SIGUSR2);
1317 PyDict_SetItemString(d, "SIGUSR2", x);
1318 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001319#endif
1320#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 x = PyLong_FromLong(SIGCLD);
1322 PyDict_SetItemString(d, "SIGCLD", x);
1323 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001324#endif
1325#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 x = PyLong_FromLong(SIGCHLD);
1327 PyDict_SetItemString(d, "SIGCHLD", x);
1328 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001329#endif
1330#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 x = PyLong_FromLong(SIGPWR);
1332 PyDict_SetItemString(d, "SIGPWR", x);
1333 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001334#endif
1335#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 x = PyLong_FromLong(SIGIO);
1337 PyDict_SetItemString(d, "SIGIO", x);
1338 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339#endif
1340#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 x = PyLong_FromLong(SIGURG);
1342 PyDict_SetItemString(d, "SIGURG", x);
1343 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001344#endif
1345#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 x = PyLong_FromLong(SIGWINCH);
1347 PyDict_SetItemString(d, "SIGWINCH", x);
1348 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349#endif
1350#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 x = PyLong_FromLong(SIGPOLL);
1352 PyDict_SetItemString(d, "SIGPOLL", x);
1353 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001354#endif
1355#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 x = PyLong_FromLong(SIGSTOP);
1357 PyDict_SetItemString(d, "SIGSTOP", x);
1358 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001359#endif
1360#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 x = PyLong_FromLong(SIGTSTP);
1362 PyDict_SetItemString(d, "SIGTSTP", x);
1363 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001364#endif
1365#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 x = PyLong_FromLong(SIGCONT);
1367 PyDict_SetItemString(d, "SIGCONT", x);
1368 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369#endif
1370#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 x = PyLong_FromLong(SIGTTIN);
1372 PyDict_SetItemString(d, "SIGTTIN", x);
1373 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001374#endif
1375#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 x = PyLong_FromLong(SIGTTOU);
1377 PyDict_SetItemString(d, "SIGTTOU", x);
1378 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001379#endif
1380#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 x = PyLong_FromLong(SIGVTALRM);
1382 PyDict_SetItemString(d, "SIGVTALRM", x);
1383 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001384#endif
1385#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 x = PyLong_FromLong(SIGPROF);
1387 PyDict_SetItemString(d, "SIGPROF", x);
1388 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001389#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001390#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 x = PyLong_FromLong(SIGXCPU);
1392 PyDict_SetItemString(d, "SIGXCPU", x);
1393 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001394#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001395#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 x = PyLong_FromLong(SIGXFSZ);
1397 PyDict_SetItemString(d, "SIGXFSZ", x);
1398 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001399#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001400#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 x = PyLong_FromLong(SIGRTMIN);
1402 PyDict_SetItemString(d, "SIGRTMIN", x);
1403 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001404#endif
1405#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 x = PyLong_FromLong(SIGRTMAX);
1407 PyDict_SetItemString(d, "SIGRTMAX", x);
1408 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001409#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001410#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 x = PyLong_FromLong(SIGINFO);
1412 PyDict_SetItemString(d, "SIGINFO", x);
1413 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001414#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001415
1416#ifdef ITIMER_REAL
1417 x = PyLong_FromLong(ITIMER_REAL);
1418 PyDict_SetItemString(d, "ITIMER_REAL", x);
1419 Py_DECREF(x);
1420#endif
1421#ifdef ITIMER_VIRTUAL
1422 x = PyLong_FromLong(ITIMER_VIRTUAL);
1423 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1424 Py_DECREF(x);
1425#endif
1426#ifdef ITIMER_PROF
1427 x = PyLong_FromLong(ITIMER_PROF);
1428 PyDict_SetItemString(d, "ITIMER_PROF", x);
1429 Py_DECREF(x);
1430#endif
1431
1432#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 ItimerError = PyErr_NewException("signal.ItimerError",
1434 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001435 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001437#endif
1438
Brian Curtineb24d742010-04-12 17:16:38 +00001439#ifdef CTRL_C_EVENT
1440 x = PyLong_FromLong(CTRL_C_EVENT);
1441 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1442 Py_DECREF(x);
1443#endif
1444
1445#ifdef CTRL_BREAK_EVENT
1446 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1447 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1448 Py_DECREF(x);
1449#endif
1450
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001451#ifdef MS_WINDOWS
1452 /* Create manual-reset event, initially unset */
1453 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1454#endif
1455
Martin v. Löwis1a214512008-06-11 05:26:20 +00001456 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 Py_DECREF(m);
1458 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001459 }
Barry Warsaw92971171997-01-03 00:14:25 +00001460
Barry Warsaw92971171997-01-03 00:14:25 +00001461 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001462 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001463}
1464
1465static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001466finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 int i;
1469 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 PyOS_setsig(SIGINT, old_siginthandler);
1472 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 for (i = 1; i < NSIG; i++) {
1475 func = Handlers[i].func;
1476 Handlers[i].tripped = 0;
1477 Handlers[i].func = NULL;
1478 if (i != SIGINT && func != NULL && func != Py_None &&
1479 func != DefaultHandler && func != IgnoreHandler)
1480 PyOS_setsig(i, SIG_DFL);
1481 Py_XDECREF(func);
1482 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001483
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001484 Py_CLEAR(IntHandler);
1485 Py_CLEAR(DefaultHandler);
1486 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001487}
1488
Barry Warsaw92971171997-01-03 00:14:25 +00001489
Barry Warsaw92971171997-01-03 00:14:25 +00001490/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001491int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001492PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 int i;
1495 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (!is_tripped)
1498 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001499
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001500#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (PyThread_get_thread_ident() != main_thread)
1502 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001503#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 /*
1506 * The is_tripped variable is meant to speed up the calls to
1507 * PyErr_CheckSignals (both directly or via pending calls) when no
1508 * signal has arrived. This variable is set to 1 when a signal arrives
1509 * and it is set to 0 here, when we know some signals arrived. This way
1510 * we can run the registered handlers with no signals blocked.
1511 *
1512 * NOTE: with this approach we can have a situation where is_tripped is
1513 * 1 but we have no more signals to handle (Handlers[i].tripped
1514 * is 0 for every signal i). This won't do us any harm (except
1515 * we're gonna spent some cycles for nothing). This happens when
1516 * we receive a signal i after we zero is_tripped and before we
1517 * check Handlers[i].tripped.
1518 */
1519 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!(f = (PyObject *)PyEval_GetFrame()))
1522 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 for (i = 1; i < NSIG; i++) {
1525 if (Handlers[i].tripped) {
1526 PyObject *result = NULL;
1527 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1528 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (arglist) {
1531 result = PyEval_CallObject(Handlers[i].func,
1532 arglist);
1533 Py_DECREF(arglist);
1534 }
1535 if (!result)
1536 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 Py_DECREF(result);
1539 }
1540 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001543}
1544
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001545
Barry Warsaw92971171997-01-03 00:14:25 +00001546/* Replacements for intrcheck.c functionality
1547 * Declared in pyerrors.h
1548 */
1549void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001550PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001551{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001552 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001553}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001554
1555void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001556PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001557{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001558 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 Py_DECREF(m);
1561 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001562}
1563
1564void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001565PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001568}
1569
1570int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001571PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001574#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (PyThread_get_thread_ident() != main_thread)
1576 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001577#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 Handlers[SIGINT].tripped = 0;
1579 return 1;
1580 }
1581 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001582}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001583
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001584static void
1585_clear_pending_signals(void)
1586{
1587 int i;
1588 if (!is_tripped)
1589 return;
1590 is_tripped = 0;
1591 for (i = 1; i < NSIG; ++i) {
1592 Handlers[i].tripped = 0;
1593 }
1594}
1595
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001596void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001597PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001598{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001599 /* Clear the signal flags after forking so that they aren't handled
1600 * in both processes if they came in just before the fork() but before
1601 * the interpreter had an opportunity to call the handlers. issue9535. */
1602 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001603#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001604 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1605 * can be called safely. */
1606 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001607 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PyEval_ReInitThreads();
1609 main_thread = PyThread_get_thread_ident();
1610 main_pid = getpid();
1611 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001612#endif
1613}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001614
1615int
1616_PyOS_IsMainThread(void)
1617{
1618#ifdef WITH_THREAD
1619 return PyThread_get_thread_ident() == main_thread;
1620#else
1621 return 1;
1622#endif
1623}
1624
1625#ifdef MS_WINDOWS
1626void *_PyOS_SigintEvent(void)
1627{
1628 /* Returns a manual-reset event which gets tripped whenever
1629 SIGINT is received.
1630
1631 Python.h does not include windows.h so we do cannot use HANDLE
1632 as the return type of this function. We use void* instead. */
1633 return sigint_event;
1634}
1635#endif