| 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 | #include "shm.h" |
| 22 | #include <unistd.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> /* For mode constants */ |
| 26 | #include <fcntl.h> /* For O_* constants */ |
| 27 | #include <assert.h> |
| 28 | #include <stdio.h> |
| 29 | #include <signal.h> |
| 30 | #include <dirent.h> |
| 31 | #include <lttng/align.h> |
| 32 | #include <helper.h> |
| 33 | #include <limits.h> |
| 34 | #include <helper.h> |
| 35 | |
| 36 | /* |
| 37 | * Ensure we have the required amount of space available by writing 0 |
| 38 | * into the entire buffer. Not doing so can trigger SIGBUS when going |
| 39 | * beyond the available shm space. |
| 40 | */ |
| 41 | static |
| 42 | int zero_file(int fd, size_t len) |
| 43 | { |
| 44 | ssize_t retlen; |
| 45 | size_t written = 0; |
| 46 | char *zeropage; |
| 47 | long pagelen; |
| 48 | int ret; |
| 49 | |
| 50 | pagelen = sysconf(_SC_PAGESIZE); |
| 51 | if (pagelen < 0) |
| 52 | return (int) pagelen; |
| 53 | zeropage = calloc(pagelen, 1); |
| 54 | if (!zeropage) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | while (len > written) { |
| 58 | do { |
| 59 | retlen = write(fd, zeropage, |
| 60 | min_t(size_t, pagelen, len - written)); |
| 61 | } while (retlen == -1UL && errno == EINTR); |
| 62 | if (retlen < 0) { |
| 63 | ret = (int) retlen; |
| 64 | goto error; |
| 65 | } |
| 66 | written += retlen; |
| 67 | } |
| 68 | ret = 0; |
| 69 | error: |
| 70 | free(zeropage); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj) |
| 75 | { |
| 76 | struct shm_object_table *table; |
| 77 | |
| 78 | table = zmalloc(sizeof(struct shm_object_table) + |
| 79 | max_nb_obj * sizeof(table->objects[0])); |
| 80 | table->size = max_nb_obj; |
| 81 | return table; |
| 82 | } |
| 83 | |
| 84 | static |
| 85 | struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, |
| 86 | size_t memory_map_size) |
| 87 | { |
| 88 | int shmfd, waitfd[2], ret, i, sigblocked = 0; |
| 89 | struct shm_object *obj; |
| 90 | char *memory_map; |
| 91 | char tmp_name[NAME_MAX] = "/ust-shm-tmp-XXXXXX"; |
| 92 | sigset_t all_sigs, orig_sigs; |
| 93 | |
| 94 | if (table->allocated_len >= table->size) |
| 95 | return NULL; |
| 96 | obj = &table->objects[table->allocated_len]; |
| 97 | |
| 98 | /* wait_fd: create pipe */ |
| 99 | ret = pipe(waitfd); |
| 100 | if (ret < 0) { |
| 101 | PERROR("pipe"); |
| 102 | goto error_pipe; |
| 103 | } |
| 104 | for (i = 0; i < 2; i++) { |
| 105 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); |
| 106 | if (ret < 0) { |
| 107 | PERROR("fcntl"); |
| 108 | goto error_fcntl; |
| 109 | } |
| 110 | } |
| 111 | /* The write end of the pipe needs to be non-blocking */ |
| 112 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); |
| 113 | if (ret < 0) { |
| 114 | PERROR("fcntl"); |
| 115 | goto error_fcntl; |
| 116 | } |
| 117 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); |
| 118 | |
| 119 | /* shm_fd: create shm */ |
| 120 | |
| 121 | /* |
| 122 | * Theoretically, we could leak a shm if the application crashes |
| 123 | * between open and unlink. Disable signals on this thread for |
| 124 | * increased safety against this scenario. |
| 125 | */ |
| 126 | sigfillset(&all_sigs); |
| 127 | ret = pthread_sigmask(SIG_BLOCK, &all_sigs, &orig_sigs); |
| 128 | if (ret == -1) { |
| 129 | PERROR("pthread_sigmask"); |
| 130 | goto error_pthread_sigmask; |
| 131 | } |
| 132 | sigblocked = 1; |
| 133 | |
| 134 | /* |
| 135 | * Allocate shm, and immediately unlink its shm oject, keeping |
| 136 | * only the file descriptor as a reference to the object. If it |
| 137 | * already exists (caused by short race window during which the |
| 138 | * global object exists in a concurrent shm_open), simply retry. |
| 139 | * We specifically do _not_ use the / at the beginning of the |
| 140 | * pathname so that some OS implementations can keep it local to |
| 141 | * the process (POSIX leaves this implementation-defined). |
| 142 | */ |
| 143 | do { |
| 144 | /* |
| 145 | * Using mktemp filename with O_CREAT | O_EXCL open |
| 146 | * flags. |
| 147 | */ |
| 148 | mktemp(tmp_name); |
| 149 | if (tmp_name[0] == '\0') { |
| 150 | PERROR("mktemp"); |
| 151 | goto error_shm_open; |
| 152 | } |
| 153 | shmfd = shm_open(tmp_name, |
| 154 | O_CREAT | O_EXCL | O_RDWR, 0700); |
| 155 | } while (shmfd < 0 && (errno == EEXIST || errno == EACCES)); |
| 156 | if (shmfd < 0) { |
| 157 | PERROR("shm_open"); |
| 158 | goto error_shm_open; |
| 159 | } |
| 160 | ret = shm_unlink(tmp_name); |
| 161 | if (ret < 0 && errno != ENOENT) { |
| 162 | PERROR("shm_unlink"); |
| 163 | goto error_shm_release; |
| 164 | } |
| 165 | sigblocked = 0; |
| 166 | ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); |
| 167 | if (ret == -1) { |
| 168 | PERROR("pthread_sigmask"); |
| 169 | goto error_sigmask_release; |
| 170 | } |
| 171 | ret = zero_file(shmfd, memory_map_size); |
| 172 | if (ret) { |
| 173 | PERROR("zero_file"); |
| 174 | goto error_zero_file; |
| 175 | } |
| 176 | ret = ftruncate(shmfd, memory_map_size); |
| 177 | if (ret) { |
| 178 | PERROR("ftruncate"); |
| 179 | goto error_ftruncate; |
| 180 | } |
| 181 | obj->shm_fd = shmfd; |
| 182 | |
| 183 | /* memory_map: mmap */ |
| 184 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, |
| 185 | MAP_SHARED, shmfd, 0); |
| 186 | if (memory_map == MAP_FAILED) { |
| 187 | PERROR("mmap"); |
| 188 | goto error_mmap; |
| 189 | } |
| 190 | obj->type = SHM_OBJECT_SHM; |
| 191 | obj->memory_map = memory_map; |
| 192 | obj->memory_map_size = memory_map_size; |
| 193 | obj->allocated_len = 0; |
| 194 | obj->index = table->allocated_len++; |
| 195 | |
| 196 | return obj; |
| 197 | |
| 198 | error_mmap: |
| 199 | error_ftruncate: |
| 200 | error_shm_release: |
| 201 | error_zero_file: |
| 202 | error_sigmask_release: |
| 203 | ret = close(shmfd); |
| 204 | if (ret) { |
| 205 | PERROR("close"); |
| 206 | assert(0); |
| 207 | } |
| 208 | error_shm_open: |
| 209 | if (sigblocked) { |
| 210 | ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); |
| 211 | if (ret == -1) { |
| 212 | PERROR("pthread_sigmask"); |
| 213 | } |
| 214 | } |
| 215 | error_pthread_sigmask: |
| 216 | error_fcntl: |
| 217 | for (i = 0; i < 2; i++) { |
| 218 | ret = close(waitfd[i]); |
| 219 | if (ret) { |
| 220 | PERROR("close"); |
| 221 | assert(0); |
| 222 | } |
| 223 | } |
| 224 | error_pipe: |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | static |
| 229 | struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, |
| 230 | size_t memory_map_size) |
| 231 | { |
| 232 | struct shm_object *obj; |
| 233 | void *memory_map; |
| 234 | |
| 235 | if (table->allocated_len >= table->size) |
| 236 | return NULL; |
| 237 | obj = &table->objects[table->allocated_len]; |
| 238 | |
| 239 | memory_map = zmalloc(memory_map_size); |
| 240 | if (!memory_map) |
| 241 | goto alloc_error; |
| 242 | |
| 243 | obj->wait_fd[0] = -1; |
| 244 | obj->wait_fd[1] = -1; |
| 245 | obj->shm_fd = -1; |
| 246 | |
| 247 | obj->type = SHM_OBJECT_MEM; |
| 248 | obj->memory_map = memory_map; |
| 249 | obj->memory_map_size = memory_map_size; |
| 250 | obj->allocated_len = 0; |
| 251 | obj->index = table->allocated_len++; |
| 252 | |
| 253 | return obj; |
| 254 | |
| 255 | alloc_error: |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | struct shm_object *shm_object_table_alloc(struct shm_object_table *table, |
| 260 | size_t memory_map_size, |
| 261 | enum shm_object_type type) |
| 262 | { |
| 263 | switch (type) { |
| 264 | case SHM_OBJECT_SHM: |
| 265 | return _shm_object_table_alloc_shm(table, memory_map_size); |
| 266 | case SHM_OBJECT_MEM: |
| 267 | return _shm_object_table_alloc_mem(table, memory_map_size); |
| 268 | default: |
| 269 | assert(0); |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | struct shm_object *shm_object_table_append_shm(struct shm_object_table *table, |
| 275 | int shm_fd, int wakeup_fd, uint32_t stream_nr, |
| 276 | size_t memory_map_size) |
| 277 | { |
| 278 | struct shm_object *obj; |
| 279 | char *memory_map; |
| 280 | int ret; |
| 281 | |
| 282 | if (table->allocated_len >= table->size) |
| 283 | return NULL; |
| 284 | /* streams _must_ be received in sequential order, else fail. */ |
| 285 | if (stream_nr + 1 != table->allocated_len) |
| 286 | return NULL; |
| 287 | |
| 288 | obj = &table->objects[table->allocated_len]; |
| 289 | |
| 290 | /* wait_fd: set write end of the pipe. */ |
| 291 | obj->wait_fd[0] = -1; /* read end is unset */ |
| 292 | obj->wait_fd[1] = wakeup_fd; |
| 293 | obj->shm_fd = shm_fd; |
| 294 | |
| 295 | ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); |
| 296 | if (ret < 0) { |
| 297 | PERROR("fcntl"); |
| 298 | goto error_fcntl; |
| 299 | } |
| 300 | /* The write end of the pipe needs to be non-blocking */ |
| 301 | ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK); |
| 302 | if (ret < 0) { |
| 303 | PERROR("fcntl"); |
| 304 | goto error_fcntl; |
| 305 | } |
| 306 | |
| 307 | /* memory_map: mmap */ |
| 308 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, |
| 309 | MAP_SHARED, shm_fd, 0); |
| 310 | if (memory_map == MAP_FAILED) { |
| 311 | PERROR("mmap"); |
| 312 | goto error_mmap; |
| 313 | } |
| 314 | obj->type = SHM_OBJECT_SHM; |
| 315 | obj->memory_map = memory_map; |
| 316 | obj->memory_map_size = memory_map_size; |
| 317 | obj->allocated_len = memory_map_size; |
| 318 | obj->index = table->allocated_len++; |
| 319 | |
| 320 | return obj; |
| 321 | |
| 322 | error_fcntl: |
| 323 | error_mmap: |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Passing ownership of mem to object. |
| 329 | */ |
| 330 | struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, |
| 331 | void *mem, size_t memory_map_size) |
| 332 | { |
| 333 | struct shm_object *obj; |
| 334 | |
| 335 | if (table->allocated_len >= table->size) |
| 336 | return NULL; |
| 337 | obj = &table->objects[table->allocated_len]; |
| 338 | |
| 339 | obj->wait_fd[0] = -1; |
| 340 | obj->wait_fd[1] = -1; |
| 341 | obj->shm_fd = -1; |
| 342 | |
| 343 | obj->type = SHM_OBJECT_MEM; |
| 344 | obj->memory_map = mem; |
| 345 | obj->memory_map_size = memory_map_size; |
| 346 | obj->allocated_len = memory_map_size; |
| 347 | obj->index = table->allocated_len++; |
| 348 | |
| 349 | return obj; |
| 350 | } |
| 351 | |
| 352 | static |
| 353 | void shmp_object_destroy(struct shm_object *obj) |
| 354 | { |
| 355 | switch (obj->type) { |
| 356 | case SHM_OBJECT_SHM: |
| 357 | { |
| 358 | int ret, i; |
| 359 | |
| 360 | ret = munmap(obj->memory_map, obj->memory_map_size); |
| 361 | if (ret) { |
| 362 | PERROR("umnmap"); |
| 363 | assert(0); |
| 364 | } |
| 365 | ret = close(obj->shm_fd); |
| 366 | if (ret) { |
| 367 | PERROR("close"); |
| 368 | assert(0); |
| 369 | } |
| 370 | for (i = 0; i < 2; i++) { |
| 371 | if (obj->wait_fd[i] < 0) |
| 372 | continue; |
| 373 | ret = close(obj->wait_fd[i]); |
| 374 | if (ret) { |
| 375 | PERROR("close"); |
| 376 | assert(0); |
| 377 | } |
| 378 | } |
| 379 | break; |
| 380 | } |
| 381 | case SHM_OBJECT_MEM: |
| 382 | free(obj->memory_map); |
| 383 | break; |
| 384 | default: |
| 385 | assert(0); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void shm_object_table_destroy(struct shm_object_table *table) |
| 390 | { |
| 391 | int i; |
| 392 | |
| 393 | for (i = 0; i < table->allocated_len; i++) |
| 394 | shmp_object_destroy(&table->objects[i]); |
| 395 | free(table); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * zalloc_shm - allocate memory within a shm object. |
| 400 | * |
| 401 | * Shared memory is already zeroed by shmget. |
| 402 | * *NOT* multithread-safe (should be protected by mutex). |
| 403 | * Returns a -1, -1 tuple on error. |
| 404 | */ |
| 405 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len) |
| 406 | { |
| 407 | struct shm_ref ref; |
| 408 | struct shm_ref shm_ref_error = { -1, -1 }; |
| 409 | |
| 410 | if (obj->memory_map_size - obj->allocated_len < len) |
| 411 | return shm_ref_error; |
| 412 | ref.index = obj->index; |
| 413 | ref.offset = obj->allocated_len; |
| 414 | obj->allocated_len += len; |
| 415 | return ref; |
| 416 | } |
| 417 | |
| 418 | void align_shm(struct shm_object *obj, size_t align) |
| 419 | { |
| 420 | size_t offset_len = offset_align(obj->allocated_len, align); |
| 421 | obj->allocated_len += offset_len; |
| 422 | } |