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