15 #include <sys/param.h>
17 #include "gcc-compat.h"
20 #include "gcc-compat.h"
23 #if (defined(__GNUC__) && defined(__i386__))
24 #define get16bits(d) (*((const uint16_t *) (d)))
26 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
27 +(uint32_t)(((const uint8_t *)(d))[0]) )
31 * SuperFastHash, by Paul Hsieh.
32 * http://www.azillionmonkeys.com/qed/hash.html
34 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
41 for (;len > 0; len--) {
42 hash += get16bits (data);
43 tmp = (get16bits (data+2) << 11) ^ hash;
44 hash = (hash << 16) ^ tmp;
45 data += 2*sizeof (uint16_t);
49 /* Handle end cases */
52 hash += get16bits (data);
54 hash ^= data[sizeof (uint16_t)] << 18;
58 hash += get16bits (data);
67 /* Force "avalanching" of final 127 bits */
78 * Return the canonical absolute name of a given file name.
80 * Slightly modified version of glibc's realpath, Copyright (C)
81 * 1996-2002,2004,2005,2006,2008 Free Software Foundation, Inc.
83 * A canonical name does not contain any `.', `..' components nor any repeated
84 * path separators ('/') or symlinks. All path components must exist. The
85 * result is malloc'd and must be freed by the caller.
87 static int dss_realpath(const char *name, char **resolved_path)
89 char *rpath = NULL, *dest, *extra_buf = NULL;
90 const char *start, *end, *rpath_limit;
92 int ret, num_links = 0;
94 if (name[0] == '\0') {
96 * As per Single Unix Specification V2 we must return an error
97 * if the name argument points to an empty string.
99 ret = -ERRNO_TO_DSS_ERROR(ENOENT);
106 * From realpath(3): Asking pathconf(3) does not really help, since on
107 * the one hand POSIX warns that the result of pathconf(3) may be
108 * huge and unsuitable for mallocing memory. And on the other hand
109 * pathconf(3) may return -1 to signify that PATH_MAX is not bounded.
111 path_max = pathconf(name, _PC_PATH_MAX);
112 if (path_max <= 0 || path_max >= 4096)
115 rpath = dss_malloc(path_max);
116 rpath_limit = rpath + path_max;
118 if (name[0] != '/') {
119 if (!getcwd(rpath, path_max)) {
120 ret = -ERRNO_TO_DSS_ERROR(errno);
123 dest = memchr(rpath, '\0', path_max);
129 for (start = end = name; *start; start = end) {
133 /* Skip sequence of multiple path-separators. */
134 while (*start == '/')
137 /* Find end of path component. */
138 for (end = start; *end && *end != '/'; ++end)
141 if (end - start == 0)
143 else if (end - start == 1 && start[0] == '.')
145 else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
146 /* Back up to previous component, ignore if at root already. */
147 if (dest > rpath + 1)
148 while ((--dest)[-1] != '/') ;
155 if (dest + (end - start) >= rpath_limit) {
156 ptrdiff_t dest_offset = dest - rpath;
158 new_size = rpath_limit - rpath;
159 if (end - start + 1 > path_max)
160 new_size += end - start + 1;
162 new_size += path_max;
163 rpath = dss_realloc(rpath, new_size);
164 rpath_limit = rpath + new_size;
165 dest = rpath + dest_offset;
168 memcpy(dest, start, end - start);
172 if (stat(rpath, &st) < 0) {
173 ret = -ERRNO_TO_DSS_ERROR(errno);
177 if (S_ISLNK(st.st_mode)) {
178 char *buf = alloca(path_max);
181 if (++num_links > MAXSYMLINKS) {
182 ret = -ERRNO_TO_DSS_ERROR(ELOOP);
186 n = readlink(rpath, buf, path_max - 1);
188 ret = -ERRNO_TO_DSS_ERROR(errno);
194 extra_buf = alloca(path_max);
197 if ((long int) (n + len) >= path_max) {
198 ret = -ERRNO_TO_DSS_ERROR(ENAMETOOLONG);
202 /* Careful here, end may be a pointer into extra_buf... */
203 memmove(&extra_buf[n], end, len + 1);
204 name = end = memcpy(extra_buf, buf, n);
206 if (buf[0] == '/') /* It's an absolute symlink */
208 else /* Back up to previous component, ignore if at root already: */
209 if (dest > rpath + 1)
210 while ((--dest)[-1] != '/')
212 } else if (!S_ISDIR(st.st_mode) && *end != '\0') {
213 ret = -ERRNO_TO_DSS_ERROR(ENOTDIR);
218 if (dest > rpath + 1 && dest[-1] == '/')
221 *resolved_path = rpath;
225 *resolved_path = NULL;
229 static inline int get_key_or_die(char *config_file)
236 if (stat(config_file, &statbuf) == 0) {
237 ret = dss_realpath(config_file, &rpath);
239 DSS_EMERG_LOG("could not resolve path %s: %s\n", config_file,
243 DSS_DEBUG_LOG("resolved path: %s\n", rpath);
246 * This happens if the user did not specify a config file, and
247 * the default config file does not exist. Another (unlikely)
248 * possibility is that the config file was removed between
249 * startup and this call. We don't care about these corner
250 * cases too much and just use the unresolved path in this
253 rpath = dss_strdup(config_file);
254 ret = super_fast_hash((uint8_t *)rpath, strlen(rpath), 0) >> 1;
259 static int mutex_get(key_t key, int flags)
263 DSS_DEBUG_LOG("getting semaphore 0x%x\n", key);
264 ret = semget(key, 2, flags);
266 return -ERRNO_TO_DSS_ERROR(errno);
270 static int do_semop(int id, struct sembuf *sops, int num)
274 DSS_DEBUG_LOG("calling semop\n");
276 ret = semop(id, sops, num);
279 } while (errno == EINTR);
280 return -ERRNO_TO_DSS_ERROR(errno);
283 static int mutex_lock(int id)
287 DSS_DEBUG_LOG("locking\n");
288 struct sembuf sops[4] = {
292 .sem_flg = SEM_UNDO | IPC_NOWAIT
297 .sem_flg = SEM_UNDO | IPC_NOWAIT
302 .sem_flg = SEM_UNDO | IPC_NOWAIT
307 .sem_flg = SEM_UNDO | IPC_NOWAIT
310 ret = do_semop(id, sops, 4);
312 return -ERRNO_TO_DSS_ERROR(errno);
316 static int mutex_try_lock(int id)
320 DSS_DEBUG_LOG("trying to lock\n");
321 struct sembuf sops[2] = {
325 .sem_flg = SEM_UNDO | IPC_NOWAIT
330 .sem_flg = SEM_UNDO | IPC_NOWAIT
333 ret = do_semop(id, sops, 2);
335 return -ERRNO_TO_DSS_ERROR(errno);
339 int lock_dss(char *config_file)
341 int ret, key = get_key_or_die(config_file);
343 ret = mutex_get(key, IPC_CREAT | 0600);
346 return mutex_lock(ret);
349 int get_dss_pid(char *config_file, pid_t *pid)
351 int ret, semid, key = get_key_or_die(config_file);
353 ret = mutex_get(key, 0);
357 ret = semctl(semid, 1, GETPID);
359 return -E_NOT_RUNNING;
361 ret = mutex_try_lock(semid);
363 return -E_NOT_RUNNING;