]> git.tuebingen.mpg.de Git - dss.git/blob - ipc.c
0f295a9d2e6fc34b600d38594d5bd219afd08ccf
[dss.git] / ipc.c
1 #include <sys/wait.h>
2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/ipc.h>
9 #include <sys/sem.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <sys/stat.h>
13 #include <stddef.h>
14 #include <limits.h>
15 #include <sys/param.h>
16 #include <stdbool.h>
17
18 #include "gcc-compat.h"
19 #include "str.h"
20 #include "log.h"
21 #include "gcc-compat.h"
22 #include "err.h"
23 #include "ipc.h"
24
25 static key_t get_key(const char *config_file)
26 {
27         key_t ret;
28
29         assert(config_file);
30         ret = ftok(config_file, 'D');
31         if (ret >= 0)
32                 return ret;
33         /*
34          * This happens if the user did not specify a config file, and the
35          * default config file does not exist.  Another (unlikely) possibility
36          * is that the config file was removed between startup and this call.
37          * We don't care about these corner cases too much and just return a
38          * fixed key in this case.
39          */
40         return 0x0D55; /* no magic, this number just looks a bit like DSS */
41 }
42
43 static int mutex_get(key_t key, int flags)
44 {
45         int ret;
46
47         DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
48         ret = semget(key, 2, flags);
49         if (ret < 0)
50                 return -ERRNO_TO_DSS_ERROR(errno);
51         return ret;
52 }
53
54 static int do_semop(int id, struct sembuf *sops, int num)
55 {
56         int ret;
57
58         DSS_DEBUG_LOG(("calling semop\n"));
59         do {
60                 ret = semop(id, sops, num);
61                 if (ret >= 0)
62                         return 1;
63         } while (errno == EINTR);
64         return -ERRNO_TO_DSS_ERROR(errno);
65 }
66
67 static bool mutex_is_locked(int id)
68 {
69         struct sembuf sops;
70         int ret;
71
72         DSS_DEBUG_LOG(("trying to lock\n"));
73
74         sops.sem_num = 0;
75         sops.sem_op = 0;
76         sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
77
78         ret = do_semop(id, &sops, 1);
79         if (ret < 0)
80                 return true;
81         return false;
82 }
83
84 int lock_dss(char *config_file)
85 {
86         int ret, id;
87         struct sembuf sops[4];
88         key_t key = get_key(config_file);
89
90         ret = mutex_get(key, IPC_CREAT | 0600);
91         if (ret < 0)
92                 return ret;
93         id = ret;
94
95         sops[0].sem_num = 0;
96         sops[0].sem_op = 0;
97         sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
98
99         sops[1].sem_num = 0;
100         sops[1].sem_op = 1;
101         sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
102
103         sops[2].sem_num = 1;
104         sops[2].sem_op = 0;
105         sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
106
107         sops[3].sem_num = 1;
108         sops[3].sem_op = 1;
109         sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
110
111         return do_semop(id, sops, 4);
112 }
113
114 int get_dss_pid(char *config_file, pid_t *pid)
115 {
116         int ret, semid;
117         key_t key = get_key(config_file);
118
119         if (pid)
120                 *pid = 0;
121         ret = mutex_get(key, 0);
122         if (ret < 0)
123                 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
124         semid = ret;
125         ret = semctl(semid, 1, GETPID);
126         if (ret < 0)
127                 return -E_NOT_RUNNING;
128         if (pid)
129                 *pid = ret;
130         return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
131 }