From a27250396644e7f603dc6de3e685b53e1671bf1f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 12 Jul 2016 11:24:50 +0200 Subject: Add loop that waits on sockets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/coopgammad.c | 3 +++ src/server.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/server.h | 7 ++++++ 3 files changed, 79 insertions(+) diff --git a/src/coopgammad.c b/src/coopgammad.c index 26a9d05..7af87ab 100644 --- a/src/coopgammad.c +++ b/src/coopgammad.c @@ -1271,6 +1271,9 @@ int main(int argc, char** argv) reexec = 0; /* See `if (reexec && !terminate)` */ } + if (main_loop() < 0) + goto fail; + if (reexec && !terminate) { size_t buffer_size; diff --git a/src/server.c b/src/server.c index c79f325..fc4bc5c 100644 --- a/src/server.c +++ b/src/server.c @@ -18,7 +18,10 @@ #include "server.h" #include "util.h" +#include #include +#include +#include #include #include @@ -57,11 +60,24 @@ struct message server_message; */ struct message* client_messages = NULL; + /** * The server socket's file descriptor */ extern int socketfd; +/** + * Has the process receive a signal + * telling it to re-execute? + */ +extern volatile sig_atomic_t reexec; + +/** + * Has the process receive a signal + * telling it to terminate? + */ +extern volatile sig_atomic_t terminate; + /** @@ -178,3 +194,56 @@ size_t server_unmarshal(const void* buf) return off; } + +/** + * Sets the file descriptor set that includes + * the server socket and all connections + * + * @param fds The file descritor set + * @return The highest set file descritor plus 1 + */ +static int update_fdset(fd_set* fds) +{ + int fdmax = socketfd; + size_t i; + FD_ZERO(fds); + FD_SET(socketfd, fds); + for (i = 0; i < connections_used; i++) + if (connections[i] >= 0) + { + FD_SET(connections[i], fds); + if (fdmax < connections[i]) + fdmax = connections[i]; + } + return fdmax + 1; +} + + +/** + * The program's main loop + * + * @return Zero on success, -1 on error + */ +int main_loop(void) +{ + fd_set fds_orig, fds_read, fds_err; + int fdn = update_fdset(&fds_orig); + + while (!reexec && !terminate) + { + memcpy(&fds_read, &fds_orig, sizeof(fd_set)); + memcpy(&fds_err, &fds_orig, sizeof(fd_set)); + if (select(fdn, &fds_read, NULL, &fds_err, NULL) < 0) + { + if (errno == EINTR) + continue; + goto fail; + } + /* TODO */ + } + + return 0; + fail: + return -1; +} + diff --git a/src/server.h b/src/server.h index 1edcf0e..f490f22 100644 --- a/src/server.h +++ b/src/server.h @@ -88,3 +88,10 @@ size_t server_marshal(void* buf); */ size_t server_unmarshal(const void* buf); +/** + * The program's main loop + * + * @return Zero on success, -1 on error + */ +int main_loop(void); + -- cgit v1.2.3-70-g09d2