2 * Sherlock 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.
13 #include "lib/mainloop.h"
29 clist main_timer_list, main_file_list, main_hook_list, main_process_list;
30 static uns main_file_cnt;
31 static uns main_poll_table_obsolete, main_poll_table_size;
32 static struct pollfd *main_poll_table;
33 static uns main_sigchld_set_up;
38 DBG("MAIN: Initializing");
39 clist_init(&main_timer_list);
40 clist_init(&main_file_list);
41 clist_init(&main_hook_list);
42 clist_init(&main_process_list);
47 timer_add(struct main_timer *tm, sh_time_t expires)
51 tm->expires = expires;
54 cnode *t = main_timer_list.head.next;
55 while (t != &main_timer_list.head && ((struct main_timer *) t)->expires < expires)
57 clist_insert_before(&tm->n, t);
62 timer_del(struct main_timer *tm)
68 file_timer_expired(struct main_timer *tm)
70 struct main_file *fi = tm->data;
71 timer_del(&fi->timer);
72 if (fi->error_handler)
73 fi->error_handler(fi, MFERR_TIMEOUT);
77 file_add(struct main_file *fi)
80 clist_add_tail(&main_file_list, &fi->n);
81 fi->timer.handler = file_timer_expired;
84 main_poll_table_obsolete = 1;
85 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
86 log(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
90 file_chg(struct main_file *fi)
92 struct pollfd *p = fi->pollfd;
97 p->events |= POLLIN | POLLHUP | POLLERR;
98 if (fi->write_handler)
99 p->events |= POLLOUT | POLLERR;
104 file_del(struct main_file *fi)
107 timer_del(&fi->timer);
108 clist_remove(&fi->n);
110 main_poll_table_obsolete = 1;
111 fi->n.next = fi->n.prev = NULL;
115 file_read_handler(struct main_file *fi)
117 while (fi->rpos < fi->rlen)
119 int l = read(fi->fd, fi->rbuf + fi->rpos, fi->rlen - fi->rpos);
120 DBG("MAIN: FD %d: read %d", fi->fd, l);
123 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
124 fi->error_handler(fi, MFERR_READ);
131 DBG("MAIN: FD %d done read %d of %d", fi->fd, fi->rpos, fi->rlen);
132 fi->read_handler = NULL;
139 file_write_handler(struct main_file *fi)
141 while (fi->wpos < fi->wlen)
143 int l = write(fi->fd, fi->wbuf + fi->wpos, fi->wlen - fi->wpos);
144 DBG("MAIN: FD %d: write %d", fi->fd, l);
147 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
148 fi->error_handler(fi, MFERR_WRITE);
153 DBG("MAIN: FD %d done write %d", fi->fd, fi->wpos);
154 fi->write_handler = NULL;
161 file_read(struct main_file *fi, void *buf, uns len)
166 fi->read_handler = file_read_handler;
173 fi->read_handler = NULL;
175 fi->rpos = fi->rlen = 0;
181 file_write(struct main_file *fi, void *buf, uns len)
186 fi->write_handler = file_write_handler;
193 fi->write_handler = NULL;
195 fi->wpos = fi->wlen = 0;
201 file_set_timeout(struct main_file *fi, sh_time_t expires)
204 timer_add(&fi->timer, expires);
208 hook_add(struct main_hook *ho)
211 clist_add_tail(&main_hook_list, &ho->n);
215 hook_del(struct main_hook *ho)
218 clist_remove(&ho->n);
219 ho->n.next = ho->n.prev = NULL;
223 main_sigchld_handler(int x UNUSED)
225 DBG("SIGCHLD received");
229 process_add(struct main_process *mp)
233 clist_add_tail(&main_process_list, &mp->n);
234 if (!main_sigchld_set_up)
237 bzero(&sa, sizeof(sa));
238 sa.sa_handler = main_sigchld_handler;
239 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
240 sigaction(SIGCHLD, &sa, NULL);
241 main_sigchld_set_up = 1;
246 process_del(struct main_process *mp)
249 clist_remove(&mp->n);
255 process_fork(struct main_process *mp)
261 DBG("MAIN: Fork failed");
263 format_exit_status(mp->status_msg, -1);
271 DBG("MAIN: Forked process %d", (int) pid);
282 log(L_DEBUG, "### Main loop status on %d", (int)now);
283 log(L_DEBUG, "\tActive timers:");
284 struct main_timer *tm;
285 CLIST_WALK(tm, main_timer_list)
286 log(L_DEBUG, "\t\t%p (expires %d, data %p)", tm, (int)tm->expires, tm->data);
287 struct main_file *fi;
288 log(L_DEBUG, "\tActive files:");
289 CLIST_WALK(fi, main_file_list)
290 log(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %d, data %p)",
291 fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler, fi->timer.expires, fi->data);
292 log(L_DEBUG, "\tActive hooks:");
293 struct main_hook *ho;
294 CLIST_WALK(ho, main_hook_list)
295 log(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
296 log(L_DEBUG, "\tActive processes:");
297 struct main_process *pr;
298 CLIST_WALK(pr, main_process_list)
299 log(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
304 main_rebuild_poll_table(void)
306 struct main_file *fi;
307 if (main_poll_table_size < main_file_cnt)
310 xfree(main_poll_table);
312 main_poll_table_size = 1;
313 while (main_poll_table_size < main_file_cnt)
314 main_poll_table_size *= 2;
315 main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
317 struct pollfd *p = main_poll_table;
318 DBG("MAIN: Rebuliding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
319 CLIST_WALK(fi, main_file_list)
325 main_poll_table_obsolete = 0;
331 DBG("MAIN: Entering main_loop");
332 ASSERT(main_timer_list.head.next);
334 struct main_file *fi;
335 struct main_hook *ho;
336 struct main_timer *tm;
337 struct main_process *pr;
340 while (!main_shutdown)
343 sh_time_t wake = now + 60;
344 while ((tm = clist_head(&main_timer_list)) && tm->expires <= now)
346 DBG("MAIN: Timer %p expired", tm);
349 CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
351 DBG("MAIN: Hook %p", ho);
355 if (main_poll_table_obsolete)
356 main_rebuild_poll_table();
357 if (!clist_empty(&main_process_list))
361 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
363 DBG("MAIN: Child %d exited with status %x", pid, stat);
364 CLIST_WALK(pr, main_process_list)
369 format_exit_status(pr->status_msg, pr->status);
370 DBG("MAIN: Calling process exit handler");
377 /* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
378 if ((tm = clist_head(&main_timer_list)) && tm->expires < wake)
380 int timeout = (wake ? (wake - now) * 1000 : 0);
381 DBG("MAIN: Poll for %d fds and timeout %d ms", main_file_cnt, timeout);
382 if (poll(main_poll_table, main_file_cnt, timeout))
384 struct pollfd *p = main_poll_table;
386 CLIST_WALK(fi, main_file_list)
388 if (p->revents & (POLLIN | POLLHUP | POLLERR))
391 DBG("MAIN: Read event on fd %d", p->fd);
392 while (fi->read_handler && fi->read_handler(fi) && !main_poll_table_obsolete);
393 if (main_poll_table_obsolete) /* File entries have been inserted or deleted => better not risk continuing to nowhere */
396 if (p->revents & (POLLOUT | POLLERR))
399 DBG("MAIN: Write event on fd %d", p->fd);
400 while (fi->write_handler && fi->write_handler(fi) && !main_poll_table_obsolete);
401 if (main_poll_table_obsolete)
412 static struct main_process mp;
413 static struct main_file fin, fout;
414 static struct main_hook hook;
415 static struct main_timer tm;
419 static void dread(struct main_file *fi)
421 if (fi->rpos < fi->rlen)
423 log(L_INFO, "Read EOF");
428 log(L_INFO, "Read done");
429 file_read(fi, rb, sizeof(rb));
433 static void derror(struct main_file *fi, int cause)
435 log(L_INFO, "Error: %m !!! (cause %d)", cause);
439 static void dwrite(struct main_file *fi UNUSED)
441 log(L_INFO, "Write done");
444 static int dhook(struct main_hook *ho UNUSED)
446 log(L_INFO, "Hook called");
450 static void dtimer(struct main_timer *tm)
452 log(L_INFO, "Timer tick");
453 timer_add(tm, now + 10);
456 static void dentry(struct main_process *pr UNUSED)
458 log(L_INFO, "*** SUBPROCESS START ***");
460 log(L_INFO, "*** SUBPROCESS FINISH ***");
463 static void dexit(struct main_process *pr)
465 log(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
475 fin.read_done = dread;
476 fin.error_handler = derror;
478 file_read(&fin, rb, sizeof(rb));
481 fout.write_done = dwrite;
482 fout.error_handler = derror;
484 file_write(&fout, "Hello, world!\n", 14);
486 hook.handler = dhook;
490 timer_add(&tm, now + 1);
493 if (!process_fork(&mp))
499 log(L_INFO, "Finished.");