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