blob: 663310881768e81bfffa50f3be317424b94fd7b3 [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
Guido van Rossum295b8e51997-06-06 21:16:41 +000088#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000089#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020090static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091static pid_t main_pid;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000092
Victor Stinner2ec6b172011-05-15 10:21:59 +020093static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +020094 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000096} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Victor Stinner11517102014-07-29 23:31:34 +020098#ifdef MS_WINDOWS
99#define INVALID_FD ((SOCKET_T)-1)
100
101static volatile struct {
102 SOCKET_T fd;
103 int use_send;
104 int send_err_set;
105 int send_errno;
106 int send_win_error;
107} wakeup = {INVALID_FD, 0, 0};
108#else
109#define INVALID_FD (-1)
Victor Stinner2ec6b172011-05-15 10:21:59 +0200110static volatile sig_atomic_t wakeup_fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200111#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000112
Christian Heimesb76922a2007-12-11 01:06:40 +0000113/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200114static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115
Barry Warsaw92971171997-01-03 00:14:25 +0000116static PyObject *DefaultHandler;
117static PyObject *IgnoreHandler;
118static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000119
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000120/* On Solaris 8, gcc will produce a warning that the function
121 declaration is not a prototype. This is caused by the definition of
122 SIG_DFL as (void (*)())0; the correct declaration would have been
123 (void (*)(int))0. */
124
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000125static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000126
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100127#ifdef MS_WINDOWS
128static HANDLE sigint_event = NULL;
129#endif
130
Martin v. Löwis823725e2008-03-24 13:39:54 +0000131#ifdef HAVE_GETITIMER
132static PyObject *ItimerError;
133
134/* auxiliary functions for setitimer/getitimer */
135static void
136timeval_from_double(double d, struct timeval *tv)
137{
138 tv->tv_sec = floor(d);
139 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200140 /* Don't disable the timer if the computation above rounds down to zero. */
141 if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) {
142 tv->tv_usec = 1;
143 }
Martin v. Löwis823725e2008-03-24 13:39:54 +0000144}
145
Christian Heimes1a8501c2008-10-02 19:56:01 +0000146Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000147double_from_timeval(struct timeval *tv)
148{
149 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
150}
151
152static PyObject *
153itimer_retval(struct itimerval *iv)
154{
155 PyObject *r, *v;
156
157 r = PyTuple_New(2);
158 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000159 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000160
161 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000162 Py_DECREF(r);
163 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000164 }
165
166 PyTuple_SET_ITEM(r, 0, v);
167
168 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000169 Py_DECREF(r);
170 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000171 }
172
173 PyTuple_SET_ITEM(r, 1, v);
174
175 return r;
176}
177#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000178
Guido van Rossume4485b01994-09-07 14:32:49 +0000179static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000180signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 PyErr_SetNone(PyExc_KeyboardInterrupt);
183 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000184}
185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000186PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000187"default_int_handler(...)\n\
188\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000189The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000190It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000191
Thomas Wouters0796b002000-07-22 23:49:30 +0000192
193static int
Victor Stinner11517102014-07-29 23:31:34 +0200194report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200195{
196 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700197 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200198 PyErr_SetFromErrno(PyExc_OSError);
199 PySys_WriteStderr("Exception ignored when trying to write to the "
200 "signal wakeup fd:\n");
201 PyErr_WriteUnraisable(NULL);
202 errno = save_errno;
203 return 0;
204}
205
Victor Stinner11517102014-07-29 23:31:34 +0200206#ifdef MS_WINDOWS
207static int
208report_wakeup_send_error(void* Py_UNUSED(data))
209{
210 PyObject *res;
211
212 if (wakeup.send_win_error) {
213 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
214 recognizes the error codes used by both GetLastError() and
215 WSAGetLastError */
216 res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
217 }
218 else {
219 errno = wakeup.send_errno;
220 res = PyErr_SetFromErrno(PyExc_OSError);
221 }
222
223 assert(res == NULL);
224 wakeup.send_err_set = 0;
225
226 PySys_WriteStderr("Exception ignored when trying to send to the "
227 "signal wakeup fd:\n");
228 PyErr_WriteUnraisable(NULL);
229
230 return 0;
231}
232#endif /* MS_WINDOWS */
233
Tim Peters4f1b2082000-07-23 21:18:09 +0000234static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200235trip_signal(int sig_num)
236{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200237 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200238 int fd;
239 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200240
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200241 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200242
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200243 /* Set is_tripped after setting .tripped, as it gets
244 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200245 _Py_atomic_store(&is_tripped, 1);
246
247 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200248 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700249
250 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200251 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
252 and then set the flag, but this allowed the following sequence of events
253 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700254
255 - main thread blocks on select([wakeup_fd], ...)
256 - signal arrives
257 - trip_signal writes to the wakeup fd
258 - the main thread wakes up
259 - the main thread checks the signal flags, sees that they're unset
260 - the main thread empties the wakeup fd
261 - the main thread goes back to sleep
262 - trip_signal sets the flags to request the Python-level signal handler
263 be run
264 - the main thread doesn't notice, because it's asleep
265
266 See bpo-30038 for more details.
267 */
268
Victor Stinner11517102014-07-29 23:31:34 +0200269#ifdef MS_WINDOWS
270 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
271#else
272 fd = wakeup_fd;
273#endif
274
275 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200276 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200277#ifdef MS_WINDOWS
278 if (wakeup.use_send) {
279 do {
280 rc = send(fd, &byte, 1, 0);
281 } while (rc < 0 && errno == EINTR);
282
283 /* we only have a storage for one error in the wakeup structure */
284 if (rc < 0 && !wakeup.send_err_set) {
285 wakeup.send_err_set = 1;
286 wakeup.send_errno = errno;
287 wakeup.send_win_error = GetLastError();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200288 /* Py_AddPendingCall() isn't signal-safe, but we
289 still use it for this exceptional case. */
Victor Stinner11517102014-07-29 23:31:34 +0200290 Py_AddPendingCall(report_wakeup_send_error, NULL);
291 }
292 }
293 else
294#endif
295 {
296 byte = (unsigned char)sig_num;
Victor Stinnere72fe392015-04-01 18:35:22 +0200297
298 /* _Py_write_noraise() retries write() if write() is interrupted by
299 a signal (fails with EINTR). */
300 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200301
302 if (rc < 0) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200303 /* Py_AddPendingCall() isn't signal-safe, but we
304 still use it for this exceptional case. */
Victor Stinner11517102014-07-29 23:31:34 +0200305 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 Pitrouf95a1b32010-05-09 15:52:27 +0000317 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000318 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200320 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000322
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000323#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000324#ifdef SIGCHLD
325 /* To avoid infinite recursion, this signal remains
326 reset until explicit re-instated.
327 Don't clear the 'func' field as it is our pointer
328 to the Python handler... */
329 if (sig_num != SIGCHLD)
330#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000332 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 * makes this true. See also issue8354. */
334 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000335#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336
337 /* Issue #10311: asynchronously executing signal handlers should not
338 mutate errno under the feet of unsuspecting C code. */
339 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100340
341#ifdef MS_WINDOWS
342 if (sig_num == SIGINT)
343 SetEvent(sigint_event);
344#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000345}
Guido van Rossume4485b01994-09-07 14:32:49 +0000346
Guido van Rossum06d511d1995-03-10 15:13:48 +0000347
Guido van Rossum1171ee61997-08-22 20:42:00 +0000348#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300349
350/*[clinic input]
351signal.alarm -> long
352
353 seconds: int
354 /
355
356Arrange for SIGALRM to arrive after the given number of seconds.
357[clinic start generated code]*/
358
359static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300360signal_alarm_impl(PyObject *module, int seconds)
361/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300364 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000365}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000366
Guido van Rossum06d511d1995-03-10 15:13:48 +0000367#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000368
Guido van Rossum1171ee61997-08-22 20:42:00 +0000369#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300370
371/*[clinic input]
372signal.pause
373
374Wait until a signal arrives.
375[clinic start generated code]*/
376
Guido van Rossuma597dde1995-01-10 20:56:29 +0000377static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300378signal_pause_impl(PyObject *module)
379/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 Py_BEGIN_ALLOW_THREADS
382 (void)pause();
383 Py_END_ALLOW_THREADS
384 /* make sure that any exceptions that got raised are propagated
385 * back into Python
386 */
387 if (PyErr_CheckSignals())
388 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000389
Tal Einatc7027b72015-05-16 14:14:49 +0300390 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000391}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000392
Guido van Rossum06d511d1995-03-10 15:13:48 +0000393#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000394
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000395
Tal Einatc7027b72015-05-16 14:14:49 +0300396/*[clinic input]
397signal.signal
398
399 signalnum: int
400 handler: object
401 /
402
403Set the action for the given signal.
404
405The action can be SIG_DFL, SIG_IGN, or a callable Python object.
406The previous action is returned. See getsignal() for possible return values.
407
408*** IMPORTANT NOTICE ***
409A signal handler function is called with two arguments:
410the first is the signal number, the second is the interrupted stack frame.
411[clinic start generated code]*/
412
Guido van Rossume4485b01994-09-07 14:32:49 +0000413static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300414signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
415/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 PyObject *old_handler;
418 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000419#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300420 /* Validate that signalnum is one of the allowable signals */
421 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000422 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000423#ifdef SIGBREAK
424 /* Issue #10003: SIGBREAK is not documented as permitted, but works
425 and corresponds to CTRL_BREAK_EVENT. */
426 case SIGBREAK: break;
427#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000428 case SIGFPE: break;
429 case SIGILL: break;
430 case SIGINT: break;
431 case SIGSEGV: break;
432 case SIGTERM: break;
433 default:
434 PyErr_SetString(PyExc_ValueError, "invalid signal value");
435 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000436 }
437#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (PyThread_get_thread_ident() != main_thread) {
439 PyErr_SetString(PyExc_ValueError,
440 "signal only works in main thread");
441 return NULL;
442 }
Tal Einatc7027b72015-05-16 14:14:49 +0300443 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 PyErr_SetString(PyExc_ValueError,
445 "signal number out of range");
446 return NULL;
447 }
Tal Einatc7027b72015-05-16 14:14:49 +0300448 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300450 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300452 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000454"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 return NULL;
456 }
457 else
458 func = signal_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300459 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200460 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return NULL;
462 }
Tal Einatc7027b72015-05-16 14:14:49 +0300463 old_handler = Handlers[signalnum].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200464 _Py_atomic_store_relaxed(&Handlers[signalnum].tripped, 0);
Tal Einatc7027b72015-05-16 14:14:49 +0300465 Py_INCREF(handler);
466 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200467 if (old_handler != NULL)
468 return old_handler;
469 else
470 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000471}
472
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000473
Tal Einatc7027b72015-05-16 14:14:49 +0300474/*[clinic input]
475signal.getsignal
476
477 signalnum: int
478 /
479
480Return the current action for the given signal.
481
482The return value can be:
483 SIG_IGN -- if the signal is being ignored
484 SIG_DFL -- if the default action for the signal is in effect
485 None -- if an unknown handler is in effect
486 anything else -- the callable Python object used as a handler
487[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000488
Guido van Rossume4485b01994-09-07 14:32:49 +0000489static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300490signal_getsignal_impl(PyObject *module, int signalnum)
491/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300494 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 PyErr_SetString(PyExc_ValueError,
496 "signal number out of range");
497 return NULL;
498 }
Tal Einatc7027b72015-05-16 14:14:49 +0300499 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200500 if (old_handler != NULL) {
501 Py_INCREF(old_handler);
502 return old_handler;
503 }
504 else {
505 Py_RETURN_NONE;
506 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000507}
508
Christian Heimes8640e742008-02-23 16:23:06 +0000509#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300510
511/*[clinic input]
512signal.siginterrupt
513
514 signalnum: int
515 flag: int
516 /
517
518Change system call restart behaviour.
519
520If flag is False, system calls will be restarted when interrupted by
521signal sig, else system calls will be interrupted.
522[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000523
524static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300525signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
526/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000527{
Tal Einatc7027b72015-05-16 14:14:49 +0300528 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyErr_SetString(PyExc_ValueError,
530 "signal number out of range");
531 return NULL;
532 }
Tal Einatc7027b72015-05-16 14:14:49 +0300533 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200534 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return NULL;
536 }
Tal Einatc7027b72015-05-16 14:14:49 +0300537 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000538}
539
540#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000541
Tal Einatc7027b72015-05-16 14:14:49 +0300542
543static PyObject*
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000544signal_set_wakeup_fd(PyObject *self, PyObject *args)
545{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200546 struct _Py_stat_struct status;
Victor Stinner11517102014-07-29 23:31:34 +0200547#ifdef MS_WINDOWS
548 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100549 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200550 int res;
551 int res_size = sizeof res;
552 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200553 int is_socket;
554
555 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
556 return NULL;
557
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100558 sockfd = PyLong_AsSocket_t(fdobj);
559 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200560 return NULL;
561#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
565 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200566#endif
567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (PyThread_get_thread_ident() != main_thread) {
569 PyErr_SetString(PyExc_ValueError,
570 "set_wakeup_fd only works in main thread");
571 return NULL;
572 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200573
Victor Stinner11517102014-07-29 23:31:34 +0200574#ifdef MS_WINDOWS
575 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100576 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200577 /* Import the _socket module to call WSAStartup() */
578 mod = PyImport_ImportModuleNoBlock("_socket");
579 if (mod == NULL)
580 return NULL;
581 Py_DECREF(mod);
582
583 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100584 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200585 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100586 int fd, err;
587
588 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200589 if (err != WSAENOTSOCK) {
590 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
591 return NULL;
592 }
593
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100594 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700595 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200596 PyErr_SetString(PyExc_ValueError, "invalid fd");
597 return NULL;
598 }
599
Victor Stinnere134a7f2015-03-30 10:09:31 +0200600 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200601 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200602
603 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200604 }
Victor Stinner38227602014-08-27 12:59:44 +0200605 else {
Victor Stinner11517102014-07-29 23:31:34 +0200606 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200607
608 /* Windows does not provide a function to test if a socket
609 is in non-blocking mode */
610 }
Victor Stinner11517102014-07-29 23:31:34 +0200611 }
612
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100613 old_sockfd = wakeup.fd;
614 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200615 wakeup.use_send = is_socket;
616
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100617 if (old_sockfd != INVALID_FD)
618 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200619 else
620 return PyLong_FromLong(-1);
621#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200622 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200623 int blocking;
624
Victor Stinnere134a7f2015-03-30 10:09:31 +0200625 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200626 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200627
628 blocking = _Py_get_blocking(fd);
629 if (blocking < 0)
630 return NULL;
631 if (blocking) {
632 PyErr_Format(PyExc_ValueError,
633 "the fd %i must be in non-blocking mode",
634 fd);
635 return NULL;
636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 old_fd = wakeup_fd;
640 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200643#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000644}
645
646PyDoc_STRVAR(set_wakeup_fd_doc,
647"set_wakeup_fd(fd) -> fd\n\
648\n\
Victor Stinner11517102014-07-29 23:31:34 +0200649Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000650comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200651The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000652\n\
653The fd must be non-blocking.");
654
655/* C API for the same, without all the error checking */
656int
657PySignal_SetWakeupFd(int fd)
658{
Victor Stinner11517102014-07-29 23:31:34 +0200659 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (fd < 0)
661 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200662
663#ifdef MS_WINDOWS
664 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
665 wakeup.fd = fd;
666#else
667 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200669#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000671}
672
673
Martin v. Löwis823725e2008-03-24 13:39:54 +0000674#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300675
676/*[clinic input]
677signal.setitimer
678
679 which: int
680 seconds: double
681 interval: double = 0.0
682 /
683
684Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
685
686The timer will fire after value seconds and after that every interval seconds.
687The itimer can be cleared by setting seconds to zero.
688
689Returns old values as a tuple: (delay, interval).
690[clinic start generated code]*/
691
Martin v. Löwis823725e2008-03-24 13:39:54 +0000692static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300693signal_setitimer_impl(PyObject *module, int which, double seconds,
Tal Einatc7027b72015-05-16 14:14:49 +0300694 double interval)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300695/*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000696{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000697 struct itimerval new, old;
698
Tal Einatc7027b72015-05-16 14:14:49 +0300699 timeval_from_double(seconds, &new.it_value);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000700 timeval_from_double(interval, &new.it_interval);
701 /* Let OS check "which" value */
702 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300703 PyErr_SetFromErrno(ItimerError);
704 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000705 }
706
707 return itimer_retval(&old);
708}
709
Martin v. Löwis823725e2008-03-24 13:39:54 +0000710#endif
711
712
713#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300714
715/*[clinic input]
716signal.getitimer
717
718 which: int
719 /
720
721Returns current value of given itimer.
722[clinic start generated code]*/
723
Martin v. Löwis823725e2008-03-24 13:39:54 +0000724static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300725signal_getitimer_impl(PyObject *module, int which)
726/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000727{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000728 struct itimerval old;
729
Martin v. Löwis823725e2008-03-24 13:39:54 +0000730 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300731 PyErr_SetFromErrno(ItimerError);
732 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000733 }
734
735 return itimer_retval(&old);
736}
737
Martin v. Löwis823725e2008-03-24 13:39:54 +0000738#endif
739
Ross Lagerwallbc808222011-06-25 12:13:40 +0200740#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
741 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200742/* Convert an iterable to a sigset.
743 Return 0 on success, return -1 and raise an exception on error. */
744
745static int
746iterable_to_sigset(PyObject *iterable, sigset_t *mask)
747{
748 int result = -1;
749 PyObject *iterator, *item;
750 long signum;
751 int err;
752
753 sigemptyset(mask);
754
755 iterator = PyObject_GetIter(iterable);
756 if (iterator == NULL)
757 goto error;
758
759 while (1)
760 {
761 item = PyIter_Next(iterator);
762 if (item == NULL) {
763 if (PyErr_Occurred())
764 goto error;
765 else
766 break;
767 }
768
769 signum = PyLong_AsLong(item);
770 Py_DECREF(item);
771 if (signum == -1 && PyErr_Occurred())
772 goto error;
773 if (0 < signum && signum < NSIG)
774 err = sigaddset(mask, (int)signum);
775 else
776 err = 1;
777 if (err) {
778 PyErr_Format(PyExc_ValueError,
779 "signal number %ld out of range", signum);
780 goto error;
781 }
782 }
783 result = 0;
784
785error:
786 Py_XDECREF(iterator);
787 return result;
788}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200789#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200790
Victor Stinnerb3e72192011-05-08 01:46:11 +0200791#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200792static PyObject*
793sigset_to_set(sigset_t mask)
794{
795 PyObject *signum, *result;
796 int sig;
797
798 result = PySet_New(0);
799 if (result == NULL)
800 return NULL;
801
802 for (sig = 1; sig < NSIG; sig++) {
803 if (sigismember(&mask, sig) != 1)
804 continue;
805
806 /* Handle the case where it is a member by adding the signal to
807 the result list. Ignore the other cases because they mean the
808 signal isn't a member of the mask or the signal was invalid,
809 and an invalid signal must have been our fault in constructing
810 the loop boundaries. */
811 signum = PyLong_FromLong(sig);
812 if (signum == NULL) {
813 Py_DECREF(result);
814 return NULL;
815 }
816 if (PySet_Add(result, signum) == -1) {
817 Py_DECREF(signum);
818 Py_DECREF(result);
819 return NULL;
820 }
821 Py_DECREF(signum);
822 }
823 return result;
824}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200825#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200826
Victor Stinnerb3e72192011-05-08 01:46:11 +0200827#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300828
829/*[clinic input]
830signal.pthread_sigmask
831
832 how: int
833 mask: object
834 /
835
836Fetch and/or change the signal mask of the calling thread.
837[clinic start generated code]*/
838
Victor Stinnera9293352011-04-30 15:21:58 +0200839static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300840signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
841/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200842{
Tal Einatc7027b72015-05-16 14:14:49 +0300843 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200844 int err;
845
Tal Einatc7027b72015-05-16 14:14:49 +0300846 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200847 return NULL;
848
Tal Einatc7027b72015-05-16 14:14:49 +0300849 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200850 if (err != 0) {
851 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200852 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200853 return NULL;
854 }
855
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200856 /* if signals was unblocked, signal handlers have been called */
857 if (PyErr_CheckSignals())
858 return NULL;
859
Victor Stinner35b300c2011-05-04 13:20:35 +0200860 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200861}
862
Victor Stinnera9293352011-04-30 15:21:58 +0200863#endif /* #ifdef PYPTHREAD_SIGMASK */
864
Martin v. Löwis823725e2008-03-24 13:39:54 +0000865
Victor Stinnerb3e72192011-05-08 01:46:11 +0200866#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300867
868/*[clinic input]
869signal.sigpending
870
871Examine pending signals.
872
873Returns a set of signal numbers that are pending for delivery to
874the calling thread.
875[clinic start generated code]*/
876
Victor Stinnerb3e72192011-05-08 01:46:11 +0200877static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300878signal_sigpending_impl(PyObject *module)
879/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200880{
881 int err;
882 sigset_t mask;
883 err = sigpending(&mask);
884 if (err)
885 return PyErr_SetFromErrno(PyExc_OSError);
886 return sigset_to_set(mask);
887}
888
Victor Stinnerb3e72192011-05-08 01:46:11 +0200889#endif /* #ifdef HAVE_SIGPENDING */
890
891
892#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300893
894/*[clinic input]
895signal.sigwait
896
897 sigset: object
898 /
899
900Wait for a signal.
901
902Suspend execution of the calling thread until the delivery of one of the
903signals specified in the signal set sigset. The function accepts the signal
904and returns the signal number.
905[clinic start generated code]*/
906
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300908signal_sigwait(PyObject *module, PyObject *sigset)
909/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200910{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200911 sigset_t set;
912 int err, signum;
913
Tal Einatc7027b72015-05-16 14:14:49 +0300914 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200915 return NULL;
916
Victor Stinner10c30d62011-06-10 01:39:53 +0200917 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200918 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200919 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200920 if (err) {
921 errno = err;
922 return PyErr_SetFromErrno(PyExc_OSError);
923 }
924
925 return PyLong_FromLong(signum);
926}
927
Tal Einatc7027b72015-05-16 14:14:49 +0300928#endif /* #ifdef HAVE_SIGWAIT */
929
Victor Stinnerb3e72192011-05-08 01:46:11 +0200930
Ross Lagerwallbc808222011-06-25 12:13:40 +0200931#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
932static int initialized;
933static PyStructSequence_Field struct_siginfo_fields[] = {
934 {"si_signo", "signal number"},
935 {"si_code", "signal code"},
936 {"si_errno", "errno associated with this signal"},
937 {"si_pid", "sending process ID"},
938 {"si_uid", "real user ID of sending process"},
939 {"si_status", "exit value or signal"},
940 {"si_band", "band event for SIGPOLL"},
941 {0}
942};
943
944PyDoc_STRVAR(struct_siginfo__doc__,
945"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
946This object may be accessed either as a tuple of\n\
947(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
948or via the attributes si_signo, si_code, and so on.");
949
950static PyStructSequence_Desc struct_siginfo_desc = {
951 "signal.struct_siginfo", /* name */
952 struct_siginfo__doc__, /* doc */
953 struct_siginfo_fields, /* fields */
954 7 /* n_in_sequence */
955};
956
957static PyTypeObject SiginfoType;
958
959static PyObject *
960fill_siginfo(siginfo_t *si)
961{
962 PyObject *result = PyStructSequence_New(&SiginfoType);
963 if (!result)
964 return NULL;
965
966 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
967 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
968 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
969 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200970 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200971 PyStructSequence_SET_ITEM(result, 5,
972 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500973#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +0200974 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500975#else
976 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
977#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200978 if (PyErr_Occurred()) {
979 Py_DECREF(result);
980 return NULL;
981 }
982
983 return result;
984}
985#endif
986
987#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300988
989/*[clinic input]
990signal.sigwaitinfo
991
992 sigset: object
993 /
994
995Wait synchronously until one of the signals in *sigset* is delivered.
996
997Returns a struct_siginfo containing information about the signal.
998[clinic start generated code]*/
999
Ross Lagerwallbc808222011-06-25 12:13:40 +02001000static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001001signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1002/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001003{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001004 sigset_t set;
1005 siginfo_t si;
1006 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001007 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001008
Tal Einatc7027b72015-05-16 14:14:49 +03001009 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001010 return NULL;
1011
Victor Stinnera453cd82015-03-20 12:54:28 +01001012 do {
1013 Py_BEGIN_ALLOW_THREADS
1014 err = sigwaitinfo(&set, &si);
1015 Py_END_ALLOW_THREADS
1016 } while (err == -1
1017 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001018 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001019 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001020
1021 return fill_siginfo(&si);
1022}
1023
Ross Lagerwallbc808222011-06-25 12:13:40 +02001024#endif /* #ifdef HAVE_SIGWAITINFO */
1025
1026#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001027
1028/*[clinic input]
1029signal.sigtimedwait
1030
1031 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001032 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001033 /
1034
1035Like sigwaitinfo(), but with a timeout.
1036
1037The timeout is specified in seconds, with floating point numbers allowed.
1038[clinic start generated code]*/
1039
Ross Lagerwallbc808222011-06-25 12:13:40 +02001040static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001041signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001042 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001043/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001044{
Victor Stinnera453cd82015-03-20 12:54:28 +01001045 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001046 sigset_t set;
1047 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001048 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001049 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001050
Victor Stinner869e1772015-03-30 03:49:14 +02001051 if (_PyTime_FromSecondsObject(&timeout,
1052 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001053 return NULL;
1054
Victor Stinnera453cd82015-03-20 12:54:28 +01001055 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001056 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1057 return NULL;
1058 }
1059
Tal Einatc7027b72015-05-16 14:14:49 +03001060 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001061 return NULL;
1062
Victor Stinner34dc0f42015-03-27 18:19:03 +01001063 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001064
1065 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001066 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1067 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001068
1069 Py_BEGIN_ALLOW_THREADS
1070 res = sigtimedwait(&set, &si, &ts);
1071 Py_END_ALLOW_THREADS
1072
1073 if (res != -1)
1074 break;
1075
1076 if (errno != EINTR) {
1077 if (errno == EAGAIN)
1078 Py_RETURN_NONE;
1079 else
1080 return PyErr_SetFromErrno(PyExc_OSError);
1081 }
1082
1083 /* sigtimedwait() was interrupted by a signal (EINTR) */
1084 if (PyErr_CheckSignals())
1085 return NULL;
1086
Victor Stinner34dc0f42015-03-27 18:19:03 +01001087 monotonic = _PyTime_GetMonotonicClock();
1088 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001089 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001090 break;
1091 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001092
1093 return fill_siginfo(&si);
1094}
1095
Ross Lagerwallbc808222011-06-25 12:13:40 +02001096#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1097
Victor Stinnerb3e72192011-05-08 01:46:11 +02001098
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001099#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001100
1101/*[clinic input]
1102signal.pthread_kill
1103
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001104 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001105 signalnum: int
1106 /
1107
1108Send a signal to a thread.
1109[clinic start generated code]*/
1110
Victor Stinnerb3e72192011-05-08 01:46:11 +02001111static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001112signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1113 int signalnum)
1114/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001115{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001116 int err;
1117
Tal Einatc7027b72015-05-16 14:14:49 +03001118 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001119 if (err != 0) {
1120 errno = err;
1121 PyErr_SetFromErrno(PyExc_OSError);
1122 return NULL;
1123 }
1124
1125 /* the signal may have been send to the current thread */
1126 if (PyErr_CheckSignals())
1127 return NULL;
1128
1129 Py_RETURN_NONE;
1130}
1131
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001132#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001133
1134
1135
Tal Einatc7027b72015-05-16 14:14:49 +03001136/* List of functions defined in the module -- some of the methoddefs are
1137 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001138static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001139 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1140 SIGNAL_ALARM_METHODDEF
1141 SIGNAL_SETITIMER_METHODDEF
1142 SIGNAL_GETITIMER_METHODDEF
1143 SIGNAL_SIGNAL_METHODDEF
1144 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001146 SIGNAL_SIGINTERRUPT_METHODDEF
1147 SIGNAL_PAUSE_METHODDEF
1148 SIGNAL_PTHREAD_KILL_METHODDEF
1149 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1150 SIGNAL_SIGPENDING_METHODDEF
1151 SIGNAL_SIGWAIT_METHODDEF
1152 SIGNAL_SIGWAITINFO_METHODDEF
1153 SIGNAL_SIGTIMEDWAIT_METHODDEF
1154 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001155};
1156
Barry Warsaw92971171997-01-03 00:14:25 +00001157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001159"This module provides mechanisms to use signal handlers in Python.\n\
1160\n\
1161Functions:\n\
1162\n\
1163alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001164setitimer() -- cause a signal (described below) after a specified\n\
1165 float time and the timer may restart then [Unix only]\n\
1166getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001167signal() -- set the action for a given signal\n\
1168getsignal() -- get the signal action for a given signal\n\
1169pause() -- wait until a signal arrives [Unix only]\n\
1170default_int_handler() -- default SIGINT handler\n\
1171\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001172signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001173SIG_DFL -- used to refer to the system default handler\n\
1174SIG_IGN -- used to ignore the signal\n\
1175NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001176SIGINT, SIGTERM, etc. -- signal numbers\n\
1177\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001178itimer constants:\n\
1179ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1180 expiration\n\
1181ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1182 and delivers SIGVTALRM upon expiration\n\
1183ITIMER_PROF -- decrements both when the process is executing and\n\
1184 when the system is executing on behalf of the process.\n\
1185 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1186 used to profile the time spent by the application\n\
1187 in user and kernel space. SIGPROF is delivered upon\n\
1188 expiration.\n\
1189\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001190*** IMPORTANT NOTICE ***\n\
1191A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001193
Martin v. Löwis1a214512008-06-11 05:26:20 +00001194static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001196 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 module_doc,
1198 -1,
1199 signal_methods,
1200 NULL,
1201 NULL,
1202 NULL,
1203 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001204};
1205
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001206PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001207PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 PyObject *m, *d, *x;
1210 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 main_thread = PyThread_get_thread_ident();
1213 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 /* Create the module and add the functions */
1216 m = PyModule_Create(&signalmodule);
1217 if (m == NULL)
1218 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001219
Ross Lagerwallbc808222011-06-25 12:13:40 +02001220#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001221 if (!initialized) {
1222 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1223 return NULL;
1224 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001225 Py_INCREF((PyObject*) &SiginfoType);
1226 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1227 initialized = 1;
1228#endif
1229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 /* Add some symbolic constants to the module */
1231 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1234 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1235 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1238 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1239 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 x = PyLong_FromLong((long)NSIG);
1242 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1243 goto finally;
1244 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001245
Victor Stinnera9293352011-04-30 15:21:58 +02001246#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001247 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1248 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001249#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001250#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001251 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1252 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001253#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001254#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001255 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1256 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001257#endif
1258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1260 if (!x)
1261 goto finally;
1262 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001263
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001264 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 for (i = 1; i < NSIG; i++) {
1266 void (*t)(int);
1267 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001268 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (t == SIG_DFL)
1270 Handlers[i].func = DefaultHandler;
1271 else if (t == SIG_IGN)
1272 Handlers[i].func = IgnoreHandler;
1273 else
1274 Handlers[i].func = Py_None; /* None of our business */
1275 Py_INCREF(Handlers[i].func);
1276 }
1277 if (Handlers[SIGINT].func == DefaultHandler) {
1278 /* Install default int handler */
1279 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001280 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1282 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001283
1284#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001285 if (PyModule_AddIntMacro(m, SIGHUP))
1286 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001287#endif
1288#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001289 if (PyModule_AddIntMacro(m, SIGINT))
1290 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001291#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001292#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001293 if (PyModule_AddIntMacro(m, SIGBREAK))
1294 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001295#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001296#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001297 if (PyModule_AddIntMacro(m, SIGQUIT))
1298 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299#endif
1300#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001301 if (PyModule_AddIntMacro(m, SIGILL))
1302 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303#endif
1304#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001305 if (PyModule_AddIntMacro(m, SIGTRAP))
1306 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001307#endif
1308#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001309 if (PyModule_AddIntMacro(m, SIGIOT))
1310 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001311#endif
1312#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001313 if (PyModule_AddIntMacro(m, SIGABRT))
1314 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001315#endif
1316#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001317 if (PyModule_AddIntMacro(m, SIGEMT))
1318 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001319#endif
1320#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001321 if (PyModule_AddIntMacro(m, SIGFPE))
1322 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001323#endif
1324#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001325 if (PyModule_AddIntMacro(m, SIGKILL))
1326 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001327#endif
1328#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001329 if (PyModule_AddIntMacro(m, SIGBUS))
1330 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001331#endif
1332#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001333 if (PyModule_AddIntMacro(m, SIGSEGV))
1334 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001335#endif
1336#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001337 if (PyModule_AddIntMacro(m, SIGSYS))
1338 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339#endif
1340#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001341 if (PyModule_AddIntMacro(m, SIGPIPE))
1342 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001343#endif
1344#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001345 if (PyModule_AddIntMacro(m, SIGALRM))
1346 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001347#endif
1348#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001349 if (PyModule_AddIntMacro(m, SIGTERM))
1350 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001351#endif
1352#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001353 if (PyModule_AddIntMacro(m, SIGUSR1))
1354 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001355#endif
1356#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001357 if (PyModule_AddIntMacro(m, SIGUSR2))
1358 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001359#endif
1360#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001361 if (PyModule_AddIntMacro(m, SIGCLD))
1362 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001363#endif
1364#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001365 if (PyModule_AddIntMacro(m, SIGCHLD))
1366 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001367#endif
1368#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001369 if (PyModule_AddIntMacro(m, SIGPWR))
1370 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001371#endif
1372#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001373 if (PyModule_AddIntMacro(m, SIGIO))
1374 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001375#endif
1376#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001377 if (PyModule_AddIntMacro(m, SIGURG))
1378 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001379#endif
1380#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001381 if (PyModule_AddIntMacro(m, SIGWINCH))
1382 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383#endif
1384#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001385 if (PyModule_AddIntMacro(m, SIGPOLL))
1386 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001387#endif
1388#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001389 if (PyModule_AddIntMacro(m, SIGSTOP))
1390 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001391#endif
1392#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001393 if (PyModule_AddIntMacro(m, SIGTSTP))
1394 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001395#endif
1396#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001397 if (PyModule_AddIntMacro(m, SIGCONT))
1398 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001399#endif
1400#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001401 if (PyModule_AddIntMacro(m, SIGTTIN))
1402 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403#endif
1404#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001405 if (PyModule_AddIntMacro(m, SIGTTOU))
1406 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001407#endif
1408#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001409 if (PyModule_AddIntMacro(m, SIGVTALRM))
1410 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001411#endif
1412#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001413 if (PyModule_AddIntMacro(m, SIGPROF))
1414 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001415#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001416#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001417 if (PyModule_AddIntMacro(m, SIGXCPU))
1418 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001419#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001420#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001421 if (PyModule_AddIntMacro(m, SIGXFSZ))
1422 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001423#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001424#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001425 if (PyModule_AddIntMacro(m, SIGRTMIN))
1426 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001427#endif
1428#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001429 if (PyModule_AddIntMacro(m, SIGRTMAX))
1430 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001431#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001432#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001433 if (PyModule_AddIntMacro(m, SIGINFO))
1434 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001435#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001436
1437#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001438 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1439 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001440#endif
1441#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001442 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1443 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001444#endif
1445#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001446 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1447 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001448#endif
1449
1450#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001452 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001453 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001454 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001455#endif
1456
Brian Curtineb24d742010-04-12 17:16:38 +00001457#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001458 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1459 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001460#endif
1461
1462#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1464 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001465#endif
1466
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001467#ifdef MS_WINDOWS
1468 /* Create manual-reset event, initially unset */
1469 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1470#endif
1471
Martin v. Löwis1a214512008-06-11 05:26:20 +00001472 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 Py_DECREF(m);
1474 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001475 }
Barry Warsaw92971171997-01-03 00:14:25 +00001476
Barry Warsaw92971171997-01-03 00:14:25 +00001477 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001478 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001479}
1480
1481static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001482finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 int i;
1485 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 PyOS_setsig(SIGINT, old_siginthandler);
1488 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 for (i = 1; i < NSIG; i++) {
1491 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001492 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 Handlers[i].func = NULL;
1494 if (i != SIGINT && func != NULL && func != Py_None &&
1495 func != DefaultHandler && func != IgnoreHandler)
1496 PyOS_setsig(i, SIG_DFL);
1497 Py_XDECREF(func);
1498 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001499
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001500 Py_CLEAR(IntHandler);
1501 Py_CLEAR(DefaultHandler);
1502 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001503}
1504
Barry Warsaw92971171997-01-03 00:14:25 +00001505
Barry Warsaw92971171997-01-03 00:14:25 +00001506/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001507int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001508PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 int i;
1511 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001512
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001513 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (PyThread_get_thread_ident() != main_thread)
1517 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 /*
1520 * The is_tripped variable is meant to speed up the calls to
1521 * PyErr_CheckSignals (both directly or via pending calls) when no
1522 * signal has arrived. This variable is set to 1 when a signal arrives
1523 * and it is set to 0 here, when we know some signals arrived. This way
1524 * we can run the registered handlers with no signals blocked.
1525 *
1526 * NOTE: with this approach we can have a situation where is_tripped is
1527 * 1 but we have no more signals to handle (Handlers[i].tripped
1528 * is 0 for every signal i). This won't do us any harm (except
1529 * we're gonna spent some cycles for nothing). This happens when
1530 * we receive a signal i after we zero is_tripped and before we
1531 * check Handlers[i].tripped.
1532 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001533 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (!(f = (PyObject *)PyEval_GetFrame()))
1536 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001539 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 PyObject *result = NULL;
1541 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001542 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (arglist) {
1545 result = PyEval_CallObject(Handlers[i].func,
1546 arglist);
1547 Py_DECREF(arglist);
1548 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001549 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001550 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001552 }
Barry Warsaw92971171997-01-03 00:14:25 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 Py_DECREF(result);
1555 }
1556 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001559}
1560
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001561
Barry Warsaw92971171997-01-03 00:14:25 +00001562/* Replacements for intrcheck.c functionality
1563 * Declared in pyerrors.h
1564 */
1565void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001566PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001567{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001568 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001569}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001570
1571void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001572PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001574 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 Py_DECREF(m);
1577 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001578}
1579
1580void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001581PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001584}
1585
1586int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001587PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001588{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001589 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 if (PyThread_get_thread_ident() != main_thread)
1591 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001592 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return 1;
1594 }
1595 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001596}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001597
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001598static void
1599_clear_pending_signals(void)
1600{
1601 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001602 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001603 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001604 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001605 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001606 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001607 }
1608}
1609
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001610void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001611_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001612{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001613 /* Clear the signal flags after forking so that they aren't handled
1614 * in both processes if they came in just before the fork() but before
1615 * the interpreter had an opportunity to call the handlers. issue9535. */
1616 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 main_thread = PyThread_get_thread_ident();
1618 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001619}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001620
1621int
1622_PyOS_IsMainThread(void)
1623{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001624 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001625}
1626
1627#ifdef MS_WINDOWS
1628void *_PyOS_SigintEvent(void)
1629{
1630 /* Returns a manual-reset event which gets tripped whenever
1631 SIGINT is received.
1632
1633 Python.h does not include windows.h so we do cannot use HANDLE
1634 as the return type of this function. We use void* instead. */
1635 return sigint_event;
1636}
1637#endif