scmsigs.c 22 KB

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