1 |
/* |
2 |
iom.C -- generic I/O multiplexer |
3 |
Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
This file is part of GVPE. |
6 |
|
7 |
GVPE is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with gvpe; if not, write to the Free Software |
19 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
*/ |
21 |
|
22 |
#include "iom.h" |
23 |
|
24 |
#include <cstdio> |
25 |
#include <cstdlib> |
26 |
#include <cerrno> |
27 |
#include <cassert> |
28 |
|
29 |
#include <sys/types.h> |
30 |
#include <sys/time.h> |
31 |
|
32 |
#if 1 // older unices need these includes for select (2) |
33 |
# include <unistd.h> |
34 |
# include <time.h> |
35 |
#endif |
36 |
|
37 |
#if IOM_CHILD |
38 |
# include <sys/wait.h> |
39 |
#endif |
40 |
|
41 |
#if IOM_SIG |
42 |
# include <csignal> |
43 |
# include <fcntl.h> |
44 |
#endif |
45 |
|
46 |
// if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
47 |
// until that happens, sys/select.h must come last |
48 |
#include <sys/select.h> |
49 |
|
50 |
#define TIMEVAL timeval |
51 |
#define TV_FRAC tv_usec |
52 |
#define TV_MULT 1000000L |
53 |
|
54 |
#ifndef IOM_LIBEVENT |
55 |
#if IOM_IO |
56 |
static io_manager_vec<io_watcher> iow; |
57 |
#endif |
58 |
#if IOM_TIME |
59 |
static io_manager_vec<time_watcher> tw; |
60 |
#endif |
61 |
#endif |
62 |
|
63 |
#if IOM_CHECK |
64 |
static io_manager_vec<check_watcher> cw; |
65 |
#endif |
66 |
#if IOM_IDLE |
67 |
static io_manager_vec<idle_watcher> iw; |
68 |
#endif |
69 |
|
70 |
#if IOM_SIG |
71 |
static int sigpipe[2]; // signal signalling pipe |
72 |
static sigset_t sigs; |
73 |
struct sig_vec : io_manager_vec<sig_watcher> { |
74 |
int pending; |
75 |
sig_vec () |
76 |
: pending (false) |
77 |
{ } |
78 |
}; |
79 |
static vector<sig_vec *> sw; |
80 |
#endif |
81 |
|
82 |
#if IOM_CHILD |
83 |
static io_manager_vec<child_watcher> pw; |
84 |
#endif |
85 |
|
86 |
// this is a dummy time watcher to ensure that the first |
87 |
// time watcher is _always_ valid, this gets rid of a lot |
88 |
// of null-pointer-checks |
89 |
// (must come _before_ iom is being defined) |
90 |
static struct tw0 : time_watcher |
91 |
{ |
92 |
void cb (time_watcher &w) |
93 |
{ |
94 |
// should never get called |
95 |
// reached end-of-time, or tstamp has a bogus definition, |
96 |
// or compiler initialisation order broken, or something else :) |
97 |
abort (); |
98 |
} |
99 |
|
100 |
tw0 () |
101 |
: time_watcher (this, &tw0::cb) |
102 |
{ } |
103 |
} tw0; |
104 |
|
105 |
tstamp NOW; |
106 |
|
107 |
#if IOM_CHILD |
108 |
// sig_watcher for child signal(s) |
109 |
static struct sw0 : sig_watcher |
110 |
{ |
111 |
void cb (sig_watcher &w) |
112 |
{ |
113 |
// SIGCHLD, call corresponding watchera |
114 |
pid_t pid; |
115 |
int status; |
116 |
|
117 |
while ((pid = waitpid (-1, &status, WNOHANG)) > 0) |
118 |
for (int i = pw.size (); i--; ) |
119 |
{ |
120 |
child_watcher *w = pw[i]; |
121 |
|
122 |
if (!w) |
123 |
pw.erase_unordered (i); |
124 |
else if (w->pid == pid) |
125 |
{ |
126 |
io_manager::unreg (*w); |
127 |
w->call (*w, status); |
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
sw0 () |
133 |
: sig_watcher (this, &sw0::cb) |
134 |
{ } |
135 |
} sw0; |
136 |
#endif |
137 |
|
138 |
#if IOM_TIME |
139 |
tstamp io_manager::now () |
140 |
{ |
141 |
struct timeval tv; |
142 |
|
143 |
gettimeofday (&tv, 0); |
144 |
return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
145 |
} |
146 |
|
147 |
void io_manager::set_now () |
148 |
{ |
149 |
NOW = now (); |
150 |
} |
151 |
#endif |
152 |
|
153 |
static bool iom_valid; |
154 |
|
155 |
// used for initialisation only |
156 |
static struct init { |
157 |
init () |
158 |
{ |
159 |
#ifdef IOM_PREINIT |
160 |
{ IOM_PREINIT } |
161 |
#endif |
162 |
#ifdef IOM_LIBEVENT |
163 |
event_init (); |
164 |
#endif |
165 |
iom_valid = true; |
166 |
|
167 |
#if IOM_SIG |
168 |
sigemptyset (&sigs); |
169 |
|
170 |
if (pipe (sigpipe)) |
171 |
{ |
172 |
perror ("io_manager: unable to create signal pipe, aborting."); |
173 |
abort (); |
174 |
} |
175 |
|
176 |
fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); |
177 |
fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); |
178 |
#endif |
179 |
|
180 |
#if IOM_CHILD |
181 |
sw0.start (SIGCHLD); |
182 |
#endif |
183 |
|
184 |
#if IOM_TIME |
185 |
io_manager::set_now (); |
186 |
|
187 |
tw0.start (TSTAMP_MAX); |
188 |
#endif |
189 |
|
190 |
#ifdef IOM_POSTINIT |
191 |
{ IOM_POSTINIT } |
192 |
#endif |
193 |
} |
194 |
|
195 |
~init () |
196 |
{ |
197 |
iom_valid = false; |
198 |
} |
199 |
|
200 |
static void required (); |
201 |
} init; |
202 |
|
203 |
void |
204 |
init::required () |
205 |
{ |
206 |
if (!iom_valid) |
207 |
{ |
208 |
write (2, "io_manager: early registration attempt, aborting.\n", |
209 |
sizeof ("io_manager: early registration attempt, aborting.\n") - 1); |
210 |
abort (); |
211 |
} |
212 |
} |
213 |
|
214 |
template<class watcher> |
215 |
void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue) |
216 |
{ |
217 |
init::required (); |
218 |
|
219 |
if (!w.active) |
220 |
{ |
221 |
queue.push_back (&w); |
222 |
w.active = queue.size (); |
223 |
} |
224 |
} |
225 |
|
226 |
template<class watcher> |
227 |
void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue) |
228 |
{ |
229 |
if (!iom_valid) |
230 |
return; |
231 |
|
232 |
if (w.active) |
233 |
{ |
234 |
queue [w.active - 1] = 0; |
235 |
w.active = 0; |
236 |
} |
237 |
} |
238 |
|
239 |
#if IOM_TIME |
240 |
# ifdef IOM_LIBEVENT |
241 |
void iom_time_c_callback (int fd, short events, void *data) |
242 |
{ |
243 |
time_watcher *w = static_cast<time_watcher *>(data); |
244 |
w->call (*w); |
245 |
} |
246 |
# else |
247 |
void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } |
248 |
void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } |
249 |
# endif |
250 |
|
251 |
void time_watcher::trigger () |
252 |
{ |
253 |
call (*this); |
254 |
start (); |
255 |
} |
256 |
#endif |
257 |
|
258 |
#if IOM_IO |
259 |
# ifdef IOM_LIBEVENT |
260 |
void iom_io_c_callback (int fd, short events, void *data) |
261 |
{ |
262 |
io_watcher *w = static_cast<io_watcher *>(data); |
263 |
w->call (*w, events); |
264 |
} |
265 |
# else |
266 |
void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } |
267 |
void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } |
268 |
# endif |
269 |
#endif |
270 |
|
271 |
#if IOM_CHECK |
272 |
void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } |
273 |
void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } |
274 |
#endif |
275 |
|
276 |
#if IOM_IDLE |
277 |
void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); } |
278 |
void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); } |
279 |
#endif |
280 |
|
281 |
#if IOM_SIG |
282 |
static void |
283 |
sighandler (int signum) |
284 |
{ |
285 |
sw [signum - 1]->pending = true; |
286 |
|
287 |
// we use a pipe for signal notifications, as most current |
288 |
// OSes (Linux...) do not implement pselect correctly. ugh. |
289 |
char ch = signum; // actual content not used |
290 |
write (sigpipe[1], &ch, 1); |
291 |
} |
292 |
|
293 |
void io_manager::reg (sig_watcher &w) |
294 |
{ |
295 |
init::required (); |
296 |
|
297 |
assert (0 < w.signum); |
298 |
|
299 |
sw.reserve (w.signum); |
300 |
|
301 |
while (sw.size () < w.signum) // pathetic |
302 |
sw.push_back (0); |
303 |
|
304 |
sig_vec *&sv = sw[w.signum - 1]; |
305 |
|
306 |
if (!sv) |
307 |
{ |
308 |
sv = new sig_vec; |
309 |
|
310 |
sigaddset (&sigs, w.signum); |
311 |
sigprocmask (SIG_BLOCK, &sigs, NULL); |
312 |
|
313 |
struct sigaction sa; |
314 |
sa.sa_handler = sighandler; |
315 |
sigfillset (&sa.sa_mask); |
316 |
sa.sa_flags = SA_RESTART; |
317 |
|
318 |
if (sigaction (w.signum, &sa, 0)) |
319 |
{ |
320 |
perror ("io_manager: error while installing signal handler, ignoring."); |
321 |
abort (); |
322 |
} |
323 |
|
324 |
} |
325 |
|
326 |
io_manager::reg (w, *sv); |
327 |
} |
328 |
|
329 |
void io_manager::unreg (sig_watcher &w) |
330 |
{ |
331 |
if (!w.active || !iom_valid) |
332 |
return; |
333 |
|
334 |
assert (0 < w.signum && w.signum <= sw.size ()); |
335 |
|
336 |
io_manager::unreg (w, *sw[w.signum - 1]); |
337 |
} |
338 |
|
339 |
void sig_watcher::start (int signum) |
340 |
{ |
341 |
stop (); |
342 |
this->signum = signum; |
343 |
io_manager::reg (*this); |
344 |
} |
345 |
#endif |
346 |
|
347 |
#if IOM_CHILD |
348 |
void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); } |
349 |
void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); } |
350 |
#endif |
351 |
|
352 |
void io_manager::loop () |
353 |
{ |
354 |
init::required (); |
355 |
|
356 |
#if IOM_TIME |
357 |
set_now (); |
358 |
#endif |
359 |
|
360 |
for (;;) |
361 |
{ |
362 |
#ifndef IOM_LIBEVENT |
363 |
#if IOM_TIME |
364 |
// call pending time watchers |
365 |
{ |
366 |
bool activity; |
367 |
|
368 |
do |
369 |
{ |
370 |
activity = false; |
371 |
|
372 |
for (int i = tw.size (); i--; ) |
373 |
if (!tw[i]) |
374 |
tw.erase_unordered (i); |
375 |
else if (tw[i]->at <= NOW) |
376 |
{ |
377 |
time_watcher &w = *tw[i]; |
378 |
|
379 |
unreg (w); |
380 |
w.call (w); |
381 |
|
382 |
activity = true; |
383 |
} |
384 |
} |
385 |
while (activity); |
386 |
} |
387 |
#endif |
388 |
#endif |
389 |
|
390 |
#if IOM_CHECK |
391 |
// call all check watchers |
392 |
for (int i = cw.size (); i--; ) |
393 |
if (!cw[i]) |
394 |
cw.erase_unordered (i); |
395 |
else |
396 |
cw[i]->call (*cw[i]); |
397 |
#endif |
398 |
|
399 |
struct TIMEVAL *to = 0; |
400 |
struct TIMEVAL tval; |
401 |
|
402 |
#if IOM_IDLE |
403 |
if (iw.size ()) |
404 |
{ |
405 |
tval.tv_sec = 0; |
406 |
tval.TV_FRAC = 0; |
407 |
to = &tval; |
408 |
} |
409 |
else |
410 |
#endif |
411 |
#ifndef IOM_LIBEVENT |
412 |
{ |
413 |
#if IOM_TIME |
414 |
// find earliest active watcher |
415 |
time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
416 |
|
417 |
for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
418 |
if (*i && (*i)->at < next->at) |
419 |
next = *i; |
420 |
|
421 |
if (next->at > NOW && next != tw[0]) |
422 |
{ |
423 |
double diff = next->at - NOW; |
424 |
tval.tv_sec = (int)diff; |
425 |
tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
426 |
to = &tval; |
427 |
} |
428 |
#endif |
429 |
} |
430 |
|
431 |
#if IOM_IO || IOM_SIG |
432 |
fd_set rfd, wfd; |
433 |
|
434 |
FD_ZERO (&rfd); |
435 |
FD_ZERO (&wfd); |
436 |
|
437 |
int fds = 0; |
438 |
|
439 |
#if IOM_IO |
440 |
for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
441 |
if (*i) |
442 |
{ |
443 |
if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
444 |
if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
445 |
|
446 |
if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
447 |
} |
448 |
#endif |
449 |
|
450 |
if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
451 |
break; // no events |
452 |
|
453 |
#if IOM_SIG |
454 |
FD_SET (sigpipe[0], &rfd); |
455 |
if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
456 |
#endif |
457 |
|
458 |
#if IOM_SIG |
459 |
// there is no race, as we use a pipe for signals, so select |
460 |
// will return if a signal is caught. |
461 |
sigprocmask (SIG_UNBLOCK, &sigs, NULL); |
462 |
#endif |
463 |
fds = select (fds, &rfd, &wfd, NULL, to); |
464 |
#if IOM_SIG |
465 |
sigprocmask (SIG_BLOCK, &sigs, NULL); |
466 |
#endif |
467 |
|
468 |
#else |
469 |
if (to) |
470 |
event_loop (EVLOOP_NONBLOCK); |
471 |
else |
472 |
event_loop (EVLOOP_ONCE); |
473 |
#endif |
474 |
|
475 |
#if IOM_TIME |
476 |
{ |
477 |
// update time, try to compensate for gross non-monotonic time changes |
478 |
tstamp diff = NOW; |
479 |
set_now (); |
480 |
diff = NOW - diff; |
481 |
|
482 |
if (diff < 0) |
483 |
for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
484 |
if (*i) |
485 |
(*i)->at += diff; |
486 |
} |
487 |
#endif |
488 |
|
489 |
#ifndef IOM_LIBEVENT |
490 |
if (fds > 0) |
491 |
{ |
492 |
#if IOM_SIG |
493 |
if (FD_ISSET (sigpipe[0], &rfd)) |
494 |
{ |
495 |
char ch; |
496 |
|
497 |
while (read (sigpipe[0], &ch, 1) > 0) |
498 |
; |
499 |
|
500 |
for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
501 |
if (*svp && (*svp)->pending) |
502 |
{ |
503 |
sig_vec &sv = **svp; |
504 |
for (int i = sv.size (); i--; ) |
505 |
if (!sv[i]) |
506 |
sv.erase_unordered (i); |
507 |
else |
508 |
sv[i]->call (*sv[i]); |
509 |
|
510 |
sv.pending = false; |
511 |
} |
512 |
} |
513 |
#endif |
514 |
|
515 |
#if IOM_IO |
516 |
for (int i = iow.size (); i--; ) |
517 |
if (!iow[i]) |
518 |
iow.erase_unordered (i); |
519 |
else |
520 |
{ |
521 |
io_watcher &w = *iow[i]; |
522 |
short revents = w.events; |
523 |
|
524 |
if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
525 |
if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
526 |
|
527 |
if (revents) |
528 |
w.call (w, revents); |
529 |
} |
530 |
#endif |
531 |
} |
532 |
else if (fds < 0 && errno != EINTR) |
533 |
{ |
534 |
perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); |
535 |
abort (); |
536 |
} |
537 |
#else |
538 |
if (0) |
539 |
; |
540 |
#endif |
541 |
#if IOM_IDLE |
542 |
else |
543 |
for (int i = iw.size (); i--; ) |
544 |
if (!iw[i]) |
545 |
iw.erase_unordered (i); |
546 |
else |
547 |
iw[i]->call (*iw[i]); |
548 |
#endif |
549 |
|
550 |
#elif IOM_TIME |
551 |
if (!to) |
552 |
break; |
553 |
|
554 |
select (0, 0, 0, 0, to); |
555 |
set_now (); |
556 |
break; |
557 |
#endif |
558 |
} |
559 |
} |
560 |
|