Module Unix.Signal

Waiting on signals.

Warning. This uses the OCaml signal handlers of Sys which are global variables. If you don't get what you want check that no other part of the program is trying to Sys.signal or Sys.set_signal.

Implementation note. Theoretically this should work reliably regardless on how signal delivery is configured in the program. The Signal.handler.Waiters signal handling mode integrates into the file descriptor watching infrastructure via a socket created with Unix.socketpair (self-pipe trick).

Waiting

val wait : Stdlib.Sys.signal -> unit

wait signal waits for the next occurence of signal and continues if and only if the handler of s is Waiters at signal occurence time.

val wait_any : Stdlib.Sys.signal list -> Stdlib.Sys.signal

wait_any signals waits for the next occurence of a signal in signals whose handler is Waiters at occurence time and continues with the signal that did.

Actions

val wait' : Stdlib.Sys.signal -> 'tag -> 'tag Affect.Action.t

wait' signal tag is the action used by wait and wait_any. The action invocation is enabled and synchronizes with tag whenever the next occurence of signal is delivered to the process and signal is set to be handled by Waiters at that instant.

Handling

type handler =
  1. | Default
    (*

    Default signal hander, usually abort the program.

    *)
  2. | Ignore
    (*

    Ignore the signal.

    *)
  3. | Fun of Stdlib.Sys.signal -> unit
    (*

    Call the given function with the signal number.

    *)
  4. | Waiters
    (*

    Unblock functions waiting on the signal with the wait' action.

    *)
val set : Stdlib.Sys.signal -> handler -> unit

set signal h sets the handler of signal to h.

val set_and_restore : Stdlib.Sys.signal -> handler -> (unit -> 'a) -> 'a

set_and_restore signal b f gets the handler for signal as current, sets the handler of signal to b, calls f () and sets the handler of signal back to current after f returned or raised.

Warning. Signal handlers are global, there is no notion of scope here. If someone sets signal in parallel these changes will also be visible in f.

Formatting

val pp : Stdlib.Format.formatter -> Stdlib.Sys.signal -> unit

pp formats signals for inspection.