Commit | Line | Data |
---|---|---|
1d498196 MD |
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> | |
8da6cd6d MD |
16 | #include <stdio.h> |
17 | #include <signal.h> | |
18 | #include <dirent.h> | |
4318ae1b | 19 | #include <lttng/align.h> |
35897f8b | 20 | #include <helper.h> |
96e80018 | 21 | #include <limits.h> |
1d498196 MD |
22 | |
23 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj) | |
24 | { | |
25 | struct shm_object_table *table; | |
26 | ||
27 | table = zmalloc(sizeof(struct shm_object_table) + | |
28 | max_nb_obj * sizeof(table->objects[0])); | |
29 | table->size = max_nb_obj; | |
30 | return table; | |
31 | } | |
32 | ||
33 | struct shm_object *shm_object_table_append(struct shm_object_table *table, | |
34 | size_t memory_map_size) | |
35 | { | |
5a61337d | 36 | int shmfd, waitfd[2], ret, i, sigblocked = 0; |
1d498196 MD |
37 | struct shm_object *obj; |
38 | char *memory_map; | |
8da6cd6d MD |
39 | char tmp_name[NAME_MAX] = "ust-shm-tmp-XXXXXX"; |
40 | sigset_t all_sigs, orig_sigs; | |
1d498196 MD |
41 | |
42 | if (table->allocated_len >= table->size) | |
43 | return NULL; | |
7a9c21bd | 44 | obj = &table->objects[table->allocated_len]; |
1d498196 MD |
45 | |
46 | /* wait_fd: create pipe */ | |
47 | ret = pipe(waitfd); | |
48 | if (ret < 0) { | |
49 | PERROR("pipe"); | |
50 | goto error_pipe; | |
51 | } | |
52 | for (i = 0; i < 2; i++) { | |
53 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); | |
54 | if (ret < 0) { | |
55 | PERROR("fcntl"); | |
56 | goto error_fcntl; | |
57 | } | |
58 | } | |
5d61a504 MD |
59 | /* The write end of the pipe needs to be non-blocking */ |
60 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); | |
61 | if (ret < 0) { | |
62 | PERROR("fcntl"); | |
63 | goto error_fcntl; | |
64 | } | |
7a9c21bd | 65 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); |
1d498196 MD |
66 | |
67 | /* shm_fd: create shm */ | |
68 | ||
8da6cd6d MD |
69 | /* |
70 | * Theoretically, we could leak a shm if the application crashes | |
71 | * between open and unlink. Disable signals on this thread for | |
72 | * increased safety against this scenario. | |
73 | */ | |
74 | sigfillset(&all_sigs); | |
75 | ret = pthread_sigmask(SIG_BLOCK, &all_sigs, &orig_sigs); | |
76 | if (ret == -1) { | |
77 | PERROR("pthread_sigmask"); | |
78 | goto error_pthread_sigmask; | |
79 | } | |
5a61337d | 80 | sigblocked = 1; |
8da6cd6d | 81 | |
1d498196 MD |
82 | /* |
83 | * Allocate shm, and immediately unlink its shm oject, keeping | |
84 | * only the file descriptor as a reference to the object. If it | |
85 | * already exists (caused by short race window during which the | |
86 | * global object exists in a concurrent shm_open), simply retry. | |
87 | * We specifically do _not_ use the / at the beginning of the | |
88 | * pathname so that some OS implementations can keep it local to | |
89 | * the process (POSIX leaves this implementation-defined). | |
90 | */ | |
91 | do { | |
8da6cd6d MD |
92 | /* |
93 | * Using mktemp filename with O_CREAT | O_EXCL open | |
94 | * flags. | |
95 | */ | |
96 | mktemp(tmp_name); | |
97 | if (tmp_name[0] == '\0') { | |
98 | PERROR("mktemp"); | |
99 | goto error_shm_open; | |
a8803897 | 100 | } |
8da6cd6d | 101 | shmfd = shm_open(tmp_name, |
1d498196 | 102 | O_CREAT | O_EXCL | O_RDWR, 0700); |
8da6cd6d | 103 | } while (shmfd < 0 && (errno == EEXIST || errno == EACCES)); |
1d498196 MD |
104 | if (shmfd < 0) { |
105 | PERROR("shm_open"); | |
106 | goto error_shm_open; | |
107 | } | |
8da6cd6d | 108 | ret = shm_unlink(tmp_name); |
a8803897 MD |
109 | if (ret < 0 && errno != ENOENT) { |
110 | PERROR("shm_unlink"); | |
111 | goto error_shm_release; | |
112 | } | |
5a61337d | 113 | sigblocked = 0; |
8da6cd6d MD |
114 | ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); |
115 | if (ret == -1) { | |
116 | PERROR("pthread_sigmask"); | |
5a61337d | 117 | goto error_sigmask_release; |
8da6cd6d | 118 | } |
1d498196 MD |
119 | ret = ftruncate(shmfd, memory_map_size); |
120 | if (ret) { | |
121 | PERROR("ftruncate"); | |
122 | goto error_ftruncate; | |
123 | } | |
124 | obj->shm_fd = shmfd; | |
125 | ||
126 | /* memory_map: mmap */ | |
127 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, | |
128 | MAP_SHARED, shmfd, 0); | |
129 | if (memory_map == MAP_FAILED) { | |
130 | PERROR("mmap"); | |
131 | goto error_mmap; | |
132 | } | |
133 | obj->memory_map = memory_map; | |
134 | obj->memory_map_size = memory_map_size; | |
135 | obj->allocated_len = 0; | |
dc613eb9 | 136 | obj->index = table->allocated_len++; |
7a9c21bd | 137 | |
1d498196 MD |
138 | return obj; |
139 | ||
140 | error_mmap: | |
141 | error_ftruncate: | |
a8803897 | 142 | error_shm_release: |
5a61337d | 143 | error_sigmask_release: |
1d498196 MD |
144 | ret = close(shmfd); |
145 | if (ret) { | |
146 | PERROR("close"); | |
147 | assert(0); | |
148 | } | |
149 | error_shm_open: | |
5a61337d MD |
150 | if (sigblocked) { |
151 | ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); | |
152 | if (ret == -1) { | |
153 | PERROR("pthread_sigmask"); | |
154 | } | |
155 | } | |
8da6cd6d | 156 | error_pthread_sigmask: |
1d498196 MD |
157 | error_fcntl: |
158 | for (i = 0; i < 2; i++) { | |
159 | ret = close(waitfd[i]); | |
160 | if (ret) { | |
161 | PERROR("close"); | |
162 | assert(0); | |
163 | } | |
164 | } | |
165 | error_pipe: | |
1d498196 MD |
166 | return NULL; |
167 | ||
168 | } | |
169 | ||
193183fb MD |
170 | struct shm_object *shm_object_table_append_shadow(struct shm_object_table *table, |
171 | int shm_fd, int wait_fd, size_t memory_map_size) | |
172 | { | |
173 | struct shm_object *obj; | |
174 | char *memory_map; | |
175 | ||
176 | if (table->allocated_len >= table->size) | |
177 | return NULL; | |
178 | obj = &table->objects[table->allocated_len]; | |
179 | ||
180 | /* wait_fd: set read end of the pipe. */ | |
181 | obj->wait_fd[0] = wait_fd; | |
182 | obj->wait_fd[1] = -1; /* write end is unset. */ | |
183 | obj->shm_fd = shm_fd; | |
184 | ||
185 | /* memory_map: mmap */ | |
186 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, | |
187 | MAP_SHARED, shm_fd, 0); | |
188 | if (memory_map == MAP_FAILED) { | |
189 | PERROR("mmap"); | |
190 | goto error_mmap; | |
191 | } | |
192 | obj->memory_map = memory_map; | |
193 | obj->memory_map_size = memory_map_size; | |
194 | obj->allocated_len = memory_map_size; | |
195 | obj->index = table->allocated_len++; | |
196 | ||
197 | return obj; | |
198 | ||
199 | error_mmap: | |
200 | return NULL; | |
201 | } | |
202 | ||
1d498196 MD |
203 | static |
204 | void shmp_object_destroy(struct shm_object *obj) | |
205 | { | |
206 | int ret, i; | |
207 | ||
7a784989 MD |
208 | if (!obj->is_shadow) { |
209 | ret = munmap(obj->memory_map, obj->memory_map_size); | |
210 | if (ret) { | |
211 | PERROR("umnmap"); | |
212 | assert(0); | |
213 | } | |
214 | } | |
ef9ff354 MD |
215 | if (obj->shm_fd >= 0) { |
216 | ret = close(obj->shm_fd); | |
217 | if (ret) { | |
218 | PERROR("close"); | |
219 | assert(0); | |
220 | } | |
1d498196 MD |
221 | } |
222 | for (i = 0; i < 2; i++) { | |
824f40b8 MD |
223 | if (obj->wait_fd[i] < 0) |
224 | continue; | |
1d498196 MD |
225 | ret = close(obj->wait_fd[i]); |
226 | if (ret) { | |
227 | PERROR("close"); | |
228 | assert(0); | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
233 | void shm_object_table_destroy(struct shm_object_table *table) | |
234 | { | |
235 | int i; | |
236 | ||
237 | for (i = 0; i < table->allocated_len; i++) | |
238 | shmp_object_destroy(&table->objects[i]); | |
239 | free(table); | |
240 | } | |
241 | ||
242 | /* | |
243 | * zalloc_shm - allocate memory within a shm object. | |
244 | * | |
245 | * Shared memory is already zeroed by shmget. | |
246 | * *NOT* multithread-safe (should be protected by mutex). | |
247 | * Returns a -1, -1 tuple on error. | |
248 | */ | |
249 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len) | |
250 | { | |
251 | struct shm_ref ref; | |
252 | struct shm_ref shm_ref_error = { -1, -1 }; | |
253 | ||
254 | if (obj->memory_map_size - obj->allocated_len < len) | |
255 | return shm_ref_error; | |
256 | ref.index = obj->index; | |
257 | ref.offset = obj->allocated_len; | |
258 | obj->allocated_len += len; | |
259 | return ref; | |
260 | } | |
261 | ||
262 | void align_shm(struct shm_object *obj, size_t align) | |
263 | { | |
264 | size_t offset_len = offset_align(obj->allocated_len, align); | |
265 | obj->allocated_len += offset_len; | |
266 | } |