build: Combine CFLAGS and DEBUG_CFLAGS.
[dss.git] / ipc.c
diff --git a/ipc.c b/ipc.c
index 65ef20049d26748ef54b6fd60068e7492b159beb..c55554c5d3fd815cd09c045a94f7b72d5bd5db85 100644 (file)
--- a/ipc.c
+++ b/ipc.c
@@ -13,6 +13,7 @@
 #include <stddef.h>
 #include <limits.h>
 #include <sys/param.h>
+#include <stdbool.h>
 
 #include "gcc-compat.h"
 #include "str.h"
@@ -227,7 +228,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;
@@ -284,7 +285,6 @@ static int do_semop(int id, struct sembuf *sops, int num)
 static int mutex_lock(int id)
 {
        struct sembuf sops[4];
-       int ret;
 
        DSS_DEBUG_LOG(("locking\n"));
 
@@ -304,31 +304,24 @@ static int mutex_lock(int id)
        sops[3].sem_op = 1;
        sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
 
-       ret = do_semop(id, sops, 4);
-       if (ret < 0)
-               return -ERRNO_TO_DSS_ERROR(errno);
-       return 1;
+       return do_semop(id, sops, 4);
 }
 
-static int mutex_try_lock(int id)
+static bool mutex_is_locked(int id)
 {
-       struct sembuf sops[2];
+       struct sembuf sops;
        int ret;
 
        DSS_DEBUG_LOG(("trying to lock\n"));
 
-       sops[0].sem_num = 0;
-       sops[0].sem_op = 0;
-       sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
+       sops.sem_num = 0;
+       sops.sem_op = 0;
+       sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
 
-       sops[1].sem_num = 0;
-       sops[1].sem_op = 1;
-       sops[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)
@@ -345,6 +338,8 @@ int get_dss_pid(char *config_file, pid_t *pid)
 {
        int ret, semid, key = get_key_or_die(config_file);
 
+       if (pid)
+               *pid = 0;
        ret = mutex_get(key, 0);
        if (ret < 0)
                return ret;
@@ -352,9 +347,7 @@ int get_dss_pid(char *config_file, pid_t *pid)
        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;
 }