Code

socket frontend: Disallow duplicate listen_and_serve() calls.
authorSebastian Harl <sh@tokkee.org>
Sat, 26 Oct 2013 11:51:06 +0000 (13:51 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 26 Oct 2013 11:51:06 +0000 (13:51 +0200)
… and fixed a memory leak.

src/frontend/sock.c

index be69f0ade8832c9076e301da44078cafb1c3fcf0..9fba695b6ef44c6e4ed79a933dafc93ba725128e 100644 (file)
@@ -476,6 +476,9 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
        if ((! sock) || (! sock->listeners_num) || (! loop))
                return -1;
 
+       if (sock->chan)
+               return -1;
+
        FD_ZERO(&sockets);
 
        for (i = 0; i < sock->listeners_num; ++i) {
@@ -523,7 +526,7 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
                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)) {
@@ -546,7 +549,7 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
 
                        sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
                                        sdb_strerror(errno, buf, sizeof(buf)));
-                       return -1;
+                       break;
                }
 
                if (! n)
@@ -563,7 +566,7 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
                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)) {
@@ -587,6 +590,9 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
                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 */