SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
index 209931f5644a3d74dc27ed1a8f7dbb3d3863d387..cd9b273c28f32f36be201df68263100a11c7b7bb 100644 (file)
@@ -37,6 +37,7 @@
 #include <common/utils.h>
 #include <common/index/index.h>
 #include <common/consumer/consumer.h>
+#include <common/shm.h>
 #include <common/optional.h>
 
 #include "ust-consumer.h"
@@ -352,48 +353,6 @@ error_alloc:
        return ret;
 }
 
-/*
- * create_posix_shm is never called concurrently within a process.
- */
-static
-int create_posix_shm(void)
-{
-       char tmp_name[NAME_MAX];
-       int shmfd, ret;
-
-       ret = snprintf(tmp_name, NAME_MAX, "/ust-shm-consumer-%d", getpid());
-       if (ret < 0) {
-               PERROR("snprintf");
-               return -1;
-       }
-       /*
-        * Allocate shm, and immediately unlink its shm oject, keeping
-        * only the file descriptor as a reference to the object.
-        * 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).
-        */
-       shmfd = shm_open(tmp_name, O_CREAT | O_EXCL | O_RDWR, 0700);
-       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");
-       }
-error_shm_open:
-       return -1;
-}
-
 static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
                const struct lttng_credentials *session_credentials)
 {
@@ -401,7 +360,7 @@ static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
        int ret;
 
        if (!channel->shm_path[0]) {
-               return create_posix_shm();
+               return shm_create_anonymous("ust-consumer");
        }
        ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
        if (ret) {
@@ -409,7 +368,8 @@ static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
        }
        return run_as_open(shm_path,
                O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
-               session_credentials->uid, session_credentials->gid);
+               lttng_credentials_get_uid(session_credentials),
+               lttng_credentials_get_gid(session_credentials));
 
 error_shm_path:
        return -1;
@@ -487,8 +447,10 @@ error_open:
                                ERR("Cannot get stream shm path");
                        }
                        closeret = run_as_unlink(shm_path,
-                                       channel->buffer_credentials.value.uid,
-                                       channel->buffer_credentials.value.gid);
+                                       lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
+                                                       channel->buffer_credentials)),
+                                       lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
+                                                       channel->buffer_credentials)));
                        if (closeret) {
                                PERROR("unlink %s", shm_path);
                        }
@@ -497,8 +459,10 @@ error_open:
        /* Try to rmdir all directories under shm_path root. */
        if (channel->root_shm_path[0]) {
                (void) run_as_rmdir_recursive(channel->root_shm_path,
-                               channel->buffer_credentials.value.uid,
-                               channel->buffer_credentials.value.gid,
+                               lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
+                                               channel->buffer_credentials)),
+                               lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
+                                               channel->buffer_credentials)),
                                LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
        }
        free(stream_fds);
@@ -1461,8 +1425,8 @@ int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
                struct ustctl_consumer_channel_attr attr;
                const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
                const struct lttng_credentials buffer_credentials = {
-                       .uid = msg.u.ask_channel.buffer_credentials.uid,
-                       .gid = msg.u.ask_channel.buffer_credentials.gid,
+                       .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
+                       .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
                };
 
                /* Create a plain object and reserve a channel key. */
@@ -2072,8 +2036,8 @@ end_rotate_channel_nosignal:
        case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
        {
                const struct lttng_credentials credentials = {
-                       .uid = msg.u.create_trace_chunk.credentials.value.uid,
-                       .gid = msg.u.create_trace_chunk.credentials.value.gid,
+                       .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
+                       .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
                };
                const bool is_local_trace =
                                !msg.u.create_trace_chunk.relayd_id.is_set;
@@ -2402,8 +2366,10 @@ void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
                                ERR("Cannot get stream shm path");
                        }
                        ret = run_as_unlink(shm_path,
-                                       chan->buffer_credentials.value.uid,
-                                       chan->buffer_credentials.value.gid);
+                                       lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
+                                                       chan->buffer_credentials)),
+                                       lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
+                                                       chan->buffer_credentials)));
                        if (ret) {
                                PERROR("unlink %s", shm_path);
                        }
@@ -2422,8 +2388,10 @@ void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
        /* Try to rmdir all directories under shm_path root. */
        if (chan->root_shm_path[0]) {
                (void) run_as_rmdir_recursive(chan->root_shm_path,
-                               chan->buffer_credentials.value.uid,
-                               chan->buffer_credentials.value.gid,
+                               lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
+                                               chan->buffer_credentials)),
+                               lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
+                                               chan->buffer_credentials)),
                                LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
        }
        free(chan->stream_fds);
This page took 0.025784 seconds and 5 git commands to generate.