Add application context support to lttng-ctl lttng_add_context
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index 0e7de6e5e4786d27383eec0481a48504a84b5c04..7a8b5ae42dcfedb4c7af6a159997d3eaa98dd02f 100644 (file)
@@ -17,7 +17,6 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
 #define _LGPL_SOURCE
 #include <getopt.h>
 #include <grp.h>
@@ -38,7 +37,6 @@
 #include <sys/wait.h>
 #include <urcu/uatomic.h>
 #include <unistd.h>
-#include <config.h>
 
 #include <common/common.h>
 #include <common/compat/socket.h>
@@ -49,7 +47,7 @@
 #include <common/relayd/relayd.h>
 #include <common/utils.h>
 #include <common/daemonize.h>
-#include <common/config/config.h>
+#include <common/config/session-config.h>
 
 #include "lttng-sessiond.h"
 #include "buffer-registry.h"
@@ -801,6 +799,8 @@ static void sessiond_cleanup_options(void)
        free(kmod_probes_list);
        free(kmod_extra_probes_list);
 
+       run_as_destroy_worker();
+
        /* <fun> */
        DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
                        "Matthew, BEET driven development works!%c[%dm",
@@ -1165,31 +1165,33 @@ static void *thread_manage_kernel(void *data)
                        }
 
                        /* Check for data on kernel pipe */
-                       if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
-                               (void) lttng_read(kernel_poll_pipe[0],
-                                       &tmp, 1);
-                               /*
-                                * Ret value is useless here, if this pipe gets any actions an
-                                * update is required anyway.
-                                */
-                               update_poll_flag = 1;
-                               continue;
-                       } else {
-                               /*
-                                * New CPU detected by the kernel. Adding kernel stream to
-                                * kernel session and updating the kernel consumer
-                                */
-                               if (revents & LPOLLIN) {
+                       if (revents & LPOLLIN) {
+                               if (pollfd == kernel_poll_pipe[0]) {
+                                       (void) lttng_read(kernel_poll_pipe[0],
+                                               &tmp, 1);
+                                       /*
+                                        * Ret value is useless here, if this pipe gets any actions an
+                                        * update is required anyway.
+                                        */
+                                       update_poll_flag = 1;
+                                       continue;
+                               } else {
+                                       /*
+                                        * New CPU detected by the kernel. Adding kernel stream to
+                                        * kernel session and updating the kernel consumer
+                                        */
                                        ret = update_kernel_stream(&kconsumer_data, pollfd);
                                        if (ret < 0) {
                                                continue;
                                        }
                                        break;
-                                       /*
-                                        * TODO: We might want to handle the LPOLLERR | LPOLLHUP
-                                        * and unregister kernel stream at this point.
-                                        */
                                }
+                       } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                               update_poll_flag = 1;
+                               continue;
+                       } else {
+                               ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                               goto error;
                        }
                }
        }
@@ -1319,9 +1321,14 @@ restart:
 
                /* Event on the registration socket */
                if (pollfd == consumer_data->err_sock) {
-                       if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                       if (revents & LPOLLIN) {
+                               continue;
+                       } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
                                ERR("consumer err socket poll error");
                                goto error;
+                       } else {
+                               ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                               goto error;
                        }
                }
        }
@@ -1451,7 +1458,8 @@ restart_poll:
 
                        if (pollfd == sock) {
                                /* Event on the consumerd socket */
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
+                                               && !(revents & LPOLLIN)) {
                                        ERR("consumer err socket second poll error");
                                        goto error;
                                }
@@ -1469,6 +1477,11 @@ restart_poll:
 
                                goto exit;
                        } else if (pollfd == consumer_data->metadata_fd) {
+                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
+                                               && !(revents & LPOLLIN)) {
+                                       ERR("consumer err metadata socket second poll error");
+                                       goto error;
+                               }
                                /* UST metadata requests */
                                ret = ust_consumer_metadata_request(
                                                &consumer_data->metadata_sock);
@@ -1636,10 +1649,7 @@ static void *thread_manage_apps(void *data)
 
                        /* Inspect the apps cmd pipe */
                        if (pollfd == apps_cmd_pipe[0]) {
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
-                                       ERR("Apps command pipe error");
-                                       goto error;
-                               } else if (revents & LPOLLIN) {
+                               if (revents & LPOLLIN) {
                                        int sock;
 
                                        /* Empty pipe */
@@ -1652,9 +1662,8 @@ static void *thread_manage_apps(void *data)
                                        health_code_update();
 
                                        /*
-                                        * We only monitor the error events of the socket. This
-                                        * thread does not handle any incoming data from UST
-                                        * (POLLIN).
+                                        * Since this is a command socket (write then read),
+                                        * we only monitor the error events of the socket.
                                         */
                                        ret = lttng_poll_add(&events, sock,
                                                        LPOLLERR | LPOLLHUP | LPOLLRDHUP);
@@ -1663,6 +1672,12 @@ static void *thread_manage_apps(void *data)
                                        }
 
                                        DBG("Apps with sock %d added to poll set", sock);
+                               } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                                       ERR("Apps command pipe error");
+                                       goto error;
+                               } else {
+                                       ERR("Unknown poll events %u for sock %d", revents, pollfd);
+                                       goto error;
                                }
                        } else {
                                /*
@@ -1678,6 +1693,9 @@ static void *thread_manage_apps(void *data)
 
                                        /* Socket closed on remote end. */
                                        ust_app_unregister(pollfd);
+                               } else {
+                                       ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                                       goto error;
                                }
                        }
 
@@ -1825,6 +1843,9 @@ static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
                                 */
                                wait_node = NULL;
                                break;
+                       } else {
+                               ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                               goto error;
                        }
                }
        }
@@ -2185,10 +2206,7 @@ static void *thread_registration_apps(void *data)
 
                        /* Event on the registration socket */
                        if (pollfd == apps_sock) {
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
-                                       ERR("Register apps socket poll error");
-                                       goto error;
-                               } else if (revents & LPOLLIN) {
+                               if (revents & LPOLLIN) {
                                        sock = lttcomm_accept_unix_sock(apps_sock);
                                        if (sock < 0) {
                                                goto error;
@@ -2275,6 +2293,12 @@ static void *thread_registration_apps(void *data)
                                         * barrier with the exchange in cds_wfcq_enqueue.
                                         */
                                        futex_nto1_wake(&ust_cmd_queue.futex);
+                               } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                                       ERR("Register apps socket poll error");
+                                       goto error;
+                               } else {
+                                       ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                                       goto error;
                                }
                        }
                }
@@ -3319,9 +3343,74 @@ skip_domain:
        switch (cmd_ctx->lsm->cmd_type) {
        case LTTNG_ADD_CONTEXT:
        {
-               ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
+               /*
+                * An LTTNG_ADD_CONTEXT command might have a supplementary
+                * payload if the context being added is an application context.
+                */
+               if (cmd_ctx->lsm->u.context.ctx.ctx ==
+                               LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
+                       char *provider_name = NULL, *context_name = NULL;
+                       size_t provider_name_len =
+                                       cmd_ctx->lsm->u.context.provider_name_len;
+                       size_t context_name_len =
+                                       cmd_ctx->lsm->u.context.context_name_len;
+
+                       if (provider_name_len == 0 || context_name_len == 0) {
+                               /*
+                                * Application provider and context names MUST
+                                * be provided.
+                                */
+                               ret = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+
+                       provider_name = zmalloc(provider_name_len + 1);
+                       if (!provider_name) {
+                               ret = -LTTNG_ERR_NOMEM;
+                               goto error;
+                       }
+
+                       context_name = zmalloc(context_name_len + 1);
+                       if (!context_name) {
+                               ret = -LTTNG_ERR_NOMEM;
+                               goto error_add_context;
+                       }
+
+                       ret = lttcomm_recv_unix_sock(sock, provider_name,
+                                       provider_name_len);
+                       if (ret < 0) {
+                               goto error_add_context;
+                       }
+
+                       ret = lttcomm_recv_unix_sock(sock, context_name,
+                                       context_name_len);
+                       if (ret < 0) {
+                               goto error_add_context;
+                       }
+                       cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
+                                       provider_name;
+                       cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
+                                       context_name;
+               }
+
+               /*
+                * cmd_add_context assumes ownership of the provider and context
+                * names.
+                */
+               ret = cmd_add_context(cmd_ctx->session,
+                               cmd_ctx->lsm->domain.type,
                                cmd_ctx->lsm->u.context.channel_name,
-                               &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
+                               &cmd_ctx->lsm->u.context.ctx,
+                               kernel_poll_pipe[1]);
+
+               cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
+               cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
+error_add_context:
+               free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
+               free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
+               if (ret < 0) {
+                       goto error;
+               }
                break;
        }
        case LTTNG_DISABLE_CHANNEL:
@@ -4177,9 +4266,14 @@ restart:
 
                        /* Event on the registration socket */
                        if (pollfd == sock) {
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                               if (revents & LPOLLIN) {
+                                       continue;
+                               } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
                                        ERR("Health socket poll error");
                                        goto error;
+                               } else {
+                                       ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                                       goto error;
                                }
                        }
                }
@@ -4354,9 +4448,14 @@ static void *thread_manage_clients(void *data)
 
                        /* Event on the registration socket */
                        if (pollfd == client_sock) {
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                               if (revents & LPOLLIN) {
+                                       continue;
+                               } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
                                        ERR("Client socket poll error");
                                        goto error;
+                               } else {
+                                       ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                                       goto error;
                                }
                        }
                }
@@ -4867,7 +4966,7 @@ end:
 
 /*
  * config_entry_handler_cb used to handle options read from a config file.
- * See config_entry_handler_cb comment in common/config/config.h for the
+ * See config_entry_handler_cb comment in common/config/session-config.h for the
  * return value conventions.
  */
 static int config_entry_handler(const struct config_entry *entry, void *unused)
@@ -5476,6 +5575,10 @@ int main(int argc, char **argv)
                }
        }
 
+       if (run_as_create_worker(argv[0]) < 0) {
+               goto exit_create_run_as_worker_cleanup;
+       }
+
        /*
         * Starting from here, we can create threads. This needs to be after
         * lttng_daemonize due to RCU.
@@ -6093,13 +6196,15 @@ exit_ht_cleanup_quit_pipe:
 
        health_app_destroy(health_sessiond);
 exit_health_sessiond_cleanup:
+exit_create_run_as_worker_cleanup:
 
 exit_options:
+       /* Ensure all prior call_rcu are done. */
+       rcu_barrier();
+
        sessiond_cleanup_options();
 
 exit_set_signal_handler:
-       /* Ensure all prior call_rcu are done. */
-       rcu_barrier();
 
        if (!retval) {
                exit(EXIT_SUCCESS);
This page took 0.028532 seconds and 4 git commands to generate.