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