ยปCore Development>Code coverage>Modules/signalmodule.c

Python code coverage for Modules/signalmodule.c

#countcontent
1n/a
2n/a/* Signal module -- many thanks to Lance Ellinghaus */
3n/a
4n/a/* XXX Signals should be recorded per thread, now we have thread state. */
5n/a
6n/a#include "Python.h"
7n/a#ifndef MS_WINDOWS
8n/a#include "posixmodule.h"
9n/a#endif
10n/a#ifdef MS_WINDOWS
11n/a#include "socketmodule.h" /* needed for SOCKET_T */
12n/a#endif
13n/a
14n/a#ifdef MS_WINDOWS
15n/a#include <windows.h>
16n/a#ifdef HAVE_PROCESS_H
17n/a#include <process.h>
18n/a#endif
19n/a#endif
20n/a
21n/a#ifdef HAVE_SIGNAL_H
22n/a#include <signal.h>
23n/a#endif
24n/a#ifdef HAVE_SYS_STAT_H
25n/a#include <sys/stat.h>
26n/a#endif
27n/a#ifdef HAVE_SYS_TIME_H
28n/a#include <sys/time.h>
29n/a#endif
30n/a
31n/a#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
32n/a# define PYPTHREAD_SIGMASK
33n/a#endif
34n/a
35n/a#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
36n/a# include <pthread.h>
37n/a#endif
38n/a
39n/a#ifndef SIG_ERR
40n/a#define SIG_ERR ((PyOS_sighandler_t)(-1))
41n/a#endif
42n/a
43n/a#ifndef NSIG
44n/a# if defined(_NSIG)
45n/a# define NSIG _NSIG /* For BSD/SysV */
46n/a# elif defined(_SIGMAX)
47n/a# define NSIG (_SIGMAX + 1) /* For QNX */
48n/a# elif defined(SIGMAX)
49n/a# define NSIG (SIGMAX + 1) /* For djgpp */
50n/a# else
51n/a# define NSIG 64 /* Use a reasonable default value */
52n/a# endif
53n/a#endif
54n/a
55n/a#include "clinic/signalmodule.c.h"
56n/a
57n/a/*[clinic input]
58n/amodule signal
59n/a[clinic start generated code]*/
60n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
61n/a
62n/a
63n/a/*
64n/a NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
65n/a
66n/a When threads are supported, we want the following semantics:
67n/a
68n/a - only the main thread can set a signal handler
69n/a - any thread can get a signal handler
70n/a - signals are only delivered to the main thread
71n/a
72n/a I.e. we don't support "synchronous signals" like SIGFPE (catching
73n/a this doesn't make much sense in Python anyway) nor do we support
74n/a signals as a means of inter-thread communication, since not all
75n/a thread implementations support that (at least our thread library
76n/a doesn't).
77n/a
78n/a We still have the problem that in some implementations signals
79n/a generated by the keyboard (e.g. SIGINT) are delivered to all
80n/a threads (e.g. SGI), while in others (e.g. Solaris) such signals are
81n/a delivered to one random thread (an intermediate possibility would
82n/a be to deliver it to the main thread -- POSIX?). For now, we have
83n/a a working implementation that works in all three cases -- the
84n/a handler ignores signals if getpid() isn't the same as in the main
85n/a thread. XXX This is a hack.
86n/a*/
87n/a
88n/a#ifdef WITH_THREAD
89n/a#include <sys/types.h> /* For pid_t */
90n/a#include "pythread.h"
91n/astatic long main_thread;
92n/astatic pid_t main_pid;
93n/a#endif
94n/a
95n/astatic volatile struct {
96n/a sig_atomic_t tripped;
97n/a PyObject *func;
98n/a} Handlers[NSIG];
99n/a
100n/a#ifdef MS_WINDOWS
101n/a#define INVALID_FD ((SOCKET_T)-1)
102n/a
103n/astatic volatile struct {
104n/a SOCKET_T fd;
105n/a int use_send;
106n/a int send_err_set;
107n/a int send_errno;
108n/a int send_win_error;
109n/a} wakeup = {INVALID_FD, 0, 0};
110n/a#else
111n/a#define INVALID_FD (-1)
112n/astatic volatile sig_atomic_t wakeup_fd = -1;
113n/a#endif
114n/a
115n/a/* Speed up sigcheck() when none tripped */
116n/astatic volatile sig_atomic_t is_tripped = 0;
117n/a
118n/astatic PyObject *DefaultHandler;
119n/astatic PyObject *IgnoreHandler;
120n/astatic PyObject *IntHandler;
121n/a
122n/a/* On Solaris 8, gcc will produce a warning that the function
123n/a declaration is not a prototype. This is caused by the definition of
124n/a SIG_DFL as (void (*)())0; the correct declaration would have been
125n/a (void (*)(int))0. */
126n/a
127n/astatic PyOS_sighandler_t old_siginthandler = SIG_DFL;
128n/a
129n/a#ifdef MS_WINDOWS
130n/astatic HANDLE sigint_event = NULL;
131n/a#endif
132n/a
133n/a#ifdef HAVE_GETITIMER
134n/astatic PyObject *ItimerError;
135n/a
136n/a/* auxiliary functions for setitimer/getitimer */
137n/astatic void
138n/atimeval_from_double(double d, struct timeval *tv)
139n/a{
140n/a tv->tv_sec = floor(d);
141n/a tv->tv_usec = fmod(d, 1.0) * 1000000.0;
142n/a}
143n/a
144n/aPy_LOCAL_INLINE(double)
145n/adouble_from_timeval(struct timeval *tv)
146n/a{
147n/a return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
148n/a}
149n/a
150n/astatic PyObject *
151n/aitimer_retval(struct itimerval *iv)
152n/a{
153n/a PyObject *r, *v;
154n/a
155n/a r = PyTuple_New(2);
156n/a if (r == NULL)
157n/a return NULL;
158n/a
159n/a if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
160n/a Py_DECREF(r);
161n/a return NULL;
162n/a }
163n/a
164n/a PyTuple_SET_ITEM(r, 0, v);
165n/a
166n/a if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
167n/a Py_DECREF(r);
168n/a return NULL;
169n/a }
170n/a
171n/a PyTuple_SET_ITEM(r, 1, v);
172n/a
173n/a return r;
174n/a}
175n/a#endif
176n/a
177n/astatic PyObject *
178n/asignal_default_int_handler(PyObject *self, PyObject *args)
179n/a{
180n/a PyErr_SetNone(PyExc_KeyboardInterrupt);
181n/a return NULL;
182n/a}
183n/a
184n/aPyDoc_STRVAR(default_int_handler_doc,
185n/a"default_int_handler(...)\n\
186n/a\n\
187n/aThe default handler for SIGINT installed by Python.\n\
188n/aIt raises KeyboardInterrupt.");
189n/a
190n/a
191n/astatic int
192n/achecksignals_witharg(void * unused)
193n/a{
194n/a return PyErr_CheckSignals();
195n/a}
196n/a
197n/astatic int
198n/areport_wakeup_write_error(void *data)
199n/a{
200n/a int save_errno = errno;
201n/a errno = (int) (intptr_t) data;
202n/a PyErr_SetFromErrno(PyExc_OSError);
203n/a PySys_WriteStderr("Exception ignored when trying to write to the "
204n/a "signal wakeup fd:\n");
205n/a PyErr_WriteUnraisable(NULL);
206n/a errno = save_errno;
207n/a return 0;
208n/a}
209n/a
210n/a#ifdef MS_WINDOWS
211n/astatic int
212n/areport_wakeup_send_error(void* Py_UNUSED(data))
213n/a{
214n/a PyObject *res;
215n/a
216n/a if (wakeup.send_win_error) {
217n/a /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
218n/a recognizes the error codes used by both GetLastError() and
219n/a WSAGetLastError */
220n/a res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
221n/a }
222n/a else {
223n/a errno = wakeup.send_errno;
224n/a res = PyErr_SetFromErrno(PyExc_OSError);
225n/a }
226n/a
227n/a assert(res == NULL);
228n/a wakeup.send_err_set = 0;
229n/a
230n/a PySys_WriteStderr("Exception ignored when trying to send to the "
231n/a "signal wakeup fd:\n");
232n/a PyErr_WriteUnraisable(NULL);
233n/a
234n/a return 0;
235n/a}
236n/a#endif /* MS_WINDOWS */
237n/a
238n/astatic void
239n/atrip_signal(int sig_num)
240n/a{
241n/a unsigned char byte;
242n/a int fd;
243n/a Py_ssize_t rc;
244n/a
245n/a Handlers[sig_num].tripped = 1;
246n/a
247n/a#ifdef MS_WINDOWS
248n/a fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
249n/a#else
250n/a fd = wakeup_fd;
251n/a#endif
252n/a
253n/a if (fd != INVALID_FD) {
254n/a byte = (unsigned char)sig_num;
255n/a#ifdef MS_WINDOWS
256n/a if (wakeup.use_send) {
257n/a do {
258n/a rc = send(fd, &byte, 1, 0);
259n/a } while (rc < 0 && errno == EINTR);
260n/a
261n/a /* we only have a storage for one error in the wakeup structure */
262n/a if (rc < 0 && !wakeup.send_err_set) {
263n/a wakeup.send_err_set = 1;
264n/a wakeup.send_errno = errno;
265n/a wakeup.send_win_error = GetLastError();
266n/a Py_AddPendingCall(report_wakeup_send_error, NULL);
267n/a }
268n/a }
269n/a else
270n/a#endif
271n/a {
272n/a byte = (unsigned char)sig_num;
273n/a
274n/a /* _Py_write_noraise() retries write() if write() is interrupted by
275n/a a signal (fails with EINTR). */
276n/a rc = _Py_write_noraise(fd, &byte, 1);
277n/a
278n/a if (rc < 0) {
279n/a Py_AddPendingCall(report_wakeup_write_error,
280n/a (void *)(intptr_t)errno);
281n/a }
282n/a }
283n/a }
284n/a
285n/a if (!is_tripped) {
286n/a /* Set is_tripped after setting .tripped, as it gets
287n/a cleared in PyErr_CheckSignals() before .tripped. */
288n/a is_tripped = 1;
289n/a Py_AddPendingCall(checksignals_witharg, NULL);
290n/a }
291n/a}
292n/a
293n/astatic void
294n/asignal_handler(int sig_num)
295n/a{
296n/a int save_errno = errno;
297n/a
298n/a#ifdef WITH_THREAD
299n/a /* See NOTES section above */
300n/a if (getpid() == main_pid)
301n/a#endif
302n/a {
303n/a trip_signal(sig_num);
304n/a }
305n/a
306n/a#ifndef HAVE_SIGACTION
307n/a#ifdef SIGCHLD
308n/a /* To avoid infinite recursion, this signal remains
309n/a reset until explicit re-instated.
310n/a Don't clear the 'func' field as it is our pointer
311n/a to the Python handler... */
312n/a if (sig_num != SIGCHLD)
313n/a#endif
314n/a /* If the handler was not set up with sigaction, reinstall it. See
315n/a * Python/pylifecycle.c for the implementation of PyOS_setsig which
316n/a * makes this true. See also issue8354. */
317n/a PyOS_setsig(sig_num, signal_handler);
318n/a#endif
319n/a
320n/a /* Issue #10311: asynchronously executing signal handlers should not
321n/a mutate errno under the feet of unsuspecting C code. */
322n/a errno = save_errno;
323n/a
324n/a#ifdef MS_WINDOWS
325n/a if (sig_num == SIGINT)
326n/a SetEvent(sigint_event);
327n/a#endif
328n/a}
329n/a
330n/a
331n/a#ifdef HAVE_ALARM
332n/a
333n/a/*[clinic input]
334n/asignal.alarm -> long
335n/a
336n/a seconds: int
337n/a /
338n/a
339n/aArrange for SIGALRM to arrive after the given number of seconds.
340n/a[clinic start generated code]*/
341n/a
342n/astatic long
343n/asignal_alarm_impl(PyObject *module, int seconds)
344n/a/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
345n/a{
346n/a /* alarm() returns the number of seconds remaining */
347n/a return (long)alarm(seconds);
348n/a}
349n/a
350n/a#endif
351n/a
352n/a#ifdef HAVE_PAUSE
353n/a
354n/a/*[clinic input]
355n/asignal.pause
356n/a
357n/aWait until a signal arrives.
358n/a[clinic start generated code]*/
359n/a
360n/astatic PyObject *
361n/asignal_pause_impl(PyObject *module)
362n/a/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
363n/a{
364n/a Py_BEGIN_ALLOW_THREADS
365n/a (void)pause();
366n/a Py_END_ALLOW_THREADS
367n/a /* make sure that any exceptions that got raised are propagated
368n/a * back into Python
369n/a */
370n/a if (PyErr_CheckSignals())
371n/a return NULL;
372n/a
373n/a Py_RETURN_NONE;
374n/a}
375n/a
376n/a#endif
377n/a
378n/a
379n/a/*[clinic input]
380n/asignal.signal
381n/a
382n/a signalnum: int
383n/a handler: object
384n/a /
385n/a
386n/aSet the action for the given signal.
387n/a
388n/aThe action can be SIG_DFL, SIG_IGN, or a callable Python object.
389n/aThe previous action is returned. See getsignal() for possible return values.
390n/a
391n/a*** IMPORTANT NOTICE ***
392n/aA signal handler function is called with two arguments:
393n/athe first is the signal number, the second is the interrupted stack frame.
394n/a[clinic start generated code]*/
395n/a
396n/astatic PyObject *
397n/asignal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
398n/a/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
399n/a{
400n/a PyObject *old_handler;
401n/a void (*func)(int);
402n/a#ifdef MS_WINDOWS
403n/a /* Validate that signalnum is one of the allowable signals */
404n/a switch (signalnum) {
405n/a case SIGABRT: break;
406n/a#ifdef SIGBREAK
407n/a /* Issue #10003: SIGBREAK is not documented as permitted, but works
408n/a and corresponds to CTRL_BREAK_EVENT. */
409n/a case SIGBREAK: break;
410n/a#endif
411n/a case SIGFPE: break;
412n/a case SIGILL: break;
413n/a case SIGINT: break;
414n/a case SIGSEGV: break;
415n/a case SIGTERM: break;
416n/a default:
417n/a PyErr_SetString(PyExc_ValueError, "invalid signal value");
418n/a return NULL;
419n/a }
420n/a#endif
421n/a#ifdef WITH_THREAD
422n/a if (PyThread_get_thread_ident() != main_thread) {
423n/a PyErr_SetString(PyExc_ValueError,
424n/a "signal only works in main thread");
425n/a return NULL;
426n/a }
427n/a#endif
428n/a if (signalnum < 1 || signalnum >= NSIG) {
429n/a PyErr_SetString(PyExc_ValueError,
430n/a "signal number out of range");
431n/a return NULL;
432n/a }
433n/a if (handler == IgnoreHandler)
434n/a func = SIG_IGN;
435n/a else if (handler == DefaultHandler)
436n/a func = SIG_DFL;
437n/a else if (!PyCallable_Check(handler)) {
438n/a PyErr_SetString(PyExc_TypeError,
439n/a"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
440n/a return NULL;
441n/a }
442n/a else
443n/a func = signal_handler;
444n/a if (PyOS_setsig(signalnum, func) == SIG_ERR) {
445n/a PyErr_SetFromErrno(PyExc_OSError);
446n/a return NULL;
447n/a }
448n/a old_handler = Handlers[signalnum].func;
449n/a Handlers[signalnum].tripped = 0;
450n/a Py_INCREF(handler);
451n/a Handlers[signalnum].func = handler;
452n/a if (old_handler != NULL)
453n/a return old_handler;
454n/a else
455n/a Py_RETURN_NONE;
456n/a}
457n/a
458n/a
459n/a/*[clinic input]
460n/asignal.getsignal
461n/a
462n/a signalnum: int
463n/a /
464n/a
465n/aReturn the current action for the given signal.
466n/a
467n/aThe return value can be:
468n/a SIG_IGN -- if the signal is being ignored
469n/a SIG_DFL -- if the default action for the signal is in effect
470n/a None -- if an unknown handler is in effect
471n/a anything else -- the callable Python object used as a handler
472n/a[clinic start generated code]*/
473n/a
474n/astatic PyObject *
475n/asignal_getsignal_impl(PyObject *module, int signalnum)
476n/a/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
477n/a{
478n/a PyObject *old_handler;
479n/a if (signalnum < 1 || signalnum >= NSIG) {
480n/a PyErr_SetString(PyExc_ValueError,
481n/a "signal number out of range");
482n/a return NULL;
483n/a }
484n/a old_handler = Handlers[signalnum].func;
485n/a if (old_handler != NULL) {
486n/a Py_INCREF(old_handler);
487n/a return old_handler;
488n/a }
489n/a else {
490n/a Py_RETURN_NONE;
491n/a }
492n/a}
493n/a
494n/a#ifdef HAVE_SIGINTERRUPT
495n/a
496n/a/*[clinic input]
497n/asignal.siginterrupt
498n/a
499n/a signalnum: int
500n/a flag: int
501n/a /
502n/a
503n/aChange system call restart behaviour.
504n/a
505n/aIf flag is False, system calls will be restarted when interrupted by
506n/asignal sig, else system calls will be interrupted.
507n/a[clinic start generated code]*/
508n/a
509n/astatic PyObject *
510n/asignal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
511n/a/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
512n/a{
513n/a if (signalnum < 1 || signalnum >= NSIG) {
514n/a PyErr_SetString(PyExc_ValueError,
515n/a "signal number out of range");
516n/a return NULL;
517n/a }
518n/a if (siginterrupt(signalnum, flag)<0) {
519n/a PyErr_SetFromErrno(PyExc_OSError);
520n/a return NULL;
521n/a }
522n/a Py_RETURN_NONE;
523n/a}
524n/a
525n/a#endif
526n/a
527n/a
528n/astatic PyObject*
529n/asignal_set_wakeup_fd(PyObject *self, PyObject *args)
530n/a{
531n/a struct _Py_stat_struct status;
532n/a#ifdef MS_WINDOWS
533n/a PyObject *fdobj;
534n/a SOCKET_T sockfd, old_sockfd;
535n/a int res;
536n/a int res_size = sizeof res;
537n/a PyObject *mod;
538n/a int is_socket;
539n/a
540n/a if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
541n/a return NULL;
542n/a
543n/a sockfd = PyLong_AsSocket_t(fdobj);
544n/a if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
545n/a return NULL;
546n/a#else
547n/a int fd, old_fd;
548n/a
549n/a if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
550n/a return NULL;
551n/a#endif
552n/a
553n/a#ifdef WITH_THREAD
554n/a if (PyThread_get_thread_ident() != main_thread) {
555n/a PyErr_SetString(PyExc_ValueError,
556n/a "set_wakeup_fd only works in main thread");
557n/a return NULL;
558n/a }
559n/a#endif
560n/a
561n/a#ifdef MS_WINDOWS
562n/a is_socket = 0;
563n/a if (sockfd != INVALID_FD) {
564n/a /* Import the _socket module to call WSAStartup() */
565n/a mod = PyImport_ImportModuleNoBlock("_socket");
566n/a if (mod == NULL)
567n/a return NULL;
568n/a Py_DECREF(mod);
569n/a
570n/a /* test the socket */
571n/a if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
572n/a (char *)&res, &res_size) != 0) {
573n/a int fd, err;
574n/a
575n/a err = WSAGetLastError();
576n/a if (err != WSAENOTSOCK) {
577n/a PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
578n/a return NULL;
579n/a }
580n/a
581n/a fd = (int)sockfd;
582n/a if ((SOCKET_T)fd != sockfd) {
583n/a PyErr_SetString(PyExc_ValueError, "invalid fd");
584n/a return NULL;
585n/a }
586n/a
587n/a if (_Py_fstat(fd, &status) != 0)
588n/a return NULL;
589n/a
590n/a /* on Windows, a file cannot be set to non-blocking mode */
591n/a }
592n/a else {
593n/a is_socket = 1;
594n/a
595n/a /* Windows does not provide a function to test if a socket
596n/a is in non-blocking mode */
597n/a }
598n/a }
599n/a
600n/a old_sockfd = wakeup.fd;
601n/a wakeup.fd = sockfd;
602n/a wakeup.use_send = is_socket;
603n/a
604n/a if (old_sockfd != INVALID_FD)
605n/a return PyLong_FromSocket_t(old_sockfd);
606n/a else
607n/a return PyLong_FromLong(-1);
608n/a#else
609n/a if (fd != -1) {
610n/a int blocking;
611n/a
612n/a if (_Py_fstat(fd, &status) != 0)
613n/a return NULL;
614n/a
615n/a blocking = _Py_get_blocking(fd);
616n/a if (blocking < 0)
617n/a return NULL;
618n/a if (blocking) {
619n/a PyErr_Format(PyExc_ValueError,
620n/a "the fd %i must be in non-blocking mode",
621n/a fd);
622n/a return NULL;
623n/a }
624n/a }
625n/a
626n/a old_fd = wakeup_fd;
627n/a wakeup_fd = fd;
628n/a
629n/a return PyLong_FromLong(old_fd);
630n/a#endif
631n/a}
632n/a
633n/aPyDoc_STRVAR(set_wakeup_fd_doc,
634n/a"set_wakeup_fd(fd) -> fd\n\
635n/a\n\
636n/aSets the fd to be written to (with the signal number) when a signal\n\
637n/acomes in. A library can use this to wakeup select or poll.\n\
638n/aThe previous fd or -1 is returned.\n\
639n/a\n\
640n/aThe fd must be non-blocking.");
641n/a
642n/a/* C API for the same, without all the error checking */
643n/aint
644n/aPySignal_SetWakeupFd(int fd)
645n/a{
646n/a int old_fd;
647n/a if (fd < 0)
648n/a fd = -1;
649n/a
650n/a#ifdef MS_WINDOWS
651n/a old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
652n/a wakeup.fd = fd;
653n/a#else
654n/a old_fd = wakeup_fd;
655n/a wakeup_fd = fd;
656n/a#endif
657n/a return old_fd;
658n/a}
659n/a
660n/a
661n/a#ifdef HAVE_SETITIMER
662n/a
663n/a/*[clinic input]
664n/asignal.setitimer
665n/a
666n/a which: int
667n/a seconds: double
668n/a interval: double = 0.0
669n/a /
670n/a
671n/aSets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
672n/a
673n/aThe timer will fire after value seconds and after that every interval seconds.
674n/aThe itimer can be cleared by setting seconds to zero.
675n/a
676n/aReturns old values as a tuple: (delay, interval).
677n/a[clinic start generated code]*/
678n/a
679n/astatic PyObject *
680n/asignal_setitimer_impl(PyObject *module, int which, double seconds,
681n/a double interval)
682n/a/*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/
683n/a{
684n/a struct itimerval new, old;
685n/a
686n/a timeval_from_double(seconds, &new.it_value);
687n/a timeval_from_double(interval, &new.it_interval);
688n/a /* Let OS check "which" value */
689n/a if (setitimer(which, &new, &old) != 0) {
690n/a PyErr_SetFromErrno(ItimerError);
691n/a return NULL;
692n/a }
693n/a
694n/a return itimer_retval(&old);
695n/a}
696n/a
697n/a#endif
698n/a
699n/a
700n/a#ifdef HAVE_GETITIMER
701n/a
702n/a/*[clinic input]
703n/asignal.getitimer
704n/a
705n/a which: int
706n/a /
707n/a
708n/aReturns current value of given itimer.
709n/a[clinic start generated code]*/
710n/a
711n/astatic PyObject *
712n/asignal_getitimer_impl(PyObject *module, int which)
713n/a/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
714n/a{
715n/a struct itimerval old;
716n/a
717n/a if (getitimer(which, &old) != 0) {
718n/a PyErr_SetFromErrno(ItimerError);
719n/a return NULL;
720n/a }
721n/a
722n/a return itimer_retval(&old);
723n/a}
724n/a
725n/a#endif
726n/a
727n/a#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
728n/a defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
729n/a/* Convert an iterable to a sigset.
730n/a Return 0 on success, return -1 and raise an exception on error. */
731n/a
732n/astatic int
733n/aiterable_to_sigset(PyObject *iterable, sigset_t *mask)
734n/a{
735n/a int result = -1;
736n/a PyObject *iterator, *item;
737n/a long signum;
738n/a int err;
739n/a
740n/a sigemptyset(mask);
741n/a
742n/a iterator = PyObject_GetIter(iterable);
743n/a if (iterator == NULL)
744n/a goto error;
745n/a
746n/a while (1)
747n/a {
748n/a item = PyIter_Next(iterator);
749n/a if (item == NULL) {
750n/a if (PyErr_Occurred())
751n/a goto error;
752n/a else
753n/a break;
754n/a }
755n/a
756n/a signum = PyLong_AsLong(item);
757n/a Py_DECREF(item);
758n/a if (signum == -1 && PyErr_Occurred())
759n/a goto error;
760n/a if (0 < signum && signum < NSIG)
761n/a err = sigaddset(mask, (int)signum);
762n/a else
763n/a err = 1;
764n/a if (err) {
765n/a PyErr_Format(PyExc_ValueError,
766n/a "signal number %ld out of range", signum);
767n/a goto error;
768n/a }
769n/a }
770n/a result = 0;
771n/a
772n/aerror:
773n/a Py_XDECREF(iterator);
774n/a return result;
775n/a}
776n/a#endif
777n/a
778n/a#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
779n/astatic PyObject*
780n/asigset_to_set(sigset_t mask)
781n/a{
782n/a PyObject *signum, *result;
783n/a int sig;
784n/a
785n/a result = PySet_New(0);
786n/a if (result == NULL)
787n/a return NULL;
788n/a
789n/a for (sig = 1; sig < NSIG; sig++) {
790n/a if (sigismember(&mask, sig) != 1)
791n/a continue;
792n/a
793n/a /* Handle the case where it is a member by adding the signal to
794n/a the result list. Ignore the other cases because they mean the
795n/a signal isn't a member of the mask or the signal was invalid,
796n/a and an invalid signal must have been our fault in constructing
797n/a the loop boundaries. */
798n/a signum = PyLong_FromLong(sig);
799n/a if (signum == NULL) {
800n/a Py_DECREF(result);
801n/a return NULL;
802n/a }
803n/a if (PySet_Add(result, signum) == -1) {
804n/a Py_DECREF(signum);
805n/a Py_DECREF(result);
806n/a return NULL;
807n/a }
808n/a Py_DECREF(signum);
809n/a }
810n/a return result;
811n/a}
812n/a#endif
813n/a
814n/a#ifdef PYPTHREAD_SIGMASK
815n/a
816n/a/*[clinic input]
817n/asignal.pthread_sigmask
818n/a
819n/a how: int
820n/a mask: object
821n/a /
822n/a
823n/aFetch and/or change the signal mask of the calling thread.
824n/a[clinic start generated code]*/
825n/a
826n/astatic PyObject *
827n/asignal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
828n/a/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
829n/a{
830n/a sigset_t newmask, previous;
831n/a int err;
832n/a
833n/a if (iterable_to_sigset(mask, &newmask))
834n/a return NULL;
835n/a
836n/a err = pthread_sigmask(how, &newmask, &previous);
837n/a if (err != 0) {
838n/a errno = err;
839n/a PyErr_SetFromErrno(PyExc_OSError);
840n/a return NULL;
841n/a }
842n/a
843n/a /* if signals was unblocked, signal handlers have been called */
844n/a if (PyErr_CheckSignals())
845n/a return NULL;
846n/a
847n/a return sigset_to_set(previous);
848n/a}
849n/a
850n/a#endif /* #ifdef PYPTHREAD_SIGMASK */
851n/a
852n/a
853n/a#ifdef HAVE_SIGPENDING
854n/a
855n/a/*[clinic input]
856n/asignal.sigpending
857n/a
858n/aExamine pending signals.
859n/a
860n/aReturns a set of signal numbers that are pending for delivery to
861n/athe calling thread.
862n/a[clinic start generated code]*/
863n/a
864n/astatic PyObject *
865n/asignal_sigpending_impl(PyObject *module)
866n/a/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
867n/a{
868n/a int err;
869n/a sigset_t mask;
870n/a err = sigpending(&mask);
871n/a if (err)
872n/a return PyErr_SetFromErrno(PyExc_OSError);
873n/a return sigset_to_set(mask);
874n/a}
875n/a
876n/a#endif /* #ifdef HAVE_SIGPENDING */
877n/a
878n/a
879n/a#ifdef HAVE_SIGWAIT
880n/a
881n/a/*[clinic input]
882n/asignal.sigwait
883n/a
884n/a sigset: object
885n/a /
886n/a
887n/aWait for a signal.
888n/a
889n/aSuspend execution of the calling thread until the delivery of one of the
890n/asignals specified in the signal set sigset. The function accepts the signal
891n/aand returns the signal number.
892n/a[clinic start generated code]*/
893n/a
894n/astatic PyObject *
895n/asignal_sigwait(PyObject *module, PyObject *sigset)
896n/a/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
897n/a{
898n/a sigset_t set;
899n/a int err, signum;
900n/a
901n/a if (iterable_to_sigset(sigset, &set))
902n/a return NULL;
903n/a
904n/a Py_BEGIN_ALLOW_THREADS
905n/a err = sigwait(&set, &signum);
906n/a Py_END_ALLOW_THREADS
907n/a if (err) {
908n/a errno = err;
909n/a return PyErr_SetFromErrno(PyExc_OSError);
910n/a }
911n/a
912n/a return PyLong_FromLong(signum);
913n/a}
914n/a
915n/a#endif /* #ifdef HAVE_SIGWAIT */
916n/a
917n/a
918n/a#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
919n/astatic int initialized;
920n/astatic PyStructSequence_Field struct_siginfo_fields[] = {
921n/a {"si_signo", "signal number"},
922n/a {"si_code", "signal code"},
923n/a {"si_errno", "errno associated with this signal"},
924n/a {"si_pid", "sending process ID"},
925n/a {"si_uid", "real user ID of sending process"},
926n/a {"si_status", "exit value or signal"},
927n/a {"si_band", "band event for SIGPOLL"},
928n/a {0}
929n/a};
930n/a
931n/aPyDoc_STRVAR(struct_siginfo__doc__,
932n/a"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
933n/aThis object may be accessed either as a tuple of\n\
934n/a(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
935n/aor via the attributes si_signo, si_code, and so on.");
936n/a
937n/astatic PyStructSequence_Desc struct_siginfo_desc = {
938n/a "signal.struct_siginfo", /* name */
939n/a struct_siginfo__doc__, /* doc */
940n/a struct_siginfo_fields, /* fields */
941n/a 7 /* n_in_sequence */
942n/a};
943n/a
944n/astatic PyTypeObject SiginfoType;
945n/a
946n/astatic PyObject *
947n/afill_siginfo(siginfo_t *si)
948n/a{
949n/a PyObject *result = PyStructSequence_New(&SiginfoType);
950n/a if (!result)
951n/a return NULL;
952n/a
953n/a PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
954n/a PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
955n/a PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
956n/a PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
957n/a PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
958n/a PyStructSequence_SET_ITEM(result, 5,
959n/a PyLong_FromLong((long)(si->si_status)));
960n/a#ifdef HAVE_SIGINFO_T_SI_BAND
961n/a PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
962n/a#else
963n/a PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
964n/a#endif
965n/a if (PyErr_Occurred()) {
966n/a Py_DECREF(result);
967n/a return NULL;
968n/a }
969n/a
970n/a return result;
971n/a}
972n/a#endif
973n/a
974n/a#ifdef HAVE_SIGWAITINFO
975n/a
976n/a/*[clinic input]
977n/asignal.sigwaitinfo
978n/a
979n/a sigset: object
980n/a /
981n/a
982n/aWait synchronously until one of the signals in *sigset* is delivered.
983n/a
984n/aReturns a struct_siginfo containing information about the signal.
985n/a[clinic start generated code]*/
986n/a
987n/astatic PyObject *
988n/asignal_sigwaitinfo(PyObject *module, PyObject *sigset)
989n/a/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
990n/a{
991n/a sigset_t set;
992n/a siginfo_t si;
993n/a int err;
994n/a int async_err = 0;
995n/a
996n/a if (iterable_to_sigset(sigset, &set))
997n/a return NULL;
998n/a
999n/a do {
1000n/a Py_BEGIN_ALLOW_THREADS
1001n/a err = sigwaitinfo(&set, &si);
1002n/a Py_END_ALLOW_THREADS
1003n/a } while (err == -1
1004n/a && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1005n/a if (err == -1)
1006n/a return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
1007n/a
1008n/a return fill_siginfo(&si);
1009n/a}
1010n/a
1011n/a#endif /* #ifdef HAVE_SIGWAITINFO */
1012n/a
1013n/a#ifdef HAVE_SIGTIMEDWAIT
1014n/a
1015n/a/*[clinic input]
1016n/asignal.sigtimedwait
1017n/a
1018n/a sigset: object
1019n/a timeout as timeout_obj: object
1020n/a /
1021n/a
1022n/aLike sigwaitinfo(), but with a timeout.
1023n/a
1024n/aThe timeout is specified in seconds, with floating point numbers allowed.
1025n/a[clinic start generated code]*/
1026n/a
1027n/astatic PyObject *
1028n/asignal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
1029n/a PyObject *timeout_obj)
1030n/a/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
1031n/a{
1032n/a struct timespec ts;
1033n/a sigset_t set;
1034n/a siginfo_t si;
1035n/a int res;
1036n/a _PyTime_t timeout, deadline, monotonic;
1037n/a
1038n/a if (_PyTime_FromSecondsObject(&timeout,
1039n/a timeout_obj, _PyTime_ROUND_CEILING) < 0)
1040n/a return NULL;
1041n/a
1042n/a if (timeout < 0) {
1043n/a PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1044n/a return NULL;
1045n/a }
1046n/a
1047n/a if (iterable_to_sigset(sigset, &set))
1048n/a return NULL;
1049n/a
1050n/a deadline = _PyTime_GetMonotonicClock() + timeout;
1051n/a
1052n/a do {
1053n/a if (_PyTime_AsTimespec(timeout, &ts) < 0)
1054n/a return NULL;
1055n/a
1056n/a Py_BEGIN_ALLOW_THREADS
1057n/a res = sigtimedwait(&set, &si, &ts);
1058n/a Py_END_ALLOW_THREADS
1059n/a
1060n/a if (res != -1)
1061n/a break;
1062n/a
1063n/a if (errno != EINTR) {
1064n/a if (errno == EAGAIN)
1065n/a Py_RETURN_NONE;
1066n/a else
1067n/a return PyErr_SetFromErrno(PyExc_OSError);
1068n/a }
1069n/a
1070n/a /* sigtimedwait() was interrupted by a signal (EINTR) */
1071n/a if (PyErr_CheckSignals())
1072n/a return NULL;
1073n/a
1074n/a monotonic = _PyTime_GetMonotonicClock();
1075n/a timeout = deadline - monotonic;
1076n/a if (timeout < 0)
1077n/a break;
1078n/a } while (1);
1079n/a
1080n/a return fill_siginfo(&si);
1081n/a}
1082n/a
1083n/a#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1084n/a
1085n/a
1086n/a#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
1087n/a
1088n/a/*[clinic input]
1089n/asignal.pthread_kill
1090n/a
1091n/a thread_id: long
1092n/a signalnum: int
1093n/a /
1094n/a
1095n/aSend a signal to a thread.
1096n/a[clinic start generated code]*/
1097n/a
1098n/astatic PyObject *
1099n/asignal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum)
1100n/a/*[clinic end generated code: output=2a09ce41f1c4228a input=77ed6a3b6f2a8122]*/
1101n/a{
1102n/a int err;
1103n/a
1104n/a err = pthread_kill((pthread_t)thread_id, signalnum);
1105n/a if (err != 0) {
1106n/a errno = err;
1107n/a PyErr_SetFromErrno(PyExc_OSError);
1108n/a return NULL;
1109n/a }
1110n/a
1111n/a /* the signal may have been send to the current thread */
1112n/a if (PyErr_CheckSignals())
1113n/a return NULL;
1114n/a
1115n/a Py_RETURN_NONE;
1116n/a}
1117n/a
1118n/a#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1119n/a
1120n/a
1121n/a
1122n/a/* List of functions defined in the module -- some of the methoddefs are
1123n/a defined to nothing if the corresponding C function is not available. */
1124n/astatic PyMethodDef signal_methods[] = {
1125n/a {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1126n/a SIGNAL_ALARM_METHODDEF
1127n/a SIGNAL_SETITIMER_METHODDEF
1128n/a SIGNAL_GETITIMER_METHODDEF
1129n/a SIGNAL_SIGNAL_METHODDEF
1130n/a SIGNAL_GETSIGNAL_METHODDEF
1131n/a {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
1132n/a SIGNAL_SIGINTERRUPT_METHODDEF
1133n/a SIGNAL_PAUSE_METHODDEF
1134n/a SIGNAL_PTHREAD_KILL_METHODDEF
1135n/a SIGNAL_PTHREAD_SIGMASK_METHODDEF
1136n/a SIGNAL_SIGPENDING_METHODDEF
1137n/a SIGNAL_SIGWAIT_METHODDEF
1138n/a SIGNAL_SIGWAITINFO_METHODDEF
1139n/a SIGNAL_SIGTIMEDWAIT_METHODDEF
1140n/a {NULL, NULL} /* sentinel */
1141n/a};
1142n/a
1143n/a
1144n/aPyDoc_STRVAR(module_doc,
1145n/a"This module provides mechanisms to use signal handlers in Python.\n\
1146n/a\n\
1147n/aFunctions:\n\
1148n/a\n\
1149n/aalarm() -- cause SIGALRM after a specified time [Unix only]\n\
1150n/asetitimer() -- cause a signal (described below) after a specified\n\
1151n/a float time and the timer may restart then [Unix only]\n\
1152n/agetitimer() -- get current value of timer [Unix only]\n\
1153n/asignal() -- set the action for a given signal\n\
1154n/agetsignal() -- get the signal action for a given signal\n\
1155n/apause() -- wait until a signal arrives [Unix only]\n\
1156n/adefault_int_handler() -- default SIGINT handler\n\
1157n/a\n\
1158n/asignal constants:\n\
1159n/aSIG_DFL -- used to refer to the system default handler\n\
1160n/aSIG_IGN -- used to ignore the signal\n\
1161n/aNSIG -- number of defined signals\n\
1162n/aSIGINT, SIGTERM, etc. -- signal numbers\n\
1163n/a\n\
1164n/aitimer constants:\n\
1165n/aITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1166n/a expiration\n\
1167n/aITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1168n/a and delivers SIGVTALRM upon expiration\n\
1169n/aITIMER_PROF -- decrements both when the process is executing and\n\
1170n/a when the system is executing on behalf of the process.\n\
1171n/a Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1172n/a used to profile the time spent by the application\n\
1173n/a in user and kernel space. SIGPROF is delivered upon\n\
1174n/a expiration.\n\
1175n/a\n\n\
1176n/a*** IMPORTANT NOTICE ***\n\
1177n/aA signal handler function is called with two arguments:\n\
1178n/athe first is the signal number, the second is the interrupted stack frame.");
1179n/a
1180n/astatic struct PyModuleDef signalmodule = {
1181n/a PyModuleDef_HEAD_INIT,
1182n/a "_signal",
1183n/a module_doc,
1184n/a -1,
1185n/a signal_methods,
1186n/a NULL,
1187n/a NULL,
1188n/a NULL,
1189n/a NULL
1190n/a};
1191n/a
1192n/aPyMODINIT_FUNC
1193n/aPyInit__signal(void)
1194n/a{
1195n/a PyObject *m, *d, *x;
1196n/a int i;
1197n/a
1198n/a#ifdef WITH_THREAD
1199n/a main_thread = PyThread_get_thread_ident();
1200n/a main_pid = getpid();
1201n/a#endif
1202n/a
1203n/a /* Create the module and add the functions */
1204n/a m = PyModule_Create(&signalmodule);
1205n/a if (m == NULL)
1206n/a return NULL;
1207n/a
1208n/a#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1209n/a if (!initialized) {
1210n/a if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1211n/a return NULL;
1212n/a }
1213n/a Py_INCREF((PyObject*) &SiginfoType);
1214n/a PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1215n/a initialized = 1;
1216n/a#endif
1217n/a
1218n/a /* Add some symbolic constants to the module */
1219n/a d = PyModule_GetDict(m);
1220n/a
1221n/a x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1222n/a if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1223n/a goto finally;
1224n/a
1225n/a x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1226n/a if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1227n/a goto finally;
1228n/a
1229n/a x = PyLong_FromLong((long)NSIG);
1230n/a if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1231n/a goto finally;
1232n/a Py_DECREF(x);
1233n/a
1234n/a#ifdef SIG_BLOCK
1235n/a if (PyModule_AddIntMacro(m, SIG_BLOCK))
1236n/a goto finally;
1237n/a#endif
1238n/a#ifdef SIG_UNBLOCK
1239n/a if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1240n/a goto finally;
1241n/a#endif
1242n/a#ifdef SIG_SETMASK
1243n/a if (PyModule_AddIntMacro(m, SIG_SETMASK))
1244n/a goto finally;
1245n/a#endif
1246n/a
1247n/a x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1248n/a if (!x)
1249n/a goto finally;
1250n/a Py_INCREF(IntHandler);
1251n/a
1252n/a Handlers[0].tripped = 0;
1253n/a for (i = 1; i < NSIG; i++) {
1254n/a void (*t)(int);
1255n/a t = PyOS_getsig(i);
1256n/a Handlers[i].tripped = 0;
1257n/a if (t == SIG_DFL)
1258n/a Handlers[i].func = DefaultHandler;
1259n/a else if (t == SIG_IGN)
1260n/a Handlers[i].func = IgnoreHandler;
1261n/a else
1262n/a Handlers[i].func = Py_None; /* None of our business */
1263n/a Py_INCREF(Handlers[i].func);
1264n/a }
1265n/a if (Handlers[SIGINT].func == DefaultHandler) {
1266n/a /* Install default int handler */
1267n/a Py_INCREF(IntHandler);
1268n/a Py_SETREF(Handlers[SIGINT].func, IntHandler);
1269n/a old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1270n/a }
1271n/a
1272n/a#ifdef SIGHUP
1273n/a if (PyModule_AddIntMacro(m, SIGHUP))
1274n/a goto finally;
1275n/a#endif
1276n/a#ifdef SIGINT
1277n/a if (PyModule_AddIntMacro(m, SIGINT))
1278n/a goto finally;
1279n/a#endif
1280n/a#ifdef SIGBREAK
1281n/a if (PyModule_AddIntMacro(m, SIGBREAK))
1282n/a goto finally;
1283n/a#endif
1284n/a#ifdef SIGQUIT
1285n/a if (PyModule_AddIntMacro(m, SIGQUIT))
1286n/a goto finally;
1287n/a#endif
1288n/a#ifdef SIGILL
1289n/a if (PyModule_AddIntMacro(m, SIGILL))
1290n/a goto finally;
1291n/a#endif
1292n/a#ifdef SIGTRAP
1293n/a if (PyModule_AddIntMacro(m, SIGTRAP))
1294n/a goto finally;
1295n/a#endif
1296n/a#ifdef SIGIOT
1297n/a if (PyModule_AddIntMacro(m, SIGIOT))
1298n/a goto finally;
1299n/a#endif
1300n/a#ifdef SIGABRT
1301n/a if (PyModule_AddIntMacro(m, SIGABRT))
1302n/a goto finally;
1303n/a#endif
1304n/a#ifdef SIGEMT
1305n/a if (PyModule_AddIntMacro(m, SIGEMT))
1306n/a goto finally;
1307n/a#endif
1308n/a#ifdef SIGFPE
1309n/a if (PyModule_AddIntMacro(m, SIGFPE))
1310n/a goto finally;
1311n/a#endif
1312n/a#ifdef SIGKILL
1313n/a if (PyModule_AddIntMacro(m, SIGKILL))
1314n/a goto finally;
1315n/a#endif
1316n/a#ifdef SIGBUS
1317n/a if (PyModule_AddIntMacro(m, SIGBUS))
1318n/a goto finally;
1319n/a#endif
1320n/a#ifdef SIGSEGV
1321n/a if (PyModule_AddIntMacro(m, SIGSEGV))
1322n/a goto finally;
1323n/a#endif
1324n/a#ifdef SIGSYS
1325n/a if (PyModule_AddIntMacro(m, SIGSYS))
1326n/a goto finally;
1327n/a#endif
1328n/a#ifdef SIGPIPE
1329n/a if (PyModule_AddIntMacro(m, SIGPIPE))
1330n/a goto finally;
1331n/a#endif
1332n/a#ifdef SIGALRM
1333n/a if (PyModule_AddIntMacro(m, SIGALRM))
1334n/a goto finally;
1335n/a#endif
1336n/a#ifdef SIGTERM
1337n/a if (PyModule_AddIntMacro(m, SIGTERM))
1338n/a goto finally;
1339n/a#endif
1340n/a#ifdef SIGUSR1
1341n/a if (PyModule_AddIntMacro(m, SIGUSR1))
1342n/a goto finally;
1343n/a#endif
1344n/a#ifdef SIGUSR2
1345n/a if (PyModule_AddIntMacro(m, SIGUSR2))
1346n/a goto finally;
1347n/a#endif
1348n/a#ifdef SIGCLD
1349n/a if (PyModule_AddIntMacro(m, SIGCLD))
1350n/a goto finally;
1351n/a#endif
1352n/a#ifdef SIGCHLD
1353n/a if (PyModule_AddIntMacro(m, SIGCHLD))
1354n/a goto finally;
1355n/a#endif
1356n/a#ifdef SIGPWR
1357n/a if (PyModule_AddIntMacro(m, SIGPWR))
1358n/a goto finally;
1359n/a#endif
1360n/a#ifdef SIGIO
1361n/a if (PyModule_AddIntMacro(m, SIGIO))
1362n/a goto finally;
1363n/a#endif
1364n/a#ifdef SIGURG
1365n/a if (PyModule_AddIntMacro(m, SIGURG))
1366n/a goto finally;
1367n/a#endif
1368n/a#ifdef SIGWINCH
1369n/a if (PyModule_AddIntMacro(m, SIGWINCH))
1370n/a goto finally;
1371n/a#endif
1372n/a#ifdef SIGPOLL
1373n/a if (PyModule_AddIntMacro(m, SIGPOLL))
1374n/a goto finally;
1375n/a#endif
1376n/a#ifdef SIGSTOP
1377n/a if (PyModule_AddIntMacro(m, SIGSTOP))
1378n/a goto finally;
1379n/a#endif
1380n/a#ifdef SIGTSTP
1381n/a if (PyModule_AddIntMacro(m, SIGTSTP))
1382n/a goto finally;
1383n/a#endif
1384n/a#ifdef SIGCONT
1385n/a if (PyModule_AddIntMacro(m, SIGCONT))
1386n/a goto finally;
1387n/a#endif
1388n/a#ifdef SIGTTIN
1389n/a if (PyModule_AddIntMacro(m, SIGTTIN))
1390n/a goto finally;
1391n/a#endif
1392n/a#ifdef SIGTTOU
1393n/a if (PyModule_AddIntMacro(m, SIGTTOU))
1394n/a goto finally;
1395n/a#endif
1396n/a#ifdef SIGVTALRM
1397n/a if (PyModule_AddIntMacro(m, SIGVTALRM))
1398n/a goto finally;
1399n/a#endif
1400n/a#ifdef SIGPROF
1401n/a if (PyModule_AddIntMacro(m, SIGPROF))
1402n/a goto finally;
1403n/a#endif
1404n/a#ifdef SIGXCPU
1405n/a if (PyModule_AddIntMacro(m, SIGXCPU))
1406n/a goto finally;
1407n/a#endif
1408n/a#ifdef SIGXFSZ
1409n/a if (PyModule_AddIntMacro(m, SIGXFSZ))
1410n/a goto finally;
1411n/a#endif
1412n/a#ifdef SIGRTMIN
1413n/a if (PyModule_AddIntMacro(m, SIGRTMIN))
1414n/a goto finally;
1415n/a#endif
1416n/a#ifdef SIGRTMAX
1417n/a if (PyModule_AddIntMacro(m, SIGRTMAX))
1418n/a goto finally;
1419n/a#endif
1420n/a#ifdef SIGINFO
1421n/a if (PyModule_AddIntMacro(m, SIGINFO))
1422n/a goto finally;
1423n/a#endif
1424n/a
1425n/a#ifdef ITIMER_REAL
1426n/a if (PyModule_AddIntMacro(m, ITIMER_REAL))
1427n/a goto finally;
1428n/a#endif
1429n/a#ifdef ITIMER_VIRTUAL
1430n/a if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1431n/a goto finally;
1432n/a#endif
1433n/a#ifdef ITIMER_PROF
1434n/a if (PyModule_AddIntMacro(m, ITIMER_PROF))
1435n/a goto finally;
1436n/a#endif
1437n/a
1438n/a#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
1439n/a ItimerError = PyErr_NewException("signal.ItimerError",
1440n/a PyExc_IOError, NULL);
1441n/a if (ItimerError != NULL)
1442n/a PyDict_SetItemString(d, "ItimerError", ItimerError);
1443n/a#endif
1444n/a
1445n/a#ifdef CTRL_C_EVENT
1446n/a if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1447n/a goto finally;
1448n/a#endif
1449n/a
1450n/a#ifdef CTRL_BREAK_EVENT
1451n/a if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1452n/a goto finally;
1453n/a#endif
1454n/a
1455n/a#ifdef MS_WINDOWS
1456n/a /* Create manual-reset event, initially unset */
1457n/a sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1458n/a#endif
1459n/a
1460n/a if (PyErr_Occurred()) {
1461n/a Py_DECREF(m);
1462n/a m = NULL;
1463n/a }
1464n/a
1465n/a finally:
1466n/a return m;
1467n/a}
1468n/a
1469n/astatic void
1470n/afinisignal(void)
1471n/a{
1472n/a int i;
1473n/a PyObject *func;
1474n/a
1475n/a PyOS_setsig(SIGINT, old_siginthandler);
1476n/a old_siginthandler = SIG_DFL;
1477n/a
1478n/a for (i = 1; i < NSIG; i++) {
1479n/a func = Handlers[i].func;
1480n/a Handlers[i].tripped = 0;
1481n/a Handlers[i].func = NULL;
1482n/a if (i != SIGINT && func != NULL && func != Py_None &&
1483n/a func != DefaultHandler && func != IgnoreHandler)
1484n/a PyOS_setsig(i, SIG_DFL);
1485n/a Py_XDECREF(func);
1486n/a }
1487n/a
1488n/a Py_CLEAR(IntHandler);
1489n/a Py_CLEAR(DefaultHandler);
1490n/a Py_CLEAR(IgnoreHandler);
1491n/a}
1492n/a
1493n/a
1494n/a/* Declared in pyerrors.h */
1495n/aint
1496n/aPyErr_CheckSignals(void)
1497n/a{
1498n/a int i;
1499n/a PyObject *f;
1500n/a
1501n/a if (!is_tripped)
1502n/a return 0;
1503n/a
1504n/a#ifdef WITH_THREAD
1505n/a if (PyThread_get_thread_ident() != main_thread)
1506n/a return 0;
1507n/a#endif
1508n/a
1509n/a /*
1510n/a * The is_tripped variable is meant to speed up the calls to
1511n/a * PyErr_CheckSignals (both directly or via pending calls) when no
1512n/a * signal has arrived. This variable is set to 1 when a signal arrives
1513n/a * and it is set to 0 here, when we know some signals arrived. This way
1514n/a * we can run the registered handlers with no signals blocked.
1515n/a *
1516n/a * NOTE: with this approach we can have a situation where is_tripped is
1517n/a * 1 but we have no more signals to handle (Handlers[i].tripped
1518n/a * is 0 for every signal i). This won't do us any harm (except
1519n/a * we're gonna spent some cycles for nothing). This happens when
1520n/a * we receive a signal i after we zero is_tripped and before we
1521n/a * check Handlers[i].tripped.
1522n/a */
1523n/a is_tripped = 0;
1524n/a
1525n/a if (!(f = (PyObject *)PyEval_GetFrame()))
1526n/a f = Py_None;
1527n/a
1528n/a for (i = 1; i < NSIG; i++) {
1529n/a if (Handlers[i].tripped) {
1530n/a PyObject *result = NULL;
1531n/a PyObject *arglist = Py_BuildValue("(iO)", i, f);
1532n/a Handlers[i].tripped = 0;
1533n/a
1534n/a if (arglist) {
1535n/a result = PyEval_CallObject(Handlers[i].func,
1536n/a arglist);
1537n/a Py_DECREF(arglist);
1538n/a }
1539n/a if (!result)
1540n/a return -1;
1541n/a
1542n/a Py_DECREF(result);
1543n/a }
1544n/a }
1545n/a
1546n/a return 0;
1547n/a}
1548n/a
1549n/a
1550n/a/* Replacements for intrcheck.c functionality
1551n/a * Declared in pyerrors.h
1552n/a */
1553n/avoid
1554n/aPyErr_SetInterrupt(void)
1555n/a{
1556n/a trip_signal(SIGINT);
1557n/a}
1558n/a
1559n/avoid
1560n/aPyOS_InitInterrupts(void)
1561n/a{
1562n/a PyObject *m = PyImport_ImportModule("_signal");
1563n/a if (m) {
1564n/a Py_DECREF(m);
1565n/a }
1566n/a}
1567n/a
1568n/avoid
1569n/aPyOS_FiniInterrupts(void)
1570n/a{
1571n/a finisignal();
1572n/a}
1573n/a
1574n/aint
1575n/aPyOS_InterruptOccurred(void)
1576n/a{
1577n/a if (Handlers[SIGINT].tripped) {
1578n/a#ifdef WITH_THREAD
1579n/a if (PyThread_get_thread_ident() != main_thread)
1580n/a return 0;
1581n/a#endif
1582n/a Handlers[SIGINT].tripped = 0;
1583n/a return 1;
1584n/a }
1585n/a return 0;
1586n/a}
1587n/a
1588n/astatic void
1589n/a_clear_pending_signals(void)
1590n/a{
1591n/a int i;
1592n/a if (!is_tripped)
1593n/a return;
1594n/a is_tripped = 0;
1595n/a for (i = 1; i < NSIG; ++i) {
1596n/a Handlers[i].tripped = 0;
1597n/a }
1598n/a}
1599n/a
1600n/avoid
1601n/aPyOS_AfterFork(void)
1602n/a{
1603n/a /* Clear the signal flags after forking so that they aren't handled
1604n/a * in both processes if they came in just before the fork() but before
1605n/a * the interpreter had an opportunity to call the handlers. issue9535. */
1606n/a _clear_pending_signals();
1607n/a#ifdef WITH_THREAD
1608n/a /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1609n/a * can be called safely. */
1610n/a PyThread_ReInitTLS();
1611n/a _PyGILState_Reinit();
1612n/a PyEval_ReInitThreads();
1613n/a main_thread = PyThread_get_thread_ident();
1614n/a main_pid = getpid();
1615n/a _PyImport_ReInitLock();
1616n/a#endif
1617n/a}
1618n/a
1619n/aint
1620n/a_PyOS_IsMainThread(void)
1621n/a{
1622n/a#ifdef WITH_THREAD
1623n/a return PyThread_get_thread_ident() == main_thread;
1624n/a#else
1625n/a return 1;
1626n/a#endif
1627n/a}
1628n/a
1629n/a#ifdef MS_WINDOWS
1630n/avoid *_PyOS_SigintEvent(void)
1631n/a{
1632n/a /* Returns a manual-reset event which gets tripped whenever
1633n/a SIGINT is received.
1634n/a
1635n/a Python.h does not include windows.h so we do cannot use HANDLE
1636n/a as the return type of this function. We use void* instead. */
1637n/a return sigint_event;
1638n/a}
1639n/a#endif