blob: bc36d41648e564b536cc285aa0a2f3e4fb1322ec [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)));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500960#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +0200961 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500962#else
963 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
964#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200965 if (PyErr_Occurred()) {
966 Py_DECREF(result);
967 return NULL;
968 }
969
970 return result;
971}
972#endif
973
974#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300975
976/*[clinic input]
977signal.sigwaitinfo
978
979 sigset: object
980 /
981
982Wait synchronously until one of the signals in *sigset* is delivered.
983
984Returns a struct_siginfo containing information about the signal.
985[clinic start generated code]*/
986
Ross Lagerwallbc808222011-06-25 12:13:40 +0200987static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300988signal_sigwaitinfo(PyObject *module, PyObject *sigset)
989/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +0200990{
Ross Lagerwallbc808222011-06-25 12:13:40 +0200991 sigset_t set;
992 siginfo_t si;
993 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +0100994 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +0200995
Tal Einatc7027b72015-05-16 14:14:49 +0300996 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200997 return NULL;
998
Victor Stinnera453cd82015-03-20 12:54:28 +0100999 do {
1000 Py_BEGIN_ALLOW_THREADS
1001 err = sigwaitinfo(&set, &si);
1002 Py_END_ALLOW_THREADS
1003 } while (err == -1
1004 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001005 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001006 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001007
1008 return fill_siginfo(&si);
1009}
1010
Ross Lagerwallbc808222011-06-25 12:13:40 +02001011#endif /* #ifdef HAVE_SIGWAITINFO */
1012
1013#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001014
1015/*[clinic input]
1016signal.sigtimedwait
1017
1018 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001019 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001020 /
1021
1022Like sigwaitinfo(), but with a timeout.
1023
1024The timeout is specified in seconds, with floating point numbers allowed.
1025[clinic start generated code]*/
1026
Ross Lagerwallbc808222011-06-25 12:13:40 +02001027static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001028signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001029 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001030/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001031{
Victor Stinnera453cd82015-03-20 12:54:28 +01001032 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001033 sigset_t set;
1034 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001035 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001036 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001037
Victor Stinner869e1772015-03-30 03:49:14 +02001038 if (_PyTime_FromSecondsObject(&timeout,
1039 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001040 return NULL;
1041
Victor Stinnera453cd82015-03-20 12:54:28 +01001042 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001043 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1044 return NULL;
1045 }
1046
Tal Einatc7027b72015-05-16 14:14:49 +03001047 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001048 return NULL;
1049
Victor Stinner34dc0f42015-03-27 18:19:03 +01001050 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001051
1052 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001053 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1054 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001055
1056 Py_BEGIN_ALLOW_THREADS
1057 res = sigtimedwait(&set, &si, &ts);
1058 Py_END_ALLOW_THREADS
1059
1060 if (res != -1)
1061 break;
1062
1063 if (errno != EINTR) {
1064 if (errno == EAGAIN)
1065 Py_RETURN_NONE;
1066 else
1067 return PyErr_SetFromErrno(PyExc_OSError);
1068 }
1069
1070 /* sigtimedwait() was interrupted by a signal (EINTR) */
1071 if (PyErr_CheckSignals())
1072 return NULL;
1073
Victor Stinner34dc0f42015-03-27 18:19:03 +01001074 monotonic = _PyTime_GetMonotonicClock();
1075 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001076 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001077 break;
1078 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001079
1080 return fill_siginfo(&si);
1081}
1082
Ross Lagerwallbc808222011-06-25 12:13:40 +02001083#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1084
Victor Stinnerb3e72192011-05-08 01:46:11 +02001085
1086#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
Tal Einatc7027b72015-05-16 14:14:49 +03001087
1088/*[clinic input]
1089signal.pthread_kill
1090
1091 thread_id: long
1092 signalnum: int
1093 /
1094
1095Send a signal to a thread.
1096[clinic start generated code]*/
1097
Victor Stinnerb3e72192011-05-08 01:46:11 +02001098static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001099signal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum)
1100/*[clinic end generated code: output=2a09ce41f1c4228a input=77ed6a3b6f2a8122]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001101{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001102 int err;
1103
Tal Einatc7027b72015-05-16 14:14:49 +03001104 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001105 if (err != 0) {
1106 errno = err;
1107 PyErr_SetFromErrno(PyExc_OSError);
1108 return NULL;
1109 }
1110
1111 /* the signal may have been send to the current thread */
1112 if (PyErr_CheckSignals())
1113 return NULL;
1114
1115 Py_RETURN_NONE;
1116}
1117
Victor Stinnerb3e72192011-05-08 01:46:11 +02001118#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1119
1120
1121
Tal Einatc7027b72015-05-16 14:14:49 +03001122/* List of functions defined in the module -- some of the methoddefs are
1123 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001124static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001125 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1126 SIGNAL_ALARM_METHODDEF
1127 SIGNAL_SETITIMER_METHODDEF
1128 SIGNAL_GETITIMER_METHODDEF
1129 SIGNAL_SIGNAL_METHODDEF
1130 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001132 SIGNAL_SIGINTERRUPT_METHODDEF
1133 SIGNAL_PAUSE_METHODDEF
1134 SIGNAL_PTHREAD_KILL_METHODDEF
1135 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1136 SIGNAL_SIGPENDING_METHODDEF
1137 SIGNAL_SIGWAIT_METHODDEF
1138 SIGNAL_SIGWAITINFO_METHODDEF
1139 SIGNAL_SIGTIMEDWAIT_METHODDEF
1140 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001141};
1142
Barry Warsaw92971171997-01-03 00:14:25 +00001143
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001144PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001145"This module provides mechanisms to use signal handlers in Python.\n\
1146\n\
1147Functions:\n\
1148\n\
1149alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001150setitimer() -- cause a signal (described below) after a specified\n\
1151 float time and the timer may restart then [Unix only]\n\
1152getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001153signal() -- set the action for a given signal\n\
1154getsignal() -- get the signal action for a given signal\n\
1155pause() -- wait until a signal arrives [Unix only]\n\
1156default_int_handler() -- default SIGINT handler\n\
1157\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001158signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001159SIG_DFL -- used to refer to the system default handler\n\
1160SIG_IGN -- used to ignore the signal\n\
1161NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001162SIGINT, SIGTERM, etc. -- signal numbers\n\
1163\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001164itimer constants:\n\
1165ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1166 expiration\n\
1167ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1168 and delivers SIGVTALRM upon expiration\n\
1169ITIMER_PROF -- decrements both when the process is executing and\n\
1170 when the system is executing on behalf of the process.\n\
1171 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1172 used to profile the time spent by the application\n\
1173 in user and kernel space. SIGPROF is delivered upon\n\
1174 expiration.\n\
1175\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001176*** IMPORTANT NOTICE ***\n\
1177A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001178the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001179
Martin v. Löwis1a214512008-06-11 05:26:20 +00001180static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001182 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 module_doc,
1184 -1,
1185 signal_methods,
1186 NULL,
1187 NULL,
1188 NULL,
1189 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001190};
1191
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001192PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001193PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 PyObject *m, *d, *x;
1196 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001197
1198#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 main_thread = PyThread_get_thread_ident();
1200 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001201#endif
1202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 /* Create the module and add the functions */
1204 m = PyModule_Create(&signalmodule);
1205 if (m == NULL)
1206 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001207
Ross Lagerwallbc808222011-06-25 12:13:40 +02001208#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001209 if (!initialized) {
1210 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1211 return NULL;
1212 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001213 Py_INCREF((PyObject*) &SiginfoType);
1214 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1215 initialized = 1;
1216#endif
1217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 /* Add some symbolic constants to the module */
1219 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1222 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1223 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1226 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1227 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 x = PyLong_FromLong((long)NSIG);
1230 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1231 goto finally;
1232 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001233
Victor Stinnera9293352011-04-30 15:21:58 +02001234#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001235 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1236 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001237#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001238#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001239 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1240 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001241#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001242#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001243 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1244 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001245#endif
1246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1248 if (!x)
1249 goto finally;
1250 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 Handlers[0].tripped = 0;
1253 for (i = 1; i < NSIG; i++) {
1254 void (*t)(int);
1255 t = PyOS_getsig(i);
1256 Handlers[i].tripped = 0;
1257 if (t == SIG_DFL)
1258 Handlers[i].func = DefaultHandler;
1259 else if (t == SIG_IGN)
1260 Handlers[i].func = IgnoreHandler;
1261 else
1262 Handlers[i].func = Py_None; /* None of our business */
1263 Py_INCREF(Handlers[i].func);
1264 }
1265 if (Handlers[SIGINT].func == DefaultHandler) {
1266 /* Install default int handler */
1267 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001268 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1270 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001271
1272#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001273 if (PyModule_AddIntMacro(m, SIGHUP))
1274 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001275#endif
1276#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001277 if (PyModule_AddIntMacro(m, SIGINT))
1278 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001279#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001280#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001281 if (PyModule_AddIntMacro(m, SIGBREAK))
1282 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001283#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001284#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001285 if (PyModule_AddIntMacro(m, SIGQUIT))
1286 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001287#endif
1288#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001289 if (PyModule_AddIntMacro(m, SIGILL))
1290 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001291#endif
1292#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001293 if (PyModule_AddIntMacro(m, SIGTRAP))
1294 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001295#endif
1296#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001297 if (PyModule_AddIntMacro(m, SIGIOT))
1298 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299#endif
1300#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001301 if (PyModule_AddIntMacro(m, SIGABRT))
1302 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303#endif
1304#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001305 if (PyModule_AddIntMacro(m, SIGEMT))
1306 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001307#endif
1308#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001309 if (PyModule_AddIntMacro(m, SIGFPE))
1310 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001311#endif
1312#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001313 if (PyModule_AddIntMacro(m, SIGKILL))
1314 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001315#endif
1316#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001317 if (PyModule_AddIntMacro(m, SIGBUS))
1318 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001319#endif
1320#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001321 if (PyModule_AddIntMacro(m, SIGSEGV))
1322 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001323#endif
1324#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001325 if (PyModule_AddIntMacro(m, SIGSYS))
1326 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001327#endif
1328#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001329 if (PyModule_AddIntMacro(m, SIGPIPE))
1330 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001331#endif
1332#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001333 if (PyModule_AddIntMacro(m, SIGALRM))
1334 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001335#endif
1336#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001337 if (PyModule_AddIntMacro(m, SIGTERM))
1338 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339#endif
1340#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001341 if (PyModule_AddIntMacro(m, SIGUSR1))
1342 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001343#endif
1344#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001345 if (PyModule_AddIntMacro(m, SIGUSR2))
1346 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001347#endif
1348#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001349 if (PyModule_AddIntMacro(m, SIGCLD))
1350 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001351#endif
1352#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001353 if (PyModule_AddIntMacro(m, SIGCHLD))
1354 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001355#endif
1356#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001357 if (PyModule_AddIntMacro(m, SIGPWR))
1358 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001359#endif
1360#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001361 if (PyModule_AddIntMacro(m, SIGIO))
1362 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001363#endif
1364#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001365 if (PyModule_AddIntMacro(m, SIGURG))
1366 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001367#endif
1368#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001369 if (PyModule_AddIntMacro(m, SIGWINCH))
1370 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001371#endif
1372#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001373 if (PyModule_AddIntMacro(m, SIGPOLL))
1374 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001375#endif
1376#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001377 if (PyModule_AddIntMacro(m, SIGSTOP))
1378 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001379#endif
1380#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001381 if (PyModule_AddIntMacro(m, SIGTSTP))
1382 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383#endif
1384#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001385 if (PyModule_AddIntMacro(m, SIGCONT))
1386 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001387#endif
1388#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001389 if (PyModule_AddIntMacro(m, SIGTTIN))
1390 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001391#endif
1392#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001393 if (PyModule_AddIntMacro(m, SIGTTOU))
1394 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001395#endif
1396#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001397 if (PyModule_AddIntMacro(m, SIGVTALRM))
1398 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001399#endif
1400#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001401 if (PyModule_AddIntMacro(m, SIGPROF))
1402 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001404#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001405 if (PyModule_AddIntMacro(m, SIGXCPU))
1406 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001407#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001408#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001409 if (PyModule_AddIntMacro(m, SIGXFSZ))
1410 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001411#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001412#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001413 if (PyModule_AddIntMacro(m, SIGRTMIN))
1414 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001415#endif
1416#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001417 if (PyModule_AddIntMacro(m, SIGRTMAX))
1418 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001419#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001420#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001421 if (PyModule_AddIntMacro(m, SIGINFO))
1422 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001423#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001424
1425#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001426 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1427 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001428#endif
1429#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001430 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1431 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001432#endif
1433#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001434 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1435 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001436#endif
1437
1438#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 ItimerError = PyErr_NewException("signal.ItimerError",
Martin Panter6d57fe12016-09-17 03:26:16 +00001440 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001441 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001442 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001443#endif
1444
Brian Curtineb24d742010-04-12 17:16:38 +00001445#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001446 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1447 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001448#endif
1449
1450#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1452 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001453#endif
1454
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001455#ifdef MS_WINDOWS
1456 /* Create manual-reset event, initially unset */
1457 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1458#endif
1459
Martin v. Löwis1a214512008-06-11 05:26:20 +00001460 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 Py_DECREF(m);
1462 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001463 }
Barry Warsaw92971171997-01-03 00:14:25 +00001464
Barry Warsaw92971171997-01-03 00:14:25 +00001465 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001466 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001467}
1468
1469static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001470finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 int i;
1473 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 PyOS_setsig(SIGINT, old_siginthandler);
1476 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 for (i = 1; i < NSIG; i++) {
1479 func = Handlers[i].func;
1480 Handlers[i].tripped = 0;
1481 Handlers[i].func = NULL;
1482 if (i != SIGINT && func != NULL && func != Py_None &&
1483 func != DefaultHandler && func != IgnoreHandler)
1484 PyOS_setsig(i, SIG_DFL);
1485 Py_XDECREF(func);
1486 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001487
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001488 Py_CLEAR(IntHandler);
1489 Py_CLEAR(DefaultHandler);
1490 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001491}
1492
Barry Warsaw92971171997-01-03 00:14:25 +00001493
Barry Warsaw92971171997-01-03 00:14:25 +00001494/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001495int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001496PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 int i;
1499 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (!is_tripped)
1502 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001503
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001504#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (PyThread_get_thread_ident() != main_thread)
1506 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001507#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 /*
1510 * The is_tripped variable is meant to speed up the calls to
1511 * PyErr_CheckSignals (both directly or via pending calls) when no
1512 * signal has arrived. This variable is set to 1 when a signal arrives
1513 * and it is set to 0 here, when we know some signals arrived. This way
1514 * we can run the registered handlers with no signals blocked.
1515 *
1516 * NOTE: with this approach we can have a situation where is_tripped is
1517 * 1 but we have no more signals to handle (Handlers[i].tripped
1518 * is 0 for every signal i). This won't do us any harm (except
1519 * we're gonna spent some cycles for nothing). This happens when
1520 * we receive a signal i after we zero is_tripped and before we
1521 * check Handlers[i].tripped.
1522 */
1523 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (!(f = (PyObject *)PyEval_GetFrame()))
1526 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 for (i = 1; i < NSIG; i++) {
1529 if (Handlers[i].tripped) {
1530 PyObject *result = NULL;
1531 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1532 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (arglist) {
1535 result = PyEval_CallObject(Handlers[i].func,
1536 arglist);
1537 Py_DECREF(arglist);
1538 }
1539 if (!result)
1540 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 Py_DECREF(result);
1543 }
1544 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001547}
1548
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001549
Barry Warsaw92971171997-01-03 00:14:25 +00001550/* Replacements for intrcheck.c functionality
1551 * Declared in pyerrors.h
1552 */
1553void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001554PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001555{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001556 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001557}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001558
1559void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001560PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001561{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001562 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 Py_DECREF(m);
1565 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001566}
1567
1568void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001569PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001572}
1573
1574int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001575PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001578#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (PyThread_get_thread_ident() != main_thread)
1580 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001581#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 Handlers[SIGINT].tripped = 0;
1583 return 1;
1584 }
1585 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001586}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001587
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001588static void
1589_clear_pending_signals(void)
1590{
1591 int i;
1592 if (!is_tripped)
1593 return;
1594 is_tripped = 0;
1595 for (i = 1; i < NSIG; ++i) {
1596 Handlers[i].tripped = 0;
1597 }
1598}
1599
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001600void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001601PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001602{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001603 /* Clear the signal flags after forking so that they aren't handled
1604 * in both processes if they came in just before the fork() but before
1605 * the interpreter had an opportunity to call the handlers. issue9535. */
1606 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001607#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001608 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1609 * can be called safely. */
1610 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001611 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 PyEval_ReInitThreads();
1613 main_thread = PyThread_get_thread_ident();
1614 main_pid = getpid();
1615 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001616#endif
1617}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001618
1619int
1620_PyOS_IsMainThread(void)
1621{
1622#ifdef WITH_THREAD
1623 return PyThread_get_thread_ident() == main_thread;
1624#else
1625 return 1;
1626#endif
1627}
1628
1629#ifdef MS_WINDOWS
1630void *_PyOS_SigintEvent(void)
1631{
1632 /* Returns a manual-reset event which gets tripped whenever
1633 SIGINT is received.
1634
1635 Python.h does not include windows.h so we do cannot use HANDLE
1636 as the return type of this function. We use void* instead. */
1637 return sigint_event;
1638}
1639#endif