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 "lib/mainloop.h"
27 sh_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 %Ld 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+%Ld)", 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 log(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);
285 process_fork(struct main_process *mp)
291 DBG("MAIN: Fork failed");
293 format_exit_status(mp->status_msg, -1);
301 DBG("MAIN: Forked process %d", (int) pid);
312 log(L_DEBUG, "### Main loop status on %Ld", (long long)main_now);
313 log(L_DEBUG, "\tActive timers:");
314 struct main_timer *tm;
315 CLIST_WALK(tm, main_timer_list)
316 log(L_DEBUG, "\t\t%p (expires %Ld, data %p)", tm, (long long)(tm->expires ? tm->expires-main_now : 999999), tm->data);
317 struct main_file *fi;
318 log(L_DEBUG, "\tActive files:");
319 CLIST_WALK(fi, main_file_list)
320 log(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %Ld, data %p)",
321 fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler,
322 (long long)(fi->timer.expires ? fi->timer.expires-main_now : 999999), fi->data);
323 log(L_DEBUG, "\tActive hooks:");
324 struct main_hook *ho;
325 CLIST_WALK(ho, main_hook_list)
326 log(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
327 log(L_DEBUG, "\tActive processes:");
328 struct main_process *pr;
329 CLIST_WALK(pr, main_process_list)
330 log(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
335 main_rebuild_poll_table(void)
337 struct main_file *fi;
338 if (main_poll_table_size < main_file_cnt)
341 xfree(main_poll_table);
343 main_poll_table_size = 1;
344 while (main_poll_table_size < main_file_cnt)
345 main_poll_table_size *= 2;
346 main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
348 struct pollfd *p = main_poll_table;
349 DBG("MAIN: Rebuilding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
350 CLIST_WALK(fi, main_file_list)
356 main_poll_table_obsolete = 0;
362 DBG("MAIN: Entering main_loop");
363 ASSERT(main_timer_list.head.next);
365 struct main_file *fi;
366 struct main_hook *ho;
367 struct main_timer *tm;
368 struct main_process *pr;
374 timestamp_t wake = main_now + 1000000000;
375 while ((tm = clist_head(&main_timer_list)) && tm->expires <= main_now)
377 DBG("MAIN: Timer %p expired at now-%Ld", tm, (long long)(main_now - tm->expires));
380 int hook_min = HOOK_RETRY;
381 int hook_max = HOOK_SHUTDOWN;
382 CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
384 DBG("MAIN: Hook %p", ho);
385 int ret = ho->handler(ho);
386 hook_min = MIN(hook_min, ret);
387 hook_max = MAX(hook_max, ret);
389 if (hook_min == HOOK_SHUTDOWN ||
390 hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
393 DBG("MAIN: Shut down by %s", main_shutdown ? "main_shutdown" : "a hook");
396 if (hook_max == HOOK_RETRY)
398 if (main_poll_table_obsolete)
399 main_rebuild_poll_table();
400 if (!clist_empty(&main_process_list))
404 wake = MIN(wake, main_now + 10000);
405 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
407 DBG("MAIN: Child %d exited with status %x", pid, stat);
408 CLIST_WALK(pr, main_process_list)
413 format_exit_status(pr->status_msg, pr->status);
414 DBG("MAIN: Calling process exit handler");
421 /* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
422 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 if (poll(main_poll_table, main_file_cnt, timeout))
428 struct pollfd *p = main_poll_table;
430 CLIST_WALK(fi, main_file_list)
432 if (p->revents & (POLLIN | POLLHUP | POLLERR))
435 DBG("MAIN: Read event on fd %d", p->fd);
436 while (fi->read_handler && fi->read_handler(fi) && !main_poll_table_obsolete);
437 if (main_poll_table_obsolete) /* File entries have been inserted or deleted => better not risk continuing to nowhere */
440 if (p->revents & (POLLOUT | POLLERR))
443 DBG("MAIN: Write event on fd %d", p->fd);
444 while (fi->write_handler && fi->write_handler(fi) && !main_poll_table_obsolete);
445 if (main_poll_table_obsolete)
456 static struct main_process mp;
457 static struct main_file fin, fout;
458 static struct main_hook hook;
459 static struct main_timer tm;
463 static void dread(struct main_file *fi)
465 if (fi->rpos < fi->rlen)
467 log(L_INFO, "Read EOF");
472 log(L_INFO, "Read done");
473 file_read(fi, rb, sizeof(rb));
477 static void derror(struct main_file *fi, int cause)
479 log(L_INFO, "Error: %m !!! (cause %d)", cause);
483 static void dwrite(struct main_file *fi UNUSED)
485 log(L_INFO, "Write done");
488 static int dhook(struct main_hook *ho UNUSED)
490 log(L_INFO, "Hook called");
494 static void dtimer(struct main_timer *tm)
496 log(L_INFO, "Timer tick");
497 timer_add(tm, main_now + 10000);
500 static void dentry(void)
502 log(L_INFO, "*** SUBPROCESS START ***");
504 log(L_INFO, "*** SUBPROCESS FINISH ***");
508 static void dexit(struct main_process *pr)
510 log(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
520 fin.read_done = dread;
521 fin.error_handler = derror;
523 file_read(&fin, rb, sizeof(rb));
526 fout.write_done = dwrite;
527 fout.error_handler = derror;
529 file_write(&fout, "Hello, world!\n", 14);
531 hook.handler = dhook;
535 timer_add(&tm, main_now + 1000);
538 if (!process_fork(&mp))
544 log(L_INFO, "Finished.");