00dfbd7d2e6e5c873b01dd66b80a7d3098198162
[lttng-tools.git] / src / bin / lttng-relayd / session.c
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. */
40 static uint64_t last_relay_session_id;
41 static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
42
43 static 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
106 end:
107 free(session_directory);
108 return ret;
109 }
110
111 static 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
159 end:
160 free(session_directory);
161 return ret;
162 }
163
164 static 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
184 static int session_set_anonymous_chunk(struct relay_session *session)
185 {
186 int ret = 0;
187 struct lttng_trace_chunk *chunk = NULL;
188 enum lttng_trace_chunk_status status;
189 struct lttng_directory_handle *output_directory;
190
191 output_directory = session_create_output_directory_handle(session);
192 if (!output_directory) {
193 goto end;
194 }
195
196 chunk = lttng_trace_chunk_create_anonymous();
197 if (!chunk) {
198 goto end;
199 }
200
201 status = lttng_trace_chunk_set_credentials_current_user(chunk);
202 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
203 ret = -1;
204 goto end;
205 }
206
207 status = lttng_trace_chunk_set_as_owner(chunk, output_directory);
208 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
209 ret = -1;
210 goto end;
211 }
212 output_directory = NULL;
213 session->current_trace_chunk = chunk;
214 chunk = NULL;
215 end:
216 lttng_trace_chunk_put(chunk);
217 lttng_directory_handle_put(output_directory);
218 return ret;
219 }
220
221 /*
222 * Check if a name is safe to use in a path.
223 *
224 * A name that is deemed "path-safe":
225 * - Does not contains a path separator (/ or \, platform dependant),
226 * - Does not start with a '.' (hidden file/folder),
227 * - Is not empty.
228 */
229 static bool is_name_path_safe(const char *name)
230 {
231 const size_t name_len = strlen(name);
232
233 /* Not empty. */
234 if (name_len == 0) {
235 WARN("An empty name is not allowed to be used in a path");
236 return false;
237 }
238 /* Does not start with '.'. */
239 if (name[0] == '.') {
240 WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'", name);
241 return false;
242 }
243 /* Does not contain a path-separator. */
244 if (strchr(name, LTTNG_PATH_SEPARATOR)) {
245 WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator", name);
246 return false;
247 }
248
249 return true;
250 }
251
252 /*
253 * Create a new session by assigning a new session ID.
254 *
255 * Return allocated session or else NULL.
256 */
257 struct relay_session *session_create(const char *session_name,
258 const char *hostname, const char *base_path,
259 uint32_t live_timer,
260 bool snapshot,
261 const lttng_uuid sessiond_uuid,
262 const uint64_t *id_sessiond,
263 const uint64_t *current_chunk_id,
264 const time_t *creation_time,
265 uint32_t major,
266 uint32_t minor,
267 bool session_name_contains_creation_time)
268 {
269 int ret;
270 struct relay_session *session = NULL;
271
272 assert(session_name);
273 assert(hostname);
274 assert(base_path);
275
276 if (!is_name_path_safe(session_name)) {
277 ERR("Refusing to create session as the provided session name is not path-safe");
278 goto error;
279 }
280 if (!is_name_path_safe(hostname)) {
281 ERR("Refusing to create session as the provided hostname is not path-safe");
282 goto error;
283 }
284 if (strstr(base_path, "../")) {
285 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
286 base_path);
287 goto error;
288 }
289
290 session = zmalloc(sizeof(*session));
291 if (!session) {
292 PERROR("Failed to allocate session");
293 goto error;
294 }
295 if (lttng_strncpy(session->session_name, session_name,
296 sizeof(session->session_name))) {
297 WARN("Session name exceeds maximal allowed length");
298 goto error;
299 }
300 if (lttng_strncpy(session->hostname, hostname,
301 sizeof(session->hostname))) {
302 WARN("Hostname exceeds maximal allowed length");
303 goto error;
304 }
305 if (lttng_strncpy(session->base_path, base_path,
306 sizeof(session->base_path))) {
307 WARN("Base path exceeds maximal allowed length");
308 goto error;
309 }
310 if (creation_time) {
311 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
312 }
313 session->session_name_contains_creation_time =
314 session_name_contains_creation_time;
315
316 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
317 if (!session->ctf_traces_ht) {
318 goto error;
319 }
320
321 pthread_mutex_lock(&last_relay_session_id_lock);
322 session->id = ++last_relay_session_id;
323 pthread_mutex_unlock(&last_relay_session_id_lock);
324
325 session->major = major;
326 session->minor = minor;
327 lttng_ht_node_init_u64(&session->session_n, session->id);
328 urcu_ref_init(&session->ref);
329 CDS_INIT_LIST_HEAD(&session->recv_list);
330 pthread_mutex_init(&session->lock, NULL);
331 pthread_mutex_init(&session->recv_list_lock, NULL);
332
333 session->live_timer = live_timer;
334 session->snapshot = snapshot;
335 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
336
337 if (id_sessiond) {
338 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
339 }
340
341 if (major == 2 && minor >= 11) {
342 /* Only applies for 2.11+ peers using trace chunks. */
343 ret = init_session_output_path(session);
344 if (ret) {
345 goto error;
346 }
347 }
348
349 ret = sessiond_trace_chunk_registry_session_created(
350 sessiond_trace_chunk_registry, sessiond_uuid);
351 if (ret) {
352 goto error;
353 }
354
355 if (id_sessiond && current_chunk_id) {
356 session->current_trace_chunk =
357 sessiond_trace_chunk_registry_get_chunk(
358 sessiond_trace_chunk_registry,
359 session->sessiond_uuid,
360 session->id_sessiond.value,
361 *current_chunk_id);
362 if (!session->current_trace_chunk) {
363 char uuid_str[LTTNG_UUID_STR_LEN];
364
365 lttng_uuid_to_str(sessiond_uuid, uuid_str);
366 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
367 uuid_str, *id_sessiond,
368 *current_chunk_id);
369 }
370 } else if (!id_sessiond) {
371 /*
372 * Pre-2.11 peers will not announce trace chunks. An
373 * anonymous trace chunk which will remain set for the
374 * duration of the session is created.
375 */
376 ret = session_set_anonymous_chunk(session);
377 if (ret) {
378 goto error;
379 }
380 }
381
382 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
383 return session;
384
385 error:
386 session_put(session);
387 return NULL;
388 }
389
390 /* Should be called with RCU read-side lock held. */
391 bool session_get(struct relay_session *session)
392 {
393 return urcu_ref_get_unless_zero(&session->ref);
394 }
395
396 /*
397 * Lookup a session within the session hash table using the session id
398 * as key. A session reference is taken when a session is returned.
399 * session_put() must be called on that session.
400 *
401 * Return session or NULL if not found.
402 */
403 struct relay_session *session_get_by_id(uint64_t id)
404 {
405 struct relay_session *session = NULL;
406 struct lttng_ht_node_u64 *node;
407 struct lttng_ht_iter iter;
408
409 rcu_read_lock();
410 lttng_ht_lookup(sessions_ht, &id, &iter);
411 node = lttng_ht_iter_get_node_u64(&iter);
412 if (!node) {
413 DBG("Session find by ID %" PRIu64 " id NOT found", id);
414 goto end;
415 }
416 session = caa_container_of(node, struct relay_session, session_n);
417 DBG("Session find by ID %" PRIu64 " id found", id);
418 if (!session_get(session)) {
419 session = NULL;
420 }
421 end:
422 rcu_read_unlock();
423 return session;
424 }
425
426 static void rcu_destroy_session(struct rcu_head *rcu_head)
427 {
428 struct relay_session *session =
429 caa_container_of(rcu_head, struct relay_session,
430 rcu_node);
431 /*
432 * Since each trace has a reference on the session, it means
433 * that if we are at the point where we teardown the session, no
434 * trace belonging to that session exist at this point.
435 * Calling lttng_ht_destroy in call_rcu worker thread so we
436 * don't hold the RCU read-side lock while calling it.
437 */
438 lttng_ht_destroy(session->ctf_traces_ht);
439 free(session);
440 }
441
442 /*
443 * Delete session from the given hash table.
444 *
445 * Return lttng ht del error code being 0 on success and 1 on failure.
446 */
447 static int session_delete(struct relay_session *session)
448 {
449 struct lttng_ht_iter iter;
450
451 iter.iter.node = &session->session_n.node;
452 return lttng_ht_del(sessions_ht, &iter);
453 }
454
455
456 static void destroy_session(struct relay_session *session)
457 {
458 int ret;
459
460 ret = session_delete(session);
461 assert(!ret);
462 lttng_trace_chunk_put(session->current_trace_chunk);
463 session->current_trace_chunk = NULL;
464 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
465 session->pending_closure_trace_chunk = NULL;
466 ret = sessiond_trace_chunk_registry_session_destroyed(
467 sessiond_trace_chunk_registry, session->sessiond_uuid);
468 assert(!ret);
469 call_rcu(&session->rcu_node, rcu_destroy_session);
470 }
471
472 static void session_release(struct urcu_ref *ref)
473 {
474 struct relay_session *session =
475 caa_container_of(ref, struct relay_session, ref);
476
477 destroy_session(session);
478 }
479
480 void session_put(struct relay_session *session)
481 {
482 if (!session) {
483 return;
484 }
485 rcu_read_lock();
486 urcu_ref_put(&session->ref, session_release);
487 rcu_read_unlock();
488 }
489
490 int session_close(struct relay_session *session)
491 {
492 int ret = 0;
493 struct ctf_trace *trace;
494 struct lttng_ht_iter iter;
495 struct relay_stream *stream;
496
497 pthread_mutex_lock(&session->lock);
498 DBG("closing session %" PRIu64 ": is conn already closed %d",
499 session->id, session->connection_closed);
500 session->connection_closed = true;
501 pthread_mutex_unlock(&session->lock);
502
503 rcu_read_lock();
504 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
505 &iter.iter, trace, node.node) {
506 ret = ctf_trace_close(trace);
507 if (ret) {
508 goto rcu_unlock;
509 }
510 }
511 cds_list_for_each_entry_rcu(stream, &session->recv_list,
512 recv_node) {
513 /* Close streams which have not been published yet. */
514 try_stream_close(stream);
515 }
516 rcu_unlock:
517 rcu_read_unlock();
518 if (ret) {
519 return ret;
520 }
521 /* Put self-reference from create. */
522 session_put(session);
523 return ret;
524 }
525
526 int session_abort(struct relay_session *session)
527 {
528 int ret = 0;
529
530 if (!session) {
531 return 0;
532 }
533
534 pthread_mutex_lock(&session->lock);
535 DBG("aborting session %" PRIu64, session->id);
536 session->aborted = true;
537 pthread_mutex_unlock(&session->lock);
538 return ret;
539 }
540
541 void print_sessions(void)
542 {
543 struct lttng_ht_iter iter;
544 struct relay_session *session;
545
546 if (!sessions_ht) {
547 return;
548 }
549
550 rcu_read_lock();
551 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
552 session_n.node) {
553 if (!session_get(session)) {
554 continue;
555 }
556 DBG("session %p refcount %ld session %" PRIu64,
557 session,
558 session->ref.refcount,
559 session->id);
560 session_put(session);
561 }
562 rcu_read_unlock();
563 }
564
565 struct lttng_directory_handle *session_create_output_directory_handle(
566 struct relay_session *session)
567 {
568 int ret;
569 /*
570 * relayd_output_path/session_directory
571 * e.g. /home/user/lttng-traces/hostname/session_name
572 */
573 char *full_session_path = NULL;
574 struct lttng_directory_handle *handle = NULL;
575
576 pthread_mutex_lock(&session->lock);
577 full_session_path = create_output_path(session->output_path);
578 if (!full_session_path) {
579 goto end;
580 }
581
582 ret = utils_mkdir_recursive(
583 full_session_path, S_IRWXU | S_IRWXG, -1, -1);
584 if (ret) {
585 ERR("Failed to create session output path \"%s\"",
586 full_session_path);
587 goto end;
588 }
589
590 handle = lttng_directory_handle_create(full_session_path);
591 end:
592 pthread_mutex_unlock(&session->lock);
593 free(full_session_path);
594 return handle;
595 }
This page took 0.063928 seconds and 3 git commands to generate.