X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Fbackward-compatibility-group-by.cpp;fp=src%2Fbin%2Flttng-relayd%2Fbackward-compatibility-group-by.cpp;h=f0454a56b70a0c2db24a632323d8df62f462c525;hb=ac497a37018f3c253d2e50397294f58d33f7f24f;hp=0000000000000000000000000000000000000000;hpb=7966af5763c4aaca39df9bbfa9277ff15715c720;p=lttng-tools.git diff --git a/src/bin/lttng-relayd/backward-compatibility-group-by.cpp b/src/bin/lttng-relayd/backward-compatibility-group-by.cpp new file mode 100644 index 000000000..f0454a56b --- /dev/null +++ b/src/bin/lttng-relayd/backward-compatibility-group-by.cpp @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2019 Jonathan Rajotte + * + * SPDX-License-Identifier: GPL-2.0-only + * + */ + +#include "common/time.h" +#include +#include +#include +#include + +#include +#include +#include + +#include "backward-compatibility-group-by.h" + +#define DATETIME_REGEX \ + ".*-[1-2][0-9][0-9][0-9][0-1][0-9][0-3][0-9]-[0-2][0-9][0-5][0-9][0-5][0-9]$" + +/* + * Provide support for --group-output-by-session for producer >= 2.4 and < 2.11. + * Take the stream path, extract all available information, craft a new path to + * the best of our ability enforcing the group by session. + * + * Return the allocated string containing the new stream path or else NULL. + */ +char *backward_compat_group_by_session(const char *path, + const char *local_session_name, + time_t relay_session_creation_time) +{ + int ret; + size_t len; + char *leftover_ptr; + char *local_copy = NULL; + char *datetime = NULL; + char *partial_base_path = NULL; + char *filepath_per_session = NULL; + const char *second_token_ptr; + const char *leftover_second_token_ptr; + const char *hostname_ptr; + regex_t regex; + + LTTNG_ASSERT(path); + LTTNG_ASSERT(local_session_name); + LTTNG_ASSERT(local_session_name[0] != '\0'); + + DBG("Parsing path \"%s\" of session \"%s\" to create a new path that is grouped by session", + path, local_session_name); + + /* Get a local copy for strtok */ + local_copy = strdup(path); + if (!local_copy) { + PERROR("Failed to parse session path: couldn't copy input path"); + goto error; + } + + /* + * The use of strtok with '/' as delimiter is valid since we refuse '/' + * in session name and '/' is not a valid hostname character based on + * RFC-952 [1], RFC-921 [2] and refined in RFC-1123 [3]. + * [1] https://tools.ietf.org/html/rfc952 + * [2] https://tools.ietf.org/html/rfc921 + * [3] https://tools.ietf.org/html/rfc1123#page-13 + */ + + /* + * Get the hostname and possible session_name. + * Note that we can get the hostname and session name from the + * relay_session object we already have. Still, it is easier to + * tokenized the passed path to obtain the start of the path leftover. + */ + hostname_ptr = strtok_r(local_copy, "/", &leftover_ptr); + if (!hostname_ptr) { + ERR("Failed to parse session path \"%s\": couldn't identify hostname", + path); + goto error; + } + + second_token_ptr = strtok_r(NULL, "/", &leftover_ptr); + if (!second_token_ptr) { + ERR("Failed to parse session path \"%s\": couldn't identify session name", + path); + goto error; + } + + /* + * Check if the second token is a base path set at url level. This is + * legal in streaming, live and snapshot [1]. Otherwise it is the + * session name with possibly a datetime attached [2]. Note that when + * "adding" snapshot output (lttng snapshot add-output), no session name + * is present in the path by default. The handling for "base path" take + * care of this case as well. + * [1] e.g --set-url net://localhost/my_marvellous_path + * [2] Can be: + * + * When using --snapshot on session create. + * --