From 3b9fc779bc528586f52178223a3bbf2d9cd42cd8 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Jan 12 2018 15:42:03 +0000 Subject: Add SANLOCK_RUN_DIR environment variable Using this environment variable, we can use custom run directory for placing sanlock socket and lockfile. This allows running sanlock as non-privileged user in the tests. When running the sanlock executable, we warn if using non-standard run directory, since this should be used only for testing. Signed-off-by: Nir Soffer --- diff --git a/src/client.c b/src/client.c index 6c0fcec..fce6c60 100644 --- a/src/client.c +++ b/src/client.c @@ -34,6 +34,7 @@ #include "sanlock_admin.h" #include "sanlock_sock.h" #include "sanlock_rv.h" +#include "env.h" #ifndef GNUC_UNUSED #define GNUC_UNUSED __attribute__((__unused__)) @@ -43,13 +44,17 @@ static int connect_socket(int *sock_fd) { int rv, s; struct sockaddr_un addr; + static const char *run_dir; *sock_fd = -1; s = socket(AF_LOCAL, SOCK_STREAM, 0); if (s < 0) return -errno; - rv = sanlock_socket_address(SANLK_RUN_DIR, &addr); + if (run_dir == NULL) + run_dir = env_get("SANLOCK_RUN_DIR", DEFAULT_RUN_DIR); + + rv = sanlock_socket_address(run_dir, &addr); if (rv < 0) { close(s); return rv; diff --git a/src/main.c b/src/main.c index e2c882f..747e4c2 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ #include "helper.h" #include "timeouts.h" #include "paxos_lease.h" +#include "env.h" #define SIGRUNPATH 100 /* anything that's not SIGTERM/SIGKILL */ @@ -86,6 +87,7 @@ static struct thread_pool pool; static struct random_data rand_data; static char rand_state[32]; static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER; +static const char *run_dir = NULL; static void close_helper(void) { @@ -1290,7 +1292,7 @@ static int setup_listener(void) struct sockaddr_un addr; int rv, fd, ci; - rv = sanlock_socket_address(SANLK_RUN_DIR, &addr); + rv = sanlock_socket_address(run_dir, &addr); if (rv < 0) return rv; @@ -1631,6 +1633,7 @@ static int do_daemon(void) { int fd, rv; + run_dir = env_get(SANLOCK_RUN_DIR, DEFAULT_RUN_DIR); /* This can take a while so do it before forking. */ setup_groups(); @@ -1665,7 +1668,10 @@ static int do_daemon(void) setup_signals(); setup_logging(); - fd = lockfile(SANLK_RUN_DIR, SANLK_LOCKFILE_NAME, com.uid, com.gid); + if (strcmp(run_dir, DEFAULT_RUN_DIR)) + log_warn("Using non-standard run directory '%s'", run_dir); + + fd = lockfile(run_dir, SANLK_LOCKFILE_NAME, com.uid, com.gid); if (fd < 0) { close_logging(); return fd; @@ -1706,7 +1712,7 @@ static int do_daemon(void) out: /* order reversed from setup so lockfile is last */ close_logging(); - unlink_lockfile(fd, SANLK_RUN_DIR, SANLK_LOCKFILE_NAME); + unlink_lockfile(fd, run_dir, SANLK_LOCKFILE_NAME); return rv; } diff --git a/src/sanlock_internal.h b/src/sanlock_internal.h index 46581b7..87d8473 100644 --- a/src/sanlock_internal.h +++ b/src/sanlock_internal.h @@ -43,7 +43,9 @@ #define COMMAND_MAX 4096 -#define SANLK_RUN_DIR "/var/run/sanlock" +#define SANLOCK_RUN_DIR "SANLOCK_RUN_DIR" +#define DEFAULT_RUN_DIR "/var/run/sanlock" + #define SANLK_LOG_DIR "/var/log" #define SANLK_LOGFILE_NAME "sanlock.log" #define SANLK_LOCKFILE_NAME "sanlock.pid"