Last active
February 16, 2022 08:33
-
-
Save MisterDA/f299c6b6f67128acb4f3b5c1316897d5 to your computer and use it in GitHub Desktop.
OCaml win32unix: kill an arbitrary process?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_build/ | |
_opam/ | |
*.install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(executable | |
(name main) | |
(libraries unix) | |
(foreign_stubs | |
(language c) | |
(names pid))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
external get_current_process_id : unit -> int = "stub_GetCurrentProcessId" | |
let parent () = | |
let null = Unix.openfile Filename.null [Unix.O_RDWR; O_CLOEXEC] 0o0 in | |
let pin, pout = Unix.pipe ~cloexec:true () in | |
let pid, pid' = Unix.getpid (), get_current_process_id () in | |
Printf.eprintf "PARENT: %d %d\n%!" pid pid'; | |
let child = Unix.create_process Sys.argv.(0) [|Sys.argv.(0); "child"|] | |
null pout Unix.stderr in | |
Printf.eprintf "PARENT: CHILD: %d\n%!" child; | |
let buf = Bytes.create 8 in | |
assert (Unix.read pin buf 0 8 = 8); | |
let sub_child = Bytes.get_int64_ne buf 0 |> Int64.to_int in | |
Printf.eprintf "PARENT: SUB_CHILD: %d\n%!" sub_child; | |
begin try | |
Unix.kill child Sys.sigkill | |
with Unix.Unix_error(Unix.ESRCH, _, _) -> | |
Printf.eprintf "PARENT: couldn't kill child process\n%!" | |
end; | |
begin try | |
Unix.kill sub_child Sys.sigkill | |
with Unix.Unix_error(Unix.ESRCH, _, _) -> | |
Printf.eprintf "PARENT: couldn't kill sub_child process\n%!" | |
end | |
let child () = | |
let pid, pid' = Unix.getpid (), get_current_process_id () in | |
Printf.eprintf "CHILD: %d %d\n%!" pid pid'; | |
let sub_child = Unix.create_process Sys.argv.(0) [|Sys.argv.(0); "subchild"|] | |
Unix.stdin Unix.stdout Unix.stderr in | |
Printf.eprintf "CHILD: SUB_CHILD: %d\n%!" sub_child; | |
Unix.sleep 20 | |
let sub_child () = | |
let pid, pid' = Unix.getpid (), get_current_process_id () in | |
Printf.eprintf "SUB_CHILD: %d %d\n%!" pid pid'; | |
let buf = Bytes.create 8 in | |
Bytes.set_int64_ne buf 0 (Int64.of_int pid); | |
assert (Unix.write Unix.stdout buf 0 8 = 8); | |
Unix.sleep 20 | |
let () = | |
match Sys.argv with | |
| [|_|] -> parent () | |
| [|_; "child"|] -> child () | |
| [|_; "subchild"|] -> sub_child () | |
| _ -> invalid_arg "Sys.argv" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <processthreadsapi.h> | |
#include <caml/mlvalues.h> | |
#include <caml/memory.h> | |
CAMLprim value | |
stub_GetCurrentProcessId(value v_unit) | |
{ | |
CAMLparam1(v_unit); | |
CAMLreturn(Val_long(GetCurrentProcessId())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment