Basic serialization/deserialization for lttng_session
[lttng-tools.git] / src / bin / lttng-sessiond / client.cpp
index f28037dcbdf592fd6977dd4dbf0fc25b2f1a80b9..aafbd919ce07e73d0b071cac3ec3327996498201 100644 (file)
@@ -7,25 +7,25 @@
  *
  */
 
-#include "common/buffer-view.h"
-#include "common/compat/socket.h"
-#include "common/dynamic-array.h"
-#include "common/dynamic-buffer.h"
-#include "common/fd-handle.h"
-#include "common/payload-view.h"
-#include "common/payload.h"
-#include "common/sessiond-comm/sessiond-comm.h"
+#include "common/buffer-view.hpp"
+#include "common/compat/socket.hpp"
+#include "common/dynamic-array.hpp"
+#include "common/dynamic-buffer.hpp"
+#include "common/fd-handle.hpp"
+#include "common/payload-view.hpp"
+#include "common/payload.hpp"
+#include "common/sessiond-comm/sessiond-comm.hpp"
 #include "lttng/lttng-error.h"
 #include "lttng/tracker.h"
-#include <common/compat/getenv.h>
-#include <common/tracker.h>
-#include <common/unix.h>
-#include <common/utils.h>
-#include <lttng/error-query-internal.h>
-#include <lttng/event-internal.h>
-#include <lttng/session-descriptor-internal.h>
-#include <lttng/session-internal.h>
-#include <lttng/userspace-probe-internal.h>
+#include <common/compat/getenv.hpp>
+#include <common/tracker.hpp>
+#include <common/unix.hpp>
+#include <common/utils.hpp>
+#include <lttng/error-query-internal.hpp>
+#include <lttng/event-internal.hpp>
+#include <lttng/session-descriptor-internal.hpp>
+#include <lttng/session-internal.hpp>
+#include <lttng/userspace-probe-internal.hpp>
 #include <pthread.h>
 #include <signal.h>
 #include <stddef.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "agent-thread.h"
-#include "clear.h"
-#include "client.h"
-#include "cmd.h"
-#include "health-sessiond.h"
-#include "kernel.h"
-#include "lttng-sessiond.h"
-#include "manage-consumer.h"
-#include "save.h"
-#include "testpoint.h"
-#include "utils.h"
-
-static bool is_root;
-
-static struct thread_state {
+#include "agent-thread.hpp"
+#include "clear.hpp"
+#include "client.hpp"
+#include "cmd.hpp"
+#include "health-sessiond.hpp"
+#include "kernel.hpp"
+#include "lttng-sessiond.hpp"
+#include "manage-consumer.hpp"
+#include "save.hpp"
+#include "testpoint.hpp"
+#include "utils.hpp"
+
+namespace {
+bool is_root;
+
+struct thread_state {
        sem_t ready;
        bool running;
        int client_sock;
 } thread_state;
+} /* namespace */
 
 static void set_thread_status(bool running)
 {
@@ -264,7 +266,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                        tmp = "";
                                }
                                tmplen = strlen(the_config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
-                               tmpnew = (char *) zmalloc(tmplen + 1 /* \0 */);
+                               tmpnew = zmalloc<char>(tmplen + 1 /* \0 */);
                                if (!tmpnew) {
                                        ret = -ENOMEM;
                                        goto error;
@@ -306,7 +308,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                        tmp = "";
                                }
                                tmplen = strlen(the_config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
-                               tmpnew = (char *) zmalloc(tmplen + 1 /* \0 */);
+                               tmpnew = zmalloc<char>(tmplen + 1 /* \0 */);
                                if (!tmpnew) {
                                        ret = -ENOMEM;
                                        goto error;
@@ -580,32 +582,6 @@ error_create:
        return ret;
 }
 
-/*
- * Count number of session permitted by uid/gid.
- */
-static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
-{
-       unsigned int i = 0;
-       struct ltt_session *session;
-       const struct ltt_session_list *session_list = session_get_list();
-
-       DBG("Counting number of available session for UID %d", uid);
-       cds_list_for_each_entry(session, &session_list->head, list) {
-               if (!session_get(session)) {
-                       continue;
-               }
-               session_lock(session);
-               /* Only count the sessions the user can control. */
-               if (session_access_ok(session, uid) &&
-                               !session->destroyed) {
-                       i++;
-               }
-               session_unlock(session);
-               session_put(session);
-       }
-       return i;
-}
-
 static enum lttng_error_code receive_lttng_trigger(struct command_ctx *cmd_ctx,
                int sock,
                int *sock_error,
@@ -763,6 +739,10 @@ static enum lttng_error_code receive_lttng_event(struct command_ctx *cmd_ctx,
        ssize_t sock_recv_len;
        enum lttng_error_code ret_code;
        struct lttng_payload event_payload;
+       struct lttng_event *local_event = NULL;
+       char *local_filter_expression = NULL;
+       struct lttng_bytecode *local_bytecode = NULL;
+       struct lttng_event_exclusion *local_exclusion = NULL;
 
        lttng_payload_init(&event_payload);
        if (cmd_ctx->lsm.cmd_type == LTTNG_ENABLE_EVENT) {
@@ -810,23 +790,45 @@ static enum lttng_error_code receive_lttng_event(struct command_ctx *cmd_ctx,
 
        /* Deserialize event. */
        {
+               ssize_t len;
                struct lttng_payload_view event_view =
                                lttng_payload_view_from_payload(
                                                &event_payload, 0, -1);
 
-               if (lttng_event_create_from_payload(&event_view, out_event,
-                                   out_exclusion, out_filter_expression,
-                                   out_bytecode) != event_len) {
-                       ERR("Invalid event received as part of command payload");
+               len = lttng_event_create_from_payload(&event_view, &local_event,
+                                   &local_exclusion, &local_filter_expression,
+                                   &local_bytecode);
+
+               if (len < 0) {
+                       ERR("Failed to create an event from the received buffer");
+                       ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+                       goto end;
+               }
+
+               if (len != event_len) {
+                       ERR("Userspace probe location from the received buffer is not the advertised length: header length = %zu" PRIu32 ", payload length = %zd", event_len, len);
                        ret_code = LTTNG_ERR_INVALID_PROTOCOL;
                        goto end;
                }
        }
 
+       *out_event = local_event;
+       *out_exclusion = local_exclusion;
+       *out_filter_expression = local_filter_expression;
+       *out_bytecode = local_bytecode;
+       local_event = NULL;
+       local_exclusion = NULL;
+       local_filter_expression = NULL;
+       local_bytecode = NULL;
+
        ret_code = LTTNG_OK;
 
 end:
        lttng_payload_reset(&event_payload);
+       lttng_event_destroy(local_event);
+       free(local_filter_expression);
+       free(local_bytecode);
+       free(local_exclusion);
        return ret_code;
 }
 
@@ -842,6 +844,7 @@ static enum lttng_error_code receive_lttng_event_context(
        ssize_t sock_recv_len;
        enum lttng_error_code ret_code;
        struct lttng_payload event_context_payload;
+       struct lttng_event_context *context = NULL;
 
        lttng_payload_init(&event_context_payload);
 
@@ -864,22 +867,33 @@ static enum lttng_error_code receive_lttng_event_context(
 
        /* Deserialize event. */
        {
+               ssize_t len;
                struct lttng_payload_view event_context_view =
                                lttng_payload_view_from_payload(
                                                &event_context_payload, 0, -1);
 
-               if (lttng_event_context_create_from_payload(
-                               &event_context_view, out_event_context) !=
-                               event_context_len) {
-                       ERR("Invalid event context received as part of command payload");
+               len = lttng_event_context_create_from_payload(
+                               &event_context_view, &context);
+
+               if (len < 0) {
+                       ERR("Failed to create a event context from the received buffer");
+                       ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+                       goto end;
+               }
+
+               if (len != event_context_len) {
+                       ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd", event_context_len, len);
                        ret_code = LTTNG_ERR_INVALID_PROTOCOL;
                        goto end;
                }
        }
 
+       *out_event_context = context;
+       context = NULL;
        ret_code = LTTNG_OK;
 
 end:
+       lttng_event_context_destroy(context);
        lttng_payload_reset(&event_context_payload);
        return ret_code;
 }
@@ -1353,7 +1367,7 @@ skip_domain:
        switch (cmd_ctx->lsm.cmd_type) {
        case LTTNG_ADD_CONTEXT:
        {
-               struct lttng_event_context *event_context;
+               struct lttng_event_context *event_context = NULL;
                const enum lttng_error_code ret_code =
                        receive_lttng_event_context(
                                cmd_ctx, *sock, sock_error, &event_context);
@@ -1712,7 +1726,7 @@ skip_domain:
                        goto error;
                }
 
-               uris = (lttng_uri *) zmalloc(len);
+               uris = calloc<lttng_uri>(nb_uri);
                if (uris == NULL) {
                        ret = LTTNG_ERR_FATAL;
                        goto error;
@@ -1852,38 +1866,33 @@ skip_domain:
        }
        case LTTNG_LIST_SESSIONS:
        {
-               unsigned int nr_sessions;
-               lttng_session *sessions_payload;
-               size_t payload_len;
-
-               session_lock_list();
-               nr_sessions = lttng_sessions_count(
-                               LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
-                               LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
-
-               payload_len = (sizeof(struct lttng_session) * nr_sessions) +
-                               (sizeof(struct lttng_session_extended) * nr_sessions);
-               sessions_payload = (lttng_session *) zmalloc(payload_len);
+               enum lttng_error_code ret_code;
+               size_t original_payload_size;
+               size_t payload_size;
+               const size_t command_header_size = sizeof(struct lttcomm_list_command_header);
 
-               if (!sessions_payload) {
-                       session_unlock_list();
-                       ret = -ENOMEM;
+               ret = setup_empty_lttng_msg(cmd_ctx);
+               if (ret) {
+                       ret = LTTNG_ERR_NOMEM;
                        goto setup_error;
                }
 
-               cmd_list_lttng_sessions(sessions_payload, nr_sessions,
-                       LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
-                       LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
-               session_unlock_list();
-
-               ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
-                       payload_len);
-               free(sessions_payload);
+               original_payload_size = cmd_ctx->reply_payload.buffer.size;
 
-               if (ret < 0) {
-                       goto setup_error;
+               session_lock_list();
+               ret_code = cmd_list_lttng_sessions(&cmd_ctx->reply_payload,
+                               LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
+                               LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
+               session_unlock_list();
+               if (ret_code != LTTNG_OK) {
+                       ret = (int) ret_code;
+                       goto error;
                }
 
+               payload_size = cmd_ctx->reply_payload.buffer.size - command_header_size -
+                               original_payload_size;
+               update_lttng_msg(cmd_ctx, command_header_size, payload_size);
+
                ret = LTTNG_OK;
                break;
        }
@@ -2003,8 +2012,7 @@ skip_domain:
        {
                lttng_snapshot_output output = cmd_ctx->lsm.u.snapshot_record.output;
                ret = cmd_snapshot_record(cmd_ctx->session,
-                               &output,
-                               cmd_ctx->lsm.u.snapshot_record.wait);
+                               &output, 0); // RFC: set to zero since it's ignored by cmd_snapshot_record
                break;
        }
        case LTTNG_CREATE_SESSION_EXT:
@@ -2397,7 +2405,7 @@ static void cleanup_client_thread(void *data)
        lttng_pipe_destroy(quit_pipe);
 }
 
-static void thread_init_cleanup(void *data)
+static void thread_init_cleanup(void *data __attribute__((unused)))
 {
        set_thread_status(false);
 }
This page took 0.028771 seconds and 5 git commands to generate.