2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "common/time.h"
15 #include <common/common.h>
16 #include <common/defaults.h>
17 #include <common/utils.h>
19 #include "backward-compatibility-group-by.h"
21 #define DATETIME_REGEX \
22 ".*-[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]$"
25 * Provide support for --group-output-by-session for producer >= 2.4 and < 2.11.
26 * Take the stream path, extract all available information, craft a new path to
27 * the best of our ability enforcing the group by session.
29 * Return the allocated string containing the new stream path or else NULL.
31 char *backward_compat_group_by_session(const char *path
,
32 const char *local_session_name
,
33 time_t relay_session_creation_time
)
38 char *local_copy
= NULL
;
39 char *datetime
= NULL
;
40 char *partial_base_path
= NULL
;
41 char *filepath_per_session
= NULL
;
42 const char *second_token_ptr
;
43 const char *leftover_second_token_ptr
;
44 const char *hostname_ptr
;
48 assert(local_session_name
);
49 assert(local_session_name
[0] != '\0');
51 DBG("Parsing path \"%s\" of session \"%s\" to create a new path that is grouped by session",
52 path
, local_session_name
);
54 /* Get a local copy for strtok */
55 local_copy
= strdup(path
);
57 PERROR("Failed to parse session path: couldn't copy input path");
62 * The use of strtok with '/' as delimiter is valid since we refuse '/'
63 * in session name and '/' is not a valid hostname character based on
64 * RFC-952 [1], RFC-921 [2] and refined in RFC-1123 [3].
65 * [1] https://tools.ietf.org/html/rfc952
66 * [2] https://tools.ietf.org/html/rfc921
67 * [3] https://tools.ietf.org/html/rfc1123#page-13
71 * Get the hostname and possible session_name.
72 * Note that we can get the hostname and session name from the
73 * relay_session object we already have. Still, it is easier to
74 * tokenized the passed path to obtain the start of the path leftover.
76 hostname_ptr
= strtok_r(local_copy
, "/", &leftover_ptr
);
78 ERR("Failed to parse session path \"%s\": couldn't identify hostname",
83 second_token_ptr
= strtok_r(NULL
, "/", &leftover_ptr
);
84 if (!second_token_ptr
) {
85 ERR("Failed to parse session path \"%s\": couldn't identify session name",
91 * Check if the second token is a base path set at url level. This is
92 * legal in streaming, live and snapshot [1]. Otherwise it is the
93 * session name with possibly a datetime attached [2]. Note that when
94 * "adding" snapshot output (lttng snapshot add-output), no session name
95 * is present in the path by default. The handling for "base path" take
96 * care of this case as well.
97 * [1] e.g --set-url net://localhost/my_marvellous_path
100 * When using --snapshot on session create.
101 * <session_name>-<date>-<time>
102 * <auto>-<date>-<time>
104 if (strncmp(second_token_ptr
, local_session_name
,
105 strlen(local_session_name
)) != 0) {
107 * Token does not start with session name.
108 * This mean this is an extra path scenario.
109 * Duplicate the current token since it is part of an
111 * Set secDuplicate the current token since it is part of an
112 * base_path. The rest is the leftover.
113 * Set second_token_ptr to the local_session_name for further
116 partial_base_path
= strdup(second_token_ptr
);
117 if (!partial_base_path
) {
118 PERROR("Failed to parse session path: couldn't copy partial base path");
122 second_token_ptr
= local_session_name
;
126 * Based on the previous test, we can move inside the token ptr to
127 * remove the "local_session_name" and inspect the rest of the token.
128 * We are looking into extracting the creation datetime from either the
129 * session_name or the token. We need to to all this gymnastic because
130 * an extra path could decide to append a datetime to its first
134 * <session_name>-<date>-<time>
135 * <auto>-<date>-<time>
136 * <session_name>_base_path_foo_bar
137 * <session_name>-<false date>-<false-time> (via a base path)
139 * We have no way to discern from the basic scenario of:
140 * <session_name>-<date>-<time>
141 * and one done using a base path with the exact format we normally
145 * lttng create my_session -U
146 * net://localhost/my_session-19910319-120000/
148 ret
= regcomp(®ex
, DATETIME_REGEX
, 0);
150 ERR("Failed to parse session path: regex compilation failed with code %d", ret
);
154 leftover_second_token_ptr
=
155 second_token_ptr
+ strlen(local_session_name
);
156 len
= strlen(leftover_second_token_ptr
);
159 * We are either dealing with an auto session name or only the
160 * session_name. If this is a auto session name, we need to
161 * fetch the creation datetime.
163 ret
= regexec(®ex
, local_session_name
, 0, NULL
, 0);
165 const ssize_t local_session_name_offset
=
166 strlen(local_session_name
) - DATETIME_STR_LEN
+ 1;
168 assert(local_session_name_offset
>= 0);
169 datetime
= strdup(local_session_name
+
170 local_session_name_offset
);
172 PERROR("Failed to parse session path: couldn't copy datetime on regex match");
176 datetime
= zmalloc(DATETIME_STR_LEN
);
178 PERROR("Failed to allocate DATETIME string");
182 ret
= time_to_datetime_str(relay_session_creation_time
,
183 datetime
, DATETIME_STR_LEN
);
185 /* time_to_datetime_str already logs errors. */
189 } else if (len
== DATETIME_STR_LEN
&&
190 !regexec(®ex
, leftover_second_token_ptr
, 0, NULL
,
193 * The leftover from the second token is of format
194 * "-<datetime>", use it as the creation time.
195 * Ignore leading "-".
197 datetime
= strdup(&leftover_second_token_ptr
[1]);
199 PERROR("Failed to parse session path: couldn't copy datetime on regex match");
204 * Base path scenario.
205 * We cannot try to extract the datetime from the session name
206 * since nothing prevent a user to name a session in the
207 * "name-<datetime>" format. Using the datetime from such a
208 * session would be invalid.
210 assert(partial_base_path
== NULL
);
211 assert(datetime
== NULL
);
213 partial_base_path
= strdup(second_token_ptr
);
214 if (!partial_base_path
) {
215 PERROR("Failed to parse session path: couldn't copy partial base path");
220 ret
= asprintf(&filepath_per_session
, "%s/%s%s%s/%s%s%s",
221 local_session_name
, hostname_ptr
, datetime
? "-" : "",
222 datetime
? datetime
: "",
223 partial_base_path
? partial_base_path
: "",
224 partial_base_path
? "/" : "", leftover_ptr
);
226 filepath_per_session
= NULL
;
233 free(partial_base_path
);
235 return filepath_per_session
;