*** empty log message ***
[vsys.git] / backend.ml
1 (** backend.ml: 
2   Defines handlers for events related to the backend directory, where the
3        scripts are stored. Eg. a new script results in a new part of pipes in
4        the frontend etc. These handlers are defined in the backendHandler
5        class.
6
7   @author Sapan Bhatia <sapanb\@cs.princeton.edu>
8   *)
9
10 open Unix
11 open Globals
12 open Dirwatcher
13 open Inotify
14 open Fifowatcher
15 open Frontend
16 open Printf
17
18 (** Helper functions:
19
20 *)
21
22 (** Turn an absolute path into a relative path. *)
23 let delete_prefix prefix str =
24   let len = String.length str in
25   let plen = String.length prefix in
26     if (String.sub str 0 plen <> prefix) 
27     then 
28       (* XXX can a user make this happen? *)
29       raise Bad_path
30     else
31         Relpath(String.sub str (plen+1) (len-plen-1))
32
33 let rec list_check lst elt =
34   match lst with
35     | [] -> false
36     | car::cdr -> if (car==elt) then true else list_check cdr elt
37
38
39 (** The backendhandler class: defines event handlers for events in
40 the backend backend directory.
41   @param dir_root The location of the backend in the server context (eg. root context for vservers)
42   @param frontend_list List of frontends to serve with this backend
43   *)
44 class backendHandler dir_root (frontend_lst: frontendHandler list) =
45    let mk_rel_path = delete_prefix dir_root in object(this)
46
47      (** Regular expression that defines a legal script name. Filter out
48        * temporary files using this *)
49      val file_regexp = Str.regexp "[a-zA-Z][a-zA-Z0-9_\.]*"
50      val acl_file_regexp = Str.regexp ".*acl$"
51
52      val dir_regexp = Str.regexp "^dir_";
53      val acl_regexp = Str.regexp ".*_.*";
54
55      (** Somebody created a new directory *)
56      (* XXX Race condition here *)
57      method private new_dir slice_list fqp func =
58        let s = Unix.stat fqp in
59          List.iter 
60            (fun frontend->
61               frontend#mkdir (mk_rel_path fqp) (s.st_perm);
62               Dirwatcher.add_watch fqp [S_Create;S_Delete] (Some(func)))
63            slice_list
64
65      (** Somebody copied in a new script *)
66      (* XXX Race condition here *)
67      method private new_script slice_list fqp =
68        let s = Unix.stat fqp in
69          List.iter (fun frontend->
70                       frontend#mkentry (mk_rel_path fqp) fqp (s.st_perm)) slice_list 
71
72      method private make_filter acl_fqp =
73        let filter = Hashtbl.create 16 in
74        try 
75          let acl_file = open_in acl_fqp in
76          let rec read_acl cur_filter = 
77            let next_item = 
78              try Some(input_line acl_file)
79              with _ -> None
80            in
81              match next_item with
82                | None -> cur_filter
83                | Some(item) -> 
84                    Hashtbl.add cur_filter item true;
85                    read_acl cur_filter
86          in
87            Some(read_acl filter)
88        with _ ->
89          None
90
91      (** Gets called every time there's an inotify event at the backend 
92        @param dirname Name of the backend directory
93        @param evlist Description of what happened
94        @param fname Name of the file that the event applies to
95      *)
96      method handle_dir_event dirname evlist fname = 
97        let fqp = String.concat "/" [dirname;fname] in
98          if ((Str.string_match file_regexp fname 0) && not (Str.string_match acl_file_regexp fname 0)) then  
99            begin
100              (* Filter frontend list based on acl *)
101              let acl_fqp = String.concat "." [fqp;"acl"] in
102              let acl_filter = this#make_filter acl_fqp in
103              let slice_list = 
104                match acl_filter with
105                  | None -> frontend_lst 
106                  | Some(filter) -> List.filter (fun fe->Hashtbl.mem filter (fe#get_slice_name ())) frontend_lst 
107              in 
108              let is_event = list_check evlist in
109                if (is_event Create) then
110                  begin
111                    if (is_event Isdir) then
112                      begin
113                        this#new_dir slice_list fqp this#handle_dir_event
114                      end 
115                    else
116                      (* It's a new script *)
117                      begin
118                        (*
119                         if (Str.string_match dir_regexp fname 0) then
120                         let fqp = String.concat "/" [dirname;String.sub fname 4 ((String.length fname)-4+1)]  in 
121                         let real_fqp = String.concat "/" [dirname;fname]  in 
122                         this#new_dir fqp this#handle_spool_event;
123                         Hashtbl.add spools fqp real_fqp
124                         else*)
125                        this#new_script slice_list fqp
126                      end
127                  end
128                else if (is_event Delete) then
129                  begin
130                    if (is_event Isdir) then
131                      begin
132                        (*this#rm_watch fqp;*)
133                        List.iter (fun frontend->
134                                     frontend#rmdir (mk_rel_path fqp)) slice_list
135                      end
136                    else List.iter (fun frontend ->
137                                      frontend#unlink (mk_rel_path fqp)) slice_list
138                  end
139            end
140          else (* regex not matched *)
141            ()
142
143      (** Initializer - build the initial tree based on the contents of /vsys *)
144      initializer 
145      let rec build_initial_tree dir =
146        let dir_handle = opendir dir in
147        let cont = ref true in
148          while (!cont) do
149            try 
150              let curfile = readdir dir_handle  in
151              let fqp = String.concat "/" [dir;curfile] in
152              let acl_fqp = String.concat "." [fqp;"acl"] in
153              let acl_filter = this#make_filter acl_fqp in
154              let slice_list = 
155                match acl_filter with
156                  | None -> frontend_lst 
157                  | Some(filter) -> List.filter (fun fe->Hashtbl.mem filter (fe#get_slice_name ())) frontend_lst 
158              in
159                if (Str.string_match file_regexp curfile 0 && not (Str.string_match acl_file_regexp curfile 0)) then
160                  let s = Unix.stat fqp in
161                    begin
162                      match s.st_kind with
163                        | S_DIR ->
164                            this#new_dir slice_list fqp this#handle_dir_event;
165                            build_initial_tree fqp;
166                        | S_REG ->
167                            this#new_script slice_list fqp
168                        | _ ->
169                            printf "Don't know what to do with %s\n" curfile;flush Pervasives.stdout
170                    end
171            with 
172                _->cont:=false;()
173          done 
174      in
175        begin
176          build_initial_tree dir_root;
177          Dirwatcher.add_watch dir_root [S_Create;S_Delete] (Some(this#handle_dir_event));
178        end
179    end