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