]> git.tuebingen.mpg.de Git - dss.git/blob - ipc.c
ipc.c: Use ftok() instead of SuperFastHash.
[dss.git] / ipc.c
1 #include <sys/wait.h>
2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/ipc.h>
9 #include <sys/sem.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <sys/stat.h>
13 #include <stddef.h>
14 #include <limits.h>
15 #include <sys/param.h>
16 #include <stdbool.h>
17
18 #include "gcc-compat.h"
19 #include "str.h"
20 #include "log.h"
21 #include "gcc-compat.h"
22 #include "err.h"
23 #include "ipc.h"
24
25 static int get_key(const char *config_file)
26 {
27         int ret;
28
29         assert(config_file);
30         ret = ftok(config_file, 'D');
31         if (ret >= 0)
32                 return ret;
33         /*
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.
39          */
40         return 0x0D55; /* no magic, this number just looks a bit like DSS */
41 }
42
43 static int mutex_get(int key, int flags)
44 {
45         int ret;
46
47         DSS_DEBUG_LOG(("getting semaphore 0x%x\n", key));
48         ret = semget(key, 2, flags);
49         if (ret < 0)
50                 return -ERRNO_TO_DSS_ERROR(errno);
51         return ret;
52 }
53
54 static int do_semop(int id, struct sembuf *sops, int num)
55 {
56         int ret;
57
58         DSS_DEBUG_LOG(("calling semop\n"));
59         do {
60                 ret = semop(id, sops, num);
61                 if (ret >= 0)
62                         return 1;
63         } while (errno == EINTR);
64         return -ERRNO_TO_DSS_ERROR(errno);
65 }
66
67 static int mutex_lock(int id)
68 {
69         struct sembuf sops[4];
70
71         DSS_DEBUG_LOG(("locking\n"));
72
73         sops[0].sem_num = 0;
74         sops[0].sem_op = 0;
75         sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
76
77         sops[1].sem_num = 0;
78         sops[1].sem_op = 1;
79         sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
80
81         sops[2].sem_num = 1;
82         sops[2].sem_op = 0;
83         sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
84
85         sops[3].sem_num = 1;
86         sops[3].sem_op = 1;
87         sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
88
89         return do_semop(id, sops, 4);
90 }
91
92 static bool mutex_is_locked(int id)
93 {
94         struct sembuf sops;
95         int ret;
96
97         DSS_DEBUG_LOG(("trying to lock\n"));
98
99         sops.sem_num = 0;
100         sops.sem_op = 0;
101         sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
102
103         ret = do_semop(id, &sops, 1);
104         if (ret < 0)
105                 return true;
106         return false;
107 }
108
109 int lock_dss(char *config_file)
110 {
111         int ret, key = get_key(config_file);
112
113         ret = mutex_get(key, IPC_CREAT | 0600);
114         if (ret < 0)
115                 return ret;
116         return mutex_lock(ret);
117 }
118
119 int get_dss_pid(char *config_file, pid_t *pid)
120 {
121         int ret, semid, key = get_key(config_file);
122
123         if (pid)
124                 *pid = 0;
125         ret = mutex_get(key, 0);
126         if (ret < 0)
127                 return ret;
128         semid = ret;
129         ret = semctl(semid, 1, GETPID);
130         if (ret < 0)
131                 return -E_NOT_RUNNING;
132         if (pid)
133                 *pid = ret;
134         return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
135 }