<commit_before>open Fe_debug

let setup sock cmdargs id_to_fd_map env =
  let fd_sock_path = Printf.sprintf "/var/xapi/forker/fd_%s" 
    (Uuid.to_string (Uuid.make_uuid ())) in
  let fd_sock = Fecomms.open_unix_domain_sock () in
  Unixext.unlink_safe fd_sock_path;
  debug "About to bind to %s" fd_sock_path;
  Unix.bind fd_sock (Unix.ADDR_UNIX fd_sock_path);
  Unix.listen fd_sock 5;
  debug "bound, listening";
  let result = Unix.fork () in
  if result=0 
  then begin
    debug "Child here!";
    let result2 = Unix.fork () in
    if result2=0 then begin
      debug "Grandchild here!";
      (* Grandchild *)
      let state = {
	Child.cmdargs=cmdargs; 
	env=env;
	id_to_fd_map=id_to_fd_map; 
	ids_received=[];
	fd_sock2=None;
	finished=false;
      } in
      Child.run state sock fd_sock fd_sock_path
    end else begin
      (* Child *)
      exit 0;
    end
  end else begin
    (* Parent *)
    debug "Waiting for process %d to exit" result;
    ignore(Unix.waitpid [] result);
    Unix.close fd_sock;
    Some {Fe.fd_sock_path=fd_sock_path}
  end

let _ =
  (* Unixext.daemonize ();*)
  Sys.set_signal Sys.sigpipe (Sys.Signal_ignore);

  let main_sock = Fecomms.open_unix_domain_sock_server "/var/xapi/forker/main" in

  while true do
    try<commit_msg>CA-33440: Add [-daemon] and [-pidfile] arguments to the fork/exec daemon.<commit_after>
(** We write our PID here when we're ready to receive connections. *)
let default_pidfile = "/var/run/fe.pid"

open Fe_debug

let setup sock cmdargs id_to_fd_map env =
  let fd_sock_path = Printf.sprintf "/var/xapi/forker/fd_%s" 
    (Uuid.to_string (Uuid.make_uuid ())) in
  let fd_sock = Fecomms.open_unix_domain_sock () in
  Unixext.unlink_safe fd_sock_path;
  debug "About to bind to %s" fd_sock_path;
  Unix.bind fd_sock (Unix.ADDR_UNIX fd_sock_path);
  Unix.listen fd_sock 5;
  debug "bound, listening";
  let result = Unix.fork () in
  if result=0 
  then begin
    debug "Child here!";
    let result2 = Unix.fork () in
    if result2=0 then begin
      debug "Grandchild here!";
      (* Grandchild *)
      let state = {
	Child.cmdargs=cmdargs; 
	env=env;
	id_to_fd_map=id_to_fd_map; 
	ids_received=[];
	fd_sock2=None;
	finished=false;
      } in
      Child.run state sock fd_sock fd_sock_path
    end else begin
      (* Child *)
      exit 0;
    end
  end else begin
    (* Parent *)
    debug "Waiting for process %d to exit" result;
    ignore(Unix.waitpid [] result);
    Unix.close fd_sock;
    Some {Fe.fd_sock_path=fd_sock_path}
  end

let _ =
  let pidfile = ref default_pidfile in
  let daemonize = ref false in
 
  Arg.parse (Arg.align [
	       "-daemon", Arg.Set daemonize, "Create a daemon";
	       "-pidfile", Arg.Set_string pidfile, Printf.sprintf "Set the pid file (default \"%s\")" !pidfile;
	     ])
    (fun _ -> failwith "Invalid argument")
    "Usage: fe [-daemon] [-pidfile filename]";

  if !daemonize then Unixext.daemonize ();

  Sys.set_signal Sys.sigpipe (Sys.Signal_ignore);

  let main_sock = Fecomms.open_unix_domain_sock_server "/var/xapi/forker/main" in

  Unixext.pidfile_write !pidfile;

  (* At this point the init.d script should return and we are listening on our socket. *)

  while true do
    try
{'fraction_non_alphanumeric': 0.10705368289637952, 'fraction_numerical': 0.005930087390761548, 'mean_word_length': 3.167750325097529, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4714295', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1167, 'n_words': 311}