From 57622e7436de3e40f2dc56b17381eb0bfb18d23c Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Wed, 2 Jul 2008 07:32:46 +0000 Subject: [PATCH] FINALLY... This one is fixed. --- directfifowatcher.ml | 45 +++++++++++++++++++++++------------ dirwatcher.ml | 55 ++++++++++++------------------------------- globals.ml | 1 + tests/vsys_conctest.c | 47 +++++++++++++++++++++++++++++++----- 4 files changed, 87 insertions(+), 61 deletions(-) diff --git a/directfifowatcher.ml b/directfifowatcher.ml index 075f08c..e97d7c5 100644 --- a/directfifowatcher.ml +++ b/directfifowatcher.ml @@ -26,16 +26,30 @@ type directory = string type base_pathname = string type slice_name = string - let direct_fifo_table: (in_pathname,(directory*base_pathname*slice_name*Unix.file_descr) option) Hashtbl.t = Hashtbl.create 1024 let pidmap: (int,in_pathname * Unix.file_descr) Hashtbl.t = Hashtbl.create 1024 -let rec list_check lst elt = - match lst with - | [] -> false - | car::cdr -> if (car==elt) then true else list_check cdr elt +let move_gate fname = + let tmpfname=String.concat "." [fname;"tmp"] in + Unix.rename fname tmpfname; + tmpfname + +let move_ungate fname restore = + Unix.rename restore fname + +let list_check lst elt _ = + let rec list_check_rec lst = + match lst with + | [] -> false + | car::cdr -> + if (car==elt) then + true + else + list_check_rec cdr + in + list_check_rec lst let openentry_int fifoin = let fdin = @@ -46,23 +60,21 @@ let openentry_int fifoin = (** Open entry safely, by first masking out the file to be opened *) let openentry_safe root_dir fqp_in backend_spec = - Dirwatcher.mask_watch fqp_in; - let fd_in = openentry_int fqp_in in + let restore = move_gate fqp_in in + let fd_in = openentry_int restore in + move_ungate fqp_in restore; let (fqp,slice_name) = backend_spec in Hashtbl.replace direct_fifo_table fqp_in (Some(root_dir,fqp,slice_name,fd_in)) let openentry root_dir fqp backend_spec = - () (* let fqp_in = String.concat "." [fqp;"in"] in - openentry_safe root_dir fqp_in backend_spec*) + openentry_safe root_dir fqp_in backend_spec let reopenentry fifoin = - () - (* let entry = try Hashtbl.find direct_fifo_table fifoin with _ -> None in match entry with | Some(dir, fqp,slice_name,fd) -> close_if_open fd;openentry_safe dir fifoin (fqp,slice_name) - | None -> ()*) + | None -> () (* vsys is activated when a client opens an in file *) let connect_file fqp_in = @@ -77,7 +89,10 @@ let connect_file fqp_in = let fqp_out = String.concat "." [fqp;"out"] in let fifo_fdout = try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with - _->logprint "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;stdout + _-> (* The client is opening the descriptor too fast *) + sleep 1;try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with + _-> + logprint "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;stdout in ignore(sigprocmask SIG_BLOCK [Sys.sigchld]); ( @@ -141,9 +156,9 @@ let rec add_dir_watch fqp = and direct_fifo_handler wd dirname evlist fname = let is_event = list_check evlist in - if (is_event Open) then + if (is_event Open Attrib) then let fqp_in = String.concat "/" [dirname;fname] in - connect_file fqp_in + connect_file fqp_in let del_dir_watch fqp = () diff --git a/dirwatcher.ml b/dirwatcher.ml index a50416f..53c4c1c 100644 --- a/dirwatcher.ml +++ b/dirwatcher.ml @@ -9,7 +9,6 @@ open Globals * leaks - fix implementation of rmdir accordingly *) let wdmap = Hashtbl.create 1024 -let masks = Hashtbl.create 1024 let fd = Inotify.init () @@ -18,34 +17,23 @@ let rec list_check lst elt = | [] -> false | car::cdr -> if (car==elt) then true else list_check cdr elt -let handle_dir_event dirname evlist str = - let fname = String.concat "/" [dirname;str] in - logprint "File: %s. " fname; +let pevlist evlist = List.iter (fun e -> logprint "Event: %s\n" (string_of_event e)) evlist +let handle_dir_event dirname evlist str = + let fname = String.concat "/" [dirname;str] in + logprint "File: %s. " fname; + pevlist evlist + let add_watch dir events handler = let wd = Inotify.add_watch fd dir events in Hashtbl.add wdmap wd (dir,Some(handler)) (* Ignore the possibility that the whole directory can disappear and come * back while it is masked *) -let mask_watch fqp = - try - Hashtbl.replace masks fqp true - with _ -> - () - -let unmask_watch fqp = - if (Hashtbl.mem masks fqp) then - begin - Hashtbl.remove masks fqp - end - else - logprint "WARNING: %s -- Unpaired unmask\n" fqp - let asciiz s = let rec findfirstnul str idx len = if ((idx==len) || @@ -67,28 +55,15 @@ let receive_event (eventdescriptor:fname_and_fd) (bla:fname_and_fd) = let (dirname,handler) = try Hashtbl.find wdmap wd with Not_found->("",None) in - match handler with - | None->logprint "Unhandled watch descriptor\n" - | Some(handler)-> - let fqp = String.concat "/" [dirname;purestr] in - logprint "Received event from %s\n" fqp; - let mask_filter = Hashtbl.mem masks fqp in - begin - if ((not mask_filter)) then - begin - (* - logprint "Received event for - %s\n" - fqp;*) - handler wd dirname evlist - purestr - end - else - begin - (*logprint "Unmasking %s\n" - * fqp;*) - unmask_watch fqp - end - end + match handler with + | None-> + logprint "Unhandled watch descriptor\n" + | Some(handler)-> + let fqp = String.concat "/" [dirname;purestr] in + begin + handler wd dirname evlist + purestr + end end | _ -> ()) evs diff --git a/globals.ml b/globals.ml index 1506e01..ca475d7 100644 --- a/globals.ml +++ b/globals.ml @@ -10,6 +10,7 @@ let failsafe = ref false let logfd = open_out_gen [Open_append;Open_creat] 0o644 !log_filepath let logprint fmt = Printf.fprintf logfd (fmt ^^ "%!") +let debprint fmt = if (!debug) then Printf.fprintf logfd (fmt ^^ "%!") let print fmt = Printf.printf (fmt ^^ "%!") type result = Success | Failed diff --git a/tests/vsys_conctest.c b/tests/vsys_conctest.c index 16fd279..35f7c2d 100644 --- a/tests/vsys_conctest.c +++ b/tests/vsys_conctest.c @@ -16,7 +16,7 @@ int main() char buf[4096]; int fd_in = -1, fd_out; int res; - int flag; + int flag,flag2; int count = 1; struct timeval tv={.tv_sec=5,.tv_usec=0}; @@ -27,27 +27,40 @@ int main() //usleep(200); printf("(%d)", count);fflush(stdout); - if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) { fprintf(stderr, "error executing top\n"); exit(-1); } -// printf("(opening in file)"); - if ((fd_in = open(top_in_file, O_WRONLY)) < 0) { - fprintf(stderr, "error opening %s\n", top_in_file); - exit(-1); + //printf("((0))");fflush(stdout); + while ((fd_in = open(top_in_file, O_WRONLY| O_NONBLOCK)) < 0) { + fprintf(stderr, "Waiting for %s (%s)\n", top_in_file,strerror(errno)); + sleep (1); } + printf("%d open\n",fd_in); + + //printf("(1)"); if ((flag = fcntl(fd_out, F_GETFL)) == -1) { printf("fcntl get failed\n"); exit(-1); } + //printf("(2)"); + + //printf("(3)"); + if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) { + printf("fcntl get failed\n"); + exit(-1); + } + //printf("(4)"); + while (1) { FD_ZERO(&readSet); FD_SET(fd_out, &readSet); + //printf("(5)"); res = select(fd_out + 1, &readSet, NULL, NULL, NULL); + //printf("(6)"); if (res < 0) { if (errno == EINTR || errno == EAGAIN) { printf("."); @@ -59,21 +72,43 @@ int main() break; /* we're done */ } + //printf("(7)"); if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) { printf("fcntl set failed\n"); exit(-1); } + //printf("(8)"); + //printf("(9)"); if ((flag = fcntl(fd_out, F_GETFL)) == -1) { printf("fcntl get failed\n"); exit(-1); } + //printf("(10)"); + + + //printf("(11)"); + if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) { + printf("fcntl set failed\n"); + exit(-1); + } + + //printf("(11)"); + if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) { + printf("fcntl get failed\n"); + exit(-1); + } + //printf("(12)"); if (flag & O_NONBLOCK == 0) { printf("fd_out still nonblocking\n"); exit(-1); } + if (flag & O_NONBLOCK == 0) { + printf("fd_in still nonblocking\n"); + exit(-1); + } if ((fp = fdopen(fd_out, "r")) == NULL) { printf("fdopen failed\n"); exit(-1); -- 2.43.0