Branch data Line data Source code
1 : : /**
2 : : * \file
3 : : *
4 : : * \author Georg Hopp
5 : : *
6 : : * \copyright
7 : : * Copyright © 2012 Georg Hopp
8 : : *
9 : : * This program is free software: you can redistribute it and/or modify
10 : : * it under the terms of the GNU General Public License as published by
11 : : * the Free Software Foundation, either version 3 of the License, or
12 : : * (at your option) any later version.
13 : : *
14 : : * This program is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : : * GNU General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU General Public License
20 : : * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : : */
22 : :
23 : : #include <poll.h>
24 : : #include <errno.h>
25 : :
26 : : #include "server.h"
27 : : #include "logger.h"
28 : :
29 : : #include "utils/signalHandling.h"
30 : :
31 : : #define POLLFD(ptr) ((struct pollfd *)(ptr))
32 : :
33 : : int
34 : 189 : serverPoll(Server this) {
35 : : int events;
36 : :
37 : : /**
38 : : * put all closed fds to end of array in O(this->nfds)
39 : : */
40 : 189 : struct pollfd * fda = &(this->fds[2]);
41 : 189 : struct pollfd * fdb = &(this->fds[this->nfds-1]);
42 : :
43 : 566 : while (fda <= fdb) {
44 : 376 : while (0 == fdb->fd && fda <= fdb) {
45 : 0 : fdb--;
46 : 0 : this->nfds--;
47 : : }
48 : :
49 : 188 : while (0 != fda->fd && fda <= fdb) fda++;
50 : :
51 : 188 : if (fda < fdb) {
52 : 0 : memcpy(fda, fdb, sizeof(struct pollfd));
53 : 0 : fdb--;
54 : 0 : this->nfds--;
55 : : }
56 : : }
57 : :
58 : : /*
59 : : * wait for handles to become ready
60 : : */
61 : : do {
62 : 189 : if (-1 == (events = poll(this->fds, this->nfds, -1))) {
63 : 0 : switch (errno) {
64 : : default:
65 : : case EBADF:
66 : : case EINVAL:
67 : : case ENOMEM:
68 : 0 : doShutdown = 1;
69 : : // DROP THROUGH
70 : :
71 : : case EINTR:
72 : 0 : loggerLog(this->logger, LOGGER_CRIT,
73 : : "poll systemcall failed: [%s] - service terminated",
74 : 0 : strerror(errno));
75 : : }
76 : : }
77 : 189 : } while (! doShutdown && 0 >= events);
78 : :
79 : 189 : return events;
80 : : }
81 : :
82 : : // vim: set ts=4 sw=4:
|