blob: ed2f4e8cf0a2dc18f5fbca419134eda846c4b295 [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
Victor Stinner0b7629c2017-06-10 11:20:03 +0200247 if (!is_tripped) {
248 /* Set is_tripped after setting .tripped, as it gets
249 cleared in PyErr_CheckSignals() before .tripped. */
250 is_tripped = 1;
251 Py_AddPendingCall(checksignals_witharg, NULL);
252 }
253
254 /* And then write to the wakeup fd *after* setting all the globals and
255 doing the Py_AddPendingCall. We used to write to the wakeup fd and then
256 set the flag, but this allowed the following sequence of events
257 (especially on windows, where trip_signal runs in a new thread):
258
259 - main thread blocks on select([wakeup_fd], ...)
260 - signal arrives
261 - trip_signal writes to the wakeup fd
262 - the main thread wakes up
263 - the main thread checks the signal flags, sees that they're unset
264 - the main thread empties the wakeup fd
265 - the main thread goes back to sleep
266 - trip_signal sets the flags to request the Python-level signal handler
267 be run
268 - the main thread doesn't notice, because it's asleep
269
270 See bpo-30038 for more details.
271 */
272
Victor Stinner11517102014-07-29 23:31:34 +0200273#ifdef MS_WINDOWS
274 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
275#else
276 fd = wakeup_fd;
277#endif
278
279 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200280 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200281#ifdef MS_WINDOWS
282 if (wakeup.use_send) {
283 do {
284 rc = send(fd, &byte, 1, 0);
285 } while (rc < 0 && errno == EINTR);
286
287 /* we only have a storage for one error in the wakeup structure */
288 if (rc < 0 && !wakeup.send_err_set) {
289 wakeup.send_err_set = 1;
290 wakeup.send_errno = errno;
291 wakeup.send_win_error = GetLastError();
292 Py_AddPendingCall(report_wakeup_send_error, NULL);
293 }
294 }
295 else
296#endif
297 {
298 byte = (unsigned char)sig_num;
Victor Stinnere72fe392015-04-01 18:35:22 +0200299
300 /* _Py_write_noraise() retries write() if write() is interrupted by
301 a signal (fails with EINTR). */
302 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200303
304 if (rc < 0) {
305 Py_AddPendingCall(report_wakeup_write_error,
Benjamin Petersonca470632016-09-06 13:47:26 -0700306 (void *)(intptr_t)errno);
Victor Stinner11517102014-07-29 23:31:34 +0200307 }
308 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200309 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200310}
311
312static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000313signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000315 int save_errno = errno;
316
Antoine Pitrou39a65912010-11-05 19:47:27 +0000317#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000320#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000321 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200322 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000324
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000325#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000326#ifdef SIGCHLD
327 /* To avoid infinite recursion, this signal remains
328 reset until explicit re-instated.
329 Don't clear the 'func' field as it is our pointer
330 to the Python handler... */
331 if (sig_num != SIGCHLD)
332#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000334 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 * makes this true. See also issue8354. */
336 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000337#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000338
339 /* Issue #10311: asynchronously executing signal handlers should not
340 mutate errno under the feet of unsuspecting C code. */
341 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100342
343#ifdef MS_WINDOWS
344 if (sig_num == SIGINT)
345 SetEvent(sigint_event);
346#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000347}
Guido van Rossume4485b01994-09-07 14:32:49 +0000348
Guido van Rossum06d511d1995-03-10 15:13:48 +0000349
Guido van Rossum1171ee61997-08-22 20:42:00 +0000350#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300351
352/*[clinic input]
353signal.alarm -> long
354
355 seconds: int
356 /
357
358Arrange for SIGALRM to arrive after the given number of seconds.
359[clinic start generated code]*/
360
361static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300362signal_alarm_impl(PyObject *module, int seconds)
363/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300366 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000367}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000368
Guido van Rossum06d511d1995-03-10 15:13:48 +0000369#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000370
Guido van Rossum1171ee61997-08-22 20:42:00 +0000371#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300372
373/*[clinic input]
374signal.pause
375
376Wait until a signal arrives.
377[clinic start generated code]*/
378
Guido van Rossuma597dde1995-01-10 20:56:29 +0000379static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300380signal_pause_impl(PyObject *module)
381/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_BEGIN_ALLOW_THREADS
384 (void)pause();
385 Py_END_ALLOW_THREADS
386 /* make sure that any exceptions that got raised are propagated
387 * back into Python
388 */
389 if (PyErr_CheckSignals())
390 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000391
Tal Einatc7027b72015-05-16 14:14:49 +0300392 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000393}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000394
Guido van Rossum06d511d1995-03-10 15:13:48 +0000395#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000396
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000397
Tal Einatc7027b72015-05-16 14:14:49 +0300398/*[clinic input]
399signal.signal
400
401 signalnum: int
402 handler: object
403 /
404
405Set the action for the given signal.
406
407The action can be SIG_DFL, SIG_IGN, or a callable Python object.
408The previous action is returned. See getsignal() for possible return values.
409
410*** IMPORTANT NOTICE ***
411A signal handler function is called with two arguments:
412the first is the signal number, the second is the interrupted stack frame.
413[clinic start generated code]*/
414
Guido van Rossume4485b01994-09-07 14:32:49 +0000415static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300416signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
417/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyObject *old_handler;
420 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000421#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300422 /* Validate that signalnum is one of the allowable signals */
423 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000424 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000425#ifdef SIGBREAK
426 /* Issue #10003: SIGBREAK is not documented as permitted, but works
427 and corresponds to CTRL_BREAK_EVENT. */
428 case SIGBREAK: break;
429#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000430 case SIGFPE: break;
431 case SIGILL: break;
432 case SIGINT: break;
433 case SIGSEGV: break;
434 case SIGTERM: break;
435 default:
436 PyErr_SetString(PyExc_ValueError, "invalid signal value");
437 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000438 }
439#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (PyThread_get_thread_ident() != main_thread) {
442 PyErr_SetString(PyExc_ValueError,
443 "signal only works in main thread");
444 return NULL;
445 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000446#endif
Tal Einatc7027b72015-05-16 14:14:49 +0300447 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyErr_SetString(PyExc_ValueError,
449 "signal number out of range");
450 return NULL;
451 }
Tal Einatc7027b72015-05-16 14:14:49 +0300452 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300454 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300456 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return NULL;
460 }
461 else
462 func = signal_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300463 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200464 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return NULL;
466 }
Tal Einatc7027b72015-05-16 14:14:49 +0300467 old_handler = Handlers[signalnum].func;
468 Handlers[signalnum].tripped = 0;
469 Py_INCREF(handler);
470 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200471 if (old_handler != NULL)
472 return old_handler;
473 else
474 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000475}
476
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000477
Tal Einatc7027b72015-05-16 14:14:49 +0300478/*[clinic input]
479signal.getsignal
480
481 signalnum: int
482 /
483
484Return the current action for the given signal.
485
486The return value can be:
487 SIG_IGN -- if the signal is being ignored
488 SIG_DFL -- if the default action for the signal is in effect
489 None -- if an unknown handler is in effect
490 anything else -- the callable Python object used as a handler
491[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000492
Guido van Rossume4485b01994-09-07 14:32:49 +0000493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300494signal_getsignal_impl(PyObject *module, int signalnum)
495/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300498 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyErr_SetString(PyExc_ValueError,
500 "signal number out of range");
501 return NULL;
502 }
Tal Einatc7027b72015-05-16 14:14:49 +0300503 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200504 if (old_handler != NULL) {
505 Py_INCREF(old_handler);
506 return old_handler;
507 }
508 else {
509 Py_RETURN_NONE;
510 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511}
512
Christian Heimes8640e742008-02-23 16:23:06 +0000513#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300514
515/*[clinic input]
516signal.siginterrupt
517
518 signalnum: int
519 flag: int
520 /
521
522Change system call restart behaviour.
523
524If flag is False, system calls will be restarted when interrupted by
525signal sig, else system calls will be interrupted.
526[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000527
528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300529signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
530/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000531{
Tal Einatc7027b72015-05-16 14:14:49 +0300532 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyErr_SetString(PyExc_ValueError,
534 "signal number out of range");
535 return NULL;
536 }
Tal Einatc7027b72015-05-16 14:14:49 +0300537 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200538 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return NULL;
540 }
Tal Einatc7027b72015-05-16 14:14:49 +0300541 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000542}
543
544#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000545
Tal Einatc7027b72015-05-16 14:14:49 +0300546
547static PyObject*
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000548signal_set_wakeup_fd(PyObject *self, PyObject *args)
549{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200550 struct _Py_stat_struct status;
Victor Stinner11517102014-07-29 23:31:34 +0200551#ifdef MS_WINDOWS
552 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100553 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200554 int res;
555 int res_size = sizeof res;
556 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200557 int is_socket;
558
559 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
560 return NULL;
561
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100562 sockfd = PyLong_AsSocket_t(fdobj);
563 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200564 return NULL;
565#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
569 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200570#endif
571
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000572#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (PyThread_get_thread_ident() != main_thread) {
574 PyErr_SetString(PyExc_ValueError,
575 "set_wakeup_fd only works in main thread");
576 return NULL;
577 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000578#endif
Victor Stinner0bffc942014-07-21 16:28:54 +0200579
Victor Stinner11517102014-07-29 23:31:34 +0200580#ifdef MS_WINDOWS
581 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100582 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200583 /* Import the _socket module to call WSAStartup() */
584 mod = PyImport_ImportModuleNoBlock("_socket");
585 if (mod == NULL)
586 return NULL;
587 Py_DECREF(mod);
588
589 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100590 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200591 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100592 int fd, err;
593
594 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200595 if (err != WSAENOTSOCK) {
596 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
597 return NULL;
598 }
599
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100600 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700601 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200602 PyErr_SetString(PyExc_ValueError, "invalid fd");
603 return NULL;
604 }
605
Victor Stinnere134a7f2015-03-30 10:09:31 +0200606 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200607 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200608
609 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200610 }
Victor Stinner38227602014-08-27 12:59:44 +0200611 else {
Victor Stinner11517102014-07-29 23:31:34 +0200612 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200613
614 /* Windows does not provide a function to test if a socket
615 is in non-blocking mode */
616 }
Victor Stinner11517102014-07-29 23:31:34 +0200617 }
618
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100619 old_sockfd = wakeup.fd;
620 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200621 wakeup.use_send = is_socket;
622
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100623 if (old_sockfd != INVALID_FD)
624 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200625 else
626 return PyLong_FromLong(-1);
627#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200628 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200629 int blocking;
630
Victor Stinnere134a7f2015-03-30 10:09:31 +0200631 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200632 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200633
634 blocking = _Py_get_blocking(fd);
635 if (blocking < 0)
636 return NULL;
637 if (blocking) {
638 PyErr_Format(PyExc_ValueError,
639 "the fd %i must be in non-blocking mode",
640 fd);
641 return NULL;
642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 old_fd = wakeup_fd;
646 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200649#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000650}
651
652PyDoc_STRVAR(set_wakeup_fd_doc,
653"set_wakeup_fd(fd) -> fd\n\
654\n\
Victor Stinner11517102014-07-29 23:31:34 +0200655Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000656comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200657The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000658\n\
659The fd must be non-blocking.");
660
661/* C API for the same, without all the error checking */
662int
663PySignal_SetWakeupFd(int fd)
664{
Victor Stinner11517102014-07-29 23:31:34 +0200665 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 if (fd < 0)
667 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200668
669#ifdef MS_WINDOWS
670 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
671 wakeup.fd = fd;
672#else
673 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200675#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000677}
678
679
Martin v. Löwis823725e2008-03-24 13:39:54 +0000680#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300681
682/*[clinic input]
683signal.setitimer
684
685 which: int
686 seconds: double
687 interval: double = 0.0
688 /
689
690Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
691
692The timer will fire after value seconds and after that every interval seconds.
693The itimer can be cleared by setting seconds to zero.
694
695Returns old values as a tuple: (delay, interval).
696[clinic start generated code]*/
697
Martin v. Löwis823725e2008-03-24 13:39:54 +0000698static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300699signal_setitimer_impl(PyObject *module, int which, double seconds,
Tal Einatc7027b72015-05-16 14:14:49 +0300700 double interval)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300701/*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000702{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000703 struct itimerval new, old;
704
Tal Einatc7027b72015-05-16 14:14:49 +0300705 timeval_from_double(seconds, &new.it_value);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000706 timeval_from_double(interval, &new.it_interval);
707 /* Let OS check "which" value */
708 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300709 PyErr_SetFromErrno(ItimerError);
710 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000711 }
712
713 return itimer_retval(&old);
714}
715
Martin v. Löwis823725e2008-03-24 13:39:54 +0000716#endif
717
718
719#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300720
721/*[clinic input]
722signal.getitimer
723
724 which: int
725 /
726
727Returns current value of given itimer.
728[clinic start generated code]*/
729
Martin v. Löwis823725e2008-03-24 13:39:54 +0000730static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300731signal_getitimer_impl(PyObject *module, int which)
732/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000733{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000734 struct itimerval old;
735
Martin v. Löwis823725e2008-03-24 13:39:54 +0000736 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300737 PyErr_SetFromErrno(ItimerError);
738 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000739 }
740
741 return itimer_retval(&old);
742}
743
Martin v. Löwis823725e2008-03-24 13:39:54 +0000744#endif
745
Ross Lagerwallbc808222011-06-25 12:13:40 +0200746#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
747 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200748/* Convert an iterable to a sigset.
749 Return 0 on success, return -1 and raise an exception on error. */
750
751static int
752iterable_to_sigset(PyObject *iterable, sigset_t *mask)
753{
754 int result = -1;
755 PyObject *iterator, *item;
756 long signum;
757 int err;
758
759 sigemptyset(mask);
760
761 iterator = PyObject_GetIter(iterable);
762 if (iterator == NULL)
763 goto error;
764
765 while (1)
766 {
767 item = PyIter_Next(iterator);
768 if (item == NULL) {
769 if (PyErr_Occurred())
770 goto error;
771 else
772 break;
773 }
774
775 signum = PyLong_AsLong(item);
776 Py_DECREF(item);
777 if (signum == -1 && PyErr_Occurred())
778 goto error;
779 if (0 < signum && signum < NSIG)
780 err = sigaddset(mask, (int)signum);
781 else
782 err = 1;
783 if (err) {
784 PyErr_Format(PyExc_ValueError,
785 "signal number %ld out of range", signum);
786 goto error;
787 }
788 }
789 result = 0;
790
791error:
792 Py_XDECREF(iterator);
793 return result;
794}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200795#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200796
Victor Stinnerb3e72192011-05-08 01:46:11 +0200797#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200798static PyObject*
799sigset_to_set(sigset_t mask)
800{
801 PyObject *signum, *result;
802 int sig;
803
804 result = PySet_New(0);
805 if (result == NULL)
806 return NULL;
807
808 for (sig = 1; sig < NSIG; sig++) {
809 if (sigismember(&mask, sig) != 1)
810 continue;
811
812 /* Handle the case where it is a member by adding the signal to
813 the result list. Ignore the other cases because they mean the
814 signal isn't a member of the mask or the signal was invalid,
815 and an invalid signal must have been our fault in constructing
816 the loop boundaries. */
817 signum = PyLong_FromLong(sig);
818 if (signum == NULL) {
819 Py_DECREF(result);
820 return NULL;
821 }
822 if (PySet_Add(result, signum) == -1) {
823 Py_DECREF(signum);
824 Py_DECREF(result);
825 return NULL;
826 }
827 Py_DECREF(signum);
828 }
829 return result;
830}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200831#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200832
Victor Stinnerb3e72192011-05-08 01:46:11 +0200833#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300834
835/*[clinic input]
836signal.pthread_sigmask
837
838 how: int
839 mask: object
840 /
841
842Fetch and/or change the signal mask of the calling thread.
843[clinic start generated code]*/
844
Victor Stinnera9293352011-04-30 15:21:58 +0200845static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300846signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
847/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200848{
Tal Einatc7027b72015-05-16 14:14:49 +0300849 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200850 int err;
851
Tal Einatc7027b72015-05-16 14:14:49 +0300852 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200853 return NULL;
854
Tal Einatc7027b72015-05-16 14:14:49 +0300855 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200856 if (err != 0) {
857 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200858 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200859 return NULL;
860 }
861
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200862 /* if signals was unblocked, signal handlers have been called */
863 if (PyErr_CheckSignals())
864 return NULL;
865
Victor Stinner35b300c2011-05-04 13:20:35 +0200866 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200867}
868
Victor Stinnera9293352011-04-30 15:21:58 +0200869#endif /* #ifdef PYPTHREAD_SIGMASK */
870
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871
Victor Stinnerb3e72192011-05-08 01:46:11 +0200872#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300873
874/*[clinic input]
875signal.sigpending
876
877Examine pending signals.
878
879Returns a set of signal numbers that are pending for delivery to
880the calling thread.
881[clinic start generated code]*/
882
Victor Stinnerb3e72192011-05-08 01:46:11 +0200883static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300884signal_sigpending_impl(PyObject *module)
885/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200886{
887 int err;
888 sigset_t mask;
889 err = sigpending(&mask);
890 if (err)
891 return PyErr_SetFromErrno(PyExc_OSError);
892 return sigset_to_set(mask);
893}
894
Victor Stinnerb3e72192011-05-08 01:46:11 +0200895#endif /* #ifdef HAVE_SIGPENDING */
896
897
898#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300899
900/*[clinic input]
901signal.sigwait
902
903 sigset: object
904 /
905
906Wait for a signal.
907
908Suspend execution of the calling thread until the delivery of one of the
909signals specified in the signal set sigset. The function accepts the signal
910and returns the signal number.
911[clinic start generated code]*/
912
Victor Stinnerb3e72192011-05-08 01:46:11 +0200913static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300914signal_sigwait(PyObject *module, PyObject *sigset)
915/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200916{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917 sigset_t set;
918 int err, signum;
919
Tal Einatc7027b72015-05-16 14:14:49 +0300920 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200921 return NULL;
922
Victor Stinner10c30d62011-06-10 01:39:53 +0200923 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200924 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200925 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200926 if (err) {
927 errno = err;
928 return PyErr_SetFromErrno(PyExc_OSError);
929 }
930
931 return PyLong_FromLong(signum);
932}
933
Tal Einatc7027b72015-05-16 14:14:49 +0300934#endif /* #ifdef HAVE_SIGWAIT */
935
Victor Stinnerb3e72192011-05-08 01:46:11 +0200936
Ross Lagerwallbc808222011-06-25 12:13:40 +0200937#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
938static int initialized;
939static PyStructSequence_Field struct_siginfo_fields[] = {
940 {"si_signo", "signal number"},
941 {"si_code", "signal code"},
942 {"si_errno", "errno associated with this signal"},
943 {"si_pid", "sending process ID"},
944 {"si_uid", "real user ID of sending process"},
945 {"si_status", "exit value or signal"},
946 {"si_band", "band event for SIGPOLL"},
947 {0}
948};
949
950PyDoc_STRVAR(struct_siginfo__doc__,
951"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
952This object may be accessed either as a tuple of\n\
953(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
954or via the attributes si_signo, si_code, and so on.");
955
956static PyStructSequence_Desc struct_siginfo_desc = {
957 "signal.struct_siginfo", /* name */
958 struct_siginfo__doc__, /* doc */
959 struct_siginfo_fields, /* fields */
960 7 /* n_in_sequence */
961};
962
963static PyTypeObject SiginfoType;
964
965static PyObject *
966fill_siginfo(siginfo_t *si)
967{
968 PyObject *result = PyStructSequence_New(&SiginfoType);
969 if (!result)
970 return NULL;
971
972 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
973 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
974 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
975 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200976 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200977 PyStructSequence_SET_ITEM(result, 5,
978 PyLong_FromLong((long)(si->si_status)));
979 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
980 if (PyErr_Occurred()) {
981 Py_DECREF(result);
982 return NULL;
983 }
984
985 return result;
986}
987#endif
988
989#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300990
991/*[clinic input]
992signal.sigwaitinfo
993
994 sigset: object
995 /
996
997Wait synchronously until one of the signals in *sigset* is delivered.
998
999Returns a struct_siginfo containing information about the signal.
1000[clinic start generated code]*/
1001
Ross Lagerwallbc808222011-06-25 12:13:40 +02001002static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001003signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1004/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001005{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001006 sigset_t set;
1007 siginfo_t si;
1008 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001009 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001010
Tal Einatc7027b72015-05-16 14:14:49 +03001011 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001012 return NULL;
1013
Victor Stinnera453cd82015-03-20 12:54:28 +01001014 do {
1015 Py_BEGIN_ALLOW_THREADS
1016 err = sigwaitinfo(&set, &si);
1017 Py_END_ALLOW_THREADS
1018 } while (err == -1
1019 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001020 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001021 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001022
1023 return fill_siginfo(&si);
1024}
1025
Ross Lagerwallbc808222011-06-25 12:13:40 +02001026#endif /* #ifdef HAVE_SIGWAITINFO */
1027
1028#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001029
1030/*[clinic input]
1031signal.sigtimedwait
1032
1033 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001034 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001035 /
1036
1037Like sigwaitinfo(), but with a timeout.
1038
1039The timeout is specified in seconds, with floating point numbers allowed.
1040[clinic start generated code]*/
1041
Ross Lagerwallbc808222011-06-25 12:13:40 +02001042static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001043signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001044 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001045/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001046{
Victor Stinnera453cd82015-03-20 12:54:28 +01001047 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001048 sigset_t set;
1049 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001050 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001051 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001052
Victor Stinner869e1772015-03-30 03:49:14 +02001053 if (_PyTime_FromSecondsObject(&timeout,
1054 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001055 return NULL;
1056
Victor Stinnera453cd82015-03-20 12:54:28 +01001057 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001058 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1059 return NULL;
1060 }
1061
Tal Einatc7027b72015-05-16 14:14:49 +03001062 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001063 return NULL;
1064
Victor Stinner34dc0f42015-03-27 18:19:03 +01001065 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001066
1067 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001068 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1069 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001070
1071 Py_BEGIN_ALLOW_THREADS
1072 res = sigtimedwait(&set, &si, &ts);
1073 Py_END_ALLOW_THREADS
1074
1075 if (res != -1)
1076 break;
1077
1078 if (errno != EINTR) {
1079 if (errno == EAGAIN)
1080 Py_RETURN_NONE;
1081 else
1082 return PyErr_SetFromErrno(PyExc_OSError);
1083 }
1084
1085 /* sigtimedwait() was interrupted by a signal (EINTR) */
1086 if (PyErr_CheckSignals())
1087 return NULL;
1088
Victor Stinner34dc0f42015-03-27 18:19:03 +01001089 monotonic = _PyTime_GetMonotonicClock();
1090 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001091 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001092 break;
1093 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001094
1095 return fill_siginfo(&si);
1096}
1097
Ross Lagerwallbc808222011-06-25 12:13:40 +02001098#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1099
Victor Stinnerb3e72192011-05-08 01:46:11 +02001100
1101#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
Tal Einatc7027b72015-05-16 14:14:49 +03001102
1103/*[clinic input]
1104signal.pthread_kill
1105
1106 thread_id: long
1107 signalnum: int
1108 /
1109
1110Send a signal to a thread.
1111[clinic start generated code]*/
1112
Victor Stinnerb3e72192011-05-08 01:46:11 +02001113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001114signal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum)
1115/*[clinic end generated code: output=2a09ce41f1c4228a input=77ed6a3b6f2a8122]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001116{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001117 int err;
1118
Tal Einatc7027b72015-05-16 14:14:49 +03001119 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001120 if (err != 0) {
1121 errno = err;
1122 PyErr_SetFromErrno(PyExc_OSError);
1123 return NULL;
1124 }
1125
1126 /* the signal may have been send to the current thread */
1127 if (PyErr_CheckSignals())
1128 return NULL;
1129
1130 Py_RETURN_NONE;
1131}
1132
Victor Stinnerb3e72192011-05-08 01:46:11 +02001133#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1134
1135
1136
Tal Einatc7027b72015-05-16 14:14:49 +03001137/* List of functions defined in the module -- some of the methoddefs are
1138 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001139static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001140 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1141 SIGNAL_ALARM_METHODDEF
1142 SIGNAL_SETITIMER_METHODDEF
1143 SIGNAL_GETITIMER_METHODDEF
1144 SIGNAL_SIGNAL_METHODDEF
1145 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001147 SIGNAL_SIGINTERRUPT_METHODDEF
1148 SIGNAL_PAUSE_METHODDEF
1149 SIGNAL_PTHREAD_KILL_METHODDEF
1150 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1151 SIGNAL_SIGPENDING_METHODDEF
1152 SIGNAL_SIGWAIT_METHODDEF
1153 SIGNAL_SIGWAITINFO_METHODDEF
1154 SIGNAL_SIGTIMEDWAIT_METHODDEF
1155 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001156};
1157
Barry Warsaw92971171997-01-03 00:14:25 +00001158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001159PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001160"This module provides mechanisms to use signal handlers in Python.\n\
1161\n\
1162Functions:\n\
1163\n\
1164alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001165setitimer() -- cause a signal (described below) after a specified\n\
1166 float time and the timer may restart then [Unix only]\n\
1167getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001168signal() -- set the action for a given signal\n\
1169getsignal() -- get the signal action for a given signal\n\
1170pause() -- wait until a signal arrives [Unix only]\n\
1171default_int_handler() -- default SIGINT handler\n\
1172\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001173signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001174SIG_DFL -- used to refer to the system default handler\n\
1175SIG_IGN -- used to ignore the signal\n\
1176NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001177SIGINT, SIGTERM, etc. -- signal numbers\n\
1178\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001179itimer constants:\n\
1180ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1181 expiration\n\
1182ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1183 and delivers SIGVTALRM upon expiration\n\
1184ITIMER_PROF -- decrements both when the process is executing and\n\
1185 when the system is executing on behalf of the process.\n\
1186 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1187 used to profile the time spent by the application\n\
1188 in user and kernel space. SIGPROF is delivered upon\n\
1189 expiration.\n\
1190\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001191*** IMPORTANT NOTICE ***\n\
1192A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001193the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001194
Martin v. Löwis1a214512008-06-11 05:26:20 +00001195static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001197 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 module_doc,
1199 -1,
1200 signal_methods,
1201 NULL,
1202 NULL,
1203 NULL,
1204 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001205};
1206
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001207PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001208PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 PyObject *m, *d, *x;
1211 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001212
1213#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 main_thread = PyThread_get_thread_ident();
1215 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001216#endif
1217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 /* Create the module and add the functions */
1219 m = PyModule_Create(&signalmodule);
1220 if (m == NULL)
1221 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001222
Ross Lagerwallbc808222011-06-25 12:13:40 +02001223#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001224 if (!initialized) {
1225 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1226 return NULL;
1227 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001228 Py_INCREF((PyObject*) &SiginfoType);
1229 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1230 initialized = 1;
1231#endif
1232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 /* Add some symbolic constants to the module */
1234 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1237 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1238 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1241 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1242 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 x = PyLong_FromLong((long)NSIG);
1245 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1246 goto finally;
1247 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001248
Victor Stinnera9293352011-04-30 15:21:58 +02001249#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001250 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1251 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001252#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001253#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001254 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1255 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001256#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001257#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001258 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1259 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001260#endif
1261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1263 if (!x)
1264 goto finally;
1265 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 Handlers[0].tripped = 0;
1268 for (i = 1; i < NSIG; i++) {
1269 void (*t)(int);
1270 t = PyOS_getsig(i);
1271 Handlers[i].tripped = 0;
1272 if (t == SIG_DFL)
1273 Handlers[i].func = DefaultHandler;
1274 else if (t == SIG_IGN)
1275 Handlers[i].func = IgnoreHandler;
1276 else
1277 Handlers[i].func = Py_None; /* None of our business */
1278 Py_INCREF(Handlers[i].func);
1279 }
1280 if (Handlers[SIGINT].func == DefaultHandler) {
1281 /* Install default int handler */
1282 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001283 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1285 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001286
1287#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001288 if (PyModule_AddIntMacro(m, SIGHUP))
1289 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001290#endif
1291#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001292 if (PyModule_AddIntMacro(m, SIGINT))
1293 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001294#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001295#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001296 if (PyModule_AddIntMacro(m, SIGBREAK))
1297 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001298#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001300 if (PyModule_AddIntMacro(m, SIGQUIT))
1301 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001302#endif
1303#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001304 if (PyModule_AddIntMacro(m, SIGILL))
1305 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001306#endif
1307#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001308 if (PyModule_AddIntMacro(m, SIGTRAP))
1309 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001310#endif
1311#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001312 if (PyModule_AddIntMacro(m, SIGIOT))
1313 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001314#endif
1315#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001316 if (PyModule_AddIntMacro(m, SIGABRT))
1317 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001318#endif
1319#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001320 if (PyModule_AddIntMacro(m, SIGEMT))
1321 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001322#endif
1323#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001324 if (PyModule_AddIntMacro(m, SIGFPE))
1325 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001326#endif
1327#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001328 if (PyModule_AddIntMacro(m, SIGKILL))
1329 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001330#endif
1331#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001332 if (PyModule_AddIntMacro(m, SIGBUS))
1333 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001334#endif
1335#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001336 if (PyModule_AddIntMacro(m, SIGSEGV))
1337 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001338#endif
1339#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001340 if (PyModule_AddIntMacro(m, SIGSYS))
1341 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001342#endif
1343#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001344 if (PyModule_AddIntMacro(m, SIGPIPE))
1345 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001346#endif
1347#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001348 if (PyModule_AddIntMacro(m, SIGALRM))
1349 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001350#endif
1351#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001352 if (PyModule_AddIntMacro(m, SIGTERM))
1353 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001354#endif
1355#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001356 if (PyModule_AddIntMacro(m, SIGUSR1))
1357 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001358#endif
1359#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001360 if (PyModule_AddIntMacro(m, SIGUSR2))
1361 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001362#endif
1363#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001364 if (PyModule_AddIntMacro(m, SIGCLD))
1365 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001366#endif
1367#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001368 if (PyModule_AddIntMacro(m, SIGCHLD))
1369 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001370#endif
1371#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001372 if (PyModule_AddIntMacro(m, SIGPWR))
1373 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001374#endif
1375#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001376 if (PyModule_AddIntMacro(m, SIGIO))
1377 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001378#endif
1379#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001380 if (PyModule_AddIntMacro(m, SIGURG))
1381 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001382#endif
1383#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001384 if (PyModule_AddIntMacro(m, SIGWINCH))
1385 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001386#endif
1387#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001388 if (PyModule_AddIntMacro(m, SIGPOLL))
1389 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001390#endif
1391#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001392 if (PyModule_AddIntMacro(m, SIGSTOP))
1393 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001394#endif
1395#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001396 if (PyModule_AddIntMacro(m, SIGTSTP))
1397 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001398#endif
1399#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001400 if (PyModule_AddIntMacro(m, SIGCONT))
1401 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001402#endif
1403#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001404 if (PyModule_AddIntMacro(m, SIGTTIN))
1405 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001406#endif
1407#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001408 if (PyModule_AddIntMacro(m, SIGTTOU))
1409 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001410#endif
1411#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001412 if (PyModule_AddIntMacro(m, SIGVTALRM))
1413 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001414#endif
1415#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001416 if (PyModule_AddIntMacro(m, SIGPROF))
1417 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001418#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001419#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001420 if (PyModule_AddIntMacro(m, SIGXCPU))
1421 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001422#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001423#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001424 if (PyModule_AddIntMacro(m, SIGXFSZ))
1425 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001426#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001427#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001428 if (PyModule_AddIntMacro(m, SIGRTMIN))
1429 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001430#endif
1431#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001432 if (PyModule_AddIntMacro(m, SIGRTMAX))
1433 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001434#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001435#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001436 if (PyModule_AddIntMacro(m, SIGINFO))
1437 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001438#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001439
1440#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001441 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1442 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001443#endif
1444#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001445 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1446 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001447#endif
1448#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001449 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1450 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001451#endif
1452
1453#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 ItimerError = PyErr_NewException("signal.ItimerError",
Martin Panter6d57fe12016-09-17 03:26:16 +00001455 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001456 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001457 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001458#endif
1459
Brian Curtineb24d742010-04-12 17:16:38 +00001460#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001461 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1462 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001463#endif
1464
1465#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001466 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1467 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001468#endif
1469
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001470#ifdef MS_WINDOWS
1471 /* Create manual-reset event, initially unset */
1472 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1473#endif
1474
Martin v. Löwis1a214512008-06-11 05:26:20 +00001475 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 Py_DECREF(m);
1477 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001478 }
Barry Warsaw92971171997-01-03 00:14:25 +00001479
Barry Warsaw92971171997-01-03 00:14:25 +00001480 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001481 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001482}
1483
1484static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001485finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 int i;
1488 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 PyOS_setsig(SIGINT, old_siginthandler);
1491 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 for (i = 1; i < NSIG; i++) {
1494 func = Handlers[i].func;
1495 Handlers[i].tripped = 0;
1496 Handlers[i].func = NULL;
1497 if (i != SIGINT && func != NULL && func != Py_None &&
1498 func != DefaultHandler && func != IgnoreHandler)
1499 PyOS_setsig(i, SIG_DFL);
1500 Py_XDECREF(func);
1501 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001502
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001503 Py_CLEAR(IntHandler);
1504 Py_CLEAR(DefaultHandler);
1505 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001506}
1507
Barry Warsaw92971171997-01-03 00:14:25 +00001508
Barry Warsaw92971171997-01-03 00:14:25 +00001509/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001510int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001511PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 int i;
1514 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (!is_tripped)
1517 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001518
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001519#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (PyThread_get_thread_ident() != main_thread)
1521 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001522#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 /*
1525 * The is_tripped variable is meant to speed up the calls to
1526 * PyErr_CheckSignals (both directly or via pending calls) when no
1527 * signal has arrived. This variable is set to 1 when a signal arrives
1528 * and it is set to 0 here, when we know some signals arrived. This way
1529 * we can run the registered handlers with no signals blocked.
1530 *
1531 * NOTE: with this approach we can have a situation where is_tripped is
1532 * 1 but we have no more signals to handle (Handlers[i].tripped
1533 * is 0 for every signal i). This won't do us any harm (except
1534 * we're gonna spent some cycles for nothing). This happens when
1535 * we receive a signal i after we zero is_tripped and before we
1536 * check Handlers[i].tripped.
1537 */
1538 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (!(f = (PyObject *)PyEval_GetFrame()))
1541 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 for (i = 1; i < NSIG; i++) {
1544 if (Handlers[i].tripped) {
1545 PyObject *result = NULL;
1546 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1547 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (arglist) {
1550 result = PyEval_CallObject(Handlers[i].func,
1551 arglist);
1552 Py_DECREF(arglist);
1553 }
1554 if (!result)
1555 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 Py_DECREF(result);
1558 }
1559 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001562}
1563
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001564
Barry Warsaw92971171997-01-03 00:14:25 +00001565/* Replacements for intrcheck.c functionality
1566 * Declared in pyerrors.h
1567 */
1568void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001569PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001570{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001571 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001572}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573
1574void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001575PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001576{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001577 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 Py_DECREF(m);
1580 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001581}
1582
1583void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001584PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001587}
1588
1589int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001590PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001593#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (PyThread_get_thread_ident() != main_thread)
1595 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001596#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Handlers[SIGINT].tripped = 0;
1598 return 1;
1599 }
1600 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001601}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001602
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001603static void
1604_clear_pending_signals(void)
1605{
1606 int i;
1607 if (!is_tripped)
1608 return;
1609 is_tripped = 0;
1610 for (i = 1; i < NSIG; ++i) {
1611 Handlers[i].tripped = 0;
1612 }
1613}
1614
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001615void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001616PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001617{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001618 /* Clear the signal flags after forking so that they aren't handled
1619 * in both processes if they came in just before the fork() but before
1620 * the interpreter had an opportunity to call the handlers. issue9535. */
1621 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001622#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001623 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1624 * can be called safely. */
1625 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001626 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 PyEval_ReInitThreads();
1628 main_thread = PyThread_get_thread_ident();
1629 main_pid = getpid();
1630 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001631#endif
1632}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001633
1634int
1635_PyOS_IsMainThread(void)
1636{
1637#ifdef WITH_THREAD
1638 return PyThread_get_thread_ident() == main_thread;
1639#else
1640 return 1;
1641#endif
1642}
1643
1644#ifdef MS_WINDOWS
1645void *_PyOS_SigintEvent(void)
1646{
1647 /* Returns a manual-reset event which gets tripped whenever
1648 SIGINT is received.
1649
1650 Python.h does not include windows.h so we do cannot use HANDLE
1651 as the return type of this function. We use void* instead. */
1652 return sigint_event;
1653}
1654#endif