Checking in daemonization, writing to a logfile, and saving the pid
[vsys.git] / main.ml
1 (** main () *)
2 open Globals
3 open Printf
4 open Inotify
5 open Backend
6 open Frontend
7 open Fifowatcher
8 open Conffile
9
10 let input_file_list = ref []
11 let cur_dir = ref ""
12 let cur_slice = ref ""
13 let daemonize = ref false
14
15 let cmdspeclist =
16   [
17     ("-daemon",Arg.Set(daemonize), "Daemonize");
18     ("-conffile",Arg.Set_string(Globals.conffile), "Config file");
19     ("-backend",Arg.Set_string(Globals.backend), "Backend directory");
20     ("-frontend",Arg.Tuple[Arg.String(fun s->cur_dir:=s);Arg.String(fun s->cur_slice:=s;input_file_list:=(!cur_dir,!cur_slice)::!input_file_list)], "frontendN,slicenameN");
21     ("-nochroot",Arg.Set(Globals.nochroot), "Run in non-chroot environment")
22   ]
23
24 let cont = ref true
25
26 let _ =
27   printf "Vsys v%s\n" Globals.vsys_version;flush stdout;
28   Arg.parse cmdspeclist (fun x->()) "Usage: vsys <list of mount points>";  
29   if (!Globals.backend == "") then
30       printf "Try vsys --help\n"
31   else
32     begin
33       if (!daemonize) then
34         begin
35           printf "Daemonizing\n";flush Pervasives.stdout;
36         let child = Unix.fork () in
37           if (child <> 0) then
38             begin
39                 let pidfile = open_out !Globals.pid_filepath in
40                   fprintf pidfile "%d" child;
41                     close_out pidfile;
42                     exit(0)
43             end
44           end;
45
46             Dirwatcher.initialize ();
47             Fifowatcher.initialize ();
48             if (!Globals.conffile <> "") then
49               begin
50               let frontends = Conffile.read_frontends !Globals.conffile in
51                 input_file_list:=List.concat [!input_file_list;frontends]
52               end;
53
54             let felst = List.map (fun lst->let (x,y)=lst in printf "Slice %s (%s)\n" x y;flush logfd;new frontendHandler lst) !input_file_list in
55                 let _ = new backendHandler !Globals.backend felst in
56                  Fdwatcher.start_watch ()
57     end