Debian Codegraph @

pypy/module/cpyext/src/pysignals.c

raw symbols SCIP
1 #include <Python.h>
2 #include <signal.h>
3
4 /* From pythonrun.c in the standard Python distribution */
5
6 /* Wrappers around sigaction() or signal(). */
7
8 /* It may seem odd that these functions do not interact with the rest of the
9 * system (i.e. their effects are not visible in the signal module) but
10 * this is apparently intentional, CPython works the same way. The signal
11 * handlers defined in the signal module define what happens if the normal
12 * Python signal handler is called.
13 *
14 * A bit whacky, but that's the way it is */
15
16 PyOS_sighandler_t
17 PyOS_getsig(int sig)
18 {
19 #ifdef SA_RESTART
20 /* assume sigaction exists */
21 struct sigaction context;
22 if (sigaction(sig, NULL, &context) == -1)
23 return SIG_ERR;
24 return context.sa_handler;
25 #else
26 PyOS_sighandler_t handler;
27 /* Special signal handling for the secure CRT in Visual Studio 2005 */
28 #if defined(_MSC_VER) && _MSC_VER >= 1400
29 switch (sig) {
30 /* Only these signals are valid */
31 case SIGINT:
32 case SIGILL:
33 case SIGFPE:
34 case SIGSEGV:
35 case SIGTERM:
36 case SIGBREAK:
37 case SIGABRT:
38 break;
39 /* Don't call signal() with other values or it will assert */
40 default:
41 return SIG_ERR;
42 }
43 #endif /* _MSC_VER && _MSC_VER >= 1400 */
44 handler = signal(sig, SIG_IGN);
45 if (handler != SIG_ERR)
46 signal(sig, handler);
47 return handler;
48 #endif
49 }
50
51 /*
52 * All of the code in this function must only use async-signal-safe functions,
53 * listed at `man 7 signal` or
54 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
55 */
56 PyOS_sighandler_t
57 PyOS_setsig(int sig, PyOS_sighandler_t handler)
58 {
59 #ifdef SA_RESTART
60 /* assume sigaction exists */
61 struct sigaction context, ocontext;
62 context.sa_handler = handler;
63 sigemptyset(&context.sa_mask);
64 context.sa_flags = 0;
65 if (sigaction(sig, &context, &ocontext) == -1)
66 return SIG_ERR;
67 return ocontext.sa_handler;
68 #else
69 PyOS_sighandler_t oldhandler;
70 oldhandler = signal(sig, handler);
71 #ifndef MS_WINDOWS
72 /* should check if this exists */
73 siginterrupt(sig, 1);
74 #endif
75 return oldhandler;
76 #endif
77 }