X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=ipc.c;h=183c76e9b4ad0b46ee2910207f12626fbb7087ca;hp=65ef20049d26748ef54b6fd60068e7492b159beb;hb=5b1c780d1a3e02a6f64242ef5d8c0a2dc71de3ec;hpb=f33cf243ae1cb5657cccb7c43c99093936093b4a diff --git a/ipc.c b/ipc.c index 65ef200..183c76e 100644 --- a/ipc.c +++ b/ipc.c @@ -13,6 +13,7 @@ #include #include #include +#include #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; @@ -257,11 +258,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); @@ -281,41 +282,33 @@ 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[4]; + struct sembuf sops; int ret; - DSS_DEBUG_LOG(("locking\n")); - - 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; + DSS_DEBUG_LOG(("trying to lock\n")); - sops[3].sem_num = 1; - sops[3].sem_op = 1; - sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT; + sops.sem_num = 0; + sops.sem_op = 0; + sops.sem_flg = SEM_UNDO | IPC_NOWAIT; - ret = do_semop(id, sops, 4); + ret = do_semop(id, &sops, 1); if (ret < 0) - return -ERRNO_TO_DSS_ERROR(errno); - return 1; + return true; + return false; } -static int mutex_try_lock(int id) +int lock_dss(char *config_file) { - struct sembuf sops[2]; - int ret; + int ret, id; + struct sembuf sops[4]; + key_t key = get_key_or_die(config_file); - DSS_DEBUG_LOG(("trying to lock\n")); + ret = mutex_get(key, IPC_CREAT | 0600); + if (ret < 0) + return ret; + id = ret; sops[0].sem_num = 0; sops[0].sem_op = 0; @@ -325,36 +318,32 @@ static int mutex_try_lock(int id) sops[1].sem_op = 1; sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT; - ret = do_semop(id, sops, 2); - if (ret < 0) - return -ERRNO_TO_DSS_ERROR(errno); - return 1; -} + sops[2].sem_num = 1; + sops[2].sem_op = 0; + sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT; -int lock_dss(char *config_file) -{ - int ret, key = get_key_or_die(config_file); + sops[3].sem_num = 1; + sops[3].sem_op = 1; + sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT; - ret = mutex_get(key, IPC_CREAT | 0600); - if (ret < 0) - return ret; - return mutex_lock(ret); + 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; }