From: Sapan Bhatia Date: Mon, 3 Mar 2008 17:49:48 +0000 (+0000) Subject: Next batch of tweaks X-Git-Tag: vsys-0.7-4~21 X-Git-Url: http://git.onelab.eu/?p=vsys.git;a=commitdiff_plain;h=47e86c6701f0bfe11a8a8784cd06db3283a57775 Next batch of tweaks --- diff --git a/backend.ml b/backend.ml index 690f1c6..d85ee7d 100644 --- a/backend.ml +++ b/backend.ml @@ -60,7 +60,7 @@ class backendHandler dir_root (frontend_lst: frontendHandler list) = (fun frontend-> try begin frontend#mkdir (mk_rel_path fqp) (s.st_perm); - Dirwatcher.add_watch fqp [S_Create;S_Delete] (Some(func)) + Dirwatcher.add_watch fqp [S_Create;S_Delete] func end with _ -> fprintf logfd "Could not create %s. Looks like a slice shot itself in the foot\n" fqp;flush logfd; @@ -98,7 +98,7 @@ class backendHandler dir_root (frontend_lst: frontendHandler list) = @param evlist Description of what happened @param fname Name of the file that the event applies to *) - method handle_dir_event dirname evlist fname = + method handle_dir_event _ dirname evlist fname = let fqp = String.concat "/" [dirname;fname] in if ((Str.string_match file_regexp fname 0) && not (Str.string_match acl_file_regexp fname 0)) then begin @@ -179,6 +179,6 @@ class backendHandler dir_root (frontend_lst: frontendHandler list) = in begin build_initial_tree dir_root; - Dirwatcher.add_watch dir_root [S_Create;S_Delete] (Some(this#handle_dir_event)); + Dirwatcher.add_watch dir_root [S_Create;S_Delete] (this#handle_dir_event); end end diff --git a/directfifowatcher.ml b/directfifowatcher.ml index 3f89ad4..0ff74b3 100644 --- a/directfifowatcher.ml +++ b/directfifowatcher.ml @@ -1,4 +1,9 @@ -(** fifowatcher.ml: Routines to handle non-persistent scripts *) +(** directfifowatcher.ml: Routines to handle non-persistent scripts *) +(* Semantics: + * - The 'out' descriptor must be opened first + * - As soon as the backend script dies, the connection to the entry is + * closed. + *) open Inotify open Unix @@ -15,21 +20,19 @@ let rec list_check lst elt = | [] -> false | car::cdr -> if (car==elt) then true else list_check cdr elt - - - (* vsys is activated when a client opens an in file *) -let connect_file fqp_in = +let connect_file mask_events fqp_out = (* Do we care about this file? *) let entry_info = try - Hashtbl.find direct_fifo_table fqp_in with _ -> fprintf logfd "[Alert] Access via unauthorized vsys entry: %s\n" fqp_in;flush logfd;None in + Hashtbl.find direct_fifo_table fqp_out with _ -> None in match entry_info with | Some(execpath,slice_name) -> fprintf logfd "Executing %s for slice %s\n" execpath slice_name;flush logfd; begin - let len = String.length fqp_in in - let fqp = String.sub fqp_in 0 (len-3) in - let fqp_out = String.concat "." [fqp;"out"] in + let len = String.length fqp_out in + let fqp = String.sub fqp_out 0 (len-4) in + mask_events true; + let fqp_in = String.concat "." [fqp;"in"] in let fifo_fdin = try openfile fqp_in [O_RDONLY;O_NONBLOCK] 0o777 with e->fprintf logfd "Error opening and connecting FIFO: %s\n" fqp_in;flush logfd;raise e @@ -38,7 +41,10 @@ let connect_file fqp_in = try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with _->fprintf logfd "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;flush logfd;stdout in - try ignore(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> fprintf logfd "Error executing service: %s\n" execpath;flush logfd + try ignore(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout); with e -> begin fprintf logfd "Error executing service: %s\n" execpath;flush logfd end; + close fifo_fdin; + close fifo_fdout; + mask_events false; end | None -> () @@ -67,23 +73,25 @@ let mkentry fqp abspath perm uname = (** Open fifos for a session. SHOULD NOt shutdown vsys if the fifos don't exist *) let openentry fqp backend_spec = - let fqp_in = String.concat "." [fqp;"in"] in + let fqp_in = String.concat "." [fqp;"out"] in Hashtbl.replace direct_fifo_table fqp_in (Some(backend_spec)) (** Close fifos that just got removed *) let closeentry fqp = - let fqp_in = String.concat "." [fqp;"in"] in + let fqp_in = String.concat "." [fqp;"out"] in Hashtbl.remove direct_fifo_table fqp_in -let direct_fifo_handler dirname evlist fname = - printf "Received event %s %s\n" dirname fname;flush Pervasives.stdout; +let direct_fifo_handler wd dirname evlist fname = + let mask_events flag = + if (flag) then Dirwatcher.mask_events wd else Dirwatcher.unmask_events wd + in let is_event = list_check evlist in if (is_event Open) then - let fqp_in = String.concat "/" [dirname;fname] in - connect_file fqp_in + let fqp_out = String.concat "/" [dirname;fname] in + connect_file mask_events fqp_out let add_dir_watch fqp = - Dirwatcher.add_watch fqp [S_Open] (Some(direct_fifo_handler)) + Dirwatcher.add_watch fqp [S_Open] direct_fifo_handler let del_dir_watch fqp = (* XXX Dirwatcher.del_watch fqp *) diff --git a/dirwatcher.ml b/dirwatcher.ml index e43de77..643830e 100644 --- a/dirwatcher.ml +++ b/dirwatcher.ml @@ -9,6 +9,8 @@ open Globals * leaks - fix implementation of rmdir accordingly *) +type 'a handlertype = Nohandler | Activehandler of 'a | Maskedhandler of 'a + let wdmap = Hashtbl.create 1024 let fd = Inotify.init () @@ -22,9 +24,27 @@ let handle_dir_event dirname evlist str = flush logfd let add_watch dir events handler = - printf "Adding watch for %s\n" dir;flush Pervasives.stdout; let wd = Inotify.add_watch fd dir events in - Hashtbl.add wdmap wd (dir,handler) + Hashtbl.add wdmap wd (dir,Activehandler(handler)) + +let mask_events wd = + let (dirname,handler) = try Hashtbl.find wdmap wd with Not_found->("",Nohandler) + in + match handler with + | Activehandler(func)-> + Hashtbl.replace wdmap wd (dirname,Maskedhandler(func)) + | _ -> + () + +let unmask_events wd = + let (dirname,handler) = try Hashtbl.find wdmap wd with Not_found->("",Nohandler) + in + match handler with + | Maskedhandler(func)-> + Hashtbl.replace wdmap wd (dirname,Activehandler(func)) + | _ -> + () + (* XXX let del_watch dir = @@ -51,12 +71,13 @@ let receive_event (eventdescriptor:fname_and_fd) (bla:fname_and_fd) = | (wd,evlist,_,Some(str)) -> let purestr = asciiz(str) in let (dirname,handler) = - try Hashtbl.find wdmap wd with Not_found->fprintf logfd "Unknown watch descriptor\n";raise Not_found + try Hashtbl.find wdmap wd with Not_found->("",Nohandler) in ( match handler with - | None->handle_dir_event dirname evlist purestr - | Some(handler)->handler dirname evlist purestr + | Nohandler->fprintf logfd "Unhandled watch descriptor\n";flush logfd + | Activehandler(handler)->handler wd dirname evlist purestr + | Maskedhandler(_)->() ) | _ -> ()) evs diff --git a/vsyssh/vsyssh.c b/vsyssh/vsyssh.c index 889e41a..b77d7f2 100644 --- a/vsyssh/vsyssh.c +++ b/vsyssh/vsyssh.c @@ -40,11 +40,13 @@ int main(int argc, char **argv, char **envp) strcat(inf,".in"); strcat(outf,".out"); - vfd1 = open(inf,O_WRONLY|O_NONBLOCK); vfd0 = open(outf,O_RDONLY|O_NONBLOCK); + printf("Out file: %d\n",vfd0); + vfd1 = open(inf,O_WRONLY); + printf("In file: %d\n",vfd1); if (vfd0==-1 || vfd1 == -1) { - printf("Error opening vsys entry %s\n", argv[1]); + printf("Error opening vsys entry %s (%s)\n", argv[1],strerror(errno)); exit(1); }