Fix: mark ltt_sessions_ht_destroy as static
[lttng-tools.git] / src / bin / lttng-sessiond / session.c
CommitLineData
5b74c7b1
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
91d76f53 7 *
5b74c7b1
DG
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d14d33bf 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b74c7b1
DG
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
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.
5b74c7b1
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
6c9cc2ab 19#include <limits.h>
d022620a 20#include <inttypes.h>
5b74c7b1
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
a304c14c 24#include <sys/stat.h>
f6a9efaa 25#include <urcu.h>
3d071855
MD
26#include <dirent.h>
27#include <sys/types.h>
5b74c7b1 28
990570ed 29#include <common/common.h>
db758600 30#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 31
5b74c7b1 32#include "session.h"
23324029 33#include "utils.h"
5b74c7b1 34
8c0faa1d 35/*
b5541356 36 * NOTES:
8c0faa1d 37 *
b5541356
DG
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
54d01ffb 41 * using session_lock() and session_unlock().
8c0faa1d 42 */
8c0faa1d 43
5b74c7b1 44/*
b5541356 45 * Init tracing session list.
5b74c7b1 46 *
b5541356 47 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 48 */
b5541356
DG
49static struct ltt_session_list ltt_session_list = {
50 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
51 .lock = PTHREAD_MUTEX_INITIALIZER,
a24f7994 52 .next_uuid = 0,
b5541356 53};
5b74c7b1 54
1c1c3634
DG
55/* These characters are forbidden in a session name. Used by validate_name. */
56static const char *forbidden_name_chars = "/";
57
23324029
JD
58/* Global hash table to keep the sessions, indexed by id. */
59static struct lttng_ht *ltt_sessions_ht_by_id = NULL;
60
1c1c3634
DG
61/*
62 * Validate the session name for forbidden characters.
63 *
64 * Return 0 on success else -1 meaning a forbidden char. has been found.
65 */
66static int validate_name(const char *name)
67{
68 int ret;
69 char *tok, *tmp_name;
70
71 assert(name);
72
73 tmp_name = strdup(name);
74 if (!tmp_name) {
75 /* ENOMEM here. */
76 ret = -1;
77 goto error;
78 }
79
80 tok = strpbrk(tmp_name, forbidden_name_chars);
81 if (tok) {
82 DBG("Session name %s contains a forbidden character", name);
83 /* Forbidden character has been found. */
84 ret = -1;
85 goto error;
86 }
87 ret = 0;
88
89error:
90 free(tmp_name);
91 return ret;
92}
93
5b74c7b1 94/*
050349bb 95 * Add a ltt_session structure to the global list.
5b74c7b1 96 *
050349bb 97 * The caller MUST acquire the session list lock before.
44e96653 98 * Returns the unique identifier for the session.
5b74c7b1 99 */
d022620a 100static uint64_t add_session_list(struct ltt_session *ls)
5b74c7b1 101{
0525e9ae
DG
102 assert(ls);
103
5b74c7b1 104 cds_list_add(&ls->list, &ltt_session_list.head);
a24f7994 105 return ltt_session_list.next_uuid++;
5b74c7b1
DG
106}
107
108/*
050349bb 109 * Delete a ltt_session structure to the global list.
b5541356 110 *
050349bb 111 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
112 */
113static void del_session_list(struct ltt_session *ls)
114{
0525e9ae
DG
115 assert(ls);
116
5b74c7b1 117 cds_list_del(&ls->list);
5b74c7b1
DG
118}
119
b5541356 120/*
050349bb 121 * Return a pointer to the session list.
b5541356 122 */
54d01ffb 123struct ltt_session_list *session_get_list(void)
b5541356
DG
124{
125 return &ltt_session_list;
126}
127
128/*
6c9cc2ab 129 * Acquire session list lock
b5541356 130 */
54d01ffb 131void session_lock_list(void)
b5541356 132{
6c9cc2ab 133 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
134}
135
136/*
6c9cc2ab 137 * Release session list lock
b5541356 138 */
54d01ffb 139void session_unlock_list(void)
b5541356 140{
6c9cc2ab 141 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
142}
143
23324029
JD
144/*
145 * Allocate the ltt_sessions_ht_by_id HT.
9c6518bc
JG
146 *
147 * The session list lock must be held.
23324029
JD
148 */
149int ltt_sessions_ht_alloc(void)
150{
151 int ret = 0;
152
153 DBG("Allocating ltt_sessions_ht_by_id");
154 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
155 if (!ltt_sessions_ht_by_id) {
156 ret = -1;
157 ERR("Failed to allocate ltt_sessions_ht_by_id");
158 goto end;
159 }
160end:
161 return ret;
162}
163
164/*
165 * Destroy the ltt_sessions_ht_by_id HT.
9c6518bc
JG
166 *
167 * The session list lock must be held.
23324029 168 */
accdc9bf 169static void ltt_sessions_ht_destroy(void)
23324029
JD
170{
171 if (!ltt_sessions_ht_by_id) {
172 return;
173 }
174 ht_cleanup_push(ltt_sessions_ht_by_id);
175 ltt_sessions_ht_by_id = NULL;
176}
177
178/*
179 * Add a ltt_session to the ltt_sessions_ht_by_id.
180 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
181 * The session list lock must be held.
182 */
183static void add_session_ht(struct ltt_session *ls)
184{
185 int ret;
186
187 assert(ls);
188
189 if (!ltt_sessions_ht_by_id) {
190 ret = ltt_sessions_ht_alloc();
191 if (ret) {
192 ERR("Error allocating the sessions HT");
193 goto end;
194 }
195 }
196 lttng_ht_node_init_u64(&ls->node, ls->id);
197 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
198
199end:
200 return;
201}
202
203/*
204 * Test if ltt_sessions_ht_by_id is empty.
205 * Return 1 if empty, 0 if not empty.
206 * The session list lock must be held.
207 */
def88971 208static int ltt_sessions_ht_empty(void)
23324029
JD
209{
210 int ret;
211
212 if (!ltt_sessions_ht_by_id) {
213 ret = 1;
214 goto end;
215 }
216
217 ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
218end:
219 return ret;
220}
221
222/*
223 * Remove a ltt_session from the ltt_sessions_ht_by_id.
224 * If empty, the ltt_sessions_ht_by_id HT is freed.
225 * The session list lock must be held.
226 */
227static void del_session_ht(struct ltt_session *ls)
228{
229 struct lttng_ht_iter iter;
230 int ret;
231
232 assert(ls);
233 assert(ltt_sessions_ht_by_id);
234
235 iter.iter.node = &ls->node.node;
236 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
237 assert(!ret);
238
239 if (ltt_sessions_ht_empty()) {
240 DBG("Empty ltt_sessions_ht_by_id, destroying it");
241 ltt_sessions_ht_destroy();
242 }
243}
244
b5541356 245/*
6c9cc2ab 246 * Acquire session lock
b5541356 247 */
54d01ffb 248void session_lock(struct ltt_session *session)
b5541356 249{
0525e9ae
DG
250 assert(session);
251
6c9cc2ab
DG
252 pthread_mutex_lock(&session->lock);
253}
b5541356 254
6c9cc2ab
DG
255/*
256 * Release session lock
257 */
54d01ffb 258void session_unlock(struct ltt_session *session)
6c9cc2ab 259{
0525e9ae
DG
260 assert(session);
261
6c9cc2ab 262 pthread_mutex_unlock(&session->lock);
b5541356
DG
263}
264
5b74c7b1 265/*
74babd95 266 * Return a ltt_session structure ptr that matches name. If no session found,
23324029 267 * NULL is returned. This must be called with the session list lock held using
74babd95 268 * session_lock_list and session_unlock_list.
5b74c7b1 269 */
58a1a227 270struct ltt_session *session_find_by_name(const char *name)
5b74c7b1 271{
5b74c7b1
DG
272 struct ltt_session *iter;
273
0525e9ae
DG
274 assert(name);
275
5f822d0a
DG
276 DBG2("Trying to find session by name %s", name);
277
5b74c7b1 278 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 279 if (strncmp(iter->name, name, NAME_MAX) == 0) {
74babd95 280 goto found;
5b74c7b1
DG
281 }
282 }
283
74babd95 284 iter = NULL;
5b74c7b1 285
74babd95 286found:
5b74c7b1
DG
287 return iter;
288}
289
23324029
JD
290/*
291 * Return an ltt_session that matches the id. If no session is found,
292 * NULL is returned. This must be called with rcu_read_lock and
293 * session list lock held (to guarantee the lifetime of the session).
294 */
295struct ltt_session *session_find_by_id(uint64_t id)
296{
297 struct lttng_ht_node_u64 *node;
298 struct lttng_ht_iter iter;
299 struct ltt_session *ls;
300
301 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
302 node = lttng_ht_iter_get_node_u64(&iter);
303 if (node == NULL) {
304 goto error;
305 }
306 ls = caa_container_of(node, struct ltt_session, node);
307
308 DBG3("Session %" PRIu64 " found by id.", id);
309 return ls;
310
311error:
312 DBG3("Session %" PRIu64 " NOT found by id", id);
313 return NULL;
314}
315
5b74c7b1 316/*
050349bb 317 * Delete session from the session list and free the memory.
5b74c7b1 318 *
050349bb 319 * Return -1 if no session is found. On success, return 1;
36b588ed 320 * Should *NOT* be called with RCU read-side lock held.
5b74c7b1 321 */
271933a4 322int session_destroy(struct ltt_session *session)
5b74c7b1 323{
271933a4 324 /* Safety check */
0525e9ae 325 assert(session);
271933a4
DG
326
327 DBG("Destroying session %s", session->name);
328 del_session_list(session);
271933a4 329 pthread_mutex_destroy(&session->lock);
23324029 330 del_session_ht(session);
07424f16 331
6addfa37 332 consumer_output_put(session->consumer);
6dc3064a 333 snapshot_destroy(&session->snapshot);
271933a4 334 free(session);
5b74c7b1 335
f73fabfd 336 return LTTNG_OK;
5b74c7b1
DG
337}
338
339/*
050349bb 340 * Create a brand new session and add it to the session list.
5b74c7b1 341 */
dec56f6c 342int session_create(char *name, uid_t uid, gid_t gid)
5b74c7b1 343{
f3ed775e 344 int ret;
5b74c7b1 345 struct ltt_session *new_session;
e07ae692 346
5b74c7b1 347 /* Allocate session data structure */
ba7f0ae5 348 new_session = zmalloc(sizeof(struct ltt_session));
5b74c7b1 349 if (new_session == NULL) {
df0f840b 350 PERROR("zmalloc");
f73fabfd 351 ret = LTTNG_ERR_FATAL;
f3ed775e 352 goto error_malloc;
5b74c7b1
DG
353 }
354
f3ed775e 355 /* Define session name */
5b74c7b1 356 if (name != NULL) {
74babd95 357 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
f73fabfd 358 ret = LTTNG_ERR_FATAL;
f3ed775e 359 goto error_asprintf;
5b74c7b1
DG
360 }
361 } else {
f3ed775e 362 ERR("No session name given");
f73fabfd 363 ret = LTTNG_ERR_FATAL;
f3ed775e
DG
364 goto error;
365 }
366
1c1c3634
DG
367 ret = validate_name(name);
368 if (ret < 0) {
369 ret = LTTNG_ERR_SESSION_INVALID_CHAR;
370 goto error;
371 }
372
d3e2ba59 373 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
73184835
DG
374 if (ret < 0) {
375 if (errno == ENAMETOOLONG) {
376 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
377 } else {
378 ret = LTTNG_ERR_FATAL;
379 goto error;
380 }
d3e2ba59
JD
381 }
382
1d4b027a
DG
383 /* Init kernel session */
384 new_session->kernel_session = NULL;
f6a9efaa 385 new_session->ust_session = NULL;
1657e9bb 386
0177d773
DG
387 /* Init lock */
388 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 389
6df2e2c9
MD
390 new_session->uid = uid;
391 new_session->gid = gid;
392
6dc3064a
DG
393 ret = snapshot_init(&new_session->snapshot);
394 if (ret < 0) {
395 ret = LTTNG_ERR_NOMEM;
396 goto error;
397 }
398
b5541356 399 /* Add new session to the session list */
54d01ffb 400 session_lock_list();
a991f516 401 new_session->id = add_session_list(new_session);
23324029
JD
402 /*
403 * Add the new session to the ltt_sessions_ht_by_id.
404 * No ownership is taken by the hash table; it is merely
405 * a wrapper around the session list used for faster access
406 * by session id.
407 */
408 add_session_ht(new_session);
54d01ffb 409 session_unlock_list();
b5541356 410
a4b92340
DG
411 /*
412 * Consumer is let to NULL since the create_session_uri command will set it
413 * up and, if valid, assign it to the session.
414 */
d022620a
DG
415 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
416 name, new_session->id, new_session->uid, new_session->gid);
b082db07 417
f73fabfd 418 return LTTNG_OK;
5b74c7b1
DG
419
420error:
f3ed775e 421error_asprintf:
0e428499 422 free(new_session);
5b74c7b1 423
f3ed775e
DG
424error_malloc:
425 return ret;
5b74c7b1 426}
2f77fc4b
DG
427
428/*
429 * Check if the UID or GID match the session. Root user has access to all
430 * sessions.
431 */
432int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
433{
434 assert(session);
435
436 if (uid != session->uid && gid != session->gid && uid != 0) {
437 return 0;
438 } else {
439 return 1;
440 }
441}
This page took 0.065108 seconds and 4 git commands to generate.