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