Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ceaa72

Browse files
committed
Patch #975056 - fixes for restartable signals on *BSD. In addition,
a few remaining calls to signal() were converted to PyOS_setsig().
1 parent 7d42878 commit 9ceaa72

5 files changed

Lines changed: 28 additions & 48 deletions

File tree

Modules/posixmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,18 +2903,18 @@ posix_openpty(PyObject *self, PyObject *noargs)
29032903
master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
29042904
if (master_fd < 0)
29052905
return posix_error();
2906-
sig_saved = signal(SIGCHLD, SIG_DFL);
2906+
sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
29072907
/* change permission of slave */
29082908
if (grantpt(master_fd) < 0) {
2909-
signal(SIGCHLD, sig_saved);
2909+
PyOS_setsig(SIGCHLD, sig_saved);
29102910
return posix_error();
29112911
}
29122912
/* unlock slave */
29132913
if (unlockpt(master_fd) < 0) {
2914-
signal(SIGCHLD, sig_saved);
2914+
PyOS_setsig(SIGCHLD, sig_saved);
29152915
return posix_error();
29162916
}
2917-
signal(SIGCHLD, sig_saved);
2917+
PyOS_setsig(SIGCHLD, sig_saved);
29182918
slave_name = ptsname(master_fd); /* get name of slave */
29192919
if (slave_name == NULL)
29202920
return posix_error();

Modules/signalmodule.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ signal_handler(int sig_num)
136136
to the Python handler... */
137137
return;
138138
}
139-
#endif
140-
#ifdef HAVE_SIGINTERRUPT
141-
siginterrupt(sig_num, 1);
142139
#endif
143140
PyOS_setsig(sig_num, signal_handler);
144141
}
@@ -217,9 +214,6 @@ signal_signal(PyObject *self, PyObject *args)
217214
}
218215
else
219216
func = signal_handler;
220-
#ifdef HAVE_SIGINTERRUPT
221-
siginterrupt(sig_num, 1);
222-
#endif
223217
if (PyOS_setsig(sig_num, func) == SIG_ERR) {
224218
PyErr_SetFromErrno(PyExc_RuntimeError);
225219
return NULL;

Modules/socketmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
217217

218218
/* Generic includes */
219219
#include <sys/types.h>
220-
#include <signal.h>
220+
//#include <signal.h>
221221

222222
/* Generic socket object definitions and includes */
223223
#define PySocket_BUILDING_SOCKET

Parser/intrcheck.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ intcatcher(int sig)
137137
Py_Exit(1);
138138
break;
139139
}
140-
signal(SIGINT, intcatcher);
140+
PyOS_setsig(SIGINT, intcatcher);
141141
Py_AddPendingCall(checksignals_witharg, NULL);
142142
}
143143

@@ -146,23 +146,14 @@ static void (*old_siginthandler)(int) = SIG_DFL;
146146
void
147147
PyOS_InitInterrupts(void)
148148
{
149-
if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
150-
signal(SIGINT, intcatcher);
151-
#ifdef HAVE_SIGINTERRUPT
152-
/* This is for SunOS and other modern BSD derivatives.
153-
It means that system calls (like read()) are not restarted
154-
after an interrupt. This is necessary so interrupting a
155-
read() or readline() call works as expected.
156-
XXX On old BSD (pure 4.2 or older) you may have to do this
157-
differently! */
158-
siginterrupt(SIGINT, 1);
159-
#endif /* HAVE_SIGINTERRUPT */
149+
if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
150+
PyOS_setsig(SIGINT, intcatcher);
160151
}
161152

162153
void
163154
PyOS_FiniInterrupts(void)
164155
{
165-
signal(SIGINT, old_siginthandler);
156+
PyOS_setsig(SIGINT, old_siginthandler);
166157
}
167158

168159
int

Python/pythonrun.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,13 +1576,13 @@ static void
15761576
initsigs(void)
15771577
{
15781578
#ifdef SIGPIPE
1579-
signal(SIGPIPE, SIG_IGN);
1579+
PyOS_setsig(SIGPIPE, SIG_IGN);
15801580
#endif
15811581
#ifdef SIGXFZ
1582-
signal(SIGXFZ, SIG_IGN);
1582+
PyOS_setsig(SIGXFZ, SIG_IGN);
15831583
#endif
15841584
#ifdef SIGXFSZ
1585-
signal(SIGXFSZ, SIG_IGN);
1585+
PyOS_setsig(SIGXFSZ, SIG_IGN);
15861586
#endif
15871587
PyOS_InitInterrupts(); /* May imply initsignal() */
15881588
}
@@ -1646,18 +1646,14 @@ PyOS_getsig(int sig)
16461646
{
16471647
#ifdef HAVE_SIGACTION
16481648
struct sigaction context;
1649-
/* Initialize context.sa_handler to SIG_ERR which makes about as
1650-
* much sense as anything else. It should get overwritten if
1651-
* sigaction actually succeeds and otherwise we avoid an
1652-
* uninitialized memory read.
1653-
*/
1654-
context.sa_handler = SIG_ERR;
1655-
sigaction(sig, NULL, &context);
1649+
if (sigaction(sig, NULL, &context) == -1)
1650+
return SIG_ERR;
16561651
return context.sa_handler;
16571652
#else
16581653
PyOS_sighandler_t handler;
16591654
handler = signal(sig, SIG_IGN);
1660-
signal(sig, handler);
1655+
if (handler != SIG_ERR)
1656+
signal(sig, handler);
16611657
return handler;
16621658
#endif
16631659
}
@@ -1666,20 +1662,19 @@ PyOS_sighandler_t
16661662
PyOS_setsig(int sig, PyOS_sighandler_t handler)
16671663
{
16681664
#ifdef HAVE_SIGACTION
1669-
struct sigaction context;
1670-
PyOS_sighandler_t oldhandler;
1671-
/* Initialize context.sa_handler to SIG_ERR which makes about as
1672-
* much sense as anything else. It should get overwritten if
1673-
* sigaction actually succeeds and otherwise we avoid an
1674-
* uninitialized memory read.
1675-
*/
1676-
context.sa_handler = SIG_ERR;
1677-
sigaction(sig, NULL, &context);
1678-
oldhandler = context.sa_handler;
1665+
struct sigaction context, ocontext;
16791666
context.sa_handler = handler;
1680-
sigaction(sig, &context, NULL);
1681-
return oldhandler;
1667+
sigemptyset(&context.sa_mask);
1668+
context.sa_flags = 0;
1669+
if (sigaction(sig, &context, &ocontext) == -1)
1670+
return SIG_ERR;
1671+
return ocontext.sa_handler;
16821672
#else
1683-
return signal(sig, handler);
1673+
PyOS_sighandler_t oldhandler;
1674+
oldhandler = signal(sig, handler);
1675+
#ifdef HAVE_SIGINTERRUPT
1676+
siginterrupt(sig, 1);
1677+
#endif
1678+
return oldhandler;
16841679
#endif
16851680
}

0 commit comments

Comments
 (0)