15 #include <sys/param.h>
18 #include "gcc-compat.h"
21 #include "gcc-compat.h"
25 #if (defined(__GNUC__) && defined(__i386__))
26 #define get16bits(d) (*((const uint16_t *) (d)))
28 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
29 +(uint32_t)(((const uint8_t *)(d))[0]) )
33 * SuperFastHash, by Paul Hsieh.
34 * http://www.azillionmonkeys.com/qed/hash.html
36 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
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);
51 /* Handle end cases */
54 hash += get16bits (data);
56 hash ^= data[sizeof (uint16_t)] << 18;
60 hash += get16bits (data);
69 /* Force "avalanching" of final 127 bits */
80 * Return the canonical absolute name of a given file name.
82 * Slightly modified version of glibc's realpath, Copyright (C)
83 * 1996-2002,2004,2005,2006,2008 Free Software Foundation, Inc.
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.
89 static int dss_realpath(const char *name, char **resolved_path)
91 char *rpath = NULL, *dest, *extra_buf = NULL;
92 const char *start, *end, *rpath_limit;
94 int ret, num_links = 0;
96 if (name[0] == '\0') {
98 * As per Single Unix Specification V2 we must return an error
99 * if the name argument points to an empty string.
101 ret = -ERRNO_TO_DSS_ERROR(ENOENT);
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.
113 path_max = pathconf(name, _PC_PATH_MAX);
114 if (path_max <= 0 || path_max >= 4096)
117 rpath = dss_malloc(path_max);
118 rpath_limit = rpath + path_max;
120 if (name[0] != '/') {
121 if (!getcwd(rpath, path_max)) {
122 ret = -ERRNO_TO_DSS_ERROR(errno);
125 dest = memchr(rpath, '\0', path_max);
131 for (start = end = name; *start; start = end) {
135 /* Skip sequence of multiple path-separators. */
136 while (*start == '/')
139 /* Find end of path component. */
140 for (end = start; *end && *end != '/'; ++end)
143 if (end - start == 0)
145 else if (end - start == 1 && start[0] == '.')
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] != '/') ;
157 if (dest + (end - start) >= rpath_limit) {
158 ptrdiff_t dest_offset = dest - rpath;
160 new_size = rpath_limit - rpath;
161 if (end - start + 1 > path_max)
162 new_size += end - start + 1;
164 new_size += path_max;
165 rpath = dss_realloc(rpath, new_size);
166 rpath_limit = rpath + new_size;
167 dest = rpath + dest_offset;
170 memcpy(dest, start, end - start);
174 if (stat(rpath, &st) < 0) {
175 ret = -ERRNO_TO_DSS_ERROR(errno);
179 if (S_ISLNK(st.st_mode)) {
180 char *buf = alloca(path_max);
183 if (++num_links > MAXSYMLINKS) {
184 ret = -ERRNO_TO_DSS_ERROR(ELOOP);
188 n = readlink(rpath, buf, path_max - 1);
190 ret = -ERRNO_TO_DSS_ERROR(errno);
196 extra_buf = alloca(path_max);
199 if ((long int) (n + len) >= path_max) {
200 ret = -ERRNO_TO_DSS_ERROR(ENAMETOOLONG);
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);
208 if (buf[0] == '/') /* It's an absolute symlink */
210 else /* Back up to previous component, ignore if at root already: */
211 if (dest > rpath + 1)
212 while ((--dest)[-1] != '/')
214 } else if (!S_ISDIR(st.st_mode) && *end != '\0') {
215 ret = -ERRNO_TO_DSS_ERROR(ENOTDIR);
220 if (dest > rpath + 1 && dest[-1] == '/')
223 *resolved_path = rpath;
227 *resolved_path = NULL;
231 static int get_key_or_die(const char *config_file)
238 if (stat(config_file, &statbuf) == 0) {
239 ret = dss_realpath(config_file, &rpath);
241 DSS_EMERG_LOG(("could not resolve path %s: %s\n", config_file,
242 dss_strerror(-ret)));
245 DSS_DEBUG_LOG(("resolved path: %s\n", rpath));
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
255 rpath = dss_strdup(config_file);
256 ret = super_fast_hash((uint8_t *)rpath, strlen(rpath), 0) >> 1;
261 static int mutex_get(key_t key, int flags)
265 DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
266 ret = semget(key, 2, flags);
268 return -ERRNO_TO_DSS_ERROR(errno);
272 static int do_semop(int id, struct sembuf *sops, int num)
276 DSS_DEBUG_LOG(("calling semop\n"));
278 ret = semop(id, sops, num);
281 } while (errno == EINTR);
282 return -ERRNO_TO_DSS_ERROR(errno);
285 static bool mutex_is_locked(int id)
290 DSS_DEBUG_LOG(("trying to lock\n"));
294 sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
296 ret = do_semop(id, &sops, 1);
302 int lock_dss(char *config_file)
305 struct sembuf sops[4];
306 key_t key = get_key_or_die(config_file);
308 ret = mutex_get(key, IPC_CREAT | 0600);
315 sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
319 sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
323 sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
327 sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
329 return do_semop(id, sops, 4);
332 int get_dss_pid(char *config_file, pid_t *pid)
335 key_t key = get_key_or_die(config_file);
339 ret = mutex_get(key, 0);
341 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
343 ret = semctl(semid, 1, GETPID);
345 return -E_NOT_RUNNING;
348 return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;