scmsigs.c 22 KB

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