libringbuffer shm: be resilient to app crash between shm_open and shm_unlink
[lttng-ust.git] / libringbuffer / shm.c
1 /*
2 * libringbuffer/shm.c
3 *
4 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Dual LGPL v2.1/GPL v2 license.
7 */
8
9 #include "shm.h"
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h> /* For mode constants */
14 #include <fcntl.h> /* For O_* constants */
15 #include <assert.h>
16 #include <ust/align.h>
17
18 struct shm_object_table *shm_object_table_create(size_t max_nb_obj)
19 {
20 struct shm_object_table *table;
21
22 table = zmalloc(sizeof(struct shm_object_table) +
23 max_nb_obj * sizeof(table->objects[0]));
24 table->size = max_nb_obj;
25 return table;
26 }
27
28 struct shm_object *shm_object_table_append(struct shm_object_table *table,
29 size_t memory_map_size)
30 {
31 int shmfd, waitfd[2], ret, i;
32 struct shm_object *obj;
33 char *memory_map;
34
35 if (table->allocated_len >= table->size)
36 return NULL;
37 obj = &table->objects[table->allocated_len];
38
39 /* wait_fd: create pipe */
40 ret = pipe(waitfd);
41 if (ret < 0) {
42 PERROR("pipe");
43 goto error_pipe;
44 }
45 for (i = 0; i < 2; i++) {
46 ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC);
47 if (ret < 0) {
48 PERROR("fcntl");
49 goto error_fcntl;
50 }
51 }
52 /* The write end of the pipe needs to be non-blocking */
53 ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK);
54 if (ret < 0) {
55 PERROR("fcntl");
56 goto error_fcntl;
57 }
58 memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
59
60 /* shm_fd: create shm */
61
62 /*
63 * Allocate shm, and immediately unlink its shm oject, keeping
64 * only the file descriptor as a reference to the object. If it
65 * already exists (caused by short race window during which the
66 * global object exists in a concurrent shm_open), simply retry.
67 * We specifically do _not_ use the / at the beginning of the
68 * pathname so that some OS implementations can keep it local to
69 * the process (POSIX leaves this implementation-defined).
70 * Ignore the shm_unlink errors, because we handle leaks that
71 * could occur by applications crashing between shm_open and
72 * shm_unlink by unlinking the shm before every open. Therefore,
73 * we can only leak one single shm (and only if the application
74 * crashes between shm_open and the following shm_unlink).
75 */
76 do {
77 (void) shm_unlink("ust-shm-tmp");
78 shmfd = shm_open("ust-shm-tmp",
79 O_CREAT | O_EXCL | O_RDWR, 0700);
80 } while (shmfd < 0 && errno == EEXIST);
81 if (shmfd < 0) {
82 PERROR("shm_open");
83 goto error_shm_open;
84 }
85 ret = shm_unlink("ust-shm-tmp");
86 if (ret) {
87 PERROR("shm_unlink");
88 goto error_unlink;
89 }
90 ret = ftruncate(shmfd, memory_map_size);
91 if (ret) {
92 PERROR("ftruncate");
93 goto error_ftruncate;
94 }
95 obj->shm_fd = shmfd;
96
97 /* memory_map: mmap */
98 memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
99 MAP_SHARED, shmfd, 0);
100 if (memory_map == MAP_FAILED) {
101 PERROR("mmap");
102 goto error_mmap;
103 }
104 obj->memory_map = memory_map;
105 obj->memory_map_size = memory_map_size;
106 obj->allocated_len = 0;
107 obj->index = table->allocated_len++;
108
109 return obj;
110
111 error_mmap:
112 error_ftruncate:
113 error_unlink:
114 ret = close(shmfd);
115 if (ret) {
116 PERROR("close");
117 assert(0);
118 }
119 error_shm_open:
120 error_fcntl:
121 for (i = 0; i < 2; i++) {
122 ret = close(waitfd[i]);
123 if (ret) {
124 PERROR("close");
125 assert(0);
126 }
127 }
128 error_pipe:
129 return NULL;
130
131 }
132
133 static
134 void shmp_object_destroy(struct shm_object *obj)
135 {
136 int ret, i;
137
138 ret = munmap(obj->memory_map, obj->memory_map_size);
139 if (ret) {
140 PERROR("umnmap");
141 assert(0);
142 }
143 ret = close(obj->shm_fd);
144 if (ret) {
145 PERROR("close");
146 assert(0);
147 }
148 for (i = 0; i < 2; i++) {
149 ret = close(obj->wait_fd[i]);
150 if (ret) {
151 PERROR("close");
152 assert(0);
153 }
154 }
155 }
156
157 void shm_object_table_destroy(struct shm_object_table *table)
158 {
159 int i;
160
161 for (i = 0; i < table->allocated_len; i++)
162 shmp_object_destroy(&table->objects[i]);
163 free(table);
164 }
165
166 /*
167 * zalloc_shm - allocate memory within a shm object.
168 *
169 * Shared memory is already zeroed by shmget.
170 * *NOT* multithread-safe (should be protected by mutex).
171 * Returns a -1, -1 tuple on error.
172 */
173 struct shm_ref zalloc_shm(struct shm_object *obj, size_t len)
174 {
175 struct shm_ref ref;
176 struct shm_ref shm_ref_error = { -1, -1 };
177
178 if (obj->memory_map_size - obj->allocated_len < len)
179 return shm_ref_error;
180 ref.index = obj->index;
181 ref.offset = obj->allocated_len;
182 obj->allocated_len += len;
183 return ref;
184 }
185
186 void align_shm(struct shm_object *obj, size_t align)
187 {
188 size_t offset_len = offset_align(obj->allocated_len, align);
189 obj->allocated_len += offset_len;
190 }
This page took 0.033601 seconds and 5 git commands to generate.