add -fPIC option to the C compiler, required in f31
[vsys.git] / unixsocketwatcher.ml
index b5672b0..7663be9 100644 (file)
@@ -13,6 +13,8 @@ open Globals
 open Fdwatcher
 open Printf
 
+exception Exec_failed
+
 let close_if_open fd = (try (ignore(close fd);) with _ -> ())
 
 type control_path_name = string
@@ -36,16 +38,27 @@ let receive_event (listening_socket_spec:fname_and_fd) (_:fname_and_fd) =
         match mapping with
           |None -> logprint "Received unexpected socket event\n";()
           |Some (execpath, slice_name) ->
-              print "Execpath: %s\n" execpath;
               begin
-                let child = fork () in
+                let child = try fork () with _ -> -1 in
                   if (child == 0) then
                     begin
                       (* Child *)
                       (* Close all fds except for the socket *)
-                      ignore(execv execpath,[execpath,sprintf "%d" (Obj.magic data_socket)]);
-                      logprint "Could not execve %s" execpath
+                      let fd = Obj.magic data_socket in
+                        let _ = 
+                          (* Close fds *)
+                          for i = 3 to 1023 do
+                            if (i != fd) then close_if_open(Obj.magic i)
+                          done;
+                            execv execpath [|execpath;slice_name;sprintf "%d" fd|];
+                            raise Exec_failed
+                        (*with
+                                Unix_error(num,str1,str2)->logprint "Error %d: %s (%s)" (Obj.magic num) str1 str2;raise (Unix_error(num,str1,str2))*)
+                        in
+                            logprint "Could not execve %s" execpath
                     end
+                  else
+                    close_if_open(data_socket)
               end
           | None -> ()
     with e-> logprint "Error accepting socket\n"