2 * UCW Library -- Main Loop
4 * (c) 2004--2005 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.
13 #include "lib/mainloop.h"
28 sh_time_t main_now_seconds;
31 clist main_timer_list, main_file_list, main_hook_list, main_process_list;
32 static uns main_file_cnt;
33 static uns main_poll_table_obsolete, main_poll_table_size;
34 static struct pollfd *main_poll_table;
35 static uns main_sigchld_set_up;
41 gettimeofday(&tv, NULL);
42 main_now_seconds = tv.tv_sec;
43 main_now = (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
44 DBG("It's %Ld o'clock", (long long) main_now);
50 DBG("MAIN: Initializing");
51 clist_init(&main_timer_list);
52 clist_init(&main_file_list);
53 clist_init(&main_hook_list);
54 clist_init(&main_process_list);
59 timer_add(struct main_timer *tm, timestamp_t expires)
63 tm->expires = expires;
66 cnode *t = main_timer_list.head.next;
67 while (t != &main_timer_list.head && ((struct main_timer *) t)->expires < expires)
69 clist_insert_before(&tm->n, t);
74 timer_del(struct main_timer *tm)
80 file_timer_expired(struct main_timer *tm)
82 struct main_file *fi = tm->data;
83 timer_del(&fi->timer);
84 if (fi->error_handler)
85 fi->error_handler(fi, MFERR_TIMEOUT);
89 file_add(struct main_file *fi)
92 clist_add_tail(&main_file_list, &fi->n);
93 fi->timer.handler = file_timer_expired;
96 main_poll_table_obsolete = 1;
97 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
98 log(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
102 file_chg(struct main_file *fi)
104 struct pollfd *p = fi->pollfd;
108 if (fi->read_handler)
109 p->events |= POLLIN | POLLHUP | POLLERR;
110 if (fi->write_handler)
111 p->events |= POLLOUT | POLLERR;
116 file_del(struct main_file *fi)
119 timer_del(&fi->timer);
120 clist_remove(&fi->n);
122 main_poll_table_obsolete = 1;
123 fi->n.next = fi->n.prev = NULL;
127 file_read_handler(struct main_file *fi)
129 while (fi->rpos < fi->rlen)
131 int l = read(fi->fd, fi->rbuf + fi->rpos, fi->rlen - fi->rpos);
132 DBG("MAIN: FD %d: read %d", fi->fd, l);
135 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
136 fi->error_handler(fi, MFERR_READ);
143 DBG("MAIN: FD %d done read %d of %d", fi->fd, fi->rpos, fi->rlen);
144 fi->read_handler = NULL;
151 file_write_handler(struct main_file *fi)
153 while (fi->wpos < fi->wlen)
155 int l = write(fi->fd, fi->wbuf + fi->wpos, fi->wlen - fi->wpos);
156 DBG("MAIN: FD %d: write %d", fi->fd, l);
159 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
160 fi->error_handler(fi, MFERR_WRITE);
165 DBG("MAIN: FD %d done write %d", fi->fd, fi->wpos);
166 fi->write_handler = NULL;
173 file_read(struct main_file *fi, void *buf, uns len)
178 fi->read_handler = file_read_handler;
185 fi->read_handler = NULL;
187 fi->rpos = fi->rlen = 0;
193 file_write(struct main_file *fi, void *buf, uns len)
198 fi->write_handler = file_write_handler;
205 fi->write_handler = NULL;
207 fi->wpos = fi->wlen = 0;
213 file_set_timeout(struct main_file *fi, timestamp_t expires)
216 timer_add(&fi->timer, expires);
220 hook_add(struct main_hook *ho)
223 clist_add_tail(&main_hook_list, &ho->n);
227 hook_del(struct main_hook *ho)
230 clist_remove(&ho->n);
231 ho->n.next = ho->n.prev = NULL;
235 main_sigchld_handler(int x UNUSED)
237 DBG("SIGCHLD received");
241 process_add(struct main_process *mp)
245 clist_add_tail(&main_process_list, &mp->n);
246 if (!main_sigchld_set_up)
249 bzero(&sa, sizeof(sa));
250 sa.sa_handler = main_sigchld_handler;
251 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
252 sigaction(SIGCHLD, &sa, NULL);
253 main_sigchld_set_up = 1;
258 process_del(struct main_process *mp)
261 clist_remove(&mp->n);
267 process_fork(struct main_process *mp)
273 DBG("MAIN: Fork failed");
275 format_exit_status(mp->status_msg, -1);
283 DBG("MAIN: Forked process %d", (int) pid);
294 log(L_DEBUG, "### Main loop status on %Ld", (long long)main_now);
295 log(L_DEBUG, "\tActive timers:");
296 struct main_timer *tm;
297 CLIST_WALK(tm, main_timer_list)
298 log(L_DEBUG, "\t\t%p (expires %Ld, data %p)", tm, (long long)(tm->expires ? tm->expires-main_now : 999999), tm->data);
299 struct main_file *fi;
300 log(L_DEBUG, "\tActive files:");
301 CLIST_WALK(fi, main_file_list)
302 log(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %Ld, data %p)",
303 fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler,
304 (long long)(fi->timer.expires ? fi->timer.expires-main_now : 999999), fi->data);
305 log(L_DEBUG, "\tActive hooks:");
306 struct main_hook *ho;
307 CLIST_WALK(ho, main_hook_list)
308 log(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
309 log(L_DEBUG, "\tActive processes:");
310 struct main_process *pr;
311 CLIST_WALK(pr, main_process_list)
312 log(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
317 main_rebuild_poll_table(void)
319 struct main_file *fi;
320 if (main_poll_table_size < main_file_cnt)
323 xfree(main_poll_table);
325 main_poll_table_size = 1;
326 while (main_poll_table_size < main_file_cnt)
327 main_poll_table_size *= 2;
328 main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
330 struct pollfd *p = main_poll_table;
331 DBG("MAIN: Rebuliding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
332 CLIST_WALK(fi, main_file_list)
338 main_poll_table_obsolete = 0;
344 DBG("MAIN: Entering main_loop");
345 ASSERT(main_timer_list.head.next);
347 struct main_file *fi;
348 struct main_hook *ho;
349 struct main_timer *tm;
350 struct main_process *pr;
353 while (!main_shutdown)
356 timestamp_t wake = main_now + 1000000000;
357 while ((tm = clist_head(&main_timer_list)) && tm->expires <= main_now)
359 DBG("MAIN: Timer %p expired at %Ld", tm, (long long) tm->expires);
362 CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
364 DBG("MAIN: Hook %p", ho);
368 if (main_poll_table_obsolete)
369 main_rebuild_poll_table();
370 if (!clist_empty(&main_process_list))
374 wake = MIN(wake, main_now + 10000);
375 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
377 DBG("MAIN: Child %d exited with status %x", pid, stat);
378 CLIST_WALK(pr, main_process_list)
383 format_exit_status(pr->status_msg, pr->status);
384 DBG("MAIN: Calling process exit handler");
391 /* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
392 if ((tm = clist_head(&main_timer_list)) && tm->expires < wake)
394 int timeout = (wake ? wake - main_now : 0);
395 DBG("MAIN: Poll for %d fds and timeout %d ms", main_file_cnt, timeout);
396 if (poll(main_poll_table, main_file_cnt, timeout))
398 struct pollfd *p = main_poll_table;
400 CLIST_WALK(fi, main_file_list)
402 if (p->revents & (POLLIN | POLLHUP | POLLERR))
405 DBG("MAIN: Read event on fd %d", p->fd);
406 while (fi->read_handler && fi->read_handler(fi) && !main_poll_table_obsolete);
407 if (main_poll_table_obsolete) /* File entries have been inserted or deleted => better not risk continuing to nowhere */
410 if (p->revents & (POLLOUT | POLLERR))
413 DBG("MAIN: Write event on fd %d", p->fd);
414 while (fi->write_handler && fi->write_handler(fi) && !main_poll_table_obsolete);
415 if (main_poll_table_obsolete)
426 static struct main_process mp;
427 static struct main_file fin, fout;
428 static struct main_hook hook;
429 static struct main_timer tm;
433 static void dread(struct main_file *fi)
435 if (fi->rpos < fi->rlen)
437 log(L_INFO, "Read EOF");
442 log(L_INFO, "Read done");
443 file_read(fi, rb, sizeof(rb));
447 static void derror(struct main_file *fi, int cause)
449 log(L_INFO, "Error: %m !!! (cause %d)", cause);
453 static void dwrite(struct main_file *fi UNUSED)
455 log(L_INFO, "Write done");
458 static int dhook(struct main_hook *ho UNUSED)
460 log(L_INFO, "Hook called");
464 static void dtimer(struct main_timer *tm)
466 log(L_INFO, "Timer tick");
467 timer_add(tm, main_now + 10000);
470 static void dentry(void)
472 log(L_INFO, "*** SUBPROCESS START ***");
474 log(L_INFO, "*** SUBPROCESS FINISH ***");
478 static void dexit(struct main_process *pr)
480 log(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
490 fin.read_done = dread;
491 fin.error_handler = derror;
493 file_read(&fin, rb, sizeof(rb));
496 fout.write_done = dwrite;
497 fout.error_handler = derror;
499 file_write(&fout, "Hello, world!\n", 14);
501 hook.handler = dhook;
505 timer_add(&tm, main_now + 1000);
508 if (!process_fork(&mp))
514 log(L_INFO, "Finished.");