2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
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.
27 #include <sys/types.h>
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <lttng/location-internal.h>
33 #include "lttng-sessiond.h"
38 #include "trace-ust.h"
44 * No ltt_session.lock is taken here because those data structure are widely
45 * spread across the lttng-tools code base so before caling functions below
46 * that can read/write a session, the caller MUST acquire the session lock
47 * using session_lock() and session_unlock().
51 * Init tracing session list.
53 * Please see session.h for more explanation and correct usage of the list.
55 static struct ltt_session_list ltt_session_list
= {
56 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
57 .lock
= PTHREAD_MUTEX_INITIALIZER
,
58 .removal_cond
= PTHREAD_COND_INITIALIZER
,
62 /* These characters are forbidden in a session name. Used by validate_name. */
63 static const char *forbidden_name_chars
= "/";
65 /* Global hash table to keep the sessions, indexed by id. */
66 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
69 * Validate the session name for forbidden characters.
71 * Return 0 on success else -1 meaning a forbidden char. has been found.
73 static int validate_name(const char *name
)
80 tmp_name
= strdup(name
);
87 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
89 DBG("Session name %s contains a forbidden character", name
);
90 /* Forbidden character has been found. */
102 * Add a ltt_session structure to the global list.
104 * The caller MUST acquire the session list lock before.
105 * Returns the unique identifier for the session.
107 static uint64_t add_session_list(struct ltt_session
*ls
)
111 cds_list_add(&ls
->list
, <t_session_list
.head
);
112 return ltt_session_list
.next_uuid
++;
116 * Delete a ltt_session structure to the global list.
118 * The caller MUST acquire the session list lock before.
120 static void del_session_list(struct ltt_session
*ls
)
124 cds_list_del(&ls
->list
);
128 * Return a pointer to the session list.
130 struct ltt_session_list
*session_get_list(void)
132 return <t_session_list
;
136 * Returns once the session list is empty.
138 void session_list_wait_empty(void)
140 pthread_mutex_lock(<t_session_list
.lock
);
141 while (!cds_list_empty(<t_session_list
.head
)) {
142 pthread_cond_wait(<t_session_list
.removal_cond
,
143 <t_session_list
.lock
);
145 pthread_mutex_unlock(<t_session_list
.lock
);
149 * Acquire session list lock
151 void session_lock_list(void)
153 pthread_mutex_lock(<t_session_list
.lock
);
157 * Try to acquire session list lock
159 int session_trylock_list(void)
161 return pthread_mutex_trylock(<t_session_list
.lock
);
165 * Release session list lock
167 void session_unlock_list(void)
169 pthread_mutex_unlock(<t_session_list
.lock
);
173 * Get the session's consumer destination type.
175 * The caller must hold the session lock.
177 enum consumer_dst_type
session_get_consumer_destination_type(
178 const struct ltt_session
*session
)
181 * The output information is duplicated in both of those session types.
182 * Hence, it doesn't matter from which it is retrieved. However, it is
183 * possible for only one of them to be set.
185 return session
->kernel_session
?
186 session
->kernel_session
->consumer
->type
:
187 session
->ust_session
->consumer
->type
;
191 * Get the session's consumer network hostname.
192 * The caller must ensure that the destination is of type "net".
194 * The caller must hold the session lock.
196 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
198 const char *hostname
= NULL
;
199 const struct consumer_output
*output
;
201 output
= session
->kernel_session
?
202 session
->kernel_session
->consumer
:
203 session
->ust_session
->consumer
;
206 * hostname is assumed to be the same for both control and data
209 switch (output
->dst
.net
.control
.dtype
) {
211 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
214 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
223 * Get the session's consumer network control and data ports.
224 * The caller must ensure that the destination is of type "net".
226 * The caller must hold the session lock.
228 void session_get_net_consumer_ports(const struct ltt_session
*session
,
229 uint16_t *control_port
, uint16_t *data_port
)
231 const struct consumer_output
*output
;
233 output
= session
->kernel_session
?
234 session
->kernel_session
->consumer
:
235 session
->ust_session
->consumer
;
236 *control_port
= output
->dst
.net
.control
.port
;
237 *data_port
= output
->dst
.net
.data
.port
;
241 * Get the location of the latest trace archive produced by a rotation.
243 * The caller must hold the session lock.
245 struct lttng_trace_archive_location
*session_get_trace_archive_location(
246 struct ltt_session
*session
)
248 struct lttng_trace_archive_location
*location
= NULL
;
250 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
254 switch (session_get_consumer_destination_type(session
)) {
255 case CONSUMER_DST_LOCAL
:
256 location
= lttng_trace_archive_location_local_create(
257 session
->rotation_chunk
.current_rotate_path
);
259 case CONSUMER_DST_NET
:
261 const char *hostname
;
262 uint16_t control_port
, data_port
;
264 hostname
= session_get_net_consumer_hostname(session
);
265 session_get_net_consumer_ports(session
,
268 location
= lttng_trace_archive_location_relay_create(
270 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
271 control_port
, data_port
,
272 session
->rotation_chunk
.current_rotate_path
);
283 * Allocate the ltt_sessions_ht_by_id HT.
285 * The session list lock must be held.
287 int ltt_sessions_ht_alloc(void)
291 DBG("Allocating ltt_sessions_ht_by_id");
292 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
293 if (!ltt_sessions_ht_by_id
) {
295 ERR("Failed to allocate ltt_sessions_ht_by_id");
303 * Destroy the ltt_sessions_ht_by_id HT.
305 * The session list lock must be held.
307 static void ltt_sessions_ht_destroy(void)
309 if (!ltt_sessions_ht_by_id
) {
312 ht_cleanup_push(ltt_sessions_ht_by_id
);
313 ltt_sessions_ht_by_id
= NULL
;
317 * Add a ltt_session to the ltt_sessions_ht_by_id.
318 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
319 * The session list lock must be held.
321 static void add_session_ht(struct ltt_session
*ls
)
327 if (!ltt_sessions_ht_by_id
) {
328 ret
= ltt_sessions_ht_alloc();
330 ERR("Error allocating the sessions HT");
334 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
335 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
342 * Test if ltt_sessions_ht_by_id is empty.
343 * Return 1 if empty, 0 if not empty.
344 * The session list lock must be held.
346 static int ltt_sessions_ht_empty(void)
350 if (!ltt_sessions_ht_by_id
) {
355 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
361 * Remove a ltt_session from the ltt_sessions_ht_by_id.
362 * If empty, the ltt_sessions_ht_by_id HT is freed.
363 * The session list lock must be held.
365 static void del_session_ht(struct ltt_session
*ls
)
367 struct lttng_ht_iter iter
;
371 assert(ltt_sessions_ht_by_id
);
373 iter
.iter
.node
= &ls
->node
.node
;
374 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
377 if (ltt_sessions_ht_empty()) {
378 DBG("Empty ltt_sessions_ht_by_id, destroying it");
379 ltt_sessions_ht_destroy();
384 * Acquire session lock
386 void session_lock(struct ltt_session
*session
)
390 pthread_mutex_lock(&session
->lock
);
394 * Release session lock
396 void session_unlock(struct ltt_session
*session
)
400 pthread_mutex_unlock(&session
->lock
);
404 void session_release(struct urcu_ref
*ref
)
407 struct ltt_ust_session
*usess
;
408 struct ltt_kernel_session
*ksess
;
409 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
411 usess
= session
->ust_session
;
412 ksess
= session
->kernel_session
;
414 /* Clean kernel session teardown */
415 kernel_destroy_session(ksess
);
417 /* UST session teardown */
419 /* Close any relayd session */
420 consumer_output_send_destroy_relayd(usess
->consumer
);
422 /* Destroy every UST application related to this session. */
423 ret
= ust_app_destroy_trace_all(usess
);
425 ERR("Error in ust_app_destroy_trace_all");
428 /* Clean up the rest. */
429 trace_ust_destroy_session(usess
);
433 * Must notify the kernel thread here to update it's poll set in order to
434 * remove the channel(s)' fd just destroyed.
436 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
438 PERROR("write kernel poll pipe");
441 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
442 pthread_mutex_destroy(&session
->lock
);
444 consumer_output_put(session
->consumer
);
445 snapshot_destroy(&session
->snapshot
);
447 if (session
->published
) {
448 ASSERT_LOCKED(ltt_session_list
.lock
);
449 del_session_list(session
);
450 del_session_ht(session
);
451 pthread_cond_broadcast(<t_session_list
.removal_cond
);
457 * Acquire a reference to a session.
458 * This function may fail (return false); its return value must be checked.
460 bool session_get(struct ltt_session
*session
)
462 return urcu_ref_get_unless_zero(&session
->ref
);
466 * Release a reference to a session.
468 void session_put(struct ltt_session
*session
)
471 * The session list lock must be held as any session_put()
472 * may cause the removal of the session from the session_list.
474 ASSERT_LOCKED(ltt_session_list
.lock
);
475 assert(session
->ref
.refcount
);
476 urcu_ref_put(&session
->ref
, session_release
);
482 * This method does not immediately release/free the session as other
483 * components may still hold a reference to the session. However,
484 * the session should no longer be presented to the user.
486 * Releases the session list's reference to the session
487 * and marks it as destroyed. Iterations on the session list should be
488 * mindful of the "destroyed" flag.
490 void session_destroy(struct ltt_session
*session
)
492 assert(!session
->destroyed
);
493 session
->destroyed
= true;
494 session_put(session
);
498 * Return a ltt_session structure ptr that matches name. If no session found,
499 * NULL is returned. This must be called with the session list lock held using
500 * session_lock_list and session_unlock_list.
501 * A reference to the session is implicitly acquired by this function.
503 struct ltt_session
*session_find_by_name(const char *name
)
505 struct ltt_session
*iter
;
508 ASSERT_LOCKED(ltt_session_list
.lock
);
510 DBG2("Trying to find session by name %s", name
);
512 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
513 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
521 return session_get(iter
) ? iter
: NULL
;
525 * Return an ltt_session that matches the id. If no session is found,
526 * NULL is returned. This must be called with rcu_read_lock and
527 * session list lock held (to guarantee the lifetime of the session).
529 struct ltt_session
*session_find_by_id(uint64_t id
)
531 struct lttng_ht_node_u64
*node
;
532 struct lttng_ht_iter iter
;
533 struct ltt_session
*ls
;
535 ASSERT_LOCKED(ltt_session_list
.lock
);
537 if (!ltt_sessions_ht_by_id
) {
541 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
542 node
= lttng_ht_iter_get_node_u64(&iter
);
546 ls
= caa_container_of(node
, struct ltt_session
, node
);
548 DBG3("Session %" PRIu64
" found by id.", id
);
549 return session_get(ls
) ? ls
: NULL
;
552 DBG3("Session %" PRIu64
" NOT found by id", id
);
557 * Create a brand new session and add it to the session list.
559 int session_create(char *name
, uid_t uid
, gid_t gid
)
562 struct ltt_session
*new_session
;
564 /* Allocate session data structure */
565 new_session
= zmalloc(sizeof(struct ltt_session
));
566 if (new_session
== NULL
) {
568 ret
= LTTNG_ERR_FATAL
;
572 urcu_ref_init(&new_session
->ref
);
574 /* Define session name */
576 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
577 ret
= LTTNG_ERR_FATAL
;
581 ERR("No session name given");
582 ret
= LTTNG_ERR_FATAL
;
586 ret
= validate_name(name
);
588 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
592 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
594 if (errno
== ENAMETOOLONG
) {
595 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
597 ret
= LTTNG_ERR_FATAL
;
602 /* Init kernel session */
603 new_session
->kernel_session
= NULL
;
604 new_session
->ust_session
= NULL
;
607 pthread_mutex_init(&new_session
->lock
, NULL
);
609 new_session
->uid
= uid
;
610 new_session
->gid
= gid
;
612 ret
= snapshot_init(&new_session
->snapshot
);
614 ret
= LTTNG_ERR_NOMEM
;
618 new_session
->rotation_pending_local
= false;
619 new_session
->rotation_pending_relay
= false;
620 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
622 new_session
->rotation_pending_check_timer_enabled
= false;
623 new_session
->rotation_schedule_timer_enabled
= false;
625 /* Add new session to the session list */
627 new_session
->id
= add_session_list(new_session
);
629 * Add the new session to the ltt_sessions_ht_by_id.
630 * No ownership is taken by the hash table; it is merely
631 * a wrapper around the session list used for faster access
634 add_session_ht(new_session
);
635 new_session
->published
= true;
636 session_unlock_list();
639 * Consumer is let to NULL since the create_session_uri command will set it
640 * up and, if valid, assign it to the session.
642 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
643 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
650 session_put(new_session
);
651 session_unlock_list();
658 * Check if the UID or GID match the session. Root user has access to all
661 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
665 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
673 * Set a session's rotation state and reset all associated state.
675 * This function resets the rotation state (check timers, pending
676 * flags, etc.) and sets the result of the last rotation. The result
677 * can be queries by a liblttng-ctl client.
679 * Be careful of the result passed to this function. For instance,
680 * on failure to launch a rotation, a client will expect the rotation
681 * state to be set to "NO_ROTATION". If an error occured while the
682 * rotation was "ONGOING", result should be set to "ERROR", which will
683 * allow a client to report it.
685 * Must be called with the session and session_list locks held.
687 int session_reset_rotation_state(struct ltt_session
*session
,
688 enum lttng_rotation_state result
)
692 ASSERT_LOCKED(ltt_session_list
.lock
);
693 ASSERT_LOCKED(session
->lock
);
695 session
->rotation_pending_local
= false;
696 session
->rotation_pending_relay
= false;
697 session
->rotated_after_last_stop
= false;
698 session
->rotation_state
= result
;
699 if (session
->rotation_pending_check_timer_enabled
) {
700 ret
= timer_session_rotation_pending_check_stop(session
);