]> git.tuebingen.mpg.de Git - dss.git/blob - ipc.c
Use standard realpath().
[dss.git] / ipc.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #include <sys/wait.h>
4 #include <stdio.h>
5 #include <inttypes.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/ipc.h>
11 #include <sys/sem.h>
12 #include <string.h>
13 #include <assert.h>
14 #include <sys/stat.h>
15 #include <stddef.h>
16 #include <limits.h>
17 #include <sys/param.h>
18 #include <stdbool.h>
19
20 #include "gcc-compat.h"
21 #include "str.h"
22 #include "log.h"
23 #include "gcc-compat.h"
24 #include "err.h"
25 #include "ipc.h"
26
27 #if (defined(__GNUC__) && defined(__i386__))
28 #define get16bits(d) (*((const uint16_t *) (d)))
29 #else
30 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
31                 +(uint32_t)(((const uint8_t *)(d))[0]) )
32 #endif
33
34 /*
35  * SuperFastHash, by Paul Hsieh.
36  * http://www.azillionmonkeys.com/qed/hash.html
37  */
38 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
39 {
40         uint32_t tmp;
41         int rem = len & 3;
42
43         len >>= 2;
44
45         for (;len > 0; len--) {
46                 hash  += get16bits (data);
47                 tmp    = (get16bits (data+2) << 11) ^ hash;
48                 hash   = (hash << 16) ^ tmp;
49                 data  += 2*sizeof (uint16_t);
50                 hash  += hash >> 11;
51         }
52
53         /* Handle end cases */
54         switch (rem) {
55         case 3:
56                 hash += get16bits (data);
57                 hash ^= hash << 16;
58                 hash ^= data[sizeof (uint16_t)] << 18;
59                 hash += hash >> 11;
60                 break;
61         case 2:
62                 hash += get16bits (data);
63                 hash ^= hash << 11;
64                 hash += hash >> 17;
65                 break;
66         case 1:
67                 hash += *data;
68                 hash ^= hash << 10;
69                 hash += hash >> 1;
70         }
71         /* Force "avalanching" of final 127 bits */
72         hash ^= hash << 3;
73         hash += hash >> 5;
74         hash ^= hash << 4;
75         hash += hash >> 17;
76         hash ^= hash << 25;
77         hash += hash >> 6;
78         return hash;
79 }
80
81 static int get_key_or_die(const char *config_file)
82 {
83         int ret;
84         struct stat statbuf;
85         char *rpath;
86
87         assert(config_file);
88         if (stat(config_file, &statbuf) == 0) {
89                 rpath = realpath(config_file, NULL);
90                 if (!rpath) {
91                         DSS_EMERG_LOG(("could not resolve path %s: %m\n", config_file));
92                         exit(EXIT_FAILURE);
93                 }
94                 DSS_DEBUG_LOG(("resolved path: %s\n", rpath));
95         } else
96                 /*
97                  * This happens if the user did not specify a config file, and
98                  * the default config file does not exist.  Another (unlikely)
99                  * possibility is that the config file was removed between
100                  * startup and this call. We don't care about these corner
101                  * cases too much and just use the unresolved path in this
102                  * case.
103                  */
104                 rpath = dss_strdup(config_file);
105         ret = super_fast_hash((uint8_t *)rpath, strlen(rpath), 0) >> 1;
106         free(rpath);
107         return ret;
108 }
109
110 static int mutex_get(key_t key, int flags)
111 {
112         int ret;
113
114         DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
115         ret = semget(key, 2, flags);
116         if (ret < 0)
117                 return -ERRNO_TO_DSS_ERROR(errno);
118         return ret;
119 }
120
121 static int do_semop(int id, struct sembuf *sops, int num)
122 {
123         int ret;
124
125         DSS_DEBUG_LOG(("calling semop\n"));
126         do {
127                 ret = semop(id, sops, num);
128                 if (ret >= 0)
129                         return 1;
130         } while (errno == EINTR);
131         return -ERRNO_TO_DSS_ERROR(errno);
132 }
133
134 static bool mutex_is_locked(int id)
135 {
136         struct sembuf sops;
137         int ret;
138
139         DSS_DEBUG_LOG(("trying to lock\n"));
140
141         sops.sem_num = 0;
142         sops.sem_op = 0;
143         sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
144
145         ret = do_semop(id, &sops, 1);
146         if (ret < 0)
147                 return true;
148         return false;
149 }
150
151 int lock_dss(char *config_file)
152 {
153         int ret, id;
154         struct sembuf sops[4];
155         key_t key = get_key_or_die(config_file);
156
157         ret = mutex_get(key, IPC_CREAT | 0600);
158         if (ret < 0)
159                 return ret;
160         id = ret;
161
162         sops[0].sem_num = 0;
163         sops[0].sem_op = 0;
164         sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
165
166         sops[1].sem_num = 0;
167         sops[1].sem_op = 1;
168         sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
169
170         sops[2].sem_num = 1;
171         sops[2].sem_op = 0;
172         sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
173
174         sops[3].sem_num = 1;
175         sops[3].sem_op = 1;
176         sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
177
178         return do_semop(id, sops, 4);
179 }
180
181 int get_dss_pid(char *config_file, pid_t *pid)
182 {
183         int ret, semid;
184         key_t key = get_key_or_die(config_file);
185
186         if (pid)
187                 *pid = 0;
188         ret = mutex_get(key, 0);
189         if (ret < 0)
190                 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
191         semid = ret;
192         ret = semctl(semid, 1, GETPID);
193         if (ret < 0)
194                 return -E_NOT_RUNNING;
195         if (pid)
196                 *pid = ret;
197         return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
198 }