+static enum lttng_error_code receive_lttng_event(struct command_ctx *cmd_ctx,
+ int sock,
+ int *sock_error,
+ struct lttng_event **out_event,
+ char **out_filter_expression,
+ struct lttng_bytecode **out_bytecode,
+ struct lttng_event_exclusion **out_exclusion)
+{
+ int ret;
+ size_t event_len;
+ ssize_t sock_recv_len;
+ enum lttng_error_code ret_code;
+ struct lttng_payload event_payload;
+
+ lttng_payload_init(&event_payload);
+ if (cmd_ctx->lsm.cmd_type == LTTNG_ENABLE_EVENT) {
+ event_len = (size_t) cmd_ctx->lsm.u.enable.length;
+ } else if (cmd_ctx->lsm.cmd_type == LTTNG_DISABLE_EVENT) {
+ event_len = (size_t) cmd_ctx->lsm.u.disable.length;
+ } else {
+ abort();
+ }
+
+ ret = lttng_dynamic_buffer_set_size(&event_payload.buffer, event_len);
+ if (ret) {
+ ret_code = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ sock_recv_len = lttcomm_recv_unix_sock(
+ sock, event_payload.buffer.data, event_len);
+ if (sock_recv_len < 0 || sock_recv_len != event_len) {
+ ERR("Failed to receive event in command payload");
+ *sock_error = 1;
+ ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+ goto end;
+ }
+
+ /* Receive fds, if any. */
+ if (cmd_ctx->lsm.fd_count > 0) {
+ sock_recv_len = lttcomm_recv_payload_fds_unix_sock(
+ sock, cmd_ctx->lsm.fd_count, &event_payload);
+ if (sock_recv_len > 0 &&
+ sock_recv_len != cmd_ctx->lsm.fd_count * sizeof(int)) {
+ ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
+ cmd_ctx->lsm.fd_count, (int) ret);
+ ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+ *sock_error = 1;
+ goto end;
+ } else if (sock_recv_len <= 0) {
+ ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
+ cmd_ctx->lsm.fd_count, (int) ret);
+ ret_code = LTTNG_ERR_FATAL;
+ *sock_error = 1;
+ goto end;
+ }
+ }
+
+ /* Deserialize event. */
+ {
+ 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");
+ ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+ goto end;
+ }
+ }
+
+ ret_code = LTTNG_OK;
+
+end:
+ lttng_payload_reset(&event_payload);
+ return ret_code;
+}
+