Skip to content

Commit c8d208b

Browse files
committed
pass pid as int not string
1 parent 5c6ee6a commit c8d208b

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

lib/macos.ml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
(* Extensions to the Os module for macOS *)
2-
open Lwt.Infix
2+
open Lwt.Syntax
33
open Os
44

55
let ( / ) = Filename.concat
66

77
let user_exists ~user =
8-
pread ["sudo"; "dscl"; "."; "list"; "/Users"] >|= fun s ->
8+
let+ s = pread ["sudo"; "dscl"; "."; "list"; "/Users"] in
99
List.exists (Astring.String.equal user) (Astring.String.cuts ~sep:"\n" s)
1010

1111
(* Generates a new MacOS user called `<prefix><uid>' *)
1212
let create_new_user ~username ~home ~uid ~gid =
13-
user_exists ~user:username >>= begin function
14-
| true -> Lwt.return_ok ()
15-
| false ->
16-
let user = "/Users" / username in
17-
let pp s ppf = Fmt.pf ppf "[ Mac ] %s\n" s in
18-
let dscl = ["dscl"; "."; "-create"; user ] in
19-
sudo_result ~pp:(pp "UniqueID") (dscl @ ["UniqueID"; uid]) >>!= fun _ ->
20-
sudo_result ~pp:(pp "PrimaryGroupID") (dscl @ ["PrimaryGroupID"; gid]) >>!= fun _ ->
21-
sudo_result ~pp:(pp "UserShell") (dscl @ ["UserShell"; "/bin/bash"]) >>!= fun _ ->
22-
sudo_result ~pp:(pp "NFSHomeDirectory") (dscl @ ["NFSHomeDirectory"; home])
23-
end
13+
let* exists = user_exists ~user:username in
14+
if exists then Lwt.return_ok ()
15+
else
16+
let user = "/Users" / username in
17+
let pp s ppf = Fmt.pf ppf "[ Mac ] %s\n" s in
18+
let dscl = [ "dscl"; "."; "-create"; user ] in
19+
sudo_result ~pp:(pp "UniqueID") (dscl @ [ "UniqueID"; uid ]) >>!= fun _ ->
20+
sudo_result ~pp:(pp "PrimaryGroupID") (dscl @ [ "PrimaryGroupID"; gid ])
21+
>>!= fun _ ->
22+
sudo_result ~pp:(pp "UserShell") (dscl @ [ "UserShell"; "/bin/bash" ])
23+
>>!= fun _ ->
24+
sudo_result ~pp:(pp "NFSHomeDirectory") (dscl @ [ "NFSHomeDirectory"; home ])
2425

2526
let delete_user ~user =
26-
user_exists ~user >>= begin function
27+
let* exists = user_exists ~user in
28+
match exists with
2729
| false ->
2830
Log.info (fun f -> f "Not deleting %s as they don't exist" user);
2931
Lwt_result.return ()
@@ -32,32 +34,30 @@ let delete_user ~user =
3234
let pp s ppf = Fmt.pf ppf "[ Mac ] %s\n" s in
3335
let delete = ["dscl"; "."; "-delete"; user ] in
3436
sudo_result ~pp:(pp "Deleting") delete
35-
end
3637

3738
let descendants ~pid =
3839
Lwt.catch
39-
(fun () -> pread ["sudo"; "pgrep"; "-P"; pid ] >|= fun s -> Astring.String.cuts ~sep:"\n" s)
40+
(fun () ->
41+
let+ s = pread ["sudo"; "pgrep"; "-P"; string_of_int pid ] in
42+
let pids = Astring.String.cuts ~sep:"\n" s in
43+
List.filter_map int_of_string_opt pids)
4044
(* Errors if there are none, probably errors for other reasons too... *)
4145
(fun _ -> Lwt.return [])
4246

4347
let kill ~pid =
44-
let pp s ppf = Fmt.pf ppf "[ %s ]" s in
45-
if String.length pid = 0 then (Log.warn (fun f -> f "Empty PID"); Lwt.return ())
46-
else begin
47-
let delete = ["kill"; "-9"; pid ] in
48-
sudo_result ~pp:(pp "KILL") delete >>= fun t ->
49-
match t with
50-
| Ok () -> Lwt.return ()
51-
| Error (`Msg m) -> (
52-
Log.warn (fun f -> f "Failed to kill process %s because %s" pid m);
53-
Lwt.return ()
54-
)
55-
end
48+
let pp _ ppf = Fmt.pf ppf "[ KILL ]" in
49+
let delete = ["kill"; "-9"; string_of_int pid ] in
50+
let* t = sudo_result ~pp:(pp "KILL") delete in
51+
match t with
52+
| Ok () -> Lwt.return ()
53+
| Error (`Msg m) ->
54+
Log.warn (fun f -> f "Failed to kill process %i because %s" pid m);
55+
Lwt.return ()
5656

5757
let kill_all_descendants ~pid =
5858
let rec kill_all pid : unit Lwt.t =
59-
descendants ~pid >>= fun ds ->
60-
Lwt_list.iter_s kill_all ds >>= fun () ->
59+
let* ds = descendants ~pid in
60+
let* () = Lwt_list.iter_s kill_all ds in
6161
kill ~pid
6262
in
6363
kill_all pid

lib/macos_sandbox.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ let run ~cancelled ?stdin:stdin ~log (t : t) config homedir =
9292
let aux () =
9393
if Lwt.is_sleeping proc then (
9494
match !proc_id with
95-
| Some pid -> Macos.kill_all_descendants ~pid:(string_of_int pid)
95+
| Some pid -> Macos.kill_all_descendants ~pid
9696
| None -> Log.warn (fun f -> f "Failed to find pid..."); Lwt.return ()
9797
)
9898
else Lwt.return_unit (* Process has already finished *)

lib/macos_sandbox.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ val cmdliner : config Cmdliner.Term.t
1010
and parameters to setup a specific sandbox's configuration. *)
1111

1212
val create : state_dir:string -> config -> t Lwt.t
13-
(** [create ~state_dir config] is a runc sandboxing system that keeps state in [state_dir]
14-
and is configured using [config]. *)
13+
(** [create ~state_dir config] is a user-based sandboxing system for macOS that
14+
keeps state in [state_dir] and is configured using [config]. *)

0 commit comments

Comments
 (0)