Cleaned up a bit
[vsys.git] / unixsocketwatcher.ml
1 (** unixsocketwathcer.ml: Routines to handle unix sockets for fd passing *)
2 (* Semantics for the client C, script S and Vsys, V
3  * - V creates a UNIX socket and listens on it, adding a watch
4  * - C connects to the socket
5  * - V accepts the connection, forks, execve()s S and gets out of the way
6  * - S sends an fd to C and closes the connection
7  * - If one of S or C dies, then the other gets a SIGPIPE, Vsys gets a sigchld,
8  * either way, Vsys should survive the transaction.
9  *)
10
11 open Unix
12 open Globals
13 open Fdwatcher
14 open Printf
15
16 exception Exec_failed
17
18 let close_if_open fd = (try (ignore(close fd);) with _ -> ())
19
20 type control_path_name = string
21 type exec_path_name = string
22 type slice_name = string
23
24 let unix_socket_table_fname: (control_path_name,Unix.file_descr option) Hashtbl.t = 
25   Hashtbl.create 1024
26
27 let unix_socket_table_fd: (Unix.file_descr, (exec_path_name * slice_name) option) Hashtbl.t =
28   Hashtbl.create 1024
29
30 let receive_event (listening_socket_spec:fname_and_fd) (_:fname_and_fd) =
31   let (_,listening_socket) = listening_socket_spec in
32     try 
33       let (data_socket, _) = accept listening_socket in
34       let (mapping) = 
35         try
36           Hashtbl.find unix_socket_table_fd listening_socket
37         with _ -> None in
38         match mapping with
39           |None -> logprint "Received unexpected socket event\n";()
40           |Some (execpath, slice_name) ->
41               begin
42                 let child = try fork () with _ -> -1 in
43                   if (child == 0) then
44                     begin
45                       (* Child *)
46                       (* Close all fds except for the socket *)
47                       let fd = Obj.magic data_socket in
48                         let _ = 
49                           (* Close fds *)
50                           for i = 3 to 1023 do
51                             if (i != fd) then close_if_open(Obj.magic i)
52                           done;
53                             execv execpath [|execpath;slice_name;sprintf "%d" fd|];
54                             raise Exec_failed
55                         (*with
56                                 Unix_error(num,str1,str2)->logprint "Error %d: %s (%s)" (Obj.magic num) str1 str2;raise (Unix_error(num,str1,str2))*)
57                         in
58                             logprint "Could not execve %s" execpath
59                     end
60                   else
61                     close_if_open(data_socket)
62               end
63           | None -> ()
64     with e-> logprint "Error accepting socket\n"
65
66 (** Make a pair of fifo entries *)
67 let mkentry fqp exec_fqp perm slice_name = 
68   logprint "Making control entry %s->%s\n" fqp exec_fqp;
69   let control_filename=sprintf "%s.control" fqp in
70     try
71       let listening_socket = socket PF_UNIX SOCK_STREAM 0 in
72         (try Unix.unlink control_filename with _ -> ());
73         let socket_address = ADDR_UNIX(control_filename) in
74           bind listening_socket socket_address;
75           listen listening_socket 10;
76           ( (* Make the user the owner of the pipes in a non-chroot environment *)
77             if (!Globals.nochroot) then
78               let pwentry = Unix.getpwnam slice_name in
79                 Unix.chown control_filename pwentry.pw_uid pwentry.pw_gid
80           );
81           Hashtbl.replace unix_socket_table_fname control_filename (Some(listening_socket));
82           Hashtbl.replace unix_socket_table_fd listening_socket (Some(exec_fqp,slice_name));
83           Fdwatcher.add_fd (None,listening_socket) (None,listening_socket) receive_event;
84           Success
85     with 
86         e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp exec_fqp;Failed
87
88   
89 (** Close sockets that just got removed *)
90 let closeentry fqp =
91   let control_filename = String.concat "." [fqp;"control"] in
92   let entry = try Hashtbl.find unix_socket_table_fname control_filename with Not_found -> None in
93     match entry with
94       | None -> ()
95       | Some(fd) -> 
96           Hashtbl.remove unix_socket_table_fd fd;
97           shutdown fd SHUTDOWN_ALL;
98           close_if_open fd;
99           Hashtbl.remove unix_socket_table_fname control_filename
100
101
102
103 let initialize () =
104   ()