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