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