Code

socket frontend: Close listening sockets before returning.
authorSebastian Harl <sh@tokkee.org>
Sat, 26 Oct 2013 12:05:42 +0000 (14:05 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 26 Oct 2013 12:05:42 +0000 (14:05 +0200)
Also, cleaned up listener handling a bit.

src/frontend/sock.c
src/include/frontend/sock.h

index 9fba695b6ef44c6e4ed79a933dafc93ba725128e..27a1b679c2815f17932cd3845383ff7649a9d502 100644 (file)
@@ -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))
index ba33d13c194ff575774108ac5bf1af4cc7954b2c..6ab8ccadbf9ebb4ebee101a2701e9e43d57cb296 100644 (file)
@@ -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