]> git.tuebingen.mpg.de Git - dss.git/commitdiff
Compute ipc key only once.
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 11 May 2024 21:12:10 +0000 (23:12 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 23 May 2024 14:52:28 +0000 (16:52 +0200)
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
ipc.c
ipc.h

diff --git a/dss.c b/dss.c
index e709d34e0d7a0d6b12a3d74b3c0f0d7eaa901c2d..1ea8adab7b3dd0155c340c1810fedfe700826432 100644 (file)
--- 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 2ccd156abb283a8cc0c7eeb33750edd6bc8da60b..e6ec79e062cf2543f8159d6bcf390cdb872438c6 100644 (file)
--- 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 b5d6451ea8f59185682df68238c0d937e1d8a7b8..234b652310295bbc8c9b9f4ff1a339fa6eb41694 100644 (file)
--- 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);