Merge branch 'refs/heads/t/configtest'
[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 #if (defined(__GNUC__) && defined(__i386__))
26 #define get16bits(d) (*((const uint16_t *) (d)))
27 #else
28 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
29 +(uint32_t)(((const uint8_t *)(d))[0]) )
30 #endif
31
32 /*
33 * SuperFastHash, by Paul Hsieh.
34 * http://www.azillionmonkeys.com/qed/hash.html
35 */
36 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
37 {
38 uint32_t tmp;
39 int rem = len & 3;
40
41 len >>= 2;
42
43 for (;len > 0; len--) {
44 hash += get16bits (data);
45 tmp = (get16bits (data+2) << 11) ^ hash;
46 hash = (hash << 16) ^ tmp;
47 data += 2*sizeof (uint16_t);
48 hash += hash >> 11;
49 }
50
51 /* Handle end cases */
52 switch (rem) {
53 case 3:
54 hash += get16bits (data);
55 hash ^= hash << 16;
56 hash ^= data[sizeof (uint16_t)] << 18;
57 hash += hash >> 11;
58 break;
59 case 2:
60 hash += get16bits (data);
61 hash ^= hash << 11;
62 hash += hash >> 17;
63 break;
64 case 1:
65 hash += *data;
66 hash ^= hash << 10;
67 hash += hash >> 1;
68 }
69 /* Force "avalanching" of final 127 bits */
70 hash ^= hash << 3;
71 hash += hash >> 5;
72 hash ^= hash << 4;
73 hash += hash >> 17;
74 hash ^= hash << 25;
75 hash += hash >> 6;
76 return hash;
77 }
78
79 /*
80 * Return the canonical absolute name of a given file name.
81 *
82 * Slightly modified version of glibc's realpath, Copyright (C)
83 * 1996-2002,2004,2005,2006,2008 Free Software Foundation, Inc.
84 *
85 * A canonical name does not contain any `.', `..' components nor any repeated
86 * path separators ('/') or symlinks. All path components must exist. The
87 * result is malloc'd and must be freed by the caller.
88 */
89 static int dss_realpath(const char *name, char **resolved_path)
90 {
91 char *rpath = NULL, *dest, *extra_buf = NULL;
92 const char *start, *end, *rpath_limit;
93 long int path_max;
94 int ret, num_links = 0;
95
96 if (name[0] == '\0') {
97 /*
98 * As per Single Unix Specification V2 we must return an error
99 * if the name argument points to an empty string.
100 */
101 ret = -ERRNO_TO_DSS_ERROR(ENOENT);
102 goto error;
103 }
104 #ifdef PATH_MAX
105 path_max = PATH_MAX;
106 #else
107 /*
108 * From realpath(3): Asking pathconf(3) does not really help, since on
109 * the one hand POSIX warns that the result of pathconf(3) may be
110 * huge and unsuitable for mallocing memory. And on the other hand
111 * pathconf(3) may return -1 to signify that PATH_MAX is not bounded.
112 */
113 path_max = pathconf(name, _PC_PATH_MAX);
114 if (path_max <= 0 || path_max >= 4096)
115 path_max = 4096;
116 #endif
117 rpath = dss_malloc(path_max);
118 rpath_limit = rpath + path_max;
119
120 if (name[0] != '/') {
121 if (!getcwd(rpath, path_max)) {
122 ret = -ERRNO_TO_DSS_ERROR(errno);
123 goto error;
124 }
125 dest = memchr(rpath, '\0', path_max);
126 } else {
127 rpath[0] = '/';
128 dest = rpath + 1;
129 }
130
131 for (start = end = name; *start; start = end) {
132 struct stat st;
133 int n;
134
135 /* Skip sequence of multiple path-separators. */
136 while (*start == '/')
137 ++start;
138
139 /* Find end of path component. */
140 for (end = start; *end && *end != '/'; ++end)
141 /* Nothing. */ ;
142
143 if (end - start == 0)
144 break;
145 else if (end - start == 1 && start[0] == '.')
146 /* nothing */ ;
147 else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
148 /* Back up to previous component, ignore if at root already. */
149 if (dest > rpath + 1)
150 while ((--dest)[-1] != '/') ;
151 } else {
152 size_t new_size;
153
154 if (dest[-1] != '/')
155 *dest++ = '/';
156
157 if (dest + (end - start) >= rpath_limit) {
158 ptrdiff_t dest_offset = dest - rpath;
159
160 new_size = rpath_limit - rpath;
161 if (end - start + 1 > path_max)
162 new_size += end - start + 1;
163 else
164 new_size += path_max;
165 rpath = dss_realloc(rpath, new_size);
166 rpath_limit = rpath + new_size;
167 dest = rpath + dest_offset;
168 }
169
170 memcpy(dest, start, end - start);
171 dest += end - start;
172 *dest = '\0';
173
174 if (stat(rpath, &st) < 0) {
175 ret = -ERRNO_TO_DSS_ERROR(errno);
176 goto error;
177 }
178
179 if (S_ISLNK(st.st_mode)) {
180 char *buf = alloca(path_max);
181 size_t len;
182
183 if (++num_links > MAXSYMLINKS) {
184 ret = -ERRNO_TO_DSS_ERROR(ELOOP);
185 goto error;
186 }
187
188 n = readlink(rpath, buf, path_max - 1);
189 if (n < 0) {
190 ret = -ERRNO_TO_DSS_ERROR(errno);
191 goto error;
192 }
193 buf[n] = '\0';
194
195 if (!extra_buf)
196 extra_buf = alloca(path_max);
197
198 len = strlen(end);
199 if ((long int) (n + len) >= path_max) {
200 ret = -ERRNO_TO_DSS_ERROR(ENAMETOOLONG);
201 goto error;
202 }
203
204 /* Careful here, end may be a pointer into extra_buf... */
205 memmove(&extra_buf[n], end, len + 1);
206 name = end = memcpy(extra_buf, buf, n);
207
208 if (buf[0] == '/') /* It's an absolute symlink */
209 dest = rpath + 1;
210 else /* Back up to previous component, ignore if at root already: */
211 if (dest > rpath + 1)
212 while ((--dest)[-1] != '/')
213 ; /* nothing */
214 } else if (!S_ISDIR(st.st_mode) && *end != '\0') {
215 ret = -ERRNO_TO_DSS_ERROR(ENOTDIR);
216 goto error;
217 }
218 }
219 }
220 if (dest > rpath + 1 && dest[-1] == '/')
221 --dest;
222 *dest = '\0';
223 *resolved_path = rpath;
224 return 1;
225 error:
226 free(rpath);
227 *resolved_path = NULL;
228 return ret;
229 }
230
231 static int get_key_or_die(const char *config_file)
232 {
233 int ret;
234 struct stat statbuf;
235 char *rpath;
236
237 assert(config_file);
238 if (stat(config_file, &statbuf) == 0) {
239 ret = dss_realpath(config_file, &rpath);
240 if (ret < 0) {
241 DSS_EMERG_LOG(("could not resolve path %s: %s\n", config_file,
242 dss_strerror(-ret)));
243 exit(EXIT_FAILURE);
244 }
245 DSS_DEBUG_LOG(("resolved path: %s\n", rpath));
246 } else
247 /*
248 * This happens if the user did not specify a config file, and
249 * the default config file does not exist. Another (unlikely)
250 * possibility is that the config file was removed between
251 * startup and this call. We don't care about these corner
252 * cases too much and just use the unresolved path in this
253 * case.
254 */
255 rpath = dss_strdup(config_file);
256 ret = super_fast_hash((uint8_t *)rpath, strlen(rpath), 0) >> 1;
257 free(rpath);
258 return ret;
259 }
260
261 static int mutex_get(key_t key, int flags)
262 {
263 int ret;
264
265 DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
266 ret = semget(key, 2, flags);
267 if (ret < 0)
268 return -ERRNO_TO_DSS_ERROR(errno);
269 return ret;
270 }
271
272 static int do_semop(int id, struct sembuf *sops, int num)
273 {
274 int ret;
275
276 DSS_DEBUG_LOG(("calling semop\n"));
277 do {
278 ret = semop(id, sops, num);
279 if (ret >= 0)
280 return 1;
281 } while (errno == EINTR);
282 return -ERRNO_TO_DSS_ERROR(errno);
283 }
284
285 static bool mutex_is_locked(int id)
286 {
287 struct sembuf sops;
288 int ret;
289
290 DSS_DEBUG_LOG(("trying to lock\n"));
291
292 sops.sem_num = 0;
293 sops.sem_op = 0;
294 sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
295
296 ret = do_semop(id, &sops, 1);
297 if (ret < 0)
298 return true;
299 return false;
300 }
301
302 int lock_dss(char *config_file)
303 {
304 int ret, id;
305 struct sembuf sops[4];
306 key_t key = get_key_or_die(config_file);
307
308 ret = mutex_get(key, IPC_CREAT | 0600);
309 if (ret < 0)
310 return ret;
311 id = ret;
312
313 sops[0].sem_num = 0;
314 sops[0].sem_op = 0;
315 sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
316
317 sops[1].sem_num = 0;
318 sops[1].sem_op = 1;
319 sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
320
321 sops[2].sem_num = 1;
322 sops[2].sem_op = 0;
323 sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
324
325 sops[3].sem_num = 1;
326 sops[3].sem_op = 1;
327 sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
328
329 return do_semop(id, sops, 4);
330 }
331
332 int get_dss_pid(char *config_file, pid_t *pid)
333 {
334 int ret, semid;
335 key_t key = get_key_or_die(config_file);
336
337 if (pid)
338 *pid = 0;
339 ret = mutex_get(key, 0);
340 if (ret < 0)
341 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
342 semid = ret;
343 ret = semctl(semid, 1, GETPID);
344 if (ret < 0)
345 return -E_NOT_RUNNING;
346 if (pid)
347 *pid = ret;
348 return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
349 }