This change is the result of a code audit. The changes are not drastic, but should...
[vsys.git] / directfifowatcher.ml
1 (** directfifowatcher.ml: Routines to handle non-persistent scripts *)
2 (* Semantics:
3  *      - The 'out' descriptor must be opened first
4  *      - As soon as the backend script dies, the connection to the entry is
5  *      closed.
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.
13  *)
14
15 open Inotify
16 open Unix
17 open Globals
18 open Dirwatcher
19 open Printf
20 open Splice
21
22 let close_if_open fd = (try (ignore(close fd);) with _ -> ())
23
24 let direct_fifo_table: (string,(string*string*string*Unix.file_descr) option) Hashtbl.t = Hashtbl.create 1024
25 let pidmap: (int,string*Unix.file_descr) Hashtbl.t = Hashtbl.create 1024
26
27 let rec list_check lst elt =
28   match lst with
29     | [] -> false
30     | car::cdr -> if (car==elt) then true else list_check cdr elt
31
32 let openentry_int fifoin =
33   let fdin =
34     try openfile fifoin [O_RDONLY;O_NONBLOCK] 0o777 with 
35         e->fprintf logfd "Error opening and connecting FIFO: %s,%o\n" fifoin 0o777;flush logfd;raise e
36   in
37     fdin
38
39
40 (** Open fifos for a session. SHOULD NOt shutdown vsys if the fifos don't exist *)
41 let openentry_in rp root_dir fqp_in backend_spec =
42   match rp with 
43     | 
44     | 
45     Dirwatcher.mask_watch root_dir fqp_in;
46   let fd_in = openentry_int fqp_in in
47     Dirwatcher.unmask_watch root_dir fqp_in;
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 fqp_in = String.concat "." [fqp;"in"] in
53     openentry_in 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_in 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           (*fprintf logfd "Executing %s for slice %s\n" execpath
69            * slice_name;flush logfd;*)
70           begin
71             let len = String.length fqp_in in
72             let fqp = String.sub fqp_in 0 (len-3) in
73             let fqp_out = String.concat "." [fqp;"out"] in
74             let fifo_fdout =
75               try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
76                   _->fprintf logfd "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;flush logfd;stdout
77             in
78             ignore(sigprocmask SIG_BLOCK [Sys.sigchld]);
79             (
80             clear_nonblock fifo_fdin;
81             let pid=try Some(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> None in
82               match pid with 
83                 | Some(pid) ->
84                     if (fifo_fdout <> stdout) then close_if_open fifo_fdout;
85                     Hashtbl.add pidmap pid (fqp_in,fifo_fdout)
86                 | None ->fprintf logfd "Error executing service: %s\n" execpath;flush logfd;reopenentry fqp_in
87             );
88             ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld]);
89           end
90       | None -> ()
91
92
93 (** Make a pair of fifo entries *)
94 let mkentry fqp abspath perm uname = 
95   fprintf logfd "Making entry %s->%s\n" fqp abspath;flush logfd;
96   let fifoin=sprintf "%s.in" fqp in
97   let fifoout=sprintf "%s.out" fqp in
98     (try Unix.unlink fifoin with _ -> ());
99     (try Unix.unlink fifoout with _ -> ());
100     (try 
101        let infname =(sprintf "%s.in" fqp) in
102        let outfname =(sprintf "%s.out" fqp) in
103          Unix.mkfifo infname 0o666;
104          Unix.mkfifo outfname 0o666;
105          ( (* Make the user the owner of the pipes in a non-chroot environment *)
106            if (!Globals.nochroot) then
107              let pwentry = Unix.getpwnam uname in
108                Unix.chown infname pwentry.pw_uid pwentry.pw_gid; 
109                Unix.chown outfname pwentry.pw_uid pwentry.pw_gid
110          );
111          Success
112      with 
113          e->fprintf logfd "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;flush logfd;Failed)
114
115
116 (** Close fifos that just got removed *)
117 let closeentry fqp =
118   let fqp_in = String.concat "." [fqp;"in"] in
119   let entry = try Hashtbl.find direct_fifo_table fqp_in with Not_found -> None in
120     match entry with
121       | None -> ()
122       | Some(_,_,_,fd) -> 
123           close_if_open fd;
124           Hashtbl.remove direct_fifo_table fqp_in
125
126 let sigchld_handle s =
127   let pid,_=Unix.waitpid [Unix.WNOHANG] 0 in
128     try
129       let fqp_in,fd_out = Hashtbl.find pidmap pid in
130         begin
131         reopenentry fqp_in
132         end
133
134     with _ -> ()
135
136 let rec add_dir_watch fqp =
137   Dirwatcher.add_watch fqp [S_Open] direct_fifo_handler
138 and
139 direct_fifo_handler wd dirname evlist fname =
140   let is_event = list_check evlist in
141     if (is_event Open) then 
142       let fqp_in = String.concat "/" [dirname;fname] in
143       begin
144         connect_file fqp_in;
145         add_dir_watch dirname
146       end
147
148 let del_dir_watch fqp =
149   (* XXX Dirwatcher.del_watch fqp *)
150   ()
151
152 let initialize () =
153       Sys.set_signal Sys.sigchld (Sys.Signal_handle sigchld_handle)