blob: 70f305214988327cf672f9191533cb21a9e8901f [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007#ifndef MS_WINDOWS
8#include "posixmodule.h"
9#endif
Victor Stinner11517102014-07-29 23:31:34 +020010#ifdef MS_WINDOWS
11#include "socketmodule.h" /* needed for SOCKET_T */
12#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000014#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020015#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000017#include <process.h>
18#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000019#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000020
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000022#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
24#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000025#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000027#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000028#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000030
Victor Stinnera9293352011-04-30 15:21:58 +020031#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
32# define PYPTHREAD_SIGMASK
33#endif
34
35#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
36# include <pthread.h>
37#endif
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000040#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#endif
42
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000043#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#endif
54
Tal Einatc7027b72015-05-16 14:14:49 +030055#include "clinic/signalmodule.c.h"
56
57/*[clinic input]
58module signal
59[clinic start generated code]*/
60/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
61
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000062
Guido van Rossumbb4ba121994-06-23 11:25:45 +000063/*
64 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
65
66 When threads are supported, we want the following semantics:
67
68 - only the main thread can set a signal handler
69 - any thread can get a signal handler
70 - signals are only delivered to the main thread
71
72 I.e. we don't support "synchronous signals" like SIGFPE (catching
73 this doesn't make much sense in Python anyway) nor do we support
74 signals as a means of inter-thread communication, since not all
75 thread implementations support that (at least our thread library
76 doesn't).
77
78 We still have the problem that in some implementations signals
79 generated by the keyboard (e.g. SIGINT) are delivered to all
80 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
81 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000082 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000083 a working implementation that works in all three cases -- the
84 handler ignores signals if getpid() isn't the same as in the main
85 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000086*/
87
88#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000089#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000090#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091static long main_thread;
92static pid_t main_pid;
93#endif
94
Victor Stinner2ec6b172011-05-15 10:21:59 +020095static volatile struct {
96 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000098} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099
Victor Stinner11517102014-07-29 23:31:34 +0200100#ifdef MS_WINDOWS
101#define INVALID_FD ((SOCKET_T)-1)
102
103static volatile struct {
104 SOCKET_T fd;
105 int use_send;
106 int send_err_set;
107 int send_errno;
108 int send_win_error;
109} wakeup = {INVALID_FD, 0, 0};
110#else
111#define INVALID_FD (-1)
Victor Stinner2ec6b172011-05-15 10:21:59 +0200112static volatile sig_atomic_t wakeup_fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200113#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000114
Christian Heimesb76922a2007-12-11 01:06:40 +0000115/* Speed up sigcheck() when none tripped */
116static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000117
Barry Warsaw92971171997-01-03 00:14:25 +0000118static PyObject *DefaultHandler;
119static PyObject *IgnoreHandler;
120static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000121
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000122/* On Solaris 8, gcc will produce a warning that the function
123 declaration is not a prototype. This is caused by the definition of
124 SIG_DFL as (void (*)())0; the correct declaration would have been
125 (void (*)(int))0. */
126
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000127static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000128
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100129#ifdef MS_WINDOWS
130static HANDLE sigint_event = NULL;
131#endif
132
Martin v. Löwis823725e2008-03-24 13:39:54 +0000133#ifdef HAVE_GETITIMER
134static PyObject *ItimerError;
135
136/* auxiliary functions for setitimer/getitimer */
137static void
138timeval_from_double(double d, struct timeval *tv)
139{
140 tv->tv_sec = floor(d);
141 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
142}
143
Christian Heimes1a8501c2008-10-02 19:56:01 +0000144Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145double_from_timeval(struct timeval *tv)
146{
147 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
148}
149
150static PyObject *
151itimer_retval(struct itimerval *iv)
152{
153 PyObject *r, *v;
154
155 r = PyTuple_New(2);
156 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158
159 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
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, 0, v);
165
166 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 Py_DECREF(r);
168 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000169 }
170
171 PyTuple_SET_ITEM(r, 1, v);
172
173 return r;
174}
175#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000176
Guido van Rossume4485b01994-09-07 14:32:49 +0000177static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000178signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 PyErr_SetNone(PyExc_KeyboardInterrupt);
181 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000182}
183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000184PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000185"default_int_handler(...)\n\
186\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000187The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000189
Thomas Wouters0796b002000-07-22 23:49:30 +0000190
191static int
192checksignals_witharg(void * unused)
193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000195}
196
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200197static int
Victor Stinner11517102014-07-29 23:31:34 +0200198report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200199{
200 int save_errno = errno;
201 errno = (int) (Py_intptr_t) data;
202 PyErr_SetFromErrno(PyExc_OSError);
203 PySys_WriteStderr("Exception ignored when trying to write to the "
204 "signal wakeup fd:\n");
205 PyErr_WriteUnraisable(NULL);
206 errno = save_errno;
207 return 0;
208}
209
Victor Stinner11517102014-07-29 23:31:34 +0200210#ifdef MS_WINDOWS
211static int
212report_wakeup_send_error(void* Py_UNUSED(data))
213{
214 PyObject *res;
215
216 if (wakeup.send_win_error) {
217 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
218 recognizes the error codes used by both GetLastError() and
219 WSAGetLastError */
220 res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
221 }
222 else {
223 errno = wakeup.send_errno;
224 res = PyErr_SetFromErrno(PyExc_OSError);
225 }
226
227 assert(res == NULL);
228 wakeup.send_err_set = 0;
229
230 PySys_WriteStderr("Exception ignored when trying to send to the "
231 "signal wakeup fd:\n");
232 PyErr_WriteUnraisable(NULL);
233
234 return 0;
235}
236#endif /* MS_WINDOWS */
237
Tim Peters4f1b2082000-07-23 21:18:09 +0000238static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200239trip_signal(int sig_num)
240{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200241 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200242 int fd;
243 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200244
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200245 Handlers[sig_num].tripped = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200246
247#ifdef MS_WINDOWS
248 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
249#else
250 fd = wakeup_fd;
251#endif
252
253 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200254 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200255#ifdef MS_WINDOWS
256 if (wakeup.use_send) {
257 do {
258 rc = send(fd, &byte, 1, 0);
259 } while (rc < 0 && errno == EINTR);
260
261 /* we only have a storage for one error in the wakeup structure */
262 if (rc < 0 && !wakeup.send_err_set) {
263 wakeup.send_err_set = 1;
264 wakeup.send_errno = errno;
265 wakeup.send_win_error = GetLastError();
266 Py_AddPendingCall(report_wakeup_send_error, NULL);
267 }
268 }
269 else
270#endif
271 {
272 byte = (unsigned char)sig_num;
Victor Stinnere72fe392015-04-01 18:35:22 +0200273
274 /* _Py_write_noraise() retries write() if write() is interrupted by
275 a signal (fails with EINTR). */
276 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200277
278 if (rc < 0) {
279 Py_AddPendingCall(report_wakeup_write_error,
280 (void *)(Py_intptr_t)errno);
281 }
282 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200283 }
Victor Stinner11517102014-07-29 23:31:34 +0200284
285 if (!is_tripped) {
286 /* Set is_tripped after setting .tripped, as it gets
287 cleared in PyErr_CheckSignals() before .tripped. */
288 is_tripped = 1;
289 Py_AddPendingCall(checksignals_witharg, NULL);
290 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200291}
292
293static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000294signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000295{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000296 int save_errno = errno;
297
Antoine Pitrou39a65912010-11-05 19:47:27 +0000298#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000300 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000301#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000302 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200303 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000305
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000306#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000307#ifdef SIGCHLD
308 /* To avoid infinite recursion, this signal remains
309 reset until explicit re-instated.
310 Don't clear the 'func' field as it is our pointer
311 to the Python handler... */
312 if (sig_num != SIGCHLD)
313#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000315 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 * makes this true. See also issue8354. */
317 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000318#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319
320 /* Issue #10311: asynchronously executing signal handlers should not
321 mutate errno under the feet of unsuspecting C code. */
322 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100323
324#ifdef MS_WINDOWS
325 if (sig_num == SIGINT)
326 SetEvent(sigint_event);
327#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000328}
Guido van Rossume4485b01994-09-07 14:32:49 +0000329
Guido van Rossum06d511d1995-03-10 15:13:48 +0000330
Guido van Rossum1171ee61997-08-22 20:42:00 +0000331#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300332
333/*[clinic input]
334signal.alarm -> long
335
336 seconds: int
337 /
338
339Arrange for SIGALRM to arrive after the given number of seconds.
340[clinic start generated code]*/
341
342static long
343signal_alarm_impl(PyModuleDef *module, int seconds)
344/*[clinic end generated code: output=f5f9badaab25d3e7 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300347 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000348}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000349
Guido van Rossum06d511d1995-03-10 15:13:48 +0000350#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000351
Guido van Rossum1171ee61997-08-22 20:42:00 +0000352#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300353
354/*[clinic input]
355signal.pause
356
357Wait until a signal arrives.
358[clinic start generated code]*/
359
Guido van Rossuma597dde1995-01-10 20:56:29 +0000360static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300361signal_pause_impl(PyModuleDef *module)
362/*[clinic end generated code: output=9245704caa63bbe9 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 Py_BEGIN_ALLOW_THREADS
365 (void)pause();
366 Py_END_ALLOW_THREADS
367 /* make sure that any exceptions that got raised are propagated
368 * back into Python
369 */
370 if (PyErr_CheckSignals())
371 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000372
Tal Einatc7027b72015-05-16 14:14:49 +0300373 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000374}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000375
Guido van Rossum06d511d1995-03-10 15:13:48 +0000376#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000377
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000378
Tal Einatc7027b72015-05-16 14:14:49 +0300379/*[clinic input]
380signal.signal
381
382 signalnum: int
383 handler: object
384 /
385
386Set the action for the given signal.
387
388The action can be SIG_DFL, SIG_IGN, or a callable Python object.
389The previous action is returned. See getsignal() for possible return values.
390
391*** IMPORTANT NOTICE ***
392A signal handler function is called with two arguments:
393the first is the signal number, the second is the interrupted stack frame.
394[clinic start generated code]*/
395
Guido van Rossume4485b01994-09-07 14:32:49 +0000396static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300397signal_signal_impl(PyModuleDef *module, int signalnum, PyObject *handler)
398/*[clinic end generated code: output=622d7d0beebea546 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyObject *old_handler;
401 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000402#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300403 /* Validate that signalnum is one of the allowable signals */
404 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000405 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000406#ifdef SIGBREAK
407 /* Issue #10003: SIGBREAK is not documented as permitted, but works
408 and corresponds to CTRL_BREAK_EVENT. */
409 case SIGBREAK: break;
410#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000411 case SIGFPE: break;
412 case SIGILL: break;
413 case SIGINT: break;
414 case SIGSEGV: break;
415 case SIGTERM: break;
416 default:
417 PyErr_SetString(PyExc_ValueError, "invalid signal value");
418 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000419 }
420#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000421#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (PyThread_get_thread_ident() != main_thread) {
423 PyErr_SetString(PyExc_ValueError,
424 "signal only works in main thread");
425 return NULL;
426 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000427#endif
Tal Einatc7027b72015-05-16 14:14:49 +0300428 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyErr_SetString(PyExc_ValueError,
430 "signal number out of range");
431 return NULL;
432 }
Tal Einatc7027b72015-05-16 14:14:49 +0300433 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300435 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300437 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000439"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 return NULL;
441 }
442 else
443 func = signal_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300444 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200445 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return NULL;
447 }
Tal Einatc7027b72015-05-16 14:14:49 +0300448 old_handler = Handlers[signalnum].func;
449 Handlers[signalnum].tripped = 0;
450 Py_INCREF(handler);
451 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200452 if (old_handler != NULL)
453 return old_handler;
454 else
455 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000456}
457
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000458
Tal Einatc7027b72015-05-16 14:14:49 +0300459/*[clinic input]
460signal.getsignal
461
462 signalnum: int
463 /
464
465Return the current action for the given signal.
466
467The return value can be:
468 SIG_IGN -- if the signal is being ignored
469 SIG_DFL -- if the default action for the signal is in effect
470 None -- if an unknown handler is in effect
471 anything else -- the callable Python object used as a handler
472[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000473
Guido van Rossume4485b01994-09-07 14:32:49 +0000474static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300475signal_getsignal_impl(PyModuleDef *module, int signalnum)
476/*[clinic end generated code: output=d50ec355757e360c input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300479 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 PyErr_SetString(PyExc_ValueError,
481 "signal number out of range");
482 return NULL;
483 }
Tal Einatc7027b72015-05-16 14:14:49 +0300484 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200485 if (old_handler != NULL) {
486 Py_INCREF(old_handler);
487 return old_handler;
488 }
489 else {
490 Py_RETURN_NONE;
491 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000492}
493
Christian Heimes8640e742008-02-23 16:23:06 +0000494#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300495
496/*[clinic input]
497signal.siginterrupt
498
499 signalnum: int
500 flag: int
501 /
502
503Change system call restart behaviour.
504
505If flag is False, system calls will be restarted when interrupted by
506signal sig, else system calls will be interrupted.
507[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000508
509static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300510signal_siginterrupt_impl(PyModuleDef *module, int signalnum, int flag)
511/*[clinic end generated code: output=5dcf8b031b0e8044 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000512{
Tal Einatc7027b72015-05-16 14:14:49 +0300513 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 PyErr_SetString(PyExc_ValueError,
515 "signal number out of range");
516 return NULL;
517 }
Tal Einatc7027b72015-05-16 14:14:49 +0300518 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200519 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return NULL;
521 }
Tal Einatc7027b72015-05-16 14:14:49 +0300522 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000523}
524
525#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000526
Tal Einatc7027b72015-05-16 14:14:49 +0300527
528static PyObject*
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000529signal_set_wakeup_fd(PyObject *self, PyObject *args)
530{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200531 struct _Py_stat_struct status;
Victor Stinner11517102014-07-29 23:31:34 +0200532#ifdef MS_WINDOWS
533 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100534 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200535 int res;
536 int res_size = sizeof res;
537 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200538 int is_socket;
539
540 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
541 return NULL;
542
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100543 sockfd = PyLong_AsSocket_t(fdobj);
544 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200545 return NULL;
546#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
550 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200551#endif
552
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000553#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (PyThread_get_thread_ident() != main_thread) {
555 PyErr_SetString(PyExc_ValueError,
556 "set_wakeup_fd only works in main thread");
557 return NULL;
558 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000559#endif
Victor Stinner0bffc942014-07-21 16:28:54 +0200560
Victor Stinner11517102014-07-29 23:31:34 +0200561#ifdef MS_WINDOWS
562 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100563 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200564 /* Import the _socket module to call WSAStartup() */
565 mod = PyImport_ImportModuleNoBlock("_socket");
566 if (mod == NULL)
567 return NULL;
568 Py_DECREF(mod);
569
570 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100571 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200572 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100573 int fd, err;
574
575 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200576 if (err != WSAENOTSOCK) {
577 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
578 return NULL;
579 }
580
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100581 fd = (int)sockfd;
582 if ((SOCKET_T)fd != sockfd || !_PyVerify_fd(fd)) {
Victor Stinner11517102014-07-29 23:31:34 +0200583 PyErr_SetString(PyExc_ValueError, "invalid fd");
584 return NULL;
585 }
586
Victor Stinnere134a7f2015-03-30 10:09:31 +0200587 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200588 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200589
590 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200591 }
Victor Stinner38227602014-08-27 12:59:44 +0200592 else {
Victor Stinner11517102014-07-29 23:31:34 +0200593 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200594
595 /* Windows does not provide a function to test if a socket
596 is in non-blocking mode */
597 }
Victor Stinner11517102014-07-29 23:31:34 +0200598 }
599
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100600 old_sockfd = wakeup.fd;
601 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200602 wakeup.use_send = is_socket;
603
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100604 if (old_sockfd != INVALID_FD)
605 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200606 else
607 return PyLong_FromLong(-1);
608#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200609 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200610 int blocking;
611
Victor Stinner1d8948e2014-07-24 22:51:05 +0200612 if (!_PyVerify_fd(fd)) {
613 PyErr_SetString(PyExc_ValueError, "invalid fd");
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200614 return NULL;
615 }
Victor Stinner1d8948e2014-07-24 22:51:05 +0200616
Victor Stinnere134a7f2015-03-30 10:09:31 +0200617 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200618 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200619
620 blocking = _Py_get_blocking(fd);
621 if (blocking < 0)
622 return NULL;
623 if (blocking) {
624 PyErr_Format(PyExc_ValueError,
625 "the fd %i must be in non-blocking mode",
626 fd);
627 return NULL;
628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 old_fd = wakeup_fd;
632 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200635#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000636}
637
638PyDoc_STRVAR(set_wakeup_fd_doc,
639"set_wakeup_fd(fd) -> fd\n\
640\n\
Victor Stinner11517102014-07-29 23:31:34 +0200641Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000642comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200643The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000644\n\
645The fd must be non-blocking.");
646
647/* C API for the same, without all the error checking */
648int
649PySignal_SetWakeupFd(int fd)
650{
Victor Stinner11517102014-07-29 23:31:34 +0200651 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (fd < 0)
653 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200654
655#ifdef MS_WINDOWS
656 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
657 wakeup.fd = fd;
658#else
659 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200661#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000663}
664
665
Martin v. Löwis823725e2008-03-24 13:39:54 +0000666#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300667
668/*[clinic input]
669signal.setitimer
670
671 which: int
672 seconds: double
673 interval: double = 0.0
674 /
675
676Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
677
678The timer will fire after value seconds and after that every interval seconds.
679The itimer can be cleared by setting seconds to zero.
680
681Returns old values as a tuple: (delay, interval).
682[clinic start generated code]*/
683
Martin v. Löwis823725e2008-03-24 13:39:54 +0000684static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300685signal_setitimer_impl(PyModuleDef *module, int which, double seconds,
686 double interval)
687/*[clinic end generated code: output=9a9227a27bd05988 input=0d27d417cfcbd51a]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000688{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000689 struct itimerval new, old;
690
Tal Einatc7027b72015-05-16 14:14:49 +0300691 timeval_from_double(seconds, &new.it_value);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000692 timeval_from_double(interval, &new.it_interval);
693 /* Let OS check "which" value */
694 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300695 PyErr_SetFromErrno(ItimerError);
696 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000697 }
698
699 return itimer_retval(&old);
700}
701
Martin v. Löwis823725e2008-03-24 13:39:54 +0000702#endif
703
704
705#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300706
707/*[clinic input]
708signal.getitimer
709
710 which: int
711 /
712
713Returns current value of given itimer.
714[clinic start generated code]*/
715
Martin v. Löwis823725e2008-03-24 13:39:54 +0000716static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300717signal_getitimer_impl(PyModuleDef *module, int which)
718/*[clinic end generated code: output=d1349ab18aadc569 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000719{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000720 struct itimerval old;
721
Martin v. Löwis823725e2008-03-24 13:39:54 +0000722 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300723 PyErr_SetFromErrno(ItimerError);
724 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000725 }
726
727 return itimer_retval(&old);
728}
729
Martin v. Löwis823725e2008-03-24 13:39:54 +0000730#endif
731
Ross Lagerwallbc808222011-06-25 12:13:40 +0200732#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
733 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200734/* Convert an iterable to a sigset.
735 Return 0 on success, return -1 and raise an exception on error. */
736
737static int
738iterable_to_sigset(PyObject *iterable, sigset_t *mask)
739{
740 int result = -1;
741 PyObject *iterator, *item;
742 long signum;
743 int err;
744
745 sigemptyset(mask);
746
747 iterator = PyObject_GetIter(iterable);
748 if (iterator == NULL)
749 goto error;
750
751 while (1)
752 {
753 item = PyIter_Next(iterator);
754 if (item == NULL) {
755 if (PyErr_Occurred())
756 goto error;
757 else
758 break;
759 }
760
761 signum = PyLong_AsLong(item);
762 Py_DECREF(item);
763 if (signum == -1 && PyErr_Occurred())
764 goto error;
765 if (0 < signum && signum < NSIG)
766 err = sigaddset(mask, (int)signum);
767 else
768 err = 1;
769 if (err) {
770 PyErr_Format(PyExc_ValueError,
771 "signal number %ld out of range", signum);
772 goto error;
773 }
774 }
775 result = 0;
776
777error:
778 Py_XDECREF(iterator);
779 return result;
780}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200781#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200782
Victor Stinnerb3e72192011-05-08 01:46:11 +0200783#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200784static PyObject*
785sigset_to_set(sigset_t mask)
786{
787 PyObject *signum, *result;
788 int sig;
789
790 result = PySet_New(0);
791 if (result == NULL)
792 return NULL;
793
794 for (sig = 1; sig < NSIG; sig++) {
795 if (sigismember(&mask, sig) != 1)
796 continue;
797
798 /* Handle the case where it is a member by adding the signal to
799 the result list. Ignore the other cases because they mean the
800 signal isn't a member of the mask or the signal was invalid,
801 and an invalid signal must have been our fault in constructing
802 the loop boundaries. */
803 signum = PyLong_FromLong(sig);
804 if (signum == NULL) {
805 Py_DECREF(result);
806 return NULL;
807 }
808 if (PySet_Add(result, signum) == -1) {
809 Py_DECREF(signum);
810 Py_DECREF(result);
811 return NULL;
812 }
813 Py_DECREF(signum);
814 }
815 return result;
816}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200817#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200818
Victor Stinnerb3e72192011-05-08 01:46:11 +0200819#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300820
821/*[clinic input]
822signal.pthread_sigmask
823
824 how: int
825 mask: object
826 /
827
828Fetch and/or change the signal mask of the calling thread.
829[clinic start generated code]*/
830
Victor Stinnera9293352011-04-30 15:21:58 +0200831static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300832signal_pthread_sigmask_impl(PyModuleDef *module, int how, PyObject *mask)
833/*[clinic end generated code: output=b043a9f0eeb1e075 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200834{
Tal Einatc7027b72015-05-16 14:14:49 +0300835 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200836 int err;
837
Tal Einatc7027b72015-05-16 14:14:49 +0300838 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200839 return NULL;
840
Tal Einatc7027b72015-05-16 14:14:49 +0300841 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200842 if (err != 0) {
843 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200844 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200845 return NULL;
846 }
847
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200848 /* if signals was unblocked, signal handlers have been called */
849 if (PyErr_CheckSignals())
850 return NULL;
851
Victor Stinner35b300c2011-05-04 13:20:35 +0200852 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200853}
854
Victor Stinnera9293352011-04-30 15:21:58 +0200855#endif /* #ifdef PYPTHREAD_SIGMASK */
856
Martin v. Löwis823725e2008-03-24 13:39:54 +0000857
Victor Stinnerb3e72192011-05-08 01:46:11 +0200858#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300859
860/*[clinic input]
861signal.sigpending
862
863Examine pending signals.
864
865Returns a set of signal numbers that are pending for delivery to
866the calling thread.
867[clinic start generated code]*/
868
Victor Stinnerb3e72192011-05-08 01:46:11 +0200869static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300870signal_sigpending_impl(PyModuleDef *module)
871/*[clinic end generated code: output=bf4ced803e7e51dd input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200872{
873 int err;
874 sigset_t mask;
875 err = sigpending(&mask);
876 if (err)
877 return PyErr_SetFromErrno(PyExc_OSError);
878 return sigset_to_set(mask);
879}
880
Victor Stinnerb3e72192011-05-08 01:46:11 +0200881#endif /* #ifdef HAVE_SIGPENDING */
882
883
884#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300885
886/*[clinic input]
887signal.sigwait
888
889 sigset: object
890 /
891
892Wait for a signal.
893
894Suspend execution of the calling thread until the delivery of one of the
895signals specified in the signal set sigset. The function accepts the signal
896and returns the signal number.
897[clinic start generated code]*/
898
Victor Stinnerb3e72192011-05-08 01:46:11 +0200899static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300900signal_sigwait(PyModuleDef *module, PyObject *sigset)
901/*[clinic end generated code: output=dae53048b0336a5c input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200902{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200903 sigset_t set;
904 int err, signum;
905
Tal Einatc7027b72015-05-16 14:14:49 +0300906 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907 return NULL;
908
Victor Stinner10c30d62011-06-10 01:39:53 +0200909 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200910 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200911 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200912 if (err) {
913 errno = err;
914 return PyErr_SetFromErrno(PyExc_OSError);
915 }
916
917 return PyLong_FromLong(signum);
918}
919
Tal Einatc7027b72015-05-16 14:14:49 +0300920#endif /* #ifdef HAVE_SIGWAIT */
921
Victor Stinnerb3e72192011-05-08 01:46:11 +0200922
Ross Lagerwallbc808222011-06-25 12:13:40 +0200923#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
924static int initialized;
925static PyStructSequence_Field struct_siginfo_fields[] = {
926 {"si_signo", "signal number"},
927 {"si_code", "signal code"},
928 {"si_errno", "errno associated with this signal"},
929 {"si_pid", "sending process ID"},
930 {"si_uid", "real user ID of sending process"},
931 {"si_status", "exit value or signal"},
932 {"si_band", "band event for SIGPOLL"},
933 {0}
934};
935
936PyDoc_STRVAR(struct_siginfo__doc__,
937"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
938This object may be accessed either as a tuple of\n\
939(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
940or via the attributes si_signo, si_code, and so on.");
941
942static PyStructSequence_Desc struct_siginfo_desc = {
943 "signal.struct_siginfo", /* name */
944 struct_siginfo__doc__, /* doc */
945 struct_siginfo_fields, /* fields */
946 7 /* n_in_sequence */
947};
948
949static PyTypeObject SiginfoType;
950
951static PyObject *
952fill_siginfo(siginfo_t *si)
953{
954 PyObject *result = PyStructSequence_New(&SiginfoType);
955 if (!result)
956 return NULL;
957
958 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
959 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
960 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
961 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200962 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200963 PyStructSequence_SET_ITEM(result, 5,
964 PyLong_FromLong((long)(si->si_status)));
965 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
966 if (PyErr_Occurred()) {
967 Py_DECREF(result);
968 return NULL;
969 }
970
971 return result;
972}
973#endif
974
975#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300976
977/*[clinic input]
978signal.sigwaitinfo
979
980 sigset: object
981 /
982
983Wait synchronously until one of the signals in *sigset* is delivered.
984
985Returns a struct_siginfo containing information about the signal.
986[clinic start generated code]*/
987
Ross Lagerwallbc808222011-06-25 12:13:40 +0200988static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +0300989signal_sigwaitinfo(PyModuleDef *module, PyObject *sigset)
990/*[clinic end generated code: output=0bb53b07e5e926b5 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +0200991{
Ross Lagerwallbc808222011-06-25 12:13:40 +0200992 sigset_t set;
993 siginfo_t si;
994 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +0100995 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200996
Tal Einatc7027b72015-05-16 14:14:49 +0300997 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200998 return NULL;
999
Victor Stinnera453cd82015-03-20 12:54:28 +01001000 do {
1001 Py_BEGIN_ALLOW_THREADS
1002 err = sigwaitinfo(&set, &si);
1003 Py_END_ALLOW_THREADS
1004 } while (err == -1
1005 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001006 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001007 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001008
1009 return fill_siginfo(&si);
1010}
1011
Ross Lagerwallbc808222011-06-25 12:13:40 +02001012#endif /* #ifdef HAVE_SIGWAITINFO */
1013
1014#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001015
1016/*[clinic input]
1017signal.sigtimedwait
1018
1019 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001020 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001021 /
1022
1023Like sigwaitinfo(), but with a timeout.
1024
1025The timeout is specified in seconds, with floating point numbers allowed.
1026[clinic start generated code]*/
1027
Ross Lagerwallbc808222011-06-25 12:13:40 +02001028static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +03001029signal_sigtimedwait_impl(PyModuleDef *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001030 PyObject *timeout_obj)
1031/*[clinic end generated code: output=c1960b5cea139929 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001032{
Victor Stinnera453cd82015-03-20 12:54:28 +01001033 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001034 sigset_t set;
1035 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001036 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001037 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001038
Victor Stinner869e1772015-03-30 03:49:14 +02001039 if (_PyTime_FromSecondsObject(&timeout,
1040 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001041 return NULL;
1042
Victor Stinnera453cd82015-03-20 12:54:28 +01001043 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001044 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1045 return NULL;
1046 }
1047
Tal Einatc7027b72015-05-16 14:14:49 +03001048 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001049 return NULL;
1050
Victor Stinner34dc0f42015-03-27 18:19:03 +01001051 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001052
1053 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001054 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1055 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001056
1057 Py_BEGIN_ALLOW_THREADS
1058 res = sigtimedwait(&set, &si, &ts);
1059 Py_END_ALLOW_THREADS
1060
1061 if (res != -1)
1062 break;
1063
1064 if (errno != EINTR) {
1065 if (errno == EAGAIN)
1066 Py_RETURN_NONE;
1067 else
1068 return PyErr_SetFromErrno(PyExc_OSError);
1069 }
1070
1071 /* sigtimedwait() was interrupted by a signal (EINTR) */
1072 if (PyErr_CheckSignals())
1073 return NULL;
1074
Victor Stinner34dc0f42015-03-27 18:19:03 +01001075 monotonic = _PyTime_GetMonotonicClock();
1076 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001077 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001078 break;
1079 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001080
1081 return fill_siginfo(&si);
1082}
1083
Ross Lagerwallbc808222011-06-25 12:13:40 +02001084#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1085
Victor Stinnerb3e72192011-05-08 01:46:11 +02001086
1087#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
Tal Einatc7027b72015-05-16 14:14:49 +03001088
1089/*[clinic input]
1090signal.pthread_kill
1091
1092 thread_id: long
1093 signalnum: int
1094 /
1095
1096Send a signal to a thread.
1097[clinic start generated code]*/
1098
Victor Stinnerb3e72192011-05-08 01:46:11 +02001099static PyObject *
Tal Einatc7027b72015-05-16 14:14:49 +03001100signal_pthread_kill_impl(PyModuleDef *module, long thread_id, int signalnum)
1101/*[clinic end generated code: output=35aed2713c756d7a input=77ed6a3b6f2a8122]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001102{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001103 int err;
1104
Tal Einatc7027b72015-05-16 14:14:49 +03001105 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001106 if (err != 0) {
1107 errno = err;
1108 PyErr_SetFromErrno(PyExc_OSError);
1109 return NULL;
1110 }
1111
1112 /* the signal may have been send to the current thread */
1113 if (PyErr_CheckSignals())
1114 return NULL;
1115
1116 Py_RETURN_NONE;
1117}
1118
Victor Stinnerb3e72192011-05-08 01:46:11 +02001119#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1120
1121
1122
Tal Einatc7027b72015-05-16 14:14:49 +03001123/* List of functions defined in the module -- some of the methoddefs are
1124 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001125static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001126 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1127 SIGNAL_ALARM_METHODDEF
1128 SIGNAL_SETITIMER_METHODDEF
1129 SIGNAL_GETITIMER_METHODDEF
1130 SIGNAL_SIGNAL_METHODDEF
1131 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001133 SIGNAL_SIGINTERRUPT_METHODDEF
1134 SIGNAL_PAUSE_METHODDEF
1135 SIGNAL_PTHREAD_KILL_METHODDEF
1136 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1137 SIGNAL_SIGPENDING_METHODDEF
1138 SIGNAL_SIGWAIT_METHODDEF
1139 SIGNAL_SIGWAITINFO_METHODDEF
1140 SIGNAL_SIGTIMEDWAIT_METHODDEF
1141 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001142};
1143
Barry Warsaw92971171997-01-03 00:14:25 +00001144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001146"This module provides mechanisms to use signal handlers in Python.\n\
1147\n\
1148Functions:\n\
1149\n\
1150alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001151setitimer() -- cause a signal (described below) after a specified\n\
1152 float time and the timer may restart then [Unix only]\n\
1153getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001154signal() -- set the action for a given signal\n\
1155getsignal() -- get the signal action for a given signal\n\
1156pause() -- wait until a signal arrives [Unix only]\n\
1157default_int_handler() -- default SIGINT handler\n\
1158\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001159signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001160SIG_DFL -- used to refer to the system default handler\n\
1161SIG_IGN -- used to ignore the signal\n\
1162NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001163SIGINT, SIGTERM, etc. -- signal numbers\n\
1164\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001165itimer constants:\n\
1166ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1167 expiration\n\
1168ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1169 and delivers SIGVTALRM upon expiration\n\
1170ITIMER_PROF -- decrements both when the process is executing and\n\
1171 when the system is executing on behalf of the process.\n\
1172 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1173 used to profile the time spent by the application\n\
1174 in user and kernel space. SIGPROF is delivered upon\n\
1175 expiration.\n\
1176\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001177*** IMPORTANT NOTICE ***\n\
1178A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001179the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001180
Martin v. Löwis1a214512008-06-11 05:26:20 +00001181static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001183 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 module_doc,
1185 -1,
1186 signal_methods,
1187 NULL,
1188 NULL,
1189 NULL,
1190 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001191};
1192
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001193PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001194PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 PyObject *m, *d, *x;
1197 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001198
1199#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 main_thread = PyThread_get_thread_ident();
1201 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001202#endif
1203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 /* Create the module and add the functions */
1205 m = PyModule_Create(&signalmodule);
1206 if (m == NULL)
1207 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001208
Ross Lagerwallbc808222011-06-25 12:13:40 +02001209#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001210 if (!initialized) {
1211 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1212 return NULL;
1213 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001214 Py_INCREF((PyObject*) &SiginfoType);
1215 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1216 initialized = 1;
1217#endif
1218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 /* Add some symbolic constants to the module */
1220 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1223 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1224 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1227 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1228 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 x = PyLong_FromLong((long)NSIG);
1231 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1232 goto finally;
1233 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001234
Victor Stinnera9293352011-04-30 15:21:58 +02001235#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001236 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1237 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001238#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001239#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001240 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1241 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001242#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001243#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001244 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1245 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001246#endif
1247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1249 if (!x)
1250 goto finally;
1251 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Handlers[0].tripped = 0;
1254 for (i = 1; i < NSIG; i++) {
1255 void (*t)(int);
1256 t = PyOS_getsig(i);
1257 Handlers[i].tripped = 0;
1258 if (t == SIG_DFL)
1259 Handlers[i].func = DefaultHandler;
1260 else if (t == SIG_IGN)
1261 Handlers[i].func = IgnoreHandler;
1262 else
1263 Handlers[i].func = Py_None; /* None of our business */
1264 Py_INCREF(Handlers[i].func);
1265 }
1266 if (Handlers[SIGINT].func == DefaultHandler) {
1267 /* Install default int handler */
1268 Py_INCREF(IntHandler);
1269 Py_DECREF(Handlers[SIGINT].func);
1270 Handlers[SIGINT].func = IntHandler;
1271 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1272 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001273
1274#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 x = PyLong_FromLong(SIGHUP);
1276 PyDict_SetItemString(d, "SIGHUP", x);
1277 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001278#endif
1279#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 x = PyLong_FromLong(SIGINT);
1281 PyDict_SetItemString(d, "SIGINT", x);
1282 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001283#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001284#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 x = PyLong_FromLong(SIGBREAK);
1286 PyDict_SetItemString(d, "SIGBREAK", x);
1287 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001288#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001289#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 x = PyLong_FromLong(SIGQUIT);
1291 PyDict_SetItemString(d, "SIGQUIT", x);
1292 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001293#endif
1294#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 x = PyLong_FromLong(SIGILL);
1296 PyDict_SetItemString(d, "SIGILL", x);
1297 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001298#endif
1299#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 x = PyLong_FromLong(SIGTRAP);
1301 PyDict_SetItemString(d, "SIGTRAP", x);
1302 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303#endif
1304#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 x = PyLong_FromLong(SIGIOT);
1306 PyDict_SetItemString(d, "SIGIOT", x);
1307 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001308#endif
1309#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 x = PyLong_FromLong(SIGABRT);
1311 PyDict_SetItemString(d, "SIGABRT", x);
1312 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001313#endif
1314#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 x = PyLong_FromLong(SIGEMT);
1316 PyDict_SetItemString(d, "SIGEMT", x);
1317 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001318#endif
1319#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 x = PyLong_FromLong(SIGFPE);
1321 PyDict_SetItemString(d, "SIGFPE", x);
1322 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001323#endif
1324#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 x = PyLong_FromLong(SIGKILL);
1326 PyDict_SetItemString(d, "SIGKILL", x);
1327 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001328#endif
1329#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 x = PyLong_FromLong(SIGBUS);
1331 PyDict_SetItemString(d, "SIGBUS", x);
1332 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001333#endif
1334#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 x = PyLong_FromLong(SIGSEGV);
1336 PyDict_SetItemString(d, "SIGSEGV", x);
1337 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001338#endif
1339#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 x = PyLong_FromLong(SIGSYS);
1341 PyDict_SetItemString(d, "SIGSYS", x);
1342 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001343#endif
1344#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 x = PyLong_FromLong(SIGPIPE);
1346 PyDict_SetItemString(d, "SIGPIPE", x);
1347 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001348#endif
1349#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 x = PyLong_FromLong(SIGALRM);
1351 PyDict_SetItemString(d, "SIGALRM", x);
1352 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001353#endif
1354#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 x = PyLong_FromLong(SIGTERM);
1356 PyDict_SetItemString(d, "SIGTERM", x);
1357 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001358#endif
1359#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 x = PyLong_FromLong(SIGUSR1);
1361 PyDict_SetItemString(d, "SIGUSR1", x);
1362 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001363#endif
1364#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 x = PyLong_FromLong(SIGUSR2);
1366 PyDict_SetItemString(d, "SIGUSR2", x);
1367 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001368#endif
1369#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 x = PyLong_FromLong(SIGCLD);
1371 PyDict_SetItemString(d, "SIGCLD", x);
1372 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373#endif
1374#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 x = PyLong_FromLong(SIGCHLD);
1376 PyDict_SetItemString(d, "SIGCHLD", x);
1377 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001378#endif
1379#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 x = PyLong_FromLong(SIGPWR);
1381 PyDict_SetItemString(d, "SIGPWR", x);
1382 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383#endif
1384#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 x = PyLong_FromLong(SIGIO);
1386 PyDict_SetItemString(d, "SIGIO", x);
1387 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001388#endif
1389#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 x = PyLong_FromLong(SIGURG);
1391 PyDict_SetItemString(d, "SIGURG", x);
1392 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#endif
1394#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 x = PyLong_FromLong(SIGWINCH);
1396 PyDict_SetItemString(d, "SIGWINCH", x);
1397 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001398#endif
1399#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 x = PyLong_FromLong(SIGPOLL);
1401 PyDict_SetItemString(d, "SIGPOLL", x);
1402 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403#endif
1404#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 x = PyLong_FromLong(SIGSTOP);
1406 PyDict_SetItemString(d, "SIGSTOP", x);
1407 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001408#endif
1409#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 x = PyLong_FromLong(SIGTSTP);
1411 PyDict_SetItemString(d, "SIGTSTP", x);
1412 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 x = PyLong_FromLong(SIGCONT);
1416 PyDict_SetItemString(d, "SIGCONT", x);
1417 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001418#endif
1419#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 x = PyLong_FromLong(SIGTTIN);
1421 PyDict_SetItemString(d, "SIGTTIN", x);
1422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001423#endif
1424#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 x = PyLong_FromLong(SIGTTOU);
1426 PyDict_SetItemString(d, "SIGTTOU", x);
1427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001428#endif
1429#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 x = PyLong_FromLong(SIGVTALRM);
1431 PyDict_SetItemString(d, "SIGVTALRM", x);
1432 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001433#endif
1434#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 x = PyLong_FromLong(SIGPROF);
1436 PyDict_SetItemString(d, "SIGPROF", x);
1437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001438#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001439#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 x = PyLong_FromLong(SIGXCPU);
1441 PyDict_SetItemString(d, "SIGXCPU", x);
1442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001443#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001444#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 x = PyLong_FromLong(SIGXFSZ);
1446 PyDict_SetItemString(d, "SIGXFSZ", x);
1447 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001448#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001449#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 x = PyLong_FromLong(SIGRTMIN);
1451 PyDict_SetItemString(d, "SIGRTMIN", x);
1452 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001453#endif
1454#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 x = PyLong_FromLong(SIGRTMAX);
1456 PyDict_SetItemString(d, "SIGRTMAX", x);
1457 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001458#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001459#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 x = PyLong_FromLong(SIGINFO);
1461 PyDict_SetItemString(d, "SIGINFO", x);
1462 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001463#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001464
1465#ifdef ITIMER_REAL
1466 x = PyLong_FromLong(ITIMER_REAL);
1467 PyDict_SetItemString(d, "ITIMER_REAL", x);
1468 Py_DECREF(x);
1469#endif
1470#ifdef ITIMER_VIRTUAL
1471 x = PyLong_FromLong(ITIMER_VIRTUAL);
1472 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1473 Py_DECREF(x);
1474#endif
1475#ifdef ITIMER_PROF
1476 x = PyLong_FromLong(ITIMER_PROF);
1477 PyDict_SetItemString(d, "ITIMER_PROF", x);
1478 Py_DECREF(x);
1479#endif
1480
1481#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 ItimerError = PyErr_NewException("signal.ItimerError",
1483 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001484 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001486#endif
1487
Brian Curtineb24d742010-04-12 17:16:38 +00001488#ifdef CTRL_C_EVENT
1489 x = PyLong_FromLong(CTRL_C_EVENT);
1490 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1491 Py_DECREF(x);
1492#endif
1493
1494#ifdef CTRL_BREAK_EVENT
1495 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1496 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1497 Py_DECREF(x);
1498#endif
1499
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001500#ifdef MS_WINDOWS
1501 /* Create manual-reset event, initially unset */
1502 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1503#endif
1504
Martin v. Löwis1a214512008-06-11 05:26:20 +00001505 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 Py_DECREF(m);
1507 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001508 }
Barry Warsaw92971171997-01-03 00:14:25 +00001509
Barry Warsaw92971171997-01-03 00:14:25 +00001510 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001511 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001512}
1513
1514static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001515finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 int i;
1518 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 PyOS_setsig(SIGINT, old_siginthandler);
1521 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 for (i = 1; i < NSIG; i++) {
1524 func = Handlers[i].func;
1525 Handlers[i].tripped = 0;
1526 Handlers[i].func = NULL;
1527 if (i != SIGINT && func != NULL && func != Py_None &&
1528 func != DefaultHandler && func != IgnoreHandler)
1529 PyOS_setsig(i, SIG_DFL);
1530 Py_XDECREF(func);
1531 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001532
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001533 Py_CLEAR(IntHandler);
1534 Py_CLEAR(DefaultHandler);
1535 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001536}
1537
Barry Warsaw92971171997-01-03 00:14:25 +00001538
Barry Warsaw92971171997-01-03 00:14:25 +00001539/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001540int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001541PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 int i;
1544 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (!is_tripped)
1547 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001548
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001549#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (PyThread_get_thread_ident() != main_thread)
1551 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001552#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 /*
1555 * The is_tripped variable is meant to speed up the calls to
1556 * PyErr_CheckSignals (both directly or via pending calls) when no
1557 * signal has arrived. This variable is set to 1 when a signal arrives
1558 * and it is set to 0 here, when we know some signals arrived. This way
1559 * we can run the registered handlers with no signals blocked.
1560 *
1561 * NOTE: with this approach we can have a situation where is_tripped is
1562 * 1 but we have no more signals to handle (Handlers[i].tripped
1563 * is 0 for every signal i). This won't do us any harm (except
1564 * we're gonna spent some cycles for nothing). This happens when
1565 * we receive a signal i after we zero is_tripped and before we
1566 * check Handlers[i].tripped.
1567 */
1568 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (!(f = (PyObject *)PyEval_GetFrame()))
1571 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 for (i = 1; i < NSIG; i++) {
1574 if (Handlers[i].tripped) {
1575 PyObject *result = NULL;
1576 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1577 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (arglist) {
1580 result = PyEval_CallObject(Handlers[i].func,
1581 arglist);
1582 Py_DECREF(arglist);
1583 }
1584 if (!result)
1585 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 Py_DECREF(result);
1588 }
1589 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001592}
1593
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001594
Barry Warsaw92971171997-01-03 00:14:25 +00001595/* Replacements for intrcheck.c functionality
1596 * Declared in pyerrors.h
1597 */
1598void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001599PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001600{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001601 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001602}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001603
1604void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001605PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001606{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001607 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 Py_DECREF(m);
1610 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001611}
1612
1613void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001614PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001617}
1618
1619int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001620PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001623#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (PyThread_get_thread_ident() != main_thread)
1625 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001626#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Handlers[SIGINT].tripped = 0;
1628 return 1;
1629 }
1630 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001631}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001632
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001633static void
1634_clear_pending_signals(void)
1635{
1636 int i;
1637 if (!is_tripped)
1638 return;
1639 is_tripped = 0;
1640 for (i = 1; i < NSIG; ++i) {
1641 Handlers[i].tripped = 0;
1642 }
1643}
1644
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001645void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001646PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001647{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001648 /* Clear the signal flags after forking so that they aren't handled
1649 * in both processes if they came in just before the fork() but before
1650 * the interpreter had an opportunity to call the handlers. issue9535. */
1651 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001652#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001653 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1654 * can be called safely. */
1655 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001656 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 PyEval_ReInitThreads();
1658 main_thread = PyThread_get_thread_ident();
1659 main_pid = getpid();
1660 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001661#endif
1662}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001663
1664int
1665_PyOS_IsMainThread(void)
1666{
1667#ifdef WITH_THREAD
1668 return PyThread_get_thread_ident() == main_thread;
1669#else
1670 return 1;
1671#endif
1672}
1673
1674#ifdef MS_WINDOWS
1675void *_PyOS_SigintEvent(void)
1676{
1677 /* Returns a manual-reset event which gets tripped whenever
1678 SIGINT is received.
1679
1680 Python.h does not include windows.h so we do cannot use HANDLE
1681 as the return type of this function. We use void* instead. */
1682 return sigint_event;
1683}
1684#endif