Skip to content

Commit 989fc81

Browse files
authored
Merge pull request #803 from patricoferris/process-users
Add setuid and setgid fork action
2 parents 71a33f4 + 24c9b82 commit 989fc81

File tree

7 files changed

+84
-5
lines changed

7 files changed

+84
-5
lines changed

lib_eio/unix/fork_action.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,41 @@ static void action_setpgid(int errors, value v_config) {
258258
CAMLprim value eio_unix_fork_setpgid(value v_unit) {
259259
return Val_fork_fn(action_setpgid);
260260
}
261+
262+
static void action_setuid(int errors, value v_config) {
263+
#ifdef _WIN32
264+
eio_unix_fork_error(errors, "action_setuid", "Unsupported operation on windows");
265+
_exit(1);
266+
#else
267+
value v_uid = Field(v_config, 1);
268+
int r;
269+
r = setuid(Int_val(v_uid));
270+
if (r != 0) {
271+
eio_unix_fork_error(errors, "setuid", strerror(errno));
272+
_exit(1);
273+
}
274+
#endif
275+
}
276+
277+
CAMLprim value eio_unix_fork_setuid(value v_unit) {
278+
return Val_fork_fn(action_setuid);
279+
}
280+
281+
static void action_setgid(int errors, value v_config) {
282+
#ifdef _WIN32
283+
eio_unix_fork_error(errors, "action_setgid", "Unsupported operation on windows");
284+
_exit(1);
285+
#else
286+
value v_gid = Field(v_config, 1);
287+
int r;
288+
r = setgid(Int_val(v_gid));
289+
if (r != 0) {
290+
eio_unix_fork_error(errors, "setgid", strerror(errno));
291+
_exit(1);
292+
}
293+
#endif
294+
}
295+
296+
CAMLprim value eio_unix_fork_setgid(value v_unit) {
297+
return Val_fork_fn(action_setgid);
298+
}

lib_eio/unix/fork_action.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ let inherit_fds m =
7171

7272
external action_setpgid : unit -> fork_fn = "eio_unix_fork_setpgid"
7373
let action_setpgid = action_setpgid ()
74-
7574
let setpgid pgid =
7675
{ run = fun k -> k (Obj.repr (action_setpgid, 0, pgid)) }
76+
77+
external action_setuid : unit -> fork_fn = "eio_unix_fork_setuid"
78+
let action_setuid = action_setuid ()
79+
let setuid uid = {
80+
run = fun k -> k (Obj.repr (action_setuid, uid)) }
81+
82+
external action_setgid : unit -> fork_fn = "eio_unix_fork_setgid"
83+
let action_setgid = action_setgid ()
84+
let setgid gid = {
85+
run = fun k -> k (Obj.repr (action_setgid, gid)) }

lib_eio/unix/fork_action.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ val setpgid : int -> t
6464
6565
If [pgid] is [0] the child's process ID will be used as the PGID, placing
6666
the child in a {e new} process group. *)
67+
68+
val setuid : int -> t
69+
(** [setuid uid] sets the user ID to [uid]. *)
70+
71+
val setgid : int -> t
72+
(** [setgid gid] sets the group ID to [gid]. *)

lib_eio/unix/process.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ module Pi = struct
8484
sw:Switch.t ->
8585
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
8686
?pgid:int ->
87+
?uid:int ->
88+
?gid:int ->
8789
env:string array ->
8890
fds:(int * Fd.t * Fork_action.blocking) list ->
8991
executable:string ->
@@ -109,6 +111,8 @@ module Make_mgr (X : sig
109111
sw:Switch.t ->
110112
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
111113
?pgid:int ->
114+
?uid:int ->
115+
?gid:int ->
112116
env:string array ->
113117
fds:(int * Fd.t * Fork_action.blocking) list ->
114118
executable:string ->
@@ -140,11 +144,11 @@ end) = struct
140144
let spawn_unix = X.spawn_unix
141145
end
142146

143-
let spawn_unix ~sw (Eio.Resource.T (v, ops)) ?cwd ?pgid ~fds ?env ?executable args =
147+
let spawn_unix ~sw (Eio.Resource.T (v, ops)) ?cwd ?pgid ?uid ?gid ~fds ?env ?executable args =
144148
let module X = (val (Eio.Resource.get ops Pi.Mgr_unix)) in
145149
let executable = get_executable executable ~args in
146150
let env = get_env env in
147-
X.spawn_unix v ~sw ?cwd ?pgid ~fds ~env ~executable args
151+
X.spawn_unix v ~sw ?cwd ?pgid ?uid ?gid ~fds ~env ~executable args
148152

149153
let sigchld = Eio.Condition.create ()
150154

lib_eio/unix/process.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Pi : sig
2121
sw:Switch.t ->
2222
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
2323
?pgid:int ->
24+
?uid:int ->
25+
?gid:int ->
2426
env:string array ->
2527
fds:(int * Fd.t * Fork_action.blocking) list ->
2628
executable:string ->
@@ -44,6 +46,8 @@ module Make_mgr (X : sig
4446
sw:Switch.t ->
4547
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
4648
?pgid:int ->
49+
?uid:int ->
50+
?gid:int ->
4751
env:string array ->
4852
fds:(int * Fd.t * Fork_action.blocking) list ->
4953
executable:string ->
@@ -56,6 +60,8 @@ val spawn_unix :
5660
_ mgr ->
5761
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
5862
?pgid:int ->
63+
?uid:int ->
64+
?gid:int ->
5965
fds:(int * Fd.t * Fork_action.blocking) list ->
6066
?env:string array ->
6167
?executable:string ->

lib_eio_linux/eio_linux.ml

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

222-
let spawn_unix () ~sw ?cwd ?pgid ~env ~fds ~executable args =
222+
let spawn_unix () ~sw ?cwd ?pgid ?uid ?gid ~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
@@ -228,6 +228,14 @@ module Process_mgr = struct
228228
| None -> actions
229229
| Some pgid -> Eio_unix.Private.Fork_action.setpgid pgid :: actions
230230
in
231+
let actions = match uid with
232+
| None -> actions
233+
| Some uid -> Eio_unix.Private.Fork_action.setuid uid :: actions
234+
in
235+
let actions = match gid with
236+
| None -> actions
237+
| Some gid -> Eio_unix.Private.Fork_action.setgid gid :: actions
238+
in
231239
let with_actions cwd fn = match cwd with
232240
| None -> fn actions
233241
| Some (fd, s) ->

lib_eio_posix/process.ml

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

26-
let spawn_unix () ~sw ?cwd ?pgid ~env ~fds ~executable args =
26+
let spawn_unix () ~sw ?cwd ?pgid ?uid ?gid ~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
@@ -32,6 +32,14 @@ module Impl = struct
3232
| None -> actions
3333
| Some pgid -> Low_level.Process.Fork_action.setpgid pgid :: actions
3434
in
35+
let actions = match uid with
36+
| None -> actions
37+
| Some uid -> Eio_unix.Private.Fork_action.setuid uid :: actions
38+
in
39+
let actions = match gid with
40+
| None -> actions
41+
| Some gid -> Eio_unix.Private.Fork_action.setgid gid :: actions
42+
in
3543
let with_actions cwd fn = match cwd with
3644
| None -> fn actions
3745
| Some ((dir, path) : Eio.Fs.dir_ty Eio.Path.t) ->

0 commit comments

Comments
 (0)