scmsigs.c 21 KB

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