X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=ipc.c;h=c55554c5d3fd815cd09c045a94f7b72d5bd5db85;hp=14b0259a8d9ad19d8082d992c042b0c3ffd95d4c;hb=5235e61583d358c177955c1da642e7c49e527acc;hpb=b3aca66350a1a4244cdad55fcd8cd64293336006;ds=sidebyside diff --git a/ipc.c b/ipc.c index 14b0259..c55554c 100644 --- a/ipc.c +++ b/ipc.c @@ -13,12 +13,13 @@ #include #include #include +#include #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__)) @@ -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; @@ -237,11 +238,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 @@ -261,7 +262,7 @@ static int mutex_get(int key, int flags) { int ret; - DSS_DEBUG_LOG("getting semaphore 0x%x\n", key); + DSS_DEBUG_LOG(("getting semaphore 0x%x\n", key)); ret = semget(key, 2, flags); if (ret < 0) return -ERRNO_TO_DSS_ERROR(errno); @@ -272,7 +273,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) @@ -284,9 +285,8 @@ 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"); + DSS_DEBUG_LOG(("locking\n")); sops[0].sem_num = 0; sops[0].sem_op = 0; @@ -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"); + 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; }