Backport: Fix: tracker: list/track/untrack commands leak strings
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index 44b88f39f5c89808ea0c02832b563a6644cd3a53..1cd77a4aed540f1b035c5292afd93b996acd901e 100644 (file)
@@ -49,6 +49,7 @@
 #include <common/utils.h>
 #include <common/daemonize.h>
 #include <common/config/session-config.h>
+#include <common/dynamic-buffer.h>
 
 #include "lttng-sessiond.h"
 #include "buffer-registry.h"
@@ -90,6 +91,7 @@ static pid_t ppid;          /* Parent PID for --sig-parent option */
 static pid_t child_ppid;    /* Internal parent PID use with daemonize. */
 static char *rundir;
 static int lockfile_fd = -1;
+static int opt_print_version;
 
 /* Set to 1 when a SIGUSR1 signal is received. */
 static int recv_child_signal;
@@ -309,12 +311,38 @@ struct load_session_thread_data *load_info;
 struct lttng_ht *agent_apps_ht_by_sock = NULL;
 
 /*
- * Whether sessiond is ready for commands/health check requests.
- * NR_LTTNG_SESSIOND_READY must match the number of calls to
- * sessiond_notify_ready().
+ * The initialization of the session daemon is done in multiple phases.
+ *
+ * While all threads are launched near-simultaneously, only some of them
+ * are needed to ensure the session daemon can start to respond to client
+ * requests.
+ *
+ * There are two important guarantees that we wish to offer with respect
+ * to the initialisation of the session daemon:
+ *   - When the daemonize/background launcher process exits, the sessiond
+ *     is fully able to respond to client requests,
+ *   - Auto-loaded sessions are visible to clients.
+ *
+ * In order to achieve this, a number of support threads have to be launched
+ * to allow the "client" thread to function properly. Moreover, since the
+ * "load session" thread needs the client thread, we must provide a way
+ * for the "load session" thread to know that the "client" thread is up
+ * and running.
+ *
+ * Hence, the support threads decrement the lttng_sessiond_ready counter
+ * while the "client" threads waits for it to reach 0. Once the "client" thread
+ * unblocks, it posts the message_thread_ready semaphore which allows the
+ * "load session" thread to progress.
+ *
+ * This implies that the "load session" thread is the last to be initialized
+ * and will explicitly call sessiond_signal_parents(), which signals the parents
+ * that the session daemon is fully initialized.
+ *
+ * The two (2) support threads are:
+ *  - agent_thread
+ *  - health_thread
  */
-#define NR_LTTNG_SESSIOND_READY                3
-int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
+int lttng_sessiond_ready = 2;
 
 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
 {
@@ -323,28 +351,36 @@ int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
 
 /* Notify parents that we are ready for cmd and health check */
 LTTNG_HIDDEN
-void sessiond_notify_ready(void)
+void sessiond_signal_parents(void)
 {
-       if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
-               /*
-                * Notify parent pid that we are ready to accept command
-                * for client side.  This ppid is the one from the
-                * external process that spawned us.
-                */
-               if (opt_sig_parent) {
-                       kill(ppid, SIGUSR1);
-               }
+       /*
+        * Notify parent pid that we are ready to accept command
+        * for client side.  This ppid is the one from the
+        * external process that spawned us.
+        */
+       if (opt_sig_parent) {
+               kill(ppid, SIGUSR1);
+       }
 
-               /*
-                * Notify the parent of the fork() process that we are
-                * ready.
-                */
-               if (opt_daemon || opt_background) {
-                       kill(child_ppid, SIGUSR1);
-               }
+       /*
+        * Notify the parent of the fork() process that we are
+        * ready.
+        */
+       if (opt_daemon || opt_background) {
+               kill(child_ppid, SIGUSR1);
        }
 }
 
+LTTNG_HIDDEN
+void sessiond_notify_ready(void)
+{
+       /*
+        * The _return variant is used since the implied memory barriers are
+        * required.
+        */
+       (void) uatomic_sub_return(&lttng_sessiond_ready, 1);
+}
+
 static
 void setup_consumerd_path(void)
 {
@@ -2526,7 +2562,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                        } else {
                                DBG("Could not find any valid consumerd executable");
                                ret = -EINVAL;
-                               break;
+                               goto error;
                        }
                        DBG("Using kernel consumer at: %s",  consumer_to_use);
                        ret = execl(consumer_to_use,
@@ -2623,7 +2659,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                        break;
                }
                default:
-                       PERROR("unknown consumer type");
+                       ERR("unknown consumer type");
                        exit(EXIT_FAILURE);
                }
                if (errno != 0) {
@@ -2716,7 +2752,6 @@ static int init_kernel_tracer(void)
        kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
        if (kernel_tracer_fd < 0) {
                DBG("Failed to open %s", module_proc_lttng);
-               ret = -1;
                goto error_open;
        }
 
@@ -3047,7 +3082,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
        case LTTNG_LIST_CHANNELS:
        case LTTNG_LIST_EVENTS:
        case LTTNG_LIST_SYSCALLS:
-       case LTTNG_LIST_TRACKER_PIDS:
+       case LTTNG_LIST_TRACKER_IDS:
        case LTTNG_DATA_PENDING:
                break;
        default:
@@ -3456,18 +3491,102 @@ error_add_context:
                                &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
                break;
        }
-       case LTTNG_TRACK_PID:
+       case LTTNG_TRACK_ID:
        {
-               ret = cmd_track_pid(cmd_ctx->session,
+               struct lttng_tracker_id id;
+
+               memset(&id, 0, sizeof(id));
+               id.type = cmd_ctx->lsm->u.id_tracker.id_type;
+               switch (id.type) {
+               case LTTNG_ID_ALL:
+                       break;
+               case LTTNG_ID_VALUE:
+                       id.value = cmd_ctx->lsm->u.id_tracker.u.value;
+                       break;
+               case LTTNG_ID_STRING:
+               {
+                       size_t var_len = cmd_ctx->lsm->u.id_tracker.u.var_len;
+
+                       id.string = zmalloc(var_len);
+                       if (!id.string) {
+                               ret = LTTNG_ERR_NOMEM;
+                               goto error;
+                       }
+                       DBG("Receiving var len tracker id string from client.");
+                       ret = lttcomm_recv_unix_sock(sock, id.string, var_len);
+                       if (ret <= 0) {
+                               DBG("Nothing received.");
+                               *sock_error = 1;
+                               free(id.string);
+                               ret = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       if (strnlen(id.string, var_len) != var_len - 1) {
+                               DBG("Corrupted string.");
+                               free(id.string);
+                               ret = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       break;
+               }
+               default:
+                       ret = LTTNG_ERR_INVALID;
+                       goto error;
+               }
+               ret = cmd_track_id(cmd_ctx->session,
+                               cmd_ctx->lsm->u.id_tracker.tracker_type,
                                cmd_ctx->lsm->domain.type,
-                               cmd_ctx->lsm->u.pid_tracker.pid);
+                               &id);
+               free(id.string);
                break;
        }
-       case LTTNG_UNTRACK_PID:
+       case LTTNG_UNTRACK_ID:
        {
-               ret = cmd_untrack_pid(cmd_ctx->session,
+               struct lttng_tracker_id id;
+
+               memset(&id, 0, sizeof(id));
+               id.type = cmd_ctx->lsm->u.id_tracker.id_type;
+               switch (id.type) {
+               case LTTNG_ID_ALL:
+                       break;
+               case LTTNG_ID_VALUE:
+                       id.value = cmd_ctx->lsm->u.id_tracker.u.value;
+                       break;
+               case LTTNG_ID_STRING:
+               {
+                       size_t var_len = cmd_ctx->lsm->u.id_tracker.u.var_len;
+
+                       id.string = zmalloc(var_len);
+                       if (!id.string) {
+                               ret = LTTNG_ERR_NOMEM;
+                               goto error;
+                       }
+                       DBG("Receiving var len tracker id string from client.");
+                       ret = lttcomm_recv_unix_sock(sock, id.string, var_len);
+                       if (ret <= 0) {
+                               DBG("Nothing received.");
+                               *sock_error = 1;
+                               free(id.string);
+                               ret = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       if (strnlen(id.string, var_len) != var_len - 1) {
+                               DBG("Corrupted string.");
+                               free(id.string);
+                               ret = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       break;
+               }
+               default:
+                       ret = LTTNG_ERR_INVALID;
+                       goto error;
+               }
+               ret = cmd_untrack_id(cmd_ctx->session,
+                               cmd_ctx->lsm->u.id_tracker.tracker_type,
                                cmd_ctx->lsm->domain.type,
-                               cmd_ctx->lsm->u.pid_tracker.pid);
+                               &id);
+               free(id.string);
                break;
        }
        case LTTNG_ENABLE_EVENT:
@@ -3666,27 +3785,61 @@ error_add_context:
                ret = LTTNG_OK;
                break;
        }
-       case LTTNG_LIST_TRACKER_PIDS:
+       case LTTNG_LIST_TRACKER_IDS:
        {
-               int32_t *pids = NULL;
-               ssize_t nr_pids;
-
-               nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
-                               cmd_ctx->lsm->domain.type, &pids);
-               if (nr_pids < 0) {
+               struct lttcomm_tracker_command_header cmd_header;
+               struct lttng_tracker_id *ids = NULL;
+               ssize_t nr_ids, i;
+               struct lttng_dynamic_buffer buf;
+
+               nr_ids = cmd_list_tracker_ids(cmd_ctx->lsm->u.id_tracker.tracker_type,
+                               cmd_ctx->session,
+                               cmd_ctx->lsm->domain.type, &ids);
+               if (nr_ids < 0) {
                        /* Return value is a negative lttng_error_code. */
-                       ret = -nr_pids;
+                       ret = -nr_ids;
                        goto error;
                }
 
-               /*
-                * Setup lttng message with payload size set to the event list size in
-                * bytes and then copy list into the llm payload.
-                */
-               ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids,
-                       sizeof(int32_t) * nr_pids);
-               free(pids);
+               lttng_dynamic_buffer_init(&buf);
+               for (i = 0; i < nr_ids; i++) {
+                       struct lttng_tracker_id *id = &ids[i];
+                       struct lttcomm_tracker_id_header id_hdr;
+                       size_t var_data_len = 0;
+
+                       memset(&id_hdr, 0, sizeof(id_hdr));
+                       id_hdr.type = id->type;
+                       switch (id->type) {
+                       case LTTNG_ID_ALL:
+                               break;
+                       case LTTNG_ID_VALUE:
+                               id_hdr.u.value = id->value;
+                               break;
+                       case LTTNG_ID_STRING:
+                               id_hdr.u.var_data_len = var_data_len = strlen(id->string) + 1;
+                               break;
+                       default:
+                               ret = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       ret = lttng_dynamic_buffer_append(&buf, &id_hdr, sizeof(id_hdr));
+                       if (ret) {
+                               ret = LTTNG_ERR_NOMEM;
+                               goto error;
+                       }
+                       ret = lttng_dynamic_buffer_append(&buf, id->string, var_data_len);
+                       if (ret) {
+                               ret = LTTNG_ERR_NOMEM;
+                               goto error;
+                       }
+                       free(id->string);
+               }
 
+               cmd_header.nb_tracker_id = nr_ids;
+               ret = setup_lttng_msg(cmd_ctx, buf.data, buf.size, &cmd_header,
+                               sizeof(cmd_header));
+               free(ids);
+               lttng_dynamic_buffer_reset(&buf);
                if (ret < 0) {
                        goto setup_error;
                }
@@ -4376,13 +4529,50 @@ static void *thread_manage_clients(void *data)
                goto error;
        }
 
-       sessiond_notify_ready();
        ret = sem_post(&load_info->message_thread_ready);
        if (ret) {
                PERROR("sem_post message_thread_ready");
                goto error;
        }
 
+       /*
+        * Wait until all support threads are initialized before accepting
+        * commands.
+        */
+       while (uatomic_read(&lttng_sessiond_ready) != 0) {
+               fd_set read_fds;
+               struct timeval timeout;
+
+               FD_ZERO(&read_fds);
+               FD_SET(thread_quit_pipe[0], &read_fds);
+               memset(&timeout, 0, sizeof(timeout));
+               timeout.tv_usec = 1000;
+
+               /*
+                * If a support thread failed to launch, it may signal that
+                * we must exit and the sessiond would never be marked as
+                * "ready".
+                *
+                * The timeout is set to 1ms, which serves as a way to
+                * pace down this check.
+                */
+               ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL,
+                               &timeout);
+               if (ret > 0 || (ret < 0 && errno != EINTR)) {
+                       goto exit;
+               }
+       }
+       /*
+        * This barrier is paired with the one in sessiond_notify_ready() to
+        * ensure that loads accessing data initialized by the other threads,
+        * on which this thread was waiting, are not performed before this point.
+        *
+        * Note that this could be a 'read' memory barrier, but a full barrier
+        * is used in case the code changes. The performance implications of
+        * this choice are minimal since this is a slow path.
+        */
+       cmm_smp_mb();
+
        /* This testpoint is after we signal readiness to the parent. */
        if (testpoint(sessiond_thread_manage_clients)) {
                goto error;
@@ -4690,8 +4880,7 @@ static int set_option(int opt, const char *arg, const char *optname)
                }
                exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
        } else if (string_match(optname, "version") || opt == 'V') {
-               fprintf(stdout, "%s\n", VERSION);
-               exit(EXIT_SUCCESS);
+               opt_print_version = 1;
        } else if (string_match(optname, "sig-parent") || opt == 'S') {
                opt_sig_parent = 1;
        } else if (string_match(optname, "kconsumerd-err-sock")) {
@@ -5031,6 +5220,23 @@ end:
        return ret;
 }
 
+static void sessiond_config_log(void)
+{
+       DBG("LTTng-sessiond " VERSION " - " VERSION_NAME "%s%s",
+                       GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION,
+                       EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME);
+       if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
+               DBG("LTTng-sessiond extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n");
+       }
+       if (EXTRA_VERSION_PATCHES[0] != '\0') {
+               DBG("LTTng-sessiond extra patches:\n\t" EXTRA_VERSION_PATCHES "\n");
+       }
+}
+
+static void print_version(void) {
+       fprintf(stdout, "%s\n", VERSION);
+}
+
 /*
  * daemon configuration loading and argument parsing
  */
@@ -5526,6 +5732,44 @@ error:
        return ret;
 }
 
+static int set_clock_plugin_env(void)
+{
+       int ret = 0;
+       const char *original_env_value;
+       char *full_path = NULL;
+       char *new_env_value = NULL;
+
+       original_env_value = getenv("LTTNG_UST_CLOCK_PLUGIN");
+       if (!original_env_value) {
+               goto end;
+       }
+
+       full_path = utils_expand_path(original_env_value);
+       if (!full_path) {
+               ERR("Failed to expand LTTNG_UST_CLOCK_PLUGIN path \"%s\"",
+                               original_env_value);
+               ret = -1;
+               goto end;
+       }
+        ret = asprintf(&new_env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
+                       full_path);
+       free(full_path);
+       if (ret < 0) {
+               PERROR("asprintf");
+               goto end;
+       }
+
+       DBG("Updating environment: %s", new_env_value);
+       ret = putenv(new_env_value);
+       if (ret) {
+               free(new_env_value);
+               PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
+               goto end;
+       }
+end:
+       return ret;
+}
+
 /*
  * main
  */
@@ -5567,6 +5811,20 @@ int main(int argc, char **argv)
                goto exit_options;
        }
 
+       sessiond_config_log();
+
+       if (opt_print_version) {
+               print_version();
+               retval = 0;
+               goto exit_options;
+       }
+
+       ret = set_clock_plugin_env();
+       if (ret) {
+               retval = -1;
+               goto exit_options;
+       }
+
        /* Daemonize */
        if (opt_daemon || opt_background) {
                int i;
This page took 0.031611 seconds and 5 git commands to generate.