This one is fixed.
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 =
(** 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 =
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]);
(
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 =
()
* leaks - fix implementation of rmdir accordingly
*)
let wdmap = Hashtbl.create 1024
-let masks = Hashtbl.create 1024
let fd = Inotify.init ()
| [] -> 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) ||
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
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
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};
//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(".");
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);