diff --git a/src/frontend/sock.c b/src/frontend/sock.c
index 90aeab1406a6a6389f8ce083dbab72a624b4a936..96bc04baf805640b747f46861d181ec0177e4e4b 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
#include "utils/channel.h"
#include "utils/error.h"
#include "utils/llist.h"
+#include "utils/os.h"
#include "utils/strbuf.h"
#include <assert.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <libgen.h>
+
#include <pthread.h>
/*
static int
open_unix_sock(listener_t *listener)
{
+ const char *addr;
+ char *addr_copy;
+ char *base_dir;
struct sockaddr_un sa;
int status;
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);
- 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:"),
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(listener->address + strlen("unix:"));
+ unlink(addr);
} /* 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 {
- 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 },
sep = strchr(address, (int)':');
if (! sep)
- return -1;
+ return listener_impls[0].type;
assert(sep > address);
len = (size_t)(sep - address);
return -1;
}
- sdb_log(SDB_LOG_INFO, "frontend: Starting %d connection "
- "handler thread%s managing %d listener%s",
+ 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");