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>
34 #include "trace-ust.h"
39 * No ltt_session.lock is taken here because those data structure are widely
40 * spread across the lttng-tools code base so before caling functions below
41 * that can read/write a session, the caller MUST acquire the session lock
42 * using session_lock() and session_unlock().
46 * Init tracing session list.
48 * Please see session.h for more explanation and correct usage of the list.
50 static struct ltt_session_list ltt_session_list
= {
51 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
52 .lock
= PTHREAD_MUTEX_INITIALIZER
,
56 /* These characters are forbidden in a session name. Used by validate_name. */
57 static const char *forbidden_name_chars
= "/";
59 /* Global hash table to keep the sessions, indexed by id. */
60 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
63 * Validate the session name for forbidden characters.
65 * Return 0 on success else -1 meaning a forbidden char. has been found.
67 static int validate_name(const char *name
)
74 tmp_name
= strdup(name
);
81 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
83 DBG("Session name %s contains a forbidden character", name
);
84 /* Forbidden character has been found. */
96 * Add a ltt_session structure to the global list.
98 * The caller MUST acquire the session list lock before.
99 * Returns the unique identifier for the session.
101 static uint64_t add_session_list(struct ltt_session
*ls
)
105 cds_list_add(&ls
->list
, <t_session_list
.head
);
106 return ltt_session_list
.next_uuid
++;
110 * Delete a ltt_session structure to the global list.
112 * The caller MUST acquire the session list lock before.
114 static void del_session_list(struct ltt_session
*ls
)
118 cds_list_del(&ls
->list
);
122 * Return a pointer to the session list.
124 struct ltt_session_list
*session_get_list(void)
126 return <t_session_list
;
130 * Acquire session list lock
132 void session_lock_list(void)
134 pthread_mutex_lock(<t_session_list
.lock
);
138 * Try to acquire session list lock
140 int session_trylock_list(void)
142 return pthread_mutex_trylock(<t_session_list
.lock
);
146 * Release session list lock
148 void session_unlock_list(void)
150 pthread_mutex_unlock(<t_session_list
.lock
);
154 * Get the session's consumer destination type.
156 * The caller must hold the session lock.
158 enum consumer_dst_type
session_get_consumer_destination_type(
159 const struct ltt_session
*session
)
162 * The output information is duplicated in both of those session types.
163 * Hence, it doesn't matter from which it is retrieved. However, it is
164 * possible for only one of them to be set.
166 return session
->kernel_session
?
167 session
->kernel_session
->consumer
->type
:
168 session
->ust_session
->consumer
->type
;
172 * Get the session's consumer network hostname.
173 * The caller must ensure that the destination is of type "net".
175 * The caller must hold the session lock.
177 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
179 const char *hostname
= NULL
;
180 const struct consumer_output
*output
;
182 output
= session
->kernel_session
?
183 session
->kernel_session
->consumer
:
184 session
->ust_session
->consumer
;
187 * hostname is assumed to be the same for both control and data
190 switch (output
->dst
.net
.control
.dtype
) {
192 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
195 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
204 * Get the session's consumer network control and data ports.
205 * The caller must ensure that the destination is of type "net".
207 * The caller must hold the session lock.
209 void session_get_net_consumer_ports(const struct ltt_session
*session
,
210 uint16_t *control_port
, uint16_t *data_port
)
212 const struct consumer_output
*output
;
214 output
= session
->kernel_session
?
215 session
->kernel_session
->consumer
:
216 session
->ust_session
->consumer
;
217 *control_port
= output
->dst
.net
.control
.port
;
218 *data_port
= output
->dst
.net
.data
.port
;
222 * Allocate the ltt_sessions_ht_by_id HT.
224 * The session list lock must be held.
226 int ltt_sessions_ht_alloc(void)
230 DBG("Allocating ltt_sessions_ht_by_id");
231 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
232 if (!ltt_sessions_ht_by_id
) {
234 ERR("Failed to allocate ltt_sessions_ht_by_id");
242 * Destroy the ltt_sessions_ht_by_id HT.
244 * The session list lock must be held.
246 static void ltt_sessions_ht_destroy(void)
248 if (!ltt_sessions_ht_by_id
) {
251 ht_cleanup_push(ltt_sessions_ht_by_id
);
252 ltt_sessions_ht_by_id
= NULL
;
256 * Add a ltt_session to the ltt_sessions_ht_by_id.
257 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
258 * The session list lock must be held.
260 static void add_session_ht(struct ltt_session
*ls
)
266 if (!ltt_sessions_ht_by_id
) {
267 ret
= ltt_sessions_ht_alloc();
269 ERR("Error allocating the sessions HT");
273 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
274 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
281 * Test if ltt_sessions_ht_by_id is empty.
282 * Return 1 if empty, 0 if not empty.
283 * The session list lock must be held.
285 static int ltt_sessions_ht_empty(void)
289 if (!ltt_sessions_ht_by_id
) {
294 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
300 * Remove a ltt_session from the ltt_sessions_ht_by_id.
301 * If empty, the ltt_sessions_ht_by_id HT is freed.
302 * The session list lock must be held.
304 static void del_session_ht(struct ltt_session
*ls
)
306 struct lttng_ht_iter iter
;
310 assert(ltt_sessions_ht_by_id
);
312 iter
.iter
.node
= &ls
->node
.node
;
313 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
316 if (ltt_sessions_ht_empty()) {
317 DBG("Empty ltt_sessions_ht_by_id, destroying it");
318 ltt_sessions_ht_destroy();
323 * Acquire session lock
325 void session_lock(struct ltt_session
*session
)
329 pthread_mutex_lock(&session
->lock
);
333 * Release session lock
335 void session_unlock(struct ltt_session
*session
)
339 pthread_mutex_unlock(&session
->lock
);
343 * Return a ltt_session structure ptr that matches name. If no session found,
344 * NULL is returned. This must be called with the session list lock held using
345 * session_lock_list and session_unlock_list.
347 struct ltt_session
*session_find_by_name(const char *name
)
349 struct ltt_session
*iter
;
353 DBG2("Trying to find session by name %s", name
);
355 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
356 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
368 * Return an ltt_session that matches the id. If no session is found,
369 * NULL is returned. This must be called with rcu_read_lock and
370 * session list lock held (to guarantee the lifetime of the session).
372 struct ltt_session
*session_find_by_id(uint64_t id
)
374 struct lttng_ht_node_u64
*node
;
375 struct lttng_ht_iter iter
;
376 struct ltt_session
*ls
;
378 if (!ltt_sessions_ht_by_id
) {
382 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
383 node
= lttng_ht_iter_get_node_u64(&iter
);
387 ls
= caa_container_of(node
, struct ltt_session
, node
);
389 DBG3("Session %" PRIu64
" found by id.", id
);
393 DBG3("Session %" PRIu64
" NOT found by id", id
);
398 * Delete session from the session list and free the memory.
400 * Return -1 if no session is found. On success, return 1;
401 * Should *NOT* be called with RCU read-side lock held.
403 int session_destroy(struct ltt_session
*session
)
408 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
409 del_session_list(session
);
410 pthread_mutex_destroy(&session
->lock
);
411 del_session_ht(session
);
413 consumer_output_put(session
->consumer
);
414 snapshot_destroy(&session
->snapshot
);
421 * Create a brand new session and add it to the session list.
423 int session_create(char *name
, uid_t uid
, gid_t gid
)
426 struct ltt_session
*new_session
;
428 /* Allocate session data structure */
429 new_session
= zmalloc(sizeof(struct ltt_session
));
430 if (new_session
== NULL
) {
432 ret
= LTTNG_ERR_FATAL
;
436 /* Define session name */
438 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
439 ret
= LTTNG_ERR_FATAL
;
443 ERR("No session name given");
444 ret
= LTTNG_ERR_FATAL
;
448 ret
= validate_name(name
);
450 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
454 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
456 if (errno
== ENAMETOOLONG
) {
457 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
459 ret
= LTTNG_ERR_FATAL
;
464 /* Init kernel session */
465 new_session
->kernel_session
= NULL
;
466 new_session
->ust_session
= NULL
;
469 pthread_mutex_init(&new_session
->lock
, NULL
);
471 new_session
->uid
= uid
;
472 new_session
->gid
= gid
;
474 ret
= snapshot_init(&new_session
->snapshot
);
476 ret
= LTTNG_ERR_NOMEM
;
480 new_session
->rotate_pending
= false;
481 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
482 new_session
->rotate_pending_relay
= false;
483 new_session
->rotate_relay_pending_timer_enabled
= false;
484 new_session
->rotate_timer
= false;
486 /* Add new session to the session list */
488 new_session
->id
= add_session_list(new_session
);
490 * Add the new session to the ltt_sessions_ht_by_id.
491 * No ownership is taken by the hash table; it is merely
492 * a wrapper around the session list used for faster access
495 add_session_ht(new_session
);
496 session_unlock_list();
499 * Consumer is let to NULL since the create_session_uri command will set it
500 * up and, if valid, assign it to the session.
502 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
503 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
516 * Check if the UID or GID match the session. Root user has access to all
519 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
523 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {