1 (** directfifowatcher.ml: Routines to handle non-persistent scripts *)
3 * - The 'out' descriptor must be opened first
4 * - As soon as the backend script dies, the connection to the entry is
6 * - To avoid user-inflicted pain, all entries are opened at the time
7 * that they are created. Reopening these entries is a little complicated
8 * but nevertheless sound:
9 * * When a script dies, its fd is reopened
10 * * If a script fails to execute, its fd is closed and reopened to
11 * beat a race that can happen when the user closes the connection
12 * before the script can be launched.
22 let close_if_open fd = (try (ignore(close fd);) with _ -> ())
24 type in_pathname = string
25 type directory = string
26 type base_pathname = string
27 type slice_name = string
29 let direct_fifo_table: (in_pathname,(directory*base_pathname*slice_name*Unix.file_descr) option) Hashtbl.t =
32 let pidmap: (int,in_pathname * Unix.file_descr) Hashtbl.t = Hashtbl.create 1024
35 let tmpfname=String.concat "." [fname;"tmp"] in
37 Unix.rename fname tmpfname;
40 let move_ungate fname restore =
42 Unix.rename restore fname
44 let list_check lst elt _ =
45 let rec list_check_rec lst =
56 let fs_openentry fifoin =
58 try openfile fifoin [O_RDONLY;O_NONBLOCK] 0o777 with
59 e->logprint "Error opening and connecting FIFO: %s,%o\n" fifoin 0o777;raise e
63 (** Open entry safely, by first masking out the file to be opened *)
64 let openentry_safe root_dir fqp_in backend_spec =
65 let restore = move_gate fqp_in in
66 let fd_in = fs_openentry restore in
67 move_ungate fqp_in restore;
68 let (fqp,slice_name) = backend_spec in
69 Hashtbl.replace direct_fifo_table fqp_in (Some(root_dir,fqp,slice_name,fd_in))
71 let openentry root_dir fqp backend_spec =
72 let fqp_in = String.concat "." [fqp;"in"] in
73 openentry_safe root_dir fqp_in backend_spec
75 let reopenentry fifoin =
76 let entry = try Hashtbl.find direct_fifo_table fifoin with _ -> None in
78 | Some(dir, fqp,slice_name,fd) -> close_if_open fd;openentry_safe dir fifoin (fqp,slice_name)
81 (* vsys is activated when a client opens an in file *)
82 let connect_file fqp_in =
83 (* Do we care about this file? *)
85 Hashtbl.find direct_fifo_table fqp_in with _ -> None in
87 | Some(_,execpath,slice_name,fifo_fdin) ->
89 let len = String.length fqp_in in
90 let fqp = String.sub fqp_in 0 (len-3) in
91 let fqp_out = String.concat "." [fqp;"out"] in
93 try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
94 _-> (* The client is opening the descriptor too fast *)
95 sleep 1;try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
97 logprint "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;stdout
99 ignore(sigprocmask SIG_BLOCK [Sys.sigchld]);
101 clear_nonblock fifo_fdin;
102 let pid=try Some(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> None in
105 if (fifo_fdout <> stdout) then close_if_open fifo_fdout;
106 Hashtbl.add pidmap pid (fqp_in,fifo_fdout)
107 | None ->logprint "Error executing service: %s\n" execpath;reopenentry fqp_in
109 ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld]);
114 (** Make a pair of fifo entries *)
115 let mkentry fqp abspath perm uname =
116 logprint "Making entry %s->%s\n" fqp abspath;
117 let fifoin=sprintf "%s.in" fqp in
118 let fifoout=sprintf "%s.out" fqp in
119 (try Unix.unlink fifoin with _ -> ());
120 (try Unix.unlink fifoout with _ -> ());
122 let infname =(sprintf "%s.in" fqp) in
123 let outfname =(sprintf "%s.out" fqp) in
125 Unix.mkfifo infname 0o666;
126 Unix.mkfifo outfname 0o666;
127 ( (* Make the user the owner of the pipes in a non-chroot environment *)
128 if (!Globals.nochroot) then
129 let pwentry = Unix.getpwnam uname in
131 Unix.chown infname pwentry.pw_uid pwentry.pw_gid;
132 Unix.chown outfname pwentry.pw_uid pwentry.pw_gid
136 e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;Failed)
139 (** Close fifos that just got removed *)
141 let fqp_in = String.concat "." [fqp;"in"] in
142 let entry = try Hashtbl.find direct_fifo_table fqp_in with Not_found -> None in
147 Hashtbl.remove direct_fifo_table fqp_in
149 let sigchld_handle s =
150 let rec reap_all_processes () =
151 let pid,_= try Unix.waitpid [Unix.WNOHANG] 0 with _ -> (-1,WEXITED(-1)) in
156 let fqp_in,fd_out = Hashtbl.find pidmap pid in
162 reap_all_processes ()
167 (** The backendhandler class: defines event handlers for events in
168 the backend backend directory.
169 @param dir_root The location of the backend in the server context (eg. root context for vservers)
170 @param frontend_list List of frontends to serve with this backend
172 let rec add_dir_watch fqp =
173 Dirwatcher.add_watch fqp [S_Open] direct_fifo_handler
175 direct_fifo_handler wd dirname evlist fname =
176 let is_event = list_check evlist in
177 if (is_event Open Attrib) then
178 let fqp_in = String.concat "/" [dirname;fname] in
181 let del_dir_watch fqp =
185 Sys.set_signal Sys.sigchld (Sys.Signal_handle sigchld_handle)