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"
28 static clist main_timer_list, main_file_list, main_hook_list, main_process_list;
29 static uns main_file_cnt;
30 static uns main_poll_table_obsolete, main_poll_table_size;
31 static struct pollfd *main_poll_table;
32 static uns main_sigchld_set_up;
37 DBG("MAIN: Initializing");
38 clist_init(&main_timer_list);
39 clist_init(&main_file_list);
40 clist_init(&main_hook_list);
41 clist_init(&main_process_list);
46 timer_add(struct main_timer *tm, sh_time_t expires)
50 tm->expires = expires;
53 cnode *t = main_timer_list.head.next;
54 while (t != &main_timer_list.head && ((struct main_timer *) t)->expires < expires)
56 clist_insert_before(&tm->n, t);
61 timer_del(struct main_timer *tm)
67 file_timer_expired(struct main_timer *tm)
69 struct main_file *fi = tm->data;
70 timer_del(&fi->timer);
71 if (fi->error_handler)
72 fi->error_handler(fi, MFERR_TIMEOUT);
76 file_add(struct main_file *fi)
79 clist_add_tail(&main_file_list, &fi->n);
80 fi->timer.handler = file_timer_expired;
83 main_poll_table_obsolete = 1;
84 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
85 log(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
89 file_chg(struct main_file *fi)
91 struct pollfd *p = fi->pollfd;
96 p->events |= POLLIN | POLLHUP | POLLERR;
97 if (fi->write_handler)
98 p->events |= POLLOUT | POLLERR;
103 file_del(struct main_file *fi)
106 timer_del(&fi->timer);
107 clist_remove(&fi->n);
109 main_poll_table_obsolete = 1;
110 fi->n.next = fi->n.prev = NULL;
114 file_read_handler(struct main_file *fi)
116 while (fi->rpos < fi->rlen)
118 int l = read(fi->fd, fi->rbuf + fi->rpos, fi->rlen - fi->rpos);
119 DBG("MAIN: FD %d: read %d", fi->fd, l);
122 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
123 fi->error_handler(fi, MFERR_READ);
130 DBG("MAIN: FD %d done read %d of %d", fi->fd, fi->rpos, fi->rlen);
131 fi->read_handler = NULL;
138 file_write_handler(struct main_file *fi)
140 while (fi->wpos < fi->wlen)
142 int l = write(fi->fd, fi->wbuf + fi->wpos, fi->wlen - fi->wpos);
143 DBG("MAIN: FD %d: write %d", fi->fd, l);
146 if (errno != EINTR && errno != EAGAIN && fi->error_handler)
147 fi->error_handler(fi, MFERR_WRITE);
152 DBG("MAIN: FD %d done write %d", fi->fd, fi->wpos);
153 fi->write_handler = NULL;
160 file_read(struct main_file *fi, byte *buf, uns len)
165 fi->read_handler = file_read_handler;
172 fi->read_handler = NULL;
174 fi->rpos = fi->rlen = 0;
180 file_write(struct main_file *fi, byte *buf, uns len)
185 fi->write_handler = file_write_handler;
192 fi->write_handler = NULL;
194 fi->wpos = fi->wlen = 0;
200 file_set_timeout(struct main_file *fi, sh_time_t expires)
203 timer_add(&fi->timer, expires);
207 hook_add(struct main_hook *ho)
210 clist_add_tail(&main_hook_list, &ho->n);
214 hook_del(struct main_hook *ho)
217 clist_remove(&ho->n);
218 ho->n.next = ho->n.prev = NULL;
222 main_sigchld_handler(int x UNUSED)
224 DBG("SIGCHLD received");
228 process_add(struct main_process *mp)
231 clist_add_tail(&main_process_list, &mp->n);
232 if (!main_sigchld_set_up)
235 bzero(&sa, sizeof(sa));
236 sa.sa_handler = main_sigchld_handler;
237 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
238 sigaction(SIGCHLD, &sa, NULL);
239 main_sigchld_set_up = 1;
244 process_del(struct main_process *mp)
247 clist_remove(&mp->n);
253 process_fork(struct main_process *mp)
262 DBG("MAIN: Forked process %d", (int) pid);
273 log(L_DEBUG, "### Main loop status on %d", (int)now);
274 log(L_DEBUG, "\tActive timers:");
275 struct main_timer *tm;
276 CLIST_WALK(tm, main_timer_list)
277 log(L_DEBUG, "\t\t%p (expires %d, data %p)", tm, (int)tm->expires, tm->data);
278 struct main_file *fi;
279 log(L_DEBUG, "\tActive files:");
280 CLIST_WALK(fi, main_file_list)
281 log(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %d, data %p)",
282 fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler, fi->timer.expires, fi->data);
283 log(L_DEBUG, "\tActive hooks:");
284 struct main_hook *ho;
285 CLIST_WALK(ho, main_hook_list)
286 log(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
287 log(L_DEBUG, "\tActive processes:");
288 struct main_process *pr;
289 CLIST_WALK(pr, main_process_list)
290 log(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
295 main_rebuild_poll_table(void)
297 struct main_file *fi;
298 if (main_poll_table_size < main_file_cnt)
301 xfree(main_poll_table);
303 main_poll_table_size = 1;
304 while (main_poll_table_size < main_file_cnt)
305 main_poll_table_size *= 2;
306 main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
308 struct pollfd *p = main_poll_table;
309 DBG("MAIN: Rebuliding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
310 CLIST_WALK(fi, main_file_list)
316 main_poll_table_obsolete = 0;
322 DBG("MAIN: Entering main_loop");
323 ASSERT(main_timer_list.head.next);
325 struct main_file *fi;
326 struct main_hook *ho;
327 struct main_timer *tm;
328 struct main_process *pr;
331 while (!main_shutdown)
334 sh_time_t wake = now + 60;
335 while ((tm = clist_head(&main_timer_list)) && tm->expires <= now)
337 DBG("MAIN: Timer %p expired", tm);
340 CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
342 DBG("MAIN: Hook %p", ho);
346 if (main_poll_table_obsolete)
347 main_rebuild_poll_table();
348 if (!clist_empty(&main_process_list))
352 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
354 DBG("MAIN: Child %d exited with status %x", pid, stat);
355 CLIST_WALK(pr, main_process_list)
360 DBG("MAIN: Calling process exit handler");
367 /* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
368 if ((tm = clist_head(&main_timer_list)) && tm->expires < wake)
370 int timeout = (wake ? (wake - now) * 1000 : 0);
371 DBG("MAIN: Poll for %d fds and timeout %d ms", main_file_cnt, timeout);
372 if (poll(main_poll_table, main_file_cnt, timeout))
374 struct pollfd *p = main_poll_table;
376 CLIST_WALK(fi, main_file_list)
378 if (p->revents & (POLLIN | POLLHUP | POLLERR))
381 DBG("MAIN: Read event on fd %d", p->fd);
382 while (fi->read_handler && fi->read_handler(fi) && !main_poll_table_obsolete);
383 if (main_poll_table_obsolete) /* File entries have been inserted or deleted => better not risk continuing to nowhere */
386 if (p->revents & (POLLOUT | POLLERR))
389 DBG("MAIN: Write event on fd %d", p->fd);
390 while (fi->write_handler && fi->write_handler(fi) && !main_poll_table_obsolete);
391 if (main_poll_table_obsolete)
402 static struct main_process mp;
403 static struct main_file fin, fout;
404 static struct main_hook hook;
405 static struct main_timer tm;
409 static void dread(struct main_file *fi)
411 if (fi->rpos < fi->rlen)
413 log(L_INFO, "Read EOF");
418 log(L_INFO, "Read done");
419 file_read(fi, rb, sizeof(rb));
423 static void derror(struct main_file *fi, int cause)
425 log(L_INFO, "Error: %m !!! (cause %d)", cause);
429 static void dwrite(struct main_file *fi UNUSED)
431 log(L_INFO, "Write done");
434 static int dhook(struct main_hook *ho UNUSED)
436 log(L_INFO, "Hook called");
440 static void dtimer(struct main_timer *tm)
442 log(L_INFO, "Timer tick");
443 timer_add(tm, now + 10);
446 static void dentry(struct main_process *pr UNUSED)
448 log(L_INFO, "*** SUBPROCESS START ***");
450 log(L_INFO, "*** SUBPROCESS FINISH ***");
453 static void dexit(struct main_process *pr)
455 log(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
465 fin.read_done = dread;
466 fin.error_handler = derror;
468 file_read(&fin, rb, sizeof(rb));
471 fout.write_done = dwrite;
472 fout.error_handler = derror;
474 file_write(&fout, "Hello, world!\n", 14);
476 hook.handler = dhook;
480 timer_add(&tm, now + 1);
483 if (!process_fork(&mp))
489 log(L_INFO, "Finished.");