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>
35 #include "trace-ust.h"
41 * No ltt_session.lock is taken here because those data structure are widely
42 * spread across the lttng-tools code base so before caling functions below
43 * that can read/write a session, the caller MUST acquire the session lock
44 * using session_lock() and session_unlock().
48 * Init tracing session list.
50 * Please see session.h for more explanation and correct usage of the list.
52 static struct ltt_session_list ltt_session_list
= {
53 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
54 .lock
= PTHREAD_MUTEX_INITIALIZER
,
58 /* These characters are forbidden in a session name. Used by validate_name. */
59 static const char *forbidden_name_chars
= "/";
61 /* Global hash table to keep the sessions, indexed by id. */
62 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
65 * Validate the session name for forbidden characters.
67 * Return 0 on success else -1 meaning a forbidden char. has been found.
69 static int validate_name(const char *name
)
76 tmp_name
= strdup(name
);
83 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
85 DBG("Session name %s contains a forbidden character", name
);
86 /* Forbidden character has been found. */
98 * Add a ltt_session structure to the global list.
100 * The caller MUST acquire the session list lock before.
101 * Returns the unique identifier for the session.
103 static uint64_t add_session_list(struct ltt_session
*ls
)
107 cds_list_add(&ls
->list
, <t_session_list
.head
);
108 return ltt_session_list
.next_uuid
++;
112 * Delete a ltt_session structure to the global list.
114 * The caller MUST acquire the session list lock before.
116 static void del_session_list(struct ltt_session
*ls
)
120 cds_list_del(&ls
->list
);
124 * Return a pointer to the session list.
126 struct ltt_session_list
*session_get_list(void)
128 return <t_session_list
;
132 * Acquire session list lock
134 void session_lock_list(void)
136 pthread_mutex_lock(<t_session_list
.lock
);
140 * Try to acquire session list lock
142 int session_trylock_list(void)
144 return pthread_mutex_trylock(<t_session_list
.lock
);
148 * Release session list lock
150 void session_unlock_list(void)
152 pthread_mutex_unlock(<t_session_list
.lock
);
156 * Get the session's consumer destination type.
158 * The caller must hold the session lock.
160 enum consumer_dst_type
session_get_consumer_destination_type(
161 const struct ltt_session
*session
)
164 * The output information is duplicated in both of those session types.
165 * Hence, it doesn't matter from which it is retrieved. However, it is
166 * possible for only one of them to be set.
168 return session
->kernel_session
?
169 session
->kernel_session
->consumer
->type
:
170 session
->ust_session
->consumer
->type
;
174 * Get the session's consumer network hostname.
175 * The caller must ensure that the destination is of type "net".
177 * The caller must hold the session lock.
179 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
181 const char *hostname
= NULL
;
182 const struct consumer_output
*output
;
184 output
= session
->kernel_session
?
185 session
->kernel_session
->consumer
:
186 session
->ust_session
->consumer
;
189 * hostname is assumed to be the same for both control and data
192 switch (output
->dst
.net
.control
.dtype
) {
194 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
197 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
206 * Get the session's consumer network control and data ports.
207 * The caller must ensure that the destination is of type "net".
209 * The caller must hold the session lock.
211 void session_get_net_consumer_ports(const struct ltt_session
*session
,
212 uint16_t *control_port
, uint16_t *data_port
)
214 const struct consumer_output
*output
;
216 output
= session
->kernel_session
?
217 session
->kernel_session
->consumer
:
218 session
->ust_session
->consumer
;
219 *control_port
= output
->dst
.net
.control
.port
;
220 *data_port
= output
->dst
.net
.data
.port
;
224 * Get the location of the latest trace archive produced by a rotation.
226 * The caller must hold the session lock.
228 struct lttng_trace_archive_location
*session_get_trace_archive_location(
229 struct ltt_session
*session
)
231 struct lttng_trace_archive_location
*location
= NULL
;
233 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
237 switch (session_get_consumer_destination_type(session
)) {
238 case CONSUMER_DST_LOCAL
:
239 location
= lttng_trace_archive_location_local_create(
240 session
->rotation_chunk
.current_rotate_path
);
242 case CONSUMER_DST_NET
:
244 const char *hostname
;
245 uint16_t control_port
, data_port
;
247 hostname
= session_get_net_consumer_hostname(session
);
248 session_get_net_consumer_ports(session
,
251 location
= lttng_trace_archive_location_relay_create(
253 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
254 control_port
, data_port
,
255 session
->rotation_chunk
.current_rotate_path
);
266 * Allocate the ltt_sessions_ht_by_id HT.
268 * The session list lock must be held.
270 int ltt_sessions_ht_alloc(void)
274 DBG("Allocating ltt_sessions_ht_by_id");
275 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
276 if (!ltt_sessions_ht_by_id
) {
278 ERR("Failed to allocate ltt_sessions_ht_by_id");
286 * Destroy the ltt_sessions_ht_by_id HT.
288 * The session list lock must be held.
290 static void ltt_sessions_ht_destroy(void)
292 if (!ltt_sessions_ht_by_id
) {
295 ht_cleanup_push(ltt_sessions_ht_by_id
);
296 ltt_sessions_ht_by_id
= NULL
;
300 * Add a ltt_session to the ltt_sessions_ht_by_id.
301 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
302 * The session list lock must be held.
304 static void add_session_ht(struct ltt_session
*ls
)
310 if (!ltt_sessions_ht_by_id
) {
311 ret
= ltt_sessions_ht_alloc();
313 ERR("Error allocating the sessions HT");
317 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
318 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
325 * Test if ltt_sessions_ht_by_id is empty.
326 * Return 1 if empty, 0 if not empty.
327 * The session list lock must be held.
329 static int ltt_sessions_ht_empty(void)
333 if (!ltt_sessions_ht_by_id
) {
338 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
344 * Remove a ltt_session from the ltt_sessions_ht_by_id.
345 * If empty, the ltt_sessions_ht_by_id HT is freed.
346 * The session list lock must be held.
348 static void del_session_ht(struct ltt_session
*ls
)
350 struct lttng_ht_iter iter
;
354 assert(ltt_sessions_ht_by_id
);
356 iter
.iter
.node
= &ls
->node
.node
;
357 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
360 if (ltt_sessions_ht_empty()) {
361 DBG("Empty ltt_sessions_ht_by_id, destroying it");
362 ltt_sessions_ht_destroy();
367 * Acquire session lock
369 void session_lock(struct ltt_session
*session
)
373 pthread_mutex_lock(&session
->lock
);
377 * Release session lock
379 void session_unlock(struct ltt_session
*session
)
383 pthread_mutex_unlock(&session
->lock
);
387 * Return a ltt_session structure ptr that matches name. If no session found,
388 * NULL is returned. This must be called with the session list lock held using
389 * session_lock_list and session_unlock_list.
391 struct ltt_session
*session_find_by_name(const char *name
)
393 struct ltt_session
*iter
;
397 DBG2("Trying to find session by name %s", name
);
399 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
400 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
412 * Return an ltt_session that matches the id. If no session is found,
413 * NULL is returned. This must be called with rcu_read_lock and
414 * session list lock held (to guarantee the lifetime of the session).
416 struct ltt_session
*session_find_by_id(uint64_t id
)
418 struct lttng_ht_node_u64
*node
;
419 struct lttng_ht_iter iter
;
420 struct ltt_session
*ls
;
422 if (!ltt_sessions_ht_by_id
) {
426 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
427 node
= lttng_ht_iter_get_node_u64(&iter
);
431 ls
= caa_container_of(node
, struct ltt_session
, node
);
433 DBG3("Session %" PRIu64
" found by id.", id
);
437 DBG3("Session %" PRIu64
" NOT found by id", id
);
442 * Delete session from the session list and free the memory.
444 * Return -1 if no session is found. On success, return 1;
445 * Should *NOT* be called with RCU read-side lock held.
447 int session_destroy(struct ltt_session
*session
)
452 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
453 del_session_list(session
);
454 pthread_mutex_destroy(&session
->lock
);
455 del_session_ht(session
);
457 consumer_output_put(session
->consumer
);
458 snapshot_destroy(&session
->snapshot
);
465 * Create a brand new session and add it to the session list.
467 int session_create(char *name
, uid_t uid
, gid_t gid
)
470 struct ltt_session
*new_session
;
472 /* Allocate session data structure */
473 new_session
= zmalloc(sizeof(struct ltt_session
));
474 if (new_session
== NULL
) {
476 ret
= LTTNG_ERR_FATAL
;
480 /* Define session name */
482 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
483 ret
= LTTNG_ERR_FATAL
;
487 ERR("No session name given");
488 ret
= LTTNG_ERR_FATAL
;
492 ret
= validate_name(name
);
494 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
498 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
500 if (errno
== ENAMETOOLONG
) {
501 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
503 ret
= LTTNG_ERR_FATAL
;
508 /* Init kernel session */
509 new_session
->kernel_session
= NULL
;
510 new_session
->ust_session
= NULL
;
513 pthread_mutex_init(&new_session
->lock
, NULL
);
515 new_session
->uid
= uid
;
516 new_session
->gid
= gid
;
518 ret
= snapshot_init(&new_session
->snapshot
);
520 ret
= LTTNG_ERR_NOMEM
;
524 new_session
->rotation_pending_local
= false;
525 new_session
->rotation_pending_relay
= false;
526 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
528 new_session
->rotation_pending_check_timer_enabled
= false;
529 new_session
->rotation_schedule_timer_enabled
= false;
531 /* Add new session to the session list */
533 new_session
->id
= add_session_list(new_session
);
535 * Add the new session to the ltt_sessions_ht_by_id.
536 * No ownership is taken by the hash table; it is merely
537 * a wrapper around the session list used for faster access
540 add_session_ht(new_session
);
541 session_unlock_list();
544 * Consumer is let to NULL since the create_session_uri command will set it
545 * up and, if valid, assign it to the session.
547 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
548 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
561 * Check if the UID or GID match the session. Root user has access to all
564 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
568 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {