package core_unix

  1. Overview
  2. Docs
Unix-specific portions of Core

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.1.tar.gz
md5=9370dca36f518fcea046d2752e3de22b
sha512=c4e8ce9d5885ac8fa8d554a97e1857f3a1c933e0eb5dfd4fe874412b9d09e6d0a2973b644733855553f33f5c859719228f0e6aaf3a2b7eb5befb46fc513750de

doc/src/core_unix.signal_unix/signal_unix.ml.html

Source file signal_unix.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
open! Core
open! Import
open Core.Signal

external ml_caml_to_nonportable_signal_number
  :  t
  -> int
  = "ml_caml_to_nonportable_signal_number"

external ml_nonportable_to_caml_signal_number
  :  int
  -> t
  = "ml_nonportable_to_caml_signal_number"

let of_system_int t = ml_nonportable_to_caml_signal_number t
let to_system_int t = ml_caml_to_nonportable_signal_number t

type pid_spec =
  [ `Pid of Pid.t
  | `My_group
  | `Group of Pid.t
  ]
[@@deriving sexp_of]

let pid_spec_to_int = function
  | `Pid pid -> Pid.to_int pid
  | `My_group -> 0
  | `Group pid -> ~-(Pid.to_int pid)
;;

let pid_spec_to_string p = Int.to_string (pid_spec_to_int p)

let send t pid_spec =
  try
    UnixLabels.kill ~pid:(pid_spec_to_int pid_spec) ~signal:(to_caml_int t);
    `Ok
  with
  | Unix.Unix_error (Unix.ESRCH, _, _) -> `No_such_process
;;

let send_i t pid_spec =
  match send t pid_spec with
  | `Ok | `No_such_process -> ()
;;

let send_exn t pid_spec =
  match send t pid_spec with
  | `Ok -> ()
  | `No_such_process ->
    failwithf
      "Signal_unix.send_exn %s pid:%s"
      (to_string t)
      (pid_spec_to_string pid_spec)
      ()
;;

type sigprocmask_command =
  [ `Set
  | `Block
  | `Unblock
  ]

let sigprocmask mode sigs =
  let mode =
    match mode with
    | `Block -> Unix.SIG_BLOCK
    | `Unblock -> Unix.SIG_UNBLOCK
    | `Set -> Unix.SIG_SETMASK
  in
  Unix.sigprocmask mode (sigs |> List.map ~f:to_caml_int) |> List.map ~f:of_caml_int
;;

let sigpending () = Unix.sigpending () |> List.map ~f:of_caml_int
let sigsuspend ts = Unix.sigsuspend (ts |> List.map ~f:to_caml_int)

let can_send_to pid =
  try
    send_exn zero (`Pid pid);
    true
  with
  | _ -> false
;;
OCaml

Innovation. Community. Security.