(no commit message)
[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 let close_if_open fd = (try (ignore(close fd);) with _ -> ())
17
18 type control_path_name = string
19 type exec_path_name = string
20 type slice_name = string
21
22 let unix_socket_table: (control_path_name,(exec_path_name*slice_name*Unix.file_descr) option) Hashtbl.t = 
23   Hashtbl.create 1024
24
25 let receive_event (listening_socket_spec:fname_and_fd) (_:fname_and_fd) =
26     let (_,listening_socket) = listening_socket_spec in
27   try 
28     let (data_socket, addr) = accept listening_socket in
29       match addr with 
30         | ADDR_UNIX(fname) ->
31             begin
32               let entry_info = 
33                 try
34                   Hashtbl.find unix_socket_table fname 
35                 with _ -> logprint "Received unexpected socket event from %s\n" fname;None in
36                 match entry_info with
37                   | Some(execpath,slice_name,fd) ->
38                       begin
39                         let child = fork () in
40                           if (child == 0) then
41                                begin
42                                  (*Child*)
43                                  (* Close all fds except for the socket *)
44                                  ignore(execv execpath,[execpath]);
45                                  logprint "Could not execve %s" execpath
46                                end
47                       end
48                   | None -> ()
49             end
50         | _ -> logprint "Serious error! Got a non UNIX connection over a UNIX socket\n"
51   with e-> logprint "Error accepting socket\n"
52
53 (** Make a pair of fifo entries *)
54 let mkentry fqp exec_fqp perm slice_name = 
55   logprint "Making control entry %s->%s\n" fqp exec_fqp;
56   let control_filename=sprintf "%s.control" fqp in
57     try
58       let listening_socket = socket PF_UNIX SOCK_STREAM 0 in
59         (try Unix.unlink control_filename with _ -> ());
60         let socket_address = ADDR_UNIX(control_filename) in
61           bind listening_socket socket_address;
62           listen listening_socket 10;
63           ( (* Make the user the owner of the pipes in a non-chroot environment *)
64             if (!Globals.nochroot) then
65               let pwentry = Unix.getpwnam slice_name in
66                 Unix.chown control_filename pwentry.pw_uid pwentry.pw_gid
67           );
68           Hashtbl.replace unix_socket_table control_filename (Some(exec_fqp,slice_name,listening_socket));
69           Fdwatcher.add_fd (None,listening_socket) (None,listening_socket) receive_event;
70           Success
71     with 
72         e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp exec_fqp;Failed
73
74   
75 (** Close sockets that just got removed *)
76 let closeentry fqp =
77   let control_filename = String.concat "." [fqp;"control"] in
78   let entry = try Hashtbl.find unix_socket_table control_filename with Not_found -> None in
79     match entry with
80       | None -> ()
81       | Some(_,_,fd) -> 
82           shutdown fd SHUTDOWN_ALL;
83           close_if_open fd;
84           Hashtbl.remove unix_socket_table control_filename
85
86
87
88 let initialize () =
89   ()