From 71f6d9f0a6876e37a95afa93eadaa68d7f912b8c Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 11 May 2024 23:12:10 +0200 Subject: [PATCH] Compute ipc key only once. It's easier if dss.c passes the hash of the the config file path to the ipc functions than to pass a char pointer. --- dss.c | 10 +++++++--- ipc.c | 14 +++----------- ipc.h | 5 +++-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/dss.c b/dss.c index e709d34..1ea8ada 100644 --- a/dss.c +++ b/dss.c @@ -67,6 +67,8 @@ static bool daemonized; static FILE *logfile; /* Realpath of the config file. */ static char *config_file; +/* derived from config file path */ +uint32_t ipc_key; /** The read end of the signal pipe */ static int signal_pipe; /** Process id of current pre-create-hook/rsync/post-create-hook process. */ @@ -295,12 +297,14 @@ static void set_config_file_name(void) config_file = arg; } DSS_DEBUG_LOG(("config file: %s\n", config_file)); + ipc_key = super_fast_hash((uint8_t *)config_file, + strlen(config_file), 0) >> 1; } static int send_signal(int sig, bool wait) { pid_t pid; - int ret = get_dss_pid(config_file, &pid); + int ret = get_dss_pid(ipc_key, &pid); unsigned ms = 32; struct timespec ts; @@ -1599,7 +1603,7 @@ static void exit_hook(int exit_code) static void lock_dss_or_die(void) { - int ret = lock_dss(config_file); + int ret = lock_dss(ipc_key); if (ret < 0) { DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret))); @@ -1616,7 +1620,7 @@ static int com_run(void) DSS_ERROR_LOG(("dry run not supported by this command\n")); return -E_SYNTAX; } - ret = get_dss_pid(config_file, &pid); + ret = get_dss_pid(ipc_key, &pid); if (ret >= 0) { DSS_ERROR_LOG(("pid %d\n", (int)pid)); return -E_ALREADY_RUNNING; diff --git a/ipc.c b/ipc.c index 2ccd156..e6ec79e 100644 --- a/ipc.c +++ b/ipc.c @@ -35,7 +35,7 @@ * SuperFastHash, by Paul Hsieh. * http://www.azillionmonkeys.com/qed/hash.html */ -static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash) +uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash) { uint32_t tmp; int rem = len & 3; @@ -78,12 +78,6 @@ static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash return hash; } -static int get_key_or_die(const char *config_file) -{ - assert(config_file); - return super_fast_hash((uint8_t *)config_file, strlen(config_file), 0) >> 1; -} - static int mutex_get(key_t key, int flags) { int ret; @@ -125,11 +119,10 @@ static bool mutex_is_locked(int id) return false; } -int lock_dss(char *config_file) +int lock_dss(uint32_t key) { int ret, id; struct sembuf sops[4]; - key_t key = get_key_or_die(config_file); ret = mutex_get(key, IPC_CREAT | 0600); if (ret < 0) @@ -155,10 +148,9 @@ int lock_dss(char *config_file) return do_semop(id, sops, 4); } -int get_dss_pid(char *config_file, pid_t *pid) +int get_dss_pid(uint32_t key, pid_t *pid) { int ret, semid; - key_t key = get_key_or_die(config_file); if (pid) *pid = 0; diff --git a/ipc.h b/ipc.h index b5d6451..234b652 100644 --- a/ipc.h +++ b/ipc.h @@ -1,3 +1,4 @@ /* SPDX-License-Identifier: GPL-2.0 */ -int lock_dss(char *config_file); -int get_dss_pid(char *config_file, pid_t *pid); +uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash); +int lock_dss(uint32_t key); +int get_dss_pid(uint32_t key, pid_t *pid); -- 2.39.5