15 #include <sys/param.h>
17 #include "gcc-compat.h"
20 #include "gcc-compat.h"
24 #if (defined(__GNUC__) && defined(__i386__))
25 #define get16bits(d) (*((const uint16_t *) (d)))
27 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
28 +(uint32_t)(((const uint8_t *)(d))[0]) )
32 * SuperFastHash, by Paul Hsieh.
33 * http://www.azillionmonkeys.com/qed/hash.html
35 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
42 for (;len > 0; len--) {
43 hash += get16bits (data);
44 tmp = (get16bits (data+2) << 11) ^ hash;
45 hash = (hash << 16) ^ tmp;
46 data += 2*sizeof (uint16_t);
50 /* Handle end cases */
53 hash += get16bits (data);
55 hash ^= data[sizeof (uint16_t)] << 18;
59 hash += get16bits (data);
68 /* Force "avalanching" of final 127 bits */
79 * Return the canonical absolute name of a given file name.
81 * Slightly modified version of glibc's realpath, Copyright (C)
82 * 1996-2002,2004,2005,2006,2008 Free Software Foundation, Inc.
84 * A canonical name does not contain any `.', `..' components nor any repeated
85 * path separators ('/') or symlinks. All path components must exist. The
86 * result is malloc'd and must be freed by the caller.
88 static int dss_realpath(const char *name, char **resolved_path)
90 char *rpath = NULL, *dest, *extra_buf = NULL;
91 const char *start, *end, *rpath_limit;
93 int ret, num_links = 0;
95 if (name[0] == '\0') {
97 * As per Single Unix Specification V2 we must return an error
98 * if the name argument points to an empty string.
100 ret = -ERRNO_TO_DSS_ERROR(ENOENT);
107 * From realpath(3): Asking pathconf(3) does not really help, since on
108 * the one hand POSIX warns that the result of pathconf(3) may be
109 * huge and unsuitable for mallocing memory. And on the other hand
110 * pathconf(3) may return -1 to signify that PATH_MAX is not bounded.
112 path_max = pathconf(name, _PC_PATH_MAX);
113 if (path_max <= 0 || path_max >= 4096)
116 rpath = dss_malloc(path_max);
117 rpath_limit = rpath + path_max;
119 if (name[0] != '/') {
120 if (!getcwd(rpath, path_max)) {
121 ret = -ERRNO_TO_DSS_ERROR(errno);
124 dest = memchr(rpath, '\0', path_max);
130 for (start = end = name; *start; start = end) {
134 /* Skip sequence of multiple path-separators. */
135 while (*start == '/')
138 /* Find end of path component. */
139 for (end = start; *end && *end != '/'; ++end)
142 if (end - start == 0)
144 else if (end - start == 1 && start[0] == '.')
146 else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
147 /* Back up to previous component, ignore if at root already. */
148 if (dest > rpath + 1)
149 while ((--dest)[-1] != '/') ;
156 if (dest + (end - start) >= rpath_limit) {
157 ptrdiff_t dest_offset = dest - rpath;
159 new_size = rpath_limit - rpath;
160 if (end - start + 1 > path_max)
161 new_size += end - start + 1;
163 new_size += path_max;
164 rpath = dss_realloc(rpath, new_size);
165 rpath_limit = rpath + new_size;
166 dest = rpath + dest_offset;
169 memcpy(dest, start, end - start);
173 if (stat(rpath, &st) < 0) {
174 ret = -ERRNO_TO_DSS_ERROR(errno);
178 if (S_ISLNK(st.st_mode)) {
179 char *buf = alloca(path_max);
182 if (++num_links > MAXSYMLINKS) {
183 ret = -ERRNO_TO_DSS_ERROR(ELOOP);
187 n = readlink(rpath, buf, path_max - 1);
189 ret = -ERRNO_TO_DSS_ERROR(errno);
195 extra_buf = alloca(path_max);
198 if ((long int) (n + len) >= path_max) {
199 ret = -ERRNO_TO_DSS_ERROR(ENAMETOOLONG);
203 /* Careful here, end may be a pointer into extra_buf... */
204 memmove(&extra_buf[n], end, len + 1);
205 name = end = memcpy(extra_buf, buf, n);
207 if (buf[0] == '/') /* It's an absolute symlink */
209 else /* Back up to previous component, ignore if at root already: */
210 if (dest > rpath + 1)
211 while ((--dest)[-1] != '/')
213 } else if (!S_ISDIR(st.st_mode) && *end != '\0') {
214 ret = -ERRNO_TO_DSS_ERROR(ENOTDIR);
219 if (dest > rpath + 1 && dest[-1] == '/')
222 *resolved_path = rpath;
226 *resolved_path = NULL;
230 static inline int get_key_or_die(char *config_file)
237 if (stat(config_file, &statbuf) == 0) {
238 ret = dss_realpath(config_file, &rpath);
240 DSS_EMERG_LOG(("could not resolve path %s: %s\n", config_file,
241 dss_strerror(-ret)));
244 DSS_DEBUG_LOG(("resolved path: %s\n", rpath));
247 * This happens if the user did not specify a config file, and
248 * the default config file does not exist. Another (unlikely)
249 * possibility is that the config file was removed between
250 * startup and this call. We don't care about these corner
251 * cases too much and just use the unresolved path in this
254 rpath = dss_strdup(config_file);
255 ret = super_fast_hash((uint8_t *)rpath, strlen(rpath), 0) >> 1;
260 static int mutex_get(int key, int flags)
264 DSS_DEBUG_LOG(("getting semaphore 0x%x\n", key));
265 ret = semget(key, 2, flags);
267 return -ERRNO_TO_DSS_ERROR(errno);
271 static int do_semop(int id, struct sembuf *sops, int num)
275 DSS_DEBUG_LOG(("calling semop\n"));
277 ret = semop(id, sops, num);
280 } while (errno == EINTR);
281 return -ERRNO_TO_DSS_ERROR(errno);
284 static int mutex_lock(int id)
286 struct sembuf sops[4];
289 DSS_DEBUG_LOG(("locking\n"));
293 sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
297 sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
301 sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
305 sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
307 ret = do_semop(id, sops, 4);
309 return -ERRNO_TO_DSS_ERROR(errno);
313 static int mutex_try_lock(int id)
315 struct sembuf sops[2];
318 DSS_DEBUG_LOG(("trying to lock\n"));
322 sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
326 sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
328 ret = do_semop(id, sops, 2);
330 return -ERRNO_TO_DSS_ERROR(errno);
334 int lock_dss(char *config_file)
336 int ret, key = get_key_or_die(config_file);
338 ret = mutex_get(key, IPC_CREAT | 0600);
341 return mutex_lock(ret);
344 int get_dss_pid(char *config_file, pid_t *pid)
346 int ret, semid, key = get_key_or_die(config_file);
350 ret = mutex_get(key, 0);
354 ret = semctl(semid, 1, GETPID);
356 return -E_NOT_RUNNING;
359 ret = mutex_try_lock(semid);
361 return -E_NOT_RUNNING;