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>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <lttng/location-internal.h>
32 #include "lttng-sessiond.h"
37 #include "trace-ust.h"
43 * No ltt_session.lock is taken here because those data structure are widely
44 * spread across the lttng-tools code base so before caling functions below
45 * that can read/write a session, the caller MUST acquire the session lock
46 * using session_lock() and session_unlock().
50 * Init tracing session list.
52 * Please see session.h for more explanation and correct usage of the list.
54 static struct ltt_session_list ltt_session_list
= {
55 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
56 .lock
= PTHREAD_MUTEX_INITIALIZER
,
60 /* These characters are forbidden in a session name. Used by validate_name. */
61 static const char *forbidden_name_chars
= "/";
63 /* Global hash table to keep the sessions, indexed by id. */
64 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
67 * Validate the session name for forbidden characters.
69 * Return 0 on success else -1 meaning a forbidden char. has been found.
71 static int validate_name(const char *name
)
78 tmp_name
= strdup(name
);
85 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
87 DBG("Session name %s contains a forbidden character", name
);
88 /* Forbidden character has been found. */
100 * Add a ltt_session structure to the global list.
102 * The caller MUST acquire the session list lock before.
103 * Returns the unique identifier for the session.
105 static uint64_t add_session_list(struct ltt_session
*ls
)
109 cds_list_add(&ls
->list
, <t_session_list
.head
);
110 return ltt_session_list
.next_uuid
++;
114 * Delete a ltt_session structure to the global list.
116 * The caller MUST acquire the session list lock before.
118 static void del_session_list(struct ltt_session
*ls
)
122 cds_list_del(&ls
->list
);
126 * Return a pointer to the session list.
128 struct ltt_session_list
*session_get_list(void)
130 return <t_session_list
;
134 * Acquire session list lock
136 void session_lock_list(void)
138 pthread_mutex_lock(<t_session_list
.lock
);
142 * Try to acquire session list lock
144 int session_trylock_list(void)
146 return pthread_mutex_trylock(<t_session_list
.lock
);
150 * Release session list lock
152 void session_unlock_list(void)
154 pthread_mutex_unlock(<t_session_list
.lock
);
158 * Get the session's consumer destination type.
160 * The caller must hold the session lock.
162 enum consumer_dst_type
session_get_consumer_destination_type(
163 const struct ltt_session
*session
)
166 * The output information is duplicated in both of those session types.
167 * Hence, it doesn't matter from which it is retrieved. However, it is
168 * possible for only one of them to be set.
170 return session
->kernel_session
?
171 session
->kernel_session
->consumer
->type
:
172 session
->ust_session
->consumer
->type
;
176 * Get the session's consumer network hostname.
177 * The caller must ensure that the destination is of type "net".
179 * The caller must hold the session lock.
181 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
183 const char *hostname
= NULL
;
184 const struct consumer_output
*output
;
186 output
= session
->kernel_session
?
187 session
->kernel_session
->consumer
:
188 session
->ust_session
->consumer
;
191 * hostname is assumed to be the same for both control and data
194 switch (output
->dst
.net
.control
.dtype
) {
196 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
199 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
208 * Get the session's consumer network control and data ports.
209 * The caller must ensure that the destination is of type "net".
211 * The caller must hold the session lock.
213 void session_get_net_consumer_ports(const struct ltt_session
*session
,
214 uint16_t *control_port
, uint16_t *data_port
)
216 const struct consumer_output
*output
;
218 output
= session
->kernel_session
?
219 session
->kernel_session
->consumer
:
220 session
->ust_session
->consumer
;
221 *control_port
= output
->dst
.net
.control
.port
;
222 *data_port
= output
->dst
.net
.data
.port
;
226 * Get the location of the latest trace archive produced by a rotation.
228 * The caller must hold the session lock.
230 struct lttng_trace_archive_location
*session_get_trace_archive_location(
231 struct ltt_session
*session
)
233 struct lttng_trace_archive_location
*location
= NULL
;
235 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
239 switch (session_get_consumer_destination_type(session
)) {
240 case CONSUMER_DST_LOCAL
:
241 location
= lttng_trace_archive_location_local_create(
242 session
->rotation_chunk
.current_rotate_path
);
244 case CONSUMER_DST_NET
:
246 const char *hostname
;
247 uint16_t control_port
, data_port
;
249 hostname
= session_get_net_consumer_hostname(session
);
250 session_get_net_consumer_ports(session
,
253 location
= lttng_trace_archive_location_relay_create(
255 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
256 control_port
, data_port
,
257 session
->rotation_chunk
.current_rotate_path
);
268 * Allocate the ltt_sessions_ht_by_id HT.
270 * The session list lock must be held.
272 int ltt_sessions_ht_alloc(void)
276 DBG("Allocating ltt_sessions_ht_by_id");
277 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
278 if (!ltt_sessions_ht_by_id
) {
280 ERR("Failed to allocate ltt_sessions_ht_by_id");
288 * Destroy the ltt_sessions_ht_by_id HT.
290 * The session list lock must be held.
292 static void ltt_sessions_ht_destroy(void)
294 if (!ltt_sessions_ht_by_id
) {
297 ht_cleanup_push(ltt_sessions_ht_by_id
);
298 ltt_sessions_ht_by_id
= NULL
;
302 * Add a ltt_session to the ltt_sessions_ht_by_id.
303 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
304 * The session list lock must be held.
306 static void add_session_ht(struct ltt_session
*ls
)
312 if (!ltt_sessions_ht_by_id
) {
313 ret
= ltt_sessions_ht_alloc();
315 ERR("Error allocating the sessions HT");
319 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
320 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
327 * Test if ltt_sessions_ht_by_id is empty.
328 * Return 1 if empty, 0 if not empty.
329 * The session list lock must be held.
331 static int ltt_sessions_ht_empty(void)
335 if (!ltt_sessions_ht_by_id
) {
340 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
346 * Remove a ltt_session from the ltt_sessions_ht_by_id.
347 * If empty, the ltt_sessions_ht_by_id HT is freed.
348 * The session list lock must be held.
350 static void del_session_ht(struct ltt_session
*ls
)
352 struct lttng_ht_iter iter
;
356 assert(ltt_sessions_ht_by_id
);
358 iter
.iter
.node
= &ls
->node
.node
;
359 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
362 if (ltt_sessions_ht_empty()) {
363 DBG("Empty ltt_sessions_ht_by_id, destroying it");
364 ltt_sessions_ht_destroy();
369 * Acquire session lock
371 void session_lock(struct ltt_session
*session
)
375 pthread_mutex_lock(&session
->lock
);
379 * Release session lock
381 void session_unlock(struct ltt_session
*session
)
385 pthread_mutex_unlock(&session
->lock
);
389 void session_release(struct urcu_ref
*ref
)
392 struct ltt_ust_session
*usess
;
393 struct ltt_kernel_session
*ksess
;
394 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
396 usess
= session
->ust_session
;
397 ksess
= session
->kernel_session
;
399 /* Clean kernel session teardown */
400 kernel_destroy_session(ksess
);
402 /* UST session teardown */
404 /* Close any relayd session */
405 consumer_output_send_destroy_relayd(usess
->consumer
);
407 /* Destroy every UST application related to this session. */
408 ret
= ust_app_destroy_trace_all(usess
);
410 ERR("Error in ust_app_destroy_trace_all");
413 /* Clean up the rest. */
414 trace_ust_destroy_session(usess
);
418 * Must notify the kernel thread here to update it's poll set in order to
419 * remove the channel(s)' fd just destroyed.
421 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
423 PERROR("write kernel poll pipe");
426 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
427 pthread_mutex_destroy(&session
->lock
);
429 consumer_output_put(session
->consumer
);
430 snapshot_destroy(&session
->snapshot
);
431 del_session_list(session
);
432 del_session_ht(session
);
437 * Acquire a reference to a session.
438 * This function may fail (return false); its return value must be checked.
440 bool session_get(struct ltt_session
*session
)
442 return urcu_ref_get_unless_zero(&session
->ref
);
446 * Release a reference to a session.
448 void session_put(struct ltt_session
*session
)
451 * The session list lock must be held as any session_put()
452 * may cause the removal of the session from the session_list.
454 ASSERT_LOCKED(ltt_session_list
.lock
);
455 assert(session
->ref
.refcount
);
456 urcu_ref_put(&session
->ref
, session_release
);
462 * This method does not immediately release/free the session as other
463 * components may still hold a reference to the session. However,
464 * the session should no longer be presented to the user.
466 * Releases the session list's reference to the session
467 * and marks it as destroyed. Iterations on the session list should be
468 * mindful of the "destroyed" flag.
470 void session_destroy(struct ltt_session
*session
)
472 assert(!session
->destroyed
);
473 session
->destroyed
= true;
474 session_put(session
);
478 * Return a ltt_session structure ptr that matches name. If no session found,
479 * NULL is returned. This must be called with the session list lock held using
480 * session_lock_list and session_unlock_list.
481 * A reference to the session is implicitly acquired by this function.
483 struct ltt_session
*session_find_by_name(const char *name
)
485 struct ltt_session
*iter
;
488 ASSERT_LOCKED(ltt_session_list
.lock
);
490 DBG2("Trying to find session by name %s", name
);
492 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
493 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
501 return session_get(iter
) ? iter
: NULL
;
505 * Return an ltt_session that matches the id. If no session is found,
506 * NULL is returned. This must be called with rcu_read_lock and
507 * session list lock held (to guarantee the lifetime of the session).
509 struct ltt_session
*session_find_by_id(uint64_t id
)
511 struct lttng_ht_node_u64
*node
;
512 struct lttng_ht_iter iter
;
513 struct ltt_session
*ls
;
515 ASSERT_LOCKED(ltt_session_list
.lock
);
517 if (!ltt_sessions_ht_by_id
) {
521 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
522 node
= lttng_ht_iter_get_node_u64(&iter
);
526 ls
= caa_container_of(node
, struct ltt_session
, node
);
528 DBG3("Session %" PRIu64
" found by id.", id
);
529 return session_get(ls
) ? ls
: NULL
;
532 DBG3("Session %" PRIu64
" NOT found by id", id
);
537 * Create a brand new session and add it to the session list.
539 int session_create(char *name
, uid_t uid
, gid_t gid
)
542 struct ltt_session
*new_session
;
544 /* Allocate session data structure */
545 new_session
= zmalloc(sizeof(struct ltt_session
));
546 if (new_session
== NULL
) {
548 ret
= LTTNG_ERR_FATAL
;
552 urcu_ref_init(&new_session
->ref
);
554 /* Define session name */
556 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
557 ret
= LTTNG_ERR_FATAL
;
561 ERR("No session name given");
562 ret
= LTTNG_ERR_FATAL
;
566 ret
= validate_name(name
);
568 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
572 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
574 if (errno
== ENAMETOOLONG
) {
575 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
577 ret
= LTTNG_ERR_FATAL
;
582 /* Init kernel session */
583 new_session
->kernel_session
= NULL
;
584 new_session
->ust_session
= NULL
;
587 pthread_mutex_init(&new_session
->lock
, NULL
);
589 new_session
->uid
= uid
;
590 new_session
->gid
= gid
;
592 ret
= snapshot_init(&new_session
->snapshot
);
594 ret
= LTTNG_ERR_NOMEM
;
598 new_session
->rotation_pending_local
= false;
599 new_session
->rotation_pending_relay
= false;
600 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
602 new_session
->rotation_pending_check_timer_enabled
= false;
603 new_session
->rotation_schedule_timer_enabled
= false;
605 /* Add new session to the session list */
607 new_session
->id
= add_session_list(new_session
);
609 * Add the new session to the ltt_sessions_ht_by_id.
610 * No ownership is taken by the hash table; it is merely
611 * a wrapper around the session list used for faster access
614 add_session_ht(new_session
);
615 session_unlock_list();
618 * Consumer is let to NULL since the create_session_uri command will set it
619 * up and, if valid, assign it to the session.
621 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
622 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
635 * Check if the UID or GID match the session. Root user has access to all
638 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
642 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
650 * Set a session's rotation state and reset all associated state.
652 * This function resets the rotation state (check timers, pending
653 * flags, etc.) and sets the result of the last rotation. The result
654 * can be queries by a liblttng-ctl client.
656 * Be careful of the result passed to this function. For instance,
657 * on failure to launch a rotation, a client will expect the rotation
658 * state to be set to "NO_ROTATION". If an error occured while the
659 * rotation was "ONGOING", result should be set to "ERROR", which will
660 * allow a client to report it.
662 * Must be called with the session and session_list locks held.
664 int session_reset_rotation_state(struct ltt_session
*session
,
665 enum lttng_rotation_state result
)
669 ASSERT_LOCKED(ltt_session_list
.lock
);
670 ASSERT_LOCKED(session
->lock
);
672 session
->rotation_pending_local
= false;
673 session
->rotation_pending_relay
= false;
674 session
->rotated_after_last_stop
= false;
675 session
->rotation_state
= result
;
676 if (session
->rotation_pending_check_timer_enabled
) {
677 ret
= timer_session_rotation_pending_check_stop(session
);