2 * UCW Library -- Main Loop
4 * (c) 2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #include "lib/clists.h"
12 extern sh_time_t now; /* Current time */
13 extern uns main_shutdown;
14 extern clist main_timer_list, main_file_list, main_hook_list, main_process_list;
16 /* User-defined fields are marked with [*], all other fields must be initialized to zero. */
23 void (*handler)(struct main_timer *tm); /* [*] Function to be called when the timer expires. Must re-add/del the timer.*/
24 void *data; /* [*] Data for use by the handler */
27 void timer_add(struct main_timer *tm, sh_time_t expires); /* Can modify a running timer, too */
28 void timer_del(struct main_timer *tm);
34 int fd; /* [*] File descriptor */
35 int (*read_handler)(struct main_file *fi); /* [*] To be called when ready for reading/writing; must call file_chg() afterwards */
36 int (*write_handler)(struct main_file *fi);
37 void (*error_handler)(struct main_file *fi, int cause); /* [*] Handler to call on errors */
38 void *data; /* [*] Data for use by the handlers */
39 byte *rbuf; /* Read/write pointers for use by file_read/write */
43 void (*read_done)(struct main_file *fi); /* [*] Called when file_read is finished; rpos < rlen if EOF */
44 void (*write_done)(struct main_file *fi); /* [*] Called when file_write is finished */
45 struct main_timer timer;
46 struct pollfd *pollfd;
49 enum main_file_err_cause {
55 void file_add(struct main_file *fi);
56 void file_chg(struct main_file *fi);
57 void file_del(struct main_file *fi);
58 void file_read(struct main_file *fi, void *buf, uns len);
59 void file_write(struct main_file *fi, void *buf, uns len);
60 void file_set_timeout(struct main_file *fi, sh_time_t expires);
62 /* Hooks to be called in each iteration of the main loop */
66 int (*handler)(struct main_hook *ho); /* [*] Hook function; returns 1 if should be called again */
67 void *data; /* [*] For use by the handler */
70 void hook_add(struct main_hook *ho);
71 void hook_del(struct main_hook *ho);
73 /* Processes to watch */
77 int pid; /* Process id (0=not running) */
78 int status; /* Exit status (-1=fork failed) */
79 byte status_msg[EXIT_STATUS_MSG_SIZE];
80 void (*handler)(struct main_process *mp); /* [*] Called when the process exits; process_del done automatically */
81 void *data; /* [*] For use by the handler */
84 void process_add(struct main_process *mp);
85 void process_del(struct main_process *mp);
86 int process_fork(struct main_process *mp);
92 void main_debug(void);