relayd: register fd tracker instance to all created trace chunks
[lttng-tools.git] / src / bin / lttng-relayd / session.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
20#define _LGPL_SOURCE
21#include <common/common.h>
22#include <common/uuid.h>
23#include <common/time.h>
24#include <common/utils.h>
25#include <common/uuid.h>
26#include <common/compat/path.h>
27#include <urcu/rculist.h>
28
29#include <sys/stat.h>
30
31#include "ctf-trace.h"
32#include "lttng-relayd.h"
33#include "session.h"
34#include "sessiond-trace-chunks.h"
35#include "stream.h"
36#include <common/defaults.h>
37#include "utils.h"
38
39/* Global session id used in the session creation. */
40static uint64_t last_relay_session_id;
41static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
42
43static int init_session_output_path_group_by_host(struct relay_session *session)
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 {
77 char session_creation_datetime[DATETIME_STR_LEN];
78
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) {
84 ERR("Failed to format session creation timestamp while initializing session output directory handle");
85 ret = -1;
86 goto end;
87 }
88
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
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
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
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;
221 struct lttng_directory_handle *output_directory;
222
223 output_directory = session_create_output_directory_handle(session);
224 if (!output_directory) {
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
239 status = lttng_trace_chunk_set_as_owner(chunk, output_directory);
240 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
241 ret = -1;
242 goto end;
243 }
244
245 lttng_trace_chunk_set_fd_tracker(chunk, the_fd_tracker);
246 output_directory = NULL;
247 session->current_trace_chunk = chunk;
248 chunk = NULL;
249end:
250 lttng_trace_chunk_put(chunk);
251 lttng_directory_handle_put(output_directory);
252 return ret;
253}
254
255/*
256 * Check if a name is safe to use in a path.
257 *
258 * A name that is deemed "path-safe":
259 * - Does not contains a path separator (/ or \, platform dependant),
260 * - Does not start with a '.' (hidden file/folder),
261 * - Is not empty.
262 */
263static bool is_name_path_safe(const char *name)
264{
265 const size_t name_len = strlen(name);
266
267 /* Not empty. */
268 if (name_len == 0) {
269 WARN("An empty name is not allowed to be used in a path");
270 return false;
271 }
272 /* Does not start with '.'. */
273 if (name[0] == '.') {
274 WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'", name);
275 return false;
276 }
277 /* Does not contain a path-separator. */
278 if (strchr(name, LTTNG_PATH_SEPARATOR)) {
279 WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator", name);
280 return false;
281 }
282
283 return true;
284}
285
286/*
287 * Create a new session by assigning a new session ID.
288 *
289 * Return allocated session or else NULL.
290 */
291struct relay_session *session_create(const char *session_name,
292 const char *hostname, const char *base_path,
293 uint32_t live_timer,
294 bool snapshot,
295 const lttng_uuid sessiond_uuid,
296 const uint64_t *id_sessiond,
297 const uint64_t *current_chunk_id,
298 const time_t *creation_time,
299 uint32_t major,
300 uint32_t minor,
301 bool session_name_contains_creation_time)
302{
303 int ret;
304 struct relay_session *session = NULL;
305
306 assert(session_name);
307 assert(hostname);
308 assert(base_path);
309
310 if (!is_name_path_safe(session_name)) {
311 ERR("Refusing to create session as the provided session name is not path-safe");
312 goto error;
313 }
314 if (!is_name_path_safe(hostname)) {
315 ERR("Refusing to create session as the provided hostname is not path-safe");
316 goto error;
317 }
318 if (strstr(base_path, "../")) {
319 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
320 base_path);
321 goto error;
322 }
323
324 session = zmalloc(sizeof(*session));
325 if (!session) {
326 PERROR("Failed to allocate session");
327 goto error;
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 pthread_mutex_lock(&last_relay_session_id_lock);
356 session->id = ++last_relay_session_id;
357 pthread_mutex_unlock(&last_relay_session_id_lock);
358
359 session->major = major;
360 session->minor = minor;
361 lttng_ht_node_init_u64(&session->session_n, session->id);
362 urcu_ref_init(&session->ref);
363 CDS_INIT_LIST_HEAD(&session->recv_list);
364 pthread_mutex_init(&session->lock, NULL);
365 pthread_mutex_init(&session->recv_list_lock, NULL);
366
367 session->live_timer = live_timer;
368 session->snapshot = snapshot;
369 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
370
371 if (id_sessiond) {
372 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
373 }
374
375 if (major == 2 && minor >= 11) {
376 /* Only applies for 2.11+ peers using trace chunks. */
377 ret = init_session_output_path(session);
378 if (ret) {
379 goto error;
380 }
381 }
382
383 ret = sessiond_trace_chunk_registry_session_created(
384 sessiond_trace_chunk_registry, sessiond_uuid);
385 if (ret) {
386 goto error;
387 }
388
389 if (id_sessiond && current_chunk_id) {
390 enum lttng_trace_chunk_status chunk_status;
391 struct lttng_directory_handle *session_output_directory;
392
393 session->current_trace_chunk =
394 sessiond_trace_chunk_registry_get_chunk(
395 sessiond_trace_chunk_registry,
396 session->sessiond_uuid,
397 session->id_sessiond.value,
398 *current_chunk_id);
399 if (!session->current_trace_chunk) {
400 char uuid_str[LTTNG_UUID_STR_LEN];
401
402 lttng_uuid_to_str(sessiond_uuid, uuid_str);
403 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
404 uuid_str, *id_sessiond,
405 *current_chunk_id);
406 goto error;
407 }
408
409 chunk_status = lttng_trace_chunk_get_session_output_directory_handle(
410 session->current_trace_chunk,
411 &session_output_directory);
412 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
413 goto error;
414 }
415
416 assert(session_output_directory);
417 session->output_directory = session_output_directory;
418 } else if (!id_sessiond) {
419 /*
420 * Pre-2.11 peers will not announce trace chunks. An
421 * anonymous trace chunk which will remain set for the
422 * duration of the session is created.
423 */
424 ret = session_set_anonymous_chunk(session);
425 if (ret) {
426 goto error;
427 }
428 } else {
429 session->output_directory =
430 session_create_output_directory_handle(session);
431 if (!session->output_directory) {
432 goto error;
433 }
434 }
435
436 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
437 return session;
438
439error:
440 session_put(session);
441 return NULL;
442}
443
444/* Should be called with RCU read-side lock held. */
445bool session_get(struct relay_session *session)
446{
447 return urcu_ref_get_unless_zero(&session->ref);
448}
449
450/*
451 * Lookup a session within the session hash table using the session id
452 * as key. A session reference is taken when a session is returned.
453 * session_put() must be called on that session.
454 *
455 * Return session or NULL if not found.
456 */
457struct relay_session *session_get_by_id(uint64_t id)
458{
459 struct relay_session *session = NULL;
460 struct lttng_ht_node_u64 *node;
461 struct lttng_ht_iter iter;
462
463 rcu_read_lock();
464 lttng_ht_lookup(sessions_ht, &id, &iter);
465 node = lttng_ht_iter_get_node_u64(&iter);
466 if (!node) {
467 DBG("Session find by ID %" PRIu64 " id NOT found", id);
468 goto end;
469 }
470 session = caa_container_of(node, struct relay_session, session_n);
471 DBG("Session find by ID %" PRIu64 " id found", id);
472 if (!session_get(session)) {
473 session = NULL;
474 }
475end:
476 rcu_read_unlock();
477 return session;
478}
479
480static void rcu_destroy_session(struct rcu_head *rcu_head)
481{
482 struct relay_session *session =
483 caa_container_of(rcu_head, struct relay_session,
484 rcu_node);
485 /*
486 * Since each trace has a reference on the session, it means
487 * that if we are at the point where we teardown the session, no
488 * trace belonging to that session exist at this point.
489 * Calling lttng_ht_destroy in call_rcu worker thread so we
490 * don't hold the RCU read-side lock while calling it.
491 */
492 lttng_ht_destroy(session->ctf_traces_ht);
493 free(session);
494}
495
496/*
497 * Delete session from the given hash table.
498 *
499 * Return lttng ht del error code being 0 on success and 1 on failure.
500 */
501static int session_delete(struct relay_session *session)
502{
503 struct lttng_ht_iter iter;
504
505 iter.iter.node = &session->session_n.node;
506 return lttng_ht_del(sessions_ht, &iter);
507}
508
509
510static void destroy_session(struct relay_session *session)
511{
512 int ret;
513
514 ret = session_delete(session);
515 assert(!ret);
516 lttng_trace_chunk_put(session->current_trace_chunk);
517 session->current_trace_chunk = NULL;
518 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
519 session->pending_closure_trace_chunk = NULL;
520 ret = sessiond_trace_chunk_registry_session_destroyed(
521 sessiond_trace_chunk_registry, session->sessiond_uuid);
522 assert(!ret);
523 lttng_directory_handle_put(session->output_directory);
524 session->output_directory = NULL;
525 call_rcu(&session->rcu_node, rcu_destroy_session);
526}
527
528static void session_release(struct urcu_ref *ref)
529{
530 struct relay_session *session =
531 caa_container_of(ref, struct relay_session, ref);
532
533 destroy_session(session);
534}
535
536void session_put(struct relay_session *session)
537{
538 if (!session) {
539 return;
540 }
541 rcu_read_lock();
542 urcu_ref_put(&session->ref, session_release);
543 rcu_read_unlock();
544}
545
546int session_close(struct relay_session *session)
547{
548 int ret = 0;
549 struct ctf_trace *trace;
550 struct lttng_ht_iter iter;
551 struct relay_stream *stream;
552
553 pthread_mutex_lock(&session->lock);
554 DBG("closing session %" PRIu64 ": is conn already closed %d",
555 session->id, session->connection_closed);
556 session->connection_closed = true;
557 pthread_mutex_unlock(&session->lock);
558
559 rcu_read_lock();
560 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
561 &iter.iter, trace, node.node) {
562 ret = ctf_trace_close(trace);
563 if (ret) {
564 goto rcu_unlock;
565 }
566 }
567 cds_list_for_each_entry_rcu(stream, &session->recv_list,
568 recv_node) {
569 /* Close streams which have not been published yet. */
570 try_stream_close(stream);
571 }
572rcu_unlock:
573 rcu_read_unlock();
574 if (ret) {
575 return ret;
576 }
577 /* Put self-reference from create. */
578 session_put(session);
579 return ret;
580}
581
582int session_abort(struct relay_session *session)
583{
584 int ret = 0;
585
586 if (!session) {
587 return 0;
588 }
589
590 pthread_mutex_lock(&session->lock);
591 DBG("aborting session %" PRIu64, session->id);
592 session->aborted = true;
593 pthread_mutex_unlock(&session->lock);
594 return ret;
595}
596
597void print_sessions(void)
598{
599 struct lttng_ht_iter iter;
600 struct relay_session *session;
601
602 if (!sessions_ht) {
603 return;
604 }
605
606 rcu_read_lock();
607 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
608 session_n.node) {
609 if (!session_get(session)) {
610 continue;
611 }
612 DBG("session %p refcount %ld session %" PRIu64,
613 session,
614 session->ref.refcount,
615 session->id);
616 session_put(session);
617 }
618 rcu_read_unlock();
619}
This page took 0.026862 seconds and 5 git commands to generate.