blob: e27075b200194610d07c0fd3aba3ad1f45b36305 [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)
Martin Panter6d57fe12016-09-17 03:26:16 +0000157 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158
159 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +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)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +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;
Benjamin Petersonca470632016-09-06 13:47:26 -0700201 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200202 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,
Benjamin Petersonca470632016-09-06 13:47:26 -0700280 (void *)(intptr_t)errno);
Victor Stinner11517102014-07-29 23:31:34 +0200281 }
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
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300343signal_alarm_impl(PyObject *module, int seconds)
344/*[clinic end generated code: output=144232290814c298 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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300361signal_pause_impl(PyObject *module)
362/*[clinic end generated code: output=391656788b3c3929 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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300397signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
398/*[clinic end generated code: output=b44cfda43780f3a1 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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300475signal_getsignal_impl(PyObject *module, int signalnum)
476/*[clinic end generated code: output=35b3e0e796fd555e 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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300510signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
511/*[clinic end generated code: output=063816243d85dd19 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;
Steve Dower940f33a2016-09-08 11:21:54 -0700582 if ((SOCKET_T)fd != sockfd) {
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 Stinnere134a7f2015-03-30 10:09:31 +0200612 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200613 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200614
615 blocking = _Py_get_blocking(fd);
616 if (blocking < 0)
617 return NULL;
618 if (blocking) {
619 PyErr_Format(PyExc_ValueError,
620 "the fd %i must be in non-blocking mode",
621 fd);
622 return NULL;
623 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 old_fd = wakeup_fd;
627 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200630#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000631}
632
633PyDoc_STRVAR(set_wakeup_fd_doc,
634"set_wakeup_fd(fd) -> fd\n\
635\n\
Victor Stinner11517102014-07-29 23:31:34 +0200636Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000637comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200638The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000639\n\
640The fd must be non-blocking.");
641
642/* C API for the same, without all the error checking */
643int
644PySignal_SetWakeupFd(int fd)
645{
Victor Stinner11517102014-07-29 23:31:34 +0200646 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (fd < 0)
648 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200649
650#ifdef MS_WINDOWS
651 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
652 wakeup.fd = fd;
653#else
654 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200656#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000658}
659
660
Martin v. Löwis823725e2008-03-24 13:39:54 +0000661#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300662
663/*[clinic input]
664signal.setitimer
665
666 which: int
667 seconds: double
668 interval: double = 0.0
669 /
670
671Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
672
673The timer will fire after value seconds and after that every interval seconds.
674The itimer can be cleared by setting seconds to zero.
675
676Returns old values as a tuple: (delay, interval).
677[clinic start generated code]*/
678
Martin v. Löwis823725e2008-03-24 13:39:54 +0000679static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300680signal_setitimer_impl(PyObject *module, int which, double seconds,
Tal Einatc7027b72015-05-16 14:14:49 +0300681 double interval)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300682/*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000683{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000684 struct itimerval new, old;
685
Tal Einatc7027b72015-05-16 14:14:49 +0300686 timeval_from_double(seconds, &new.it_value);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000687 timeval_from_double(interval, &new.it_interval);
688 /* Let OS check "which" value */
689 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300690 PyErr_SetFromErrno(ItimerError);
691 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000692 }
693
694 return itimer_retval(&old);
695}
696
Martin v. Löwis823725e2008-03-24 13:39:54 +0000697#endif
698
699
700#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300701
702/*[clinic input]
703signal.getitimer
704
705 which: int
706 /
707
708Returns current value of given itimer.
709[clinic start generated code]*/
710
Martin v. Löwis823725e2008-03-24 13:39:54 +0000711static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300712signal_getitimer_impl(PyObject *module, int which)
713/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000714{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000715 struct itimerval old;
716
Martin v. Löwis823725e2008-03-24 13:39:54 +0000717 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300718 PyErr_SetFromErrno(ItimerError);
719 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000720 }
721
722 return itimer_retval(&old);
723}
724
Martin v. Löwis823725e2008-03-24 13:39:54 +0000725#endif
726
Ross Lagerwallbc808222011-06-25 12:13:40 +0200727#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
728 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200729/* Convert an iterable to a sigset.
730 Return 0 on success, return -1 and raise an exception on error. */
731
732static int
733iterable_to_sigset(PyObject *iterable, sigset_t *mask)
734{
735 int result = -1;
736 PyObject *iterator, *item;
737 long signum;
738 int err;
739
740 sigemptyset(mask);
741
742 iterator = PyObject_GetIter(iterable);
743 if (iterator == NULL)
744 goto error;
745
746 while (1)
747 {
748 item = PyIter_Next(iterator);
749 if (item == NULL) {
750 if (PyErr_Occurred())
751 goto error;
752 else
753 break;
754 }
755
756 signum = PyLong_AsLong(item);
757 Py_DECREF(item);
758 if (signum == -1 && PyErr_Occurred())
759 goto error;
760 if (0 < signum && signum < NSIG)
761 err = sigaddset(mask, (int)signum);
762 else
763 err = 1;
764 if (err) {
765 PyErr_Format(PyExc_ValueError,
766 "signal number %ld out of range", signum);
767 goto error;
768 }
769 }
770 result = 0;
771
772error:
773 Py_XDECREF(iterator);
774 return result;
775}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200776#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200777
Victor Stinnerb3e72192011-05-08 01:46:11 +0200778#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200779static PyObject*
780sigset_to_set(sigset_t mask)
781{
782 PyObject *signum, *result;
783 int sig;
784
785 result = PySet_New(0);
786 if (result == NULL)
787 return NULL;
788
789 for (sig = 1; sig < NSIG; sig++) {
790 if (sigismember(&mask, sig) != 1)
791 continue;
792
793 /* Handle the case where it is a member by adding the signal to
794 the result list. Ignore the other cases because they mean the
795 signal isn't a member of the mask or the signal was invalid,
796 and an invalid signal must have been our fault in constructing
797 the loop boundaries. */
798 signum = PyLong_FromLong(sig);
799 if (signum == NULL) {
800 Py_DECREF(result);
801 return NULL;
802 }
803 if (PySet_Add(result, signum) == -1) {
804 Py_DECREF(signum);
805 Py_DECREF(result);
806 return NULL;
807 }
808 Py_DECREF(signum);
809 }
810 return result;
811}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200812#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200813
Victor Stinnerb3e72192011-05-08 01:46:11 +0200814#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300815
816/*[clinic input]
817signal.pthread_sigmask
818
819 how: int
820 mask: object
821 /
822
823Fetch and/or change the signal mask of the calling thread.
824[clinic start generated code]*/
825
Victor Stinnera9293352011-04-30 15:21:58 +0200826static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300827signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
828/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200829{
Tal Einatc7027b72015-05-16 14:14:49 +0300830 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200831 int err;
832
Tal Einatc7027b72015-05-16 14:14:49 +0300833 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200834 return NULL;
835
Tal Einatc7027b72015-05-16 14:14:49 +0300836 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200837 if (err != 0) {
838 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200839 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200840 return NULL;
841 }
842
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200843 /* if signals was unblocked, signal handlers have been called */
844 if (PyErr_CheckSignals())
845 return NULL;
846
Victor Stinner35b300c2011-05-04 13:20:35 +0200847 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200848}
849
Victor Stinnera9293352011-04-30 15:21:58 +0200850#endif /* #ifdef PYPTHREAD_SIGMASK */
851
Martin v. Löwis823725e2008-03-24 13:39:54 +0000852
Victor Stinnerb3e72192011-05-08 01:46:11 +0200853#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300854
855/*[clinic input]
856signal.sigpending
857
858Examine pending signals.
859
860Returns a set of signal numbers that are pending for delivery to
861the calling thread.
862[clinic start generated code]*/
863
Victor Stinnerb3e72192011-05-08 01:46:11 +0200864static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300865signal_sigpending_impl(PyObject *module)
866/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200867{
868 int err;
869 sigset_t mask;
870 err = sigpending(&mask);
871 if (err)
872 return PyErr_SetFromErrno(PyExc_OSError);
873 return sigset_to_set(mask);
874}
875
Victor Stinnerb3e72192011-05-08 01:46:11 +0200876#endif /* #ifdef HAVE_SIGPENDING */
877
878
879#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300880
881/*[clinic input]
882signal.sigwait
883
884 sigset: object
885 /
886
887Wait for a signal.
888
889Suspend execution of the calling thread until the delivery of one of the
890signals specified in the signal set sigset. The function accepts the signal
891and returns the signal number.
892[clinic start generated code]*/
893
Victor Stinnerb3e72192011-05-08 01:46:11 +0200894static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300895signal_sigwait(PyObject *module, PyObject *sigset)
896/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200897{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200898 sigset_t set;
899 int err, signum;
900
Tal Einatc7027b72015-05-16 14:14:49 +0300901 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200902 return NULL;
903
Victor Stinner10c30d62011-06-10 01:39:53 +0200904 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200905 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200906 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907 if (err) {
908 errno = err;
909 return PyErr_SetFromErrno(PyExc_OSError);
910 }
911
912 return PyLong_FromLong(signum);
913}
914
Tal Einatc7027b72015-05-16 14:14:49 +0300915#endif /* #ifdef HAVE_SIGWAIT */
916
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917
Ross Lagerwallbc808222011-06-25 12:13:40 +0200918#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
919static int initialized;
920static PyStructSequence_Field struct_siginfo_fields[] = {
921 {"si_signo", "signal number"},
922 {"si_code", "signal code"},
923 {"si_errno", "errno associated with this signal"},
924 {"si_pid", "sending process ID"},
925 {"si_uid", "real user ID of sending process"},
926 {"si_status", "exit value or signal"},
927 {"si_band", "band event for SIGPOLL"},
928 {0}
929};
930
931PyDoc_STRVAR(struct_siginfo__doc__,
932"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
933This object may be accessed either as a tuple of\n\
934(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
935or via the attributes si_signo, si_code, and so on.");
936
937static PyStructSequence_Desc struct_siginfo_desc = {
938 "signal.struct_siginfo", /* name */
939 struct_siginfo__doc__, /* doc */
940 struct_siginfo_fields, /* fields */
941 7 /* n_in_sequence */
942};
943
944static PyTypeObject SiginfoType;
945
946static PyObject *
947fill_siginfo(siginfo_t *si)
948{
949 PyObject *result = PyStructSequence_New(&SiginfoType);
950 if (!result)
951 return NULL;
952
953 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
954 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
955 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
956 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200957 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200958 PyStructSequence_SET_ITEM(result, 5,
959 PyLong_FromLong((long)(si->si_status)));
960 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
961 if (PyErr_Occurred()) {
962 Py_DECREF(result);
963 return NULL;
964 }
965
966 return result;
967}
968#endif
969
970#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300971
972/*[clinic input]
973signal.sigwaitinfo
974
975 sigset: object
976 /
977
978Wait synchronously until one of the signals in *sigset* is delivered.
979
980Returns a struct_siginfo containing information about the signal.
981[clinic start generated code]*/
982
Ross Lagerwallbc808222011-06-25 12:13:40 +0200983static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300984signal_sigwaitinfo(PyObject *module, PyObject *sigset)
985/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +0200986{
Ross Lagerwallbc808222011-06-25 12:13:40 +0200987 sigset_t set;
988 siginfo_t si;
989 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +0100990 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200991
Tal Einatc7027b72015-05-16 14:14:49 +0300992 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200993 return NULL;
994
Victor Stinnera453cd82015-03-20 12:54:28 +0100995 do {
996 Py_BEGIN_ALLOW_THREADS
997 err = sigwaitinfo(&set, &si);
998 Py_END_ALLOW_THREADS
999 } while (err == -1
1000 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001001 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001002 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001003
1004 return fill_siginfo(&si);
1005}
1006
Ross Lagerwallbc808222011-06-25 12:13:40 +02001007#endif /* #ifdef HAVE_SIGWAITINFO */
1008
1009#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001010
1011/*[clinic input]
1012signal.sigtimedwait
1013
1014 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001015 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001016 /
1017
1018Like sigwaitinfo(), but with a timeout.
1019
1020The timeout is specified in seconds, with floating point numbers allowed.
1021[clinic start generated code]*/
1022
Ross Lagerwallbc808222011-06-25 12:13:40 +02001023static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001024signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001025 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001026/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001027{
Victor Stinnera453cd82015-03-20 12:54:28 +01001028 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001029 sigset_t set;
1030 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001031 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001032 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001033
Victor Stinner869e1772015-03-30 03:49:14 +02001034 if (_PyTime_FromSecondsObject(&timeout,
1035 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001036 return NULL;
1037
Victor Stinnera453cd82015-03-20 12:54:28 +01001038 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001039 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1040 return NULL;
1041 }
1042
Tal Einatc7027b72015-05-16 14:14:49 +03001043 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001044 return NULL;
1045
Victor Stinner34dc0f42015-03-27 18:19:03 +01001046 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001047
1048 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001049 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1050 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001051
1052 Py_BEGIN_ALLOW_THREADS
1053 res = sigtimedwait(&set, &si, &ts);
1054 Py_END_ALLOW_THREADS
1055
1056 if (res != -1)
1057 break;
1058
1059 if (errno != EINTR) {
1060 if (errno == EAGAIN)
1061 Py_RETURN_NONE;
1062 else
1063 return PyErr_SetFromErrno(PyExc_OSError);
1064 }
1065
1066 /* sigtimedwait() was interrupted by a signal (EINTR) */
1067 if (PyErr_CheckSignals())
1068 return NULL;
1069
Victor Stinner34dc0f42015-03-27 18:19:03 +01001070 monotonic = _PyTime_GetMonotonicClock();
1071 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001072 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001073 break;
1074 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001075
1076 return fill_siginfo(&si);
1077}
1078
Ross Lagerwallbc808222011-06-25 12:13:40 +02001079#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1080
Victor Stinnerb3e72192011-05-08 01:46:11 +02001081
1082#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
Tal Einatc7027b72015-05-16 14:14:49 +03001083
1084/*[clinic input]
1085signal.pthread_kill
1086
1087 thread_id: long
1088 signalnum: int
1089 /
1090
1091Send a signal to a thread.
1092[clinic start generated code]*/
1093
Victor Stinnerb3e72192011-05-08 01:46:11 +02001094static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001095signal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum)
1096/*[clinic end generated code: output=2a09ce41f1c4228a input=77ed6a3b6f2a8122]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001097{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001098 int err;
1099
Tal Einatc7027b72015-05-16 14:14:49 +03001100 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001101 if (err != 0) {
1102 errno = err;
1103 PyErr_SetFromErrno(PyExc_OSError);
1104 return NULL;
1105 }
1106
1107 /* the signal may have been send to the current thread */
1108 if (PyErr_CheckSignals())
1109 return NULL;
1110
1111 Py_RETURN_NONE;
1112}
1113
Victor Stinnerb3e72192011-05-08 01:46:11 +02001114#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1115
1116
1117
Tal Einatc7027b72015-05-16 14:14:49 +03001118/* List of functions defined in the module -- some of the methoddefs are
1119 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001120static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001121 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1122 SIGNAL_ALARM_METHODDEF
1123 SIGNAL_SETITIMER_METHODDEF
1124 SIGNAL_GETITIMER_METHODDEF
1125 SIGNAL_SIGNAL_METHODDEF
1126 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001128 SIGNAL_SIGINTERRUPT_METHODDEF
1129 SIGNAL_PAUSE_METHODDEF
1130 SIGNAL_PTHREAD_KILL_METHODDEF
1131 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1132 SIGNAL_SIGPENDING_METHODDEF
1133 SIGNAL_SIGWAIT_METHODDEF
1134 SIGNAL_SIGWAITINFO_METHODDEF
1135 SIGNAL_SIGTIMEDWAIT_METHODDEF
1136 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001137};
1138
Barry Warsaw92971171997-01-03 00:14:25 +00001139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001140PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001141"This module provides mechanisms to use signal handlers in Python.\n\
1142\n\
1143Functions:\n\
1144\n\
1145alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001146setitimer() -- cause a signal (described below) after a specified\n\
1147 float time and the timer may restart then [Unix only]\n\
1148getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001149signal() -- set the action for a given signal\n\
1150getsignal() -- get the signal action for a given signal\n\
1151pause() -- wait until a signal arrives [Unix only]\n\
1152default_int_handler() -- default SIGINT handler\n\
1153\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001154signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001155SIG_DFL -- used to refer to the system default handler\n\
1156SIG_IGN -- used to ignore the signal\n\
1157NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001158SIGINT, SIGTERM, etc. -- signal numbers\n\
1159\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001160itimer constants:\n\
1161ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1162 expiration\n\
1163ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1164 and delivers SIGVTALRM upon expiration\n\
1165ITIMER_PROF -- decrements both when the process is executing and\n\
1166 when the system is executing on behalf of the process.\n\
1167 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1168 used to profile the time spent by the application\n\
1169 in user and kernel space. SIGPROF is delivered upon\n\
1170 expiration.\n\
1171\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001172*** IMPORTANT NOTICE ***\n\
1173A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001175
Martin v. Löwis1a214512008-06-11 05:26:20 +00001176static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001178 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 module_doc,
1180 -1,
1181 signal_methods,
1182 NULL,
1183 NULL,
1184 NULL,
1185 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001186};
1187
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001188PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001189PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 PyObject *m, *d, *x;
1192 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001193
1194#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 main_thread = PyThread_get_thread_ident();
1196 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001197#endif
1198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 /* Create the module and add the functions */
1200 m = PyModule_Create(&signalmodule);
1201 if (m == NULL)
1202 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001203
Ross Lagerwallbc808222011-06-25 12:13:40 +02001204#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001205 if (!initialized) {
1206 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1207 return NULL;
1208 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001209 Py_INCREF((PyObject*) &SiginfoType);
1210 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1211 initialized = 1;
1212#endif
1213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 /* Add some symbolic constants to the module */
1215 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1218 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1219 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1222 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1223 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 x = PyLong_FromLong((long)NSIG);
1226 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1227 goto finally;
1228 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001229
Victor Stinnera9293352011-04-30 15:21:58 +02001230#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001231 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1232 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001233#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001234#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001235 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1236 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001237#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001238#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001239 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1240 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001241#endif
1242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1244 if (!x)
1245 goto finally;
1246 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Handlers[0].tripped = 0;
1249 for (i = 1; i < NSIG; i++) {
1250 void (*t)(int);
1251 t = PyOS_getsig(i);
1252 Handlers[i].tripped = 0;
1253 if (t == SIG_DFL)
1254 Handlers[i].func = DefaultHandler;
1255 else if (t == SIG_IGN)
1256 Handlers[i].func = IgnoreHandler;
1257 else
1258 Handlers[i].func = Py_None; /* None of our business */
1259 Py_INCREF(Handlers[i].func);
1260 }
1261 if (Handlers[SIGINT].func == DefaultHandler) {
1262 /* Install default int handler */
1263 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001264 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1266 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001267
1268#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001269 if (PyModule_AddIntMacro(m, SIGHUP))
1270 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001271#endif
1272#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001273 if (PyModule_AddIntMacro(m, SIGINT))
1274 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001275#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001276#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001277 if (PyModule_AddIntMacro(m, SIGBREAK))
1278 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001279#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001280#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001281 if (PyModule_AddIntMacro(m, SIGQUIT))
1282 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001283#endif
1284#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001285 if (PyModule_AddIntMacro(m, SIGILL))
1286 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001287#endif
1288#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001289 if (PyModule_AddIntMacro(m, SIGTRAP))
1290 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001291#endif
1292#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001293 if (PyModule_AddIntMacro(m, SIGIOT))
1294 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001295#endif
1296#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001297 if (PyModule_AddIntMacro(m, SIGABRT))
1298 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299#endif
1300#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001301 if (PyModule_AddIntMacro(m, SIGEMT))
1302 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303#endif
1304#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001305 if (PyModule_AddIntMacro(m, SIGFPE))
1306 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001307#endif
1308#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001309 if (PyModule_AddIntMacro(m, SIGKILL))
1310 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001311#endif
1312#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001313 if (PyModule_AddIntMacro(m, SIGBUS))
1314 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001315#endif
1316#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001317 if (PyModule_AddIntMacro(m, SIGSEGV))
1318 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001319#endif
1320#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001321 if (PyModule_AddIntMacro(m, SIGSYS))
1322 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001323#endif
1324#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001325 if (PyModule_AddIntMacro(m, SIGPIPE))
1326 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001327#endif
1328#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001329 if (PyModule_AddIntMacro(m, SIGALRM))
1330 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001331#endif
1332#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001333 if (PyModule_AddIntMacro(m, SIGTERM))
1334 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001335#endif
1336#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001337 if (PyModule_AddIntMacro(m, SIGUSR1))
1338 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339#endif
1340#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001341 if (PyModule_AddIntMacro(m, SIGUSR2))
1342 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001343#endif
1344#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001345 if (PyModule_AddIntMacro(m, SIGCLD))
1346 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001347#endif
1348#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001349 if (PyModule_AddIntMacro(m, SIGCHLD))
1350 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001351#endif
1352#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001353 if (PyModule_AddIntMacro(m, SIGPWR))
1354 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001355#endif
1356#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001357 if (PyModule_AddIntMacro(m, SIGIO))
1358 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001359#endif
1360#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001361 if (PyModule_AddIntMacro(m, SIGURG))
1362 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001363#endif
1364#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001365 if (PyModule_AddIntMacro(m, SIGWINCH))
1366 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001367#endif
1368#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001369 if (PyModule_AddIntMacro(m, SIGPOLL))
1370 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001371#endif
1372#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001373 if (PyModule_AddIntMacro(m, SIGSTOP))
1374 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001375#endif
1376#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001377 if (PyModule_AddIntMacro(m, SIGTSTP))
1378 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001379#endif
1380#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001381 if (PyModule_AddIntMacro(m, SIGCONT))
1382 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383#endif
1384#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001385 if (PyModule_AddIntMacro(m, SIGTTIN))
1386 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001387#endif
1388#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001389 if (PyModule_AddIntMacro(m, SIGTTOU))
1390 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001391#endif
1392#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001393 if (PyModule_AddIntMacro(m, SIGVTALRM))
1394 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001395#endif
1396#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001397 if (PyModule_AddIntMacro(m, SIGPROF))
1398 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001399#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001400#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001401 if (PyModule_AddIntMacro(m, SIGXCPU))
1402 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001404#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001405 if (PyModule_AddIntMacro(m, SIGXFSZ))
1406 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001407#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001408#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001409 if (PyModule_AddIntMacro(m, SIGRTMIN))
1410 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001411#endif
1412#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001413 if (PyModule_AddIntMacro(m, SIGRTMAX))
1414 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001415#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001416#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001417 if (PyModule_AddIntMacro(m, SIGINFO))
1418 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001419#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001420
1421#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001422 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1423 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001424#endif
1425#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001426 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1427 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001428#endif
1429#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001430 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1431 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001432#endif
1433
1434#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 ItimerError = PyErr_NewException("signal.ItimerError",
Martin Panter6d57fe12016-09-17 03:26:16 +00001436 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001437 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001438 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001439#endif
1440
Brian Curtineb24d742010-04-12 17:16:38 +00001441#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001442 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1443 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001444#endif
1445
1446#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001447 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1448 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001449#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