Add rmdirat and renameat to run-as commands
[lttng-tools.git] / src / bin / lttng-sessiond / session.c
CommitLineData
5b74c7b1
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
91d76f53 7 *
5b74c7b1
DG
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d14d33bf 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b74c7b1
DG
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5b74c7b1
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
6c9cc2ab 19#include <limits.h>
d022620a 20#include <inttypes.h>
5b74c7b1
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
a304c14c 24#include <sys/stat.h>
f6a9efaa 25#include <urcu.h>
3d071855
MD
26#include <dirent.h>
27#include <sys/types.h>
99d688f2 28#include <pthread.h>
5b74c7b1 29
990570ed 30#include <common/common.h>
82b69413
JG
31#include <common/utils.h>
32#include <common/trace-chunk.h>
db758600 33#include <common/sessiond-comm/sessiond-comm.h>
5d65beab 34#include <lttng/location-internal.h>
e32d7f27
JG
35#include "lttng-sessiond.h"
36#include "kernel.h"
1e307fab 37
5b74c7b1 38#include "session.h"
23324029 39#include "utils.h"
dd73d57b 40#include "trace-ust.h"
a7333da7 41#include "timer.h"
5b74c7b1 42
3e3665b8
JG
43struct ltt_session_destroy_notifier_element {
44 ltt_session_destroy_notifier notifier;
45 void *user_data;
46};
47
8c0faa1d 48/*
b5541356 49 * NOTES:
8c0faa1d 50 *
b5541356
DG
51 * No ltt_session.lock is taken here because those data structure are widely
52 * spread across the lttng-tools code base so before caling functions below
53 * that can read/write a session, the caller MUST acquire the session lock
54d01ffb 54 * using session_lock() and session_unlock().
8c0faa1d 55 */
8c0faa1d 56
5b74c7b1 57/*
b5541356 58 * Init tracing session list.
5b74c7b1 59 *
b5541356 60 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 61 */
b5541356
DG
62static struct ltt_session_list ltt_session_list = {
63 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
64 .lock = PTHREAD_MUTEX_INITIALIZER,
99d688f2 65 .removal_cond = PTHREAD_COND_INITIALIZER,
a24f7994 66 .next_uuid = 0,
b5541356 67};
5b74c7b1 68
1c1c3634
DG
69/* These characters are forbidden in a session name. Used by validate_name. */
70static const char *forbidden_name_chars = "/";
71
23324029
JD
72/* Global hash table to keep the sessions, indexed by id. */
73static struct lttng_ht *ltt_sessions_ht_by_id = NULL;
74
1c1c3634
DG
75/*
76 * Validate the session name for forbidden characters.
77 *
78 * Return 0 on success else -1 meaning a forbidden char. has been found.
79 */
80static int validate_name(const char *name)
81{
82 int ret;
83 char *tok, *tmp_name;
84
85 assert(name);
86
87 tmp_name = strdup(name);
88 if (!tmp_name) {
89 /* ENOMEM here. */
90 ret = -1;
91 goto error;
92 }
93
94 tok = strpbrk(tmp_name, forbidden_name_chars);
95 if (tok) {
96 DBG("Session name %s contains a forbidden character", name);
97 /* Forbidden character has been found. */
98 ret = -1;
99 goto error;
100 }
101 ret = 0;
102
103error:
104 free(tmp_name);
105 return ret;
106}
107
5b74c7b1 108/*
050349bb 109 * Add a ltt_session structure to the global list.
5b74c7b1 110 *
050349bb 111 * The caller MUST acquire the session list lock before.
44e96653 112 * Returns the unique identifier for the session.
5b74c7b1 113 */
d022620a 114static uint64_t add_session_list(struct ltt_session *ls)
5b74c7b1 115{
0525e9ae
DG
116 assert(ls);
117
5b74c7b1 118 cds_list_add(&ls->list, &ltt_session_list.head);
a24f7994 119 return ltt_session_list.next_uuid++;
5b74c7b1
DG
120}
121
122/*
050349bb 123 * Delete a ltt_session structure to the global list.
b5541356 124 *
050349bb 125 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
126 */
127static void del_session_list(struct ltt_session *ls)
128{
0525e9ae
DG
129 assert(ls);
130
5b74c7b1 131 cds_list_del(&ls->list);
5b74c7b1
DG
132}
133
b5541356 134/*
050349bb 135 * Return a pointer to the session list.
b5541356 136 */
54d01ffb 137struct ltt_session_list *session_get_list(void)
b5541356
DG
138{
139 return &ltt_session_list;
140}
141
99d688f2
JG
142/*
143 * Returns once the session list is empty.
144 */
145void session_list_wait_empty(void)
146{
147 pthread_mutex_lock(&ltt_session_list.lock);
148 while (!cds_list_empty(&ltt_session_list.head)) {
149 pthread_cond_wait(&ltt_session_list.removal_cond,
150 &ltt_session_list.lock);
151 }
152 pthread_mutex_unlock(&ltt_session_list.lock);
153}
154
b5541356 155/*
6c9cc2ab 156 * Acquire session list lock
b5541356 157 */
54d01ffb 158void session_lock_list(void)
b5541356 159{
6c9cc2ab 160 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
161}
162
71e0a100
JG
163/*
164 * Try to acquire session list lock
165 */
166int session_trylock_list(void)
167{
168 return pthread_mutex_trylock(&ltt_session_list.lock);
169}
170
b5541356 171/*
6c9cc2ab 172 * Release session list lock
b5541356 173 */
54d01ffb 174void session_unlock_list(void)
b5541356 175{
6c9cc2ab 176 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
177}
178
dd73d57b
JG
179/*
180 * Get the session's consumer destination type.
181 *
182 * The caller must hold the session lock.
183 */
184enum consumer_dst_type session_get_consumer_destination_type(
185 const struct ltt_session *session)
186{
187 /*
188 * The output information is duplicated in both of those session types.
189 * Hence, it doesn't matter from which it is retrieved. However, it is
190 * possible for only one of them to be set.
191 */
192 return session->kernel_session ?
193 session->kernel_session->consumer->type :
194 session->ust_session->consumer->type;
195}
196
197/*
198 * Get the session's consumer network hostname.
199 * The caller must ensure that the destination is of type "net".
200 *
201 * The caller must hold the session lock.
202 */
203const char *session_get_net_consumer_hostname(const struct ltt_session *session)
204{
205 const char *hostname = NULL;
206 const struct consumer_output *output;
207
208 output = session->kernel_session ?
209 session->kernel_session->consumer :
210 session->ust_session->consumer;
211
212 /*
213 * hostname is assumed to be the same for both control and data
214 * connections.
215 */
216 switch (output->dst.net.control.dtype) {
217 case LTTNG_DST_IPV4:
218 hostname = output->dst.net.control.dst.ipv4;
219 break;
220 case LTTNG_DST_IPV6:
221 hostname = output->dst.net.control.dst.ipv6;
222 break;
223 default:
224 abort();
225 }
226 return hostname;
227}
228
229/*
230 * Get the session's consumer network control and data ports.
231 * The caller must ensure that the destination is of type "net".
232 *
233 * The caller must hold the session lock.
234 */
235void session_get_net_consumer_ports(const struct ltt_session *session,
236 uint16_t *control_port, uint16_t *data_port)
237{
238 const struct consumer_output *output;
239
240 output = session->kernel_session ?
241 session->kernel_session->consumer :
242 session->ust_session->consumer;
243 *control_port = output->dst.net.control.port;
244 *data_port = output->dst.net.data.port;
245}
246
5d65beab
JG
247/*
248 * Get the location of the latest trace archive produced by a rotation.
249 *
250 * The caller must hold the session lock.
251 */
252struct lttng_trace_archive_location *session_get_trace_archive_location(
3e3665b8 253 const struct ltt_session *session)
5d65beab 254{
d2956687 255 int ret;
5d65beab 256 struct lttng_trace_archive_location *location = NULL;
d2956687
JG
257 char *chunk_path = NULL;
258
259 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED ||
260 !session->last_archived_chunk_name) {
261 goto end;
262 }
5d65beab 263
d2956687
JG
264 ret = asprintf(&chunk_path, "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
265 session_get_base_path(session),
266 session->last_archived_chunk_name);
267 if (ret == -1) {
5d65beab
JG
268 goto end;
269 }
270
271 switch (session_get_consumer_destination_type(session)) {
272 case CONSUMER_DST_LOCAL:
273 location = lttng_trace_archive_location_local_create(
d2956687 274 chunk_path);
5d65beab
JG
275 break;
276 case CONSUMER_DST_NET:
277 {
278 const char *hostname;
279 uint16_t control_port, data_port;
280
281 hostname = session_get_net_consumer_hostname(session);
282 session_get_net_consumer_ports(session,
283 &control_port,
284 &data_port);
285 location = lttng_trace_archive_location_relay_create(
286 hostname,
287 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
d2956687 288 control_port, data_port, chunk_path);
5d65beab
JG
289 break;
290 }
291 default:
292 abort();
293 }
294end:
d2956687 295 free(chunk_path);
5d65beab
JG
296 return location;
297}
298
23324029
JD
299/*
300 * Allocate the ltt_sessions_ht_by_id HT.
9c6518bc
JG
301 *
302 * The session list lock must be held.
23324029
JD
303 */
304int ltt_sessions_ht_alloc(void)
305{
306 int ret = 0;
307
308 DBG("Allocating ltt_sessions_ht_by_id");
309 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
310 if (!ltt_sessions_ht_by_id) {
311 ret = -1;
312 ERR("Failed to allocate ltt_sessions_ht_by_id");
313 goto end;
314 }
315end:
316 return ret;
317}
318
319/*
320 * Destroy the ltt_sessions_ht_by_id HT.
9c6518bc
JG
321 *
322 * The session list lock must be held.
23324029 323 */
accdc9bf 324static void ltt_sessions_ht_destroy(void)
23324029
JD
325{
326 if (!ltt_sessions_ht_by_id) {
327 return;
328 }
329 ht_cleanup_push(ltt_sessions_ht_by_id);
330 ltt_sessions_ht_by_id = NULL;
331}
332
333/*
334 * Add a ltt_session to the ltt_sessions_ht_by_id.
335 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
336 * The session list lock must be held.
337 */
338static void add_session_ht(struct ltt_session *ls)
339{
340 int ret;
341
342 assert(ls);
343
344 if (!ltt_sessions_ht_by_id) {
345 ret = ltt_sessions_ht_alloc();
346 if (ret) {
347 ERR("Error allocating the sessions HT");
348 goto end;
349 }
350 }
351 lttng_ht_node_init_u64(&ls->node, ls->id);
352 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
353
354end:
355 return;
356}
357
358/*
359 * Test if ltt_sessions_ht_by_id is empty.
360 * Return 1 if empty, 0 if not empty.
361 * The session list lock must be held.
362 */
def88971 363static int ltt_sessions_ht_empty(void)
23324029
JD
364{
365 int ret;
366
367 if (!ltt_sessions_ht_by_id) {
368 ret = 1;
369 goto end;
370 }
371
372 ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
373end:
374 return ret;
375}
376
377/*
378 * Remove a ltt_session from the ltt_sessions_ht_by_id.
379 * If empty, the ltt_sessions_ht_by_id HT is freed.
380 * The session list lock must be held.
381 */
382static void del_session_ht(struct ltt_session *ls)
383{
384 struct lttng_ht_iter iter;
385 int ret;
386
387 assert(ls);
388 assert(ltt_sessions_ht_by_id);
389
390 iter.iter.node = &ls->node.node;
391 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
392 assert(!ret);
393
394 if (ltt_sessions_ht_empty()) {
395 DBG("Empty ltt_sessions_ht_by_id, destroying it");
396 ltt_sessions_ht_destroy();
397 }
398}
399
b5541356 400/*
6c9cc2ab 401 * Acquire session lock
b5541356 402 */
54d01ffb 403void session_lock(struct ltt_session *session)
b5541356 404{
0525e9ae
DG
405 assert(session);
406
6c9cc2ab
DG
407 pthread_mutex_lock(&session->lock);
408}
b5541356 409
6c9cc2ab
DG
410/*
411 * Release session lock
412 */
54d01ffb 413void session_unlock(struct ltt_session *session)
6c9cc2ab 414{
0525e9ae
DG
415 assert(session);
416
6c9cc2ab 417 pthread_mutex_unlock(&session->lock);
b5541356
DG
418}
419
82b69413
JG
420static
421int _session_set_trace_chunk_no_lock_check(struct ltt_session *session,
d2956687
JG
422 struct lttng_trace_chunk *new_trace_chunk,
423 struct lttng_trace_chunk **_current_trace_chunk)
82b69413
JG
424{
425 int ret;
426 unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0;
82b69413
JG
427 struct cds_lfht_iter iter;
428 struct consumer_socket *socket;
d2956687
JG
429 struct lttng_trace_chunk *current_trace_chunk;
430 uint64_t chunk_id;
431 enum lttng_trace_chunk_status chunk_status;
432 const uint64_t relayd_id = session->consumer->net_seq_index;
433 const bool is_local_trace = relayd_id == -1ULL;
82b69413 434
d2956687 435 rcu_read_lock();
82b69413 436 /*
d2956687
JG
437 * Ownership of current trace chunk is transferred to
438 * `current_trace_chunk`.
82b69413 439 */
d2956687
JG
440 current_trace_chunk = session->current_trace_chunk;
441 session->current_trace_chunk = NULL;
82b69413 442 if (session->ust_session) {
d2956687
JG
443 lttng_trace_chunk_put(
444 session->ust_session->current_trace_chunk);
445 session->ust_session->current_trace_chunk = NULL;
82b69413
JG
446 }
447 if (session->kernel_session) {
d2956687
JG
448 lttng_trace_chunk_put(
449 session->kernel_session->current_trace_chunk);
450 session->kernel_session->current_trace_chunk = NULL;
82b69413 451 }
d2956687
JG
452 if (!new_trace_chunk) {
453 ret = 0;
454 goto end;
82b69413 455 }
d2956687
JG
456 chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id);
457 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
82b69413 458
d2956687
JG
459 refs_to_acquire = 1;
460 refs_to_acquire += !!session->ust_session;
461 refs_to_acquire += !!session->kernel_session;
462
463 for (refs_acquired = 0; refs_acquired < refs_to_acquire;
464 refs_acquired++) {
465 if (!lttng_trace_chunk_get(new_trace_chunk)) {
466 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
467 session->name);
468 goto error;
82b69413
JG
469 }
470 }
471
d2956687
JG
472 if (session->ust_session) {
473 session->ust_session->current_trace_chunk = new_trace_chunk;
474 if (is_local_trace) {
475 enum lttng_error_code ret_error_code;
82b69413 476
d2956687
JG
477 ret_error_code = ust_app_create_channel_subdirectories(
478 session->ust_session);
479 if (ret_error_code != LTTNG_OK) {
480 ret = -ret_error_code;
82b69413
JG
481 goto error;
482 }
d2956687
JG
483 }
484 cds_lfht_for_each_entry(
485 session->ust_session->consumer->socks->ht,
486 &iter, socket, node.node) {
487 pthread_mutex_lock(socket->lock);
488 ret = consumer_create_trace_chunk(socket,
489 relayd_id,
490 session->id, new_trace_chunk);
491 pthread_mutex_unlock(socket->lock);
492 if (ret) {
493 goto error;
494 }
495 }
496 }
82b69413 497 if (session->kernel_session) {
d2956687
JG
498 session->kernel_session->current_trace_chunk = new_trace_chunk;
499 if (is_local_trace) {
500 enum lttng_error_code ret_error_code;
501
502 ret_error_code = kernel_create_channel_subdirectories(
503 session->kernel_session);
504 if (ret_error_code != LTTNG_OK) {
505 ret = -ret_error_code;
506 goto error;
507 }
508 }
509 cds_lfht_for_each_entry(
510 session->kernel_session->consumer->socks->ht,
511 &iter, socket, node.node) {
512 pthread_mutex_lock(socket->lock);
513 ret = consumer_create_trace_chunk(socket,
514 relayd_id,
515 session->id, new_trace_chunk);
516 pthread_mutex_unlock(socket->lock);
517 if (ret) {
518 goto error;
519 }
520 }
521 }
82b69413
JG
522
523 /*
524 * Update local current trace chunk state last, only if all remote
d2956687 525 * creations succeeded.
82b69413
JG
526 */
527 session->current_trace_chunk = new_trace_chunk;
d2956687
JG
528 LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id);
529end:
530 if (_current_trace_chunk) {
531 *_current_trace_chunk = current_trace_chunk;
532 current_trace_chunk = NULL;
533 }
534end_no_move:
535 rcu_read_unlock();
536 lttng_trace_chunk_put(current_trace_chunk);
537 return ret;
538error:
82b69413 539 if (session->ust_session) {
d2956687 540 session->ust_session->current_trace_chunk = NULL;
82b69413
JG
541 }
542 if (session->kernel_session) {
d2956687 543 session->kernel_session->current_trace_chunk = NULL;
82b69413 544 }
d2956687 545 /*
82b69413
JG
546 * Release references taken in the case where all references could not
547 * be acquired.
548 */
549 refs_to_release = refs_to_acquire - refs_acquired;
550 for (i = 0; i < refs_to_release; i++) {
551 lttng_trace_chunk_put(new_trace_chunk);
552 }
d2956687
JG
553 ret = -1;
554 goto end_no_move;
82b69413
JG
555}
556
557static
d2956687 558bool output_supports_trace_chunks(const struct ltt_session *session)
82b69413
JG
559{
560 if (session->consumer->type == CONSUMER_DST_LOCAL) {
561 return true;
562 } else {
563 struct consumer_output *output;
564
565 if (session->ust_session) {
566 output = session->ust_session->consumer;
567 } else if (session->kernel_session) {
568 output = session->kernel_session->consumer;
569 } else {
570 abort();
571 }
572
573 if (output->relay_major_version > 2) {
574 return true;
575 } else if (output->relay_major_version == 2 &&
576 output->relay_minor_version >= 11) {
577 return true;
578 }
579 }
580 return false;
581}
582
d2956687
JG
583struct lttng_trace_chunk *session_create_new_trace_chunk(
584 struct ltt_session *session,
82b69413
JG
585 const char *session_base_path_override,
586 const char *chunk_name_override)
587{
588 int ret;
82b69413
JG
589 struct lttng_trace_chunk *trace_chunk = NULL;
590 enum lttng_trace_chunk_status chunk_status;
d2956687 591 const time_t chunk_creation_ts = time(NULL);
82b69413
JG
592 const bool is_local_trace =
593 session->consumer->type == CONSUMER_DST_LOCAL;
594 const char *base_path = session_base_path_override ? :
595 session_get_base_path(session);
596 struct lttng_directory_handle session_output_directory;
597 const struct lttng_credentials session_credentials = {
598 .uid = session->uid,
599 .gid = session->gid,
600 };
601 uint64_t next_chunk_id;
602
d2956687
JG
603 if (chunk_creation_ts == (time_t) -1) {
604 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
82b69413 605 session->name);
82b69413
JG
606 goto error;
607 }
82b69413 608
d2956687 609 if (!output_supports_trace_chunks(session)) {
82b69413
JG
610 goto end;
611 }
d2956687
JG
612 next_chunk_id = session->most_recent_chunk_id.is_set ?
613 session->most_recent_chunk_id.value + 1 : 0;
82b69413 614
d2956687
JG
615 trace_chunk = lttng_trace_chunk_create(next_chunk_id,
616 chunk_creation_ts);
82b69413 617 if (!trace_chunk) {
82b69413
JG
618 goto error;
619 }
620
621 if (chunk_name_override) {
622 chunk_status = lttng_trace_chunk_override_name(trace_chunk,
623 chunk_name_override);
d2956687 624 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
625 goto error;
626 }
627 }
628
629 if (!is_local_trace) {
630 /*
631 * No need to set crendentials and output directory
632 * for remote trace chunks.
633 */
d2956687 634 goto end;
82b69413
JG
635 }
636
637 chunk_status = lttng_trace_chunk_set_credentials(trace_chunk,
638 &session_credentials);
639 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
640 goto error;
641 }
642
d2956687
JG
643 DBG("Creating base output directory of session \"%s\" at %s",
644 session->name, base_path);
82b69413
JG
645 ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG,
646 session->uid, session->gid);
647 if (ret) {
82b69413
JG
648 goto error;
649 }
650 ret = lttng_directory_handle_init(&session_output_directory,
651 base_path);
652 if (ret) {
82b69413
JG
653 goto error;
654 }
655 chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk,
656 &session_output_directory);
657 lttng_directory_handle_fini(&session_output_directory);
658 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
659 goto error;
660 }
d2956687
JG
661end:
662 return trace_chunk;
82b69413
JG
663error:
664 lttng_trace_chunk_put(trace_chunk);
d2956687
JG
665 trace_chunk = NULL;
666 goto end;
667}
668
669int session_close_trace_chunk(const struct ltt_session *session,
670 struct lttng_trace_chunk *trace_chunk)
671{
672 int ret = 0;
673 bool error_occurred = false;
674 struct cds_lfht_iter iter;
675 struct consumer_socket *socket;
676 enum lttng_trace_chunk_status chunk_status;
677 const time_t chunk_close_timestamp = time(NULL);
678
679 if (chunk_close_timestamp == (time_t) -1) {
680 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
681 session->name);
682 ret = -1;
683 goto end;
684 }
685 chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk,
686 chunk_close_timestamp);
687 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
688 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
689 session->name);
690 ret = -1;
691 goto end;
692 }
693
694 if (session->ust_session) {
695 cds_lfht_for_each_entry(
696 session->ust_session->consumer->socks->ht,
697 &iter, socket, node.node) {
698 pthread_mutex_lock(socket->lock);
699 ret = consumer_close_trace_chunk(socket,
700 session->consumer->net_seq_index,
701 session->id,
702 trace_chunk);
703 pthread_mutex_unlock(socket->lock);
704 if (ret) {
705 ERR("Failed to close trace chunk on user space consumer");
706 error_occurred = true;
707 }
708 }
709 }
710 if (session->kernel_session) {
711 cds_lfht_for_each_entry(
712 session->kernel_session->consumer->socks->ht,
713 &iter, socket, node.node) {
714 pthread_mutex_lock(socket->lock);
715 ret = consumer_close_trace_chunk(socket,
716 session->consumer->net_seq_index,
717 session->id,
718 trace_chunk);
719 pthread_mutex_unlock(socket->lock);
720 if (ret) {
721 ERR("Failed to close trace chunk on kernel consumer");
722 error_occurred = true;
723 }
724 }
725 }
726 ret = error_occurred ? -1 : 0;
82b69413 727end:
d2956687 728 return ret;
82b69413
JG
729}
730
731/*
732 * Set a session's current trace chunk.
733 *
734 * Must be called with the session lock held.
735 */
736int session_set_trace_chunk(struct ltt_session *session,
d2956687
JG
737 struct lttng_trace_chunk *new_trace_chunk,
738 struct lttng_trace_chunk **current_trace_chunk)
82b69413
JG
739{
740 ASSERT_LOCKED(session->lock);
d2956687
JG
741 return _session_set_trace_chunk_no_lock_check(session, new_trace_chunk,
742 current_trace_chunk);
82b69413
JG
743}
744
3e3665b8
JG
745static
746void session_notify_destruction(const struct ltt_session *session)
747{
748 size_t i;
749 const size_t count = lttng_dynamic_array_get_count(
750 &session->destroy_notifiers);
751
752 for (i = 0; i < count; i++) {
753 const struct ltt_session_destroy_notifier_element *element =
754 lttng_dynamic_array_get_element(
755 &session->destroy_notifiers, i);
756
757 element->notifier(session, element->user_data);
758 }
759}
760
e32d7f27
JG
761static
762void session_release(struct urcu_ref *ref)
763{
764 int ret;
765 struct ltt_ust_session *usess;
766 struct ltt_kernel_session *ksess;
767 struct ltt_session *session = container_of(ref, typeof(*session), ref);
768
d2956687
JG
769 assert(!session->chunk_being_archived);
770
e32d7f27
JG
771 usess = session->ust_session;
772 ksess = session->kernel_session;
3e3665b8
JG
773
774 session_notify_destruction(session);
93bed9fe 775 lttng_dynamic_array_reset(&session->destroy_notifiers);
d2956687
JG
776 if (session->current_trace_chunk) {
777 ret = session_close_trace_chunk(session, session->current_trace_chunk);
778 if (ret) {
779 ERR("Failed to close the current trace chunk of session \"%s\" during its release",
780 session->name);
781 }
782 ret = _session_set_trace_chunk_no_lock_check(session, NULL, NULL);
783 if (ret) {
784 ERR("Failed to release the current trace chunk of session \"%s\" during its release",
785 session->name);
786 }
787 }
e32d7f27 788
d2956687 789 /* Clean kernel session teardown */
e32d7f27 790 kernel_destroy_session(ksess);
82b69413 791 session->kernel_session = NULL;
e32d7f27
JG
792
793 /* UST session teardown */
794 if (usess) {
795 /* Close any relayd session */
796 consumer_output_send_destroy_relayd(usess->consumer);
797
798 /* Destroy every UST application related to this session. */
799 ret = ust_app_destroy_trace_all(usess);
800 if (ret) {
801 ERR("Error in ust_app_destroy_trace_all");
802 }
803
804 /* Clean up the rest. */
805 trace_ust_destroy_session(usess);
82b69413 806 session->ust_session = NULL;
e32d7f27
JG
807 }
808
809 /*
810 * Must notify the kernel thread here to update it's poll set in order to
811 * remove the channel(s)' fd just destroyed.
812 */
813 ret = notify_thread_pipe(kernel_poll_pipe[1]);
814 if (ret < 0) {
815 PERROR("write kernel poll pipe");
816 }
817
818 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
e32d7f27
JG
819
820 consumer_output_put(session->consumer);
821 snapshot_destroy(&session->snapshot);
99d688f2 822
82b69413
JG
823 pthread_mutex_destroy(&session->lock);
824
f4cc5e83
JG
825 if (session->published) {
826 ASSERT_LOCKED(ltt_session_list.lock);
827 del_session_list(session);
828 del_session_ht(session);
829 pthread_cond_broadcast(&ltt_session_list.removal_cond);
830 }
d2956687 831 free(session->last_archived_chunk_name);
e32d7f27
JG
832 free(session);
833}
834
835/*
836 * Acquire a reference to a session.
837 * This function may fail (return false); its return value must be checked.
838 */
839bool session_get(struct ltt_session *session)
840{
841 return urcu_ref_get_unless_zero(&session->ref);
842}
843
844/*
845 * Release a reference to a session.
846 */
847void session_put(struct ltt_session *session)
848{
b178f53e
JG
849 if (!session) {
850 return;
851 }
e32d7f27
JG
852 /*
853 * The session list lock must be held as any session_put()
854 * may cause the removal of the session from the session_list.
855 */
856 ASSERT_LOCKED(ltt_session_list.lock);
857 assert(session->ref.refcount);
858 urcu_ref_put(&session->ref, session_release);
859}
860
861/*
862 * Destroy a session.
863 *
864 * This method does not immediately release/free the session as other
865 * components may still hold a reference to the session. However,
866 * the session should no longer be presented to the user.
867 *
868 * Releases the session list's reference to the session
869 * and marks it as destroyed. Iterations on the session list should be
870 * mindful of the "destroyed" flag.
871 */
872void session_destroy(struct ltt_session *session)
873{
874 assert(!session->destroyed);
875 session->destroyed = true;
876 session_put(session);
877}
878
3e3665b8
JG
879int session_add_destroy_notifier(struct ltt_session *session,
880 ltt_session_destroy_notifier notifier, void *user_data)
881{
882 const struct ltt_session_destroy_notifier_element element = {
883 .notifier = notifier,
884 .user_data = user_data
885 };
886
887 return lttng_dynamic_array_add_element(&session->destroy_notifiers,
888 &element);
889}
890
5b74c7b1 891/*
74babd95 892 * Return a ltt_session structure ptr that matches name. If no session found,
23324029 893 * NULL is returned. This must be called with the session list lock held using
74babd95 894 * session_lock_list and session_unlock_list.
e32d7f27 895 * A reference to the session is implicitly acquired by this function.
5b74c7b1 896 */
58a1a227 897struct ltt_session *session_find_by_name(const char *name)
5b74c7b1 898{
5b74c7b1
DG
899 struct ltt_session *iter;
900
0525e9ae 901 assert(name);
e32d7f27 902 ASSERT_LOCKED(ltt_session_list.lock);
0525e9ae 903
5f822d0a
DG
904 DBG2("Trying to find session by name %s", name);
905
5b74c7b1 906 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
e32d7f27
JG
907 if (!strncmp(iter->name, name, NAME_MAX) &&
908 !iter->destroyed) {
74babd95 909 goto found;
5b74c7b1
DG
910 }
911 }
912
e32d7f27 913 return NULL;
74babd95 914found:
e32d7f27 915 return session_get(iter) ? iter : NULL;
5b74c7b1
DG
916}
917
23324029
JD
918/*
919 * Return an ltt_session that matches the id. If no session is found,
920 * NULL is returned. This must be called with rcu_read_lock and
921 * session list lock held (to guarantee the lifetime of the session).
922 */
923struct ltt_session *session_find_by_id(uint64_t id)
924{
925 struct lttng_ht_node_u64 *node;
926 struct lttng_ht_iter iter;
927 struct ltt_session *ls;
928
e32d7f27
JG
929 ASSERT_LOCKED(ltt_session_list.lock);
930
d68ec974
JG
931 if (!ltt_sessions_ht_by_id) {
932 goto end;
933 }
934
23324029
JD
935 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
936 node = lttng_ht_iter_get_node_u64(&iter);
937 if (node == NULL) {
d68ec974 938 goto end;
23324029
JD
939 }
940 ls = caa_container_of(node, struct ltt_session, node);
941
942 DBG3("Session %" PRIu64 " found by id.", id);
e32d7f27 943 return session_get(ls) ? ls : NULL;
23324029 944
d68ec974 945end:
23324029
JD
946 DBG3("Session %" PRIu64 " NOT found by id", id);
947 return NULL;
948}
949
5b74c7b1 950/*
b178f53e
JG
951 * Create a new session and add it to the session list.
952 * Session list lock must be held by the caller.
5b74c7b1 953 */
b178f53e
JG
954enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
955 struct ltt_session **out_session)
5b74c7b1 956{
f3ed775e 957 int ret;
b178f53e
JG
958 enum lttng_error_code ret_code;
959 struct ltt_session *new_session = NULL;
e07ae692 960
b178f53e
JG
961 ASSERT_LOCKED(ltt_session_list.lock);
962 if (name) {
963 struct ltt_session *clashing_session;
964
965 clashing_session = session_find_by_name(name);
966 if (clashing_session) {
967 session_put(clashing_session);
968 ret_code = LTTNG_ERR_EXIST_SESS;
969 goto error;
970 }
971 }
ba7f0ae5 972 new_session = zmalloc(sizeof(struct ltt_session));
b178f53e
JG
973 if (!new_session) {
974 PERROR("Failed to allocate an ltt_session structure");
975 ret_code = LTTNG_ERR_NOMEM;
976 goto error;
5b74c7b1
DG
977 }
978
3e3665b8 979 lttng_dynamic_array_init(&new_session->destroy_notifiers,
93bed9fe
JG
980 sizeof(struct ltt_session_destroy_notifier_element),
981 NULL);
e32d7f27 982 urcu_ref_init(&new_session->ref);
b178f53e 983 pthread_mutex_init(&new_session->lock, NULL);
e32d7f27 984
b178f53e
JG
985 new_session->creation_time = time(NULL);
986 if (new_session->creation_time == (time_t) -1) {
987 PERROR("Failed to sample session creation time");
988 ret_code = LTTNG_ERR_SESSION_FAIL;
f3ed775e
DG
989 goto error;
990 }
991
b178f53e
JG
992 /* Create default consumer output. */
993 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
994 if (new_session->consumer == NULL) {
995 ret_code = LTTNG_ERR_NOMEM;
1c1c3634
DG
996 goto error;
997 }
998
b178f53e
JG
999 if (name) {
1000 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1001 if (ret) {
1002 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1003 goto error;
1004 }
1005 ret = validate_name(name);
1006 if (ret < 0) {
1007 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1008 goto error;
1009 }
1010 } else {
1011 int i = 0;
1012 bool found_name = false;
1013 char datetime[16];
1014 struct tm *timeinfo;
1015
1016 timeinfo = localtime(&new_session->creation_time);
1017 if (!timeinfo) {
1018 ret_code = LTTNG_ERR_SESSION_FAIL;
1019 goto error;
1020 }
1021 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1022 for (i = 0; i < INT_MAX; i++) {
1023 struct ltt_session *clashing_session;
1024
1025 if (i == 0) {
1026 ret = snprintf(new_session->name,
1027 sizeof(new_session->name),
1028 "%s-%s",
1029 DEFAULT_SESSION_NAME,
1030 datetime);
1031 } else {
1032 ret = snprintf(new_session->name,
1033 sizeof(new_session->name),
1034 "%s%d-%s",
1035 DEFAULT_SESSION_NAME, i,
1036 datetime);
1037 }
1038 if (ret == -1 || ret >= sizeof(new_session->name)) {
1039 /*
1040 * Null-terminate in case the name is used
1041 * in logging statements.
1042 */
1043 new_session->name[sizeof(new_session->name) - 1] = '\0';
1044 ret_code = LTTNG_ERR_SESSION_FAIL;
1045 goto error;
1046 }
1047
1048 clashing_session =
1049 session_find_by_name(new_session->name);
1050 session_put(clashing_session);
1051 if (!clashing_session) {
1052 found_name = true;
1053 break;
1054 }
1055 }
1056 if (found_name) {
1057 DBG("Generated session name \"%s\"", new_session->name);
1058 new_session->has_auto_generated_name = true;
1059 } else {
1060 ERR("Failed to auto-generate a session name");
1061 ret_code = LTTNG_ERR_SESSION_FAIL;
1062 goto error;
1063 }
1064 }
1065
d3e2ba59 1066 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
73184835
DG
1067 if (ret < 0) {
1068 if (errno == ENAMETOOLONG) {
1069 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
b178f53e
JG
1070 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1071 new_session->hostname);
73184835 1072 } else {
b178f53e 1073 ret_code = LTTNG_ERR_SESSION_FAIL;
73184835
DG
1074 goto error;
1075 }
d3e2ba59
JD
1076 }
1077
6df2e2c9
MD
1078 new_session->uid = uid;
1079 new_session->gid = gid;
1080
6dc3064a
DG
1081 ret = snapshot_init(&new_session->snapshot);
1082 if (ret < 0) {
b178f53e 1083 ret_code = LTTNG_ERR_NOMEM;
6dc3064a
DG
1084 goto error;
1085 }
1086
4f23c583 1087 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
92816cc3 1088
b178f53e 1089 /* Add new session to the session list. */
a991f516 1090 new_session->id = add_session_list(new_session);
b178f53e 1091
23324029
JD
1092 /*
1093 * Add the new session to the ltt_sessions_ht_by_id.
1094 * No ownership is taken by the hash table; it is merely
1095 * a wrapper around the session list used for faster access
1096 * by session id.
1097 */
1098 add_session_ht(new_session);
f4cc5e83 1099 new_session->published = true;
b5541356 1100
a4b92340 1101 /*
b178f53e
JG
1102 * Consumer is left to NULL since the create_session_uri command will
1103 * set it up and, if valid, assign it to the session.
a4b92340 1104 */
b178f53e
JG
1105 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
1106 new_session->name, new_session->id, new_session->uid,
1107 new_session->gid);
1108 ret_code = LTTNG_OK;
1109end:
1110 if (new_session) {
1111 (void) session_get(new_session);
1112 *out_session = new_session;
1113 }
1114 return ret_code;
5b74c7b1 1115error:
f4cc5e83 1116 session_put(new_session);
b178f53e
JG
1117 new_session = NULL;
1118 goto end;
5b74c7b1 1119}
2f77fc4b
DG
1120
1121/*
1122 * Check if the UID or GID match the session. Root user has access to all
1123 * sessions.
1124 */
1125int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
1126{
1127 assert(session);
1128
1129 if (uid != session->uid && gid != session->gid && uid != 0) {
1130 return 0;
1131 } else {
1132 return 1;
1133 }
1134}
2961f09e
JG
1135
1136/*
1137 * Set a session's rotation state and reset all associated state.
1138 *
1139 * This function resets the rotation state (check timers, pending
1140 * flags, etc.) and sets the result of the last rotation. The result
1141 * can be queries by a liblttng-ctl client.
1142 *
1143 * Be careful of the result passed to this function. For instance,
1144 * on failure to launch a rotation, a client will expect the rotation
1145 * state to be set to "NO_ROTATION". If an error occured while the
1146 * rotation was "ONGOING", result should be set to "ERROR", which will
1147 * allow a client to report it.
1148 *
1149 * Must be called with the session and session_list locks held.
1150 */
1151int session_reset_rotation_state(struct ltt_session *session,
1152 enum lttng_rotation_state result)
1153{
1154 int ret = 0;
1155
1156 ASSERT_LOCKED(ltt_session_list.lock);
1157 ASSERT_LOCKED(session->lock);
1158
2961f09e
JG
1159 session->rotation_state = result;
1160 if (session->rotation_pending_check_timer_enabled) {
1161 ret = timer_session_rotation_pending_check_stop(session);
1162 }
d2956687
JG
1163 if (session->chunk_being_archived) {
1164 uint64_t chunk_id;
1165 enum lttng_trace_chunk_status chunk_status;
1166
1167 chunk_status = lttng_trace_chunk_get_id(
1168 session->chunk_being_archived,
1169 &chunk_id);
1170 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
1171 LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id,
1172 chunk_id);
1173 lttng_trace_chunk_put(session->chunk_being_archived);
1174 session->chunk_being_archived = NULL;
1175 }
2961f09e
JG
1176 return ret;
1177}
This page took 0.107083 seconds and 4 git commands to generate.