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_STRING_SIZE 16
22 #define DATETIME_REGEX \
23 ".*-[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]$"
26 * Provide support for --group-output-by-session for producer >= 2.4 and < 2.11.
27 * Take the stream path, extract all available information, craft a new path to
28 * the best of our ability enforcing the group by session.
30 * Return the allocated string containing the new stream path or else NULL.
32 char *backward_compat_group_by_session(const char *path
,
33 const char *local_session_name
,
34 time_t relay_session_creation_time
)
39 char *local_copy
= NULL
;
40 char *datetime
= NULL
;
41 char *partial_base_path
= NULL
;
42 char *filepath_per_session
= NULL
;
43 const char *second_token_ptr
;
44 const char *leftover_second_token_ptr
;
45 const char *hostname_ptr
;
49 assert(local_session_name
);
50 assert(local_session_name
[0] != '\0');
52 DBG("Parsing path \"%s\" of session \"%s\" to create a new path that is grouped by session",
53 path
, local_session_name
);
55 /* Get a local copy for strtok */
56 local_copy
= strdup(path
);
58 PERROR("Failed to parse session path: couldn't copy input path");
63 * The use of strtok with '/' as delimiter is valid since we refuse '/'
64 * in session name and '/' is not a valid hostname character based on
65 * RFC-952 [1], RFC-921 [2] and refined in RFC-1123 [3].
66 * [1] https://tools.ietf.org/html/rfc952
67 * [2] https://tools.ietf.org/html/rfc921
68 * [3] https://tools.ietf.org/html/rfc1123#page-13
72 * Get the hostname and possible session_name.
73 * Note that we can get the hostname and session name from the
74 * relay_session object we already have. Still, it is easier to
75 * tokenized the passed path to obtain the start of the path leftover.
77 hostname_ptr
= strtok_r(local_copy
, "/", &leftover_ptr
);
79 ERR("Failed to parse session path \"%s\": couldn't identify hostname",
84 second_token_ptr
= strtok_r(NULL
, "/", &leftover_ptr
);
85 if (!second_token_ptr
) {
86 ERR("Failed to parse session path \"%s\": couldn't identify session name",
92 * Check if the second token is a base path set at url level. This is
93 * legal in streaming, live and snapshot [1]. Otherwise it is the
94 * session name with possibly a datetime attached [2]. Note that when
95 * "adding" snapshot output (lttng snapshot add-output), no session name
96 * is present in the path by default. The handling for "base path" take
97 * care of this case as well.
98 * [1] e.g --set-url net://localhost/my_marvellous_path
101 * When using --snapshot on session create.
102 * <session_name>-<date>-<time>
103 * <auto>-<date>-<time>
105 if (strncmp(second_token_ptr
, local_session_name
,
106 strlen(local_session_name
)) != 0) {
108 * Token does not start with session name.
109 * This mean this is an extra path scenario.
110 * Duplicate the current token since it is part of an
112 * Set secDuplicate the current token since it is part of an
113 * base_path. The rest is the leftover.
114 * Set second_token_ptr to the local_session_name for further
117 partial_base_path
= strdup(second_token_ptr
);
118 if (!partial_base_path
) {
119 PERROR("Failed to parse session path: couldn't copy partial base path");
123 second_token_ptr
= local_session_name
;
127 * Based on the previous test, we can move inside the token ptr to
128 * remove the "local_session_name" and inspect the rest of the token.
129 * We are looking into extracting the creation datetime from either the
130 * session_name or the token. We need to to all this gymnastic because
131 * an extra path could decide to append a datetime to its first
135 * <session_name>-<date>-<time>
136 * <auto>-<date>-<time>
137 * <session_name>_base_path_foo_bar
138 * <session_name>-<false date>-<false-time> (via a base path)
140 * We have no way to discern from the basic scenario of:
141 * <session_name>-<date>-<time>
142 * and one done using a base path with the exact format we normally
146 * lttng create my_session -U
147 * net://localhost/my_session-19910319-120000/
149 ret
= regcomp(®ex
, DATETIME_REGEX
, 0);
151 ERR("Failed to parse session path: regex compilation failed with code %d", ret
);
155 leftover_second_token_ptr
=
156 second_token_ptr
+ strlen(local_session_name
);
157 len
= strlen(leftover_second_token_ptr
);
160 * We are either dealing with an auto session name or only the
161 * session_name. If this is a auto session name, we need to
162 * fetch the creation datetime.
164 ret
= regexec(®ex
, local_session_name
, 0, NULL
, 0);
166 const ssize_t local_session_name_offset
=
167 strlen(local_session_name
) - DATETIME_STR_LEN
+ 1;
169 assert(local_session_name_offset
>= 0);
170 datetime
= strdup(local_session_name
+
171 local_session_name_offset
);
173 PERROR("Failed to parse session path: couldn't copy datetime on regex match");
177 datetime
= zmalloc(DATETIME_STR_LEN
);
179 PERROR("Failed to allocate DATETIME string");
183 ret
= time_to_datetime_str(relay_session_creation_time
,
184 datetime
, DATETIME_STR_LEN
);
186 /* time_to_datetime_str already logs errors. */
190 } else if (len
== DATETIME_STR_LEN
&&
191 !regexec(®ex
, leftover_second_token_ptr
, 0, NULL
,
194 * The leftover from the second token is of format
195 * "-<datetime>", use it as the creation time.
196 * Ignore leading "-".
198 datetime
= strdup(&leftover_second_token_ptr
[1]);
200 PERROR("Failed to parse session path: couldn't copy datetime on regex match");
205 * Base path scenario.
206 * We cannot try to extract the datetime from the session name
207 * since nothing prevent a user to name a session in the
208 * "name-<datetime>" format. Using the datetime from such a
209 * session would be invalid.
211 assert(partial_base_path
== NULL
);
212 assert(datetime
== NULL
);
214 partial_base_path
= strdup(second_token_ptr
);
215 if (!partial_base_path
) {
216 PERROR("Failed to parse session path: couldn't copy partial base path");
221 ret
= asprintf(&filepath_per_session
, "%s/%s%s%s/%s%s%s",
222 local_session_name
, hostname_ptr
, datetime
? "-" : "",
223 datetime
? datetime
: "",
224 partial_base_path
? partial_base_path
: "",
225 partial_base_path
? "/" : "", leftover_ptr
);
227 filepath_per_session
= NULL
;
234 free(partial_base_path
);
236 return filepath_per_session
;