diff --git a/.ocamlformat b/.ocamlformat index e80baea..187e7df 100644 --- a/.ocamlformat +++ b/.ocamlformat @@ -1 +1 @@ -version=0.27.0 +version=0.29.0 diff --git a/bin/dune b/bin/dune new file mode 100644 index 0000000..46e05a5 --- /dev/null +++ b/bin/dune @@ -0,0 +1,8 @@ +(executable + (name main) + (public_name opentrace) + (preprocess + (pps ppx_blob)) + (preprocessor_deps + (file ../lib/opentrace.bpf.o)) + (libraries eio_posix opentrace)) diff --git a/opentrace.ml b/bin/main.ml similarity index 98% rename from opentrace.ml rename to bin/main.ml index 97966e7..e56df73 100644 --- a/opentrace.ml +++ b/bin/main.ml @@ -1,10 +1,11 @@ +open Opentrace open Libbpf open Libbpf_maps type format = Csv | Json let obj_path = "opentrace.bpf.o" -let obj_file = [%blob "opentrace.bpf.o"] +let obj_file = [%blob "../lib/opentrace.bpf.o"] let program_names = [ @@ -209,8 +210,10 @@ let exec format output user poll (prog, args) open_flags_filter cgroups = Condition.wait start_process mutex; Eio.Switch.run @@ fun sw -> with_cgroup cgroups @@ fun group -> - let process = Spawn.make_process group (Some uid) in - let p = Eio.Process.spawn process ~sw (prog :: args) in + let process = Spawn.make_process group in + let p = + Eio_unix.Process.spawn_unix ~uid ~fds:[] process ~sw (prog :: args) + in Atomic.set pid (Some (Eio.Process.pid p)); let stat = Option.map diff --git a/dune-project b/dune-project index 04ac888..cd97086 100644 --- a/dune-project +++ b/dune-project @@ -11,7 +11,7 @@ (description "") (depends (ocaml (>= 5.2.0)) - eio_main + (eio_posix (>= 1.4)) menhir cmdliner ppx_blob diff --git a/dune b/lib/dune similarity index 94% rename from dune rename to lib/dune index 366b044..0cacaa8 100644 --- a/dune +++ b/lib/dune @@ -3,10 +3,9 @@ (ocamllex lexer) -(executable +(library (name opentrace) (public_name opentrace) - (modules opentrace config parser filter lexer spawn) (foreign_stubs (language c) (flags :standard -D_LARGEFILE64_SOURCE) diff --git a/filter.ml b/lib/filter.ml similarity index 100% rename from filter.ml rename to lib/filter.ml diff --git a/include/discover.ml b/lib/include/discover.ml similarity index 100% rename from include/discover.ml rename to lib/include/discover.ml diff --git a/include/dune b/lib/include/dune similarity index 100% rename from include/dune rename to lib/include/dune diff --git a/lexer.mll b/lib/lexer.mll similarity index 100% rename from lexer.mll rename to lib/lexer.mll diff --git a/opentrace.bpf.c b/lib/opentrace.bpf.c similarity index 100% rename from opentrace.bpf.c rename to lib/opentrace.bpf.c diff --git a/opentrace_stubs.c b/lib/opentrace_stubs.c similarity index 51% rename from opentrace_stubs.c rename to lib/opentrace_stubs.c index c63499f..f4b4871 100644 --- a/opentrace_stubs.c +++ b/lib/opentrace_stubs.c @@ -12,39 +12,18 @@ #include "include/fork_action.h" -static void action_setuid(int errors, value v_config) { - #ifdef _WIN32 - eio_unix_fork_error(errors, "action_setuid", "Unsupported operation on windows"); - #else - value v_uid = Field(v_config, 1); - int r; - r = setuid(Int_val(v_uid)); - if (r != 0) { - eio_unix_fork_error(errors, "setuid", strerror(errno)); - _exit(1); - } - #endif -} - -CAMLprim value eio_unix_fork_setuid(value v_unit) { - return Val_fork_fn(action_setuid); -} - static void action_setcgroup(int errors, value v_config) { value v_path = Field(v_config, 1); - int r; pid_t pid = getpid(); FILE *f = fopen(String_val(v_path), "w"); if (f == NULL) { - eio_unix_fork_error(errors, "cgroup", strerror(errno)); - _exit(1); + eio_unix_fork_error(errors, "cgroup", errno); } if (fprintf(f, "%d\n", pid) < 0) { fclose(f); - eio_unix_fork_error(errors, "cgroup", strerror(errno)); - _exit(1); + eio_unix_fork_error(errors, "cgroup", errno); } fclose(f); diff --git a/parser.mly b/lib/parser.mly similarity index 100% rename from parser.mly rename to lib/parser.mly diff --git a/spawn.ml b/lib/spawn.ml similarity index 65% rename from spawn.ml rename to lib/spawn.ml index 878de81..a84d920 100644 --- a/spawn.ml +++ b/lib/spawn.ml @@ -31,15 +31,6 @@ let process = module T = struct type t = unit - external action_setuid : unit -> Eio_unix.Private.Fork_action.fork_fn - = "eio_unix_fork_setuid" - - let action_setuid = action_setuid () - - let setuid (uid : int) = - Eio_unix.Private.Fork_action. - { run = (fun k -> k (Obj.repr (action_setuid, uid))) } - external action_setcgroup : unit -> Eio_unix.Private.Fork_action.fork_fn = "eio_unix_fork_setcgroup" @@ -49,13 +40,40 @@ module T = struct Eio_unix.Private.Fork_action. { run = (fun k -> k (Obj.repr (action_setcgroup, group))) } - let spawn_unix () ~group ~uid ~sw ?cwd ~env ~fds ~executable args = + let spawn_unix () ~group ~sw ?cwd ?pgid ?uid ?gid ?login_tty ~env ~fds + ~executable args = + let login_tty_action, fds = + match login_tty with + | None -> ([], fds) + | Some tty -> + let fds = + [ (0, tty, `Blocking); (1, tty, `Blocking); (2, tty, `Blocking) ] + @ fds + in + ([ Eio_unix.Private.Fork_action.login_tty tty ], fds) + in + let actions = + Low_level.Process.Fork_action.( + login_tty_action + @ [ + Eio_unix.Private.Fork_action.inherit_fds fds; + execve executable ~argv:(Array.of_list args) ~env; + ]) + in + let actions = + match pgid with + | None -> actions + | Some pgid -> Eio_unix.Private.Fork_action.setpgid pgid :: actions + in let actions = - Low_level.Process.Fork_action. - [ inherit_fds fds; execve executable ~argv:(Array.of_list args) ~env ] + match uid with + | None -> actions + | Some uid -> Eio_unix.Private.Fork_action.setuid uid :: actions in let actions = - match uid with None -> actions | Some uid -> setuid uid :: actions + match gid with + | None -> actions + | Some gid -> Eio_unix.Private.Fork_action.setgid gid :: actions in let actions = match group with None -> actions | Some g -> setcgroup g :: actions @@ -78,11 +96,11 @@ module T = struct process (Low_level.Process.spawn ~sw actions) end -let make_process group uid = +let make_process group = let module T = struct type t = unit - let spawn_unix = T.spawn_unix ~group ~uid + let spawn_unix = T.spawn_unix ~group end in let h = Eio_unix.Process.Pi.mgr_unix (module Eio_unix.Process.Make_mgr (T)) in Eio.Resource.T ((), h) diff --git a/uring.h b/lib/uring.h similarity index 100% rename from uring.h rename to lib/uring.h diff --git a/opentrace.opam b/opentrace.opam index 1c9470a..58b2582 100644 --- a/opentrace.opam +++ b/opentrace.opam @@ -5,8 +5,8 @@ description: "" depends: [ "dune" {>= "3.14"} "ocaml" {>= "5.2.0"} - "eio_main" - "menhir" + "eio_posix" {>= "1.4"} + "menhir" {>= "20180523"} "cmdliner" "ppx_blob" "jsonm"