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