2 * UCW Library -- Main Loop
4 * (c) 2004--2006 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 "ucw/mainloop.h"
27 ucw_time_t main_now_seconds;
28 timestamp_t main_idle_time;
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 %lld 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);
56 main_poll_table_obsolete = 1;
61 timer_add(struct main_timer *tm, timestamp_t expires)
64 DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires-main_now));
66 DBG("MAIN: Clearing timer %p", tm);
69 tm->expires = expires;
72 cnode *t = main_timer_list.head.next;
73 while (t != &main_timer_list.head && ((struct main_timer *) t)->expires < expires)
75 clist_insert_before(&tm->n, t);
80 timer_del(struct main_timer *tm)
86 file_timer_expired(struct main_timer *tm)
88 struct main_file *fi = tm->data;
89 timer_del(&fi->timer);
90 if (fi->error_handler)
91 fi->error_handler(fi, MFERR_TIMEOUT);
95 file_add(struct main_file *fi)
97 DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
99 clist_add_tail(&main_file_list, &fi->n);
100 fi->timer.handler = file_timer_expired;
103 main_poll_table_obsolete = 1;
104 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
105 msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
109 file_chg(struct main_file *fi)
111 struct pollfd *p = fi->pollfd;
115 if (fi->read_handler)
116 p->events |= POLLIN | POLLHUP | POLLERR;
117 if (fi->write_handler)
118 p->events |= POLLOUT | POLLERR;
123 file_del(struct main_file *fi)
125 DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
127 timer_del(&fi->timer);
128 clist_remove(&fi->n);
130 main_poll_table_obsolete = 1;
131 fi->n.next = fi->n.prev = NULL;
135 file_read_handler(struct main_file *fi)
137 while (fi->rpos < fi->rlen)
139 int l = read(fi->fd, fi->rbuf + fi->rpos, fi->rlen - fi->rpos);
140 DBG("MAIN: FD %d: read %d", fi->fd, l);
143 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
144 fi->error_handler(fi, MFERR_READ);
151 DBG("MAIN: FD %d done read %d of %d", fi->fd, fi->rpos, fi->rlen);
152 fi->read_handler = NULL;
159 file_write_handler(struct main_file *fi)
161 while (fi->wpos < fi->wlen)
163 int l = write(fi->fd, fi->wbuf + fi->wpos, fi->wlen - fi->wpos);
164 DBG("MAIN: FD %d: write %d", fi->fd, l);
167 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
168 fi->error_handler(fi, MFERR_WRITE);
173 DBG("MAIN: FD %d done write %d", fi->fd, fi->wpos);
174 fi->write_handler = NULL;
181 file_read(struct main_file *fi, void *buf, uns len)
186 fi->read_handler = file_read_handler;
193 fi->read_handler = NULL;
195 fi->rpos = fi->rlen = 0;
201 file_write(struct main_file *fi, void *buf, uns len)
206 fi->write_handler = file_write_handler;
213 fi->write_handler = NULL;
215 fi->wpos = fi->wlen = 0;
221 file_set_timeout(struct main_file *fi, timestamp_t expires)
224 timer_add(&fi->timer, expires);
230 CLIST_FOR_EACH(struct main_file *, f, main_file_list)
235 hook_add(struct main_hook *ho)
237 DBG("MAIN: Adding hook %p", ho);
239 clist_add_tail(&main_hook_list, &ho->n);
243 hook_del(struct main_hook *ho)
245 DBG("MAIN: Deleting hook %p", ho);
247 clist_remove(&ho->n);
248 ho->n.next = ho->n.prev = NULL;
252 main_sigchld_handler(int x UNUSED)
254 DBG("SIGCHLD received");
258 process_add(struct main_process *mp)
260 DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
263 clist_add_tail(&main_process_list, &mp->n);
264 if (!main_sigchld_set_up)
267 bzero(&sa, sizeof(sa));
268 sa.sa_handler = main_sigchld_handler;
269 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
270 sigaction(SIGCHLD, &sa, NULL);
271 main_sigchld_set_up = 1;
276 process_del(struct main_process *mp)
278 DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
280 clist_remove(&mp->n);
285 process_fork(struct main_process *mp)
290 DBG("MAIN: Fork failed");
292 format_exit_status(mp->status_msg, -1);
300 DBG("MAIN: Forked process %d", (int) pid);
311 msg(L_DEBUG, "### Main loop status on %lld", (long long)main_now);
312 msg(L_DEBUG, "\tActive timers:");
313 struct main_timer *tm;
314 CLIST_WALK(tm, main_timer_list)
315 msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires ? tm->expires-main_now : 999999), tm->data);
316 struct main_file *fi;
317 msg(L_DEBUG, "\tActive files:");
318 CLIST_WALK(fi, main_file_list)
319 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %lld, data %p)",
320 fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler,
321 (long long)(fi->timer.expires ? fi->timer.expires-main_now : 999999), fi->data);
322 msg(L_DEBUG, "\tActive hooks:");
323 struct main_hook *ho;
324 CLIST_WALK(ho, main_hook_list)
325 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
326 msg(L_DEBUG, "\tActive processes:");
327 struct main_process *pr;
328 CLIST_WALK(pr, main_process_list)
329 msg(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
334 main_rebuild_poll_table(void)
336 struct main_file *fi;
337 if (main_poll_table_size < main_file_cnt)
340 xfree(main_poll_table);
342 main_poll_table_size = 1;
343 while (main_poll_table_size < main_file_cnt)
344 main_poll_table_size *= 2;
345 main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
347 struct pollfd *p = main_poll_table;
348 DBG("MAIN: Rebuilding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
349 CLIST_WALK(fi, main_file_list)
355 main_poll_table_obsolete = 0;
361 DBG("MAIN: Entering main_loop");
362 ASSERT(main_timer_list.head.next);
364 struct main_file *fi;
365 struct main_hook *ho;
366 struct main_timer *tm;
367 struct main_process *pr;
373 timestamp_t wake = main_now + 1000000000;
374 while ((tm = clist_head(&main_timer_list)) && tm->expires <= main_now)
376 DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(main_now - tm->expires));
379 int hook_min = HOOK_RETRY;
380 int hook_max = HOOK_SHUTDOWN;
381 CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
383 DBG("MAIN: Hook %p", ho);
384 int ret = ho->handler(ho);
385 hook_min = MIN(hook_min, ret);
386 hook_max = MAX(hook_max, ret);
388 if (hook_min == HOOK_SHUTDOWN ||
389 hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
392 DBG("MAIN: Shut down by %s", main_shutdown ? "main_shutdown" : "a hook");
395 if (hook_max == HOOK_RETRY)
397 if (main_poll_table_obsolete)
398 main_rebuild_poll_table();
399 if (!clist_empty(&main_process_list))
403 wake = MIN(wake, main_now + 10000);
404 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
406 DBG("MAIN: Child %d exited with status %x", pid, stat);
407 CLIST_WALK(pr, main_process_list)
412 format_exit_status(pr->status_msg, pr->status);
413 DBG("MAIN: Calling process exit handler");
420 /* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
421 if ((tm = clist_head(&main_timer_list)) && tm->expires < wake)
424 int timeout = (wake ? wake - main_now : 0);
425 DBG("MAIN: Poll for %d fds and timeout %d ms", main_file_cnt, timeout);
426 int p = poll(main_poll_table, main_file_cnt, timeout);
427 timestamp_t old_now = main_now;
429 main_idle_time += main_now - old_now;
432 struct pollfd *p = main_poll_table;
433 CLIST_WALK(fi, main_file_list)
435 if (p->revents & (POLLIN | POLLHUP | POLLERR))
438 DBG("MAIN: Read event on fd %d", p->fd);
439 while (fi->read_handler && fi->read_handler(fi) && !main_poll_table_obsolete);
440 if (main_poll_table_obsolete) /* File entries have been inserted or deleted => better not risk continuing to nowhere */
443 if (p->revents & (POLLOUT | POLLERR))
446 DBG("MAIN: Write event on fd %d", p->fd);
447 while (fi->write_handler && fi->write_handler(fi) && !main_poll_table_obsolete);
448 if (main_poll_table_obsolete)
459 static struct main_process mp;
460 static struct main_file fin, fout;
461 static struct main_hook hook;
462 static struct main_timer tm;
466 static void dread(struct main_file *fi)
468 if (fi->rpos < fi->rlen)
470 msg(L_INFO, "Read EOF");
475 msg(L_INFO, "Read done");
476 file_read(fi, rb, sizeof(rb));
480 static void derror(struct main_file *fi, int cause)
482 msg(L_INFO, "Error: %m !!! (cause %d)", cause);
486 static void dwrite(struct main_file *fi UNUSED)
488 msg(L_INFO, "Write done");
491 static int dhook(struct main_hook *ho UNUSED)
493 msg(L_INFO, "Hook called");
497 static void dtimer(struct main_timer *tm)
499 msg(L_INFO, "Timer tick");
500 timer_add(tm, main_now + 10000);
503 static void dentry(void)
505 msg(L_INFO, "*** SUBPROCESS START ***");
507 msg(L_INFO, "*** SUBPROCESS FINISH ***");
511 static void dexit(struct main_process *pr)
513 msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
523 fin.read_done = dread;
524 fin.error_handler = derror;
526 file_read(&fin, rb, sizeof(rb));
529 fout.write_done = dwrite;
530 fout.error_handler = derror;
532 file_write(&fout, "Hello, world!\n", 14);
534 hook.handler = dhook;
538 timer_add(&tm, main_now + 1000);
541 if (!process_fork(&mp))
547 msg(L_INFO, "Finished.");