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.
16 let close_if_open fd = (try (ignore(close fd);) with _ -> ())
18 type in_pathname = string
19 type directory = string
20 type base_pathname = string
21 type slice_name = string
23 let unix_socket_table: (in_pathname,(directory*base_pathname*slice_name*Unix.file_descr) option) Hashtbl.t =
26 let pidmap: (int,in_pathname * Unix.file_descr) Hashtbl.t = Hashtbl.create 1024
29 let tmpfname=String.concat "." [fname;"tmp"] in
30 Unix.rename fname tmpfname;
33 let move_ungate fname restore =
34 Unix.rename restore fname
36 let list_check lst elt _ =
37 let rec list_check_rec lst =
48 let openentry_int fifoin =
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
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))
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
67 let reopenentry fifoin =
68 let entry = try Hashtbl.find direct_fifo_table fifoin with _ -> None in
70 | Some(dir, fqp,slice_name,fd) -> close_if_open fd;openentry_safe dir fifoin (fqp,slice_name)
73 (* vsys is activated when a client opens an in file *)
74 let connect_file fqp_in =
75 (* Do we care about this file? *)
77 Hashtbl.find direct_fifo_table fqp_in with _ -> None in
79 | Some(_,execpath,slice_name,fifo_fdin) ->
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
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
89 logprint "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;stdout
91 ignore(sigprocmask SIG_BLOCK [Sys.sigchld]);
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
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
101 ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld]);
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
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
122 e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;Failed)
125 (** Close fifos that just got removed *)
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
133 Hashtbl.remove direct_fifo_table fqp_in
135 let sigchld_handle s =
136 let pid,_=Unix.waitpid [Unix.WNOHANG] 0 in
138 let fqp_in,fd_out = Hashtbl.find pidmap pid in
144 let rec add_dir_watch fqp =
145 Dirwatcher.add_watch fqp [S_Open] direct_fifo_handler
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
153 let del_dir_watch fqp =
157 Sys.set_signal Sys.sigchld (Sys.Signal_handle sigchld_handle)