]> mj.ucw.cz Git - libucw.git/blob - ucw/mainloop.c
Main: Unified support for poll and epoll
[libucw.git] / ucw / mainloop.c
1 /*
2  *      UCW Library -- Main Loop
3  *
4  *      (c) 2004--2010 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "ucw/lib.h"
13 #include "ucw/heap.h"
14 #include "ucw/mainloop.h"
15 #include "ucw/threads.h"
16 #include "ucw/gary.h"
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <sys/poll.h>
27 #include <sys/wait.h>
28 #include <sys/time.h>
29
30 #ifdef CONFIG_UCW_THREADS
31 #include <pthread.h>
32 #define THREAD_SIGMASK pthread_sigmask
33 #else
34 #define THREAD_SIGMASK sigprocmask
35 #endif
36
37 #ifdef CONFIG_UCW_EPOLL
38 #include <sys/epoll.h>
39 #endif
40
41 #define MAIN_TIMER_LESS(x,y) ((x)->expires < (y)->expires)
42 #define MAIN_TIMER_SWAP(heap,a,b,t) (t=heap[a], heap[a]=heap[b], heap[b]=t, heap[a]->index=(a), heap[b]->index=(b))
43
44 #define EPOLL_BUF_SIZE 256
45
46 static void
47 do_main_get_time(struct main_context *m)
48 {
49   struct timeval tv;
50   gettimeofday(&tv, NULL);
51   m->now_seconds = tv.tv_sec;
52   m->now = (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
53 }
54
55 struct main_context *
56 main_new(void)
57 {
58   struct main_context *m = xmalloc_zero(sizeof(*m));
59
60   DBG("MAIN: New context");
61   clist_init(&m->file_list);
62   clist_init(&m->file_active_list);
63   clist_init(&m->hook_list);
64   clist_init(&m->hook_done_list);
65   clist_init(&m->process_list);
66   clist_init(&m->signal_list);
67 #ifdef CONFIG_UCW_EPOLL
68   m->epoll_fd = epoll_create(64);
69   if (m->epoll_fd < 0)
70     die("epoll_create() failed: %m");
71   m->epoll_events = xmalloc(EPOLL_BUF_SIZE * sizeof(struct epoll_event *));
72 #else
73   m->poll_table_obsolete = 1;
74 #endif
75   do_main_get_time(m);
76   sigemptyset(&m->want_signals);
77   m->sig_pipe_recv = m->sig_pipe_send = -1;
78
79   return m;
80 }
81
82 void
83 main_delete(struct main_context *m)
84 {
85   if (m->sigchld_handler)
86     signal_del(m->sigchld_handler);
87   if (m->sig_pipe_file)
88     file_del(m->sig_pipe_file);
89   if (m->sig_pipe_recv >= 0)
90     {
91       close(m->sig_pipe_recv);
92       close(m->sig_pipe_send);
93     }
94   ASSERT(clist_empty(&m->file_list));
95   ASSERT(clist_empty(&m->file_active_list));
96   ASSERT(clist_empty(&m->hook_list));
97   ASSERT(clist_empty(&m->hook_done_list));
98   ASSERT(clist_empty(&m->process_list));
99   ASSERT(clist_empty(&m->signal_list));
100   GARY_FREE(m->timer_table);
101 #ifdef CONFIG_UCW_EPOLL
102   xfree(m->epoll_events);
103   close(m->epoll_fd);
104 #else
105   GARY_FREE(m->poll_table);
106   GARY_FREE(m->poll_file_table);
107 #endif
108   xfree(m);
109   // FIXME: Some mechanism for cleaning up after fork()
110 }
111
112 struct main_context *
113 main_switch_context(struct main_context *m)
114 {
115   struct ucwlib_context *c = ucwlib_thread_context();
116   struct main_context *m0 = c->main_context;
117
118   /*
119    *  Not only we need to switch the signal sets of the two contexts,
120    *  but it is also necessary to avoid invoking a signal handler
121    *  in the middle of changing c->main_context.
122    */
123   if (m0 && !clist_empty(&m0->signal_list))
124     THREAD_SIGMASK(SIG_BLOCK, &m0->want_signals, NULL);
125   c->main_context = m;
126   if (m && !clist_empty(&m->signal_list))
127     THREAD_SIGMASK(SIG_UNBLOCK, &m->want_signals, NULL);
128
129   return m0;
130 }
131
132 struct main_context *
133 main_current(void)
134 {
135   struct ucwlib_context *c = ucwlib_thread_context();
136   struct main_context *m = c->main_context;
137   ASSERT(m);
138   return m;
139 }
140
141 void
142 main_init(void)
143 {
144   struct main_context *m = main_switch_context(main_new());
145   ASSERT(!m);
146 }
147
148 void
149 main_cleanup(void)
150 {
151   struct main_context *m = main_switch_context(NULL);
152   main_delete(m);
153 }
154
155 void
156 main_get_time(void)
157 {
158   do_main_get_time(main_current());
159 }
160
161 static inline uns
162 count_timers(struct main_context *m)
163 {
164   return GARY_SIZE(m->timer_table) - 1;
165 }
166
167 void
168 timer_add(struct main_timer *tm, timestamp_t expires)
169 {
170   struct main_context *m = main_current();
171
172   if (!m->timer_table)
173     {
174       GARY_INIT(m->timer_table, 1);
175       m->timer_table[0] = NULL;
176     }
177
178   if (expires)
179     DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires - m->now));
180   else
181     DBG("MAIN: Clearing timer %p", tm);
182   uns num_timers = count_timers(m);
183   if (tm->expires < expires)
184     {
185       if (!tm->expires)
186         {
187           tm->expires = expires;
188           tm->index = num_timers + 1;
189           *GARY_PUSH(m->timer_table, 1) = tm;
190           HEAP_INSERT(struct main_timer *, m->timer_table, tm->index, MAIN_TIMER_LESS, MAIN_TIMER_SWAP);
191         }
192       else
193         {
194           tm->expires = expires;
195           HEAP_INCREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
196         }
197     }
198   else if (tm->expires > expires)
199     {
200       if (!expires)
201         {
202           ASSERT(tm->index && tm->index <= num_timers);
203           HEAP_DELETE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
204           tm->index = 0;
205           tm->expires = 0;
206           GARY_POP(m->timer_table, 1);
207         }
208       else
209         {
210           tm->expires = expires;
211           HEAP_DECREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
212         }
213     }
214 }
215
216 void
217 timer_add_rel(struct main_timer *tm, timestamp_t expires_delta)
218 {
219   struct main_context *m = main_current();
220   return timer_add(tm, m->now + expires_delta);
221 }
222
223 void
224 timer_del(struct main_timer *tm)
225 {
226   timer_add(tm, 0);
227 }
228
229 static uns
230 file_want_events(struct main_file *fi)
231 {
232   uns events = 0;
233   if (fi->read_handler)
234     events |= POLLIN | POLLHUP | POLLERR;
235   if (fi->write_handler)
236     events |= POLLOUT | POLLERR;
237   return events;
238 }
239
240 void
241 file_add(struct main_file *fi)
242 {
243   struct main_context *m = main_current();
244
245   DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
246   ASSERT(!clist_is_linked(&fi->n));
247   clist_add_tail(&m->file_list, &fi->n);
248   m->file_cnt++;
249 #ifdef CONFIG_UCW_EPOLL
250   struct epoll_event evt = {
251     .events = file_want_events(fi),
252     .data.ptr = fi,
253   };
254   if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, fi->fd, &evt) < 0)
255     die("epoll_ctl() failed: %m");
256 #else
257   m->poll_table_obsolete = 1;
258 #endif
259   if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
260     msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
261 }
262
263 void
264 file_chg(struct main_file *fi)
265 {
266 #ifdef CONFIG_UCW_EPOLL
267   struct epoll_event evt = {
268     .events = file_want_events(fi),
269     .data.ptr = fi,
270   };
271   if (epoll_ctl(main_current()->epoll_fd, EPOLL_CTL_MOD, fi->fd, &evt) < 0)
272     die("epoll_ctl() failed: %m");
273 #else
274   struct pollfd *p = fi->pollfd;
275   if (p)
276     p->events = file_want_events(fi);
277 #endif
278 }
279
280 void
281 file_del(struct main_file *fi)
282 {
283   struct main_context *m = main_current();
284
285   DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
286   ASSERT(clist_is_linked(&fi->n));
287   clist_unlink(&fi->n);
288   m->file_cnt--;
289 #ifdef CONFIG_UCW_EPOLL
290   if (epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, fi->fd, NULL) < 0)
291     die("epoll_ctl() failed: %m");
292 #else
293   m->poll_table_obsolete = 1;
294 #endif
295 }
296
297 void
298 file_close_all(void)
299 {
300   struct main_context *m = main_current();
301
302   CLIST_FOR_EACH(struct main_file *, f, m->file_list)
303     close(f->fd);
304 }
305
306 void
307 hook_add(struct main_hook *ho)
308 {
309   struct main_context *m = main_current();
310
311   DBG("MAIN: Adding hook %p", ho);
312   ASSERT(!clist_is_linked(&ho->n));
313   clist_add_tail(&m->hook_list, &ho->n);
314 }
315
316 void
317 hook_del(struct main_hook *ho)
318 {
319   DBG("MAIN: Deleting hook %p", ho);
320   ASSERT(clist_is_linked(&ho->n));
321   clist_unlink(&ho->n);
322 }
323
324 static void
325 sigchld_received(struct main_signal *sg UNUSED)
326 {
327   struct main_context *m = main_current();
328   int stat;
329   pid_t pid;
330
331   while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
332     {
333       DBG("MAIN: Child %d exited with status %x", pid, stat);
334       CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
335         if (pr->pid == pid)
336           {
337             pr->status = stat;
338             process_del(pr);
339             format_exit_status(pr->status_msg, pr->status);
340             DBG("MAIN: Calling process exit handler");
341             pr->handler(pr);
342             break;
343           }
344     }
345 }
346
347 void
348 process_add(struct main_process *mp)
349 {
350   struct main_context *m = main_current();
351
352   DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
353   ASSERT(!clist_is_linked(&mp->n));
354   ASSERT(mp->handler);
355   clist_add_tail(&m->process_list, &mp->n);
356   if (!m->sigchld_handler)
357     {
358       struct main_signal *sg = xmalloc_zero(sizeof(*sg));
359       m->sigchld_handler = sg;
360       sg->signum = SIGCHLD;
361       sg->handler = sigchld_received;
362       signal_add(sg);
363     }
364 }
365
366 void
367 process_del(struct main_process *mp)
368 {
369   DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
370   ASSERT(clist_is_linked(&mp->n));
371   clist_unlink(&mp->n);
372 }
373
374 int
375 process_fork(struct main_process *mp)
376 {
377   pid_t pid = fork();
378   if (pid < 0)
379     {
380       DBG("MAIN: Fork failed");
381       mp->status = -1;
382       format_exit_status(mp->status_msg, -1);
383       mp->handler(mp);
384       return 1;
385     }
386   else if (!pid)
387     return 0;
388   else
389     {
390       DBG("MAIN: Forked process %d", (int) pid);
391       mp->pid = pid;
392       process_add(mp);
393       return 1;
394     }
395 }
396
397 static int
398 pipe_read_handler(struct main_file *mf UNUSED)
399 {
400   struct main_context *m = main_current();
401   int signum;
402   int n = read(m->sig_pipe_recv, &signum, sizeof(signum));
403
404   if (n < 0)
405     {
406       if (errno != EAGAIN)
407         msg(L_ERROR, "Error reading signal pipe: %m");
408       return 0;
409     }
410   ASSERT(n == sizeof(signum));
411
412   DBG("MAIN: Sigpipe: received signal %d", signum);
413   struct main_signal iter = { .signum = -1 };
414   struct main_signal *sg = clist_head(&m->signal_list);
415   while (sg)
416     {
417       if (sg->signum == signum)
418         {
419           DBG("MAIN: Sigpipe: invoking handler %p", sg);
420           clist_insert_after(&iter.n, &sg->n);
421           sg->handler(sg);
422           sg = clist_next(&m->signal_list, &iter.n);
423           clist_remove(&iter.n);
424         }
425       else
426         sg = clist_next(&m->signal_list, &sg->n);
427     }
428
429   return 1;
430 }
431
432 static void
433 pipe_configure(int fd)
434 {
435   int flags;
436   if ((flags = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
437     die("Could not set file descriptor %d to non-blocking: %m", fd);
438 }
439
440 static void
441 pipe_setup(struct main_context *m)
442 {
443   DBG("MAIN: Sigpipe: Setting up the pipe");
444
445   int pipe_result[2];
446   if (pipe(pipe_result) == -1)
447     die("Could not create signal pipe: %m");
448   pipe_configure(pipe_result[0]);
449   pipe_configure(pipe_result[1]);
450   m->sig_pipe_recv = pipe_result[0];
451   m->sig_pipe_send = pipe_result[1];
452
453   struct main_file *f = xmalloc_zero(sizeof(*f));
454   m->sig_pipe_file = f;
455   f->fd = m->sig_pipe_recv;
456   f->read_handler = pipe_read_handler;
457   file_add(f);
458 }
459
460 static void
461 signal_handler_pipe(int signum)
462 {
463   struct main_context *m = main_current();
464 #ifdef LOCAL_DEBUG
465   msg(L_DEBUG | L_SIGHANDLER, "MAIN: Sigpipe: sending signal %d down the drain", signum);
466 #endif
467   write(m->sig_pipe_send, &signum, sizeof(signum));
468 }
469
470 void
471 signal_add(struct main_signal *ms)
472 {
473   struct main_context *m = main_current();
474
475   DBG("MAIN: Adding signal %p (sig=%d)", ms, ms->signum);
476
477   ASSERT(!clist_is_linked(&ms->n));
478   // Adding at the head of the list is better if we are in the middle of walking the list.
479   clist_add_head(&m->signal_list, &ms->n);
480   if (m->sig_pipe_recv < 0)
481     pipe_setup(m);
482
483   struct sigaction sa = {
484     .sa_handler = signal_handler_pipe,
485     .sa_flags = SA_NOCLDSTOP | SA_RESTART,
486   };
487   sigaction(ms->signum, &sa, NULL);
488
489   sigset_t ss;
490   sigemptyset(&ss);
491   sigaddset(&ss, ms->signum);
492   THREAD_SIGMASK(SIG_UNBLOCK, &ss, NULL);
493   sigaddset(&m->want_signals, ms->signum);
494 }
495
496 void
497 signal_del(struct main_signal *ms)
498 {
499   struct main_context *m = main_current();
500
501   DBG("MAIN: Deleting signal %p (sig=%d)", ms, ms->signum);
502
503   ASSERT(clist_is_linked(&ms->n));
504   clist_unlink(&ms->n);
505
506   int another = 0;
507   CLIST_FOR_EACH(struct main_signal *, s, m->signal_list)
508     if (s->signum == ms->signum)
509       another++;
510   if (!another)
511     {
512       sigset_t ss;
513       sigemptyset(&ss);
514       sigaddset(&ss, ms->signum);
515       THREAD_SIGMASK(SIG_BLOCK, &ss, NULL);
516       sigdelset(&m->want_signals, ms->signum);
517     }
518 }
519
520 void
521 main_debug_context(struct main_context *m UNUSED)
522 {
523 #ifdef CONFIG_DEBUG
524   msg(L_DEBUG, "### Main loop status on %lld", (long long) m->now);
525   msg(L_DEBUG, "\tActive timers:");
526   uns num_timers = count_timers(m);
527   for (uns i = 1; i <= num_timers; i++)
528     {
529       struct main_timer *tm = m->timer_table[i];
530       msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires ? tm->expires - m->now : 999999), tm->data);
531     }
532   msg(L_DEBUG, "\tActive files:");
533   CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
534     msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p)",
535         fi, fi->fd, fi->read_handler, fi->write_handler, fi->data);
536   CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
537     msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p) [pending events: %x]",
538         fi, fi->fd, fi->read_handler, fi->write_handler, fi->data, fi->events);
539     // FIXME: Can we display status of block_io requests somehow?
540   msg(L_DEBUG, "\tActive hooks:");
541   CLIST_FOR_EACH(struct main_hook *, ho, m->hook_done_list)
542     msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
543   CLIST_FOR_EACH(struct main_hook *, ho, m->hook_list)
544     msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
545   msg(L_DEBUG, "\tActive processes:");
546   CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
547     msg(L_DEBUG, "\t\t%p (pid %d, func %p, data %p)", pr, pr->pid, pr->handler, pr->data);
548   msg(L_DEBUG, "\tActive signal catchers:");
549   CLIST_FOR_EACH(struct main_signal *, sg, m->signal_list)
550     if (sg->signum < 0)
551       msg(L_DEBUG, "\t\t(placeholder)");
552     else
553       msg(L_DEBUG, "\t\t%p (sig %d, func %p, data %p)", sg, sg->signum, sg->handler, sg->data);
554 #endif
555 }
556
557 static void
558 process_timers(struct main_context *m)
559 {
560   struct main_timer *tm;
561   while (GARY_SIZE(m->timer_table) > 1 && (tm = m->timer_table[1])->expires <= m->now)
562     {
563       DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(m->now - tm->expires));
564       tm->handler(tm);
565     }
566 }
567
568 static enum main_hook_return
569 process_hooks(struct main_context *m)
570 {
571   int hook_min = HOOK_RETRY;
572   int hook_max = HOOK_SHUTDOWN;
573   struct main_hook *ho;
574
575   while (ho = clist_remove_head(&m->hook_list))
576     {
577       clist_add_tail(&m->hook_done_list, &ho->n);
578       DBG("MAIN: Hook %p", ho);
579       int ret = ho->handler(ho);
580       hook_min = MIN(hook_min, ret);
581       hook_max = MAX(hook_max, ret);
582     }
583   clist_move(&m->hook_list, &m->hook_done_list);
584   if (hook_min == HOOK_SHUTDOWN ||
585     hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
586     m->shutdown)
587     {
588       DBG("MAIN: Shut down by %s", m->shutdown ? "main_shutdown" : "a hook");
589       return HOOK_SHUTDOWN;
590     }
591   if (hook_max == HOOK_RETRY)
592     return HOOK_RETRY;
593   else
594     return HOOK_IDLE;
595 }
596
597 #ifndef CONFIG_UCW_EPOLL
598
599 static void
600 main_rebuild_poll_table(struct main_context *m)
601 {
602   GARY_INIT_OR_RESIZE(m->poll_table, m->file_cnt);
603   GARY_INIT_OR_RESIZE(m->poll_file_table, m->file_cnt);
604   DBG("MAIN: Rebuilding poll table: %d entries", m->file_cnt);
605
606   struct pollfd *p = m->poll_table;
607   struct main_file **pf = m->poll_file_table;
608   CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
609     {
610       p->fd = fi->fd;
611       fi->pollfd = p++;
612       file_chg(fi);
613       *pf++ = fi;
614     }
615   m->poll_table_obsolete = 0;
616 }
617
618 #endif
619
620 void
621 main_loop(void)
622 {
623   DBG("MAIN: Entering main_loop");
624   struct main_context *m = main_current();
625
626   do_main_get_time(m);
627   for (;;)
628     {
629       timestamp_t wake = m->now + 1000000000;
630       process_timers(m);
631       switch (process_hooks(m))
632         {
633         case HOOK_SHUTDOWN:
634           return;
635         case HOOK_RETRY:
636           wake = 0;
637           break;
638         default: ;
639         }
640       if (count_timers(m))
641         wake = MIN(wake, m->timer_table[1]->expires);
642       do_main_get_time(m);
643       int timeout = ((wake > m->now) ? wake - m->now : 0);
644
645 #ifdef CONFIG_UCW_EPOLL
646       DBG("MAIN: Epoll for %d fds and timeout %d ms", m->file_cnt, timeout);
647       int n = epoll_wait(m->epoll_fd, m->epoll_events, EPOLL_BUF_SIZE, timeout);
648 #else
649       if (m->poll_table_obsolete)
650         main_rebuild_poll_table(m);
651       DBG("MAIN: Poll for %d fds and timeout %d ms", m->file_cnt, timeout);
652       int n = poll(m->poll_table, m->file_cnt, timeout);
653 #endif
654
655       DBG("\t-> %d events", n);
656       if (n < 0 && errno != EAGAIN && errno != EINTR)
657         die("(e)poll failed: %m");
658       timestamp_t old_now = m->now;
659       do_main_get_time(m);
660       m->idle_time += m->now - old_now;
661
662       if (n <= 0)
663         continue;
664
665       // Relink all files with a pending event to file_active_list
666 #ifdef CONFIG_UCW_EPOLL
667       for (int i=0; i<n; i++)
668         {
669           struct epoll_event *e = &m->epoll_events[i];
670           struct main_file *fi = e->data.ptr;
671           clist_remove(&fi->n);
672           clist_add_tail(&m->file_active_list, &fi->n);
673           fi->events = e->events;
674         }
675 #else
676       struct pollfd *p = m->poll_table;
677       struct main_file **pf = m->poll_file_table;
678       for (uns i=0; i < m->file_cnt; i++)
679         if (p[i].revents)
680           {
681             struct main_file *fi = pf[i];
682             clist_remove(&fi->n);
683             clist_add_tail(&m->file_active_list, &fi->n);
684             fi->events = p[i].revents;
685           }
686 #endif
687
688       // Process the buffered file events
689       struct main_file *fi;
690       while (fi = clist_remove_head(&m->file_active_list))
691         {
692           clist_add_tail(&m->file_list, &fi->n);
693           if (fi->events & (POLLIN | POLLHUP | POLLERR))
694             {
695               do
696                 DBG("MAIN: Read event on fd %d", fi->fd);
697               while (fi->read_handler && fi->read_handler(fi));
698             }
699           if (fi->events & (POLLOUT | POLLERR))
700             {
701               do
702                 DBG("MAIN: Write event on fd %d", fi->fd);
703               while (fi->write_handler && fi->write_handler(fi));
704             }
705         }
706     }
707 }
708
709 #ifdef TEST
710
711 static struct main_process mp;
712 static struct main_block_io fin, fout;
713 static struct main_hook hook;
714 static struct main_timer tm;
715 static struct main_signal sg;
716
717 static byte rb[16];
718
719 static void dread(struct main_block_io *bio)
720 {
721   if (bio->rpos < bio->rlen)
722     {
723       msg(L_INFO, "Read EOF");
724       block_io_del(bio);
725     }
726   else
727     {
728       msg(L_INFO, "Read done");
729       block_io_read(bio, rb, sizeof(rb));
730     }
731 }
732
733 static void derror(struct main_block_io *bio, int cause)
734 {
735   msg(L_INFO, "Error: %m !!! (cause %d)", cause);
736   block_io_del(bio);
737 }
738
739 static void dwrite(struct main_block_io *bio UNUSED)
740 {
741   msg(L_INFO, "Write done");
742 }
743
744 static int dhook(struct main_hook *ho UNUSED)
745 {
746   msg(L_INFO, "Hook called");
747   return 0;
748 }
749
750 static void dtimer(struct main_timer *tm)
751 {
752   msg(L_INFO, "Timer tick");
753   timer_add_rel(tm, 11000);
754   timer_add_rel(tm, 10000);
755 }
756
757 static void dentry(void)
758 {
759   msg(L_INFO, "*** SUBPROCESS START ***");
760   sleep(2);
761   msg(L_INFO, "*** SUBPROCESS FINISH ***");
762   exit(0);
763 }
764
765 static void dexit(struct main_process *pr)
766 {
767   msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
768 }
769
770 static void dsignal(struct main_signal *sg UNUSED)
771 {
772   msg(L_INFO, "SIGINT received (use Ctrl-\\ to really quit)");
773 }
774
775 int
776 main(void)
777 {
778   log_init(NULL);
779   main_init();
780
781   fin.read_done = dread;
782   fin.error_handler = derror;
783   block_io_add(&fin, 0);
784   block_io_read(&fin, rb, sizeof(rb));
785
786   fout.write_done = dwrite;
787   fout.error_handler = derror;
788   block_io_add(&fout, 1);
789   block_io_write(&fout, "Hello, world!\n", 14);
790
791   hook.handler = dhook;
792   hook_add(&hook);
793
794   tm.handler = dtimer;
795   timer_add_rel(&tm,  1000);
796
797   sg.signum = SIGINT;
798   sg.handler = dsignal;
799   signal_add(&sg);
800
801   mp.handler = dexit;
802   if (!process_fork(&mp))
803     dentry();
804
805   main_debug();
806
807   main_loop();
808   msg(L_INFO, "Finished.");
809 }
810
811 #endif