add -fPIC option to the C compiler, required in f31
[vsys.git] / fifowatcher.ml
index 6e2a12a..58113a1 100644 (file)
@@ -5,21 +5,23 @@ open Unix
 open Globals
 open Dirwatcher
 open Printf
+open Splice
 
 (** A connected process, FIFO *)
-type channel_pipe = Process of out_channel | Fifo of out_channel | BrokenPipe
-(** Signed file descriptors. Usually, we'll make sure that they're not
-  mistreated *)
+type channel_pipe = Process of Unix.file_descr | Fifo of Unix.file_descr | BrokenPipe
+
+(** Signed file descriptors. *)
 type signed_fd = Infd of Unix.file_descr | Outfd of Unix.file_descr | Eventfd of Unix.file_descr
 
+(** XXX This will get deprecated when we switch to inotify *)                                                                                  
 let fdmap: (Unix.file_descr,string*string) Hashtbl.t = Hashtbl.create 1024
+
 (** Maps pids to slice connections. Needed to clean up fds when a script dies
   with EPIPE *)
 let pidmap: (int,signed_fd list) Hashtbl.t = Hashtbl.create 1024
 let backend_prefix = ref ""
 let open_fds: (Unix.file_descr,channel_pipe) Hashtbl.t = Hashtbl.create 1024
 
-
 (** Receive an event from a running script. This event must be relayed to the
   slice that invoked it.
 
@@ -29,31 +31,31 @@ let receive_process_event (idesc: fname_and_fd) (_: fname_and_fd) =
   let (_,ifd) = idesc in
   let cp = try Hashtbl.find open_fds ifd with
       Not_found->
-        fprintf logfd "Fifo fd disappeared\n";flush logfd;raise Bug
+        logprint "Fifo fd disappeared\n";raise Bug
   in
     match (cp) with 
-      | Fifo(fifo_outchan) ->
-          let process_inchan = in_channel_of_descr ifd in
-          let cont = ref true in
-            while (!cont) do
-              try 
-                let curline = input_line process_inchan in
-                  fprintf fifo_outchan "%s\n" curline;flush fifo_outchan
-              with 
-                | End_of_file|Sys_blocked_io|Unix_error(EPIPE,_,_)|Unix_error(EBADF,_,_) ->
-                    begin
-                      cont:=false
-                    end
-                | Unix_error(_,s1,s2) -> fprintf logfd "Unix error %s - %s\n" s1 s2;flush logfd;cont:=false
-                | Sys_error(s) -> (* We get this error if the EPIPE comes before the EOF marker*) cont:=false
-                | e -> fprintf logfd "Error - received unexpected event from file system !!!\n";raise e
-            done
-      | _ -> fprintf logfd "Bug! Process fd received in the channel handler\n";flush logfd;raise Bug
+      | Fifo(fifo_outfd) ->
+          begin
+            try
+              printf "Received process event\n";flush Pervasives.stdout;
+              (* transferred = 4096 => there were at least 4096 bytes in the
+               * stream, so we should try again.
+               * transferred < 4096 => EAGAIN => either this is all the data we
+               * have (move on)
+               * OR XXX the receiver is blocking (supposedly this can't happen) *)
+              let transferred = ref 4096 in
+                while (!transferred == 4096) do 
+                  transferred:=tee ifd fifo_outfd 4096
+                done;
+            with 
+                Failure(s)->logprint "Transfer failure: %s\n" s
+          end
+      | _ -> logprint "Bug! Process fd received in the channel handler\n";raise Bug
 
 let rec openentry_int fifoin fifoout (abspath:string*string) =
   let fdin =
     try openfile fifoin [O_RDONLY;O_NONBLOCK] 0o777 with 
-        e->fprintf logfd "Error opening and connecting FIFO: %s,%o\n" fifoin 0o777;flush logfd;raise e
+        e->logprint "Error opening and connecting FIFO: %s,%o\n" fifoin 0o777;raise e
   in
     Hashtbl.replace fdmap fdin abspath;
     Fdwatcher.add_fd (Some(fifoin),fdin) (Some(fifoout),stdout) receive_fifo_event
@@ -61,16 +63,16 @@ and reopenentry_int fdin fifoin fifoout =
   close fdin;
     Fdwatcher.del_fd fdin;
     let abspath = try 
-      Hashtbl.find fdmap fdin with _ -> fprintf logfd "Bug: Phantom pipe\n";flush logfd;raise Bug
+      Hashtbl.find fdmap fdin with _ -> logprint "Bug: Phantom pipe\n";raise Bug
     in
       openentry_int fifoin fifoout abspath
+
 (** receive an event from a fifo and connect to the corresponding service, or to
   create it if it doesn't exit 
   @param eventdescriptor Name of input pipe,in descriptor
   @param outdescriptor Name of output pipe, out descriptor
   *)
 and receive_fifo_event eventdescriptor outdescriptor =
-  printf "received fifo event\n";flush Pervasives.stdout;
   let (evfname,evfd) = eventdescriptor in
   let (fname_other,fd_other) = outdescriptor in
   (* Open the output pipe, or use stdout instead *)
@@ -79,22 +81,21 @@ and receive_fifo_event eventdescriptor outdescriptor =
       | Some(str)->
           (
             try openfile str [O_WRONLY;O_NONBLOCK] 0o777 with
-                _->fprintf logfd "Output pipe not open, using stdout in place of %s\n" str;flush logfd;stdout
+                _->logprint "Output pipe not open, using stdout in place of %s\n" str;stdout
           )
-      | None-> fprintf logfd "Bug, nameless pipe\n";flush logfd;raise Bug
+      | None-> logprint "Bug, nameless pipe\n";raise Bug
   in
   (* Check if the input descriptor is already registered (=> a session is open).
    If not, register it and start a new session.*)
   let pipe = try Hashtbl.find open_fds evfd with
     | Not_found ->
-        printf "fd not found!\n";flush Pervasives.stdout;
         (* Ok, need to launch script *)
         let execpath,slice_name = Hashtbl.find fdmap evfd in
         let (script_infd,pout) = Unix.pipe () in
         let (pin,script_outfd) = Unix.pipe () in
           set_nonblock script_infd;
           ignore(sigprocmask SIG_BLOCK [Sys.sigchld]);
-          let rpid = try Some(create_process execpath [|execpath;slice_name|] pin pout pout) with e -> fprintf logfd "Error executing service: %s\n" execpath;flush logfd;None
+          let rpid = try Some(create_process execpath [|execpath;slice_name|] pin pout pout) with e -> logprint "Error executing service: %s\n" execpath;None
           in
             match rpid with
               | None-> BrokenPipe
@@ -104,55 +105,44 @@ and receive_fifo_event eventdescriptor outdescriptor =
                   Hashtbl.add pidmap pid [Infd(script_infd);Outfd(script_outfd);Eventfd(evfd)];
 
                   (* Connect pipe to running script *)
-                  Hashtbl.add open_fds evfd (Process(out_channel_of_descr script_outfd));
+                  Hashtbl.add open_fds evfd (Process(script_outfd));
 
                   (* Connect the running script to the pipe *)
-                  Hashtbl.add open_fds script_infd (Fifo(out_channel_of_descr outfd));
+                  Hashtbl.add open_fds script_infd (Fifo(outfd));
 
                   (* Activate running script *)
                   Fdwatcher.add_fd (None,script_infd) (None,script_infd) receive_process_event;
 
-                  (Process(out_channel_of_descr script_outfd))
+                  (Process(script_outfd))
   in
-  (* We have the connection to the process - because it was open, or because it
-   just got established *)
-  let inchan_fd = in_channel_of_descr evfd in
+    (* We have the connection to the process - because it was open, or because it
+     just got established *)
     match (pipe) with
-      | Process(out_channel) -> 
-          let cont = ref true in
-            while (!cont) do
-              try 
-                fprintf logfd "Reading...\n";flush logfd;
-                let curline = input_line inchan_fd in
-                  fprintf out_channel "%s\n" curline;flush out_channel 
-              with 
-                |End_of_file->
-                    (
-                      match (evfname,fname_other) with
-                        | Some(str1),Some(str2)->
-                            fprintf logfd "Reopening entry\n";flush logfd;
-                            reopenentry_int evfd str1 str2
-                        | Some(str1),None ->
-                            fprintf logfd "Bug, nameless pipe\n";flush logfd;raise Bug
-                        | None,_ ->
-                            fprintf logfd "Race condition -> user deleted file before closing it. Clever ploy, but won't work.\n";
-                            flush logfd
-                    );
-                    cont:=false
-                |Sys_blocked_io ->fprintf logfd "Sysblockedio\n";flush logfd;
-                                  cont:=false
-                | Unix_error(_,s1,s2) -> fprintf logfd "Unix error %s - %s\n" s1 s2;flush logfd;cont:=false
-                (*| _ ->fprintf logfd "Bug: unhandled exception\n";flush
-                 * logfd;raise Bug*)
-            done;
-            ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld])
+      | Process(fifo_outfd) -> 
+          begin
+            try
+              let transferred = ref 4096 in
+                while (!transferred == 4096) do
+                  begin
+                  transferred:=tee evfd fifo_outfd 4096;
+                                 printf "Transferred: %d\n" !transferred;flush Pervasives.stdout
+                  end
+                done;
+            with Failure(str) ->
+              begin
+                logprint "Error connecting user to service: %s\n" str
+              end;
+              ignore(sigprocmask SIG_UNBLOCK [Sys.sigchld]);
+                                 printf "Out of the loop\n";flush Pervasives.stdout
+
+          end
       | BrokenPipe -> ()
-      | Fifo(_) -> fprintf logfd "BUG! received process event from fifo\n";raise Bug
+      | Fifo(_) -> logprint "BUG! received process event from fifo\n";raise Bug
 
 
 (** Make a pair of fifo entries *)
 let mkentry fqp abspath perm uname = 
-  fprintf logfd "Making entry %s->%s\n" fqp abspath;flush logfd;
+  logprint "Making entry %s->%s\n" fqp abspath;
   let fifoin=sprintf "%s.in" fqp in
   let fifoout=sprintf "%s.out" fqp in
     (try Unix.unlink fifoin with _ -> ());
@@ -163,14 +153,14 @@ let mkentry fqp abspath perm uname =
          Unix.mkfifo infname 0o666;
          Unix.mkfifo outfname 0o666;
          ( (* Make the user the owner of the pipes in a non-chroot environment *)
-         if (!Globals.nochroot) then
-           let pwentry = Unix.getpwnam uname in
-             Unix.chown infname pwentry.pw_uid pwentry.pw_gid; 
-             Unix.chown outfname pwentry.pw_uid pwentry.pw_gid
+           if (!Globals.nochroot) then
+             let pwentry = Unix.getpwnam uname in
+               Unix.chown infname pwentry.pw_uid pwentry.pw_gid; 
+               Unix.chown outfname pwentry.pw_uid pwentry.pw_gid
          );
          Success
      with 
-         e->fprintf logfd "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;flush logfd;Failed)
+         e->logprint "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;Failed)
 
 (** Open fifos for a session. Will shutdown vsys if the fifos don't exist *)
 let openentry fqp abspath perm =