Avoid warning about sys/sysctl.h on glibc-2.30.
[paraslash.git] / ipc.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file ipc.c Inter-process communication and shared memory helpers. */
4
5 #include "para.h"
6 #include "error.h"
7 #include "ipc.h"
8 #include <sys/types.h>
9 #include <sys/param.h>
10
11 #include <sys/ipc.h>
12 #include <sys/shm.h>
13 #include <sys/sem.h>
14
15 /**
16 * Define a new mutex.
17 *
18 * \return The identifier for the new mutex on success, a negative error code
19 * on errors.
20 *
21 * \sa semget(2).
22 */
23 int mutex_new(void)
24 {
25 int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
26 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
27 }
28
29 /**
30 * Destroy a mutex.
31 *
32 * \param id The identifier of the mutex to be destroyed.
33 *
34 * \return Standard.
35 *
36 * \sa semctl(2)
37 */
38 int mutex_destroy(int id)
39 {
40 int ret = semctl(id, 0, IPC_RMID);
41 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
42 }
43
44 static void para_semop(int id, struct sembuf *sops, int num)
45 {
46 do {
47 if (semop(id, sops, num) >= 0)
48 return;
49 } while (errno == EINTR);
50 if (errno == EIDRM) {
51 PARA_CRIT_LOG("semaphore set %d was removed\n", id);
52 return;
53 }
54 PARA_EMERG_LOG("fatal semop error %s: pid %d\n", strerror(errno),
55 (int)getpid());
56 exit(EXIT_FAILURE);
57 }
58
59 /**
60 * Lock the given mutex.
61 *
62 * \param id The identifier of the shared memory area to lock.
63 *
64 * This function either succeeds or aborts.
65 *
66 * \sa semop(2), struct \ref misc_meta_data.
67 */
68 void mutex_lock(int id)
69 {
70 struct sembuf sops[2] = {
71 {
72 .sem_num = 0,
73 .sem_op = 0,
74 .sem_flg = SEM_UNDO
75 },
76 {
77 .sem_num = 0,
78 .sem_op = 1,
79 .sem_flg = SEM_UNDO
80 }
81 };
82 para_semop(id, sops, 2);
83 }
84
85 /**
86 * Unlock a mutex.
87 *
88 * \param id The identifier of the mutex.
89 *
90 * This function either succeeds or aborts.
91 *
92 * \sa semop(2), struct \ref misc_meta_data.
93 */
94 void mutex_unlock(int id)
95 {
96 struct sembuf sops[1] = {
97 {
98 .sem_num = 0,
99 .sem_op = -1,
100 .sem_flg = SEM_UNDO
101 },
102 };
103 para_semop(id, sops, 1);
104 }
105
106 /**
107 * Create a new shared memory area of given size.
108 *
109 * \param size The size of the shared memory area to create.
110 *
111 * \return The id of the shared memory array on success, a negative error
112 * code on errors.
113 *
114 * \sa shmget(2).
115 */
116 int shm_new(size_t size)
117 {
118 int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
119 return ret < 0 ? -ERRNO_TO_PARA_ERROR(errno) : ret;
120 }
121
122 /**
123 * Destroy the given shared memory area.
124 *
125 * \param id The shared memory identifier.
126 *
127 * \return The return value of the underlying shmctl() call on success,
128 * a negative error code on errors.
129 *
130 * \sa shmctl(2).
131 */
132 int shm_destroy(int id)
133 {
134 struct shmid_ds shm_desc;
135 int ret = shmctl(id, IPC_RMID, &shm_desc);
136 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
137 }
138
139 /**
140 * Attach a shared memory segment.
141 *
142 * \param id The identifier of the shared memory segment to attach.
143 * \param mode Either ATTACH_RO (read only) or ATTACH_RW (read/write).
144 * \param result Points to the attached area just attached on success.
145 *
146 * \return Standard.
147 *
148 * \sa shmat(2).
149 */
150 int shm_attach(int id, enum shm_attach_mode mode, void **result)
151 {
152 if (mode == ATTACH_RW)
153 *result = shmat(id, NULL, 0);
154 else
155 *result = shmat(id, NULL, SHM_RDONLY);
156 return *result == (void *) -1? -ERRNO_TO_PARA_ERROR(errno) : 1;
157 }
158
159 /**
160 * Get the size of a shared memory segment.
161 *
162 * \param id The shared memory segment identifier.
163 * \param result Size in bytes is returned here, zero on errors.
164 *
165 * \return Standard.
166 *
167 * \sa shmctl(2).
168 */
169 int shm_size(int id, size_t *result)
170 {
171 struct shmid_ds ds; /* data structure */
172
173 *result = 0;
174 if (shmctl(id, IPC_STAT, &ds) < 0)
175 return -ERRNO_TO_PARA_ERROR(errno);
176 *result = ds.shm_segsz;
177 return 1;
178 }
179
180 /**
181 * Detach a shared memory segment.
182 *
183 * \param addr The address of the attached segment.
184 *
185 * \return Standard.
186 *
187 * \sa shmdt(2).
188 */
189 int shm_detach(void *addr)
190 {
191 int ret = shmdt(addr);
192 return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : 1;
193 }
194
195 # if defined __FreeBSD__ || defined __NetBSD__
196 #include <sys/sysctl.h>
197 # define SYSCTL_SHMMAX_VARIABLE "kern.ipc.shmmax"
198 # else
199 # undef SYSCTL_SHMMAX_VARIABLE
200 # endif
201
202 /**
203 * Get the maximal size of a shared memory area.
204 *
205 * The value is only computed once when the function is called for the first
206 * time. Subsequent calls return the number which was computed during the
207 * first call.
208 *
209 * \return A number suitable as an argument to \ref shm_new().
210 */
211 size_t shm_get_shmmax(void)
212 {
213 static size_t shmmax;
214
215 if (shmmax > 0) /* only dance once */
216 return shmmax;
217 #ifdef __linux__ /* get it from proc fs */
218 {
219 int fd = open("/proc/sys/kernel/shmmax", O_RDONLY);
220 if (fd >= 0) {
221 char buf[100] = "";
222 int ret = read(fd, buf, sizeof(buf) - 1);
223 if (ret > 0) {
224 buf[ret] = '\0';
225 shmmax = strtoul(buf, NULL, 10);
226 }
227 close(fd);
228 }
229 }
230 #elif defined SYSCTL_SHMMAX_VARIABLE
231 {
232 size_t len = sizeof(shmmax);
233 sysctlbyname(SYSCTL_SHMMAX_VARIABLE, &shmmax, &len, NULL, 0);
234 }
235 #elif defined SHMMAX
236 shmmax = SHMMAX;
237 #endif
238 if (shmmax == 0) {
239 PARA_WARNING_LOG("unable to determine shmmax\n");
240 shmmax = 65535; /* last resort */
241 }
242 PARA_INFO_LOG("shmmax: %zu\n", shmmax);
243 return shmmax;
244 }