(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 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 slice_name = string
20
21 let unix_socket_table: (control_path_name,Unix.file_descr option) Hashtbl.t = 
22   Hashtbl.create 1024
23
24 let list_check lst elt _ =
25   let rec list_check_rec lst = 
26     match lst with
27       | [] -> false
28       | car::cdr -> 
29           if (car==elt) then
30                true
31           else
32             list_check_rec cdr
33   in
34     list_check_rec lst
35
36 let openentry_int fifoin =
37   let fdin =
38     try openfile fifoin [O_RDONLY;O_NONBLOCK] 0o777 with 
39         e->logprint "Error opening and connecting FIFO: %s,%o\n" fifoin 0o777;raise e
40   in
41     fdin
42
43 (** Open entry safely, by first masking out the file to be opened *)
44 let openentry_safe root_dir fqp_in backend_spec =
45   let restore = move_gate fqp_in in
46   let fd_in = openentry_int restore in
47     move_ungate fqp_in restore;
48     let (fqp,slice_name) = backend_spec in
49       Hashtbl.replace direct_fifo_table fqp_in (Some(root_dir,fqp,slice_name,fd_in))
50
51 let openentry root_dir fqp backend_spec =
52   let control_file = String.concat "." [fqp;"control"] in
53     openentry_safe root_dir fqp_in backend_spec
54
55 let reopenentry fifoin =
56   let entry = try Hashtbl.find direct_fifo_table fifoin with _ -> None in
57     match entry with
58       | Some(dir, fqp,slice_name,fd) -> close_if_open fd;openentry_safe dir fifoin (fqp,slice_name)
59       | None -> ()
60
61 (* vsys is activated when a client opens an in file *)
62 let connect_file fqp_in =
63   (* Do we care about this file? *)
64   let entry_info = try
65     Hashtbl.find direct_fifo_table fqp_in with _ -> None in
66     match entry_info with
67       | Some(_,execpath,slice_name,fifo_fdin) ->
68           begin
69             let len = String.length fqp_in in
70             let fqp = String.sub fqp_in 0 (len-3) in
71             let fqp_out = String.concat "." [fqp;"out"] in
72             let fifo_fdout =
73               try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
74                   _-> (* The client is opening the descriptor too fast *)
75                     sleep 1;try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
76                         _->
77                         logprint "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;stdout
78             in
79               ignore(sigprocmask SIG_BLOCK [Sys.sigchld]);
80               (
81                 clear_nonblock fifo_fdin;
82                 let pid=try Some(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> None in
83                   match pid with 
84                     | Some(pid) ->
85                         if (fifo_fdout <> stdout) then close_if_open fifo_fdout;
86                         Hashtbl.add pidmap pid (fqp_in,fifo_fdout)
87                     | None ->logprint "Error executing service: %s\n" execpath;reopenentry fqp_in
88               );
89               ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld]);
90           end
91       | None -> ()
92
93
94 (** Make a pair of fifo entries *)
95 let mkentry fqp abspath perm uname = 
96   logprint "Making control entry %s->%s\n" fqp abspath;
97   let control_filename=sprintf "%s.control" fqp in
98     try
99       let listening_socket = socket PF_UNIX SOCK_STREAM 0 in
100         (try Unix.unlink control_filename with _ -> ());
101         let socket_address = ADDR_UNIX(control_filename) in
102           bind listening_socket socket_address;
103           ( (* Make the user the owner of the pipes in a non-chroot environment *)
104             if (!Globals.nochroot) then
105               let pwentry = Unix.getpwnam uname in
106                 Unix.chown control_filename pwentry.pw_uid pwentry.pw_gid
107           );
108           Success
109     with 
110         e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;Failed)
111
112
113 (** Close fifos that just got removed *)
114 let closeentry fqp =
115   let control_filename = String.concat "." [fqp;"control"] in
116   let entry = try Hashtbl.find direct_fifo_table control_filename with Not_found -> None in
117     match entry with
118       | None -> ()
119       | Some(_,_,_,fd) -> 
120           shutdown fd SHUTDOWN_ALL;
121           close_if_open fd
122
123 let rec add_dir_watch fqp =
124   Dirwatcher.add_watch fqp [S_Open] direct_fifo_handler
125 and
126     direct_fifo_handler wd dirname evlist fname =
127   let is_event = list_check evlist in
128     if (is_event Open Attrib) then 
129       let fqp_in = String.concat "/" [dirname;fname] in
130         connect_file fqp_in
131
132 let del_dir_watch fqp =
133   ()
134
135 let initialize () =
136   Sys.set_signal Sys.sigchld (Sys.Signal_handle sigchld_handle)