diff --git a/src/frontend/sock.c b/src/frontend/sock.c
index ce4c07ccd1017247ea21d4edfaddaf139b486177..cb6b9fcdd7a889405e0da21771a99a9acd2c47fa 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif /* HAVE_CONFIG_H */
+
#include "sysdb.h"
#include "core/object.h"
#include "frontend/connection-private.h"
#include "sysdb.h"
#include "core/object.h"
#include "frontend/connection-private.h"
#include "utils/channel.h"
#include "utils/error.h"
#include "utils/llist.h"
#include "utils/channel.h"
#include "utils/error.h"
#include "utils/llist.h"
+#include "utils/os.h"
#include "utils/strbuf.h"
#include <assert.h>
#include "utils/strbuf.h"
#include <assert.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <libgen.h>
+
#include <pthread.h>
/*
#include <pthread.h>
/*
static int
open_unix_sock(listener_t *listener)
{
static int
open_unix_sock(listener_t *listener)
{
+ const char *addr;
+ char *addr_copy;
+ char *base_dir;
struct sockaddr_un sa;
int status;
struct sockaddr_un sa;
int status;
return -1;
}
return -1;
}
+ if (*listener->address == '/')
+ addr = listener->address;
+ else
+ addr = listener->address + strlen("unix:");
+
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_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);
- if (unlink(listener->address + strlen("unix:")) && (errno != ENOENT)) {
+ /* 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:"),
char errbuf[1024];
sdb_log(SDB_LOG_WARNING, "frontend: Failed to remove stale UNIX "
"socket %s: %s", listener->address + strlen("unix:"),
static void
close_unix_sock(listener_t *listener)
{
static void
close_unix_sock(listener_t *listener)
{
+ const char *addr;
assert(listener);
assert(listener);
+
if (! listener->address)
return;
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;
if (listener->sock_fd >= 0)
close(listener->sock_fd);
listener->sock_fd = -1;
- unlink(listener->address + strlen("unix:"));
+ unlink(addr);
} /* close_unix_sock */
/*
} /* close_unix_sock */
/*
/* 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 {
/* 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, close_unix_sock },
};
static fe_listener_impl_t listener_impls[] = {
{ LISTENER_UNIXSOCK, "unix", open_unix_sock, close_unix_sock },
sep = strchr(address, (int)':');
if (! sep)
sep = strchr(address, (int)':');
if (! sep)
- return -1;
+ return listener_impls[0].type;
assert(sep > address);
len = (size_t)(sep - address);
assert(sep > address);
len = (size_t)(sep - address);
}
listener = realloc(sock->listeners,
}
listener = realloc(sock->listeners,
- sock->listeners_num * sizeof(*sock->listeners));
+ (sock->listeners_num + 1) * sizeof(*sock->listeners));
if (! listener) {
char buf[1024];
sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
if (! listener) {
char buf[1024];
sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
return listener;
} /* listener_create */
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)
{
static void
socket_close(sdb_fe_socket_t *sock)
{
continue;
}
continue;
}
- status = (int)sdb_connection_read(conn);
+ status = (int)sdb_connection_handle(conn);
if (status <= 0) {
/* error or EOF -> close connection */
sdb_object_deref(SDB_OBJ(conn));
if (status <= 0) {
/* error or EOF -> close connection */
sdb_object_deref(SDB_OBJ(conn));
while (sdb_llist_iter_has_next(iter)) {
sdb_object_t *obj = sdb_llist_iter_get_next(iter);
while (sdb_llist_iter_has_next(iter)) {
sdb_object_t *obj = sdb_llist_iter_get_next(iter);
- if (FD_ISSET(CONN(obj)->fd, exceptions))
+ if (FD_ISSET(CONN(obj)->fd, exceptions)) {
sdb_log(SDB_LOG_INFO, "Exception on fd %d",
CONN(obj)->fd);
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);
if (FD_ISSET(CONN(obj)->fd, ready)) {
sdb_llist_iter_remove_current(iter);
void
sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
{
void
sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
{
- size_t i;
-
if (! sock)
return;
if (! sock)
return;
- for (i = 0; i < sock->listeners_num; ++i) {
- listener_destroy(sock->listeners + i);
- }
- if (sock->listeners)
- free(sock->listeners);
- sock->listeners = NULL;
+ socket_clear(sock);
sdb_llist_destroy(sock->open_connections);
sock->open_connections = NULL;
sdb_llist_destroy(sock->open_connections);
sock->open_connections = NULL;
return 0;
} /* sdb_fe_sock_add_listener */
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)
{
int
sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
{
int max_listen_fd = 0;
size_t i;
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) || sock->chan)
+ if ((! sock) || (! sock->listeners_num) || sock->chan
+ || (! loop) || (loop->num_threads <= 0))
return -1;
if (! loop->do_loop)
return -1;
if (! loop->do_loop)
return -1;
}
return -1;
}
+ sdb_log(SDB_LOG_INFO, "frontend: Starting %zu connection "
+ "handler thread%s managing %zu 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));
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 = */ sock);
+ 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) {
+ while (loop->do_loop && num_threads) {
struct timeval timeout = { 1, 0 }; /* one second */
sdb_llist_iter_t *iter;
struct timeval timeout = { 1, 0 }; /* one second */
sdb_llist_iter_t *iter;
while (sdb_llist_iter_has_next(iter)) {
sdb_object_t *obj = sdb_llist_iter_get_next(iter);
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);
FD_SET(CONN(obj)->fd, &ready);
FD_SET(CONN(obj)->fd, &exceptions);
sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads "
"to terminate");
if (! sdb_channel_shutdown(sock->chan))
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)
+ for (i = 0; i < num_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;
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;
+
+ if (! num_threads)
+ return -1;
return 0;
} /* sdb_fe_sock_listen_and_server */
return 0;
} /* sdb_fe_sock_listen_and_server */