1 /* SPDX-License-Identifier: GPL-2.0 */
17 #include <sys/param.h>
20 #include "gcc-compat.h"
23 #include "gcc-compat.h"
27 #if (defined(__GNUC__) && defined(__i386__))
28 #define get16bits(d) (*((const uint16_t *) (d)))
30 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
31 +(uint32_t)(((const uint8_t *)(d))[0]) )
35 * SuperFastHash, by Paul Hsieh.
36 * http://www.azillionmonkeys.com/qed/hash.html
38 static uint32_t super_fast_hash(const uint8_t *data
, uint32_t len
, uint32_t hash
)
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);
53 /* Handle end cases */
56 hash
+= get16bits (data
);
58 hash
^= data
[sizeof (uint16_t)] << 18;
62 hash
+= get16bits (data
);
71 /* Force "avalanching" of final 127 bits */
82 * Return the canonical absolute name of a given file name.
84 * Slightly modified version of glibc's realpath, Copyright (C)
85 * 1996-2002,2004,2005,2006,2008 Free Software Foundation, Inc.
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.
91 static int dss_realpath(const char *name
, char **resolved_path
)
93 char *rpath
= NULL
, *dest
, *extra_buf
= NULL
;
94 const char *start
, *end
, *rpath_limit
;
96 int ret
, num_links
= 0;
98 if (name
[0] == '\0') {
100 * As per Single Unix Specification V2 we must return an error
101 * if the name argument points to an empty string.
103 ret
= -ERRNO_TO_DSS_ERROR(ENOENT
);
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.
115 path_max
= pathconf(name
, _PC_PATH_MAX
);
116 if (path_max
<= 0 || path_max
>= 4096)
119 rpath
= dss_malloc(path_max
);
120 rpath_limit
= rpath
+ path_max
;
122 if (name
[0] != '/') {
123 if (!getcwd(rpath
, path_max
)) {
124 ret
= -ERRNO_TO_DSS_ERROR(errno
);
127 dest
= memchr(rpath
, '\0', path_max
);
133 for (start
= end
= name
; *start
; start
= end
) {
137 /* Skip sequence of multiple path-separators. */
138 while (*start
== '/')
141 /* Find end of path component. */
142 for (end
= start
; *end
&& *end
!= '/'; ++end
)
145 if (end
- start
== 0)
147 else if (end
- start
== 1 && start
[0] == '.')
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] != '/') ;
159 if (dest
+ (end
- start
) >= rpath_limit
) {
160 ptrdiff_t dest_offset
= dest
- rpath
;
162 new_size
= rpath_limit
- rpath
;
163 if (end
- start
+ 1 > path_max
)
164 new_size
+= end
- start
+ 1;
166 new_size
+= path_max
;
167 rpath
= dss_realloc(rpath
, new_size
);
168 rpath_limit
= rpath
+ new_size
;
169 dest
= rpath
+ dest_offset
;
172 memcpy(dest
, start
, end
- start
);
176 if (stat(rpath
, &st
) < 0) {
177 ret
= -ERRNO_TO_DSS_ERROR(errno
);
181 if (S_ISLNK(st
.st_mode
)) {
182 char *buf
= alloca(path_max
);
185 if (++num_links
> MAXSYMLINKS
) {
186 ret
= -ERRNO_TO_DSS_ERROR(ELOOP
);
190 n
= readlink(rpath
, buf
, path_max
- 1);
192 ret
= -ERRNO_TO_DSS_ERROR(errno
);
198 extra_buf
= alloca(path_max
);
201 if ((long int) (n
+ len
) >= path_max
) {
202 ret
= -ERRNO_TO_DSS_ERROR(ENAMETOOLONG
);
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
);
210 if (buf
[0] == '/') /* It's an absolute symlink */
212 else /* Back up to previous component, ignore if at root already: */
213 if (dest
> rpath
+ 1)
214 while ((--dest
)[-1] != '/')
216 } else if (!S_ISDIR(st
.st_mode
) && *end
!= '\0') {
217 ret
= -ERRNO_TO_DSS_ERROR(ENOTDIR
);
222 if (dest
> rpath
+ 1 && dest
[-1] == '/')
225 *resolved_path
= rpath
;
229 *resolved_path
= NULL
;
233 static int get_key_or_die(const char *config_file
)
240 if (stat(config_file
, &statbuf
) == 0) {
241 ret
= dss_realpath(config_file
, &rpath
);
243 DSS_EMERG_LOG(("could not resolve path %s: %s\n", config_file
,
244 dss_strerror(-ret
)));
247 DSS_DEBUG_LOG(("resolved path: %s\n", rpath
));
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
257 rpath
= dss_strdup(config_file
);
258 ret
= super_fast_hash((uint8_t *)rpath
, strlen(rpath
), 0) >> 1;
263 static int mutex_get(key_t key
, int flags
)
267 DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key
));
268 ret
= semget(key
, 2, flags
);
270 return -ERRNO_TO_DSS_ERROR(errno
);
274 static int do_semop(int id
, struct sembuf
*sops
, int num
)
278 DSS_DEBUG_LOG(("calling semop\n"));
280 ret
= semop(id
, sops
, num
);
283 } while (errno
== EINTR
);
284 return -ERRNO_TO_DSS_ERROR(errno
);
287 static bool mutex_is_locked(int id
)
292 DSS_DEBUG_LOG(("trying to lock\n"));
296 sops
.sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
298 ret
= do_semop(id
, &sops
, 1);
304 int lock_dss(char *config_file
)
307 struct sembuf sops
[4];
308 key_t key
= get_key_or_die(config_file
);
310 ret
= mutex_get(key
, IPC_CREAT
| 0600);
317 sops
[0].sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
321 sops
[1].sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
325 sops
[2].sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
329 sops
[3].sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
331 return do_semop(id
, sops
, 4);
334 int get_dss_pid(char *config_file
, pid_t
*pid
)
337 key_t key
= get_key_or_die(config_file
);
341 ret
= mutex_get(key
, 0);
343 return ret
== -ERRNO_TO_DSS_ERROR(ENOENT
)? -E_NOT_RUNNING
: ret
;
345 ret
= semctl(semid
, 1, GETPID
);
347 return -E_NOT_RUNNING
;
350 return mutex_is_locked(semid
)? 1 : -E_NOT_RUNNING
;