]> git.tuebingen.mpg.de Git - dss.git/blob - ipc.c
Merge topic branch t/realpath into pu
[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  * https://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 static int get_key_or_die(const char *config_file)
82 {
83         assert(config_file);
84         return super_fast_hash((uint8_t *)config_file, strlen(config_file), 0) >> 1;
85 }
86
87 static int mutex_get(key_t key, int flags)
88 {
89         int ret;
90
91         DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
92         ret = semget(key, 2, flags);
93         if (ret < 0)
94                 return -ERRNO_TO_DSS_ERROR(errno);
95         return ret;
96 }
97
98 static int do_semop(int id, struct sembuf *sops, int num)
99 {
100         int ret;
101
102         DSS_DEBUG_LOG(("calling semop\n"));
103         do {
104                 ret = semop(id, sops, num);
105                 if (ret >= 0)
106                         return 1;
107         } while (errno == EINTR);
108         return -ERRNO_TO_DSS_ERROR(errno);
109 }
110
111 static bool mutex_is_locked(int id)
112 {
113         struct sembuf sops;
114         int ret;
115
116         DSS_DEBUG_LOG(("trying to lock\n"));
117
118         sops.sem_num = 0;
119         sops.sem_op = 0;
120         sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
121
122         ret = do_semop(id, &sops, 1);
123         if (ret < 0)
124                 return true;
125         return false;
126 }
127
128 int lock_dss(char *config_file)
129 {
130         int ret, id;
131         struct sembuf sops[4];
132         key_t key = get_key_or_die(config_file);
133
134         ret = mutex_get(key, IPC_CREAT | 0600);
135         if (ret < 0)
136                 return ret;
137         id = ret;
138
139         sops[0].sem_num = 0;
140         sops[0].sem_op = 0;
141         sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
142
143         sops[1].sem_num = 0;
144         sops[1].sem_op = 1;
145         sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
146
147         sops[2].sem_num = 1;
148         sops[2].sem_op = 0;
149         sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
150
151         sops[3].sem_num = 1;
152         sops[3].sem_op = 1;
153         sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
154
155         return do_semop(id, sops, 4);
156 }
157
158 int get_dss_pid(char *config_file, pid_t *pid)
159 {
160         int ret, semid;
161         key_t key = get_key_or_die(config_file);
162
163         if (pid)
164                 *pid = 0;
165         ret = mutex_get(key, 0);
166         if (ret < 0)
167                 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
168         semid = ret;
169         ret = semctl(semid, 1, GETPID);
170         if (ret < 0)
171                 return -E_NOT_RUNNING;
172         if (pid)
173                 *pid = ret;
174         return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
175 }