X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Ffrontend%2Fsock.c;h=dd0afa907fbcf5564a05d187db28d9eca3c0ea0b;hp=8e2bc59dbf53d771d3eb6da298e97b8d5a46ab2f;hb=56b97a180a53aecbfe9f7162b8ece3faae973cf9;hpb=31891e01b907a4478bb427ca2830598a62da2210 diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 8e2bc59..dd0afa9 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -25,16 +25,25 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#if HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + #include "sysdb.h" -#include "core/error.h" +#include "core/object.h" +#include "frontend/connection-private.h" #include "frontend/sock.h" #include "utils/channel.h" +#include "utils/error.h" +#include "utils/llist.h" +#include "utils/os.h" +#include "utils/strbuf.h" #include - #include +#include #include #include @@ -46,18 +55,14 @@ #include #include +#include + #include /* * private data types */ -typedef struct { - int fd; - struct sockaddr_storage client_addr; - socklen_t client_addr_len; -} connection_t; - typedef struct { char *address; int type; @@ -70,11 +75,18 @@ typedef struct { const char *prefix; int (*opener)(listener_t *); + void (*closer)(listener_t *); } fe_listener_impl_t; struct sdb_fe_socket { listener_t *listeners; size_t listeners_num; + + sdb_llist_t *open_connections; + + /* channel used for communication between main + * and connection handler threads */ + sdb_channel_t *chan; }; /* @@ -84,32 +96,86 @@ struct sdb_fe_socket { static int open_unix_sock(listener_t *listener) { + const char *addr; + char *addr_copy; + char *base_dir; struct sockaddr_un sa; int status; listener->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); if (listener->sock_fd < 0) { char buf[1024]; - sdb_log(SDB_LOG_ERR, "sock: Failed to open UNIX socket: %s", - sdb_strerror(errno, buf, sizeof(buf))); + sdb_log(SDB_LOG_ERR, "frontend: Failed to open UNIX socket %s: %s", + listener->address, sdb_strerror(errno, buf, sizeof(buf))); return -1; } + if (*listener->address == '/') + addr = listener->address; + else + addr = listener->address + strlen("unix:"); + memset(&sa, 0, sizeof(sa)); sa.sun_family = AF_UNIX; - strncpy(sa.sun_path, listener->address + strlen("unix:"), - sizeof(sa.sun_path)); + strncpy(sa.sun_path, addr, sizeof(sa.sun_path)); + + addr_copy = strdup(addr); + if (! addr_copy) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: strdup failed: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + return -1; + } + base_dir = dirname(addr_copy); + + /* ensure that the directory exists */ + if (sdb_mkdir_all(base_dir, 0777)) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create directory '%s': %s", + base_dir, sdb_strerror(errno, errbuf, sizeof(errbuf))); + free(addr_copy); + return -1; + } + free(addr_copy); + + if (unlink(addr) && (errno != ENOENT)) { + char errbuf[1024]; + sdb_log(SDB_LOG_WARNING, "frontend: Failed to remove stale UNIX " + "socket %s: %s", listener->address + strlen("unix:"), + sdb_strerror(errno, errbuf, sizeof(errbuf))); + } status = bind(listener->sock_fd, (struct sockaddr *)&sa, sizeof(sa)); if (status) { char buf[1024]; - sdb_log(SDB_LOG_ERR, "sock: Failed to bind to UNIX socket: %s", - sdb_strerror(errno, buf, sizeof(buf))); + sdb_log(SDB_LOG_ERR, "frontend: Failed to bind to UNIX socket %s: %s", + listener->address, sdb_strerror(errno, buf, sizeof(buf))); return -1; } return 0; } /* open_unix_sock */ +static void +close_unix_sock(listener_t *listener) +{ + const char *addr; + assert(listener); + + if (! listener->address) + return; + + if (*listener->address == '/') + addr = listener->address; + else + addr = listener->address + strlen("unix:"); + + if (listener->sock_fd >= 0) + close(listener->sock_fd); + listener->sock_fd = -1; + + unlink(addr); +} /* close_unix_sock */ + /* * private variables */ @@ -117,16 +183,49 @@ open_unix_sock(listener_t *listener) /* the enum has to be sorted the same as the implementations array * to ensure that the type may be used as index into the array */ enum { - LISTENER_UNIXSOCK = 0, + LISTENER_UNIXSOCK = 0, /* this is the default */ }; static fe_listener_impl_t listener_impls[] = { - { LISTENER_UNIXSOCK, "unix", open_unix_sock }, + { LISTENER_UNIXSOCK, "unix", open_unix_sock, close_unix_sock }, }; /* * private helper functions */ +static int +listener_listen(listener_t *listener) +{ + assert(listener); + + /* try to reopen */ + if (listener->sock_fd < 0) + if (listener_impls[listener->type].opener(listener)) + return -1; + assert(listener->sock_fd >= 0); + + 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_impls[listener->type].closer) + listener_impls[listener->type].closer(listener); + + if (listener->sock_fd >= 0) + close(listener->sock_fd); + listener->sock_fd = -1; +} /* listener_close */ + static int get_type(const char *address) { @@ -136,7 +235,7 @@ get_type(const char *address) sep = strchr(address, (int)':'); if (! sep) - return -1; + return listener_impls[0].type; assert(sep > address); len = (size_t)(sep - address); @@ -158,11 +257,11 @@ listener_destroy(listener_t *listener) if (! listener) return; - if (listener->sock_fd >= 0) - close(listener->sock_fd); + listener_close(listener); if (listener->address) free(listener->address); + listener->address = NULL; } /* listener_destroy */ static listener_t * @@ -172,25 +271,37 @@ listener_create(sdb_fe_socket_t *sock, const char *address) int type; type = get_type(address); - if (type < 0) + if (type < 0) { + sdb_log(SDB_LOG_ERR, "frontend: Unsupported address type specified " + "in listen address '%s'", address); return NULL; + } listener = realloc(sock->listeners, - sock->listeners_num * sizeof(*sock->listeners)); - if (! listener) + (sock->listeners_num + 1) * sizeof(*sock->listeners)); + if (! listener) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s", + sdb_strerror(errno, buf, sizeof(buf))); return NULL; + } + sock->listeners = listener; listener = sock->listeners + sock->listeners_num; listener->sock_fd = -1; listener->address = strdup(address); if (! listener->address) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s", + sdb_strerror(errno, buf, sizeof(buf))); listener_destroy(listener); return NULL; } listener->type = type; if (listener_impls[type].opener(listener)) { + /* prints error */ listener_destroy(listener); return NULL; } @@ -199,6 +310,30 @@ listener_create(sdb_fe_socket_t *sock, const char *address) return listener; } /* listener_create */ +static void +socket_clear(sdb_fe_socket_t *sock) +{ + size_t i; + + assert(sock); + for (i = 0; i < sock->listeners_num; ++i) + listener_destroy(sock->listeners + i); + if (sock->listeners) + free(sock->listeners); + sock->listeners = NULL; + sock->listeners_num = 0; +} /* socket_clear */ + +static void +socket_close(sdb_fe_socket_t *sock) +{ + size_t i; + + assert(sock); + for (i = 0; i < sock->listeners_num; ++i) + listener_close(sock->listeners + i); +} /* socket_close */ + /* * connection handler functions */ @@ -206,30 +341,114 @@ listener_create(sdb_fe_socket_t *sock, const char *address) static void * connection_handler(void *data) { - sdb_channel_t *chan = data; + sdb_fe_socket_t *sock = data; - assert(chan); + assert(sock); while (42) { - connection_t conn; + struct timespec timeout = { 0, 500000000 }; /* .5 seconds */ + sdb_conn_t *conn; + int status; + + errno = 0; + status = sdb_channel_select(sock->chan, /* read */ NULL, &conn, + /* write */ NULL, NULL, &timeout); + if (status) { + char buf[1024]; - sdb_channel_select(chan, NULL, &conn, NULL, NULL, NULL); - if (conn.fd < 0) + if (errno == ETIMEDOUT) + continue; + if (errno == EBADF) /* channel shut down */ + break; + + sdb_log(SDB_LOG_ERR, "frontend: Failed to read from channel: %s", + sdb_strerror(errno, buf, sizeof(buf))); continue; + } - if (conn.client_addr.ss_family != AF_UNIX) { - sdb_log(SDB_LOG_ERR, "Accepted connection using unexpected " - "family type %d", conn.client_addr.ss_family); + status = (int)sdb_connection_read(conn); + if (status <= 0) { + /* error or EOF -> close connection */ + sdb_object_deref(SDB_OBJ(conn)); continue; } - /* XXX */ - sdb_log(SDB_LOG_INFO, "Accepted connection on fd=%i\n", conn.fd); - close(conn.fd); + /* return the connection to the main loop */ + if (sdb_llist_append(sock->open_connections, SDB_OBJ(conn))) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to re-append " + "connection %s to list of open connections", + SDB_OBJ(conn)->name); + } + + /* pass ownership back to list; or destroy in case of an error */ + sdb_object_deref(SDB_OBJ(conn)); } return NULL; } /* connection_handler */ +static int +connection_accept(sdb_fe_socket_t *sock, listener_t *listener) +{ + sdb_object_t *obj; + int status; + + obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd)); + if (! obj) + return -1; + + status = sdb_llist_append(sock->open_connections, obj); + if (status) + sdb_log(SDB_LOG_ERR, "frontend: Failed to append " + "connection %s to list of open connections", + obj->name); + + /* hand ownership over to the list; or destroy in case of an error */ + sdb_object_deref(obj); + return status; +} /* connection_accept */ + +static int +socket_handle_incoming(sdb_fe_socket_t *sock, + fd_set *ready, fd_set *exceptions) +{ + sdb_llist_iter_t *iter; + size_t i; + + for (i = 0; i < sock->listeners_num; ++i) { + listener_t *listener = sock->listeners + i; + if (FD_ISSET(listener->sock_fd, ready)) + if (connection_accept(sock, listener)) + continue; + } + + iter = sdb_llist_get_iter(sock->open_connections); + if (! iter) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator " + "for open connections"); + return -1; + } + + while (sdb_llist_iter_has_next(iter)) { + sdb_object_t *obj = sdb_llist_iter_get_next(iter); + + if (FD_ISSET(CONN(obj)->fd, exceptions)) { + sdb_log(SDB_LOG_INFO, "Exception on fd %d", + CONN(obj)->fd); + /* close the connection */ + sdb_llist_iter_remove_current(iter); + sdb_object_deref(obj); + continue; + } + + if (FD_ISSET(CONN(obj)->fd, ready)) { + sdb_llist_iter_remove_current(iter); + sdb_channel_write(sock->chan, &obj); + } + } + sdb_llist_iter_destroy(iter); + return 0; +} /* socket_handle_incoming */ + /* * public API */ @@ -242,22 +461,25 @@ sdb_fe_sock_create(void) sock = calloc(1, sizeof(*sock)); if (! sock) return NULL; + + sock->open_connections = sdb_llist_create(); + if (! sock->open_connections) { + sdb_fe_sock_destroy(sock); + return NULL; + } return sock; } /* sdb_fe_sock_create */ void sdb_fe_sock_destroy(sdb_fe_socket_t *sock) { - size_t i; - if (! sock) return; - for (i = 0; i < sock->listeners_num; ++i) { - listener_destroy(sock->listeners + i); - } - if (sock->listeners) - free(sock->listeners); + socket_clear(sock); + + sdb_llist_destroy(sock->open_connections); + sock->open_connections = NULL; free(sock); } /* sdb_fe_sock_destroy */ @@ -275,99 +497,144 @@ sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address) return 0; } /* sdb_fe_sock_add_listener */ +void +sdb_fe_sock_clear_listeners(sdb_fe_socket_t *sock) +{ + if (! sock) + return; + + socket_clear(sock); +} /* sdb_fe_sock_clear_listeners */ + int sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) { - sdb_channel_t *chan; fd_set sockets; - int max_fd = 0; + int max_listen_fd = 0; size_t i; - /* XXX: make the number of threads configurable */ - pthread_t handler_threads[5]; + pthread_t handler_threads[loop->num_threads]; + size_t num_threads; - if ((! sock) || (! sock->listeners_num) || (! loop)) + if ((! sock) || (! sock->listeners_num) || sock->chan + || (! loop) || (loop->num_threads <= 0)) return -1; - FD_ZERO(&sockets); + if (! loop->do_loop) + return 0; + 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, "sock: Failed to listen on socket %s: %s", - listener->address, sdb_strerror(errno, buf, sizeof(buf))); + if (listener_listen(listener)) { + socket_close(sock); return -1; } FD_SET(listener->sock_fd, &sockets); - if (listener->sock_fd > max_fd) - max_fd = listener->sock_fd; + if (listener->sock_fd > max_listen_fd) + max_listen_fd = listener->sock_fd; } - chan = sdb_channel_create(1024, sizeof(connection_t)); - if (! chan) + sock->chan = sdb_channel_create(1024, sizeof(sdb_conn_t *)); + if (! sock->chan) { + socket_close(sock); return -1; + } + sdb_log(SDB_LOG_INFO, "frontend: Starting %d connection " + "handler thread%s managing %d listener%s", + loop->num_threads, loop->num_threads == 1 ? "" : "s", + sock->listeners_num, sock->listeners_num == 1 ? "" : "s"); + + num_threads = loop->num_threads; memset(&handler_threads, 0, sizeof(handler_threads)); - /* XXX: error handling */ - for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) - pthread_create(&handler_threads[i], /* attr = */ NULL, - connection_handler, /* arg = */ chan); + for (i = 0; i < num_threads; ++i) { + errno = 0; + if (pthread_create(&handler_threads[i], /* attr = */ NULL, + connection_handler, /* arg = */ sock)) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "connection handler thread: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + num_threads = i; + break; + } + } - while (loop->do_loop) { - fd_set ready = sockets; + while (loop->do_loop && num_threads) { + struct timeval timeout = { 1, 0 }; /* one second */ + sdb_llist_iter_t *iter; + + int max_fd = max_listen_fd; + fd_set ready; + fd_set exceptions; int n; - struct timeval timeout = { 1, 0 }; /* one second */ + FD_ZERO(&ready); + FD_ZERO(&exceptions); + + ready = sockets; + + iter = sdb_llist_get_iter(sock->open_connections); + if (! iter) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator " + "for open connections"); + break; + } + + while (sdb_llist_iter_has_next(iter)) { + sdb_object_t *obj = sdb_llist_iter_get_next(iter); + + if (CONN(obj)->fd < 0) { + sdb_llist_iter_remove_current(iter); + sdb_object_deref(obj); + continue; + } + + FD_SET(CONN(obj)->fd, &ready); + FD_SET(CONN(obj)->fd, &exceptions); + + if (CONN(obj)->fd > max_fd) + max_fd = CONN(obj)->fd; + } + sdb_llist_iter_destroy(iter); errno = 0; - n = select(max_fd + 1, &ready, NULL, NULL, &timeout); + n = select(max_fd + 1, &ready, NULL, &exceptions, &timeout); if (n < 0) { char buf[1024]; if (errno == EINTR) continue; - sdb_log(SDB_LOG_ERR, "sock: Failed to monitor sockets: %s", + sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s", sdb_strerror(errno, buf, sizeof(buf))); - return -1; + break; } - - if (! n) + else if (! n) continue; - for (i = 0; i < sock->listeners_num; ++i) { - listener_t *listener = sock->listeners + i; - - if (FD_ISSET(listener->sock_fd, &ready)) { - connection_t conn; + /* handle new and open connections */ + if (socket_handle_incoming(sock, &ready, &exceptions)) + break; + } - memset(&conn, 0, sizeof(conn)); - conn.client_addr_len = sizeof(conn.client_addr); + socket_close(sock); - conn.fd = accept(listener->sock_fd, - (struct sockaddr *)&conn.client_addr, - &conn.client_addr_len); + sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads " + "to terminate"); + if (! sdb_channel_shutdown(sock->chan)) + for (i = 0; i < num_threads; ++i) + pthread_join(handler_threads[i], NULL); + /* else: we tried our best; let the operating system clean up */ - if (conn.fd < 0) { - char buf[1024]; - sdb_log(SDB_LOG_ERR, "sock: Failed to accept remote " - "connection: %s", sdb_strerror(errno, - buf, sizeof(buf))); - continue; - } + sdb_channel_destroy(sock->chan); + sock->chan = NULL; - sdb_channel_write(chan, &conn); - } - } - } - - for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) - pthread_cancel(handler_threads[i]); - for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) - pthread_join(handler_threads[i], NULL); + if (! num_threads) + return -1; return 0; } /* sdb_fe_sock_listen_and_server */