From: Sebastian Harl Date: Sat, 26 Oct 2013 12:05:42 +0000 (+0200) Subject: socket frontend: Close listening sockets before returning. X-Git-Tag: sysdb-0.1.0~336^2~29 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=c91469d00359ede6b2a8bf128dae0e9f8ed3770d;p=sysdb.git socket frontend: Close listening sockets before returning. Also, cleaned up listener handling a bit. --- diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 9fba695..27a1b67 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -181,6 +181,7 @@ listener_destroy(listener_t *listener) if (listener->sock_fd >= 0) close(listener->sock_fd); + listener->sock_fd = -1; if (listener->address) free(listener->address); @@ -232,6 +233,32 @@ listener_create(sdb_fe_socket_t *sock, const char *address) return listener; } /* listener_create */ +static int +listener_listen(listener_t *listener) +{ + assert(listener); + + if (listen(listener->sock_fd, /* backlog = */ 32)) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s", + listener->address, sdb_strerror(errno, buf, sizeof(buf))); + return -1; + } + return 0; +} /* listener_listen */ + +static void +listener_close(listener_t *listener) +{ + assert(listener); + + if (listener->sock_fd < 0) + return; + + close(listener->sock_fd); + listener->sock_fd = -1; +} /* listener_close */ + /* * private data types */ @@ -484,12 +511,8 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) for (i = 0; i < sock->listeners_num; ++i) { listener_t *listener = sock->listeners + i; - if (listen(listener->sock_fd, /* backlog = */ 32)) { - char buf[1024]; - sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s", - listener->address, sdb_strerror(errno, buf, sizeof(buf))); + if (listener_listen(listener)) return -1; - } FD_SET(listener->sock_fd, &sockets); if (listener->sock_fd > max_listen_fd) @@ -584,6 +607,9 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) sdb_llist_iter_destroy(iter); } + for (i = 0; i < sock->listeners_num; ++i) + listener_close(sock->listeners + i); + sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads " "to terminate"); if (! sdb_channel_shutdown(sock->chan)) diff --git a/src/include/frontend/sock.h b/src/include/frontend/sock.h index ba33d13..6ab8cca 100644 --- a/src/include/frontend/sock.h +++ b/src/include/frontend/sock.h @@ -80,7 +80,8 @@ sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address); /* * sdb_fe_sock_listen_and_serve: * Listen on the specified socket and serve client requests. The loop - * terminates on error or when the loop condition turns to false. + * terminates on error or when the loop condition turns to false. All + * listening sockets will be closed at that time. * * Returns: * - 0 on success