contd...
[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 Dirwatcher
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 (** Make a pair of fifo entries *)
26 let mkentry fqp exec_fqp perm uname = 
27   logprint "Making control entry %s->%s\n" fqp exec_fqp;
28   let control_filename=sprintf "%s.control" fqp in
29     try
30       let listening_socket = socket PF_UNIX SOCK_STREAM 0 in
31         (try Unix.unlink control_filename with _ -> ());
32         let socket_address = ADDR_UNIX(control_filename) in
33           bind listening_socket socket_address;
34           listen listening_socket 10;
35           ( (* Make the user the owner of the pipes in a non-chroot environment *)
36             if (!Globals.nochroot) then
37               let pwentry = Unix.getpwnam uname in
38                 Unix.chown control_filename pwentry.pw_uid pwentry.pw_gid
39           );
40           Hashtbl.replace unix_socket_table control_file (Some(exec_fqp,slice_name,listening_socket));
41           Success
42     with 
43         e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;Failed
44
45 (** Close sockets that just got removed *)
46 let closeentry fqp =
47   let control_filename = String.concat "." [fqp;"control"] in
48   let entry = try Hashtbl.find direct_fifo_table control_filename with Not_found -> None in
49     match entry with
50       | None -> ()
51       | Some(_,_,_,fd) -> 
52           shutdown fd SHUTDOWN_ALL;
53           close_if_open fd;
54           Hashtbl.remove unix_socket_table control_filename
55
56 (* vsys is activated when a client opens an in file *)
57 let connect_file fqp_in =
58   (* Do we care about this file? *)
59   let entry_info = try
60     Hashtbl.find direct_fifo_table fqp_in with _ -> None in
61     match entry_info with
62       | Some(_,execpath,slice_name,fifo_fdin) ->
63           begin
64             let len = String.length fqp_in in
65             let fqp = String.sub fqp_in 0 (len-3) in
66             let fqp_out = String.concat "." [fqp;"out"] in
67             let fifo_fdout =
68               try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
69                   _-> (* The client is opening the descriptor too fast *)
70                     sleep 1;try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
71                         _->
72                         logprint "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;stdout
73             in
74               ignore(sigprocmask SIG_BLOCK [Sys.sigchld]);
75               (
76                 clear_nonblock fifo_fdin;
77                 let pid=try Some(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> None in
78                   match pid with 
79                     | Some(pid) ->
80                         if (fifo_fdout <> stdout) then close_if_open fifo_fdout;
81                         Hashtbl.add pidmap pid (fqp_in,fifo_fdout)
82                     | None ->logprint "Error executing service: %s\n" execpath;reopenentry fqp_in
83               );
84               ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld]);
85           end
86       | None -> ()
87
88
89
90
91 let initialize () =
92   ()