From: Mathieu Desnoyers Date: Tue, 16 Dec 2014 01:24:22 +0000 (-0500) Subject: Move file creation/unlink from liblttng-ust-ctl to consumerd X-Git-Tag: v2.7.0-rc1~51 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=5ea386c3b8e3566ec0b6710c82aae433de4d944a;p=lttng-ust.git Move file creation/unlink from liblttng-ust-ctl to consumerd Signed-off-by: Mathieu Desnoyers --- diff --git a/include/lttng/ust-ctl.h b/include/lttng/ust-ctl.h index 0add1791..78e0cf72 100644 --- a/include/lttng/ust-ctl.h +++ b/include/lttng/ust-ctl.h @@ -53,7 +53,6 @@ struct ustctl_consumer_channel_attr { enum lttng_ust_output output; /* splice, mmap */ uint32_t chan_id; /* channel ID */ unsigned char uuid[LTTNG_UST_UUID_LEN]; /* Trace session unique ID */ - char shm_path[PATH_MAX]; /* Shared memory path */ } LTTNG_PACKED; /* @@ -149,8 +148,11 @@ struct ustctl_consumer_channel; struct ustctl_consumer_stream; struct ustctl_consumer_channel_attr; +int ustctl_get_nr_stream_per_channel(void); + struct ustctl_consumer_channel * - ustctl_create_channel(struct ustctl_consumer_channel_attr *attr); + ustctl_create_channel(struct ustctl_consumer_channel_attr *attr, + const int *stream_fds, int nr_stream_fds); /* * Each stream created needs to be destroyed before calling * ustctl_destroy_channel(). diff --git a/include/lttng/ust-events.h b/include/lttng/ust-events.h index e4b4edeb..328a8753 100644 --- a/include/lttng/ust-events.h +++ b/include/lttng/ust-events.h @@ -440,7 +440,7 @@ struct lttng_channel_ops { unsigned int read_timer_interval, unsigned char *uuid, uint32_t chan_id, - const char *shm_path); + const int *stream_fds, int nr_stream_fds); void (*channel_destroy)(struct lttng_channel *chan); union { void *_deprecated1; diff --git a/liblttng-ust-ctl/ustctl.c b/liblttng-ust-ctl/ustctl.c index aaad99ac..ca17255d 100644 --- a/liblttng-ust-ctl/ustctl.c +++ b/liblttng-ust-ctl/ustctl.c @@ -942,8 +942,14 @@ error: /* Buffer operations */ +int ustctl_get_nr_stream_per_channel(void) +{ + return num_possible_cpus(); +} + struct ustctl_consumer_channel * - ustctl_create_channel(struct ustctl_consumer_channel_attr *attr) + ustctl_create_channel(struct ustctl_consumer_channel_attr *attr, + const int *stream_fds, int nr_stream_fds) { struct ustctl_consumer_channel *chan; const char *transport_name; @@ -996,7 +1002,7 @@ struct ustctl_consumer_channel * attr->switch_timer_interval, attr->read_timer_interval, attr->uuid, attr->chan_id, - attr->shm_path[0] == '\0' ? NULL : attr->shm_path); + stream_fds, nr_stream_fds); if (!chan->chan) { goto chan_error; } diff --git a/liblttng-ust/lttng-ring-buffer-client.h b/liblttng-ust/lttng-ring-buffer-client.h index 79085e57..c0ae0c84 100644 --- a/liblttng-ust/lttng-ring-buffer-client.h +++ b/liblttng-ust/lttng-ring-buffer-client.h @@ -543,7 +543,7 @@ struct lttng_channel *_channel_create(const char *name, unsigned int read_timer_interval, unsigned char *uuid, uint32_t chan_id, - const char *shm_path) + const int *stream_fds, int nr_stream_fds) { struct lttng_channel chan_priv_init; struct lttng_ust_shm_handle *handle; @@ -559,7 +559,7 @@ struct lttng_channel *_channel_create(const char *name, &chan_priv_init, buf_addr, subbuf_size, num_subbuf, switch_timer_interval, read_timer_interval, - shm_path); + stream_fds, nr_stream_fds); if (!handle) return NULL; lttng_chan = priv; diff --git a/liblttng-ust/lttng-ring-buffer-metadata-client.h b/liblttng-ust/lttng-ring-buffer-metadata-client.h index 8d725f2a..921549fa 100644 --- a/liblttng-ust/lttng-ring-buffer-metadata-client.h +++ b/liblttng-ust/lttng-ring-buffer-metadata-client.h @@ -200,7 +200,7 @@ struct lttng_channel *_channel_create(const char *name, unsigned int read_timer_interval, unsigned char *uuid, uint32_t chan_id, - const char *shm_path) + const int *stream_fds, int nr_stream_fds) { struct lttng_channel chan_priv_init; struct lttng_ust_shm_handle *handle; @@ -216,7 +216,7 @@ struct lttng_channel *_channel_create(const char *name, &chan_priv_init, buf_addr, subbuf_size, num_subbuf, switch_timer_interval, read_timer_interval, - shm_path); + stream_fds, nr_stream_fds); if (!handle) return NULL; lttng_chan = priv; diff --git a/libringbuffer/backend_internal.h b/libringbuffer/backend_internal.h index 3c09382e..344784fe 100644 --- a/libringbuffer/backend_internal.h +++ b/libringbuffer/backend_internal.h @@ -46,7 +46,7 @@ int channel_backend_init(struct channel_backend *chanb, const struct lttng_ust_lib_ring_buffer_config *config, size_t subbuf_size, size_t num_subbuf, struct lttng_ust_shm_handle *handle, - const char *shm_path); + const int *stream_fds); void channel_backend_free(struct channel_backend *chanb, struct lttng_ust_shm_handle *handle); diff --git a/libringbuffer/frontend.h b/libringbuffer/frontend.h index 77009077..8c851708 100644 --- a/libringbuffer/frontend.h +++ b/libringbuffer/frontend.h @@ -65,7 +65,7 @@ struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buff size_t subbuf_size, size_t num_subbuf, unsigned int switch_timer_interval, unsigned int read_timer_interval, - const char *shm_path); + const int *stream_fds, int nr_stream_fds); /* * channel_destroy finalizes all channel's buffers, waits for readers to diff --git a/libringbuffer/ring_buffer_backend.c b/libringbuffer/ring_buffer_backend.c index 3a667adc..b65105f3 100644 --- a/libringbuffer/ring_buffer_backend.c +++ b/libringbuffer/ring_buffer_backend.c @@ -29,8 +29,6 @@ #include "smp.h" #include "shm.h" -#define UINT_MAX_STR_LEN 11 /* includes \0 */ - /** * lib_ring_buffer_backend_allocate - allocate a channel buffer * @config: ring buffer instance configuration @@ -200,7 +198,7 @@ void channel_backend_reset(struct channel_backend *chanb) * @subbuf_size: size of sub-buffers (> PAGE_SIZE, power of 2) * @num_subbuf: number of sub-buffers (power of 2) * @lttng_ust_shm_handle: shared memory handle - * @shm_path: shared memory files path + * @stream_fds: stream file descriptors. * * Returns channel pointer if successful, %NULL otherwise. * @@ -215,7 +213,7 @@ int channel_backend_init(struct channel_backend *chanb, const struct lttng_ust_lib_ring_buffer_config *config, size_t subbuf_size, size_t num_subbuf, struct lttng_ust_shm_handle *handle, - const char *shm_path) + const int *stream_fds) { struct channel *chan = caa_container_of(chanb, struct channel, backend); unsigned int i; @@ -288,21 +286,9 @@ int channel_backend_init(struct channel_backend *chanb, */ for_each_possible_cpu(i) { struct shm_object *shmobj; - char shm_buf_path[PATH_MAX]; - - if (shm_path) { - char cpu_nr[UINT_MAX_STR_LEN]; /* unsigned int max len */ - - strncpy(shm_buf_path, shm_path, PATH_MAX); - shm_buf_path[PATH_MAX - 1] = '\0'; - ret = snprintf(cpu_nr, UINT_MAX_STR_LEN, "%u", i); - if (ret != 1) - goto end; - strncat(shm_buf_path, cpu_nr, - PATH_MAX - strlen(shm_buf_path) - 1); - } + shmobj = shm_object_table_alloc(handle->table, shmsize, - SHM_OBJECT_SHM, shm_path ? shm_buf_path : NULL); + SHM_OBJECT_SHM, stream_fds[i]); if (!shmobj) goto end; align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer)); @@ -321,7 +307,7 @@ int channel_backend_init(struct channel_backend *chanb, struct lttng_ust_lib_ring_buffer *buf; shmobj = shm_object_table_alloc(handle->table, shmsize, - SHM_OBJECT_SHM, shm_path); + SHM_OBJECT_SHM, stream_fds[0]); if (!shmobj) goto end; align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer)); diff --git a/libringbuffer/ring_buffer_frontend.c b/libringbuffer/ring_buffer_frontend.c index 3cba68d4..45d46054 100644 --- a/libringbuffer/ring_buffer_frontend.c +++ b/libringbuffer/ring_buffer_frontend.c @@ -773,7 +773,8 @@ static void channel_free(struct channel *chan, * padding to let readers get those sub-buffers. * Used for live streaming. * @read_timer_interval: Time interval (in us) to wake up pending readers. - * @shm_path: Shared memory files path. + * @stream_fds: array of stream file descriptors. + * @nr_stream_fds: number of file descriptors in array. * * Holds cpu hotplug. * Returns NULL on failure. @@ -787,7 +788,7 @@ struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buff void *buf_addr, size_t subbuf_size, size_t num_subbuf, unsigned int switch_timer_interval, unsigned int read_timer_interval, - const char *shm_path) + const int *stream_fds, int nr_stream_fds) { int ret; size_t shmsize, chansize; @@ -801,6 +802,9 @@ struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buff else nr_streams = 1; + if (nr_stream_fds != nr_streams) + return NULL; + if (lib_ring_buffer_check_config(config, switch_timer_interval, read_timer_interval)) return NULL; @@ -825,7 +829,7 @@ struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buff /* Allocate normal memory for channel (not shared) */ shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM, - NULL); + -1); if (!shmobj) goto error_append; /* struct channel is at object 0, offset 0 (hardcoded) */ @@ -856,7 +860,7 @@ struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buff ret = channel_backend_init(&chan->backend, name, config, subbuf_size, num_subbuf, handle, - shm_path); + stream_fds); if (ret) goto error_backend_init; diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index fb2df13b..53b2b6ec 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -83,71 +83,17 @@ struct shm_object_table *shm_object_table_create(size_t max_nb_obj) return table; } -static -int create_posix_shm(void) -{ - char tmp_name[NAME_MAX] = "/ust-shm-tmp-XXXXXX"; - int shmfd, ret; - - /* - * Allocate shm, and immediately unlink its shm oject, keeping - * only the file descriptor as a reference to the object. If it - * already exists (caused by short race window during which the - * global object exists in a concurrent shm_open), simply retry. - * We specifically do _not_ use the / at the beginning of the - * pathname so that some OS implementations can keep it local to - * the process (POSIX leaves this implementation-defined). - */ - do { - /* - * Using mktemp filename with O_CREAT | O_EXCL open - * flags. - */ - (void) mktemp(tmp_name); - if (tmp_name[0] == '\0') { - PERROR("mktemp"); - goto error_shm_open; - } - shmfd = shm_open(tmp_name, - O_CREAT | O_EXCL | O_RDWR, 0700); - } while (shmfd < 0 && (errno == EEXIST || errno == EACCES)); - if (shmfd < 0) { - PERROR("shm_open"); - goto error_shm_open; - } - ret = shm_unlink(tmp_name); - if (ret < 0 && errno != ENOENT) { - PERROR("shm_unlink"); - goto error_shm_release; - } - return shmfd; - -error_shm_release: - ret = close(shmfd); - if (ret) { - PERROR("close"); - assert(0); - } -error_shm_open: - return -1; -} - -static -int create_shared_file(const char *shm_path) -{ - return open(shm_path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); -} - static struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, size_t memory_map_size, - const char *shm_path) + int stream_fd) { - int shmfd, waitfd[2], ret, i, sigblocked = 0; + int shmfd, waitfd[2], ret, i; struct shm_object *obj; char *memory_map; - sigset_t all_sigs, orig_sigs; + if (stream_fd < 0) + return NULL; if (table->allocated_len >= table->size) return NULL; obj = &table->objects[table->allocated_len]; @@ -173,42 +119,9 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, } memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); - /* shm_fd: create shm */ - - /* - * Theoretically, we could leak a shm if the application crashes - * between open and unlink. Disable signals on this thread for - * increased safety against this scenario. - */ - sigfillset(&all_sigs); - ret = pthread_sigmask(SIG_BLOCK, &all_sigs, &orig_sigs); - if (ret == -1) { - PERROR("pthread_sigmask"); - goto error_pthread_sigmask; - } - sigblocked = 1; - + /* create shm */ - if (!shm_path) { - obj->shm_path[0] = '\0'; - shmfd = create_posix_shm(); - } else { - strncpy(obj->shm_path, shm_path, - sizeof(obj->shm_path)); - obj->shm_path[sizeof(obj->shm_path) - 1] = '\0'; - - /* Path should already exist, but could fail. */ - shmfd = create_shared_file(shm_path); - } - if (shmfd < 0) - goto error_shm_open; - - sigblocked = 0; - ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); - if (ret == -1) { - PERROR("pthread_sigmask"); - goto error_sigmask_release; - } + shmfd = stream_fd; ret = zero_file(shmfd, memory_map_size); if (ret) { PERROR("zero_file"); @@ -219,6 +132,7 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, PERROR("ftruncate"); goto error_ftruncate; } + obj->shm_fd_ownership = 0; obj->shm_fd = shmfd; /* memory_map: mmap */ @@ -239,26 +153,6 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, error_mmap: error_ftruncate: error_zero_file: -error_sigmask_release: - ret = close(shmfd); - if (ret) { - PERROR("close"); - assert(0); - } - if (shm_path) { - ret = unlink(shm_path); - if (ret) { - PERROR("ret"); - } - } -error_shm_open: - if (sigblocked) { - ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL); - if (ret == -1) { - PERROR("pthread_sigmask"); - } - } -error_pthread_sigmask: error_fcntl: for (i = 0; i < 2; i++) { ret = close(waitfd[i]); @@ -310,6 +204,7 @@ struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, /* no shm_fd */ obj->shm_fd = -1; + obj->shm_fd_ownership = 0; obj->type = SHM_OBJECT_MEM; obj->memory_map = memory_map; @@ -336,12 +231,12 @@ alloc_error: struct shm_object *shm_object_table_alloc(struct shm_object_table *table, size_t memory_map_size, enum shm_object_type type, - const char *shm_path) + int stream_fd) { switch (type) { case SHM_OBJECT_SHM: return _shm_object_table_alloc_shm(table, memory_map_size, - shm_path); + stream_fd); case SHM_OBJECT_MEM: return _shm_object_table_alloc_mem(table, memory_map_size); default: @@ -370,6 +265,7 @@ struct shm_object *shm_object_table_append_shm(struct shm_object_table *table, obj->wait_fd[0] = -1; /* read end is unset */ obj->wait_fd[1] = wakeup_fd; obj->shm_fd = shm_fd; + obj->shm_fd_ownership = 1; ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); if (ret < 0) { @@ -419,6 +315,7 @@ struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, obj->wait_fd[0] = -1; /* read end is unset */ obj->wait_fd[1] = wakeup_fd; obj->shm_fd = -1; + obj->shm_fd_ownership = 0; ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); if (ret < 0) { @@ -457,15 +354,11 @@ void shmp_object_destroy(struct shm_object *obj) PERROR("umnmap"); assert(0); } - ret = close(obj->shm_fd); - if (ret) { - PERROR("close"); - assert(0); - } - if (obj->shm_path[0]) { - ret = unlink(obj->shm_path); + if (obj->shm_fd_ownership) { + ret = close(obj->shm_fd); if (ret) { - PERROR("ret"); + PERROR("close"); + assert(0); } } for (i = 0; i < 2; i++) { diff --git a/libringbuffer/shm.h b/libringbuffer/shm.h index 9253e952..a6b9ed8a 100644 --- a/libringbuffer/shm.h +++ b/libringbuffer/shm.h @@ -89,7 +89,7 @@ struct shm_object_table *shm_object_table_create(size_t max_nb_obj); struct shm_object *shm_object_table_alloc(struct shm_object_table *table, size_t memory_map_size, enum shm_object_type type, - const char *shm_path); + const int stream_fd); 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); diff --git a/libringbuffer/shm_types.h b/libringbuffer/shm_types.h index e40ba5a5..11076d29 100644 --- a/libringbuffer/shm_types.h +++ b/libringbuffer/shm_types.h @@ -40,7 +40,7 @@ struct shm_object { char *memory_map; size_t memory_map_size; uint64_t allocated_len; - char shm_path[PATH_MAX]; + int shm_fd_ownership; }; struct shm_object_table {