Fix --config-file for relative paths.
[dss.git] / ipc.c
diff --git a/ipc.c b/ipc.c
index c4389398b9b88eca5a599a25aa9b5e47d51ca808..9b38c1318541d24a5686c76eccfc330a3a204177 100644 (file)
--- a/ipc.c
+++ b/ipc.c
@@ -1,3 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
 #include <sys/wait.h>
 #include <stdio.h>
 #include <inttypes.h>
 #include <stddef.h>
 #include <limits.h>
 #include <sys/param.h>
+#include <stdbool.h>
 
 #include "gcc-compat.h"
-#include "string.h"
+#include "str.h"
 #include "log.h"
 #include "gcc-compat.h"
-#include "error.h"
+#include "err.h"
+#include "ipc.h"
 
 #if (defined(__GNUC__) && defined(__i386__))
 #define get16bits(d) (*((const uint16_t *) (d)))
@@ -201,7 +205,7 @@ static int dss_realpath(const char *name, char **resolved_path)
 
                                /* Careful here, end may be a pointer into extra_buf... */
                                memmove(&extra_buf[n], end, len + 1);
-                               name = end = memcpy(extra_buf, buf, n);
+                               end = memcpy(extra_buf, buf, n);
 
                                if (buf[0] == '/') /* It's an absolute symlink */
                                        dest = rpath + 1;
@@ -226,7 +230,7 @@ error:
        return ret;
 }
 
-static inline int get_key_or_die(char *config_file)
+static int get_key_or_die(const char *config_file)
 {
        int ret;
        struct stat statbuf;
@@ -236,11 +240,11 @@ static inline int get_key_or_die(char *config_file)
        if (stat(config_file, &statbuf) == 0) {
                ret = dss_realpath(config_file, &rpath);
                if (ret < 0) {
-                       DSS_EMERG_LOG("could not resolve path %s: %s\n", config_file,
-                               dss_strerror(-ret));
+                       DSS_EMERG_LOG(("could not resolve path %s: %s\n", config_file,
+                               dss_strerror(-ret)));
                        exit(EXIT_FAILURE);
                }
-               DSS_DEBUG_LOG("resolved path: %s\n", rpath);
+               DSS_DEBUG_LOG(("resolved path: %s\n", rpath));
        } else
                /*
                 * This happens if the user did not specify a config file, and
@@ -256,11 +260,11 @@ static inline int get_key_or_die(char *config_file)
        return ret;
 }
 
-static int mutex_get(int key, int flags)
+static int mutex_get(key_t key, int flags)
 {
        int ret;
 
-       DSS_DEBUG_LOG("getting semaphore 0x%x\n", key);
+       DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
        ret = semget(key, 2, flags);
        if (ret < 0)
                return -ERRNO_TO_DSS_ERROR(errno);
@@ -271,7 +275,7 @@ static int do_semop(int id, struct sembuf *sops, int num)
 {
        int ret;
 
-       DSS_DEBUG_LOG("calling semop\n");
+       DSS_DEBUG_LOG(("calling semop\n"));
        do {
                ret = semop(id, sops, num);
                if (ret >= 0)
@@ -280,86 +284,68 @@ static int do_semop(int id, struct sembuf *sops, int num)
        return -ERRNO_TO_DSS_ERROR(errno);
 }
 
-static int mutex_lock(int id)
+static bool mutex_is_locked(int id)
 {
+       struct sembuf sops;
        int ret;
 
-       DSS_DEBUG_LOG("locking\n");
-       struct sembuf sops[4] = {
-               {
-                       .sem_num = 0,
-                       .sem_op = 0,
-                       .sem_flg = SEM_UNDO | IPC_NOWAIT
-               },
-               {
-                       .sem_num = 0,
-                       .sem_op = 1,
-                       .sem_flg = SEM_UNDO | IPC_NOWAIT
-               },
-               {
-                       .sem_num = 1,
-                       .sem_op = 0,
-                       .sem_flg = SEM_UNDO | IPC_NOWAIT
-               },
-               {
-                       .sem_num = 1,
-                       .sem_op = 1,
-                       .sem_flg = SEM_UNDO | IPC_NOWAIT
-               }
-       };
-       ret = do_semop(id, sops, 4);
-       if (ret < 0)
-               return -ERRNO_TO_DSS_ERROR(errno);
-       return 1;
-}
+       DSS_DEBUG_LOG(("trying to lock\n"));
 
-static int mutex_try_lock(int id)
-{
-       int ret;
+       sops.sem_num = 0;
+       sops.sem_op = 0;
+       sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
 
-       DSS_DEBUG_LOG("trying to lock\n");
-       struct sembuf sops[2] = {
-               {
-                       .sem_num = 0,
-                       .sem_op = 0,
-                       .sem_flg = SEM_UNDO | IPC_NOWAIT
-               },
-               {
-                       .sem_num = 0,
-                       .sem_op = 1,
-                       .sem_flg = SEM_UNDO | IPC_NOWAIT
-               }
-       };
-       ret = do_semop(id, sops, 2);
+       ret = do_semop(id, &sops, 1);
        if (ret < 0)
-               return -ERRNO_TO_DSS_ERROR(errno);
-       return 1;
+               return true;
+       return false;
 }
 
 int lock_dss(char *config_file)
 {
-       int ret, key = get_key_or_die(config_file);
+       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)
                return ret;
-       return mutex_lock(ret);
+       id = ret;
+
+       sops[0].sem_num = 0;
+       sops[0].sem_op = 0;
+       sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
+
+       sops[1].sem_num = 0;
+       sops[1].sem_op = 1;
+       sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
+
+       sops[2].sem_num = 1;
+       sops[2].sem_op = 0;
+       sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
+
+       sops[3].sem_num = 1;
+       sops[3].sem_op = 1;
+       sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
+
+       return do_semop(id, sops, 4);
 }
 
 int get_dss_pid(char *config_file, pid_t *pid)
 {
-       int ret, semid, key = get_key_or_die(config_file);
+       int ret, semid;
+       key_t key = get_key_or_die(config_file);
 
+       if (pid)
+               *pid = 0;
        ret = mutex_get(key, 0);
        if (ret < 0)
-               return ret;
+               return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
        semid = ret;
        ret = semctl(semid, 1, GETPID);
        if (ret < 0)
                return -E_NOT_RUNNING;
-       *pid = ret;
-       ret = mutex_try_lock(semid);
-       if (ret >= 0)
-               return -E_NOT_RUNNING;
-       return 1;
+       if (pid)
+               *pid = ret;
+       return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
 }