scmsigs.c 22 KB

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