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;
30 clist main_timer_list, main_file_list, main_hook_list, main_process_list;
31 static uns main_file_cnt;
32 static uns main_poll_table_obsolete, main_poll_table_size;
33 static struct pollfd *main_poll_table;
34 static uns main_sigchld_set_up;
40 gettimeofday(&tv, NULL);
41 main_now_seconds = tv.tv_sec;
42 main_now = (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
43 // DBG("It's %lld o'clock", (long long) main_now);
49 DBG("MAIN: Initializing");
50 clist_init(&main_timer_list);
51 clist_init(&main_file_list);
52 clist_init(&main_hook_list);
53 clist_init(&main_process_list);
55 main_poll_table_obsolete = 1;
60 timer_add(struct main_timer *tm, timestamp_t expires)
63 DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires-main_now));
65 DBG("MAIN: Clearing timer %p", tm);
68 tm->expires = expires;
71 cnode *t = main_timer_list.head.next;
72 while (t != &main_timer_list.head && ((struct main_timer *) t)->expires < expires)
74 clist_insert_before(&tm->n, t);
79 timer_del(struct main_timer *tm)
85 file_timer_expired(struct main_timer *tm)
87 struct main_file *fi = tm->data;
88 timer_del(&fi->timer);
89 if (fi->error_handler)
90 fi->error_handler(fi, MFERR_TIMEOUT);
94 file_add(struct main_file *fi)
96 DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
98 clist_add_tail(&main_file_list, &fi->n);
99 fi->timer.handler = file_timer_expired;
102 main_poll_table_obsolete = 1;
103 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
104 msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
108 file_chg(struct main_file *fi)
110 struct pollfd *p = fi->pollfd;
114 if (fi->read_handler)
115 p->events |= POLLIN | POLLHUP | POLLERR;
116 if (fi->write_handler)
117 p->events |= POLLOUT | POLLERR;
122 file_del(struct main_file *fi)
124 DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
126 timer_del(&fi->timer);
127 clist_remove(&fi->n);
129 main_poll_table_obsolete = 1;
130 fi->n.next = fi->n.prev = NULL;
134 file_read_handler(struct main_file *fi)
136 while (fi->rpos < fi->rlen)
138 int l = read(fi->fd, fi->rbuf + fi->rpos, fi->rlen - fi->rpos);
139 DBG("MAIN: FD %d: read %d", fi->fd, l);
142 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
143 fi->error_handler(fi, MFERR_READ);
150 DBG("MAIN: FD %d done read %d of %d", fi->fd, fi->rpos, fi->rlen);
151 fi->read_handler = NULL;
158 file_write_handler(struct main_file *fi)
160 while (fi->wpos < fi->wlen)
162 int l = write(fi->fd, fi->wbuf + fi->wpos, fi->wlen - fi->wpos);
163 DBG("MAIN: FD %d: write %d", fi->fd, l);
166 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
167 fi->error_handler(fi, MFERR_WRITE);
172 DBG("MAIN: FD %d done write %d", fi->fd, fi->wpos);
173 fi->write_handler = NULL;
180 file_read(struct main_file *fi, void *buf, uns len)
185 fi->read_handler = file_read_handler;
192 fi->read_handler = NULL;
194 fi->rpos = fi->rlen = 0;
200 file_write(struct main_file *fi, void *buf, uns len)
205 fi->write_handler = file_write_handler;
212 fi->write_handler = NULL;
214 fi->wpos = fi->wlen = 0;
220 file_set_timeout(struct main_file *fi, timestamp_t expires)
223 timer_add(&fi->timer, expires);
229 CLIST_FOR_EACH(struct main_file *, f, main_file_list)
234 hook_add(struct main_hook *ho)
236 DBG("MAIN: Adding hook %p", ho);
238 clist_add_tail(&main_hook_list, &ho->n);
242 hook_del(struct main_hook *ho)
244 DBG("MAIN: Deleting hook %p", ho);
246 clist_remove(&ho->n);
247 ho->n.next = ho->n.prev = NULL;
251 main_sigchld_handler(int x UNUSED)
253 DBG("SIGCHLD received");
257 process_add(struct main_process *mp)
259 DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
262 clist_add_tail(&main_process_list, &mp->n);
263 if (!main_sigchld_set_up)
266 bzero(&sa, sizeof(sa));
267 sa.sa_handler = main_sigchld_handler;
268 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
269 sigaction(SIGCHLD, &sa, NULL);
270 main_sigchld_set_up = 1;
275 process_del(struct main_process *mp)
277 DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
279 clist_remove(&mp->n);
284 process_fork(struct main_process *mp)
289 DBG("MAIN: Fork failed");
291 format_exit_status(mp->status_msg, -1);
299 DBG("MAIN: Forked process %d", (int) pid);
310 msg(L_DEBUG, "### Main loop status on %lld", (long long)main_now);
311 msg(L_DEBUG, "\tActive timers:");
312 struct main_timer *tm;
313 CLIST_WALK(tm, main_timer_list)
314 msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires ? tm->expires-main_now : 999999), tm->data);
315 struct main_file *fi;
316 msg(L_DEBUG, "\tActive files:");
317 CLIST_WALK(fi, main_file_list)
318 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %lld, data %p)",
319 fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler,
320 (long long)(fi->timer.expires ? fi->timer.expires-main_now : 999999), fi->data);
321 msg(L_DEBUG, "\tActive hooks:");
322 struct main_hook *ho;
323 CLIST_WALK(ho, main_hook_list)
324 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
325 msg(L_DEBUG, "\tActive processes:");
326 struct main_process *pr;
327 CLIST_WALK(pr, main_process_list)
328 msg(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
333 main_rebuild_poll_table(void)
335 struct main_file *fi;
336 if (main_poll_table_size < main_file_cnt)
339 xfree(main_poll_table);
341 main_poll_table_size = 1;
342 while (main_poll_table_size < main_file_cnt)
343 main_poll_table_size *= 2;
344 main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
346 struct pollfd *p = main_poll_table;
347 DBG("MAIN: Rebuilding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
348 CLIST_WALK(fi, main_file_list)
354 main_poll_table_obsolete = 0;
360 DBG("MAIN: Entering main_loop");
361 ASSERT(main_timer_list.head.next);
363 struct main_file *fi;
364 struct main_hook *ho;
365 struct main_timer *tm;
366 struct main_process *pr;
372 timestamp_t wake = main_now + 1000000000;
373 while ((tm = clist_head(&main_timer_list)) && tm->expires <= main_now)
375 DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(main_now - tm->expires));
378 int hook_min = HOOK_RETRY;
379 int hook_max = HOOK_SHUTDOWN;
380 CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
382 DBG("MAIN: Hook %p", ho);
383 int ret = ho->handler(ho);
384 hook_min = MIN(hook_min, ret);
385 hook_max = MAX(hook_max, ret);
387 if (hook_min == HOOK_SHUTDOWN ||
388 hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
391 DBG("MAIN: Shut down by %s", main_shutdown ? "main_shutdown" : "a hook");
394 if (hook_max == HOOK_RETRY)
396 if (main_poll_table_obsolete)
397 main_rebuild_poll_table();
398 if (!clist_empty(&main_process_list))
402 wake = MIN(wake, main_now + 10000);
403 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
405 DBG("MAIN: Child %d exited with status %x", pid, stat);
406 CLIST_WALK(pr, main_process_list)
411 format_exit_status(pr->status_msg, pr->status);
412 DBG("MAIN: Calling process exit handler");
419 /* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
420 if ((tm = clist_head(&main_timer_list)) && tm->expires < wake)
422 int timeout = (wake ? wake - main_now : 0);
423 DBG("MAIN: Poll for %d fds and timeout %d ms", main_file_cnt, timeout);
424 if (poll(main_poll_table, main_file_cnt, timeout))
426 struct pollfd *p = main_poll_table;
428 CLIST_WALK(fi, main_file_list)
430 if (p->revents & (POLLIN | POLLHUP | POLLERR))
433 DBG("MAIN: Read event on fd %d", p->fd);
434 while (fi->read_handler && fi->read_handler(fi) && !main_poll_table_obsolete);
435 if (main_poll_table_obsolete) /* File entries have been inserted or deleted => better not risk continuing to nowhere */
438 if (p->revents & (POLLOUT | POLLERR))
441 DBG("MAIN: Write event on fd %d", p->fd);
442 while (fi->write_handler && fi->write_handler(fi) && !main_poll_table_obsolete);
443 if (main_poll_table_obsolete)
454 static struct main_process mp;
455 static struct main_file fin, fout;
456 static struct main_hook hook;
457 static struct main_timer tm;
461 static void dread(struct main_file *fi)
463 if (fi->rpos < fi->rlen)
465 msg(L_INFO, "Read EOF");
470 msg(L_INFO, "Read done");
471 file_read(fi, rb, sizeof(rb));
475 static void derror(struct main_file *fi, int cause)
477 msg(L_INFO, "Error: %m !!! (cause %d)", cause);
481 static void dwrite(struct main_file *fi UNUSED)
483 msg(L_INFO, "Write done");
486 static int dhook(struct main_hook *ho UNUSED)
488 msg(L_INFO, "Hook called");
492 static void dtimer(struct main_timer *tm)
494 msg(L_INFO, "Timer tick");
495 timer_add(tm, main_now + 10000);
498 static void dentry(void)
500 msg(L_INFO, "*** SUBPROCESS START ***");
502 msg(L_INFO, "*** SUBPROCESS FINISH ***");
506 static void dexit(struct main_process *pr)
508 msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
518 fin.read_done = dread;
519 fin.error_handler = derror;
521 file_read(&fin, rb, sizeof(rb));
524 fout.write_done = dwrite;
525 fout.error_handler = derror;
527 file_write(&fout, "Hello, world!\n", 14);
529 hook.handler = dhook;
533 timer_add(&tm, main_now + 1000);
536 if (!process_fork(&mp))
542 msg(L_INFO, "Finished.");