X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=libringbuffer%2Fshm.c;h=3bf648d253bd4f00ad8791b34cecb3d75e0c19d2;hb=6d0c2f84f1ec7f09ecfc2da1bb6145c0b7b71e26;hp=85b1e4b7e6ce9b1c4ef1fc61e04a17dfb554c85a;hpb=74d81a6cca2cd4a7718bba9368f382f9f2fbba84;p=lttng-ust.git diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index 85b1e4b7..3bf648d2 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -77,6 +77,8 @@ struct shm_object_table *shm_object_table_create(size_t max_nb_obj) table = zmalloc(sizeof(struct shm_object_table) + max_nb_obj * sizeof(table->objects[0])); + if (!table) + return NULL; table->size = max_nb_obj; return table; } @@ -145,7 +147,7 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, * Using mktemp filename with O_CREAT | O_EXCL open * flags. */ - mktemp(tmp_name); + (void) mktemp(tmp_name); if (tmp_name[0] == '\0') { PERROR("mktemp"); goto error_shm_open; @@ -231,6 +233,7 @@ struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, { struct shm_object *obj; void *memory_map; + int waitfd[2], i, ret; if (table->allocated_len >= table->size) return NULL; @@ -240,8 +243,28 @@ struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, if (!memory_map) goto alloc_error; - obj->wait_fd[0] = -1; - obj->wait_fd[1] = -1; + /* wait_fd: create pipe */ + ret = pipe(waitfd); + if (ret < 0) { + PERROR("pipe"); + goto error_pipe; + } + for (i = 0; i < 2; i++) { + ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl"); + goto error_fcntl; + } + } + /* The write end of the pipe needs to be non-blocking */ + ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); + if (ret < 0) { + PERROR("fcntl"); + goto error_fcntl; + } + memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); + + /* no shm_fd */ obj->shm_fd = -1; obj->type = SHM_OBJECT_MEM; @@ -252,6 +275,16 @@ struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, return obj; +error_fcntl: + for (i = 0; i < 2; i++) { + ret = close(waitfd[i]); + if (ret) { + PERROR("close"); + assert(0); + } + } +error_pipe: + free(memory_map); alloc_error: return NULL; } @@ -328,18 +361,31 @@ error_mmap: * Passing ownership of mem to object. */ struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, - void *mem, size_t memory_map_size) + void *mem, size_t memory_map_size, int wakeup_fd) { struct shm_object *obj; + int ret; if (table->allocated_len >= table->size) return NULL; obj = &table->objects[table->allocated_len]; - obj->wait_fd[0] = -1; - obj->wait_fd[1] = -1; + obj->wait_fd[0] = -1; /* read end is unset */ + obj->wait_fd[1] = wakeup_fd; obj->shm_fd = -1; + ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl"); + goto error_fcntl; + } + /* The write end of the pipe needs to be non-blocking */ + ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK); + if (ret < 0) { + PERROR("fcntl"); + goto error_fcntl; + } + obj->type = SHM_OBJECT_MEM; obj->memory_map = mem; obj->memory_map_size = memory_map_size; @@ -347,6 +393,9 @@ struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, obj->index = table->allocated_len++; return obj; + +error_fcntl: + return NULL; } static @@ -379,8 +428,21 @@ void shmp_object_destroy(struct shm_object *obj) break; } case SHM_OBJECT_MEM: + { + int ret, i; + + for (i = 0; i < 2; i++) { + if (obj->wait_fd[i] < 0) + continue; + ret = close(obj->wait_fd[i]); + if (ret) { + PERROR("close"); + assert(0); + } + } free(obj->memory_map); break; + } default: assert(0); }