Cleaned up a bit
[vsys.git] / main.ml
1 (** main () *)
2 open Globals
3 open Printf
4 open Inotify
5 open Backend
6 open Frontend
7 open Conffile
8
9 let input_file_list = ref []
10 let cur_dir = ref ""
11 let cur_slice = ref ""
12 let daemonize = ref false
13
14 let cmdspeclist =
15   [
16     ("-daemon",Arg.Set(daemonize), "Daemonize");
17     ("-conffile",Arg.Set_string(Globals.conffile), "Config file");
18     ("-logfile",Arg.Set_string(Globals.log_filepath), "Log file");
19     ("-backend",Arg.Set_string(Globals.backend), "Backend directory");
20     ("-frontend",Arg.Tuple[Arg.String(fun s->cur_dir:=s);
21                            Arg.String(fun s->cur_slice:=s;
22                                              input_file_list:=(!cur_dir,!cur_slice)::!input_file_list)], 
23      "frontendN,slicenameN");
24     ("-nochroot",Arg.Set(Globals.nochroot), "Run in non-chroot environment");
25     ("-failsafe",Arg.Set(Globals.failsafe), "Never crash. Be stupid, but never crash. Use at your own risk.");
26   ]
27
28 let _ =
29   Arg.parse cmdspeclist (fun x->()) "Usage: vsys <list of mount points>";  
30   Globals.logfd:=open_out_gen [Open_append;Open_creat] 0o644 !log_filepath;
31   if (!Globals.backend == "") then
32     printf "Try vsys --help\n"
33   else
34     begin
35       logprint "Starting Vsys v%s\n" Globals.vsys_version;
36       if (!daemonize) then
37         begin
38           print "Daemonizing\n";
39           let child = Unix.fork () in
40             if (child <> 0) then
41               begin
42                 let pidfile = open_out !Globals.pid_filepath in
43                   fprintf pidfile "%d" child;
44                   close_out pidfile;
45                   exit(0)
46               end
47         end;
48
49       Dirwatcher.initialize ();
50       Directfifowatcher.initialize ();
51
52       if (!Globals.conffile <> "") then
53         begin
54           let frontends = Conffile.read_frontends !Globals.conffile in
55             input_file_list:=List.concat [!input_file_list;frontends]
56         end;
57
58       let felst = List.map 
59                     (fun lst->let (x,y)=lst in 
60                        logprint "Slice %s (%s)\n" x y;
61                        new frontendHandler lst) 
62                     !input_file_list in
63       let _ = new backendHandler !Globals.backend felst in
64         Fdwatcher.start_watch ()
65     end