taskrambler  0.1.9
Web server and task management solution.
poll.c
Go to the documentation of this file.
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 serverPoll(Server this) {
35  int events;
36 
37  /**
38  * put all closed fds to end of array in O(this->nfds)
39  */
40  struct pollfd * fda = &(this->fds[2]);
41  struct pollfd * fdb = &(this->fds[this->nfds-1]);
42 
43  while (fda <= fdb) {
44  while (0 == fdb->fd && fda <= fdb) {
45  fdb--;
46  this->nfds--;
47  }
48 
49  while (0 != fda->fd && fda <= fdb) fda++;
50 
51  if (fda < fdb) {
52  memcpy(fda, fdb, sizeof(struct pollfd));
53  fdb--;
54  this->nfds--;
55  }
56  }
57 
58  /*
59  * wait for handles to become ready
60  */
61  do {
62  if (-1 == (events = poll(this->fds, this->nfds, -1))) {
63  switch (errno) {
64  default:
65  case EBADF:
66  case EINVAL:
67  case ENOMEM:
68  doShutdown = 1;
69  // DROP THROUGH
70 
71  case EINTR:
73  "poll systemcall failed: [%s] - service terminated",
74  strerror(errno));
75  }
76  }
77  } while (! doShutdown && 0 >= events);
78 
79  return events;
80 }
81 
82 // vim: set ts=4 sw=4:
Logger logger
Definition: taskrambler.c:66
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
int serverPoll(Server this)
Definition: poll.c:34
volatile int doShutdown