scmsigs.c 21 KB

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