2016-02-18 07:11:59 -08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* OCaml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1998 Institut National de Recherche en Informatique et */
|
|
|
|
/* en Automatique. */
|
|
|
|
/* */
|
|
|
|
/* All rights reserved. This file is distributed under the terms of */
|
|
|
|
/* the GNU Lesser General Public License version 2.1, with the */
|
|
|
|
/* special exception on linking described in the file LICENSE. */
|
|
|
|
/* */
|
|
|
|
/**************************************************************************/
|
1998-08-08 09:53:42 -07:00
|
|
|
|
2016-07-04 10:00:57 -07:00
|
|
|
#define CAML_INTERNALS
|
|
|
|
|
1998-08-08 09:53:42 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2014-12-27 06:41:49 -08:00
|
|
|
#include <caml/alloc.h>
|
|
|
|
#include <caml/fail.h>
|
|
|
|
#include <caml/memory.h>
|
|
|
|
#include <caml/mlvalues.h>
|
|
|
|
#include <caml/signals.h>
|
1998-08-08 09:53:42 -07:00
|
|
|
#include "unixsupport.h"
|
|
|
|
|
2000-03-17 08:45:18 -08:00
|
|
|
#ifndef NSIG
|
2008-01-11 08:13:18 -08:00
|
|
|
#define NSIG 64
|
2000-03-17 08:45:18 -08:00
|
|
|
#endif
|
|
|
|
|
1999-06-05 05:02:48 -07:00
|
|
|
#ifdef POSIX_SIGNALS
|
|
|
|
|
1998-08-08 09:53:42 -07:00
|
|
|
static void decode_sigset(value vset, sigset_t * set)
|
|
|
|
{
|
|
|
|
sigemptyset(set);
|
|
|
|
while (vset != Val_int(0)) {
|
2008-01-11 08:13:18 -08:00
|
|
|
int sig = caml_convert_signal_number(Int_val(Field(vset, 0)));
|
1998-08-13 08:58:08 -07:00
|
|
|
sigaddset(set, sig);
|
1998-08-08 09:53:42 -07:00
|
|
|
vset = Field(vset, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static value encode_sigset(sigset_t * set)
|
|
|
|
{
|
|
|
|
value res = Val_int(0);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Begin_root(res)
|
|
|
|
for (i = 1; i < NSIG; i++)
|
2008-01-11 08:13:18 -08:00
|
|
|
if (sigismember(set, i) > 0) {
|
2016-11-17 02:03:09 -08:00
|
|
|
value newcons = caml_alloc_small(2, 0);
|
2008-01-11 08:13:18 -08:00
|
|
|
Field(newcons, 0) = Val_int(caml_rev_convert_signal_number(i));
|
1998-08-08 09:53:42 -07:00
|
|
|
Field(newcons, 1) = res;
|
|
|
|
res = newcons;
|
|
|
|
}
|
|
|
|
End_roots();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sigprocmask_cmd[3] = { SIG_SETMASK, SIG_BLOCK, SIG_UNBLOCK };
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_sigprocmask(value vaction, value vset)
|
1998-08-08 09:53:42 -07:00
|
|
|
{
|
|
|
|
int how;
|
|
|
|
sigset_t set, oldset;
|
1999-06-05 06:27:54 -07:00
|
|
|
int retcode;
|
1998-08-08 09:53:42 -07:00
|
|
|
|
|
|
|
how = sigprocmask_cmd[Int_val(vaction)];
|
|
|
|
decode_sigset(vset, &set);
|
2016-11-17 02:03:09 -08:00
|
|
|
caml_enter_blocking_section();
|
Fix Thread.sigmask, take 2 (#2211)
Fix Thread.sigmaks, by checking whether a signal is masked before handling it.
We use [sigprocmask] (if available) to check whether a signal is
blocked when the systhread library is not loaded. As soon as the
[Thread] module gets loaded, we use [pthread_sigmask] instead, and
redirect all the calls to [sigprocmask] to [pthread_sigmask]. Indeed,
the latter has unspecified behavior in a multi-threaded context
anyway. In practice, this should not change the semantics of
[Unix.sigprocmask] on Linux, since on this platform, [pthread_sigmask]
is actually an alias for [sigprocmask]. On MacOSX, the semantics will
change, since [sigprocmask] changes the masks of the whole process on
this platform.
Also, include [caml_pending_signals] in signals returned by
[Unix.sigpending]. Indeed, some signals might have been handled in the
POSIX sense by the C handler in the OCaml runtime, but not been
handled in the OCaml sense (for example, because they are blocked).
This commit un-reverts 1c82c481a, which has been reverted in
79eb572e4. The issues of the original commit are corrected in this commit.
2019-03-01 05:14:29 -08:00
|
|
|
retcode = caml_sigmask_hook(how, &set, &oldset);
|
2016-11-17 02:03:09 -08:00
|
|
|
caml_leave_blocking_section();
|
2020-07-27 08:12:10 -07:00
|
|
|
/* Run any handlers for just-unmasked pending signals */
|
|
|
|
caml_process_pending_actions();
|
Fix Thread.sigmask, take 2 (#2211)
Fix Thread.sigmaks, by checking whether a signal is masked before handling it.
We use [sigprocmask] (if available) to check whether a signal is
blocked when the systhread library is not loaded. As soon as the
[Thread] module gets loaded, we use [pthread_sigmask] instead, and
redirect all the calls to [sigprocmask] to [pthread_sigmask]. Indeed,
the latter has unspecified behavior in a multi-threaded context
anyway. In practice, this should not change the semantics of
[Unix.sigprocmask] on Linux, since on this platform, [pthread_sigmask]
is actually an alias for [sigprocmask]. On MacOSX, the semantics will
change, since [sigprocmask] changes the masks of the whole process on
this platform.
Also, include [caml_pending_signals] in signals returned by
[Unix.sigpending]. Indeed, some signals might have been handled in the
POSIX sense by the C handler in the OCaml runtime, but not been
handled in the OCaml sense (for example, because they are blocked).
This commit un-reverts 1c82c481a, which has been reverted in
79eb572e4. The issues of the original commit are corrected in this commit.
2019-03-01 05:14:29 -08:00
|
|
|
if (retcode != 0) unix_error(retcode, "sigprocmask", Nothing);
|
1998-08-08 09:53:42 -07:00
|
|
|
return encode_sigset(&oldset);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_sigpending(value unit)
|
1998-08-08 09:53:42 -07:00
|
|
|
{
|
|
|
|
sigset_t pending;
|
Fix Thread.sigmask, take 2 (#2211)
Fix Thread.sigmaks, by checking whether a signal is masked before handling it.
We use [sigprocmask] (if available) to check whether a signal is
blocked when the systhread library is not loaded. As soon as the
[Thread] module gets loaded, we use [pthread_sigmask] instead, and
redirect all the calls to [sigprocmask] to [pthread_sigmask]. Indeed,
the latter has unspecified behavior in a multi-threaded context
anyway. In practice, this should not change the semantics of
[Unix.sigprocmask] on Linux, since on this platform, [pthread_sigmask]
is actually an alias for [sigprocmask]. On MacOSX, the semantics will
change, since [sigprocmask] changes the masks of the whole process on
this platform.
Also, include [caml_pending_signals] in signals returned by
[Unix.sigpending]. Indeed, some signals might have been handled in the
POSIX sense by the C handler in the OCaml runtime, but not been
handled in the OCaml sense (for example, because they are blocked).
This commit un-reverts 1c82c481a, which has been reverted in
79eb572e4. The issues of the original commit are corrected in this commit.
2019-03-01 05:14:29 -08:00
|
|
|
int i;
|
1998-08-08 09:53:42 -07:00
|
|
|
if (sigpending(&pending) == -1) uerror("sigpending", Nothing);
|
Fix Thread.sigmask, take 2 (#2211)
Fix Thread.sigmaks, by checking whether a signal is masked before handling it.
We use [sigprocmask] (if available) to check whether a signal is
blocked when the systhread library is not loaded. As soon as the
[Thread] module gets loaded, we use [pthread_sigmask] instead, and
redirect all the calls to [sigprocmask] to [pthread_sigmask]. Indeed,
the latter has unspecified behavior in a multi-threaded context
anyway. In practice, this should not change the semantics of
[Unix.sigprocmask] on Linux, since on this platform, [pthread_sigmask]
is actually an alias for [sigprocmask]. On MacOSX, the semantics will
change, since [sigprocmask] changes the masks of the whole process on
this platform.
Also, include [caml_pending_signals] in signals returned by
[Unix.sigpending]. Indeed, some signals might have been handled in the
POSIX sense by the C handler in the OCaml runtime, but not been
handled in the OCaml sense (for example, because they are blocked).
This commit un-reverts 1c82c481a, which has been reverted in
79eb572e4. The issues of the original commit are corrected in this commit.
2019-03-01 05:14:29 -08:00
|
|
|
for (i = 1; i < NSIG; i++)
|
|
|
|
if(caml_pending_signals[i])
|
|
|
|
sigaddset(&pending, i);
|
1998-08-08 09:53:42 -07:00
|
|
|
return encode_sigset(&pending);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_sigsuspend(value vset)
|
1998-08-08 09:53:42 -07:00
|
|
|
{
|
|
|
|
sigset_t set;
|
1999-06-05 06:27:54 -07:00
|
|
|
int retcode;
|
1998-08-08 09:53:42 -07:00
|
|
|
decode_sigset(vset, &set);
|
2016-11-17 02:03:09 -08:00
|
|
|
caml_enter_blocking_section();
|
1999-06-05 06:27:54 -07:00
|
|
|
retcode = sigsuspend(&set);
|
2016-11-17 02:03:09 -08:00
|
|
|
caml_leave_blocking_section();
|
1999-06-05 06:27:54 -07:00
|
|
|
if (retcode == -1 && errno != EINTR) uerror("sigsuspend", Nothing);
|
1998-08-08 09:53:42 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
1999-06-05 05:02:48 -07:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_sigprocmask(value vaction, value vset)
|
2016-11-26 16:47:22 -08:00
|
|
|
{ caml_invalid_argument("Unix.sigprocmask not available"); }
|
1999-06-05 05:02:48 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_sigpending(value unit)
|
2016-11-26 16:47:22 -08:00
|
|
|
{ caml_invalid_argument("Unix.sigpending not available"); }
|
1999-06-05 05:02:48 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_sigsuspend(value vset)
|
2016-11-26 16:47:22 -08:00
|
|
|
{ caml_invalid_argument("Unix.sigsuspend not available"); }
|
1999-06-05 05:02:48 -07:00
|
|
|
|
|
|
|
#endif
|