add -fPIC option to the C compiler, required in f31
[vsys.git] / frontend.ml
1 (* frontend.ml: Routines that implement frontend actions, such as creating directories in a slice, creating pipes etc. *)
2
3 open Printf
4 open Unix
5 open Globals
6 open Directfifowatcher
7 open Unixsocketwatcher
8
9 (** frontendhandler class: Methods to create and unlink pipes and directories 
10   @param root_dir vsys directory inside a slice
11   @param slice_name actual slice name - set with care, since the acl functionality refers to these names *)
12 class frontendHandler (root_dir,slice_name) = 
13 object(this)
14
15   (** regex indicating that the script passes fds around *)
16   val fd_regex = Str.regexp "fd_"
17
18   method is_fd_passer fname = 
19     try let _ = Str.search_forward fd_regex fname 0 
20     in 
21       true
22     with
23       | Not_found -> false 
24       | _ -> false
25
26   method get_slice_name () = slice_name
27   (** A new script was copied into the backend, make a corresponding entry in
28     the frontend.
29     @param rp Relative path of the entry in the backend
30     @param abspath Absolute path of the entry
31     @param perm Permissions of the entry at the frontend *)
32   method mkentry (rp:relpath) abspath perm = 
33     let realperm = perm land (lnot 0o111) in
34       match rp with Relpath(rel) ->
35         let fqp = String.concat "/" [root_dir;rel] in
36           if (this#is_fd_passer rel) then
37             let res = Unixsocketwatcher.mkentry fqp abspath realperm slice_name in
38               begin
39                 match res with
40                   | Success ->
41                       ()
42                   | _ -> 
43                       logprint "Could not create entry %s" abspath
44               end
45               else
46                 let res = Directfifowatcher.mkentry fqp abspath realperm slice_name in
47                   begin
48                     match res with 
49                       | Success ->
50                           Directfifowatcher.openentry root_dir fqp (abspath,slice_name)
51                       | _ -> 
52                           logprint "Could not create entry %s" abspath
53                   end
54           
55
56
57
58   (** A new directory was created at the backend, make a corresponding directory
59     at the frontend. Refer to mkentry for parameters *)
60   method mkdir rp perm =
61     match rp with Relpath(rel) ->
62       let fqp = String.concat "/" [root_dir;rel] in
63         try 
64           let s = Unix.stat fqp in
65             if (s.st_kind<>S_DIR) then
66               begin
67                 Unix.unlink fqp;
68                 Unix.mkdir fqp perm
69               end
70             else if (s.st_perm <> perm) then
71               begin
72                 Unix.rmdir fqp;
73                 Unix.mkdir fqp perm
74               end;
75         with Unix.Unix_error(_,_,_) ->
76           Unix.mkdir fqp perm;
77           Directfifowatcher.add_dir_watch fqp
78
79   (** Functions corresponding to file deletion/directory removal *)
80
81   (** *)
82   method unlink rp =
83     match rp with Relpath(rel) ->
84       let fqp = String.concat "/" [root_dir;rel] in
85       let fqp_in = String.concat "." [fqp;"in"] in
86       let fqp_out = String.concat "." [fqp;"out"] in
87       let fqp_control = String.concat "." [fqp;"out"] in
88
89         if (this#is_fd_passer rel) then
90           begin
91             Unixsocketwatcher.closeentry fqp;
92             try 
93               Unix.unlink fqp_control
94             with _ ->
95               logprint "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
96           end
97         else
98           begin
99             Directfifowatcher.closeentry fqp;
100             try 
101               Unix.unlink fqp_in;
102               Unix.unlink fqp_out
103             with _ ->
104               logprint "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
105           end
106
107   method rmdir rp =
108     match rp with Relpath(rel) ->
109       let fqp = String.concat "/" [root_dir;rel] in
110         Directfifowatcher.del_dir_watch fqp;
111         try
112           Unix.rmdir fqp
113         with _ ->
114           logprint "Hm. %s disappeared or not empty. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
115
116   initializer 
117     (
118       try 
119         let s = Unix.stat root_dir in
120           if (s.st_kind<>S_DIR) then
121             begin
122               Unix.unlink root_dir;
123               Unix.mkdir root_dir 0o700
124             end
125           else if (s.st_perm <> 0o700) then
126             begin
127               Unix.rmdir root_dir;
128               Unix.mkdir root_dir 0o700
129             end;
130       with Unix.Unix_error(_,_,_) ->
131         begin
132           try 
133             Unix.mkdir root_dir 0o700;
134           with _ -> ();
135         end);
136           Directfifowatcher.add_dir_watch root_dir
137 end