scmsigs.c 21 KB

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