relayd: Add backward compatibility for --group-output-by-session
[lttng-tools.git] / src / bin / lttng-relayd / main.c
index 09e1b796346286987ee522c8c96980d52c16ba1c..325be9cf124df7d495fd3a27863195bebb9c55c4 100644 (file)
@@ -40,6 +40,7 @@
 #include <urcu/rculist.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <strings.h>
 
 #include <lttng/lttng.h>
 #include <common/common.h>
 #include <common/buffer-view.h>
 #include <common/string-utils/format.h>
 
+#include "backward-compatibility-group-by.h"
 #include "cmd.h"
+#include "connection.h"
 #include "ctf-trace.h"
+#include "health-relayd.h"
 #include "index.h"
-#include "utils.h"
-#include "lttng-relayd.h"
 #include "live.h"
-#include "health-relayd.h"
-#include "testpoint.h"
-#include "viewer-stream.h"
+#include "lttng-relayd.h"
 #include "session.h"
+#include "sessiond-trace-chunks.h"
 #include "stream.h"
-#include "connection.h"
-#include "tracefile-array.h"
 #include "tcp_keep_alive.h"
-#include "sessiond-trace-chunks.h"
+#include "testpoint.h"
+#include "tracefile-array.h"
+#include "utils.h"
+#include "version.h"
+#include "viewer-stream.h"
 
 static const char *help_msg =
 #ifdef LTTNG_EMBED_HELP
@@ -94,8 +97,9 @@ enum relay_connection_status {
 };
 
 /* command line options */
-char *opt_output_path;
-static int opt_daemon, opt_background;
+char *opt_output_path, *opt_working_directory;
+static int opt_daemon, opt_background, opt_print_version;
+enum relay_group_output_by opt_group_output_by = RELAYD_GROUP_OUTPUT_BY_UNKNOWN;
 
 /*
  * We need to wait for listener and live listener threads, as well as
@@ -182,11 +186,31 @@ static struct option long_options[] = {
        { "verbose", 0, 0, 'v', },
        { "config", 1, 0, 'f' },
        { "version", 0, 0, 'V' },
+       { "working-directory", 1, 0, 'w', },
+       { "group-output-by-session", 0, 0, 's', },
+       { "group-output-by-host", 0, 0, 'p', },
        { NULL, 0, 0, 0, },
 };
 
 static const char *config_ignore_options[] = { "help", "config", "version" };
 
+static void print_version(void) {
+       fprintf(stdout, "%s\n", VERSION);
+}
+
+static void relayd_config_log(void)
+{
+       DBG("LTTng-relayd " 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-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n");
+       }
+       if (EXTRA_VERSION_PATCHES[0] != '\0') {
+               DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES "\n");
+       }
+}
+
 /*
  * Take an option from the getopt output and set it in the right variable to be
  * used later.
@@ -277,8 +301,8 @@ static int set_option(int opt, const char *arg, const char *optname)
                }
                exit(EXIT_FAILURE);
        case 'V':
-               fprintf(stdout, "%s\n", VERSION);
-               exit(EXIT_SUCCESS);
+               opt_print_version = 1;
+               break;
        case 'o':
                if (lttng_is_setuid_setgid()) {
                        WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
@@ -292,6 +316,20 @@ static int set_option(int opt, const char *arg, const char *optname)
                        }
                }
                break;
+       case 'w':
+               if (lttng_is_setuid_setgid()) {
+                       WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+                               "-w, --working-directory");
+               } else {
+                       ret = asprintf(&opt_working_directory, "%s", arg);
+                       if (ret < 0) {
+                               ret = -errno;
+                               PERROR("asprintf opt_working_directory");
+                               goto end;
+                       }
+               }
+               break;
+
        case 'v':
                /* Verbose level can increase using multiple -v */
                if (arg) {
@@ -303,6 +341,20 @@ static int set_option(int opt, const char *arg, const char *optname)
                        }
                }
                break;
+       case 's':
+               if (opt_group_output_by != RELAYD_GROUP_OUTPUT_BY_UNKNOWN) {
+                       ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
+                       exit(EXIT_FAILURE);
+               }
+               opt_group_output_by = RELAYD_GROUP_OUTPUT_BY_SESSION;
+               break;
+       case 'p':
+               if (opt_group_output_by != RELAYD_GROUP_OUTPUT_BY_UNKNOWN) {
+                       ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
+                       exit(EXIT_FAILURE);
+               }
+               opt_group_output_by = RELAYD_GROUP_OUTPUT_BY_HOST;
+               break;
        default:
                /* Unknown option or other error.
                 * Error is printed by getopt, just return */
@@ -372,6 +424,23 @@ end:
        return ret;
 }
 
+static int parse_env_options(void)
+{
+       int ret = 0;
+       char *value = NULL;
+
+       value = lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV);
+       if (value) {
+               opt_working_directory = strdup(value);
+               if (!opt_working_directory) {
+                       ERR("Failed to allocate working directory string (\"%s\")",
+                                       value);
+                       ret = -1;
+               }
+       }
+       return ret;
+}
+
 static int set_options(int argc, char **argv)
 {
        int c, ret = 0, option_index = 0, retval = 0;
@@ -490,6 +559,10 @@ static int set_options(int argc, char **argv)
                }
        }
 
+       if (opt_group_output_by == RELAYD_GROUP_OUTPUT_BY_UNKNOWN) {
+               opt_group_output_by = RELAYD_GROUP_OUTPUT_BY_HOST;
+       }
+
 exit:
        free(optstring);
        return retval;
@@ -522,8 +595,8 @@ static void relayd_cleanup(void)
        if (sessions_ht)
                lttng_ht_destroy(sessions_ht);
 
-       /* free the dynamically allocated opt_output_path */
        free(opt_output_path);
+       free(opt_working_directory);
 
        /* Close thread quit pipes */
        utils_close_pipe(thread_quit_pipe);
@@ -1156,21 +1229,24 @@ send_reply:
                }
        } else {
                const uint32_t output_path_length =
-                               strlen(session->output_path) + 1;
+                               session ? strlen(session->output_path) + 1 : 0;
 
                reply.output_path_length = htobe32(output_path_length);
-               ret = lttng_dynamic_buffer_append(&reply_payload, &reply,
-                               sizeof(reply));
+               ret = lttng_dynamic_buffer_append(
+                               &reply_payload, &reply, sizeof(reply));
                if (ret) {
                        ERR("Failed to append \"create session\" command reply header to payload buffer");
                        goto end;
                }
 
-               ret = lttng_dynamic_buffer_append(&reply_payload,
-                               session->output_path, output_path_length);
-               if (ret) {
-                       ERR("Failed to append \"create session\" command reply path to payload buffer");
-                       goto end;
+               if (output_path_length) {
+                       ret = lttng_dynamic_buffer_append(&reply_payload,
+                                       session->output_path,
+                                       output_path_length);
+                       if (ret) {
+                               ERR("Failed to append \"create session\" command reply path to payload buffer");
+                               goto end;
+                       }
                }
        }
 
@@ -1295,12 +1371,57 @@ static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr,
                goto send_reply;
        }
 
+       /*
+        * Backward compatibility for --group-output-by-session.
+        * Prior to lttng 2.11, the complete path is passed by the stream.
+        * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
+        * >=2.11 the chunk is responsible for the output path. When dealing
+        * with producer < 2.11 the chunk output_path is the root output path
+        * and the stream carries the complete path (path_name).
+        * To support --group-output-by-session with older producer (<2.11), we
+        * need to craft the path based on the stream path.
+        */
+       if (opt_group_output_by == RELAYD_GROUP_OUTPUT_BY_SESSION) {
+               if (conn->minor < 4) {
+                       /*
+                        * From 2.1 to 2.3, the session_name is not passed on
+                        * the RELAYD_CREATE_SESSION command. The session name
+                        * is necessary to detect the presence of a base_path
+                        * inside the stream path. Without it we cannot perform
+                        * a valid group-output-by-session transformation.
+                        */
+                       WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
+                            " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
+                                       session->id, path_name);
+               } else if (conn->minor >= 4 && conn->minor < 11) {
+                       char *group_by_session_path_name;
+
+                       assert(session->session_name[0] != '\0');
+
+                       group_by_session_path_name =
+                                       backward_compat_group_by_session(
+                                                       path_name,
+                                                       session->session_name);
+                       if (!group_by_session_path_name) {
+                               ERR("Failed to apply group by session to stream of session %" PRIu64,
+                                               session->id);
+                               goto send_reply;
+                       }
+
+                       DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
+                                       path_name, group_by_session_path_name);
+
+                       free(path_name);
+                       path_name = group_by_session_path_name;
+               }
+       }
+
        trace = ctf_trace_get_by_path_or_create(session, path_name);
        if (!trace) {
                goto send_reply;
        }
-       /* This stream here has one reference on the trace. */
 
+       /* This stream here has one reference on the trace. */
        pthread_mutex_lock(&last_relay_stream_id_lock);
        stream_handle = ++last_relay_stream_id;
        pthread_mutex_unlock(&last_relay_stream_id_lock);
@@ -2278,6 +2399,7 @@ static int relay_rotate_session_streams(
        }
 
        reply_code = LTTNG_OK;
+       ret = 0;
 end:
        if (stream) {
                stream_put(stream);
@@ -2291,47 +2413,12 @@ end:
                                send_ret);
                ret = -1;
        }
-
-       ret = 0;
 end_no_reply:
        lttng_trace_chunk_put(next_trace_chunk);
        return ret;
 }
 
-static int init_session_output_directory_handle(struct relay_session *session,
-               struct lttng_directory_handle *handle)
-{
-       int ret;
-       /*
-        * relayd_output_path/session_directory
-        * e.g. /home/user/lttng-traces/hostname/session_name
-        */
-       char *full_session_path = NULL;
 
-       pthread_mutex_lock(&session->lock);
-       full_session_path = create_output_path(session->output_path);
-       if (!full_session_path) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = utils_mkdir_recursive(
-                       full_session_path, S_IRWXU | S_IRWXG, -1, -1);
-       if (ret) {
-               ERR("Failed to create session output path \"%s\"",
-                               full_session_path);
-               goto end;
-       }
-
-       ret = lttng_directory_handle_init(handle, full_session_path);
-       if (ret) {
-               goto end;
-       }
-end:
-       pthread_mutex_unlock(&session->lock);
-       free(full_session_path);
-       return ret;
-}
 
 /*
  * relay_create_trace_chunk: create a new trace chunk
@@ -2418,13 +2505,6 @@ static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                }
        }
 
-       ret = init_session_output_directory_handle(
-                       conn->session, &session_output);
-       if (ret) {
-               reply_code = LTTNG_ERR_CREATE_DIR_FAIL;
-               goto end;
-       }
-
        chunk_status = lttng_trace_chunk_set_credentials_current_user(chunk);
        if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
                reply_code = LTTNG_ERR_UNK;
@@ -2432,7 +2512,14 @@ static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                goto end;
        }
 
+       ret = session_init_output_directory_handle(
+                       conn->session, &session_output);
+       if (ret) {
+               reply_code = LTTNG_ERR_CREATE_DIR_FAIL;
+               goto end;
+       }
        chunk_status = lttng_trace_chunk_set_as_owner(chunk, &session_output);
+       lttng_directory_handle_fini(&session_output);
        if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
                reply_code = LTTNG_ERR_UNK;
                ret = -1;
@@ -2488,7 +2575,6 @@ end:
 end_no_reply:
        lttng_trace_chunk_put(chunk);
        lttng_trace_chunk_put(published_chunk);
-       lttng_directory_handle_fini(&session_output);
        return ret;
 }
 
@@ -2499,7 +2585,7 @@ static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                struct relay_connection *conn,
                const struct lttng_buffer_view *payload)
 {
-       int ret = 0;
+       int ret = 0, buf_ret;
        ssize_t send_ret;
        struct relay_session *session = conn->session;
        struct lttcomm_relayd_close_trace_chunk *msg;
@@ -2660,17 +2746,17 @@ end_unlock_session:
 end:
        reply.generic.ret_code = htobe32((uint32_t) reply_code);
        reply.path_length = htobe32((uint32_t) path_length);
-       ret = lttng_dynamic_buffer_append(
+       buf_ret = lttng_dynamic_buffer_append(
                        &reply_payload, &reply, sizeof(reply));
-       if (ret) {
+       if (buf_ret) {
                ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
                goto end_no_reply;
        }
 
        if (reply_code == LTTNG_OK) {
-               ret = lttng_dynamic_buffer_append(&reply_payload,
+               buf_ret = lttng_dynamic_buffer_append(&reply_payload,
                                closed_trace_chunk_path, path_length);
-               if (ret) {
+               if (buf_ret) {
                        ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
                        goto end_no_reply;
                }
@@ -2705,8 +2791,8 @@ static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr *recv_hdr,
        struct lttcomm_relayd_trace_chunk_exists *msg;
        struct lttcomm_relayd_trace_chunk_exists_reply reply = {};
        struct lttng_buffer_view header_view;
-       struct lttng_trace_chunk *chunk = NULL;
        uint64_t chunk_id;
+       bool chunk_exists;
 
        if (!session || !conn->version_check_done) {
                ERR("Trying to close a trace chunk before version check");
@@ -2731,25 +2817,29 @@ static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr *recv_hdr,
        msg = (typeof(msg)) header_view.data;
        chunk_id = be64toh(msg->chunk_id);
 
-       chunk = sessiond_trace_chunk_registry_get_chunk(
+       ret = sessiond_trace_chunk_registry_chunk_exists(
                        sessiond_trace_chunk_registry,
                        conn->session->sessiond_uuid,
                        conn->session->id,
-                       chunk_id);
-
-       reply = (typeof(reply)) {
-               .generic.ret_code = htobe32((uint32_t) LTTNG_OK),
-               .trace_chunk_exists = !!chunk,
+                       chunk_id, &chunk_exists);
+       /*
+        * If ret is not 0, send the reply and report the error to the caller.
+        * It is a protocol (or internal) error and the session/connection
+        * should be torn down.
+        */
+       reply = (typeof(reply)){
+               .generic.ret_code = htobe32((uint32_t)
+                       (ret == 0 ? LTTNG_OK : LTTNG_ERR_INVALID_PROTOCOL)),
+               .trace_chunk_exists = ret == 0 ? chunk_exists : 0,
        };
-       send_ret = conn->sock->ops->sendmsg(conn->sock,
-                       &reply, sizeof(reply), 0);
+       send_ret = conn->sock->ops->sendmsg(
+                       conn->sock, &reply, sizeof(reply), 0);
        if (send_ret < (ssize_t) sizeof(reply)) {
                ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
                                send_ret);
                ret = -1;
        }
 end_no_reply:
-       lttng_trace_chunk_put(chunk);
        return ret;
 }
 
@@ -3430,8 +3520,13 @@ restart:
                                        if (ret < 0) {
                                                goto error;
                                        }
-                                       lttng_poll_add(&events, conn->sock->fd,
+                                       ret = lttng_poll_add(&events,
+                                                       conn->sock->fd,
                                                        LPOLLIN | LPOLLRDHUP);
+                                       if (ret) {
+                                               ERR("Failed to add new connection file descriptor to poll set");
+                                               goto error;
+                                       }
                                        connection_ht_add(relay_connections_ht, conn);
                                        DBG("Connection socket %d added", conn->sock->fd);
                                } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
@@ -3675,7 +3770,17 @@ int main(int argc, char **argv)
        int ret = 0, retval = 0;
        void *status;
 
-       /* Parse arguments */
+       /* Parse environment variables */
+       ret = parse_env_options();
+       if (ret) {
+               retval = -1;
+               goto exit_options;
+       }
+
+       /*
+        * Parse arguments.
+        * Command line arguments overwrite environment.
+        */
        progname = argv[0];
        if (set_options(argc, argv)) {
                retval = -1;
@@ -3687,6 +3792,14 @@ int main(int argc, char **argv)
                goto exit_options;
        }
 
+       relayd_config_log();
+
+       if (opt_print_version) {
+               print_version();
+               retval = 0;
+               goto exit_options;
+       }
+
        /* Try to create directory if -o, --output is specified. */
        if (opt_output_path) {
                if (*opt_output_path != '/') {
@@ -3725,6 +3838,14 @@ int main(int argc, char **argv)
                }
        }
 
+       if (opt_working_directory) {
+               ret = utils_change_working_directory(opt_working_directory);
+               if (ret) {
+                       /* All errors are already logged. */
+                       goto exit_options;
+               }
+       }
+
        sessiond_trace_chunk_registry = sessiond_trace_chunk_registry_create();
        if (!sessiond_trace_chunk_registry) {
                ERR("Failed to initialize session daemon trace chunk registry");
This page took 0.029417 seconds and 4 git commands to generate.