Starting commits for fd passing support
[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
8 (** frontendhandler class: Methods to create and unlink pipes and directories 
9   @param root_dir vsys directory inside a slice
10   @param slice_name actual slice name - set with care, since the acl functionality refers to these names *)
11 class frontendHandler (root_dir,slice_name) = 
12 object(this)
13
14   (** regex indicating that the script passes fds around *)
15   val fd_regex = Str.regexp "^fd_"
16
17   method is_fd_passer fname = Str.string_match fd_regex fname 0
18   method get_slice_name () = slice_name
19   (** A new script was copied into the backend, make a corresponding entry in
20     the frontend.
21     @param rp Relative path of the entry in the backend
22     @param abspath Absolute path of the entry
23     @param perm Permissions of the entry at the frontend *)
24   method mkentry (rp:relpath) abspath perm = 
25     let realperm = perm land (lnot 0o111) in
26       match rp with Relpath(rel) ->
27         let fqp = String.concat "/" [root_dir;rel] in
28         let res = Directfifowatcher.mkentry fqp abspath realperm slice_name in
29           begin
30             match res with 
31               | Success ->
32                   Directfifowatcher.openentry root_dir fqp (abspath,slice_name)
33               | _ -> 
34                   logprint "Could not create entry %s" abspath
35           end;
36           if (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                       Unixsocketwatcher.openentry root_dir fqp (abspath, slice_name)
42                   | _ -> 
43                       logprint "Could not create entry %s" abspath
44               end
45
46
47
48
49   (** A new directory was created at the backend, make a corresponding directory
50     at the frontend. Refer to mkentry for parameters *)
51   method mkdir rp perm =
52     match rp with Relpath(rel) ->
53       let fqp = String.concat "/" [root_dir;rel] in
54         try 
55           let s = Unix.stat fqp in
56             if (s.st_kind<>S_DIR) then
57               begin
58                 Unix.unlink fqp;
59                 Unix.mkdir fqp perm
60               end
61             else if (s.st_perm <> perm) then
62               begin
63                 Unix.rmdir fqp;
64                 Unix.mkdir fqp perm
65               end;
66         with Unix.Unix_error(_,_,_) ->
67           Unix.mkdir fqp perm;
68           Directfifowatcher.add_dir_watch fqp
69
70   (** Functions corresponding to file deletion/directory removal *)
71
72   (** *)
73   method unlink rp =
74     match rp with Relpath(rel) ->
75       let fqp = String.concat "/" [root_dir;rel] in
76       let fqp_in = String.concat "." [fqp;"in"] in
77       let fqp_out = String.concat "." [fqp;"out"] in
78         Directfifowatcher.closeentry fqp;
79         try 
80           Unix.unlink fqp_in;
81           Unix.unlink fqp_out
82         with _ ->
83           logprint "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
84
85   method rmdir rp =
86     match rp with Relpath(rel) ->
87       let fqp = String.concat "/" [root_dir;rel] in
88         Directfifowatcher.del_dir_watch fqp;
89         try
90           Unix.rmdir fqp
91         with _ ->
92           logprint "Hm. %s disappeared or not empty. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
93
94   initializer 
95     (
96       try 
97         let s = Unix.stat root_dir in
98           if (s.st_kind<>S_DIR) then
99             begin
100               Unix.unlink root_dir;
101               Unix.mkdir root_dir 0o700
102             end
103           else if (s.st_perm <> 0o700) then
104             begin
105               Unix.rmdir root_dir;
106               Unix.mkdir root_dir 0o700
107             end;
108       with Unix.Unix_error(_,_,_) ->
109         begin
110           try 
111             Unix.mkdir root_dir 0o700;
112           with _ -> ();
113         end);
114           Directfifowatcher.add_dir_watch root_dir
115 end