| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "Python.h" |
| 7 |
#include "intrcheck.h" |
| 8 |
|
| 9 |
#ifdef MS_WINDOWS |
| 10 |
#include <Windows.h> |
| 11 |
#ifdef HAVE_PROCESS_H |
| 12 |
#include <process.h> |
| 13 |
#endif |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifdef HAVE_SIGNAL_H |
| 17 |
#include <signal.h> |
| 18 |
#endif |
| 19 |
#ifdef HAVE_SYS_STAT_H |
| 20 |
#include <sys/stat.h> |
| 21 |
#endif |
| 22 |
#ifdef HAVE_SYS_TIME_H |
| 23 |
#include <sys/time.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifndef SIG_ERR |
| 27 |
#define SIG_ERR ((PyOS_sighandler_t)(-1)) |
| 28 |
#endif |
| 29 |
|
| 30 |
#if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 31 |
#define NSIG 12 |
| 32 |
#include <process.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef NSIG |
| 36 |
# if defined(_NSIG) |
| 37 |
# define NSIG _NSIG |
| 38 |
# elif defined(_SIGMAX) |
| 39 |
# define NSIG (_SIGMAX + 1) |
| 40 |
# elif defined(SIGMAX) |
| 41 |
# define NSIG (SIGMAX + 1) |
| 42 |
# else |
| 43 |
# define NSIG 64 |
| 44 |
# endif |
| 45 |
#endif |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#ifdef WITH_THREAD |
| 78 |
#include <sys/types.h> |
| 79 |
#include "pythread.h" |
| 80 |
static long main_thread; |
| 81 |
static pid_t main_pid; |
| 82 |
#endif |
| 83 |
|
| 84 |
static struct { |
| 85 |
int tripped; |
| 86 |
PyObject *func; |
| 87 |
} Handlers[NSIG]; |
| 88 |
|
| 89 |
static sig_atomic_t wakeup_fd = -1; |
| 90 |
|
| 91 |
|
| 92 |
static volatile sig_atomic_t is_tripped = 0; |
| 93 |
|
| 94 |
static PyObject *DefaultHandler; |
| 95 |
static PyObject *IgnoreHandler; |
| 96 |
static PyObject *IntHandler; |
| 97 |
|
| 98 |
#ifdef HAVE_GETITIMER |
| 99 |
static PyObject *ItimerError; |
| 100 |
|
| 101 |
|
| 102 |
static void |
| 103 |
timeval_from_double(double d, struct timeval *tv) |
| 104 |
{ |
| 105 |
tv->tv_sec = floor(d); |
| 106 |
tv->tv_usec = fmod(d, 1.0) * 1000000.0; |
| 107 |
|
| 108 |
if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) { |
| 109 |
tv->tv_usec = 1; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
Py_LOCAL_INLINE(double) |
| 114 |
double_from_timeval(struct timeval *tv) |
| 115 |
{ |
| 116 |
return tv->tv_sec + (double)(tv->tv_usec / 1000000.0); |
| 117 |
} |
| 118 |
|
| 119 |
static PyObject * |
| 120 |
itimer_retval(struct itimerval *iv) |
| 121 |
{ |
| 122 |
PyObject *r, *v; |
| 123 |
|
| 124 |
r = PyTuple_New(2); |
| 125 |
if (r == NULL) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) { |
| 129 |
Py_DECREF(r); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
PyTuple_SET_ITEM(r, 0, v); |
| 134 |
|
| 135 |
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) { |
| 136 |
Py_DECREF(r); |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
|
| 140 |
PyTuple_SET_ITEM(r, 1, v); |
| 141 |
|
| 142 |
return r; |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
static PyObject * |
| 147 |
signal_default_int_handler(PyObject *self, PyObject *args) |
| 148 |
{ |
| 149 |
PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
PyDoc_STRVAR(default_int_handler_doc, |
| 154 |
"default_int_handler(...)\n\ |
| 155 |
\n\ |
| 156 |
The default handler for SIGINT installed by Python.\n\ |
| 157 |
It raises KeyboardInterrupt."); |
| 158 |
|
| 159 |
|
| 160 |
static int |
| 161 |
checksignals_witharg(void * unused) |
| 162 |
{ |
| 163 |
return PyErr_CheckSignals(); |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
trip_signal(int sig_num) |
| 168 |
{ |
| 169 |
Handlers[sig_num].tripped = 1; |
| 170 |
if (is_tripped) |
| 171 |
return; |
| 172 |
|
| 173 |
|
| 174 |
is_tripped = 1; |
| 175 |
Py_AddPendingCall(checksignals_witharg, NULL); |
| 176 |
if (wakeup_fd != -1) { |
| 177 |
int rc = write(wakeup_fd, "\0", 1); |
| 178 |
(void)rc; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
signal_handler(int sig_num) |
| 184 |
{ |
| 185 |
int save_errno = errno; |
| 186 |
|
| 187 |
#if defined(WITH_THREAD) && defined(WITH_PTH) |
| 188 |
if (PyThread_get_thread_ident() != main_thread) { |
| 189 |
pth_raise(*(pth_t *) main_thread, sig_num); |
| 190 |
} |
| 191 |
else |
| 192 |
#endif |
| 193 |
{ |
| 194 |
#ifdef WITH_THREAD |
| 195 |
|
| 196 |
if (getpid() == main_pid) |
| 197 |
#endif |
| 198 |
{ |
| 199 |
trip_signal(sig_num); |
| 200 |
} |
| 201 |
|
| 202 |
#ifndef HAVE_SIGACTION |
| 203 |
#ifdef SIGCHLD |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if (sig_num != SIGCHLD) |
| 209 |
#endif |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
PyOS_setsig(sig_num, signal_handler); |
| 214 |
#endif |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
errno = save_errno; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
#ifdef HAVE_ALARM |
| 224 |
static PyObject * |
| 225 |
signal_alarm(PyObject *self, PyObject *args) |
| 226 |
{ |
| 227 |
int t; |
| 228 |
if (!PyArg_ParseTuple(args, "i:alarm", &t)) |
| 229 |
return NULL; |
| 230 |
|
| 231 |
return PyInt_FromLong((long)alarm(t)); |
| 232 |
} |
| 233 |
|
| 234 |
PyDoc_STRVAR(alarm_doc, |
| 235 |
"alarm(seconds)\n\ |
| 236 |
\n\ |
| 237 |
Arrange for SIGALRM to arrive after the given number of seconds."); |
| 238 |
#endif |
| 239 |
|
| 240 |
#ifdef HAVE_PAUSE |
| 241 |
static PyObject * |
| 242 |
signal_pause(PyObject *self) |
| 243 |
{ |
| 244 |
Py_BEGIN_ALLOW_THREADS |
| 245 |
(void)pause(); |
| 246 |
Py_END_ALLOW_THREADS |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if (PyErr_CheckSignals()) |
| 251 |
return NULL; |
| 252 |
|
| 253 |
Py_INCREF(Py_None); |
| 254 |
return Py_None; |
| 255 |
} |
| 256 |
PyDoc_STRVAR(pause_doc, |
| 257 |
"pause()\n\ |
| 258 |
\n\ |
| 259 |
Wait until a signal arrives."); |
| 260 |
|
| 261 |
#endif |
| 262 |
|
| 263 |
|
| 264 |
static PyObject * |
| 265 |
signal_signal(PyObject *self, PyObject *args) |
| 266 |
{ |
| 267 |
PyObject *obj; |
| 268 |
int sig_num; |
| 269 |
PyObject *old_handler; |
| 270 |
void (*func)(int); |
| 271 |
if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) |
| 272 |
return NULL; |
| 273 |
#ifdef MS_WINDOWS |
| 274 |
|
| 275 |
switch (sig_num) { |
| 276 |
case SIGABRT: break; |
| 277 |
#ifdef SIGBREAK |
| 278 |
|
| 279 |
|
| 280 |
case SIGBREAK: break; |
| 281 |
#endif |
| 282 |
case SIGFPE: break; |
| 283 |
case SIGILL: break; |
| 284 |
case SIGINT: break; |
| 285 |
case SIGSEGV: break; |
| 286 |
case SIGTERM: break; |
| 287 |
default: |
| 288 |
PyErr_SetString(PyExc_ValueError, "invalid signal value"); |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
#endif |
| 292 |
#ifdef WITH_THREAD |
| 293 |
if (PyThread_get_thread_ident() != main_thread) { |
| 294 |
PyErr_SetString(PyExc_ValueError, |
| 295 |
"signal only works in main thread"); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
#endif |
| 299 |
if (sig_num < 1 || sig_num >= NSIG) { |
| 300 |
PyErr_SetString(PyExc_ValueError, |
| 301 |
"signal number out of range"); |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
if (obj == IgnoreHandler) |
| 305 |
func = SIG_IGN; |
| 306 |
else if (obj == DefaultHandler) |
| 307 |
func = SIG_DFL; |
| 308 |
else if (!PyCallable_Check(obj)) { |
| 309 |
PyErr_SetString(PyExc_TypeError, |
| 310 |
"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
| 311 |
return NULL; |
| 312 |
} |
| 313 |
else |
| 314 |
func = signal_handler; |
| 315 |
|
| 316 |
if (PyErr_CheckSignals()) { |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
if (PyOS_setsig(sig_num, func) == SIG_ERR) { |
| 320 |
PyErr_SetFromErrno(PyExc_RuntimeError); |
| 321 |
return NULL; |
| 322 |
} |
| 323 |
old_handler = Handlers[sig_num].func; |
| 324 |
Py_INCREF(obj); |
| 325 |
Handlers[sig_num].func = obj; |
| 326 |
if (old_handler != NULL) |
| 327 |
return old_handler; |
| 328 |
else |
| 329 |
Py_RETURN_NONE; |
| 330 |
} |
| 331 |
|
| 332 |
PyDoc_STRVAR(signal_doc, |
| 333 |
"signal(sig, action) -> action\n\ |
| 334 |
\n\ |
| 335 |
Set the action for the given signal. The action can be SIG_DFL,\n\ |
| 336 |
SIG_IGN, or a callable Python object. The previous action is\n\ |
| 337 |
returned. See getsignal() for possible return values.\n\ |
| 338 |
\n\ |
| 339 |
*** IMPORTANT NOTICE ***\n\ |
| 340 |
A signal handler function is called with two arguments:\n\ |
| 341 |
the first is the signal number, the second is the interrupted stack frame."); |
| 342 |
|
| 343 |
|
| 344 |
static PyObject * |
| 345 |
signal_getsignal(PyObject *self, PyObject *args) |
| 346 |
{ |
| 347 |
int sig_num; |
| 348 |
PyObject *old_handler; |
| 349 |
if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) |
| 350 |
return NULL; |
| 351 |
if (sig_num < 1 || sig_num >= NSIG) { |
| 352 |
PyErr_SetString(PyExc_ValueError, |
| 353 |
"signal number out of range"); |
| 354 |
return NULL; |
| 355 |
} |
| 356 |
old_handler = Handlers[sig_num].func; |
| 357 |
if (old_handler != NULL) { |
| 358 |
Py_INCREF(old_handler); |
| 359 |
return old_handler; |
| 360 |
} |
| 361 |
else { |
| 362 |
Py_RETURN_NONE; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
PyDoc_STRVAR(getsignal_doc, |
| 367 |
"getsignal(sig) -> action\n\ |
| 368 |
\n\ |
| 369 |
Return the current action for the given signal. The return value can be:\n\ |
| 370 |
SIG_IGN -- if the signal is being ignored\n\ |
| 371 |
SIG_DFL -- if the default action for the signal is in effect\n\ |
| 372 |
None -- if an unknown handler is in effect\n\ |
| 373 |
anything else -- the callable Python object used as a handler"); |
| 374 |
|
| 375 |
#ifdef HAVE_SIGINTERRUPT |
| 376 |
PyDoc_STRVAR(siginterrupt_doc, |
| 377 |
"siginterrupt(sig, flag) -> None\n\ |
| 378 |
change system call restart behaviour: if flag is False, system calls\n\ |
| 379 |
will be restarted when interrupted by signal sig, else system calls\n\ |
| 380 |
will be interrupted."); |
| 381 |
|
| 382 |
static PyObject * |
| 383 |
signal_siginterrupt(PyObject *self, PyObject *args) |
| 384 |
{ |
| 385 |
int sig_num; |
| 386 |
int flag; |
| 387 |
|
| 388 |
if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag)) |
| 389 |
return NULL; |
| 390 |
if (sig_num < 1 || sig_num >= NSIG) { |
| 391 |
PyErr_SetString(PyExc_ValueError, |
| 392 |
"signal number out of range"); |
| 393 |
return NULL; |
| 394 |
} |
| 395 |
if (siginterrupt(sig_num, flag)<0) { |
| 396 |
PyErr_SetFromErrno(PyExc_RuntimeError); |
| 397 |
return NULL; |
| 398 |
} |
| 399 |
|
| 400 |
Py_INCREF(Py_None); |
| 401 |
return Py_None; |
| 402 |
} |
| 403 |
|
| 404 |
#endif |
| 405 |
|
| 406 |
static PyObject * |
| 407 |
signal_set_wakeup_fd(PyObject *self, PyObject *args) |
| 408 |
{ |
| 409 |
struct stat buf; |
| 410 |
int fd, old_fd; |
| 411 |
if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) |
| 412 |
return NULL; |
| 413 |
#ifdef WITH_THREAD |
| 414 |
if (PyThread_get_thread_ident() != main_thread) { |
| 415 |
PyErr_SetString(PyExc_ValueError, |
| 416 |
"set_wakeup_fd only works in main thread"); |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
#endif |
| 420 |
if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) { |
| 421 |
PyErr_SetString(PyExc_ValueError, "invalid fd"); |
| 422 |
return NULL; |
| 423 |
} |
| 424 |
old_fd = wakeup_fd; |
| 425 |
wakeup_fd = fd; |
| 426 |
return PyLong_FromLong(old_fd); |
| 427 |
} |
| 428 |
|
| 429 |
PyDoc_STRVAR(set_wakeup_fd_doc, |
| 430 |
"set_wakeup_fd(fd) -> fd\n\ |
| 431 |
\n\ |
| 432 |
Sets the fd to be written to (with '\\0') when a signal\n\ |
| 433 |
comes in. A library can use this to wakeup select or poll.\n\ |
| 434 |
The previous fd is returned.\n\ |
| 435 |
\n\ |
| 436 |
The fd must be non-blocking."); |
| 437 |
|
| 438 |
|
| 439 |
int |
| 440 |
PySignal_SetWakeupFd(int fd) |
| 441 |
{ |
| 442 |
int old_fd = wakeup_fd; |
| 443 |
if (fd < 0) |
| 444 |
fd = -1; |
| 445 |
wakeup_fd = fd; |
| 446 |
return old_fd; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
#ifdef HAVE_SETITIMER |
| 451 |
static PyObject * |
| 452 |
signal_setitimer(PyObject *self, PyObject *args) |
| 453 |
{ |
| 454 |
double first; |
| 455 |
double interval = 0; |
| 456 |
int which; |
| 457 |
struct itimerval new, old; |
| 458 |
|
| 459 |
if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval)) |
| 460 |
return NULL; |
| 461 |
|
| 462 |
timeval_from_double(first, &new.it_value); |
| 463 |
timeval_from_double(interval, &new.it_interval); |
| 464 |
|
| 465 |
if (setitimer(which, &new, &old) != 0) { |
| 466 |
PyErr_SetFromErrno(ItimerError); |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
return itimer_retval(&old); |
| 471 |
} |
| 472 |
|
| 473 |
PyDoc_STRVAR(setitimer_doc, |
| 474 |
"setitimer(which, seconds[, interval])\n\ |
| 475 |
\n\ |
| 476 |
Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\ |
| 477 |
or ITIMER_PROF) to fire after value seconds and after\n\ |
| 478 |
that every interval seconds.\n\ |
| 479 |
The itimer can be cleared by setting seconds to zero.\n\ |
| 480 |
\n\ |
| 481 |
Returns old values as a tuple: (delay, interval)."); |
| 482 |
#endif |
| 483 |
|
| 484 |
|
| 485 |
#ifdef HAVE_GETITIMER |
| 486 |
static PyObject * |
| 487 |
signal_getitimer(PyObject *self, PyObject *args) |
| 488 |
{ |
| 489 |
int which; |
| 490 |
struct itimerval old; |
| 491 |
|
| 492 |
if (!PyArg_ParseTuple(args, "i:getitimer", &which)) |
| 493 |
return NULL; |
| 494 |
|
| 495 |
if (getitimer(which, &old) != 0) { |
| 496 |
PyErr_SetFromErrno(ItimerError); |
| 497 |
return NULL; |
| 498 |
} |
| 499 |
|
| 500 |
return itimer_retval(&old); |
| 501 |
} |
| 502 |
|
| 503 |
PyDoc_STRVAR(getitimer_doc, |
| 504 |
"getitimer(which)\n\ |
| 505 |
\n\ |
| 506 |
Returns current value of given itimer."); |
| 507 |
#endif |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
static PyMethodDef signal_methods[] = { |
| 512 |
#ifdef HAVE_ALARM |
| 513 |
{"alarm", signal_alarm, METH_VARARGS, alarm_doc}, |
| 514 |
#endif |
| 515 |
#ifdef HAVE_SETITIMER |
| 516 |
{"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc}, |
| 517 |
#endif |
| 518 |
#ifdef HAVE_GETITIMER |
| 519 |
{"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc}, |
| 520 |
#endif |
| 521 |
{"signal", signal_signal, METH_VARARGS, signal_doc}, |
| 522 |
{"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc}, |
| 523 |
{"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, |
| 524 |
#ifdef HAVE_SIGINTERRUPT |
| 525 |
{"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc}, |
| 526 |
#endif |
| 527 |
#ifdef HAVE_PAUSE |
| 528 |
{"pause", (PyCFunction)signal_pause, |
| 529 |
METH_NOARGS,pause_doc}, |
| 530 |
#endif |
| 531 |
{"default_int_handler", signal_default_int_handler, |
| 532 |
METH_VARARGS, default_int_handler_doc}, |
| 533 |
{NULL, NULL} |
| 534 |
}; |
| 535 |
|
| 536 |
|
| 537 |
PyDoc_STRVAR(module_doc, |
| 538 |
"This module provides mechanisms to use signal handlers in Python.\n\ |
| 539 |
\n\ |
| 540 |
Functions:\n\ |
| 541 |
\n\ |
| 542 |
alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
| 543 |
setitimer() -- cause a signal (described below) after a specified\n\ |
| 544 |
float time and the timer may restart then [Unix only]\n\ |
| 545 |
getitimer() -- get current value of timer [Unix only]\n\ |
| 546 |
signal() -- set the action for a given signal\n\ |
| 547 |
getsignal() -- get the signal action for a given signal\n\ |
| 548 |
pause() -- wait until a signal arrives [Unix only]\n\ |
| 549 |
default_int_handler() -- default SIGINT handler\n\ |
| 550 |
\n\ |
| 551 |
signal constants:\n\ |
| 552 |
SIG_DFL -- used to refer to the system default handler\n\ |
| 553 |
SIG_IGN -- used to ignore the signal\n\ |
| 554 |
NSIG -- number of defined signals\n\ |
| 555 |
SIGINT, SIGTERM, etc. -- signal numbers\n\ |
| 556 |
\n\ |
| 557 |
itimer constants:\n\ |
| 558 |
ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\ |
| 559 |
expiration\n\ |
| 560 |
ITIMER_VIRTUAL -- decrements only when the process is executing,\n\ |
| 561 |
and delivers SIGVTALRM upon expiration\n\ |
| 562 |
ITIMER_PROF -- decrements both when the process is executing and\n\ |
| 563 |
when the system is executing on behalf of the process.\n\ |
| 564 |
Coupled with ITIMER_VIRTUAL, this timer is usually\n\ |
| 565 |
used to profile the time spent by the application\n\ |
| 566 |
in user and kernel space. SIGPROF is delivered upon\n\ |
| 567 |
expiration.\n\ |
| 568 |
\n\n\ |
| 569 |
*** IMPORTANT NOTICE ***\n\ |
| 570 |
A signal handler function is called with two arguments:\n\ |
| 571 |
the first is the signal number, the second is the interrupted stack frame."); |
| 572 |
|
| 573 |
PyMODINIT_FUNC |
| 574 |
initsignal(void) |
| 575 |
{ |
| 576 |
PyObject *m, *d, *x; |
| 577 |
int i; |
| 578 |
|
| 579 |
#ifdef WITH_THREAD |
| 580 |
main_thread = PyThread_get_thread_ident(); |
| 581 |
main_pid = getpid(); |
| 582 |
#endif |
| 583 |
|
| 584 |
|
| 585 |
m = Py_InitModule3("signal", signal_methods, module_doc); |
| 586 |
if (m == NULL) |
| 587 |
return; |
| 588 |
|
| 589 |
|
| 590 |
d = PyModule_GetDict(m); |
| 591 |
|
| 592 |
x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
| 593 |
if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
| 594 |
goto finally; |
| 595 |
|
| 596 |
x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
| 597 |
if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
| 598 |
goto finally; |
| 599 |
|
| 600 |
x = PyInt_FromLong((long)NSIG); |
| 601 |
if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
| 602 |
goto finally; |
| 603 |
Py_DECREF(x); |
| 604 |
|
| 605 |
x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
| 606 |
if (!x) |
| 607 |
goto finally; |
| 608 |
Py_INCREF(IntHandler); |
| 609 |
|
| 610 |
Handlers[0].tripped = 0; |
| 611 |
for (i = 1; i < NSIG; i++) { |
| 612 |
void (*t)(int); |
| 613 |
t = PyOS_getsig(i); |
| 614 |
Handlers[i].tripped = 0; |
| 615 |
if (t == SIG_DFL) |
| 616 |
Handlers[i].func = DefaultHandler; |
| 617 |
else if (t == SIG_IGN) |
| 618 |
Handlers[i].func = IgnoreHandler; |
| 619 |
else |
| 620 |
Handlers[i].func = Py_None; |
| 621 |
Py_INCREF(Handlers[i].func); |
| 622 |
} |
| 623 |
if (Handlers[SIGINT].func == DefaultHandler) { |
| 624 |
|
| 625 |
Py_INCREF(IntHandler); |
| 626 |
Py_SETREF(Handlers[SIGINT].func, IntHandler); |
| 627 |
PyOS_setsig(SIGINT, signal_handler); |
| 628 |
} |
| 629 |
|
| 630 |
#ifdef SIGHUP |
| 631 |
x = PyInt_FromLong(SIGHUP); |
| 632 |
PyDict_SetItemString(d, "SIGHUP", x); |
| 633 |
Py_XDECREF(x); |
| 634 |
#endif |
| 635 |
#ifdef SIGINT |
| 636 |
x = PyInt_FromLong(SIGINT); |
| 637 |
PyDict_SetItemString(d, "SIGINT", x); |
| 638 |
Py_XDECREF(x); |
| 639 |
#endif |
| 640 |
#ifdef SIGBREAK |
| 641 |
x = PyInt_FromLong(SIGBREAK); |
| 642 |
PyDict_SetItemString(d, "SIGBREAK", x); |
| 643 |
Py_XDECREF(x); |
| 644 |
#endif |
| 645 |
#ifdef SIGQUIT |
| 646 |
x = PyInt_FromLong(SIGQUIT); |
| 647 |
PyDict_SetItemString(d, "SIGQUIT", x); |
| 648 |
Py_XDECREF(x); |
| 649 |
#endif |
| 650 |
#ifdef SIGILL |
| 651 |
x = PyInt_FromLong(SIGILL); |
| 652 |
PyDict_SetItemString(d, "SIGILL", x); |
| 653 |
Py_XDECREF(x); |
| 654 |
#endif |
| 655 |
#ifdef SIGTRAP |
| 656 |
x = PyInt_FromLong(SIGTRAP); |
| 657 |
PyDict_SetItemString(d, "SIGTRAP", x); |
| 658 |
Py_XDECREF(x); |
| 659 |
#endif |
| 660 |
#ifdef SIGIOT |
| 661 |
x = PyInt_FromLong(SIGIOT); |
| 662 |
PyDict_SetItemString(d, "SIGIOT", x); |
| 663 |
Py_XDECREF(x); |
| 664 |
#endif |
| 665 |
#ifdef SIGABRT |
| 666 |
x = PyInt_FromLong(SIGABRT); |
| 667 |
PyDict_SetItemString(d, "SIGABRT", x); |
| 668 |
Py_XDECREF(x); |
| 669 |
#endif |
| 670 |
#ifdef SIGEMT |
| 671 |
x = PyInt_FromLong(SIGEMT); |
| 672 |
PyDict_SetItemString(d, "SIGEMT", x); |
| 673 |
Py_XDECREF(x); |
| 674 |
#endif |
| 675 |
#ifdef SIGFPE |
| 676 |
x = PyInt_FromLong(SIGFPE); |
| 677 |
PyDict_SetItemString(d, "SIGFPE", x); |
| 678 |
Py_XDECREF(x); |
| 679 |
#endif |
| 680 |
#ifdef SIGKILL |
| 681 |
x = PyInt_FromLong(SIGKILL); |
| 682 |
PyDict_SetItemString(d, "SIGKILL", x); |
| 683 |
Py_XDECREF(x); |
| 684 |
#endif |
| 685 |
#ifdef SIGBUS |
| 686 |
x = PyInt_FromLong(SIGBUS); |
| 687 |
PyDict_SetItemString(d, "SIGBUS", x); |
| 688 |
Py_XDECREF(x); |
| 689 |
#endif |
| 690 |
#ifdef SIGSEGV |
| 691 |
x = PyInt_FromLong(SIGSEGV); |
| 692 |
PyDict_SetItemString(d, "SIGSEGV", x); |
| 693 |
Py_XDECREF(x); |
| 694 |
#endif |
| 695 |
#ifdef SIGSYS |
| 696 |
x = PyInt_FromLong(SIGSYS); |
| 697 |
PyDict_SetItemString(d, "SIGSYS", x); |
| 698 |
Py_XDECREF(x); |
| 699 |
#endif |
| 700 |
#ifdef SIGPIPE |
| 701 |
x = PyInt_FromLong(SIGPIPE); |
| 702 |
PyDict_SetItemString(d, "SIGPIPE", x); |
| 703 |
Py_XDECREF(x); |
| 704 |
#endif |
| 705 |
#ifdef SIGALRM |
| 706 |
x = PyInt_FromLong(SIGALRM); |
| 707 |
PyDict_SetItemString(d, "SIGALRM", x); |
| 708 |
Py_XDECREF(x); |
| 709 |
#endif |
| 710 |
#ifdef SIGTERM |
| 711 |
x = PyInt_FromLong(SIGTERM); |
| 712 |
PyDict_SetItemString(d, "SIGTERM", x); |
| 713 |
Py_XDECREF(x); |
| 714 |
#endif |
| 715 |
#ifdef SIGUSR1 |
| 716 |
x = PyInt_FromLong(SIGUSR1); |
| 717 |
PyDict_SetItemString(d, "SIGUSR1", x); |
| 718 |
Py_XDECREF(x); |
| 719 |
#endif |
| 720 |
#ifdef SIGUSR2 |
| 721 |
x = PyInt_FromLong(SIGUSR2); |
| 722 |
PyDict_SetItemString(d, "SIGUSR2", x); |
| 723 |
Py_XDECREF(x); |
| 724 |
#endif |
| 725 |
#ifdef SIGCLD |
| 726 |
x = PyInt_FromLong(SIGCLD); |
| 727 |
PyDict_SetItemString(d, "SIGCLD", x); |
| 728 |
Py_XDECREF(x); |
| 729 |
#endif |
| 730 |
#ifdef SIGCHLD |
| 731 |
x = PyInt_FromLong(SIGCHLD); |
| 732 |
PyDict_SetItemString(d, "SIGCHLD", x); |
| 733 |
Py_XDECREF(x); |
| 734 |
#endif |
| 735 |
#ifdef SIGPWR |
| 736 |
x = PyInt_FromLong(SIGPWR); |
| 737 |
PyDict_SetItemString(d, "SIGPWR", x); |
| 738 |
Py_XDECREF(x); |
| 739 |
#endif |
| 740 |
#ifdef SIGIO |
| 741 |
x = PyInt_FromLong(SIGIO); |
| 742 |
PyDict_SetItemString(d, "SIGIO", x); |
| 743 |
Py_XDECREF(x); |
| 744 |
#endif |
| 745 |
#ifdef SIGURG |
| 746 |
x = PyInt_FromLong(SIGURG); |
| 747 |
PyDict_SetItemString(d, "SIGURG", x); |
| 748 |
Py_XDECREF(x); |
| 749 |
#endif |
| 750 |
#ifdef SIGWINCH |
| 751 |
x = PyInt_FromLong(SIGWINCH); |
| 752 |
PyDict_SetItemString(d, "SIGWINCH", x); |
| 753 |
Py_XDECREF(x); |
| 754 |
#endif |
| 755 |
#ifdef SIGPOLL |
| 756 |
x = PyInt_FromLong(SIGPOLL); |
| 757 |
PyDict_SetItemString(d, "SIGPOLL", x); |
| 758 |
Py_XDECREF(x); |
| 759 |
#endif |
| 760 |
#ifdef SIGSTOP |
| 761 |
x = PyInt_FromLong(SIGSTOP); |
| 762 |
PyDict_SetItemString(d, "SIGSTOP", x); |
| 763 |
Py_XDECREF(x); |
| 764 |
#endif |
| 765 |
#ifdef SIGTSTP |
| 766 |
x = PyInt_FromLong(SIGTSTP); |
| 767 |
PyDict_SetItemString(d, "SIGTSTP", x); |
| 768 |
Py_XDECREF(x); |
| 769 |
#endif |
| 770 |
#ifdef SIGCONT |
| 771 |
x = PyInt_FromLong(SIGCONT); |
| 772 |
PyDict_SetItemString(d, "SIGCONT", x); |
| 773 |
Py_XDECREF(x); |
| 774 |
#endif |
| 775 |
#ifdef SIGTTIN |
| 776 |
x = PyInt_FromLong(SIGTTIN); |
| 777 |
PyDict_SetItemString(d, "SIGTTIN", x); |
| 778 |
Py_XDECREF(x); |
| 779 |
#endif |
| 780 |
#ifdef SIGTTOU |
| 781 |
x = PyInt_FromLong(SIGTTOU); |
| 782 |
PyDict_SetItemString(d, "SIGTTOU", x); |
| 783 |
Py_XDECREF(x); |
| 784 |
#endif |
| 785 |
#ifdef SIGVTALRM |
| 786 |
x = PyInt_FromLong(SIGVTALRM); |
| 787 |
PyDict_SetItemString(d, "SIGVTALRM", x); |
| 788 |
Py_XDECREF(x); |
| 789 |
#endif |
| 790 |
#ifdef SIGPROF |
| 791 |
x = PyInt_FromLong(SIGPROF); |
| 792 |
PyDict_SetItemString(d, "SIGPROF", x); |
| 793 |
Py_XDECREF(x); |
| 794 |
#endif |
| 795 |
#ifdef SIGXCPU |
| 796 |
x = PyInt_FromLong(SIGXCPU); |
| 797 |
PyDict_SetItemString(d, "SIGXCPU", x); |
| 798 |
Py_XDECREF(x); |
| 799 |
#endif |
| 800 |
#ifdef SIGXFSZ |
| 801 |
x = PyInt_FromLong(SIGXFSZ); |
| 802 |
PyDict_SetItemString(d, "SIGXFSZ", x); |
| 803 |
Py_XDECREF(x); |
| 804 |
#endif |
| 805 |
#ifdef SIGRTMIN |
| 806 |
x = PyInt_FromLong(SIGRTMIN); |
| 807 |
PyDict_SetItemString(d, "SIGRTMIN", x); |
| 808 |
Py_XDECREF(x); |
| 809 |
#endif |
| 810 |
#ifdef SIGRTMAX |
| 811 |
x = PyInt_FromLong(SIGRTMAX); |
| 812 |
PyDict_SetItemString(d, "SIGRTMAX", x); |
| 813 |
Py_XDECREF(x); |
| 814 |
#endif |
| 815 |
#ifdef SIGINFO |
| 816 |
x = PyInt_FromLong(SIGINFO); |
| 817 |
PyDict_SetItemString(d, "SIGINFO", x); |
| 818 |
Py_XDECREF(x); |
| 819 |
#endif |
| 820 |
|
| 821 |
#ifdef ITIMER_REAL |
| 822 |
x = PyLong_FromLong(ITIMER_REAL); |
| 823 |
PyDict_SetItemString(d, "ITIMER_REAL", x); |
| 824 |
Py_DECREF(x); |
| 825 |
#endif |
| 826 |
#ifdef ITIMER_VIRTUAL |
| 827 |
x = PyLong_FromLong(ITIMER_VIRTUAL); |
| 828 |
PyDict_SetItemString(d, "ITIMER_VIRTUAL", x); |
| 829 |
Py_DECREF(x); |
| 830 |
#endif |
| 831 |
#ifdef ITIMER_PROF |
| 832 |
x = PyLong_FromLong(ITIMER_PROF); |
| 833 |
PyDict_SetItemString(d, "ITIMER_PROF", x); |
| 834 |
Py_DECREF(x); |
| 835 |
#endif |
| 836 |
|
| 837 |
#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) |
| 838 |
ItimerError = PyErr_NewException("signal.ItimerError", |
| 839 |
PyExc_IOError, NULL); |
| 840 |
if (ItimerError != NULL) |
| 841 |
PyDict_SetItemString(d, "ItimerError", ItimerError); |
| 842 |
#endif |
| 843 |
|
| 844 |
#ifdef CTRL_C_EVENT |
| 845 |
x = PyInt_FromLong(CTRL_C_EVENT); |
| 846 |
PyDict_SetItemString(d, "CTRL_C_EVENT", x); |
| 847 |
Py_DECREF(x); |
| 848 |
#endif |
| 849 |
|
| 850 |
#ifdef CTRL_BREAK_EVENT |
| 851 |
x = PyInt_FromLong(CTRL_BREAK_EVENT); |
| 852 |
PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x); |
| 853 |
Py_DECREF(x); |
| 854 |
#endif |
| 855 |
|
| 856 |
if (!PyErr_Occurred()) |
| 857 |
return; |
| 858 |
|
| 859 |
|
| 860 |
finally: |
| 861 |
return; |
| 862 |
} |
| 863 |
|
| 864 |
static void |
| 865 |
finisignal(void) |
| 866 |
{ |
| 867 |
int i; |
| 868 |
PyObject *func; |
| 869 |
|
| 870 |
for (i = 1; i < NSIG; i++) { |
| 871 |
func = Handlers[i].func; |
| 872 |
Handlers[i].tripped = 0; |
| 873 |
Handlers[i].func = NULL; |
| 874 |
if (func != NULL && func != Py_None && |
| 875 |
func != DefaultHandler && func != IgnoreHandler) |
| 876 |
PyOS_setsig(i, SIG_DFL); |
| 877 |
Py_XDECREF(func); |
| 878 |
} |
| 879 |
|
| 880 |
Py_XDECREF(IntHandler); |
| 881 |
IntHandler = NULL; |
| 882 |
Py_XDECREF(DefaultHandler); |
| 883 |
DefaultHandler = NULL; |
| 884 |
Py_XDECREF(IgnoreHandler); |
| 885 |
IgnoreHandler = NULL; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
int |
| 891 |
PyErr_CheckSignals(void) |
| 892 |
{ |
| 893 |
int i; |
| 894 |
PyObject *f; |
| 895 |
|
| 896 |
if (!is_tripped) |
| 897 |
return 0; |
| 898 |
|
| 899 |
#ifdef WITH_THREAD |
| 900 |
if (PyThread_get_thread_ident() != main_thread) |
| 901 |
return 0; |
| 902 |
#endif |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
is_tripped = 0; |
| 919 |
|
| 920 |
if (!(f = (PyObject *)PyEval_GetFrame())) |
| 921 |
f = Py_None; |
| 922 |
|
| 923 |
for (i = 1; i < NSIG; i++) { |
| 924 |
if (Handlers[i].tripped) { |
| 925 |
PyObject *result = NULL; |
| 926 |
PyObject *arglist = Py_BuildValue("(iO)", i, f); |
| 927 |
Handlers[i].tripped = 0; |
| 928 |
|
| 929 |
if (arglist) { |
| 930 |
result = PyEval_CallObject(Handlers[i].func, |
| 931 |
arglist); |
| 932 |
Py_DECREF(arglist); |
| 933 |
} |
| 934 |
if (!result) |
| 935 |
return -1; |
| 936 |
|
| 937 |
Py_DECREF(result); |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
void |
| 949 |
PyErr_SetInterrupt(void) |
| 950 |
{ |
| 951 |
trip_signal(SIGINT); |
| 952 |
} |
| 953 |
|
| 954 |
void |
| 955 |
PyOS_InitInterrupts(void) |
| 956 |
{ |
| 957 |
initsignal(); |
| 958 |
_PyImport_FixupExtension("signal", "signal"); |
| 959 |
} |
| 960 |
|
| 961 |
void |
| 962 |
PyOS_FiniInterrupts(void) |
| 963 |
{ |
| 964 |
finisignal(); |
| 965 |
} |
| 966 |
|
| 967 |
int |
| 968 |
PyOS_InterruptOccurred(void) |
| 969 |
{ |
| 970 |
if (Handlers[SIGINT].tripped) { |
| 971 |
#ifdef WITH_THREAD |
| 972 |
if (PyThread_get_thread_ident() != main_thread) |
| 973 |
return 0; |
| 974 |
#endif |
| 975 |
Handlers[SIGINT].tripped = 0; |
| 976 |
return 1; |
| 977 |
} |
| 978 |
return 0; |
| 979 |
} |
| 980 |
|
| 981 |
static void |
| 982 |
_clear_pending_signals(void) |
| 983 |
{ |
| 984 |
int i; |
| 985 |
if (!is_tripped) |
| 986 |
return; |
| 987 |
is_tripped = 0; |
| 988 |
for (i = 1; i < NSIG; ++i) { |
| 989 |
Handlers[i].tripped = 0; |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
void |
| 994 |
PyOS_AfterFork(void) |
| 995 |
{ |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
_clear_pending_signals(); |
| 1000 |
#ifdef WITH_THREAD |
| 1001 |
|
| 1002 |
|
| 1003 |
PyThread_ReInitTLS(); |
| 1004 |
PyEval_ReInitThreads(); |
| 1005 |
main_thread = PyThread_get_thread_ident(); |
| 1006 |
main_pid = getpid(); |
| 1007 |
_PyImport_ReInitLock(); |
| 1008 |
#endif |
| 1009 |
} |