2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "common/time.hpp"
14 #include <common/common.hpp>
15 #include <common/defaults.hpp>
16 #include <common/utils.hpp>
18 #include "backward-compatibility-group-by.hpp"
20 #define DATETIME_REGEX \
21 ".*-[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]$"
24 * Provide support for --group-output-by-session for producer >= 2.4 and < 2.11.
25 * Take the stream path, extract all available information, craft a new path to
26 * the best of our ability enforcing the group by session.
28 * Return the allocated string containing the new stream path or else NULL.
30 char *backward_compat_group_by_session(const char *path
,
31 const char *local_session_name
,
32 time_t relay_session_creation_time
)
37 char *local_copy
= NULL
;
38 char *datetime
= NULL
;
39 char *partial_base_path
= NULL
;
40 char *filepath_per_session
= NULL
;
41 const char *second_token_ptr
;
42 const char *leftover_second_token_ptr
;
43 const char *hostname_ptr
;
47 LTTNG_ASSERT(local_session_name
);
48 LTTNG_ASSERT(local_session_name
[0] != '\0');
50 DBG("Parsing path \"%s\" of session \"%s\" to create a new path that is grouped by session",
51 path
, local_session_name
);
53 /* Get a local copy for strtok */
54 local_copy
= strdup(path
);
56 PERROR("Failed to parse session path: couldn't copy input path");
61 * The use of strtok with '/' as delimiter is valid since we refuse '/'
62 * in session name and '/' is not a valid hostname character based on
63 * RFC-952 [1], RFC-921 [2] and refined in RFC-1123 [3].
64 * [1] https://tools.ietf.org/html/rfc952
65 * [2] https://tools.ietf.org/html/rfc921
66 * [3] https://tools.ietf.org/html/rfc1123#page-13
70 * Get the hostname and possible session_name.
71 * Note that we can get the hostname and session name from the
72 * relay_session object we already have. Still, it is easier to
73 * tokenized the passed path to obtain the start of the path leftover.
75 hostname_ptr
= strtok_r(local_copy
, "/", &leftover_ptr
);
77 ERR("Failed to parse session path \"%s\": couldn't identify hostname",
82 second_token_ptr
= strtok_r(NULL
, "/", &leftover_ptr
);
83 if (!second_token_ptr
) {
84 ERR("Failed to parse session path \"%s\": couldn't identify session name",
90 * Check if the second token is a base path set at url level. This is
91 * legal in streaming, live and snapshot [1]. Otherwise it is the
92 * session name with possibly a datetime attached [2]. Note that when
93 * "adding" snapshot output (lttng snapshot add-output), no session name
94 * is present in the path by default. The handling for "base path" take
95 * care of this case as well.
96 * [1] e.g --set-url net://localhost/my_marvellous_path
99 * When using --snapshot on session create.
100 * <session_name>-<date>-<time>
101 * <auto>-<date>-<time>
103 if (strncmp(second_token_ptr
, local_session_name
,
104 strlen(local_session_name
)) != 0) {
106 * Token does not start with session name.
107 * This mean this is an extra path scenario.
108 * Duplicate the current token since it is part of an
110 * Set secDuplicate the current token since it is part of an
111 * base_path. The rest is the leftover.
112 * Set second_token_ptr to the local_session_name for further
115 partial_base_path
= strdup(second_token_ptr
);
116 if (!partial_base_path
) {
117 PERROR("Failed to parse session path: couldn't copy partial base path");
121 second_token_ptr
= local_session_name
;
125 * Based on the previous test, we can move inside the token ptr to
126 * remove the "local_session_name" and inspect the rest of the token.
127 * We are looking into extracting the creation datetime from either the
128 * session_name or the token. We need to to all this gymnastic because
129 * an extra path could decide to append a datetime to its first
133 * <session_name>-<date>-<time>
134 * <auto>-<date>-<time>
135 * <session_name>_base_path_foo_bar
136 * <session_name>-<false date>-<false-time> (via a base path)
138 * We have no way to discern from the basic scenario of:
139 * <session_name>-<date>-<time>
140 * and one done using a base path with the exact format we normally
144 * lttng create my_session -U
145 * net://localhost/my_session-19910319-120000/
147 ret
= regcomp(®ex
, DATETIME_REGEX
, 0);
149 ERR("Failed to parse session path: regex compilation failed with code %d", ret
);
153 leftover_second_token_ptr
=
154 second_token_ptr
+ strlen(local_session_name
);
155 len
= strlen(leftover_second_token_ptr
);
158 * We are either dealing with an auto session name or only the
159 * session_name. If this is a auto session name, we need to
160 * fetch the creation datetime.
162 ret
= regexec(®ex
, local_session_name
, 0, NULL
, 0);
164 const ssize_t local_session_name_offset
=
165 strlen(local_session_name
) - DATETIME_STR_LEN
+ 1;
167 LTTNG_ASSERT(local_session_name_offset
>= 0);
168 datetime
= strdup(local_session_name
+
169 local_session_name_offset
);
171 PERROR("Failed to parse session path: couldn't copy datetime on regex match");
175 datetime
= calloc
<char>(DATETIME_STR_LEN
);
177 PERROR("Failed to allocate DATETIME string");
181 ret
= time_to_datetime_str(relay_session_creation_time
,
182 datetime
, DATETIME_STR_LEN
);
184 /* time_to_datetime_str already logs errors. */
188 } else if (len
== DATETIME_STR_LEN
&&
189 !regexec(®ex
, leftover_second_token_ptr
, 0, NULL
,
192 * The leftover from the second token is of format
193 * "-<datetime>", use it as the creation time.
194 * Ignore leading "-".
196 datetime
= strdup(&leftover_second_token_ptr
[1]);
198 PERROR("Failed to parse session path: couldn't copy datetime on regex match");
203 * Base path scenario.
204 * We cannot try to extract the datetime from the session name
205 * since nothing prevent a user to name a session in the
206 * "name-<datetime>" format. Using the datetime from such a
207 * session would be invalid.
209 LTTNG_ASSERT(partial_base_path
== NULL
);
210 LTTNG_ASSERT(datetime
== NULL
);
212 partial_base_path
= strdup(second_token_ptr
);
213 if (!partial_base_path
) {
214 PERROR("Failed to parse session path: couldn't copy partial base path");
219 ret
= asprintf(&filepath_per_session
, "%s/%s%s%s/%s%s%s",
220 local_session_name
, hostname_ptr
, datetime
? "-" : "",
221 datetime
? datetime
: "",
222 partial_base_path
? partial_base_path
: "",
223 partial_base_path
? "/" : "", leftover_ptr
);
225 filepath_per_session
= NULL
;
232 free(partial_base_path
);
234 return filepath_per_session
;