scmsigs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2004 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <signal.h>
  42. #include <errno.h>
  43. #include "libguile/_scm.h"
  44. #include "libguile/async.h"
  45. #include "libguile/eval.h"
  46. #include "libguile/root.h"
  47. #include "libguile/vectors.h"
  48. #include "libguile/validate.h"
  49. #include "libguile/scmsigs.h"
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53. #ifdef HAVE_SYS_TIME_H
  54. #include <sys/time.h>
  55. #endif
  56. /* The thread system has its own sleep and usleep functions. */
  57. #ifndef USE_THREADS
  58. #if defined(MISSING_SLEEP_DECL)
  59. int sleep ();
  60. #endif
  61. #if defined(HAVE_USLEEP) && defined(MISSING_USLEEP_DECL)
  62. int usleep ();
  63. #endif
  64. #endif
  65. #ifdef __MINGW32__
  66. #include <windows.h>
  67. #define alarm(sec) (0)
  68. /* This weird comma expression is because Sleep is void under Windows. */
  69. #define sleep(sec) (Sleep ((sec) * 1000), 0)
  70. #define kill(pid, sig) raise (sig)
  71. #endif
  72. /* SIGRETTYPE is the type that signal handlers return. See <signal.h> */
  73. #ifdef RETSIGTYPE
  74. # define SIGRETTYPE RETSIGTYPE
  75. #else
  76. # ifdef STDC_HEADERS
  77. # define SIGRETTYPE void
  78. # else
  79. # define SIGRETTYPE int
  80. # endif
  81. #endif
  82. /* take_signal is installed as the C signal handler whenever a Scheme
  83. handler is set. when a signal arrives, take_signal marks the corresponding
  84. element of got_signal and marks signal_async. the thunk in signal_async
  85. (sys_deliver_signals) will be run at the next opportunity, outside a
  86. critical section. sys_deliver_signals runs each Scheme handler for
  87. which got_signal is set. */
  88. static SCM signal_async;
  89. static char got_signal[NSIG];
  90. /* a Scheme vector of handler procedures. */
  91. static SCM *signal_handlers;
  92. /* saves the original C handlers, when a new handler is installed.
  93. set to SIG_ERR if the original handler is installed. */
  94. #ifdef HAVE_SIGACTION
  95. static struct sigaction orig_handlers[NSIG];
  96. #else
  97. static SIGRETTYPE (*orig_handlers[NSIG])(int);
  98. #endif
  99. static SIGRETTYPE
  100. take_signal (int signum)
  101. {
  102. int saved_errno = errno;
  103. SCM ignored;
  104. if (!scm_ints_disabled)
  105. {
  106. /* For reasons of speed, the SCM_NEWCELL macro doesn't defer
  107. interrupts. Instead, it first sets its argument to point to
  108. the first cell in the list, and then advances the freelist
  109. pointer to the next cell. Now, if this procedure is
  110. interrupted, the only anomalous state possible is to have
  111. both SCM_NEWCELL's argument and scm_freelist pointing to the
  112. same cell. To deal with this case, we always throw away the
  113. first cell in scm_freelist here.
  114. At least, that's the theory. I'm not convinced that that's
  115. the only anomalous path we need to worry about. */
  116. SCM_NEWCELL (ignored);
  117. }
  118. got_signal[signum] = 1;
  119. #if HAVE_SIGACTION
  120. /* unblock the signal before the scheme handler gets to run, since
  121. it may use longjmp to escape (i.e., throw an exception). */
  122. {
  123. sigset_t set;
  124. sigemptyset (&set);
  125. sigaddset (&set, signum);
  126. sigprocmask (SIG_UNBLOCK, &set, NULL);
  127. }
  128. #endif
  129. scm_system_async_mark (signal_async);
  130. errno = saved_errno;
  131. }
  132. static SCM
  133. sys_deliver_signals (void)
  134. {
  135. int i;
  136. for (i = 0; i < NSIG; i++)
  137. {
  138. if (got_signal[i])
  139. {
  140. /* The flag is reset before calling the handler in case the
  141. handler doesn't return. If the handler doesn't return
  142. but leaves other signals flagged, they their handlers
  143. will be applied some time later when the async is checked
  144. again. It would probably be better to reset the flags
  145. after doing a longjmp. */
  146. got_signal[i] = 0;
  147. #ifndef HAVE_SIGACTION
  148. signal (i, take_signal);
  149. #endif
  150. scm_call_1 (SCM_VELTS (*signal_handlers)[i], SCM_MAKINUM (i));
  151. }
  152. }
  153. return SCM_UNSPECIFIED;
  154. }
  155. /* user interface for installation of signal handlers. */
  156. SCM_DEFINE (scm_sigaction, "sigaction", 1, 2, 0,
  157. (SCM signum, SCM handler, SCM flags),
  158. "Install or report the signal handler for a specified signal.\n\n"
  159. "@var{signum} is the signal number, which can be specified using the value\n"
  160. "of variables such as @code{SIGINT}.\n\n"
  161. "If @var{action} is omitted, @code{sigaction} returns a pair: the\n"
  162. "CAR is the current\n"
  163. "signal hander, which will be either an integer with the value @code{SIG_DFL}\n"
  164. "(default action) or @code{SIG_IGN} (ignore), or the Scheme procedure which\n"
  165. "handles the signal, or @code{#f} if a non-Scheme procedure handles the\n"
  166. "signal. The CDR contains the current @code{sigaction} flags for the handler.\n\n"
  167. "If @var{action} is provided, it is installed as the new handler for\n"
  168. "@var{signum}. @var{action} can be a Scheme procedure taking one\n"
  169. "argument, or the value of @code{SIG_DFL} (default action) or\n"
  170. "@code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler\n"
  171. "was installed before @code{sigaction} was first used. Flags can\n"
  172. "optionally be specified for the new handler (@code{SA_RESTART} will\n"
  173. "always be added if it's available and the system is using restartable\n"
  174. "system calls.) The return value is a pair with information about the\n"
  175. "old handler as described above.\n\n"
  176. "This interface does not provide access to the \"signal blocking\"\n"
  177. "facility. Maybe this is not needed, since the thread support may\n"
  178. "provide solutions to the problem of consistent access to data\n"
  179. "structures.")
  180. #define FUNC_NAME s_scm_sigaction
  181. {
  182. int csig;
  183. #ifdef HAVE_SIGACTION
  184. struct sigaction action;
  185. struct sigaction old_action;
  186. #else
  187. SIGRETTYPE (* chandler) (int);
  188. SIGRETTYPE (* old_chandler) (int);
  189. #endif
  190. int query_only = 0;
  191. int save_handler = 0;
  192. SCM *scheme_handlers = SCM_VELTS (*signal_handlers);
  193. SCM old_handler;
  194. SCM_VALIDATE_INUM_COPY (1,signum,csig);
  195. if (csig < 0 || csig >= NSIG)
  196. SCM_OUT_OF_RANGE (1, signum);
  197. #if defined(HAVE_SIGACTION)
  198. #if defined(SA_RESTART) && defined(HAVE_RESTARTABLE_SYSCALLS)
  199. /* don't allow SA_RESTART to be omitted if HAVE_RESTARTABLE_SYSCALLS
  200. is defined, since libguile would be likely to produce spurious
  201. EINTR errors. */
  202. action.sa_flags = SA_RESTART;
  203. #else
  204. action.sa_flags = 0;
  205. #endif
  206. if (!SCM_UNBNDP (flags))
  207. {
  208. SCM_VALIDATE_INUM (3,flags);
  209. action.sa_flags |= SCM_INUM (flags);
  210. }
  211. sigemptyset (&action.sa_mask);
  212. #endif
  213. SCM_DEFER_INTS;
  214. old_handler = scheme_handlers[csig];
  215. if (SCM_UNBNDP (handler))
  216. query_only = 1;
  217. else if (SCM_EQ_P (scm_integer_p (handler), SCM_BOOL_T))
  218. {
  219. if (SCM_NUM2LONG (2, handler) == (long) SIG_DFL
  220. || SCM_NUM2LONG (2, handler) == (long) SIG_IGN)
  221. {
  222. #ifdef HAVE_SIGACTION
  223. action.sa_handler = (SIGRETTYPE (*) (int)) SCM_INUM (handler);
  224. #else
  225. chandler = (SIGRETTYPE (*) (int)) SCM_INUM (handler);
  226. #endif
  227. scheme_handlers[csig] = SCM_BOOL_F;
  228. }
  229. else
  230. SCM_OUT_OF_RANGE (2, handler);
  231. }
  232. else if (SCM_FALSEP (handler))
  233. {
  234. /* restore the default handler. */
  235. #ifdef HAVE_SIGACTION
  236. if (orig_handlers[csig].sa_handler == SIG_ERR)
  237. query_only = 1;
  238. else
  239. {
  240. action = orig_handlers[csig];
  241. orig_handlers[csig].sa_handler = SIG_ERR;
  242. scheme_handlers[csig] = SCM_BOOL_F;
  243. }
  244. #else
  245. if (orig_handlers[csig] == SIG_ERR)
  246. query_only = 1;
  247. else
  248. {
  249. chandler = orig_handlers[csig];
  250. orig_handlers[csig] = SIG_ERR;
  251. scheme_handlers[csig] = SCM_BOOL_F;
  252. }
  253. #endif
  254. }
  255. else
  256. {
  257. SCM_VALIDATE_PROC (2,handler);
  258. #ifdef HAVE_SIGACTION
  259. action.sa_handler = take_signal;
  260. if (orig_handlers[csig].sa_handler == SIG_ERR)
  261. save_handler = 1;
  262. #else
  263. chandler = take_signal;
  264. if (orig_handlers[csig] == SIG_ERR)
  265. save_handler = 1;
  266. #endif
  267. scheme_handlers[csig] = handler;
  268. }
  269. /* XXX - Silently ignore setting handlers for `program error signals'
  270. because they can't currently be handled by Scheme code.
  271. */
  272. switch (csig)
  273. {
  274. /* This list of program error signals is from the GNU Libc
  275. Reference Manual */
  276. case SIGFPE:
  277. case SIGILL:
  278. case SIGSEGV:
  279. #ifdef SIGBUS
  280. case SIGBUS:
  281. #endif
  282. case SIGABRT:
  283. #if defined(SIGIOT) && (SIGIOT != SIGABRT)
  284. case SIGIOT:
  285. #endif
  286. #ifdef SIGTRAP
  287. case SIGTRAP:
  288. #endif
  289. #ifdef SIGEMT
  290. case SIGEMT:
  291. #endif
  292. #ifdef SIGSYS
  293. case SIGSYS:
  294. #endif
  295. query_only = 1;
  296. }
  297. #ifdef HAVE_SIGACTION
  298. if (query_only)
  299. {
  300. if (sigaction (csig, 0, &old_action) == -1)
  301. SCM_SYSERROR;
  302. }
  303. else
  304. {
  305. if (sigaction (csig, &action , &old_action) == -1)
  306. SCM_SYSERROR;
  307. if (save_handler)
  308. orig_handlers[csig] = old_action;
  309. }
  310. if (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN)
  311. old_handler = scm_long2num ((long) old_action.sa_handler);
  312. SCM_ALLOW_INTS;
  313. return scm_cons (old_handler, SCM_MAKINUM (old_action.sa_flags));
  314. #else
  315. if (query_only)
  316. {
  317. if ((old_chandler = signal (csig, SIG_IGN)) == SIG_ERR)
  318. SCM_SYSERROR;
  319. if (signal (csig, old_chandler) == SIG_ERR)
  320. SCM_SYSERROR;
  321. }
  322. else
  323. {
  324. if ((old_chandler = signal (csig, chandler)) == SIG_ERR)
  325. SCM_SYSERROR;
  326. if (save_handler)
  327. orig_handlers[csig] = old_chandler;
  328. }
  329. if (old_chandler == SIG_DFL || old_chandler == SIG_IGN)
  330. old_handler = scm_long2num ((long) old_chandler);
  331. SCM_ALLOW_INTS;
  332. return scm_cons (old_handler, SCM_MAKINUM (0));
  333. #endif
  334. }
  335. #undef FUNC_NAME
  336. SCM_DEFINE (scm_restore_signals, "restore-signals", 0, 0, 0,
  337. (void),
  338. "Return all signal handlers to the values they had before any call to\n"
  339. "@code{sigaction} was made. The return value is unspecified.")
  340. #define FUNC_NAME s_scm_restore_signals
  341. {
  342. int i;
  343. SCM *scheme_handlers = SCM_VELTS (*signal_handlers);
  344. for (i = 0; i < NSIG; i++)
  345. {
  346. #ifdef HAVE_SIGACTION
  347. if (orig_handlers[i].sa_handler != SIG_ERR)
  348. {
  349. if (sigaction (i, &orig_handlers[i], NULL) == -1)
  350. SCM_SYSERROR;
  351. orig_handlers[i].sa_handler = SIG_ERR;
  352. scheme_handlers[i] = SCM_BOOL_F;
  353. }
  354. #else
  355. if (orig_handlers[i] != SIG_ERR)
  356. {
  357. if (signal (i, orig_handlers[i]) == SIG_ERR)
  358. SCM_SYSERROR;
  359. orig_handlers[i] = SIG_ERR;
  360. scheme_handlers[i] = SCM_BOOL_F;
  361. }
  362. #endif
  363. }
  364. return SCM_UNSPECIFIED;
  365. }
  366. #undef FUNC_NAME
  367. SCM_DEFINE (scm_alarm, "alarm", 1, 0, 0,
  368. (SCM i),
  369. "Set a timer to raise a @code{SIGALRM} signal after the specified\n"
  370. "number of seconds (an integer). It's advisable to install a signal\n"
  371. "handler for\n"
  372. "@code{SIGALRM} beforehand, since the default action is to terminate\n"
  373. "the process.\n\n"
  374. "The return value indicates the time remaining for the previous alarm,\n"
  375. "if any. The new value replaces the previous alarm. If there was\n"
  376. "no previous alarm, the return value is zero.")
  377. #define FUNC_NAME s_scm_alarm
  378. {
  379. unsigned int j;
  380. SCM_VALIDATE_INUM (1,i);
  381. j = alarm (SCM_INUM (i));
  382. return SCM_MAKINUM (j);
  383. }
  384. #undef FUNC_NAME
  385. #ifdef HAVE_SETITIMER
  386. SCM_DEFINE (scm_setitimer, "setitimer", 5, 0, 0,
  387. (SCM which_timer,
  388. SCM interval_seconds, SCM interval_microseconds,
  389. SCM value_seconds, SCM value_microseconds),
  390. "Set the timer specified by @var{which_timer} according to the given\n"
  391. "@var{interval_seconds}, @var{interval_microseconds},\n"
  392. "@var{value_seconds}, and @var{value_microseconds} values.\n"
  393. "\n"
  394. "Return information about the timer's previous setting."
  395. "\n"
  396. "Errors are handled as described in the guile info pages under ``POSIX\n"
  397. "Interface Conventions''.\n"
  398. "\n"
  399. "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
  400. "and @code{ITIMER_PROF}.\n"
  401. "\n"
  402. "The return value will be a list of two cons pairs representing the\n"
  403. "current state of the given timer. The first pair is the seconds and\n"
  404. "microseconds of the timer @code{it_interval}, and the second pair is\n"
  405. "the seconds and microseconds of the timer @code{it_value}.")
  406. #define FUNC_NAME s_scm_setitimer
  407. {
  408. int rv;
  409. int c_which_timer;
  410. struct itimerval new_timer;
  411. struct itimerval old_timer;
  412. c_which_timer = SCM_NUM2INT(1, which_timer);
  413. new_timer.it_interval.tv_sec = SCM_NUM2LONG(2, interval_seconds);
  414. new_timer.it_interval.tv_usec = SCM_NUM2LONG(3, interval_microseconds);
  415. new_timer.it_value.tv_sec = SCM_NUM2LONG(4, value_seconds);
  416. new_timer.it_value.tv_usec = SCM_NUM2LONG(5, value_microseconds);
  417. SCM_SYSCALL(rv = setitimer(c_which_timer, &new_timer, &old_timer));
  418. if(rv != 0)
  419. SCM_SYSERROR;
  420. return scm_list_2(scm_cons(scm_long2num(old_timer.it_interval.tv_sec),
  421. scm_long2num(old_timer.it_interval.tv_usec)),
  422. scm_cons(scm_long2num(old_timer.it_value.tv_sec),
  423. scm_long2num(old_timer.it_value.tv_usec)));
  424. }
  425. #undef FUNC_NAME
  426. #endif /* HAVE_SETITIMER */
  427. #ifdef HAVE_GETITIMER
  428. SCM_DEFINE (scm_getitimer, "getitimer", 1, 0, 0,
  429. (SCM which_timer),
  430. "Return information about the timer specified by @var{which_timer}"
  431. "\n"
  432. "Errors are handled as described in the guile info pages under ``POSIX\n"
  433. "Interface Conventions''.\n"
  434. "\n"
  435. "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
  436. "and @code{ITIMER_PROF}.\n"
  437. "\n"
  438. "The return value will be a list of two cons pairs representing the\n"
  439. "current state of the given timer. The first pair is the seconds and\n"
  440. "microseconds of the timer @code{it_interval}, and the second pair is\n"
  441. "the seconds and microseconds of the timer @code{it_value}.")
  442. #define FUNC_NAME s_scm_getitimer
  443. {
  444. int rv;
  445. int c_which_timer;
  446. struct itimerval old_timer;
  447. c_which_timer = SCM_NUM2INT(1, which_timer);
  448. SCM_SYSCALL(rv = getitimer(c_which_timer, &old_timer));
  449. if(rv != 0)
  450. SCM_SYSERROR;
  451. return scm_list_2(scm_cons(scm_long2num(old_timer.it_interval.tv_sec),
  452. scm_long2num(old_timer.it_interval.tv_usec)),
  453. scm_cons(scm_long2num(old_timer.it_value.tv_sec),
  454. scm_long2num(old_timer.it_value.tv_usec)));
  455. }
  456. #undef FUNC_NAME
  457. #endif /* HAVE_GETITIMER */
  458. #ifdef HAVE_PAUSE
  459. SCM_DEFINE (scm_pause, "pause", 0, 0, 0,
  460. (),
  461. "Pause the current process (thread?) until a signal arrives whose\n"
  462. "action is to either terminate the current process or invoke a\n"
  463. "handler procedure. The return value is unspecified.")
  464. #define FUNC_NAME s_scm_pause
  465. {
  466. pause ();
  467. return SCM_UNSPECIFIED;
  468. }
  469. #undef FUNC_NAME
  470. #endif
  471. SCM_DEFINE (scm_sleep, "sleep", 1, 0, 0,
  472. (SCM i),
  473. "Wait for the given number of seconds (an integer) or until a signal\n"
  474. "arrives. The return value is zero if the time elapses or the number\n"
  475. "of seconds remaining otherwise.")
  476. #define FUNC_NAME s_scm_sleep
  477. {
  478. unsigned long j;
  479. SCM_VALIDATE_INUM_MIN (1,i,0);
  480. #ifdef USE_THREADS
  481. j = scm_thread_sleep (SCM_INUM(i));
  482. #else
  483. j = sleep (SCM_INUM(i));
  484. #endif
  485. return scm_ulong2num (j);
  486. }
  487. #undef FUNC_NAME
  488. #if defined(USE_THREADS) || defined(HAVE_USLEEP)
  489. SCM_DEFINE (scm_usleep, "usleep", 1, 0, 0,
  490. (SCM i),
  491. "Sleep for I microseconds. @code{usleep} is not available on\n"
  492. "all platforms.")
  493. #define FUNC_NAME s_scm_usleep
  494. {
  495. SCM_VALIDATE_INUM_MIN (1,i,0);
  496. #ifdef USE_THREADS
  497. /* If we have threads, we use the thread system's sleep function. */
  498. {
  499. unsigned long j = scm_thread_usleep (SCM_INUM (i));
  500. return scm_ulong2num (j);
  501. }
  502. #else
  503. #ifdef USLEEP_RETURNS_VOID
  504. usleep (SCM_INUM (i));
  505. return SCM_INUM0;
  506. #else
  507. {
  508. int j = usleep (SCM_INUM (i));
  509. return SCM_MAKINUM (j);
  510. }
  511. #endif
  512. #endif
  513. }
  514. #undef FUNC_NAME
  515. #endif /* GUILE_ISELECT || HAVE_USLEEP */
  516. SCM_DEFINE (scm_raise, "raise", 1, 0, 0,
  517. (SCM sig),
  518. "Sends a specified signal @var{sig} to the current process, where\n"
  519. "@var{sig} is as described for the kill procedure.")
  520. #define FUNC_NAME s_scm_raise
  521. {
  522. SCM_VALIDATE_INUM (1,sig);
  523. SCM_DEFER_INTS;
  524. if (kill (getpid (), (int) SCM_INUM (sig)) != 0)
  525. SCM_SYSERROR;
  526. SCM_ALLOW_INTS;
  527. return SCM_UNSPECIFIED;
  528. }
  529. #undef FUNC_NAME
  530. void
  531. scm_init_scmsigs ()
  532. {
  533. SCM thunk;
  534. int i;
  535. signal_handlers =
  536. SCM_VARIABLE_LOC (scm_c_define ("signal-handlers",
  537. scm_c_make_vector (NSIG, SCM_BOOL_F)));
  538. /* XXX - use scm_c_make_gsubr here instead of `define'? */
  539. thunk = scm_c_define_gsubr ("%deliver-signals", 0, 0, 0,
  540. sys_deliver_signals);
  541. signal_async = scm_system_async (thunk);
  542. for (i = 0; i < NSIG; i++)
  543. {
  544. got_signal[i] = 0;
  545. #ifdef HAVE_SIGACTION
  546. orig_handlers[i].sa_handler = SIG_ERR;
  547. #else
  548. orig_handlers[i] = SIG_ERR;
  549. #endif
  550. #ifdef HAVE_RESTARTABLE_SYSCALLS
  551. /* If HAVE_RESTARTABLE_SYSCALLS is defined, it's important that
  552. signals really are restartable. don't rely on the same
  553. run-time that configure got: reset the default for every signal.
  554. */
  555. #ifdef HAVE_SIGINTERRUPT
  556. siginterrupt (i, 0);
  557. #elif defined(SA_RESTART)
  558. {
  559. struct sigaction action;
  560. sigaction (i, NULL, &action);
  561. if (!(action.sa_flags & SA_RESTART))
  562. {
  563. action.sa_flags |= SA_RESTART;
  564. sigaction (i, &action, NULL);
  565. }
  566. }
  567. #endif
  568. /* if neither siginterrupt nor SA_RESTART are available we may
  569. as well assume that signals are always restartable. */
  570. #endif
  571. }
  572. scm_c_define ("NSIG", scm_long2num (NSIG));
  573. scm_c_define ("SIG_IGN", scm_long2num ((long) SIG_IGN));
  574. scm_c_define ("SIG_DFL", scm_long2num ((long) SIG_DFL));
  575. #ifdef SA_NOCLDSTOP
  576. scm_c_define ("SA_NOCLDSTOP", scm_long2num (SA_NOCLDSTOP));
  577. #endif
  578. #ifdef SA_RESTART
  579. scm_c_define ("SA_RESTART", scm_long2num (SA_RESTART));
  580. #endif
  581. #if defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER)
  582. /* Stuff needed by setitimer and getitimer. */
  583. scm_c_define ("ITIMER_REAL", SCM_MAKINUM (ITIMER_REAL));
  584. scm_c_define ("ITIMER_VIRTUAL", SCM_MAKINUM (ITIMER_VIRTUAL));
  585. scm_c_define ("ITIMER_PROF", SCM_MAKINUM (ITIMER_PROF));
  586. #endif /* defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER) */
  587. #include "libguile/scmsigs.x"
  588. }
  589. /*
  590. Local Variables:
  591. c-file-style: "gnu"
  592. End:
  593. */