diff --git a/src/frontend/sock.c b/src/frontend/sock.c
index d157853bb64bc4b7489af9beb8c12dff5ab0e853..27a1b679c2815f17932cd3845383ff7649a9d502 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
if (listener->sock_fd >= 0)
close(listener->sock_fd);
+ listener->sock_fd = -1;
if (listener->address)
free(listener->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
*/
status = connection_read(conn->conn.fd);
if (status <= 0) {
- /* error or EOF → close connection */
+ /* error or EOF -> close connection */
sdb_object_deref(SDB_OBJ(conn));
}
else {
if ((! sock) || (! sock->listeners_num) || (! loop))
return -1;
+ if (sock->chan)
+ return -1;
+
FD_ZERO(&sockets);
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)
if (! iter) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
"for open connections");
- return -1;
+ break;
}
while (sdb_llist_iter_has_next(iter)) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
sdb_strerror(errno, buf, sizeof(buf)));
- return -1;
+ break;
}
if (! n)
if (! iter) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
"for open connections");
- return -1;
+ break;
}
while (sdb_llist_iter_has_next(iter)) {
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))
for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
pthread_join(handler_threads[i], NULL);
/* else: we tried our best; let the operating system clean up */
+
+ sdb_channel_destroy(sock->chan);
+ sock->chan = NULL;
return 0;
} /* sdb_fe_sock_listen_and_server */