ust-fd: Add close_range declaration
[lttng-ust.git] / src / common / ringbuffer / shm.c
index a5de019cd8dac4e91fb8fdbbab3101e4788c04da..347f9af0ab1d7aa6ca8353b0641daece84afd461 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "common/macros.h"
 #include "common/ust-fd.h"
-#include "mmap.h"
+#include "common/compat/mmap.h"
 
 /*
  * Ensure we have the required amount of space available by writing 0
@@ -69,12 +69,12 @@ error:
        return ret;
 }
 
-struct shm_object_table *shm_object_table_create(size_t max_nb_obj)
+struct shm_object_table *shm_object_table_create(size_t max_nb_obj, bool populate)
 {
        struct shm_object_table *table;
 
-       table = zmalloc(sizeof(struct shm_object_table) +
-                       max_nb_obj * sizeof(table->objects[0]));
+       table = zmalloc_populate(sizeof(struct shm_object_table) +
+                       max_nb_obj * sizeof(table->objects[0]), populate);
        if (!table)
                return NULL;
        table->size = max_nb_obj;
@@ -84,9 +84,11 @@ struct shm_object_table *shm_object_table_create(size_t max_nb_obj)
 static
 struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
                                           size_t memory_map_size,
-                                          int stream_fd)
+                                          int stream_fd,
+                                          bool populate)
 {
        int shmfd, waitfd[2], ret, i;
+       int flags = MAP_SHARED;
        struct shm_object *obj;
        char *memory_map;
 
@@ -97,18 +99,11 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
        obj = &table->objects[table->allocated_len];
 
        /* wait_fd: create pipe */
-       ret = pipe(waitfd);
+       ret = pipe2(waitfd, O_CLOEXEC);
        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) {
@@ -152,9 +147,11 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
        obj->shm_fd_ownership = 0;
        obj->shm_fd = shmfd;
 
+       if (populate)
+               flags |= LTTNG_MAP_POPULATE;
        /* memory_map: mmap */
        memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
-                         MAP_SHARED | LTTNG_MAP_POPULATE, shmfd, 0);
+                         flags, shmfd, 0);
        if (memory_map == MAP_FAILED) {
                PERROR("mmap");
                goto error_mmap;
@@ -185,7 +182,7 @@ error_pipe:
 
 static
 struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table,
-                                          size_t memory_map_size)
+                                          size_t memory_map_size, bool populate)
 {
        struct shm_object *obj;
        void *memory_map;
@@ -195,23 +192,16 @@ struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table,
                return NULL;
        obj = &table->objects[table->allocated_len];
 
-       memory_map = zmalloc(memory_map_size);
+       memory_map = zmalloc_populate(memory_map_size, populate);
        if (!memory_map)
                goto alloc_error;
 
        /* wait_fd: create pipe */
-       ret = pipe(waitfd);
+       ret = pipe2(waitfd, O_CLOEXEC);
        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) {
@@ -264,11 +254,21 @@ static bool lttng_is_numa_available(void)
 }
 #endif
 
+#ifdef HAVE_LIBNUMA
+struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
+                       size_t memory_map_size,
+                       enum shm_object_type type,
+                       int stream_fd,
+                       int cpu,
+                       bool populate)
+#else
 struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
                        size_t memory_map_size,
                        enum shm_object_type type,
                        int stream_fd,
-                       int cpu)
+                       int cpu __attribute__((unused)),
+                       bool populate)
+#endif
 {
        struct shm_object *shm_object;
 #ifdef HAVE_LIBNUMA
@@ -290,10 +290,11 @@ struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
        switch (type) {
        case SHM_OBJECT_SHM:
                shm_object = _shm_object_table_alloc_shm(table, memory_map_size,
-                               stream_fd);
+                               stream_fd, populate);
                break;
        case SHM_OBJECT_MEM:
-               shm_object = _shm_object_table_alloc_mem(table, memory_map_size);
+               shm_object = _shm_object_table_alloc_mem(table, memory_map_size,
+                               populate);
                break;
        default:
                assert(0);
@@ -307,8 +308,9 @@ struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
 
 struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
                        int shm_fd, int wakeup_fd, uint32_t stream_nr,
-                       size_t memory_map_size)
+                       size_t memory_map_size, bool populate)
 {
+       int flags = MAP_SHARED;
        struct shm_object *obj;
        char *memory_map;
        int ret;
@@ -334,9 +336,11 @@ struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
                goto error_fcntl;
        }
 
+       if (populate)
+               flags |= LTTNG_MAP_POPULATE;
        /* memory_map: mmap */
        memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
-                         MAP_SHARED | LTTNG_MAP_POPULATE, shm_fd, 0);
+                         flags, shm_fd, 0);
        if (memory_map == MAP_FAILED) {
                PERROR("mmap");
                goto error_mmap;
@@ -372,11 +376,6 @@ struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
        obj->shm_fd = -1;
        obj->shm_fd_ownership = 0;
 
-       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) {
This page took 0.028526 seconds and 4 git commands to generate.