8ac321e3931906888f79dd9e2419a7dbca8225de
[lttng-ust.git] / libringbuffer / shm.c
1 /*
2 * libringbuffer/shm.c
3 *
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #define _LGPL_SOURCE
22 #include "shm.h"
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include <sys/stat.h> /* For mode constants */
28 #include <fcntl.h> /* For O_* constants */
29 #include <assert.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <dirent.h>
33 #include <lttng/align.h>
34 #include <limits.h>
35 #include <helper.h>
36 #include <ust-fd.h>
37
38 /*
39 * Ensure we have the required amount of space available by writing 0
40 * into the entire buffer. Not doing so can trigger SIGBUS when going
41 * beyond the available shm space.
42 *
43 * Also ensure the file metadata is synced with the storage by using
44 * fsync(2).
45 */
46 static
47 int zero_file(int fd, size_t len)
48 {
49 ssize_t retlen;
50 size_t written = 0;
51 char *zeropage;
52 long pagelen;
53 int ret;
54
55 pagelen = sysconf(_SC_PAGESIZE);
56 if (pagelen < 0)
57 return (int) pagelen;
58 zeropage = calloc(pagelen, 1);
59 if (!zeropage)
60 return -ENOMEM;
61
62 while (len > written) {
63 do {
64 retlen = write(fd, zeropage,
65 min_t(size_t, pagelen, len - written));
66 } while (retlen == -1UL && errno == EINTR);
67 if (retlen < 0) {
68 ret = (int) retlen;
69 goto error;
70 }
71 written += retlen;
72 }
73 ret = fsync(fd);
74 if (ret) {
75 ret = (int) -errno;
76 goto error;
77 }
78 ret = 0;
79 error:
80 free(zeropage);
81 return ret;
82 }
83
84 struct shm_object_table *shm_object_table_create(size_t max_nb_obj)
85 {
86 struct shm_object_table *table;
87
88 table = zmalloc(sizeof(struct shm_object_table) +
89 max_nb_obj * sizeof(table->objects[0]));
90 if (!table)
91 return NULL;
92 table->size = max_nb_obj;
93 return table;
94 }
95
96 static
97 struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
98 size_t memory_map_size,
99 int stream_fd)
100 {
101 int shmfd, waitfd[2], ret, i;
102 struct shm_object *obj;
103 char *memory_map;
104
105 if (stream_fd < 0)
106 return NULL;
107 if (table->allocated_len >= table->size)
108 return NULL;
109 obj = &table->objects[table->allocated_len];
110
111 /* wait_fd: create pipe */
112 ret = pipe(waitfd);
113 if (ret < 0) {
114 PERROR("pipe");
115 goto error_pipe;
116 }
117 for (i = 0; i < 2; i++) {
118 ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC);
119 if (ret < 0) {
120 PERROR("fcntl");
121 goto error_fcntl;
122 }
123 }
124 /* The write end of the pipe needs to be non-blocking */
125 ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK);
126 if (ret < 0) {
127 PERROR("fcntl");
128 goto error_fcntl;
129 }
130 memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
131
132 /* create shm */
133
134 shmfd = stream_fd;
135 ret = zero_file(shmfd, memory_map_size);
136 if (ret) {
137 PERROR("zero_file");
138 goto error_zero_file;
139 }
140 ret = ftruncate(shmfd, memory_map_size);
141 if (ret) {
142 PERROR("ftruncate");
143 goto error_ftruncate;
144 }
145 obj->shm_fd_ownership = 0;
146 obj->shm_fd = shmfd;
147
148 /* memory_map: mmap */
149 memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
150 MAP_SHARED, shmfd, 0);
151 if (memory_map == MAP_FAILED) {
152 PERROR("mmap");
153 goto error_mmap;
154 }
155 obj->type = SHM_OBJECT_SHM;
156 obj->memory_map = memory_map;
157 obj->memory_map_size = memory_map_size;
158 obj->allocated_len = 0;
159 obj->index = table->allocated_len++;
160
161 return obj;
162
163 error_mmap:
164 error_ftruncate:
165 error_zero_file:
166 error_fcntl:
167 for (i = 0; i < 2; i++) {
168 ret = close(waitfd[i]);
169 if (ret) {
170 PERROR("close");
171 assert(0);
172 }
173 }
174 error_pipe:
175 return NULL;
176 }
177
178 static
179 struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table,
180 size_t memory_map_size)
181 {
182 struct shm_object *obj;
183 void *memory_map;
184 int waitfd[2], i, ret;
185
186 if (table->allocated_len >= table->size)
187 return NULL;
188 obj = &table->objects[table->allocated_len];
189
190 memory_map = zmalloc(memory_map_size);
191 if (!memory_map)
192 goto alloc_error;
193
194 /* wait_fd: create pipe */
195 ret = pipe(waitfd);
196 if (ret < 0) {
197 PERROR("pipe");
198 goto error_pipe;
199 }
200 for (i = 0; i < 2; i++) {
201 ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC);
202 if (ret < 0) {
203 PERROR("fcntl");
204 goto error_fcntl;
205 }
206 }
207 /* The write end of the pipe needs to be non-blocking */
208 ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK);
209 if (ret < 0) {
210 PERROR("fcntl");
211 goto error_fcntl;
212 }
213 memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
214
215 /* no shm_fd */
216 obj->shm_fd = -1;
217 obj->shm_fd_ownership = 0;
218
219 obj->type = SHM_OBJECT_MEM;
220 obj->memory_map = memory_map;
221 obj->memory_map_size = memory_map_size;
222 obj->allocated_len = 0;
223 obj->index = table->allocated_len++;
224
225 return obj;
226
227 error_fcntl:
228 for (i = 0; i < 2; i++) {
229 ret = close(waitfd[i]);
230 if (ret) {
231 PERROR("close");
232 assert(0);
233 }
234 }
235 error_pipe:
236 free(memory_map);
237 alloc_error:
238 return NULL;
239 }
240
241 struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
242 size_t memory_map_size,
243 enum shm_object_type type,
244 int stream_fd)
245 {
246 switch (type) {
247 case SHM_OBJECT_SHM:
248 return _shm_object_table_alloc_shm(table, memory_map_size,
249 stream_fd);
250 case SHM_OBJECT_MEM:
251 return _shm_object_table_alloc_mem(table, memory_map_size);
252 default:
253 assert(0);
254 }
255 return NULL;
256 }
257
258 struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
259 int shm_fd, int wakeup_fd, uint32_t stream_nr,
260 size_t memory_map_size)
261 {
262 struct shm_object *obj;
263 char *memory_map;
264 int ret;
265
266 if (table->allocated_len >= table->size)
267 return NULL;
268 /* streams _must_ be received in sequential order, else fail. */
269 if (stream_nr + 1 != table->allocated_len)
270 return NULL;
271
272 obj = &table->objects[table->allocated_len];
273
274 /* wait_fd: set write end of the pipe. */
275 obj->wait_fd[0] = -1; /* read end is unset */
276 obj->wait_fd[1] = wakeup_fd;
277 obj->shm_fd = shm_fd;
278 obj->shm_fd_ownership = 1;
279
280 ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC);
281 if (ret < 0) {
282 PERROR("fcntl");
283 goto error_fcntl;
284 }
285 /* The write end of the pipe needs to be non-blocking */
286 ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK);
287 if (ret < 0) {
288 PERROR("fcntl");
289 goto error_fcntl;
290 }
291
292 /* memory_map: mmap */
293 memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
294 MAP_SHARED, shm_fd, 0);
295 if (memory_map == MAP_FAILED) {
296 PERROR("mmap");
297 goto error_mmap;
298 }
299 obj->type = SHM_OBJECT_SHM;
300 obj->memory_map = memory_map;
301 obj->memory_map_size = memory_map_size;
302 obj->allocated_len = memory_map_size;
303 obj->index = table->allocated_len++;
304
305 return obj;
306
307 error_fcntl:
308 error_mmap:
309 return NULL;
310 }
311
312 /*
313 * Passing ownership of mem to object.
314 */
315 struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
316 void *mem, size_t memory_map_size, int wakeup_fd)
317 {
318 struct shm_object *obj;
319 int ret;
320
321 if (table->allocated_len >= table->size)
322 return NULL;
323 obj = &table->objects[table->allocated_len];
324
325 obj->wait_fd[0] = -1; /* read end is unset */
326 obj->wait_fd[1] = wakeup_fd;
327 obj->shm_fd = -1;
328 obj->shm_fd_ownership = 0;
329
330 ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC);
331 if (ret < 0) {
332 PERROR("fcntl");
333 goto error_fcntl;
334 }
335 /* The write end of the pipe needs to be non-blocking */
336 ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK);
337 if (ret < 0) {
338 PERROR("fcntl");
339 goto error_fcntl;
340 }
341
342 obj->type = SHM_OBJECT_MEM;
343 obj->memory_map = mem;
344 obj->memory_map_size = memory_map_size;
345 obj->allocated_len = memory_map_size;
346 obj->index = table->allocated_len++;
347
348 return obj;
349
350 error_fcntl:
351 return NULL;
352 }
353
354 static
355 void shmp_object_destroy(struct shm_object *obj, int consumer)
356 {
357 switch (obj->type) {
358 case SHM_OBJECT_SHM:
359 {
360 int ret, i;
361
362 ret = munmap(obj->memory_map, obj->memory_map_size);
363 if (ret) {
364 PERROR("umnmap");
365 assert(0);
366 }
367
368 if (obj->shm_fd_ownership) {
369 /* Delete FDs only if called from app (not consumer). */
370 if (!consumer) {
371 lttng_ust_lock_fd_tracker();
372 ret = close(obj->shm_fd);
373 if (!ret) {
374 lttng_ust_delete_fd_from_tracker(obj->shm_fd);
375 } else {
376 PERROR("close");
377 assert(0);
378 }
379 lttng_ust_unlock_fd_tracker();
380 } else {
381 ret = close(obj->shm_fd);
382 if (ret) {
383 PERROR("close");
384 assert(0);
385 }
386 }
387 }
388 for (i = 0; i < 2; i++) {
389 if (obj->wait_fd[i] < 0)
390 continue;
391 if (!consumer) {
392 lttng_ust_lock_fd_tracker();
393 ret = close(obj->wait_fd[i]);
394 if (!ret) {
395 lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]);
396 } else {
397 PERROR("close");
398 assert(0);
399 }
400 lttng_ust_unlock_fd_tracker();
401 } else {
402 ret = close(obj->wait_fd[i]);
403 if (ret) {
404 PERROR("close");
405 assert(0);
406 }
407 }
408 }
409 break;
410 }
411 case SHM_OBJECT_MEM:
412 {
413 int ret, i;
414
415 for (i = 0; i < 2; i++) {
416 if (obj->wait_fd[i] < 0)
417 continue;
418 if (!consumer) {
419 lttng_ust_lock_fd_tracker();
420 ret = close(obj->wait_fd[i]);
421 if (!ret) {
422 lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]);
423 } else {
424 PERROR("close");
425 assert(0);
426 }
427 lttng_ust_unlock_fd_tracker();
428 } else {
429 ret = close(obj->wait_fd[i]);
430 if (ret) {
431 PERROR("close");
432 assert(0);
433 }
434 }
435 }
436 free(obj->memory_map);
437 break;
438 }
439 default:
440 assert(0);
441 }
442 }
443
444 void shm_object_table_destroy(struct shm_object_table *table, int consumer)
445 {
446 int i;
447
448 for (i = 0; i < table->allocated_len; i++)
449 shmp_object_destroy(&table->objects[i], consumer);
450 free(table);
451 }
452
453 /*
454 * zalloc_shm - allocate memory within a shm object.
455 *
456 * Shared memory is already zeroed by shmget.
457 * *NOT* multithread-safe (should be protected by mutex).
458 * Returns a -1, -1 tuple on error.
459 */
460 struct shm_ref zalloc_shm(struct shm_object *obj, size_t len)
461 {
462 struct shm_ref ref;
463 struct shm_ref shm_ref_error = { -1, -1 };
464
465 if (obj->memory_map_size - obj->allocated_len < len)
466 return shm_ref_error;
467 ref.index = obj->index;
468 ref.offset = obj->allocated_len;
469 obj->allocated_len += len;
470 return ref;
471 }
472
473 void align_shm(struct shm_object *obj, size_t align)
474 {
475 size_t offset_len = offset_align(obj->allocated_len, align);
476 obj->allocated_len += offset_len;
477 }
This page took 0.037615 seconds and 3 git commands to generate.