| 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 | if (!table) |
| 81 | return NULL; |
| 82 | table->size = max_nb_obj; |
| 83 | return table; |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, |
| 88 | size_t memory_map_size) |
| 89 | { |
| 90 | int shmfd, waitfd[2], ret, i, sigblocked = 0; |
| 91 | struct shm_object *obj; |
| 92 | char *memory_map; |
| 93 | char tmp_name[NAME_MAX] = "/ust-shm-tmp-XXXXXX"; |
| 94 | sigset_t all_sigs, orig_sigs; |
| 95 | |
| 96 | if (table->allocated_len >= table->size) |
| 97 | return NULL; |
| 98 | obj = &table->objects[table->allocated_len]; |
| 99 | |
| 100 | /* wait_fd: create pipe */ |
| 101 | ret = pipe(waitfd); |
| 102 | if (ret < 0) { |
| 103 | PERROR("pipe"); |
| 104 | goto error_pipe; |
| 105 | } |
| 106 | for (i = 0; i < 2; i++) { |
| 107 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); |
| 108 | if (ret < 0) { |
| 109 | PERROR("fcntl"); |
| 110 | goto error_fcntl; |
| 111 | } |
| 112 | } |
| 113 | /* The write end of the pipe needs to be non-blocking */ |
| 114 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); |
| 115 | if (ret < 0) { |
| 116 | PERROR("fcntl"); |
| 117 | goto error_fcntl; |
| 118 | } |
| 119 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); |
| 120 | |
| 121 | /* shm_fd: create shm */ |
| 122 | |
| 123 | /* |
| 124 | * Theoretically, we could leak a shm if the application crashes |
| 125 | * between open and unlink. Disable signals on this thread for |
| 126 | * increased safety against this scenario. |
| 127 | */ |
| 128 | sigfillset(&all_sigs); |
| 129 | ret = pthread_sigmask(SIG_BLOCK, &all_sigs, &orig_sigs); |
| 130 | if (ret == -1) { |
| 131 | PERROR("pthread_sigmask"); |
| 132 | goto error_pthread_sigmask; |
| 133 | } |
| 134 | sigblocked = 1; |
| 135 | |
| 136 | /* |
| 137 | * Allocate shm, and immediately unlink its shm oject, keeping |
| 138 | * only the file descriptor as a reference to the object. If it |
| 139 | * already exists (caused by short race window during which the |
| 140 | * global object exists in a concurrent shm_open), simply retry. |
| 141 | * We specifically do _not_ use the / at the beginning of the |
| 142 | * pathname so that some OS implementations can keep it local to |
| 143 | * the process (POSIX leaves this implementation-defined). |
| 144 | */ |
| 145 | do { |
| 146 | /* |
| 147 | * Using mktemp filename with O_CREAT | O_EXCL open |
| 148 | * flags. |
| 149 | */ |
| 150 | (void) mktemp(tmp_name); |
| 151 | if (tmp_name[0] == '\0') { |
| 152 | PERROR("mktemp"); |
| 153 | goto error_shm_open; |
| 154 | } |
| 155 | shmfd = shm_open(tmp_name, |
| 156 | O_CREAT | O_EXCL | O_RDWR, 0700); |
| 157 | } while (shmfd < 0 && (errno == EEXIST || errno == EACCES)); |
| 158 | if (shmfd < 0) { |
| 159 | PERROR("shm_open"); |
| 160 | goto error_shm_open; |
| 161 | } |
| 162 | ret = shm_unlink(tmp_name); |
| 163 | if (ret < 0 && errno != ENOENT) { |
| 164 | PERROR("shm_unlink"); |
| 165 | goto error_shm_release; |
| 166 | } |
| 167 | sigblocked = 0; |
| 168 | ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); |
| 169 | if (ret == -1) { |
| 170 | PERROR("pthread_sigmask"); |
| 171 | goto error_sigmask_release; |
| 172 | } |
| 173 | ret = zero_file(shmfd, memory_map_size); |
| 174 | if (ret) { |
| 175 | PERROR("zero_file"); |
| 176 | goto error_zero_file; |
| 177 | } |
| 178 | ret = ftruncate(shmfd, memory_map_size); |
| 179 | if (ret) { |
| 180 | PERROR("ftruncate"); |
| 181 | goto error_ftruncate; |
| 182 | } |
| 183 | obj->shm_fd = shmfd; |
| 184 | |
| 185 | /* memory_map: mmap */ |
| 186 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, |
| 187 | MAP_SHARED, shmfd, 0); |
| 188 | if (memory_map == MAP_FAILED) { |
| 189 | PERROR("mmap"); |
| 190 | goto error_mmap; |
| 191 | } |
| 192 | obj->type = SHM_OBJECT_SHM; |
| 193 | obj->memory_map = memory_map; |
| 194 | obj->memory_map_size = memory_map_size; |
| 195 | obj->allocated_len = 0; |
| 196 | obj->index = table->allocated_len++; |
| 197 | |
| 198 | return obj; |
| 199 | |
| 200 | error_mmap: |
| 201 | error_ftruncate: |
| 202 | error_shm_release: |
| 203 | error_zero_file: |
| 204 | error_sigmask_release: |
| 205 | ret = close(shmfd); |
| 206 | if (ret) { |
| 207 | PERROR("close"); |
| 208 | assert(0); |
| 209 | } |
| 210 | error_shm_open: |
| 211 | if (sigblocked) { |
| 212 | ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); |
| 213 | if (ret == -1) { |
| 214 | PERROR("pthread_sigmask"); |
| 215 | } |
| 216 | } |
| 217 | error_pthread_sigmask: |
| 218 | error_fcntl: |
| 219 | for (i = 0; i < 2; i++) { |
| 220 | ret = close(waitfd[i]); |
| 221 | if (ret) { |
| 222 | PERROR("close"); |
| 223 | assert(0); |
| 224 | } |
| 225 | } |
| 226 | error_pipe: |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static |
| 231 | struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, |
| 232 | size_t memory_map_size) |
| 233 | { |
| 234 | struct shm_object *obj; |
| 235 | void *memory_map; |
| 236 | int waitfd[2], i, ret; |
| 237 | |
| 238 | if (table->allocated_len >= table->size) |
| 239 | return NULL; |
| 240 | obj = &table->objects[table->allocated_len]; |
| 241 | |
| 242 | memory_map = zmalloc(memory_map_size); |
| 243 | if (!memory_map) |
| 244 | goto alloc_error; |
| 245 | |
| 246 | /* wait_fd: create pipe */ |
| 247 | ret = pipe(waitfd); |
| 248 | if (ret < 0) { |
| 249 | PERROR("pipe"); |
| 250 | goto error_pipe; |
| 251 | } |
| 252 | for (i = 0; i < 2; i++) { |
| 253 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); |
| 254 | if (ret < 0) { |
| 255 | PERROR("fcntl"); |
| 256 | goto error_fcntl; |
| 257 | } |
| 258 | } |
| 259 | /* The write end of the pipe needs to be non-blocking */ |
| 260 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); |
| 261 | if (ret < 0) { |
| 262 | PERROR("fcntl"); |
| 263 | goto error_fcntl; |
| 264 | } |
| 265 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); |
| 266 | |
| 267 | /* no shm_fd */ |
| 268 | obj->shm_fd = -1; |
| 269 | |
| 270 | obj->type = SHM_OBJECT_MEM; |
| 271 | obj->memory_map = memory_map; |
| 272 | obj->memory_map_size = memory_map_size; |
| 273 | obj->allocated_len = 0; |
| 274 | obj->index = table->allocated_len++; |
| 275 | |
| 276 | return obj; |
| 277 | |
| 278 | error_fcntl: |
| 279 | for (i = 0; i < 2; i++) { |
| 280 | ret = close(waitfd[i]); |
| 281 | if (ret) { |
| 282 | PERROR("close"); |
| 283 | assert(0); |
| 284 | } |
| 285 | } |
| 286 | error_pipe: |
| 287 | free(memory_map); |
| 288 | alloc_error: |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | struct shm_object *shm_object_table_alloc(struct shm_object_table *table, |
| 293 | size_t memory_map_size, |
| 294 | enum shm_object_type type) |
| 295 | { |
| 296 | switch (type) { |
| 297 | case SHM_OBJECT_SHM: |
| 298 | return _shm_object_table_alloc_shm(table, memory_map_size); |
| 299 | case SHM_OBJECT_MEM: |
| 300 | return _shm_object_table_alloc_mem(table, memory_map_size); |
| 301 | default: |
| 302 | assert(0); |
| 303 | } |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | struct shm_object *shm_object_table_append_shm(struct shm_object_table *table, |
| 308 | int shm_fd, int wakeup_fd, uint32_t stream_nr, |
| 309 | size_t memory_map_size) |
| 310 | { |
| 311 | struct shm_object *obj; |
| 312 | char *memory_map; |
| 313 | int ret; |
| 314 | |
| 315 | if (table->allocated_len >= table->size) |
| 316 | return NULL; |
| 317 | /* streams _must_ be received in sequential order, else fail. */ |
| 318 | if (stream_nr + 1 != table->allocated_len) |
| 319 | return NULL; |
| 320 | |
| 321 | obj = &table->objects[table->allocated_len]; |
| 322 | |
| 323 | /* wait_fd: set write end of the pipe. */ |
| 324 | obj->wait_fd[0] = -1; /* read end is unset */ |
| 325 | obj->wait_fd[1] = wakeup_fd; |
| 326 | obj->shm_fd = shm_fd; |
| 327 | |
| 328 | ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); |
| 329 | if (ret < 0) { |
| 330 | PERROR("fcntl"); |
| 331 | goto error_fcntl; |
| 332 | } |
| 333 | /* The write end of the pipe needs to be non-blocking */ |
| 334 | ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK); |
| 335 | if (ret < 0) { |
| 336 | PERROR("fcntl"); |
| 337 | goto error_fcntl; |
| 338 | } |
| 339 | |
| 340 | /* memory_map: mmap */ |
| 341 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, |
| 342 | MAP_SHARED, shm_fd, 0); |
| 343 | if (memory_map == MAP_FAILED) { |
| 344 | PERROR("mmap"); |
| 345 | goto error_mmap; |
| 346 | } |
| 347 | obj->type = SHM_OBJECT_SHM; |
| 348 | obj->memory_map = memory_map; |
| 349 | obj->memory_map_size = memory_map_size; |
| 350 | obj->allocated_len = memory_map_size; |
| 351 | obj->index = table->allocated_len++; |
| 352 | |
| 353 | return obj; |
| 354 | |
| 355 | error_fcntl: |
| 356 | error_mmap: |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Passing ownership of mem to object. |
| 362 | */ |
| 363 | struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, |
| 364 | void *mem, size_t memory_map_size, int wakeup_fd) |
| 365 | { |
| 366 | struct shm_object *obj; |
| 367 | int ret; |
| 368 | |
| 369 | if (table->allocated_len >= table->size) |
| 370 | return NULL; |
| 371 | obj = &table->objects[table->allocated_len]; |
| 372 | |
| 373 | obj->wait_fd[0] = -1; /* read end is unset */ |
| 374 | obj->wait_fd[1] = wakeup_fd; |
| 375 | obj->shm_fd = -1; |
| 376 | |
| 377 | ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); |
| 378 | if (ret < 0) { |
| 379 | PERROR("fcntl"); |
| 380 | goto error_fcntl; |
| 381 | } |
| 382 | /* The write end of the pipe needs to be non-blocking */ |
| 383 | ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK); |
| 384 | if (ret < 0) { |
| 385 | PERROR("fcntl"); |
| 386 | goto error_fcntl; |
| 387 | } |
| 388 | |
| 389 | obj->type = SHM_OBJECT_MEM; |
| 390 | obj->memory_map = mem; |
| 391 | obj->memory_map_size = memory_map_size; |
| 392 | obj->allocated_len = memory_map_size; |
| 393 | obj->index = table->allocated_len++; |
| 394 | |
| 395 | return obj; |
| 396 | |
| 397 | error_fcntl: |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | static |
| 402 | void shmp_object_destroy(struct shm_object *obj) |
| 403 | { |
| 404 | switch (obj->type) { |
| 405 | case SHM_OBJECT_SHM: |
| 406 | { |
| 407 | int ret, i; |
| 408 | |
| 409 | ret = munmap(obj->memory_map, obj->memory_map_size); |
| 410 | if (ret) { |
| 411 | PERROR("umnmap"); |
| 412 | assert(0); |
| 413 | } |
| 414 | ret = close(obj->shm_fd); |
| 415 | if (ret) { |
| 416 | PERROR("close"); |
| 417 | assert(0); |
| 418 | } |
| 419 | for (i = 0; i < 2; i++) { |
| 420 | if (obj->wait_fd[i] < 0) |
| 421 | continue; |
| 422 | ret = close(obj->wait_fd[i]); |
| 423 | if (ret) { |
| 424 | PERROR("close"); |
| 425 | assert(0); |
| 426 | } |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case SHM_OBJECT_MEM: |
| 431 | { |
| 432 | int ret, i; |
| 433 | |
| 434 | for (i = 0; i < 2; i++) { |
| 435 | if (obj->wait_fd[i] < 0) |
| 436 | continue; |
| 437 | ret = close(obj->wait_fd[i]); |
| 438 | if (ret) { |
| 439 | PERROR("close"); |
| 440 | assert(0); |
| 441 | } |
| 442 | } |
| 443 | free(obj->memory_map); |
| 444 | break; |
| 445 | } |
| 446 | default: |
| 447 | assert(0); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void shm_object_table_destroy(struct shm_object_table *table) |
| 452 | { |
| 453 | int i; |
| 454 | |
| 455 | for (i = 0; i < table->allocated_len; i++) |
| 456 | shmp_object_destroy(&table->objects[i]); |
| 457 | free(table); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * zalloc_shm - allocate memory within a shm object. |
| 462 | * |
| 463 | * Shared memory is already zeroed by shmget. |
| 464 | * *NOT* multithread-safe (should be protected by mutex). |
| 465 | * Returns a -1, -1 tuple on error. |
| 466 | */ |
| 467 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len) |
| 468 | { |
| 469 | struct shm_ref ref; |
| 470 | struct shm_ref shm_ref_error = { -1, -1 }; |
| 471 | |
| 472 | if (obj->memory_map_size - obj->allocated_len < len) |
| 473 | return shm_ref_error; |
| 474 | ref.index = obj->index; |
| 475 | ref.offset = obj->allocated_len; |
| 476 | obj->allocated_len += len; |
| 477 | return ref; |
| 478 | } |
| 479 | |
| 480 | void align_shm(struct shm_object *obj, size_t align) |
| 481 | { |
| 482 | size_t offset_len = offset_align(obj->allocated_len, align); |
| 483 | obj->allocated_len += offset_len; |
| 484 | } |