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>
38 * No ltt_session.lock is taken here because those data structure are widely
39 * spread across the lttng-tools code base so before caling functions below
40 * that can read/write a session, the caller MUST acquire the session lock
41 * using session_lock() and session_unlock().
45 * Init tracing session list.
47 * Please see session.h for more explanation and correct usage of the list.
49 static struct ltt_session_list ltt_session_list
= {
50 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
51 .lock
= PTHREAD_MUTEX_INITIALIZER
,
55 /* These characters are forbidden in a session name. Used by validate_name. */
56 static const char *forbidden_name_chars
= "/";
58 /* Global hash table to keep the sessions, indexed by id. */
59 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
62 * Validate the session name for forbidden characters.
64 * Return 0 on success else -1 meaning a forbidden char. has been found.
66 static int validate_name(const char *name
)
73 tmp_name
= strdup(name
);
80 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
82 DBG("Session name %s contains a forbidden character", name
);
83 /* Forbidden character has been found. */
95 * Add a ltt_session structure to the global list.
97 * The caller MUST acquire the session list lock before.
98 * Returns the unique identifier for the session.
100 static uint64_t add_session_list(struct ltt_session
*ls
)
104 cds_list_add(&ls
->list
, <t_session_list
.head
);
105 return ltt_session_list
.next_uuid
++;
109 * Delete a ltt_session structure to the global list.
111 * The caller MUST acquire the session list lock before.
113 static void del_session_list(struct ltt_session
*ls
)
117 cds_list_del(&ls
->list
);
121 * Return a pointer to the session list.
123 struct ltt_session_list
*session_get_list(void)
125 return <t_session_list
;
129 * Acquire session list lock
131 void session_lock_list(void)
133 pthread_mutex_lock(<t_session_list
.lock
);
137 * Release session list lock
139 void session_unlock_list(void)
141 pthread_mutex_unlock(<t_session_list
.lock
);
145 * Allocate the ltt_sessions_ht_by_id HT.
147 int ltt_sessions_ht_alloc(void)
151 DBG("Allocating ltt_sessions_ht_by_id");
152 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
153 if (!ltt_sessions_ht_by_id
) {
155 ERR("Failed to allocate ltt_sessions_ht_by_id");
163 * Destroy the ltt_sessions_ht_by_id HT.
165 void ltt_sessions_ht_destroy(void)
167 if (!ltt_sessions_ht_by_id
) {
170 ht_cleanup_push(ltt_sessions_ht_by_id
);
171 ltt_sessions_ht_by_id
= NULL
;
175 * Add a ltt_session to the ltt_sessions_ht_by_id.
176 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
177 * The session list lock must be held.
179 static void add_session_ht(struct ltt_session
*ls
)
185 if (!ltt_sessions_ht_by_id
) {
186 ret
= ltt_sessions_ht_alloc();
188 ERR("Error allocating the sessions HT");
192 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
193 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
200 * Test if ltt_sessions_ht_by_id is empty.
201 * Return 1 if empty, 0 if not empty.
202 * The session list lock must be held.
204 static int ltt_sessions_ht_empty()
208 if (!ltt_sessions_ht_by_id
) {
213 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
219 * Remove a ltt_session from the ltt_sessions_ht_by_id.
220 * If empty, the ltt_sessions_ht_by_id HT is freed.
221 * The session list lock must be held.
223 static void del_session_ht(struct ltt_session
*ls
)
225 struct lttng_ht_iter iter
;
229 assert(ltt_sessions_ht_by_id
);
231 iter
.iter
.node
= &ls
->node
.node
;
232 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
235 if (ltt_sessions_ht_empty()) {
236 DBG("Empty ltt_sessions_ht_by_id, destroying it");
237 ltt_sessions_ht_destroy();
242 * Acquire session lock
244 void session_lock(struct ltt_session
*session
)
248 pthread_mutex_lock(&session
->lock
);
252 * Release session lock
254 void session_unlock(struct ltt_session
*session
)
258 pthread_mutex_unlock(&session
->lock
);
262 * Return a ltt_session structure ptr that matches name. If no session found,
263 * NULL is returned. This must be called with the session list lock held using
264 * session_lock_list and session_unlock_list.
266 struct ltt_session
*session_find_by_name(const char *name
)
268 struct ltt_session
*iter
;
272 DBG2("Trying to find session by name %s", name
);
274 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
275 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
287 * Return an ltt_session that matches the id. If no session is found,
288 * NULL is returned. This must be called with rcu_read_lock and
289 * session list lock held (to guarantee the lifetime of the session).
291 struct ltt_session
*session_find_by_id(uint64_t id
)
293 struct lttng_ht_node_u64
*node
;
294 struct lttng_ht_iter iter
;
295 struct ltt_session
*ls
;
297 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
298 node
= lttng_ht_iter_get_node_u64(&iter
);
302 ls
= caa_container_of(node
, struct ltt_session
, node
);
304 DBG3("Session %" PRIu64
" found by id.", id
);
308 DBG3("Session %" PRIu64
" NOT found by id", id
);
313 * Delete session from the session list and free the memory.
315 * Return -1 if no session is found. On success, return 1;
316 * Should *NOT* be called with RCU read-side lock held.
318 int session_destroy(struct ltt_session
*session
)
323 DBG("Destroying session %s", session
->name
);
324 del_session_list(session
);
325 pthread_mutex_destroy(&session
->lock
);
326 del_session_ht(session
);
328 consumer_output_put(session
->consumer
);
329 snapshot_destroy(&session
->snapshot
);
336 * Create a brand new session and add it to the session list.
338 int session_create(char *name
, uid_t uid
, gid_t gid
)
341 struct ltt_session
*new_session
;
343 /* Allocate session data structure */
344 new_session
= zmalloc(sizeof(struct ltt_session
));
345 if (new_session
== NULL
) {
347 ret
= LTTNG_ERR_FATAL
;
351 /* Define session name */
353 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
354 ret
= LTTNG_ERR_FATAL
;
358 ERR("No session name given");
359 ret
= LTTNG_ERR_FATAL
;
363 ret
= validate_name(name
);
365 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
369 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
371 if (errno
== ENAMETOOLONG
) {
372 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
374 ret
= LTTNG_ERR_FATAL
;
379 /* Init kernel session */
380 new_session
->kernel_session
= NULL
;
381 new_session
->ust_session
= NULL
;
384 pthread_mutex_init(&new_session
->lock
, NULL
);
386 new_session
->uid
= uid
;
387 new_session
->gid
= gid
;
389 ret
= snapshot_init(&new_session
->snapshot
);
391 ret
= LTTNG_ERR_NOMEM
;
395 /* Add new session to the session list */
397 new_session
->id
= add_session_list(new_session
);
399 * Add the new session to the ltt_sessions_ht_by_id.
400 * No ownership is taken by the hash table; it is merely
401 * a wrapper around the session list used for faster access
404 add_session_ht(new_session
);
405 session_unlock_list();
408 * Consumer is let to NULL since the create_session_uri command will set it
409 * up and, if valid, assign it to the session.
411 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
412 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
425 * Check if the UID or GID match the session. Root user has access to all
428 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
432 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {