From 99608320e6099c72850fa2df81e6f92fa38352d7 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Tue, 14 Jul 2020 13:00:06 -0400 Subject: [PATCH] sessiond: comm: add fd count information for lttng-ctl -> sessiond comm MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This allows bulk reception of fds incoming from lttng-ctl before doing any object deserialization. Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: I16f054be1d8b8d1e7868784c9b4088750b9a3651 --- src/bin/lttng-sessiond/cmd.c | 27 +++++++ src/common/sessiond-comm/sessiond-comm.h | 2 + src/lib/lttng-ctl/lttng-ctl.c | 96 +++++++++++++++++++----- 3 files changed, 106 insertions(+), 19 deletions(-) diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 37c0e31ce..1922fd594 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -4283,6 +4283,20 @@ int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, goto end; } + /* Receive fds, if any. */ + if (cmd_ctx->lsm.fd_count > 0) { + ret = lttcomm_recv_payload_fds_unix_sock( + sock, cmd_ctx->lsm.fd_count, &trigger_payload); + if (ret > 0 && ret != cmd_ctx->lsm.fd_count * sizeof(int)) { + ret = LTTNG_ERR_INVALID_PROTOCOL; + goto end; + } else if (ret <= 0) { + ret = LTTNG_ERR_FATAL; + goto end; + } + } + + /* Deserialize trigger. */ { struct lttng_payload_view view = lttng_payload_view_from_payload( @@ -4333,6 +4347,19 @@ int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, goto end; } + /* Receive fds, if any. */ + if (cmd_ctx->lsm.fd_count > 0) { + ret = lttcomm_recv_payload_fds_unix_sock( + sock, cmd_ctx->lsm.fd_count, &trigger_payload); + if (ret > 0 && ret != cmd_ctx->lsm.fd_count * sizeof(int)) { + ret = LTTNG_ERR_INVALID_PROTOCOL; + goto end; + } else if (ret <= 0) { + ret = LTTNG_ERR_FATAL; + goto end; + } + } + { struct lttng_payload_view view = lttng_payload_view_from_payload( diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index d054a8bd0..c15a26f88 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -416,6 +416,8 @@ struct lttcomm_session_msg { /* An lttng_session_descriptor follows. */ } LTTNG_PACKED create_session; } u; + /* Count of fds sent. */ + uint32_t fd_count; } LTTNG_PACKED; #define LTTNG_FILTER_MAX_LEN 65536 diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 700a83a13..81ddeb74a 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -3083,10 +3083,16 @@ end: int lttng_register_trigger(struct lttng_trigger *trigger) { int ret; - struct lttcomm_session_msg lsm; - struct lttng_payload payload; + struct lttcomm_session_msg lsm = { + .cmd_type = LTTNG_REGISTER_TRIGGER, + }; + struct lttcomm_session_msg *message_lsm; + struct lttng_payload message; + struct lttng_payload reply; + + lttng_payload_init(&message); + lttng_payload_init(&reply); - lttng_payload_init(&payload); if (!trigger) { ret = -LTTNG_ERR_INVALID; goto end; @@ -3097,19 +3103,39 @@ int lttng_register_trigger(struct lttng_trigger *trigger) goto end; } - ret = lttng_trigger_serialize(trigger, &payload); + lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); + + /* + * This is needed to populate the trigger object size for the command + * header. + */ + message_lsm = (struct lttcomm_session_msg *) message.buffer.data; + + ret = lttng_trigger_serialize(trigger, &message); if (ret < 0) { ret = -LTTNG_ERR_UNK; goto end; } - memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_REGISTER_TRIGGER; - lsm.u.trigger.length = (uint32_t) payload.buffer.size; - ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( - &lsm, payload.buffer.data, payload.buffer.size, NULL); + message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); + + { + struct lttng_payload_view message_view = + lttng_payload_view_from_payload( + &message, 0, -1); + + message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( + &message_view); + ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); + if (ret < 0) { + goto end; + } + } + + ret = 0; end: - lttng_payload_reset(&payload); + lttng_payload_reset(&message); + lttng_payload_reset(&reply); return ret; } @@ -3117,9 +3143,13 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger) { int ret; struct lttcomm_session_msg lsm; - struct lttng_payload payload; + struct lttcomm_session_msg *message_lsm; + struct lttng_payload message; + struct lttng_payload reply; + + lttng_payload_init(&message); + lttng_payload_init(&reply); - lttng_payload_init(&payload); if (!trigger) { ret = -LTTNG_ERR_INVALID; goto end; @@ -3130,19 +3160,47 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger) goto end; } - ret = lttng_trigger_serialize(trigger, &payload); + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; + + lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); + + /* + * This is needed to populate the trigger object size for the command + * header and number of fds sent. + */ + message_lsm = (struct lttcomm_session_msg *) message.buffer.data; + + ret = lttng_trigger_serialize(trigger, &message); if (ret < 0) { ret = -LTTNG_ERR_UNK; goto end; } - memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; - lsm.u.trigger.length = (uint32_t) payload.buffer.size; - ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( - &lsm, payload.buffer.data, payload.buffer.size, NULL); + message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); + + { + struct lttng_payload_view message_view = + lttng_payload_view_from_payload( + &message, 0, -1); + + /* + * Update the message header with the number of fd that will be + * sent. + */ + message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( + &message_view); + + ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); + if (ret < 0) { + goto end; + } + } + + ret = 0; end: - lttng_payload_reset(&payload); + lttng_payload_reset(&message); + lttng_payload_reset(&reply); return ret; } -- 2.34.1