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