scmsigs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2004, 2006, 2007 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <fcntl.h> /* for mingw */
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/async.h"
  26. #include "libguile/eval.h"
  27. #include "libguile/root.h"
  28. #include "libguile/vectors.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/scmsigs.h"
  31. #ifdef HAVE_IO_H
  32. #include <io.h> /* for mingw _pipe() */
  33. #endif
  34. #ifdef HAVE_PROCESS_H
  35. #include <process.h> /* for mingw */
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifdef HAVE_SYS_TIME_H
  41. #include <sys/time.h>
  42. #endif
  43. #ifdef __MINGW32__
  44. #include <windows.h>
  45. #define alarm(sec) (0)
  46. /* This weird comma expression is because Sleep is void under Windows. */
  47. #define sleep(sec) (Sleep ((sec) * 1000), 0)
  48. #define usleep(usec) (Sleep ((usec) / 1000), 0)
  49. #define pipe(fd) _pipe (fd, 256, O_BINARY)
  50. #endif
  51. /* SIGRETTYPE is the type that signal handlers return. See <signal.h> */
  52. #ifdef RETSIGTYPE
  53. # define SIGRETTYPE RETSIGTYPE
  54. #else
  55. # ifdef STDC_HEADERS
  56. # define SIGRETTYPE void
  57. # else
  58. # define SIGRETTYPE int
  59. # endif
  60. #endif
  61. /* take_signal is installed as the C signal handler whenever a Scheme
  62. handler is set. When a signal arrives, take_signal will write a
  63. byte into the 'signal pipe'. The 'signal delivery thread' will
  64. read this pipe and queue the appropriate asyncs.
  65. When Guile is built without threads, the signal handler will
  66. install the async directly.
  67. */
  68. /* Scheme vectors with information about a signal. signal_handlers
  69. contains the handler procedure or #f, signal_handler_asyncs
  70. contains the thunk to be marked as an async when the signal arrives
  71. (or the cell with the thunk in a singlethreaded Guile), and
  72. signal_handler_threads points to the thread that a signal should be
  73. delivered to.
  74. */
  75. static SCM *signal_handlers;
  76. static SCM signal_handler_asyncs;
  77. static SCM signal_handler_threads;
  78. /* saves the original C handlers, when a new handler is installed.
  79. set to SIG_ERR if the original handler is installed. */
  80. #ifdef HAVE_SIGACTION
  81. static struct sigaction orig_handlers[NSIG];
  82. #else
  83. static SIGRETTYPE (*orig_handlers[NSIG])(int);
  84. #endif
  85. static SCM
  86. handler_to_async (SCM handler, int signum)
  87. {
  88. if (scm_is_false (handler))
  89. return SCM_BOOL_F;
  90. else
  91. {
  92. SCM async = scm_primitive_eval_x (scm_list_3 (scm_sym_lambda, SCM_EOL,
  93. scm_list_2 (handler,
  94. scm_from_int (signum))));
  95. #if !SCM_USE_PTHREAD_THREADS
  96. async = scm_cons (async, SCM_BOOL_F);
  97. #endif
  98. return async;
  99. }
  100. }
  101. #if SCM_USE_PTHREAD_THREADS
  102. /* On mingw there's no notion of inter-process signals, only a raise()
  103. within the process itself which apparently invokes the registered handler
  104. immediately. Not sure how well the following code will cope in this
  105. case. It builds but it may not offer quite the same scheme-level
  106. semantics as on a proper system. If you're relying on much in the way of
  107. signal handling on mingw you probably lose anyway. */
  108. static int signal_pipe[2];
  109. static SIGRETTYPE
  110. take_signal (int signum)
  111. {
  112. size_t count;
  113. char sigbyte = signum;
  114. count = write (signal_pipe[1], &sigbyte, 1);
  115. #ifndef HAVE_SIGACTION
  116. signal (signum, take_signal);
  117. #endif
  118. }
  119. typedef struct {
  120. ssize_t res;
  121. int fd;
  122. char *buf;
  123. size_t n;
  124. } read_without_guile_data;
  125. static void *
  126. do_read_without_guile (void *raw_data)
  127. {
  128. read_without_guile_data *data = (read_without_guile_data *)raw_data;
  129. data->res = read (data->fd, data->buf, data->n);
  130. return NULL;
  131. }
  132. static ssize_t
  133. read_without_guile (int fd, char *buf, size_t n)
  134. {
  135. read_without_guile_data data;
  136. data.fd = fd;
  137. data.buf = buf;
  138. data.n = n;
  139. scm_without_guile (do_read_without_guile, &data);
  140. return data.res;
  141. }
  142. static SCM
  143. signal_delivery_thread (void *data)
  144. {
  145. int n, sig;
  146. char sigbyte;
  147. #if HAVE_PTHREAD_SIGMASK /* not on mingw, see notes above */
  148. sigset_t all_sigs;
  149. sigfillset (&all_sigs);
  150. scm_i_pthread_sigmask (SIG_SETMASK, &all_sigs, NULL);
  151. #endif
  152. while (1)
  153. {
  154. n = read_without_guile (signal_pipe[0], &sigbyte, 1);
  155. sig = sigbyte;
  156. if (n == 1 && sig >= 0 && sig < NSIG)
  157. {
  158. SCM h, t;
  159. h = SCM_SIMPLE_VECTOR_REF (signal_handler_asyncs, sig);
  160. t = SCM_SIMPLE_VECTOR_REF (signal_handler_threads, sig);
  161. if (scm_is_true (h))
  162. scm_system_async_mark_for_thread (h, t);
  163. }
  164. else if (n < 0 && errno != EINTR)
  165. perror ("error in signal delivery thread");
  166. }
  167. return SCM_UNSPECIFIED; /* not reached */
  168. }
  169. static void
  170. start_signal_delivery_thread (void)
  171. {
  172. if (pipe (signal_pipe) != 0)
  173. scm_syserror (NULL);
  174. scm_spawn_thread (signal_delivery_thread, NULL,
  175. scm_handle_by_message, "signal delivery thread");
  176. }
  177. static void
  178. ensure_signal_delivery_thread ()
  179. {
  180. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  181. scm_i_pthread_once (&once, start_signal_delivery_thread);
  182. }
  183. #else /* !SCM_USE_PTHREAD_THREADS */
  184. static SIGRETTYPE
  185. take_signal (int signum)
  186. {
  187. SCM cell = SCM_SIMPLE_VECTOR_REF (signal_handler_asyncs, signum);
  188. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  189. if (scm_is_false (SCM_CDR (cell)))
  190. {
  191. SCM_SETCDR (cell, t->active_asyncs);
  192. t->active_asyncs = cell;
  193. t->pending_asyncs = 1;
  194. }
  195. #ifndef HAVE_SIGACTION
  196. signal (signum, take_signal);
  197. #endif
  198. }
  199. static void
  200. ensure_signal_delivery_thread ()
  201. {
  202. return;
  203. }
  204. #endif /* !SCM_USE_PTHREAD_THREADS */
  205. static void
  206. install_handler (int signum, SCM thread, SCM handler, SCM async)
  207. {
  208. SCM_SIMPLE_VECTOR_SET (*signal_handlers, signum, handler);
  209. SCM_SIMPLE_VECTOR_SET (signal_handler_asyncs, signum, async);
  210. SCM_SIMPLE_VECTOR_SET (signal_handler_threads, signum, thread);
  211. }
  212. SCM
  213. scm_sigaction (SCM signum, SCM handler, SCM flags)
  214. {
  215. return scm_sigaction_for_thread (signum, handler, flags, SCM_UNDEFINED);
  216. }
  217. /* user interface for installation of signal handlers. */
  218. SCM_DEFINE (scm_sigaction_for_thread, "sigaction", 1, 3, 0,
  219. (SCM signum, SCM handler, SCM flags, SCM thread),
  220. "Install or report the signal handler for a specified signal.\n\n"
  221. "@var{signum} is the signal number, which can be specified using the value\n"
  222. "of variables such as @code{SIGINT}.\n\n"
  223. "If @var{handler} is omitted, @code{sigaction} returns a pair: the\n"
  224. "CAR is the current\n"
  225. "signal hander, which will be either an integer with the value @code{SIG_DFL}\n"
  226. "(default action) or @code{SIG_IGN} (ignore), or the Scheme procedure which\n"
  227. "handles the signal, or @code{#f} if a non-Scheme procedure handles the\n"
  228. "signal. The CDR contains the current @code{sigaction} flags for the handler.\n\n"
  229. "If @var{handler} is provided, it is installed as the new handler for\n"
  230. "@var{signum}. @var{handler} can be a Scheme procedure taking one\n"
  231. "argument, or the value of @code{SIG_DFL} (default action) or\n"
  232. "@code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler\n"
  233. "was installed before @code{sigaction} was first used. When\n"
  234. "a scheme procedure has been specified, that procedure will run\n"
  235. "in the given @var{thread}. When no thread has been given, the\n"
  236. "thread that made this call to @code{sigaction} is used.\n"
  237. "Flags can optionally be specified for the new handler.\n"
  238. "The return value is a pair with information about the\n"
  239. "old handler as described above.\n\n"
  240. "This interface does not provide access to the \"signal blocking\"\n"
  241. "facility. Maybe this is not needed, since the thread support may\n"
  242. "provide solutions to the problem of consistent access to data\n"
  243. "structures.")
  244. #define FUNC_NAME s_scm_sigaction_for_thread
  245. {
  246. int csig;
  247. #ifdef HAVE_SIGACTION
  248. struct sigaction action;
  249. struct sigaction old_action;
  250. #else
  251. SIGRETTYPE (* chandler) (int) = SIG_DFL;
  252. SIGRETTYPE (* old_chandler) (int);
  253. #endif
  254. int query_only = 0;
  255. int save_handler = 0;
  256. SCM old_handler;
  257. SCM async;
  258. csig = scm_to_signed_integer (signum, 0, NSIG-1);
  259. #if defined(HAVE_SIGACTION)
  260. action.sa_flags = 0;
  261. if (!SCM_UNBNDP (flags))
  262. action.sa_flags |= scm_to_int (flags);
  263. sigemptyset (&action.sa_mask);
  264. #endif
  265. if (SCM_UNBNDP (thread))
  266. thread = scm_current_thread ();
  267. else
  268. {
  269. SCM_VALIDATE_THREAD (4, thread);
  270. if (scm_c_thread_exited_p (thread))
  271. SCM_MISC_ERROR ("thread has already exited", SCM_EOL);
  272. }
  273. /* Allocate upfront, as we can't do it inside the critical
  274. section. */
  275. async = handler_to_async (handler, csig);
  276. ensure_signal_delivery_thread ();
  277. SCM_CRITICAL_SECTION_START;
  278. old_handler = SCM_SIMPLE_VECTOR_REF (*signal_handlers, csig);
  279. if (SCM_UNBNDP (handler))
  280. query_only = 1;
  281. else if (scm_is_integer (handler))
  282. {
  283. long handler_int = scm_to_long (handler);
  284. if (handler_int == (long) SIG_DFL || handler_int == (long) SIG_IGN)
  285. {
  286. #ifdef HAVE_SIGACTION
  287. action.sa_handler = (SIGRETTYPE (*) (int)) handler_int;
  288. #else
  289. chandler = (SIGRETTYPE (*) (int)) handler_int;
  290. #endif
  291. install_handler (csig, SCM_BOOL_F, SCM_BOOL_F, async);
  292. }
  293. else
  294. {
  295. SCM_CRITICAL_SECTION_END;
  296. SCM_OUT_OF_RANGE (2, handler);
  297. }
  298. }
  299. else if (scm_is_false (handler))
  300. {
  301. /* restore the default handler. */
  302. #ifdef HAVE_SIGACTION
  303. if (orig_handlers[csig].sa_handler == SIG_ERR)
  304. query_only = 1;
  305. else
  306. {
  307. action = orig_handlers[csig];
  308. orig_handlers[csig].sa_handler = SIG_ERR;
  309. install_handler (csig, SCM_BOOL_F, SCM_BOOL_F, async);
  310. }
  311. #else
  312. if (orig_handlers[csig] == SIG_ERR)
  313. query_only = 1;
  314. else
  315. {
  316. chandler = orig_handlers[csig];
  317. orig_handlers[csig] = SIG_ERR;
  318. install_handler (csig, SCM_BOOL_F, SCM_BOOL_F, async);
  319. }
  320. #endif
  321. }
  322. else
  323. {
  324. SCM_VALIDATE_PROC (2, handler);
  325. #ifdef HAVE_SIGACTION
  326. action.sa_handler = take_signal;
  327. if (orig_handlers[csig].sa_handler == SIG_ERR)
  328. save_handler = 1;
  329. #else
  330. chandler = take_signal;
  331. if (orig_handlers[csig] == SIG_ERR)
  332. save_handler = 1;
  333. #endif
  334. install_handler (csig, thread, handler, async);
  335. }
  336. /* XXX - Silently ignore setting handlers for `program error signals'
  337. because they can't currently be handled by Scheme code.
  338. */
  339. switch (csig)
  340. {
  341. /* This list of program error signals is from the GNU Libc
  342. Reference Manual */
  343. case SIGFPE:
  344. case SIGILL:
  345. case SIGSEGV:
  346. #ifdef SIGBUS
  347. case SIGBUS:
  348. #endif
  349. case SIGABRT:
  350. #if defined(SIGIOT) && (SIGIOT != SIGABRT)
  351. case SIGIOT:
  352. #endif
  353. #ifdef SIGTRAP
  354. case SIGTRAP:
  355. #endif
  356. #ifdef SIGEMT
  357. case SIGEMT:
  358. #endif
  359. #ifdef SIGSYS
  360. case SIGSYS:
  361. #endif
  362. query_only = 1;
  363. }
  364. #ifdef HAVE_SIGACTION
  365. if (query_only)
  366. {
  367. if (sigaction (csig, 0, &old_action) == -1)
  368. SCM_SYSERROR;
  369. }
  370. else
  371. {
  372. if (sigaction (csig, &action , &old_action) == -1)
  373. SCM_SYSERROR;
  374. if (save_handler)
  375. orig_handlers[csig] = old_action;
  376. }
  377. if (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN)
  378. old_handler = scm_from_long ((long) old_action.sa_handler);
  379. SCM_CRITICAL_SECTION_END;
  380. return scm_cons (old_handler, scm_from_int (old_action.sa_flags));
  381. #else
  382. if (query_only)
  383. {
  384. if ((old_chandler = signal (csig, SIG_IGN)) == SIG_ERR)
  385. SCM_SYSERROR;
  386. if (signal (csig, old_chandler) == SIG_ERR)
  387. SCM_SYSERROR;
  388. }
  389. else
  390. {
  391. if ((old_chandler = signal (csig, chandler)) == SIG_ERR)
  392. SCM_SYSERROR;
  393. if (save_handler)
  394. orig_handlers[csig] = old_chandler;
  395. }
  396. if (old_chandler == SIG_DFL || old_chandler == SIG_IGN)
  397. old_handler = scm_from_long ((long) old_chandler);
  398. SCM_CRITICAL_SECTION_END;
  399. return scm_cons (old_handler, scm_from_int (0));
  400. #endif
  401. }
  402. #undef FUNC_NAME
  403. SCM_DEFINE (scm_restore_signals, "restore-signals", 0, 0, 0,
  404. (void),
  405. "Return all signal handlers to the values they had before any call to\n"
  406. "@code{sigaction} was made. The return value is unspecified.")
  407. #define FUNC_NAME s_scm_restore_signals
  408. {
  409. int i;
  410. for (i = 0; i < NSIG; i++)
  411. {
  412. #ifdef HAVE_SIGACTION
  413. if (orig_handlers[i].sa_handler != SIG_ERR)
  414. {
  415. if (sigaction (i, &orig_handlers[i], NULL) == -1)
  416. SCM_SYSERROR;
  417. orig_handlers[i].sa_handler = SIG_ERR;
  418. SCM_SIMPLE_VECTOR_SET (*signal_handlers, i, SCM_BOOL_F);
  419. }
  420. #else
  421. if (orig_handlers[i] != SIG_ERR)
  422. {
  423. if (signal (i, orig_handlers[i]) == SIG_ERR)
  424. SCM_SYSERROR;
  425. orig_handlers[i] = SIG_ERR;
  426. SCM_SIMPLE_VECTOR_SET (*signal_handlers, i, SCM_BOOL_F);
  427. }
  428. #endif
  429. }
  430. return SCM_UNSPECIFIED;
  431. }
  432. #undef FUNC_NAME
  433. SCM_DEFINE (scm_alarm, "alarm", 1, 0, 0,
  434. (SCM i),
  435. "Set a timer to raise a @code{SIGALRM} signal after the specified\n"
  436. "number of seconds (an integer). It's advisable to install a signal\n"
  437. "handler for\n"
  438. "@code{SIGALRM} beforehand, since the default action is to terminate\n"
  439. "the process.\n\n"
  440. "The return value indicates the time remaining for the previous alarm,\n"
  441. "if any. The new value replaces the previous alarm. If there was\n"
  442. "no previous alarm, the return value is zero.")
  443. #define FUNC_NAME s_scm_alarm
  444. {
  445. return scm_from_uint (alarm (scm_to_uint (i)));
  446. }
  447. #undef FUNC_NAME
  448. #ifdef HAVE_SETITIMER
  449. SCM_DEFINE (scm_setitimer, "setitimer", 5, 0, 0,
  450. (SCM which_timer,
  451. SCM interval_seconds, SCM interval_microseconds,
  452. SCM value_seconds, SCM value_microseconds),
  453. "Set the timer specified by @var{which_timer} according to the given\n"
  454. "@var{interval_seconds}, @var{interval_microseconds},\n"
  455. "@var{value_seconds}, and @var{value_microseconds} values.\n"
  456. "\n"
  457. "Return information about the timer's previous setting."
  458. "\n"
  459. "Errors are handled as described in the guile info pages under ``POSIX\n"
  460. "Interface Conventions''.\n"
  461. "\n"
  462. "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
  463. "and @code{ITIMER_PROF}.\n"
  464. "\n"
  465. "The return value will be a list of two cons pairs representing the\n"
  466. "current state of the given timer. The first pair is the seconds and\n"
  467. "microseconds of the timer @code{it_interval}, and the second pair is\n"
  468. "the seconds and microseconds of the timer @code{it_value}.")
  469. #define FUNC_NAME s_scm_setitimer
  470. {
  471. int rv;
  472. int c_which_timer;
  473. struct itimerval new_timer;
  474. struct itimerval old_timer;
  475. c_which_timer = SCM_NUM2INT(1, which_timer);
  476. new_timer.it_interval.tv_sec = SCM_NUM2LONG(2, interval_seconds);
  477. new_timer.it_interval.tv_usec = SCM_NUM2LONG(3, interval_microseconds);
  478. new_timer.it_value.tv_sec = SCM_NUM2LONG(4, value_seconds);
  479. new_timer.it_value.tv_usec = SCM_NUM2LONG(5, value_microseconds);
  480. SCM_SYSCALL(rv = setitimer(c_which_timer, &new_timer, &old_timer));
  481. if(rv != 0)
  482. SCM_SYSERROR;
  483. return scm_list_2 (scm_cons (scm_from_long (old_timer.it_interval.tv_sec),
  484. scm_from_long (old_timer.it_interval.tv_usec)),
  485. scm_cons (scm_from_long (old_timer.it_value.tv_sec),
  486. scm_from_long (old_timer.it_value.tv_usec)));
  487. }
  488. #undef FUNC_NAME
  489. #endif /* HAVE_SETITIMER */
  490. #ifdef HAVE_GETITIMER
  491. SCM_DEFINE (scm_getitimer, "getitimer", 1, 0, 0,
  492. (SCM which_timer),
  493. "Return information about the timer specified by @var{which_timer}"
  494. "\n"
  495. "Errors are handled as described in the guile info pages under ``POSIX\n"
  496. "Interface Conventions''.\n"
  497. "\n"
  498. "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
  499. "and @code{ITIMER_PROF}.\n"
  500. "\n"
  501. "The return value will be a list of two cons pairs representing the\n"
  502. "current state of the given timer. The first pair is the seconds and\n"
  503. "microseconds of the timer @code{it_interval}, and the second pair is\n"
  504. "the seconds and microseconds of the timer @code{it_value}.")
  505. #define FUNC_NAME s_scm_getitimer
  506. {
  507. int rv;
  508. int c_which_timer;
  509. struct itimerval old_timer;
  510. c_which_timer = SCM_NUM2INT(1, which_timer);
  511. SCM_SYSCALL(rv = getitimer(c_which_timer, &old_timer));
  512. if(rv != 0)
  513. SCM_SYSERROR;
  514. return scm_list_2 (scm_cons (scm_from_long (old_timer.it_interval.tv_sec),
  515. scm_from_long (old_timer.it_interval.tv_usec)),
  516. scm_cons (scm_from_long (old_timer.it_value.tv_sec),
  517. scm_from_long (old_timer.it_value.tv_usec)));
  518. }
  519. #undef FUNC_NAME
  520. #endif /* HAVE_GETITIMER */
  521. #ifdef HAVE_PAUSE
  522. SCM_DEFINE (scm_pause, "pause", 0, 0, 0,
  523. (),
  524. "Pause the current process (thread?) until a signal arrives whose\n"
  525. "action is to either terminate the current process or invoke a\n"
  526. "handler procedure. The return value is unspecified.")
  527. #define FUNC_NAME s_scm_pause
  528. {
  529. pause ();
  530. return SCM_UNSPECIFIED;
  531. }
  532. #undef FUNC_NAME
  533. #endif
  534. SCM_DEFINE (scm_sleep, "sleep", 1, 0, 0,
  535. (SCM i),
  536. "Wait for the given number of seconds (an integer) or until a signal\n"
  537. "arrives. The return value is zero if the time elapses or the number\n"
  538. "of seconds remaining otherwise.\n"
  539. "\n"
  540. "See also @code{usleep}.")
  541. #define FUNC_NAME s_scm_sleep
  542. {
  543. return scm_from_uint (scm_std_sleep (scm_to_uint (i)));
  544. }
  545. #undef FUNC_NAME
  546. SCM_DEFINE (scm_usleep, "usleep", 1, 0, 0,
  547. (SCM i),
  548. "Wait the given period @var{usecs} microseconds (an integer).\n"
  549. "If a signal arrives the wait stops and the return value is the\n"
  550. "time remaining, in microseconds. If the period elapses with no\n"
  551. "signal the return is zero.\n"
  552. "\n"
  553. "On most systems the process scheduler is not microsecond accurate and\n"
  554. "the actual period slept by @code{usleep} may be rounded to a system\n"
  555. "clock tick boundary. Traditionally such ticks were 10 milliseconds\n"
  556. "apart, and that interval is often still used.\n"
  557. "\n"
  558. "See also @code{sleep}.")
  559. #define FUNC_NAME s_scm_usleep
  560. {
  561. return scm_from_ulong (scm_std_usleep (scm_to_ulong (i)));
  562. }
  563. #undef FUNC_NAME
  564. SCM_DEFINE (scm_raise, "raise", 1, 0, 0,
  565. (SCM sig),
  566. "Sends a specified signal @var{sig} to the current process, where\n"
  567. "@var{sig} is as described for the kill procedure.")
  568. #define FUNC_NAME s_scm_raise
  569. {
  570. if (raise (scm_to_int (sig)) != 0)
  571. SCM_SYSERROR;
  572. return SCM_UNSPECIFIED;
  573. }
  574. #undef FUNC_NAME
  575. void
  576. scm_init_scmsigs ()
  577. {
  578. int i;
  579. signal_handlers =
  580. SCM_VARIABLE_LOC (scm_c_define ("signal-handlers",
  581. scm_c_make_vector (NSIG, SCM_BOOL_F)));
  582. signal_handler_asyncs =
  583. scm_permanent_object (scm_c_make_vector (NSIG, SCM_BOOL_F));
  584. signal_handler_threads =
  585. scm_permanent_object (scm_c_make_vector (NSIG, SCM_BOOL_F));
  586. for (i = 0; i < NSIG; i++)
  587. {
  588. #ifdef HAVE_SIGACTION
  589. orig_handlers[i].sa_handler = SIG_ERR;
  590. #else
  591. orig_handlers[i] = SIG_ERR;
  592. #endif
  593. }
  594. scm_c_define ("NSIG", scm_from_long (NSIG));
  595. scm_c_define ("SIG_IGN", scm_from_long ((long) SIG_IGN));
  596. scm_c_define ("SIG_DFL", scm_from_long ((long) SIG_DFL));
  597. #ifdef SA_NOCLDSTOP
  598. scm_c_define ("SA_NOCLDSTOP", scm_from_long (SA_NOCLDSTOP));
  599. #endif
  600. #ifdef SA_RESTART
  601. scm_c_define ("SA_RESTART", scm_from_long (SA_RESTART));
  602. #endif
  603. #if defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER)
  604. /* Stuff needed by setitimer and getitimer. */
  605. scm_c_define ("ITIMER_REAL", scm_from_int (ITIMER_REAL));
  606. scm_c_define ("ITIMER_VIRTUAL", scm_from_int (ITIMER_VIRTUAL));
  607. scm_c_define ("ITIMER_PROF", scm_from_int (ITIMER_PROF));
  608. #endif /* defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER) */
  609. #include "libguile/scmsigs.x"
  610. }
  611. /*
  612. Local Variables:
  613. c-file-style: "gnu"
  614. End:
  615. */