Skip to content

Commit f6dac4a

Browse files
patricoferristalex5
andcommitted
Add process groups to unix backends
Co-authored-by: Thomas Leonard <[email protected]>
1 parent 62b9714 commit f6dac4a

File tree

7 files changed

+50
-4
lines changed

7 files changed

+50
-4
lines changed

lib_eio/unix/fork_action.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,24 @@ static void action_dups(int errors, value v_config) {
237237
CAMLprim value eio_unix_fork_dups(value v_unit) {
238238
return Val_fork_fn(action_dups);
239239
}
240+
241+
static void action_setpgid(int errors, value v_config) {
242+
#ifdef _WIN32
243+
eio_unix_fork_error(errors, "setpgid", "Unsupported operation on windows");
244+
_exit(1);
245+
#else
246+
value vpid = Field(v_config, 1);
247+
value vpgid = Field(v_config, 2);
248+
249+
int r;
250+
r = setpgid(Int_val(vpid), Int_val(vpgid));
251+
if (r != 0) {
252+
eio_unix_fork_error(errors, "setpgid", strerror(errno));
253+
_exit(1);
254+
}
255+
#endif
256+
}
257+
258+
CAMLprim value eio_unix_fork_setpgid(value v_unit) {
259+
return Val_fork_fn(action_setpgid);
260+
}

lib_eio/unix/fork_action.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ let inherit_fds m =
6868
with_fds m @@ fun m ->
6969
let plan : action list = Inherit_fds.plan m in
7070
{ run = fun k -> k (Obj.repr (action_dups, plan, blocking)) }
71+
72+
external action_setpgid : unit -> fork_fn = "eio_unix_fork_setpgid"
73+
let action_setpgid = action_setpgid ()
74+
75+
let setpgid pgid =
76+
{ run = fun k -> k (Obj.repr (action_setpgid, 0, pgid)) }

lib_eio/unix/fork_action.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ val inherit_fds : (int * Fd.t * [< blocking]) list -> t
5858
A mapping from an FD to itself simply clears the close-on-exec flag.
5959
6060
After this, the new FDs may also be set as blocking or non-blocking, depending on [flags]. *)
61+
62+
val setpgid : int -> t
63+
(** [setpgid pgid] sets the child's process group ID to [pgid].
64+
65+
If [pgid] is [0] the child's process ID will be used as the PGID, placing
66+
the child in a {e new} process group. *)

lib_eio/unix/process.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ module Pi = struct
8383
t ->
8484
sw:Switch.t ->
8585
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
86+
?pgid:int ->
8687
env:string array ->
8788
fds:(int * Fd.t * Fork_action.blocking) list ->
8889
executable:string ->
@@ -107,6 +108,7 @@ module Make_mgr (X : sig
107108
t ->
108109
sw:Switch.t ->
109110
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
111+
?pgid:int ->
110112
env:string array ->
111113
fds:(int * Fd.t * Fork_action.blocking) list ->
112114
executable:string ->
@@ -138,11 +140,11 @@ end) = struct
138140
let spawn_unix = X.spawn_unix
139141
end
140142

141-
let spawn_unix ~sw (Eio.Resource.T (v, ops)) ?cwd ~fds ?env ?executable args =
143+
let spawn_unix ~sw (Eio.Resource.T (v, ops)) ?cwd ?pgid ~fds ?env ?executable args =
142144
let module X = (val (Eio.Resource.get ops Pi.Mgr_unix)) in
143145
let executable = get_executable executable ~args in
144146
let env = get_env env in
145-
X.spawn_unix v ~sw ?cwd ~fds ~env ~executable args
147+
X.spawn_unix v ~sw ?cwd ?pgid ~fds ~env ~executable args
146148

147149
let sigchld = Eio.Condition.create ()
148150

lib_eio/unix/process.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Pi : sig
2020
t ->
2121
sw:Switch.t ->
2222
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
23+
?pgid:int ->
2324
env:string array ->
2425
fds:(int * Fd.t * Fork_action.blocking) list ->
2526
executable:string ->
@@ -42,6 +43,7 @@ module Make_mgr (X : sig
4243
t ->
4344
sw:Switch.t ->
4445
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
46+
?pgid:int ->
4547
env:string array ->
4648
fds:(int * Fd.t * Fork_action.blocking) list ->
4749
executable:string ->
@@ -53,6 +55,7 @@ val spawn_unix :
5355
sw:Switch.t ->
5456
_ mgr ->
5557
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
58+
?pgid:int ->
5659
fds:(int * Fd.t * Fork_action.blocking) list ->
5760
?env:string array ->
5861
?executable:string ->

lib_eio_linux/eio_linux.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,15 @@ module Process_mgr = struct
219219
module T = struct
220220
type t = unit
221221

222-
let spawn_unix () ~sw ?cwd ~env ~fds ~executable args =
222+
let spawn_unix () ~sw ?cwd ?pgid ~env ~fds ~executable args =
223223
let actions = Low_level.Process.Fork_action.[
224224
Eio_unix.Private.Fork_action.inherit_fds fds;
225225
execve executable ~argv:(Array.of_list args) ~env
226226
] in
227+
let actions = match pgid with
228+
| None -> actions
229+
| Some pgid -> Eio_unix.Private.Fork_action.setpgid pgid :: actions
230+
in
227231
let with_actions cwd fn = match cwd with
228232
| None -> fn actions
229233
| Some (fd, s) ->

lib_eio_posix/process.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ module Impl = struct
2323
module T = struct
2424
type t = unit
2525

26-
let spawn_unix () ~sw ?cwd ~env ~fds ~executable args =
26+
let spawn_unix () ~sw ?cwd ?pgid ~env ~fds ~executable args =
2727
let actions = Low_level.Process.Fork_action.[
2828
inherit_fds fds;
2929
execve executable ~argv:(Array.of_list args) ~env
3030
] in
31+
let actions = match pgid with
32+
| None -> actions
33+
| Some pgid -> Low_level.Process.Fork_action.setpgid pgid :: actions
34+
in
3135
let with_actions cwd fn = match cwd with
3236
| None -> fn actions
3337
| Some ((dir, path) : Eio.Fs.dir_ty Eio.Path.t) ->

0 commit comments

Comments
 (0)