Checking in daemonization, writing to a logfile, and saving the pid
[vsys.git] / fdwatcher.ml
1 (** Fdwatcher - The main event loop. Agnostic to the type of file descriptors
2  involved.*)
3
4 open Printf
5 open Globals
6
7 let fdset = ref []
8 let cbtable = Hashtbl.create 1024
9
10 (* The in descriptor is always open. Thanks to the broken semantics of
11  * fifo outputs, the out descriptor must be opened a nouveau whenever we
12  * want to send out data, and so we keep the associated filename as well.
13  * Same with input fifos. Yipee.*)
14 let add_fd (evpair:fname_and_fd) (fd_other:fname_and_fd) (callback:fname_and_fd->fname_and_fd->unit) = 
15   let (fname,fd) = evpair in
16   fdset := (fd::!fdset);
17   Hashtbl.replace cbtable fd (callback,(evpair,fd_other))
18
19 let del_fd fd =
20   fdset:=List.filter (fun l->l<>fd) !fdset;
21   flush logfd
22
23 let start_watch () =
24     while (true)
25     do
26               let (fds,_,_) = try Unix.select !fdset [] [] (-1.) 
27               with e->
28                 ([],[],[])
29               in
30                 List.iter (fun elt->
31                              let (func,(evd,fd_other)) = Hashtbl.find cbtable elt in
32                                func evd fd_other) fds
33     done