Introduced direct-connected services, which offer pipe semantics.
[vsys.git] / directfifowatcher.ml
1 (** fifowatcher.ml: Routines to handle non-persistent scripts *)
2
3 open Inotify
4 open Unix
5 open Globals
6 open Dirwatcher
7 open Printf
8 open Splice
9
10 let backend_prefix = ref ""
11 let direct_fifo_table: (string,(string*string) option) Hashtbl.t = Hashtbl.create 1024
12
13 let rec list_check lst elt =
14   match lst with
15     | [] -> false
16     | car::cdr -> if (car==elt) then true else list_check cdr elt
17
18
19
20
21 (* vsys is activated when a client opens an in file *)
22 let connect_file fqp_in =
23   (* Do we care about this file? *)
24   let entry_info = try
25     Hashtbl.find direct_fifo_table fqp_in with _ -> fprintf logfd "[Alert] Access via unauthorized vsys entry: %s\n" fqp_in;flush logfd;None in
26     match entry_info with
27       | Some(execpath,slice_name) ->
28           fprintf logfd "Executing %s for slice %s\n" execpath slice_name;flush logfd;
29           begin
30             let len = String.length fqp_in in
31             let fqp = String.sub fqp_in 0 (len-3) in
32             let fqp_out = String.concat "." [fqp;"out"] in
33             let fifo_fdin =
34               try openfile fqp_in [O_RDONLY;O_NONBLOCK] 0o777 with
35                   e->fprintf logfd "Error opening and connecting FIFO: %s\n" fqp_in;flush logfd;raise e
36             in
37             let fifo_fdout =
38               try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
39                   _->fprintf logfd "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;flush logfd;stdout
40             in
41               try ignore(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> fprintf logfd "Error executing service: %s\n" execpath;flush logfd
42           end
43       | None -> ()
44
45
46 (** Make a pair of fifo entries *)
47 let mkentry fqp abspath perm uname = 
48   fprintf logfd "Making entry %s->%s\n" fqp abspath;flush logfd;
49   let fifoin=sprintf "%s.in" fqp in
50   let fifoout=sprintf "%s.out" fqp in
51     (try Unix.unlink fifoin with _ -> ());
52     (try Unix.unlink fifoout with _ -> ());
53     (try 
54        let infname =(sprintf "%s.in" fqp) in
55        let outfname =(sprintf "%s.out" fqp) in
56          Unix.mkfifo infname 0o666;
57          Unix.mkfifo outfname 0o666;
58          ( (* Make the user the owner of the pipes in a non-chroot environment *)
59            if (!Globals.nochroot) then
60              let pwentry = Unix.getpwnam uname in
61                Unix.chown infname pwentry.pw_uid pwentry.pw_gid; 
62                Unix.chown outfname pwentry.pw_uid pwentry.pw_gid
63          );
64          Success
65      with 
66          e->fprintf logfd "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;flush logfd;Failed)
67
68 (** Open fifos for a session. SHOULD NOt shutdown vsys if the fifos don't exist *)
69 let openentry fqp backend_spec =
70   let fqp_in = String.concat "." [fqp;"in"] in
71     Hashtbl.replace direct_fifo_table fqp_in (Some(backend_spec))
72
73 (** Close fifos that just got removed *)
74 let closeentry fqp =
75   let fqp_in = String.concat "." [fqp;"in"] in
76     Hashtbl.remove direct_fifo_table fqp_in
77
78 let direct_fifo_handler dirname evlist fname =
79   printf "Received event %s %s\n" dirname fname;flush Pervasives.stdout;
80   let is_event = list_check evlist in
81     if (is_event Open) then 
82       let fqp_in = String.concat "/" [dirname;fname] in
83         connect_file fqp_in
84
85 let add_dir_watch fqp =
86   Dirwatcher.add_watch fqp [S_Open] (Some(direct_fifo_handler))
87
88 let del_dir_watch fqp =
89   (* XXX Dirwatcher.del_watch fqp *)
90   ()