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