scmsigs.c 25 KB

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