Skip to content

Commit

Permalink
Merge pull request #543 from talex5/pp-signal
Browse files Browse the repository at this point in the history
Use `Fmt.Dump.signal` to format signals
  • Loading branch information
talex5 authored Jun 4, 2023
2 parents 85adab3 + ce83e85 commit dbcbd64
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ All process functions either return the exit status or check that it was zero (s
let proc_mgr = Eio.Stdenv.process_mgr env in
Eio.Process.parse_out proc_mgr Eio.Buf_read.take_all ["sh"; "-c"; "exit 3"];;
Exception:
Eio.Io Process Child_error Exited 3,
Eio.Io Process Child_error Exited (code 3),
running command: sh -c "exit 3"
```

Expand Down
6 changes: 3 additions & 3 deletions lib_eio/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ type exit_status = [
type status = [ exit_status | `Stopped of int ]

let pp_status ppf = function
| `Exited i -> Format.fprintf ppf "Exited %i" i
| `Signaled i -> Format.fprintf ppf "Signalled %i" i
| `Stopped i -> Format.fprintf ppf "Stopped %i" i
| `Exited i -> Format.fprintf ppf "Exited (code %i)" i
| `Signaled i -> Format.fprintf ppf "Exited (signal %a)" Fmt.Dump.signal i
| `Stopped i -> Format.fprintf ppf "Stopped (signal %a)" Fmt.Dump.signal i

type error =
| Executable_not_found of string
Expand Down
14 changes: 6 additions & 8 deletions tests/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ let run fn =
Eio_main.run @@ fun env ->
fn env#process_mgr env
let check_signal expected = function
| `Signaled x when x = expected -> Ok ()
| x -> Error x;;
let status_to_string = Fmt.to_to_string Eio.Process.pp_status
```

Running a program as a subprocess:
Expand All @@ -43,17 +41,17 @@ Stopping a subprocess works and checking the status waits and reports correctly:
Switch.run @@ fun sw ->
let t = Process.spawn ~sw mgr [ "sleep"; "10" ] in
Process.signal t Sys.sigkill;
Process.await t |> check_signal Sys.sigkill;;
- : (unit, Process.exit_status) result = Ok ()
Process.await t |> status_to_string
- : string = "Exited (signal SIGKILL)"
```

A switch will stop a process when it is released:

```ocaml
# run @@ fun mgr env ->
let proc = Switch.run (fun sw -> Process.spawn ~sw mgr [ "sleep"; "10" ]) in
Process.await proc |> check_signal Sys.sigkill;;
- : (unit, Process.exit_status) result = Ok ()
Process.await proc |> status_to_string
- : string = "Exited (signal SIGKILL)"
```

Passing in flows allows you to redirect the child process' stdout:
Expand Down Expand Up @@ -146,7 +144,7 @@ If a command fails, we get shown the arguments (quoted if necessary):
# run @@ fun mgr env ->
Process.run mgr ["bash"; "-c"; "exit 3"; ""; "foo"];;
Exception:
Eio.Io Process Child_error Exited 3,
Eio.Io Process Child_error Exited (code 3),
running command: bash -c "exit 3" "" foo
```

Expand Down

0 comments on commit dbcbd64

Please sign in to comment.