15 #include <sys/param.h>
18 #include "gcc-compat.h"
21 #include "gcc-compat.h"
25 static key_t get_key(const char *config_file)
30 ret = ftok(config_file, 'D');
34 * This happens if the user did not specify a config file, and the
35 * default config file does not exist. Another (unlikely) possibility
36 * is that the config file was removed between startup and this call.
37 * We don't care about these corner cases too much and just return a
38 * fixed key in this case.
40 return 0x0D55; /* no magic, this number just looks a bit like DSS */
43 static int mutex_get(key_t key, int flags)
47 DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
48 ret = semget(key, 2, flags);
50 return -ERRNO_TO_DSS_ERROR(errno);
54 static int do_semop(int id, struct sembuf *sops, int num)
58 DSS_DEBUG_LOG(("calling semop\n"));
60 ret = semop(id, sops, num);
63 } while (errno == EINTR);
64 return -ERRNO_TO_DSS_ERROR(errno);
67 static bool mutex_is_locked(int id)
72 DSS_DEBUG_LOG(("trying to lock\n"));
76 sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
78 ret = do_semop(id, &sops, 1);
84 int lock_dss(char *config_file)
87 struct sembuf sops[4];
88 key_t key = get_key(config_file);
90 ret = mutex_get(key, IPC_CREAT | 0600);
97 sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
101 sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
105 sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
109 sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
111 return do_semop(id, sops, 4);
114 int get_dss_pid(char *config_file, pid_t *pid)
117 key_t key = get_key(config_file);
121 ret = mutex_get(key, 0);
123 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
125 ret = semctl(semid, 1, GETPID);
127 return -E_NOT_RUNNING;
130 return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;