sessiond: comm: add fd count information for lttng-ctl -> sessiond comm
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index b2f25ecd06ac58c471f94c4ea34daa2d30383cbb..81ddeb74a081c0f14c0979e9647194b9ff2e08df 100644 (file)
 #include <common/compat/string.h>
 #include <common/defaults.h>
 #include <common/dynamic-buffer.h>
+#include <common/dynamic-array.h>
 #include <common/payload.h>
 #include <common/payload-view.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/tracker.h>
+#include <common/unix.h>
 #include <common/uri.h>
 #include <common/utils.h>
 #include <lttng/channel-internal.h>
@@ -39,6 +41,7 @@
 #include <lttng/session-internal.h>
 #include <lttng/trigger/trigger-internal.h>
 #include <lttng/userspace-probe-internal.h>
+#include <lttng/lttng-error.h>
 
 #include "filter/filter-ast.h"
 #include "filter/filter-parser.h"
@@ -627,9 +630,10 @@ int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
 {
        int ret;
        struct lttcomm_lttng_msg llm;
+       const int fd_count = lttng_payload_view_get_fd_handle_count(message);
 
        assert(reply->buffer.size == 0);
-       assert(reply->_fds.size == 0);
+       assert(lttng_dynamic_pointer_array_get_count(&reply->_fd_handles) == 0);
 
        ret = connect_sessiond();
        if (ret < 0) {
@@ -648,10 +652,13 @@ int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
                goto end;
        }
 
-       if (lttng_payload_view_get_fd_count(message) > 0) {
-               ret = lttcomm_send_fds_unix_sock(sessiond_socket,
-                               (const int *) message->_fds.buffer.data,
-                               lttng_dynamic_array_get_count(&message->_fds));
+       if (fd_count > 0) {
+               ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket,
+                               message);
+               if (ret < 0) {
+                       ret = -LTTNG_ERR_FATAL;
+                       goto end;
+               }
        }
 
        /* Get header from data transmission */
@@ -685,19 +692,9 @@ int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
        }
 
        if (llm.fd_count > 0) {
-               ret = lttng_dynamic_array_set_count(&reply->_fds, llm.fd_count);
-               if (ret) {
-                       ret = -LTTNG_ERR_NOMEM;
-                       goto end;
-               }
-
-               ret = lttcomm_recv_fds_unix_sock(sessiond_socket,
-                               (int *) reply->_fds.buffer.data, llm.fd_count);
-               if (ret > 0 && ret != llm.fd_count * sizeof(int)) {
-                       ret = -LTTNG_ERR_INVALID_PROTOCOL;
-                       goto end;
-               } else if (ret <= 0) {
-                       ret = -LTTNG_ERR_FATAL;
+               ret = lttcomm_recv_payload_fds_unix_sock(
+                               sessiond_socket, llm.fd_count, reply);
+               if (ret < 0) {
                        goto end;
                }
        }
@@ -1360,7 +1357,7 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
        {
                struct lttng_payload_view view = lttng_payload_view_from_payload(
                        &payload, 0, -1);
-               int fd_count = lttng_payload_view_get_fd_count(&view);
+               int fd_count = lttng_payload_view_get_fd_handle_count(&view);
                int fd_to_send;
 
                if (fd_count < 0) {
@@ -1369,12 +1366,15 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
 
                assert(fd_count == 0 || fd_count == 1);
                if (fd_count == 1) {
-                       ret = lttng_payload_view_pop_fd(&view);
-                       if (ret < 0) {
+                       struct fd_handle *handle =
+                                       lttng_payload_view_pop_fd_handle(&view);
+
+                       if (!handle) {
                                goto mem_error;
                        }
 
-                       fd_to_send = ret;
+                       fd_to_send = fd_handle_get_fd(handle);
+                       fd_handle_put(handle);
                }
 
                ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
@@ -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;
 }
 
This page took 0.026208 seconds and 4 git commands to generate.