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