blob: 818df7d46e4c80d64ed50456693dc57a1afee8d2 [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
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030062/*[python input]
63
64class sigset_t_converter(CConverter):
65 type = 'sigset_t'
66 converter = '_Py_Sigset_Converter'
67
68[python start generated code]*/
69/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000070
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071/*
72 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
73
74 When threads are supported, we want the following semantics:
75
76 - only the main thread can set a signal handler
77 - any thread can get a signal handler
78 - signals are only delivered to the main thread
79
80 I.e. we don't support "synchronous signals" like SIGFPE (catching
81 this doesn't make much sense in Python anyway) nor do we support
82 signals as a means of inter-thread communication, since not all
83 thread implementations support that (at least our thread library
84 doesn't).
85
86 We still have the problem that in some implementations signals
87 generated by the keyboard (e.g. SIGINT) are delivered to all
88 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
89 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000090 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091 a working implementation that works in all three cases -- the
92 handler ignores signals if getpid() isn't the same as in the main
93 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000094*/
95
Guido van Rossum295b8e51997-06-06 21:16:41 +000096#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000097#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020098static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000099static pid_t main_pid;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000100
Victor Stinner2ec6b172011-05-15 10:21:59 +0200101static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200102 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000104} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000105
Victor Stinner11517102014-07-29 23:31:34 +0200106#ifdef MS_WINDOWS
107#define INVALID_FD ((SOCKET_T)-1)
108
109static volatile struct {
110 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800111 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200112 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800113} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200114#else
115#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800116static volatile struct {
117 sig_atomic_t fd;
118 int warn_on_full_buffer;
119} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200120#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000121
Christian Heimesb76922a2007-12-11 01:06:40 +0000122/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200123static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000124
Barry Warsaw92971171997-01-03 00:14:25 +0000125static PyObject *DefaultHandler;
126static PyObject *IgnoreHandler;
127static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000128
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000129/* On Solaris 8, gcc will produce a warning that the function
130 declaration is not a prototype. This is caused by the definition of
131 SIG_DFL as (void (*)())0; the correct declaration would have been
132 (void (*)(int))0. */
133
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000134static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000135
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100136#ifdef MS_WINDOWS
137static HANDLE sigint_event = NULL;
138#endif
139
Martin v. Löwis823725e2008-03-24 13:39:54 +0000140#ifdef HAVE_GETITIMER
141static PyObject *ItimerError;
142
Victor Stinneref611c92017-10-13 13:49:43 -0700143/* auxiliary functions for setitimer */
144static int
145timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000146{
Victor Stinneref611c92017-10-13 13:49:43 -0700147 if (obj == NULL) {
148 tv->tv_sec = 0;
149 tv->tv_usec = 0;
150 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200151 }
Victor Stinneref611c92017-10-13 13:49:43 -0700152
153 _PyTime_t t;
154 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
155 return -1;
156 }
157 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158}
159
Christian Heimes1a8501c2008-10-02 19:56:01 +0000160Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000161double_from_timeval(struct timeval *tv)
162{
163 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
164}
165
166static PyObject *
167itimer_retval(struct itimerval *iv)
168{
169 PyObject *r, *v;
170
171 r = PyTuple_New(2);
172 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000173 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000174
175 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000176 Py_DECREF(r);
177 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000178 }
179
180 PyTuple_SET_ITEM(r, 0, v);
181
182 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000183 Py_DECREF(r);
184 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000185 }
186
187 PyTuple_SET_ITEM(r, 1, v);
188
189 return r;
190}
191#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000192
Guido van Rossume4485b01994-09-07 14:32:49 +0000193static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000194signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyErr_SetNone(PyExc_KeyboardInterrupt);
197 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000201"default_int_handler(...)\n\
202\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000203The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000205
Thomas Wouters0796b002000-07-22 23:49:30 +0000206
207static int
Victor Stinner11517102014-07-29 23:31:34 +0200208report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200209{
210 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700211 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200212 PyErr_SetFromErrno(PyExc_OSError);
213 PySys_WriteStderr("Exception ignored when trying to write to the "
214 "signal wakeup fd:\n");
215 PyErr_WriteUnraisable(NULL);
216 errno = save_errno;
217 return 0;
218}
219
Victor Stinner11517102014-07-29 23:31:34 +0200220#ifdef MS_WINDOWS
221static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800222report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200223{
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800224 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
225 recognizes the error codes used by both GetLastError() and
226 WSAGetLastError */
227 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200228 PySys_WriteStderr("Exception ignored when trying to send to the "
229 "signal wakeup fd:\n");
230 PyErr_WriteUnraisable(NULL);
Victor Stinner11517102014-07-29 23:31:34 +0200231 return 0;
232}
233#endif /* MS_WINDOWS */
234
Tim Peters4f1b2082000-07-23 21:18:09 +0000235static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200236trip_signal(int sig_num)
237{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200238 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200239 int fd;
240 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200241
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200242 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200243
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200244 /* Set is_tripped after setting .tripped, as it gets
245 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200246 _Py_atomic_store(&is_tripped, 1);
247
248 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200249 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700250
251 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200252 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
253 and then set the flag, but this allowed the following sequence of events
254 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700255
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800256 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700257 - signal arrives
258 - trip_signal writes to the wakeup fd
259 - the main thread wakes up
260 - the main thread checks the signal flags, sees that they're unset
261 - the main thread empties the wakeup fd
262 - the main thread goes back to sleep
263 - trip_signal sets the flags to request the Python-level signal handler
264 be run
265 - the main thread doesn't notice, because it's asleep
266
267 See bpo-30038 for more details.
268 */
269
Victor Stinner11517102014-07-29 23:31:34 +0200270#ifdef MS_WINDOWS
271 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
272#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800273 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200274#endif
275
276 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200277 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200278#ifdef MS_WINDOWS
279 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800280 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200281
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800282 if (rc < 0) {
283 int last_error = GetLastError();
284 if (wakeup.warn_on_full_buffer ||
285 last_error != WSAEWOULDBLOCK)
286 {
287 /* Py_AddPendingCall() isn't signal-safe, but we
288 still use it for this exceptional case. */
289 Py_AddPendingCall(report_wakeup_send_error,
290 (void *)(intptr_t) last_error);
291 }
Victor Stinner11517102014-07-29 23:31:34 +0200292 }
293 }
294 else
295#endif
296 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200297 /* _Py_write_noraise() retries write() if write() is interrupted by
298 a signal (fails with EINTR). */
299 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200300
301 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800302 if (wakeup.warn_on_full_buffer ||
303 (errno != EWOULDBLOCK && errno != EAGAIN))
304 {
305 /* Py_AddPendingCall() isn't signal-safe, but we
306 still use it for this exceptional case. */
307 Py_AddPendingCall(report_wakeup_write_error,
308 (void *)(intptr_t)errno);
309 }
Victor Stinner11517102014-07-29 23:31:34 +0200310 }
311 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200312 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200313}
314
315static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000316signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000317{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000318 int save_errno = errno;
319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000321 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000322 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200323 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000325
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000326#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000327#ifdef SIGCHLD
328 /* To avoid infinite recursion, this signal remains
329 reset until explicit re-instated.
330 Don't clear the 'func' field as it is our pointer
331 to the Python handler... */
332 if (sig_num != SIGCHLD)
333#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000335 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 * makes this true. See also issue8354. */
337 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000338#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000339
340 /* Issue #10311: asynchronously executing signal handlers should not
341 mutate errno under the feet of unsuspecting C code. */
342 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100343
344#ifdef MS_WINDOWS
345 if (sig_num == SIGINT)
346 SetEvent(sigint_event);
347#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348}
Guido van Rossume4485b01994-09-07 14:32:49 +0000349
Guido van Rossum06d511d1995-03-10 15:13:48 +0000350
Guido van Rossum1171ee61997-08-22 20:42:00 +0000351#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300352
353/*[clinic input]
354signal.alarm -> long
355
356 seconds: int
357 /
358
359Arrange for SIGALRM to arrive after the given number of seconds.
360[clinic start generated code]*/
361
362static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300363signal_alarm_impl(PyObject *module, int seconds)
364/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300367 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000368}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000369
Guido van Rossum06d511d1995-03-10 15:13:48 +0000370#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000371
Guido van Rossum1171ee61997-08-22 20:42:00 +0000372#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300373
374/*[clinic input]
375signal.pause
376
377Wait until a signal arrives.
378[clinic start generated code]*/
379
Guido van Rossuma597dde1995-01-10 20:56:29 +0000380static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300381signal_pause_impl(PyObject *module)
382/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 Py_BEGIN_ALLOW_THREADS
385 (void)pause();
386 Py_END_ALLOW_THREADS
387 /* make sure that any exceptions that got raised are propagated
388 * back into Python
389 */
390 if (PyErr_CheckSignals())
391 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000392
Tal Einatc7027b72015-05-16 14:14:49 +0300393 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000394}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000395
Guido van Rossum06d511d1995-03-10 15:13:48 +0000396#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000397
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000398
Tal Einatc7027b72015-05-16 14:14:49 +0300399/*[clinic input]
400signal.signal
401
402 signalnum: int
403 handler: object
404 /
405
406Set the action for the given signal.
407
408The action can be SIG_DFL, SIG_IGN, or a callable Python object.
409The previous action is returned. See getsignal() for possible return values.
410
411*** IMPORTANT NOTICE ***
412A signal handler function is called with two arguments:
413the first is the signal number, the second is the interrupted stack frame.
414[clinic start generated code]*/
415
Guido van Rossume4485b01994-09-07 14:32:49 +0000416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300417signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
418/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 PyObject *old_handler;
421 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000422#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300423 /* Validate that signalnum is one of the allowable signals */
424 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000425 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000426#ifdef SIGBREAK
427 /* Issue #10003: SIGBREAK is not documented as permitted, but works
428 and corresponds to CTRL_BREAK_EVENT. */
429 case SIGBREAK: break;
430#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000431 case SIGFPE: break;
432 case SIGILL: break;
433 case SIGINT: break;
434 case SIGSEGV: break;
435 case SIGTERM: break;
436 default:
437 PyErr_SetString(PyExc_ValueError, "invalid signal value");
438 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000439 }
440#endif
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 }
Tal Einatc7027b72015-05-16 14:14:49 +0300446 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 PyErr_SetString(PyExc_ValueError,
448 "signal number out of range");
449 return NULL;
450 }
Tal Einatc7027b72015-05-16 14:14:49 +0300451 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300453 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300455 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000457"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return NULL;
459 }
460 else
461 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100462 /* Check for pending signals before changing signal handler */
463 if (PyErr_CheckSignals()) {
464 return NULL;
465 }
Tal Einatc7027b72015-05-16 14:14:49 +0300466 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200467 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 return NULL;
469 }
Tal Einatc7027b72015-05-16 14:14:49 +0300470 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300471 Py_INCREF(handler);
472 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200473 if (old_handler != NULL)
474 return old_handler;
475 else
476 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477}
478
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000479
Tal Einatc7027b72015-05-16 14:14:49 +0300480/*[clinic input]
481signal.getsignal
482
483 signalnum: int
484 /
485
486Return the current action for the given signal.
487
488The return value can be:
489 SIG_IGN -- if the signal is being ignored
490 SIG_DFL -- if the default action for the signal is in effect
491 None -- if an unknown handler is in effect
492 anything else -- the callable Python object used as a handler
493[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000494
Guido van Rossume4485b01994-09-07 14:32:49 +0000495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300496signal_getsignal_impl(PyObject *module, int signalnum)
497/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300500 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 PyErr_SetString(PyExc_ValueError,
502 "signal number out of range");
503 return NULL;
504 }
Tal Einatc7027b72015-05-16 14:14:49 +0300505 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200506 if (old_handler != NULL) {
507 Py_INCREF(old_handler);
508 return old_handler;
509 }
510 else {
511 Py_RETURN_NONE;
512 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513}
514
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100515
516/*[clinic input]
517signal.strsignal
518
519 signalnum: int
520 /
521
522Return the system description of the given signal.
523
524The return values can be such as "Interrupt", "Segmentation fault", etc.
525Returns None if the signal is not recognized.
526[clinic start generated code]*/
527
528static PyObject *
529signal_strsignal_impl(PyObject *module, int signalnum)
530/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
531{
532 char *res;
533
534 if (signalnum < 1 || signalnum >= NSIG) {
535 PyErr_SetString(PyExc_ValueError,
536 "signal number out of range");
537 return NULL;
538 }
539
540#ifdef MS_WINDOWS
541 /* Custom redefinition of POSIX signals allowed on Windows */
542 switch (signalnum) {
543 case SIGINT:
544 res = "Interrupt";
545 break;
546 case SIGILL:
547 res = "Illegal instruction";
548 break;
549 case SIGABRT:
550 res = "Aborted";
551 break;
552 case SIGFPE:
553 res = "Floating point exception";
554 break;
555 case SIGSEGV:
556 res = "Segmentation fault";
557 break;
558 case SIGTERM:
559 res = "Terminated";
560 break;
561 default:
562 Py_RETURN_NONE;
563 }
564#else
565 errno = 0;
566 res = strsignal(signalnum);
567
568 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
569 Py_RETURN_NONE;
570#endif
571
572 return Py_BuildValue("s", res);
573}
574
Christian Heimes8640e742008-02-23 16:23:06 +0000575#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300576
577/*[clinic input]
578signal.siginterrupt
579
580 signalnum: int
581 flag: int
582 /
583
584Change system call restart behaviour.
585
586If flag is False, system calls will be restarted when interrupted by
587signal sig, else system calls will be interrupted.
588[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000589
590static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300591signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
592/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000593{
Tal Einatc7027b72015-05-16 14:14:49 +0300594 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyErr_SetString(PyExc_ValueError,
596 "signal number out of range");
597 return NULL;
598 }
Tal Einatc7027b72015-05-16 14:14:49 +0300599 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200600 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return NULL;
602 }
Tal Einatc7027b72015-05-16 14:14:49 +0300603 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000604}
605
606#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000607
Tal Einatc7027b72015-05-16 14:14:49 +0300608
609static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800610signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000611{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200612 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800613 static char *kwlist[] = {
614 "", "warn_on_full_buffer", NULL,
615 };
616 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200617#ifdef MS_WINDOWS
618 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100619 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200620 int res;
621 int res_size = sizeof res;
622 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200623 int is_socket;
624
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800625 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
626 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200627 return NULL;
628
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100629 sockfd = PyLong_AsSocket_t(fdobj);
630 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200631 return NULL;
632#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200634
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800635 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
636 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200638#endif
639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (PyThread_get_thread_ident() != main_thread) {
641 PyErr_SetString(PyExc_ValueError,
642 "set_wakeup_fd only works in main thread");
643 return NULL;
644 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200645
Victor Stinner11517102014-07-29 23:31:34 +0200646#ifdef MS_WINDOWS
647 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100648 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200649 /* Import the _socket module to call WSAStartup() */
650 mod = PyImport_ImportModuleNoBlock("_socket");
651 if (mod == NULL)
652 return NULL;
653 Py_DECREF(mod);
654
655 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100656 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200657 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100658 int fd, err;
659
660 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200661 if (err != WSAENOTSOCK) {
662 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
663 return NULL;
664 }
665
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100666 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700667 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200668 PyErr_SetString(PyExc_ValueError, "invalid fd");
669 return NULL;
670 }
671
Victor Stinnere134a7f2015-03-30 10:09:31 +0200672 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200673 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200674
675 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200676 }
Victor Stinner38227602014-08-27 12:59:44 +0200677 else {
Victor Stinner11517102014-07-29 23:31:34 +0200678 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200679
680 /* Windows does not provide a function to test if a socket
681 is in non-blocking mode */
682 }
Victor Stinner11517102014-07-29 23:31:34 +0200683 }
684
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100685 old_sockfd = wakeup.fd;
686 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800687 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200688 wakeup.use_send = is_socket;
689
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100690 if (old_sockfd != INVALID_FD)
691 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200692 else
693 return PyLong_FromLong(-1);
694#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200695 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200696 int blocking;
697
Victor Stinnere134a7f2015-03-30 10:09:31 +0200698 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200699 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200700
701 blocking = _Py_get_blocking(fd);
702 if (blocking < 0)
703 return NULL;
704 if (blocking) {
705 PyErr_Format(PyExc_ValueError,
706 "the fd %i must be in non-blocking mode",
707 fd);
708 return NULL;
709 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200711
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800712 old_fd = wakeup.fd;
713 wakeup.fd = fd;
714 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200717#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000718}
719
720PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800721"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000722\n\
Victor Stinner11517102014-07-29 23:31:34 +0200723Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000724comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200725The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000726\n\
727The fd must be non-blocking.");
728
729/* C API for the same, without all the error checking */
730int
731PySignal_SetWakeupFd(int fd)
732{
Victor Stinner11517102014-07-29 23:31:34 +0200733 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (fd < 0)
735 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200736
737#ifdef MS_WINDOWS
738 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200739#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800740 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200741#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800742 wakeup.fd = fd;
743 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000745}
746
747
Martin v. Löwis823725e2008-03-24 13:39:54 +0000748#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300749
750/*[clinic input]
751signal.setitimer
752
753 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700754 seconds: object
755 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300756 /
757
758Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
759
760The timer will fire after value seconds and after that every interval seconds.
761The itimer can be cleared by setting seconds to zero.
762
763Returns old values as a tuple: (delay, interval).
764[clinic start generated code]*/
765
Martin v. Löwis823725e2008-03-24 13:39:54 +0000766static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700767signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
768 PyObject *interval)
769/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000770{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000771 struct itimerval new, old;
772
Victor Stinneref611c92017-10-13 13:49:43 -0700773 if (timeval_from_double(seconds, &new.it_value) < 0) {
774 return NULL;
775 }
776 if (timeval_from_double(interval, &new.it_interval) < 0) {
777 return NULL;
778 }
779
Martin v. Löwis823725e2008-03-24 13:39:54 +0000780 /* Let OS check "which" value */
781 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300782 PyErr_SetFromErrno(ItimerError);
783 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000784 }
785
786 return itimer_retval(&old);
787}
788
Martin v. Löwis823725e2008-03-24 13:39:54 +0000789#endif
790
791
792#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300793
794/*[clinic input]
795signal.getitimer
796
797 which: int
798 /
799
800Returns current value of given itimer.
801[clinic start generated code]*/
802
Martin v. Löwis823725e2008-03-24 13:39:54 +0000803static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300804signal_getitimer_impl(PyObject *module, int which)
805/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000806{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000807 struct itimerval old;
808
Martin v. Löwis823725e2008-03-24 13:39:54 +0000809 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300810 PyErr_SetFromErrno(ItimerError);
811 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000812 }
813
814 return itimer_retval(&old);
815}
816
Martin v. Löwis823725e2008-03-24 13:39:54 +0000817#endif
818
Victor Stinnerb3e72192011-05-08 01:46:11 +0200819#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200820static PyObject*
821sigset_to_set(sigset_t mask)
822{
823 PyObject *signum, *result;
824 int sig;
825
826 result = PySet_New(0);
827 if (result == NULL)
828 return NULL;
829
830 for (sig = 1; sig < NSIG; sig++) {
831 if (sigismember(&mask, sig) != 1)
832 continue;
833
834 /* Handle the case where it is a member by adding the signal to
835 the result list. Ignore the other cases because they mean the
836 signal isn't a member of the mask or the signal was invalid,
837 and an invalid signal must have been our fault in constructing
838 the loop boundaries. */
839 signum = PyLong_FromLong(sig);
840 if (signum == NULL) {
841 Py_DECREF(result);
842 return NULL;
843 }
844 if (PySet_Add(result, signum) == -1) {
845 Py_DECREF(signum);
846 Py_DECREF(result);
847 return NULL;
848 }
849 Py_DECREF(signum);
850 }
851 return result;
852}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200853#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200854
Victor Stinnerb3e72192011-05-08 01:46:11 +0200855#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300856
857/*[clinic input]
858signal.pthread_sigmask
859
860 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300861 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300862 /
863
864Fetch and/or change the signal mask of the calling thread.
865[clinic start generated code]*/
866
Victor Stinnera9293352011-04-30 15:21:58 +0200867static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300868signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
869/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200870{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300871 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200872 int err;
873
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300874 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200875 if (err != 0) {
876 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200877 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200878 return NULL;
879 }
880
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200881 /* if signals was unblocked, signal handlers have been called */
882 if (PyErr_CheckSignals())
883 return NULL;
884
Victor Stinner35b300c2011-05-04 13:20:35 +0200885 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200886}
887
Victor Stinnera9293352011-04-30 15:21:58 +0200888#endif /* #ifdef PYPTHREAD_SIGMASK */
889
Martin v. Löwis823725e2008-03-24 13:39:54 +0000890
Victor Stinnerb3e72192011-05-08 01:46:11 +0200891#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300892
893/*[clinic input]
894signal.sigpending
895
896Examine pending signals.
897
898Returns a set of signal numbers that are pending for delivery to
899the calling thread.
900[clinic start generated code]*/
901
Victor Stinnerb3e72192011-05-08 01:46:11 +0200902static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300903signal_sigpending_impl(PyObject *module)
904/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200905{
906 int err;
907 sigset_t mask;
908 err = sigpending(&mask);
909 if (err)
910 return PyErr_SetFromErrno(PyExc_OSError);
911 return sigset_to_set(mask);
912}
913
Victor Stinnerb3e72192011-05-08 01:46:11 +0200914#endif /* #ifdef HAVE_SIGPENDING */
915
916
917#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300918
919/*[clinic input]
920signal.sigwait
921
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300922 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300923 /
924
925Wait for a signal.
926
927Suspend execution of the calling thread until the delivery of one of the
928signals specified in the signal set sigset. The function accepts the signal
929and returns the signal number.
930[clinic start generated code]*/
931
Victor Stinnerb3e72192011-05-08 01:46:11 +0200932static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300933signal_sigwait_impl(PyObject *module, sigset_t sigset)
934/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200935{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200936 int err, signum;
937
Victor Stinner10c30d62011-06-10 01:39:53 +0200938 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300939 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200940 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200941 if (err) {
942 errno = err;
943 return PyErr_SetFromErrno(PyExc_OSError);
944 }
945
946 return PyLong_FromLong(signum);
947}
948
Tal Einatc7027b72015-05-16 14:14:49 +0300949#endif /* #ifdef HAVE_SIGWAIT */
950
Victor Stinnerb3e72192011-05-08 01:46:11 +0200951
Antoine Pitrou9d3627e2018-05-04 13:00:50 +0200952#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
953
954/*[clinic input]
955signal.valid_signals
956
957Return a set of valid signal numbers on this platform.
958
959The signal numbers returned by this function can be safely passed to
960functions like `pthread_sigmask`.
961[clinic start generated code]*/
962
963static PyObject *
964signal_valid_signals_impl(PyObject *module)
965/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
966{
967#ifdef MS_WINDOWS
968#ifdef SIGBREAK
969 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
970 SIGILL, SIGINT, SIGSEGV, SIGTERM);
971#else
972 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
973 SIGINT, SIGSEGV, SIGTERM);
974#endif
975 if (tup == NULL) {
976 return NULL;
977 }
978 PyObject *set = PySet_New(tup);
979 Py_DECREF(tup);
980 return set;
981#else
982 sigset_t mask;
983 if (sigemptyset(&mask) || sigfillset(&mask)) {
984 return PyErr_SetFromErrno(PyExc_OSError);
985 }
986 return sigset_to_set(mask);
987#endif
988}
989
990#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
991
992
Ross Lagerwallbc808222011-06-25 12:13:40 +0200993#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
994static int initialized;
995static PyStructSequence_Field struct_siginfo_fields[] = {
996 {"si_signo", "signal number"},
997 {"si_code", "signal code"},
998 {"si_errno", "errno associated with this signal"},
999 {"si_pid", "sending process ID"},
1000 {"si_uid", "real user ID of sending process"},
1001 {"si_status", "exit value or signal"},
1002 {"si_band", "band event for SIGPOLL"},
1003 {0}
1004};
1005
1006PyDoc_STRVAR(struct_siginfo__doc__,
1007"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1008This object may be accessed either as a tuple of\n\
1009(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1010or via the attributes si_signo, si_code, and so on.");
1011
1012static PyStructSequence_Desc struct_siginfo_desc = {
1013 "signal.struct_siginfo", /* name */
1014 struct_siginfo__doc__, /* doc */
1015 struct_siginfo_fields, /* fields */
1016 7 /* n_in_sequence */
1017};
1018
1019static PyTypeObject SiginfoType;
1020
1021static PyObject *
1022fill_siginfo(siginfo_t *si)
1023{
1024 PyObject *result = PyStructSequence_New(&SiginfoType);
1025 if (!result)
1026 return NULL;
1027
1028 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1029 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1030 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1031 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001032 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001033 PyStructSequence_SET_ITEM(result, 5,
1034 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001035#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001036 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001037#else
1038 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1039#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001040 if (PyErr_Occurred()) {
1041 Py_DECREF(result);
1042 return NULL;
1043 }
1044
1045 return result;
1046}
1047#endif
1048
1049#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001050
1051/*[clinic input]
1052signal.sigwaitinfo
1053
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001054 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001055 /
1056
1057Wait synchronously until one of the signals in *sigset* is delivered.
1058
1059Returns a struct_siginfo containing information about the signal.
1060[clinic start generated code]*/
1061
Ross Lagerwallbc808222011-06-25 12:13:40 +02001062static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001063signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1064/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001065{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001066 siginfo_t si;
1067 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001068 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001069
Victor Stinnera453cd82015-03-20 12:54:28 +01001070 do {
1071 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001072 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001073 Py_END_ALLOW_THREADS
1074 } while (err == -1
1075 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001076 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001077 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001078
1079 return fill_siginfo(&si);
1080}
1081
Ross Lagerwallbc808222011-06-25 12:13:40 +02001082#endif /* #ifdef HAVE_SIGWAITINFO */
1083
1084#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001085
1086/*[clinic input]
1087signal.sigtimedwait
1088
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001089 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001090 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001091 /
1092
1093Like sigwaitinfo(), but with a timeout.
1094
1095The timeout is specified in seconds, with floating point numbers allowed.
1096[clinic start generated code]*/
1097
Ross Lagerwallbc808222011-06-25 12:13:40 +02001098static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001099signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001100 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001101/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001102{
Victor Stinnera453cd82015-03-20 12:54:28 +01001103 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001104 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001105 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001106 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001107
Victor Stinner869e1772015-03-30 03:49:14 +02001108 if (_PyTime_FromSecondsObject(&timeout,
1109 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001110 return NULL;
1111
Victor Stinnera453cd82015-03-20 12:54:28 +01001112 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001113 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1114 return NULL;
1115 }
1116
Victor Stinner34dc0f42015-03-27 18:19:03 +01001117 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001118
1119 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001120 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1121 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001122
1123 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001124 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001125 Py_END_ALLOW_THREADS
1126
1127 if (res != -1)
1128 break;
1129
1130 if (errno != EINTR) {
1131 if (errno == EAGAIN)
1132 Py_RETURN_NONE;
1133 else
1134 return PyErr_SetFromErrno(PyExc_OSError);
1135 }
1136
1137 /* sigtimedwait() was interrupted by a signal (EINTR) */
1138 if (PyErr_CheckSignals())
1139 return NULL;
1140
Victor Stinner34dc0f42015-03-27 18:19:03 +01001141 monotonic = _PyTime_GetMonotonicClock();
1142 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001143 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001144 break;
1145 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001146
1147 return fill_siginfo(&si);
1148}
1149
Ross Lagerwallbc808222011-06-25 12:13:40 +02001150#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1151
Victor Stinnerb3e72192011-05-08 01:46:11 +02001152
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001153#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001154
1155/*[clinic input]
1156signal.pthread_kill
1157
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001158 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001159 signalnum: int
1160 /
1161
1162Send a signal to a thread.
1163[clinic start generated code]*/
1164
Victor Stinnerb3e72192011-05-08 01:46:11 +02001165static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001166signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1167 int signalnum)
1168/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001169{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001170 int err;
1171
Tal Einatc7027b72015-05-16 14:14:49 +03001172 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001173 if (err != 0) {
1174 errno = err;
1175 PyErr_SetFromErrno(PyExc_OSError);
1176 return NULL;
1177 }
1178
1179 /* the signal may have been send to the current thread */
1180 if (PyErr_CheckSignals())
1181 return NULL;
1182
1183 Py_RETURN_NONE;
1184}
1185
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001186#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001187
1188
1189
Tal Einatc7027b72015-05-16 14:14:49 +03001190/* List of functions defined in the module -- some of the methoddefs are
1191 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001192static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001193 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1194 SIGNAL_ALARM_METHODDEF
1195 SIGNAL_SETITIMER_METHODDEF
1196 SIGNAL_GETITIMER_METHODDEF
1197 SIGNAL_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001198 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001199 SIGNAL_GETSIGNAL_METHODDEF
Nathaniel J. Smith902ab802017-12-17 20:10:18 -08001200 {"set_wakeup_fd", (PyCFunction)signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001201 SIGNAL_SIGINTERRUPT_METHODDEF
1202 SIGNAL_PAUSE_METHODDEF
1203 SIGNAL_PTHREAD_KILL_METHODDEF
1204 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1205 SIGNAL_SIGPENDING_METHODDEF
1206 SIGNAL_SIGWAIT_METHODDEF
1207 SIGNAL_SIGWAITINFO_METHODDEF
1208 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001209#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1210 SIGNAL_VALID_SIGNALS_METHODDEF
1211#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001212 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001213};
1214
Barry Warsaw92971171997-01-03 00:14:25 +00001215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001216PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001217"This module provides mechanisms to use signal handlers in Python.\n\
1218\n\
1219Functions:\n\
1220\n\
1221alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001222setitimer() -- cause a signal (described below) after a specified\n\
1223 float time and the timer may restart then [Unix only]\n\
1224getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001225signal() -- set the action for a given signal\n\
1226getsignal() -- get the signal action for a given signal\n\
1227pause() -- wait until a signal arrives [Unix only]\n\
1228default_int_handler() -- default SIGINT handler\n\
1229\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001230signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001231SIG_DFL -- used to refer to the system default handler\n\
1232SIG_IGN -- used to ignore the signal\n\
1233NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001234SIGINT, SIGTERM, etc. -- signal numbers\n\
1235\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001236itimer constants:\n\
1237ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1238 expiration\n\
1239ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1240 and delivers SIGVTALRM upon expiration\n\
1241ITIMER_PROF -- decrements both when the process is executing and\n\
1242 when the system is executing on behalf of the process.\n\
1243 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1244 used to profile the time spent by the application\n\
1245 in user and kernel space. SIGPROF is delivered upon\n\
1246 expiration.\n\
1247\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001248*** IMPORTANT NOTICE ***\n\
1249A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001250the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001251
Martin v. Löwis1a214512008-06-11 05:26:20 +00001252static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001254 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 module_doc,
1256 -1,
1257 signal_methods,
1258 NULL,
1259 NULL,
1260 NULL,
1261 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001262};
1263
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001264PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001265PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 PyObject *m, *d, *x;
1268 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 main_thread = PyThread_get_thread_ident();
1271 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* Create the module and add the functions */
1274 m = PyModule_Create(&signalmodule);
1275 if (m == NULL)
1276 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001277
Ross Lagerwallbc808222011-06-25 12:13:40 +02001278#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001279 if (!initialized) {
1280 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1281 return NULL;
1282 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001283 Py_INCREF((PyObject*) &SiginfoType);
1284 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1285 initialized = 1;
1286#endif
1287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* Add some symbolic constants to the module */
1289 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1292 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1293 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1296 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1297 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 x = PyLong_FromLong((long)NSIG);
1300 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1301 goto finally;
1302 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001303
Victor Stinnera9293352011-04-30 15:21:58 +02001304#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001305 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1306 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001307#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001308#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001309 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1310 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001311#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001312#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001313 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1314 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001315#endif
1316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1318 if (!x)
1319 goto finally;
1320 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001321
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001322 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 for (i = 1; i < NSIG; i++) {
1324 void (*t)(int);
1325 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001326 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 if (t == SIG_DFL)
1328 Handlers[i].func = DefaultHandler;
1329 else if (t == SIG_IGN)
1330 Handlers[i].func = IgnoreHandler;
1331 else
1332 Handlers[i].func = Py_None; /* None of our business */
1333 Py_INCREF(Handlers[i].func);
1334 }
1335 if (Handlers[SIGINT].func == DefaultHandler) {
1336 /* Install default int handler */
1337 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001338 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1340 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341
1342#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001343 if (PyModule_AddIntMacro(m, SIGHUP))
1344 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001345#endif
1346#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001347 if (PyModule_AddIntMacro(m, SIGINT))
1348 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001350#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001351 if (PyModule_AddIntMacro(m, SIGBREAK))
1352 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001353#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001354#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001355 if (PyModule_AddIntMacro(m, SIGQUIT))
1356 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001357#endif
1358#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001359 if (PyModule_AddIntMacro(m, SIGILL))
1360 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001361#endif
1362#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001363 if (PyModule_AddIntMacro(m, SIGTRAP))
1364 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001365#endif
1366#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001367 if (PyModule_AddIntMacro(m, SIGIOT))
1368 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369#endif
1370#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001371 if (PyModule_AddIntMacro(m, SIGABRT))
1372 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373#endif
1374#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001375 if (PyModule_AddIntMacro(m, SIGEMT))
1376 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001377#endif
1378#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001379 if (PyModule_AddIntMacro(m, SIGFPE))
1380 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381#endif
1382#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001383 if (PyModule_AddIntMacro(m, SIGKILL))
1384 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001385#endif
1386#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001387 if (PyModule_AddIntMacro(m, SIGBUS))
1388 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001389#endif
1390#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001391 if (PyModule_AddIntMacro(m, SIGSEGV))
1392 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#endif
1394#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001395 if (PyModule_AddIntMacro(m, SIGSYS))
1396 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001397#endif
1398#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001399 if (PyModule_AddIntMacro(m, SIGPIPE))
1400 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401#endif
1402#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001403 if (PyModule_AddIntMacro(m, SIGALRM))
1404 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405#endif
1406#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001407 if (PyModule_AddIntMacro(m, SIGTERM))
1408 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409#endif
1410#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGUSR1))
1412 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGUSR2))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
1418#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGCLD))
1420 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001421#endif
1422#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGCHLD))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
1426#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGPWR))
1428 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001429#endif
1430#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGIO))
1432 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001433#endif
1434#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGURG))
1436 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001437#endif
1438#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGWINCH))
1440 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001441#endif
1442#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001443 if (PyModule_AddIntMacro(m, SIGPOLL))
1444 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001445#endif
1446#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001447 if (PyModule_AddIntMacro(m, SIGSTOP))
1448 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001449#endif
1450#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, SIGTSTP))
1452 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453#endif
1454#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGCONT))
1456 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001457#endif
1458#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGTTIN))
1460 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001461#endif
1462#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGTTOU))
1464 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001465#endif
1466#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGVTALRM))
1468 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGPROF))
1472 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001474#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGXCPU))
1476 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001478#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGXFSZ))
1480 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001481#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001482#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGRTMIN))
1484 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001485#endif
1486#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGRTMAX))
1488 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001489#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001490#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGINFO))
1492 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001493#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001494
1495#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001496 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1497 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001498#endif
1499#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001500 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1501 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001502#endif
1503#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001504 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1505 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001506#endif
1507
1508#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001510 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001511 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001512 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001513#endif
1514
Brian Curtineb24d742010-04-12 17:16:38 +00001515#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001516 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1517 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001518#endif
1519
1520#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001521 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1522 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001523#endif
1524
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001525#ifdef MS_WINDOWS
1526 /* Create manual-reset event, initially unset */
1527 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1528#endif
1529
Martin v. Löwis1a214512008-06-11 05:26:20 +00001530 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 Py_DECREF(m);
1532 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001533 }
Barry Warsaw92971171997-01-03 00:14:25 +00001534
Barry Warsaw92971171997-01-03 00:14:25 +00001535 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001536 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001537}
1538
1539static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001540finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 int i;
1543 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 PyOS_setsig(SIGINT, old_siginthandler);
1546 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 for (i = 1; i < NSIG; i++) {
1549 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001550 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 Handlers[i].func = NULL;
1552 if (i != SIGINT && func != NULL && func != Py_None &&
1553 func != DefaultHandler && func != IgnoreHandler)
1554 PyOS_setsig(i, SIG_DFL);
1555 Py_XDECREF(func);
1556 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001557
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001558 Py_CLEAR(IntHandler);
1559 Py_CLEAR(DefaultHandler);
1560 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001561}
1562
Barry Warsaw92971171997-01-03 00:14:25 +00001563
Barry Warsaw92971171997-01-03 00:14:25 +00001564/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001565int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001566PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 int i;
1569 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001570
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001571 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if (PyThread_get_thread_ident() != main_thread)
1575 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 /*
1578 * The is_tripped variable is meant to speed up the calls to
1579 * PyErr_CheckSignals (both directly or via pending calls) when no
1580 * signal has arrived. This variable is set to 1 when a signal arrives
1581 * and it is set to 0 here, when we know some signals arrived. This way
1582 * we can run the registered handlers with no signals blocked.
1583 *
1584 * NOTE: with this approach we can have a situation where is_tripped is
1585 * 1 but we have no more signals to handle (Handlers[i].tripped
1586 * is 0 for every signal i). This won't do us any harm (except
1587 * we're gonna spent some cycles for nothing). This happens when
1588 * we receive a signal i after we zero is_tripped and before we
1589 * check Handlers[i].tripped.
1590 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001591 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (!(f = (PyObject *)PyEval_GetFrame()))
1594 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001597 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 PyObject *result = NULL;
1599 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001600 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 if (arglist) {
1603 result = PyEval_CallObject(Handlers[i].func,
1604 arglist);
1605 Py_DECREF(arglist);
1606 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001607 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001608 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001610 }
Barry Warsaw92971171997-01-03 00:14:25 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 Py_DECREF(result);
1613 }
1614 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001617}
1618
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001619
Barry Warsaw92971171997-01-03 00:14:25 +00001620/* Replacements for intrcheck.c functionality
1621 * Declared in pyerrors.h
1622 */
1623void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001624PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001625{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001626 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001627}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001628
1629void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001630PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001631{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001632 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 Py_DECREF(m);
1635 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001636}
1637
1638void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001639PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001642}
1643
1644int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001645PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001646{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001647 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (PyThread_get_thread_ident() != main_thread)
1649 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001650 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 return 1;
1652 }
1653 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001654}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001655
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001656static void
1657_clear_pending_signals(void)
1658{
1659 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001660 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001661 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001662 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001663 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001664 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001665 }
1666}
1667
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001668void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001669_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001670{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001671 /* Clear the signal flags after forking so that they aren't handled
1672 * in both processes if they came in just before the fork() but before
1673 * the interpreter had an opportunity to call the handlers. issue9535. */
1674 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 main_thread = PyThread_get_thread_ident();
1676 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001677}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001678
1679int
1680_PyOS_IsMainThread(void)
1681{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001682 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001683}
1684
1685#ifdef MS_WINDOWS
1686void *_PyOS_SigintEvent(void)
1687{
1688 /* Returns a manual-reset event which gets tripped whenever
1689 SIGINT is received.
1690
1691 Python.h does not include windows.h so we do cannot use HANDLE
1692 as the return type of this function. We use void* instead. */
1693 return sigint_event;
1694}
1695#endif