Commit | Line | Data |
---|---|---|
2f8f53af DG |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
7591bab1 | 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2f8f53af DG |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License, version 2 only, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
2a174661 | 21 | #include <common/common.h> |
1e791a74 | 22 | #include <common/compat/uuid.h> |
a8b66566 JR |
23 | #include <common/time.h> |
24 | #include <common/utils.h> | |
7591bab1 | 25 | #include <urcu/rculist.h> |
2a174661 | 26 | |
d37856b8 JR |
27 | #include <sys/stat.h> |
28 | ||
2a174661 | 29 | #include "ctf-trace.h" |
a620c891 | 30 | #include "lttng-relayd.h" |
2f8f53af | 31 | #include "session.h" |
23c8ff50 | 32 | #include "sessiond-trace-chunks.h" |
a620c891 | 33 | #include "stream.h" |
a0409c33 | 34 | #include <common/defaults.h> |
d37856b8 | 35 | #include "utils.h" |
2a174661 DG |
36 | |
37 | /* Global session id used in the session creation. */ | |
38 | static uint64_t last_relay_session_id; | |
7591bab1 | 39 | static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
2a174661 | 40 | |
a8b66566 | 41 | static int init_session_output_path_group_by_host(struct relay_session *session) |
ecd1a12f MD |
42 | { |
43 | /* | |
44 | * session_directory: | |
45 | * | |
46 | * if base_path is \0' | |
47 | * hostname/session_name | |
48 | * else | |
49 | * hostname/base_path | |
50 | */ | |
51 | char *session_directory = NULL; | |
52 | int ret = 0; | |
53 | ||
54 | if (session->output_path[0] != '\0') { | |
55 | goto end; | |
56 | } | |
57 | /* | |
58 | * If base path is set, it overrides the session name for the | |
59 | * session relative base path. No timestamp is appended if the | |
60 | * base path is overridden. | |
61 | * | |
62 | * If the session name already contains the creation time (e.g. | |
63 | * auto-<timestamp>, don't append yet another timestamp after | |
64 | * the session name in the generated path. | |
65 | * | |
66 | * Otherwise, generate the path with session_name-<timestamp>. | |
67 | */ | |
68 | if (session->base_path[0] != '\0') { | |
69 | ret = asprintf(&session_directory, "%s/%s", session->hostname, | |
70 | session->base_path); | |
71 | } else if (session->session_name_contains_creation_time) { | |
72 | ret = asprintf(&session_directory, "%s/%s", session->hostname, | |
73 | session->session_name); | |
74 | } else { | |
a8b66566 | 75 | char session_creation_datetime[DATETIME_STR_LEN]; |
ecd1a12f | 76 | |
a8b66566 JR |
77 | ret = time_to_datetime_str( |
78 | LTTNG_OPTIONAL_GET(session->creation_time), | |
79 | session_creation_datetime, | |
80 | sizeof(session_creation_datetime)); | |
81 | if (ret) { | |
ecd1a12f MD |
82 | ERR("Failed to format session creation timestamp while initializing session output directory handle"); |
83 | ret = -1; | |
84 | goto end; | |
85 | } | |
a8b66566 | 86 | |
ecd1a12f MD |
87 | ret = asprintf(&session_directory, "%s/%s-%s", |
88 | session->hostname, session->session_name, | |
89 | session_creation_datetime); | |
90 | } | |
91 | if (ret < 0) { | |
92 | PERROR("Failed to format session directory name"); | |
93 | goto end; | |
94 | } | |
95 | ||
96 | if (strlen(session_directory) >= LTTNG_PATH_MAX) { | |
97 | ERR("Session output directory exceeds maximal length"); | |
98 | ret = -1; | |
99 | goto end; | |
100 | } | |
101 | strcpy(session->output_path, session_directory); | |
102 | ret = 0; | |
103 | ||
104 | end: | |
105 | free(session_directory); | |
106 | return ret; | |
107 | } | |
108 | ||
a8b66566 JR |
109 | static int init_session_output_path_group_by_session( |
110 | struct relay_session *session) | |
111 | { | |
112 | /* | |
113 | * session_directory: | |
114 | * | |
115 | * session_name/hostname-creation_time/base_path | |
116 | * | |
117 | * For session name including the datetime, use it as the complete name | |
118 | * since. Do not perform modification on it since the datetime is an | |
119 | * integral part of the name and how a user identify a session. | |
120 | */ | |
121 | int ret = 0; | |
122 | char *session_directory = NULL; | |
123 | char creation_datetime[DATETIME_STR_LEN]; | |
124 | ||
125 | if (session->output_path[0] != '\0') { | |
126 | /* output_path as been generated already */ | |
127 | goto end; | |
128 | } | |
129 | ||
130 | ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time), | |
131 | creation_datetime, sizeof(creation_datetime)); | |
132 | if (ret) { | |
133 | ERR("Failed to format session creation timestamp while initializing session output directory handle"); | |
134 | ret = -1; | |
135 | goto end; | |
136 | } | |
137 | ||
138 | ret = asprintf(&session_directory, "%s/%s-%s%s%s", | |
139 | session->session_name, session->hostname, | |
140 | creation_datetime, | |
141 | session->base_path[0] != '\0' ? "/" : "", | |
142 | session->base_path); | |
143 | if (ret < 0) { | |
144 | PERROR("Failed to format session directory name"); | |
145 | goto end; | |
146 | } | |
147 | ||
148 | if (strlen(session_directory) >= LTTNG_PATH_MAX) { | |
149 | ERR("Session output directory exceeds maximal length"); | |
150 | ret = -1; | |
151 | goto end; | |
152 | } | |
153 | ||
154 | strcpy(session->output_path, session_directory); | |
155 | ret = 0; | |
156 | ||
157 | end: | |
158 | free(session_directory); | |
159 | return ret; | |
160 | } | |
161 | ||
162 | static int init_session_output_path(struct relay_session *session) | |
163 | { | |
164 | int ret; | |
165 | ||
166 | switch (opt_group_output_by) { | |
167 | case RELAYD_GROUP_OUTPUT_BY_HOST: | |
168 | ret = init_session_output_path_group_by_host(session); | |
169 | break; | |
170 | case RELAYD_GROUP_OUTPUT_BY_SESSION: | |
171 | ret = init_session_output_path_group_by_session(session); | |
172 | break; | |
173 | case RELAYD_GROUP_OUTPUT_BY_UNKNOWN: | |
174 | default: | |
175 | abort(); | |
176 | break; | |
177 | } | |
178 | ||
179 | return ret; | |
180 | } | |
181 | ||
1e791a74 JG |
182 | static int session_set_anonymous_chunk(struct relay_session *session) |
183 | { | |
184 | int ret = 0; | |
185 | struct lttng_trace_chunk *chunk = NULL; | |
186 | enum lttng_trace_chunk_status status; | |
187 | struct lttng_directory_handle output_directory; | |
188 | ||
d37856b8 | 189 | ret = session_init_output_directory_handle(session, &output_directory); |
1e791a74 JG |
190 | if (ret) { |
191 | goto end; | |
192 | } | |
193 | ||
194 | chunk = lttng_trace_chunk_create_anonymous(); | |
195 | if (!chunk) { | |
196 | goto end; | |
197 | } | |
198 | ||
199 | status = lttng_trace_chunk_set_credentials_current_user(chunk); | |
200 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
201 | ret = -1; | |
202 | goto end; | |
203 | } | |
204 | ||
205 | status = lttng_trace_chunk_set_as_owner(chunk, &output_directory); | |
206 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
207 | ret = -1; | |
208 | goto end; | |
209 | } | |
210 | session->current_trace_chunk = chunk; | |
211 | chunk = NULL; | |
212 | end: | |
213 | lttng_trace_chunk_put(chunk); | |
214 | lttng_directory_handle_fini(&output_directory); | |
215 | return ret; | |
216 | } | |
217 | ||
2a174661 DG |
218 | /* |
219 | * Create a new session by assigning a new session ID. | |
220 | * | |
221 | * Return allocated session or else NULL. | |
222 | */ | |
7591bab1 | 223 | struct relay_session *session_create(const char *session_name, |
6fa5fe7c | 224 | const char *hostname, const char *base_path, |
db1da059 JG |
225 | uint32_t live_timer, |
226 | bool snapshot, | |
227 | const lttng_uuid sessiond_uuid, | |
228 | const uint64_t *id_sessiond, | |
229 | const uint64_t *current_chunk_id, | |
230 | const time_t *creation_time, | |
231 | uint32_t major, | |
46ef2188 MD |
232 | uint32_t minor, |
233 | bool session_name_contains_creation_time) | |
2a174661 | 234 | { |
23c8ff50 | 235 | int ret; |
590f0324 JG |
236 | struct relay_session *session = NULL; |
237 | ||
5c956ba3 JG |
238 | assert(session_name); |
239 | assert(hostname); | |
240 | assert(base_path); | |
241 | ||
242 | if (strstr(session_name, ".")) { | |
590f0324 JG |
243 | ERR("Illegal character in session name: \"%s\"", |
244 | session_name); | |
245 | goto error; | |
246 | } | |
5c956ba3 | 247 | if (strstr(base_path, "../")) { |
590f0324 JG |
248 | ERR("Invalid session base path walks up the path hierarchy: \"%s\"", |
249 | base_path); | |
250 | goto error; | |
251 | } | |
5c956ba3 | 252 | if (strstr(hostname, ".")) { |
590f0324 JG |
253 | ERR("Invalid character in hostname: \"%s\"", |
254 | hostname); | |
255 | goto error; | |
256 | } | |
2a174661 DG |
257 | |
258 | session = zmalloc(sizeof(*session)); | |
259 | if (!session) { | |
1e791a74 | 260 | PERROR("Failed to allocate session"); |
2a174661 DG |
261 | goto error; |
262 | } | |
bb5d54e7 MD |
263 | if (lttng_strncpy(session->session_name, session_name, |
264 | sizeof(session->session_name))) { | |
1e791a74 | 265 | WARN("Session name exceeds maximal allowed length"); |
bb5d54e7 MD |
266 | goto error; |
267 | } | |
268 | if (lttng_strncpy(session->hostname, hostname, | |
269 | sizeof(session->hostname))) { | |
1e791a74 | 270 | WARN("Hostname exceeds maximal allowed length"); |
bb5d54e7 MD |
271 | goto error; |
272 | } | |
6fa5fe7c MD |
273 | if (lttng_strncpy(session->base_path, base_path, |
274 | sizeof(session->base_path))) { | |
275 | WARN("Base path exceeds maximal allowed length"); | |
276 | goto error; | |
277 | } | |
46ef2188 MD |
278 | if (creation_time) { |
279 | LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); | |
280 | } | |
281 | session->session_name_contains_creation_time = | |
282 | session_name_contains_creation_time; | |
6fa5fe7c | 283 | |
2a174661 DG |
284 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
285 | if (!session->ctf_traces_ht) { | |
2a174661 DG |
286 | goto error; |
287 | } | |
288 | ||
7591bab1 | 289 | pthread_mutex_lock(&last_relay_session_id_lock); |
2a174661 | 290 | session->id = ++last_relay_session_id; |
7591bab1 MD |
291 | pthread_mutex_unlock(&last_relay_session_id_lock); |
292 | ||
293 | session->major = major; | |
294 | session->minor = minor; | |
2a174661 | 295 | lttng_ht_node_init_u64(&session->session_n, session->id); |
7591bab1 MD |
296 | urcu_ref_init(&session->ref); |
297 | CDS_INIT_LIST_HEAD(&session->recv_list); | |
298 | pthread_mutex_init(&session->lock, NULL); | |
7591bab1 MD |
299 | pthread_mutex_init(&session->recv_list_lock, NULL); |
300 | ||
7591bab1 MD |
301 | session->live_timer = live_timer; |
302 | session->snapshot = snapshot; | |
23c8ff50 JG |
303 | lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); |
304 | ||
1e791a74 JG |
305 | if (id_sessiond) { |
306 | LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); | |
307 | } | |
308 | ||
d519f442 JR |
309 | if (major == 2 && minor >= 11) { |
310 | /* Only applies for 2.11+ peers using trace chunks. */ | |
311 | ret = init_session_output_path(session); | |
312 | if (ret) { | |
313 | goto error; | |
314 | } | |
ecd1a12f | 315 | } |
d519f442 | 316 | |
23c8ff50 JG |
317 | ret = sessiond_trace_chunk_registry_session_created( |
318 | sessiond_trace_chunk_registry, sessiond_uuid); | |
319 | if (ret) { | |
320 | goto error; | |
321 | } | |
7591bab1 | 322 | |
1e791a74 JG |
323 | if (id_sessiond && current_chunk_id) { |
324 | session->current_trace_chunk = | |
325 | sessiond_trace_chunk_registry_get_chunk( | |
326 | sessiond_trace_chunk_registry, | |
327 | session->sessiond_uuid, | |
328 | session->id_sessiond.value, | |
329 | *current_chunk_id); | |
330 | if (!session->current_trace_chunk) { | |
331 | char uuid_str[UUID_STR_LEN]; | |
332 | ||
333 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
334 | ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64, | |
335 | uuid_str, *id_sessiond, | |
336 | *current_chunk_id); | |
337 | } | |
338 | } else if (!id_sessiond) { | |
339 | /* | |
340 | * Pre-2.11 peers will not announce trace chunks. An | |
341 | * anonymous trace chunk which will remain set for the | |
342 | * duration of the session is created. | |
343 | */ | |
344 | ret = session_set_anonymous_chunk(session); | |
345 | if (ret) { | |
346 | goto error; | |
347 | } | |
348 | } | |
349 | ||
7591bab1 | 350 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); |
bb5d54e7 | 351 | return session; |
2a174661 DG |
352 | |
353 | error: | |
1e791a74 | 354 | session_put(session); |
bb5d54e7 | 355 | return NULL; |
2a174661 | 356 | } |
2f8f53af | 357 | |
7591bab1 MD |
358 | /* Should be called with RCU read-side lock held. */ |
359 | bool session_get(struct relay_session *session) | |
360 | { | |
ce4d4083 | 361 | return urcu_ref_get_unless_zero(&session->ref); |
7591bab1 MD |
362 | } |
363 | ||
2f8f53af | 364 | /* |
7591bab1 MD |
365 | * Lookup a session within the session hash table using the session id |
366 | * as key. A session reference is taken when a session is returned. | |
367 | * session_put() must be called on that session. | |
2f8f53af DG |
368 | * |
369 | * Return session or NULL if not found. | |
370 | */ | |
7591bab1 | 371 | struct relay_session *session_get_by_id(uint64_t id) |
2f8f53af DG |
372 | { |
373 | struct relay_session *session = NULL; | |
2a174661 | 374 | struct lttng_ht_node_u64 *node; |
2f8f53af DG |
375 | struct lttng_ht_iter iter; |
376 | ||
7591bab1 MD |
377 | rcu_read_lock(); |
378 | lttng_ht_lookup(sessions_ht, &id, &iter); | |
2a174661 | 379 | node = lttng_ht_iter_get_node_u64(&iter); |
2f8f53af | 380 | if (!node) { |
2a174661 | 381 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
2f8f53af DG |
382 | goto end; |
383 | } | |
384 | session = caa_container_of(node, struct relay_session, session_n); | |
2a174661 | 385 | DBG("Session find by ID %" PRIu64 " id found", id); |
7591bab1 MD |
386 | if (!session_get(session)) { |
387 | session = NULL; | |
388 | } | |
2f8f53af | 389 | end: |
7591bab1 | 390 | rcu_read_unlock(); |
2f8f53af DG |
391 | return session; |
392 | } | |
2a174661 | 393 | |
7591bab1 MD |
394 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
395 | { | |
396 | struct relay_session *session = | |
397 | caa_container_of(rcu_head, struct relay_session, | |
398 | rcu_node); | |
49e614cb MD |
399 | /* |
400 | * Since each trace has a reference on the session, it means | |
401 | * that if we are at the point where we teardown the session, no | |
402 | * trace belonging to that session exist at this point. | |
403 | * Calling lttng_ht_destroy in call_rcu worker thread so we | |
404 | * don't hold the RCU read-side lock while calling it. | |
405 | */ | |
406 | lttng_ht_destroy(session->ctf_traces_ht); | |
7591bab1 MD |
407 | free(session); |
408 | } | |
409 | ||
2a174661 DG |
410 | /* |
411 | * Delete session from the given hash table. | |
412 | * | |
413 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
414 | */ | |
7591bab1 | 415 | static int session_delete(struct relay_session *session) |
2a174661 DG |
416 | { |
417 | struct lttng_ht_iter iter; | |
418 | ||
2a174661 | 419 | iter.iter.node = &session->session_n.node; |
7591bab1 | 420 | return lttng_ht_del(sessions_ht, &iter); |
2a174661 DG |
421 | } |
422 | ||
7591bab1 MD |
423 | |
424 | static void destroy_session(struct relay_session *session) | |
425 | { | |
426 | int ret; | |
427 | ||
428 | ret = session_delete(session); | |
429 | assert(!ret); | |
639ddf68 | 430 | lttng_trace_chunk_put(session->current_trace_chunk); |
c35f9726 | 431 | session->current_trace_chunk = NULL; |
62bad3bf JG |
432 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
433 | session->pending_closure_trace_chunk = NULL; | |
23c8ff50 JG |
434 | ret = sessiond_trace_chunk_registry_session_destroyed( |
435 | sessiond_trace_chunk_registry, session->sessiond_uuid); | |
436 | assert(!ret); | |
7591bab1 MD |
437 | call_rcu(&session->rcu_node, rcu_destroy_session); |
438 | } | |
439 | ||
440 | void session_release(struct urcu_ref *ref) | |
2a174661 | 441 | { |
7591bab1 MD |
442 | struct relay_session *session = |
443 | caa_container_of(ref, struct relay_session, ref); | |
2a174661 | 444 | |
7591bab1 MD |
445 | destroy_session(session); |
446 | } | |
2a174661 | 447 | |
7591bab1 MD |
448 | void session_put(struct relay_session *session) |
449 | { | |
874ec45e JG |
450 | if (!session) { |
451 | return; | |
452 | } | |
7591bab1 | 453 | rcu_read_lock(); |
7591bab1 | 454 | urcu_ref_put(&session->ref, session_release); |
7591bab1 | 455 | rcu_read_unlock(); |
2a174661 DG |
456 | } |
457 | ||
7591bab1 | 458 | int session_close(struct relay_session *session) |
2a174661 DG |
459 | { |
460 | int ret = 0; | |
7591bab1 MD |
461 | struct ctf_trace *trace; |
462 | struct lttng_ht_iter iter; | |
463 | struct relay_stream *stream; | |
464 | ||
465 | pthread_mutex_lock(&session->lock); | |
466 | DBG("closing session %" PRIu64 ": is conn already closed %d", | |
467 | session->id, session->connection_closed); | |
7591bab1 | 468 | session->connection_closed = true; |
7591bab1 | 469 | pthread_mutex_unlock(&session->lock); |
2a174661 | 470 | |
7591bab1 MD |
471 | rcu_read_lock(); |
472 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, | |
473 | &iter.iter, trace, node.node) { | |
474 | ret = ctf_trace_close(trace); | |
475 | if (ret) { | |
476 | goto rcu_unlock; | |
2a174661 DG |
477 | } |
478 | } | |
7591bab1 MD |
479 | cds_list_for_each_entry_rcu(stream, &session->recv_list, |
480 | recv_node) { | |
bda7c7b9 JG |
481 | /* Close streams which have not been published yet. */ |
482 | try_stream_close(stream); | |
7591bab1 MD |
483 | } |
484 | rcu_unlock: | |
485 | rcu_read_unlock(); | |
486 | if (ret) { | |
487 | return ret; | |
488 | } | |
489 | /* Put self-reference from create. */ | |
490 | session_put(session); | |
491 | return ret; | |
2a174661 DG |
492 | } |
493 | ||
98ba050e JR |
494 | int session_abort(struct relay_session *session) |
495 | { | |
496 | int ret = 0; | |
497 | ||
498 | if (!session) { | |
499 | return 0; | |
500 | } | |
501 | ||
502 | pthread_mutex_lock(&session->lock); | |
503 | DBG("aborting session %" PRIu64, session->id); | |
98ba050e | 504 | session->aborted = true; |
98ba050e JR |
505 | pthread_mutex_unlock(&session->lock); |
506 | return ret; | |
507 | } | |
508 | ||
7591bab1 | 509 | void print_sessions(void) |
2a174661 | 510 | { |
2a174661 | 511 | struct lttng_ht_iter iter; |
7591bab1 | 512 | struct relay_session *session; |
2a174661 | 513 | |
ce3f3ba3 JG |
514 | if (!sessions_ht) { |
515 | return; | |
516 | } | |
517 | ||
2a174661 | 518 | rcu_read_lock(); |
7591bab1 MD |
519 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
520 | session_n.node) { | |
521 | if (!session_get(session)) { | |
522 | continue; | |
523 | } | |
524 | DBG("session %p refcount %ld session %" PRIu64, | |
525 | session, | |
526 | session->ref.refcount, | |
527 | session->id); | |
528 | session_put(session); | |
2a174661 | 529 | } |
2a174661 | 530 | rcu_read_unlock(); |
2a174661 | 531 | } |
d37856b8 JR |
532 | |
533 | int session_init_output_directory_handle(struct relay_session *session, | |
534 | struct lttng_directory_handle *handle) | |
535 | { | |
536 | int ret; | |
537 | /* | |
538 | * relayd_output_path/session_directory | |
539 | * e.g. /home/user/lttng-traces/hostname/session_name | |
540 | */ | |
541 | char *full_session_path = NULL; | |
542 | ||
543 | pthread_mutex_lock(&session->lock); | |
544 | full_session_path = create_output_path(session->output_path); | |
545 | if (!full_session_path) { | |
546 | ret = -1; | |
547 | goto end; | |
548 | } | |
549 | ||
550 | ret = utils_mkdir_recursive( | |
551 | full_session_path, S_IRWXU | S_IRWXG, -1, -1); | |
552 | if (ret) { | |
553 | ERR("Failed to create session output path \"%s\"", | |
554 | full_session_path); | |
555 | goto end; | |
556 | } | |
557 | ||
558 | ret = lttng_directory_handle_init(handle, full_session_path); | |
559 | if (ret) { | |
560 | goto end; | |
561 | } | |
562 | end: | |
563 | pthread_mutex_unlock(&session->lock); | |
564 | free(full_session_path); | |
565 | return ret; | |
566 | } |