1 | /* |
1 | /* |
2 | iom.C -- generic I/O multiplexor |
2 | iom.C -- generic I/O multiplexer |
3 | Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
5 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
10 | (at your option) any later version. |
9 | |
11 | |
10 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
14 | |
16 | |
15 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | */ |
20 | */ |
19 | |
21 | |
20 | #include "iom.h" |
22 | #include "iom.h" |
21 | |
23 | |
22 | #include <cstdio> |
24 | #include <cstdio> |
23 | #include <cstdlib> |
25 | #include <cstdlib> |
24 | #include <cerrno> |
26 | #include <cerrno> |
|
|
27 | #include <cassert> |
25 | |
28 | |
|
|
29 | #include <sys/types.h> |
26 | #include <sys/time.h> |
30 | #include <sys/time.h> |
27 | |
|
|
28 | #include <assert.h> |
|
|
29 | |
31 | |
30 | #if 1 // older unices need these includes for select (2) |
32 | #if 1 // older unices need these includes for select (2) |
31 | # include <unistd.h> |
33 | # include <unistd.h> |
32 | # include <sys/types.h> |
|
|
33 | # include <time.h> |
34 | # include <time.h> |
34 | #endif |
35 | #endif |
35 | |
36 | |
36 | // for IOM_SIG |
37 | #if IOM_CHILD |
|
|
38 | # include <sys/wait.h> |
|
|
39 | #endif |
|
|
40 | |
37 | #if IOM_SIG |
41 | #if IOM_SIG |
38 | # include <signal.h> |
42 | # include <csignal> |
39 | # include <fcntl.h> |
43 | # include <fcntl.h> |
40 | #endif |
44 | #endif |
41 | |
45 | |
42 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
46 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
43 | // until that happens, sys/select.h must come last |
47 | // until that happens, sys/select.h must come last |
44 | #include <sys/select.h> |
48 | #include <sys/select.h> |
45 | |
|
|
46 | // TSTAMP_MAX must still fit into a positive struct timeval |
|
|
47 | #define TSTAMP_MAX (double)(1UL<<31) |
|
|
48 | |
49 | |
49 | #define TIMEVAL timeval |
50 | #define TIMEVAL timeval |
50 | #define TV_FRAC tv_usec |
51 | #define TV_FRAC tv_usec |
51 | #define TV_MULT 1000000L |
52 | #define TV_MULT 1000000L |
52 | |
53 | |
… | |
… | |
71 | : pending (false) |
72 | : pending (false) |
72 | { } |
73 | { } |
73 | }; |
74 | }; |
74 | static vector<sig_vec *> sw; |
75 | static vector<sig_vec *> sw; |
75 | #endif |
76 | #endif |
|
|
77 | #if IOM_CHILD |
|
|
78 | static io_manager_vec<child_watcher> pw; |
|
|
79 | #endif |
76 | |
80 | |
77 | // this is a dummy time watcher to ensure that the first |
81 | // this is a dummy time watcher to ensure that the first |
78 | // time watcher is _always_ valid, this gets rid of a lot |
82 | // time watcher is _always_ valid, this gets rid of a lot |
79 | // of null-pointer-checks |
83 | // of null-pointer-checks |
80 | // (must come _before_ iom is being defined) |
84 | // (must come _before_ iom is being defined) |
81 | static struct tw0 : time_watcher |
85 | static struct tw0 : time_watcher |
|
|
86 | { |
|
|
87 | void cb (time_watcher &w) |
82 | { |
88 | { |
83 | void cb (time_watcher &w) |
|
|
84 | { |
|
|
85 | // should never get called |
89 | // should never get called |
86 | // reached end-of-time, or tstamp has a bogus definition, |
90 | // reached end-of-time, or tstamp has a bogus definition, |
87 | // or compiler initialisation order broken, or something else :) |
91 | // or compiler initialisation order broken, or something else :) |
88 | abort (); |
92 | abort (); |
89 | } |
93 | } |
90 | |
94 | |
91 | tw0 () |
95 | tw0 () |
92 | : time_watcher (this, &tw0::cb) |
96 | : time_watcher (this, &tw0::cb) |
93 | { } |
97 | { } |
94 | } tw0; |
98 | } tw0; |
95 | |
99 | |
96 | tstamp NOW; |
100 | tstamp NOW; |
97 | |
101 | |
|
|
102 | #if IOM_CHILD |
|
|
103 | // sig_watcher for child signal(s) |
|
|
104 | static struct sw0 : sig_watcher |
|
|
105 | { |
|
|
106 | void cb (sig_watcher &w) |
|
|
107 | { |
|
|
108 | // SIGCHLD, call corresponding watchera |
|
|
109 | pid_t pid; |
|
|
110 | int status; |
|
|
111 | |
|
|
112 | while ((pid = waitpid (-1, &status, WNOHANG)) > 0) |
|
|
113 | for (int i = pw.size (); i--; ) |
|
|
114 | { |
|
|
115 | child_watcher *w = pw[i]; |
|
|
116 | |
|
|
117 | if (!w) |
|
|
118 | pw.erase_unordered (i); |
|
|
119 | else if (w->pid == pid) |
|
|
120 | { |
|
|
121 | io_manager::unreg (*w); |
|
|
122 | w->call (*w, status); |
|
|
123 | } |
|
|
124 | } |
|
|
125 | } |
|
|
126 | |
|
|
127 | sw0 () |
|
|
128 | : sig_watcher (this, &sw0::cb) |
|
|
129 | { } |
|
|
130 | } sw0; |
|
|
131 | #endif |
|
|
132 | |
98 | #if IOM_TIME |
133 | #if IOM_TIME |
99 | inline void set_now (void) |
134 | tstamp io_manager::now () |
100 | { |
135 | { |
101 | struct timeval tv; |
136 | struct timeval tv; |
102 | |
137 | |
103 | gettimeofday (&tv, 0); |
138 | gettimeofday (&tv, 0); |
104 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
139 | return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
|
|
140 | } |
|
|
141 | |
|
|
142 | void io_manager::set_now () |
|
|
143 | { |
|
|
144 | NOW = now (); |
105 | } |
145 | } |
106 | #endif |
146 | #endif |
107 | |
147 | |
108 | static bool iom_valid; |
148 | static bool iom_valid; |
109 | |
149 | |
110 | // used for initialisation only |
150 | // used for initialisation only |
111 | static struct init { |
151 | static struct init { |
112 | init () |
152 | init () |
113 | { |
153 | { |
|
|
154 | #ifdef IOM_PREINIT |
|
|
155 | { IOM_PREINIT } |
|
|
156 | #endif |
|
|
157 | iom_valid = true; |
|
|
158 | |
114 | #if IOM_SIG |
159 | #if IOM_SIG |
115 | sigemptyset (&sigs); |
160 | sigemptyset (&sigs); |
116 | |
161 | |
117 | if (pipe (sigpipe)) |
162 | if (pipe (sigpipe)) |
118 | { |
163 | { |
119 | perror ("io_manager: unable to create signal pipe, aborting."); |
164 | perror ("io_manager: unable to create signal pipe, aborting."); |
120 | abort (); |
165 | abort (); |
121 | } |
166 | } |
122 | |
167 | |
123 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); |
168 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); |
124 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); |
169 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); |
125 | #endif |
170 | #endif |
126 | |
171 | |
127 | iom_valid = true; |
172 | #if IOM_CHILD |
|
|
173 | sw0.start (SIGCHLD); |
|
|
174 | #endif |
128 | |
175 | |
129 | #if IOM_TIME |
176 | #if IOM_TIME |
130 | set_now (); |
177 | io_manager::set_now (); |
131 | |
178 | |
132 | tw0.start (TSTAMP_MAX); |
179 | tw0.start (TSTAMP_MAX); |
133 | #endif |
180 | #endif |
|
|
181 | |
|
|
182 | #ifdef IOM_POSTINIT |
|
|
183 | { IOM_POSTINIT } |
|
|
184 | #endif |
|
|
185 | } |
|
|
186 | |
|
|
187 | ~init () |
|
|
188 | { |
|
|
189 | iom_valid = false; |
134 | } |
190 | } |
135 | |
191 | |
136 | static void required (); |
192 | static void required (); |
137 | } init; |
193 | } init; |
138 | |
194 | |
… | |
… | |
152 | { |
208 | { |
153 | init::required (); |
209 | init::required (); |
154 | |
210 | |
155 | if (!w.active) |
211 | if (!w.active) |
156 | { |
212 | { |
157 | #if IOM_CHECK |
|
|
158 | queue.activity = true; |
|
|
159 | #endif |
|
|
160 | queue.push_back (&w); |
213 | queue.push_back (&w); |
161 | w.active = queue.size (); |
214 | w.active = queue.size (); |
162 | } |
215 | } |
163 | } |
216 | } |
164 | |
217 | |
… | |
… | |
213 | write (sigpipe[1], &ch, 1); |
266 | write (sigpipe[1], &ch, 1); |
214 | } |
267 | } |
215 | |
268 | |
216 | void io_manager::reg (sig_watcher &w) |
269 | void io_manager::reg (sig_watcher &w) |
217 | { |
270 | { |
|
|
271 | init::required (); |
|
|
272 | |
218 | assert (0 < w.signum); |
273 | assert (0 < w.signum); |
219 | |
274 | |
220 | sw.reserve (w.signum); |
275 | sw.reserve (w.signum); |
221 | |
276 | |
222 | while (sw.size () < w.signum) // pathetic |
277 | while (sw.size () < w.signum) // pathetic |
… | |
… | |
247 | io_manager::reg (w, *sv); |
302 | io_manager::reg (w, *sv); |
248 | } |
303 | } |
249 | |
304 | |
250 | void io_manager::unreg (sig_watcher &w) |
305 | void io_manager::unreg (sig_watcher &w) |
251 | { |
306 | { |
252 | if (!w.active) |
307 | if (!w.active || !iom_valid) |
253 | return; |
308 | return; |
254 | |
309 | |
255 | assert (0 < w.signum && w.signum <= sw.size ()); |
310 | assert (0 < w.signum && w.signum <= sw.size ()); |
256 | |
311 | |
257 | io_manager::unreg (w, *sw[w.signum - 1]); |
312 | io_manager::unreg (w, *sw[w.signum - 1]); |
… | |
… | |
263 | this->signum = signum; |
318 | this->signum = signum; |
264 | io_manager::reg (*this); |
319 | io_manager::reg (*this); |
265 | } |
320 | } |
266 | #endif |
321 | #endif |
267 | |
322 | |
|
|
323 | #if IOM_CHILD |
|
|
324 | void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); } |
|
|
325 | void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); } |
|
|
326 | #endif |
|
|
327 | |
268 | void io_manager::loop () |
328 | void io_manager::loop () |
269 | { |
329 | { |
270 | init::required (); |
330 | init::required (); |
271 | |
331 | |
272 | #if IOM_TIME |
332 | #if IOM_TIME |
273 | set_now (); |
333 | set_now (); |
274 | #endif |
334 | #endif |
275 | |
335 | |
276 | for (;;) |
336 | for (;;) |
277 | { |
337 | { |
|
|
338 | |
|
|
339 | #if IOM_TIME |
|
|
340 | // call pending time watchers |
|
|
341 | { |
|
|
342 | bool activity; |
|
|
343 | |
|
|
344 | do |
|
|
345 | { |
|
|
346 | activity = false; |
|
|
347 | |
|
|
348 | for (int i = tw.size (); i--; ) |
|
|
349 | if (!tw[i]) |
|
|
350 | tw.erase_unordered (i); |
|
|
351 | else if (tw[i]->at <= NOW) |
|
|
352 | { |
|
|
353 | time_watcher &w = *tw[i]; |
|
|
354 | |
|
|
355 | unreg (w); |
|
|
356 | w.call (w); |
|
|
357 | |
|
|
358 | activity = true; |
|
|
359 | } |
|
|
360 | } |
|
|
361 | while (activity); |
|
|
362 | } |
|
|
363 | #endif |
|
|
364 | |
|
|
365 | #if IOM_CHECK |
|
|
366 | // call all check watchers |
|
|
367 | for (int i = cw.size (); i--; ) |
|
|
368 | if (!cw[i]) |
|
|
369 | cw.erase_unordered (i); |
|
|
370 | else |
|
|
371 | cw[i]->call (*cw[i]); |
|
|
372 | #endif |
|
|
373 | |
278 | struct TIMEVAL *to = 0; |
374 | struct TIMEVAL *to = 0; |
279 | struct TIMEVAL tval; |
375 | struct TIMEVAL tval; |
280 | |
376 | |
281 | #if IOM_IDLE |
377 | #if IOM_IDLE |
282 | if (iw.size ()) |
378 | if (iw.size ()) |
… | |
… | |
287 | } |
383 | } |
288 | else |
384 | else |
289 | #endif |
385 | #endif |
290 | { |
386 | { |
291 | #if IOM_TIME |
387 | #if IOM_TIME |
292 | time_watcher *next; |
388 | // find earliest active watcher |
|
|
389 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
293 | |
390 | |
294 | for (;;) |
391 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
392 | if (*i && (*i)->at < next->at) |
|
|
393 | next = *i; |
|
|
394 | |
|
|
395 | if (next->at > NOW && next != tw[0]) |
295 | { |
396 | { |
296 | next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
297 | |
|
|
298 | for (int i = tw.size (); i--; ) |
|
|
299 | if (!tw[i]) |
|
|
300 | tw.erase_unordered (i); |
|
|
301 | else if (tw[i]->at < next->at) |
|
|
302 | next = tw[i]; |
|
|
303 | |
|
|
304 | if (next->at > NOW) |
|
|
305 | { |
|
|
306 | if (next != tw[0]) |
|
|
307 | { |
|
|
308 | double diff = next->at - NOW; |
397 | double diff = next->at - NOW; |
309 | tval.tv_sec = (int)diff; |
398 | tval.tv_sec = (int)diff; |
310 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
399 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
311 | to = &tval; |
400 | to = &tval; |
312 | } |
|
|
313 | break; |
|
|
314 | } |
|
|
315 | else |
|
|
316 | { |
|
|
317 | unreg (*next); |
|
|
318 | next->call (*next); |
|
|
319 | } |
|
|
320 | } |
401 | } |
321 | #endif |
|
|
322 | } |
|
|
323 | |
|
|
324 | #if IOM_CHECK |
|
|
325 | tw.activity = false; |
|
|
326 | |
|
|
327 | for (int i = cw.size (); i--; ) |
|
|
328 | if (!cw[i]) |
|
|
329 | cw.erase_unordered (i); |
|
|
330 | else |
|
|
331 | cw[i]->call (*cw[i]); |
|
|
332 | |
|
|
333 | if (tw.activity) |
|
|
334 | { |
|
|
335 | tval.tv_sec = 0; |
|
|
336 | tval.TV_FRAC = 0; |
|
|
337 | to = &tval; |
|
|
338 | } |
402 | } |
339 | #endif |
403 | #endif |
340 | |
404 | |
341 | #if IOM_IO || IOM_SIG |
405 | #if IOM_IO || IOM_SIG |
342 | fd_set rfd, wfd; |
406 | fd_set rfd, wfd; |
… | |
… | |
345 | FD_ZERO (&wfd); |
409 | FD_ZERO (&wfd); |
346 | |
410 | |
347 | int fds = 0; |
411 | int fds = 0; |
348 | |
412 | |
349 | # if IOM_IO |
413 | # if IOM_IO |
350 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
414 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
351 | if (*i) |
415 | if (*i) |
352 | { |
416 | { |
353 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
417 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
354 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
418 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
355 | |
419 | |
356 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
420 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
357 | } |
421 | } |
358 | # endif |
422 | # endif |
359 | |
423 | |
360 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
424 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
361 | break; // no events |
425 | break; // no events |
362 | |
426 | |
363 | # if IOM_SIG |
427 | # if IOM_SIG |
364 | FD_SET (sigpipe[0], &rfd); |
428 | FD_SET (sigpipe[0], &rfd); |
365 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
429 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
… | |
… | |
374 | # if IOM_SIG |
438 | # if IOM_SIG |
375 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
439 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
376 | # endif |
440 | # endif |
377 | |
441 | |
378 | # if IOM_TIME |
442 | # if IOM_TIME |
|
|
443 | { |
|
|
444 | // update time, try to compensate for gross non-monotonic time changes |
|
|
445 | tstamp diff = NOW; |
379 | set_now (); |
446 | set_now (); |
|
|
447 | diff = NOW - diff; |
|
|
448 | |
|
|
449 | if (diff < 0) |
|
|
450 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
451 | if (*i) |
|
|
452 | (*i)->at += diff; |
|
|
453 | } |
380 | # endif |
454 | # endif |
381 | |
455 | |
382 | if (fds > 0) |
456 | if (fds > 0) |
383 | { |
457 | { |
384 | # if IOM_SIG |
458 | # if IOM_SIG |
… | |
… | |
387 | char ch; |
461 | char ch; |
388 | |
462 | |
389 | while (read (sigpipe[0], &ch, 1) > 0) |
463 | while (read (sigpipe[0], &ch, 1) > 0) |
390 | ; |
464 | ; |
391 | |
465 | |
392 | for (sig_vec **svp = sw.end (); svp-- > sw.begin (); ) |
466 | for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
393 | if (*svp && (*svp)->pending) |
467 | if (*svp && (*svp)->pending) |
394 | { |
468 | { |
395 | sig_vec &sv = **svp; |
469 | sig_vec &sv = **svp; |
396 | for (int i = sv.size (); i--; ) |
470 | for (int i = sv.size (); i--; ) |
397 | if (!sv[i]) |
471 | if (!sv[i]) |
… | |
… | |
408 | for (int i = iow.size (); i--; ) |
482 | for (int i = iow.size (); i--; ) |
409 | if (!iow[i]) |
483 | if (!iow[i]) |
410 | iow.erase_unordered (i); |
484 | iow.erase_unordered (i); |
411 | else |
485 | else |
412 | { |
486 | { |
|
|
487 | io_watcher &w = *iow[i]; |
413 | short revents = iow[i]->events; |
488 | short revents = w.events; |
414 | |
489 | |
415 | if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; |
490 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
416 | if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; |
491 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
417 | |
492 | |
418 | if (revents) |
493 | if (revents) |
419 | iow[i]->call (*iow[i], revents); |
494 | w.call (w, revents); |
420 | } |
495 | } |
421 | #endif |
496 | #endif |
422 | } |
497 | } |
423 | else if (fds < 0 && errno != EINTR) |
498 | else if (fds < 0 && errno != EINTR) |
424 | { |
499 | { |