Fix: skip uid registry when metadata key is 0
[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>
5d65beab 31#include <lttng/location-internal.h>
1e307fab 32
5b74c7b1 33#include "session.h"
23324029 34#include "utils.h"
dd73d57b 35#include "trace-ust.h"
5b74c7b1 36
8c0faa1d 37/*
b5541356 38 * NOTES:
8c0faa1d 39 *
b5541356
DG
40 * No ltt_session.lock is taken here because those data structure are widely
41 * spread across the lttng-tools code base so before caling functions below
42 * that can read/write a session, the caller MUST acquire the session lock
54d01ffb 43 * using session_lock() and session_unlock().
8c0faa1d 44 */
8c0faa1d 45
5b74c7b1 46/*
b5541356 47 * Init tracing session list.
5b74c7b1 48 *
b5541356 49 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 50 */
b5541356
DG
51static struct ltt_session_list ltt_session_list = {
52 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
53 .lock = PTHREAD_MUTEX_INITIALIZER,
a24f7994 54 .next_uuid = 0,
b5541356 55};
5b74c7b1 56
1c1c3634
DG
57/* These characters are forbidden in a session name. Used by validate_name. */
58static const char *forbidden_name_chars = "/";
59
23324029
JD
60/* Global hash table to keep the sessions, indexed by id. */
61static struct lttng_ht *ltt_sessions_ht_by_id = NULL;
62
1c1c3634
DG
63/*
64 * Validate the session name for forbidden characters.
65 *
66 * Return 0 on success else -1 meaning a forbidden char. has been found.
67 */
68static int validate_name(const char *name)
69{
70 int ret;
71 char *tok, *tmp_name;
72
73 assert(name);
74
75 tmp_name = strdup(name);
76 if (!tmp_name) {
77 /* ENOMEM here. */
78 ret = -1;
79 goto error;
80 }
81
82 tok = strpbrk(tmp_name, forbidden_name_chars);
83 if (tok) {
84 DBG("Session name %s contains a forbidden character", name);
85 /* Forbidden character has been found. */
86 ret = -1;
87 goto error;
88 }
89 ret = 0;
90
91error:
92 free(tmp_name);
93 return ret;
94}
95
5b74c7b1 96/*
050349bb 97 * Add a ltt_session structure to the global list.
5b74c7b1 98 *
050349bb 99 * The caller MUST acquire the session list lock before.
44e96653 100 * Returns the unique identifier for the session.
5b74c7b1 101 */
d022620a 102static uint64_t add_session_list(struct ltt_session *ls)
5b74c7b1 103{
0525e9ae
DG
104 assert(ls);
105
5b74c7b1 106 cds_list_add(&ls->list, &ltt_session_list.head);
a24f7994 107 return ltt_session_list.next_uuid++;
5b74c7b1
DG
108}
109
110/*
050349bb 111 * Delete a ltt_session structure to the global list.
b5541356 112 *
050349bb 113 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
114 */
115static void del_session_list(struct ltt_session *ls)
116{
0525e9ae
DG
117 assert(ls);
118
5b74c7b1 119 cds_list_del(&ls->list);
5b74c7b1
DG
120}
121
b5541356 122/*
050349bb 123 * Return a pointer to the session list.
b5541356 124 */
54d01ffb 125struct ltt_session_list *session_get_list(void)
b5541356
DG
126{
127 return &ltt_session_list;
128}
129
130/*
6c9cc2ab 131 * Acquire session list lock
b5541356 132 */
54d01ffb 133void session_lock_list(void)
b5541356 134{
6c9cc2ab 135 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
136}
137
71e0a100
JG
138/*
139 * Try to acquire session list lock
140 */
141int session_trylock_list(void)
142{
143 return pthread_mutex_trylock(&ltt_session_list.lock);
144}
145
b5541356 146/*
6c9cc2ab 147 * Release session list lock
b5541356 148 */
54d01ffb 149void session_unlock_list(void)
b5541356 150{
6c9cc2ab 151 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
152}
153
dd73d57b
JG
154/*
155 * Get the session's consumer destination type.
156 *
157 * The caller must hold the session lock.
158 */
159enum consumer_dst_type session_get_consumer_destination_type(
160 const struct ltt_session *session)
161{
162 /*
163 * The output information is duplicated in both of those session types.
164 * Hence, it doesn't matter from which it is retrieved. However, it is
165 * possible for only one of them to be set.
166 */
167 return session->kernel_session ?
168 session->kernel_session->consumer->type :
169 session->ust_session->consumer->type;
170}
171
172/*
173 * Get the session's consumer network hostname.
174 * The caller must ensure that the destination is of type "net".
175 *
176 * The caller must hold the session lock.
177 */
178const char *session_get_net_consumer_hostname(const struct ltt_session *session)
179{
180 const char *hostname = NULL;
181 const struct consumer_output *output;
182
183 output = session->kernel_session ?
184 session->kernel_session->consumer :
185 session->ust_session->consumer;
186
187 /*
188 * hostname is assumed to be the same for both control and data
189 * connections.
190 */
191 switch (output->dst.net.control.dtype) {
192 case LTTNG_DST_IPV4:
193 hostname = output->dst.net.control.dst.ipv4;
194 break;
195 case LTTNG_DST_IPV6:
196 hostname = output->dst.net.control.dst.ipv6;
197 break;
198 default:
199 abort();
200 }
201 return hostname;
202}
203
204/*
205 * Get the session's consumer network control and data ports.
206 * The caller must ensure that the destination is of type "net".
207 *
208 * The caller must hold the session lock.
209 */
210void session_get_net_consumer_ports(const struct ltt_session *session,
211 uint16_t *control_port, uint16_t *data_port)
212{
213 const struct consumer_output *output;
214
215 output = session->kernel_session ?
216 session->kernel_session->consumer :
217 session->ust_session->consumer;
218 *control_port = output->dst.net.control.port;
219 *data_port = output->dst.net.data.port;
220}
221
5d65beab
JG
222/*
223 * Get the location of the latest trace archive produced by a rotation.
224 *
225 * The caller must hold the session lock.
226 */
227struct lttng_trace_archive_location *session_get_trace_archive_location(
228 struct ltt_session *session)
229{
230 struct lttng_trace_archive_location *location = NULL;
231
232 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
233 goto end;
234 }
235
236 switch (session_get_consumer_destination_type(session)) {
237 case CONSUMER_DST_LOCAL:
238 location = lttng_trace_archive_location_local_create(
239 session->rotation_chunk.current_rotate_path);
240 break;
241 case CONSUMER_DST_NET:
242 {
243 const char *hostname;
244 uint16_t control_port, data_port;
245
246 hostname = session_get_net_consumer_hostname(session);
247 session_get_net_consumer_ports(session,
248 &control_port,
249 &data_port);
250 location = lttng_trace_archive_location_relay_create(
251 hostname,
252 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
253 control_port, data_port,
254 session->rotation_chunk.current_rotate_path);
255 break;
256 }
257 default:
258 abort();
259 }
260end:
261 return location;
262}
263
23324029
JD
264/*
265 * Allocate the ltt_sessions_ht_by_id HT.
9c6518bc
JG
266 *
267 * The session list lock must be held.
23324029
JD
268 */
269int ltt_sessions_ht_alloc(void)
270{
271 int ret = 0;
272
273 DBG("Allocating ltt_sessions_ht_by_id");
274 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
275 if (!ltt_sessions_ht_by_id) {
276 ret = -1;
277 ERR("Failed to allocate ltt_sessions_ht_by_id");
278 goto end;
279 }
280end:
281 return ret;
282}
283
284/*
285 * Destroy the ltt_sessions_ht_by_id HT.
9c6518bc
JG
286 *
287 * The session list lock must be held.
23324029 288 */
accdc9bf 289static void ltt_sessions_ht_destroy(void)
23324029
JD
290{
291 if (!ltt_sessions_ht_by_id) {
292 return;
293 }
294 ht_cleanup_push(ltt_sessions_ht_by_id);
295 ltt_sessions_ht_by_id = NULL;
296}
297
298/*
299 * Add a ltt_session to the ltt_sessions_ht_by_id.
300 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
301 * The session list lock must be held.
302 */
303static void add_session_ht(struct ltt_session *ls)
304{
305 int ret;
306
307 assert(ls);
308
309 if (!ltt_sessions_ht_by_id) {
310 ret = ltt_sessions_ht_alloc();
311 if (ret) {
312 ERR("Error allocating the sessions HT");
313 goto end;
314 }
315 }
316 lttng_ht_node_init_u64(&ls->node, ls->id);
317 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
318
319end:
320 return;
321}
322
323/*
324 * Test if ltt_sessions_ht_by_id is empty.
325 * Return 1 if empty, 0 if not empty.
326 * The session list lock must be held.
327 */
def88971 328static int ltt_sessions_ht_empty(void)
23324029
JD
329{
330 int ret;
331
332 if (!ltt_sessions_ht_by_id) {
333 ret = 1;
334 goto end;
335 }
336
337 ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
338end:
339 return ret;
340}
341
342/*
343 * Remove a ltt_session from the ltt_sessions_ht_by_id.
344 * If empty, the ltt_sessions_ht_by_id HT is freed.
345 * The session list lock must be held.
346 */
347static void del_session_ht(struct ltt_session *ls)
348{
349 struct lttng_ht_iter iter;
350 int ret;
351
352 assert(ls);
353 assert(ltt_sessions_ht_by_id);
354
355 iter.iter.node = &ls->node.node;
356 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
357 assert(!ret);
358
359 if (ltt_sessions_ht_empty()) {
360 DBG("Empty ltt_sessions_ht_by_id, destroying it");
361 ltt_sessions_ht_destroy();
362 }
363}
364
b5541356 365/*
6c9cc2ab 366 * Acquire session lock
b5541356 367 */
54d01ffb 368void session_lock(struct ltt_session *session)
b5541356 369{
0525e9ae
DG
370 assert(session);
371
6c9cc2ab
DG
372 pthread_mutex_lock(&session->lock);
373}
b5541356 374
6c9cc2ab
DG
375/*
376 * Release session lock
377 */
54d01ffb 378void session_unlock(struct ltt_session *session)
6c9cc2ab 379{
0525e9ae
DG
380 assert(session);
381
6c9cc2ab 382 pthread_mutex_unlock(&session->lock);
b5541356
DG
383}
384
5b74c7b1 385/*
74babd95 386 * Return a ltt_session structure ptr that matches name. If no session found,
23324029 387 * NULL is returned. This must be called with the session list lock held using
74babd95 388 * session_lock_list and session_unlock_list.
5b74c7b1 389 */
58a1a227 390struct ltt_session *session_find_by_name(const char *name)
5b74c7b1 391{
5b74c7b1
DG
392 struct ltt_session *iter;
393
0525e9ae
DG
394 assert(name);
395
5f822d0a
DG
396 DBG2("Trying to find session by name %s", name);
397
5b74c7b1 398 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 399 if (strncmp(iter->name, name, NAME_MAX) == 0) {
74babd95 400 goto found;
5b74c7b1
DG
401 }
402 }
403
74babd95 404 iter = NULL;
5b74c7b1 405
74babd95 406found:
5b74c7b1
DG
407 return iter;
408}
409
23324029
JD
410/*
411 * Return an ltt_session that matches the id. If no session is found,
412 * NULL is returned. This must be called with rcu_read_lock and
413 * session list lock held (to guarantee the lifetime of the session).
414 */
415struct ltt_session *session_find_by_id(uint64_t id)
416{
417 struct lttng_ht_node_u64 *node;
418 struct lttng_ht_iter iter;
419 struct ltt_session *ls;
420
d68ec974
JG
421 if (!ltt_sessions_ht_by_id) {
422 goto end;
423 }
424
23324029
JD
425 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
426 node = lttng_ht_iter_get_node_u64(&iter);
427 if (node == NULL) {
d68ec974 428 goto end;
23324029
JD
429 }
430 ls = caa_container_of(node, struct ltt_session, node);
431
432 DBG3("Session %" PRIu64 " found by id.", id);
433 return ls;
434
d68ec974 435end:
23324029
JD
436 DBG3("Session %" PRIu64 " NOT found by id", id);
437 return NULL;
438}
439
5b74c7b1 440/*
050349bb 441 * Delete session from the session list and free the memory.
5b74c7b1 442 *
050349bb 443 * Return -1 if no session is found. On success, return 1;
36b588ed 444 * Should *NOT* be called with RCU read-side lock held.
5b74c7b1 445 */
271933a4 446int session_destroy(struct ltt_session *session)
5b74c7b1 447{
271933a4 448 /* Safety check */
0525e9ae 449 assert(session);
271933a4 450
5c408ad8 451 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
271933a4 452 del_session_list(session);
271933a4 453 pthread_mutex_destroy(&session->lock);
23324029 454 del_session_ht(session);
07424f16 455
6addfa37 456 consumer_output_put(session->consumer);
6dc3064a 457 snapshot_destroy(&session->snapshot);
271933a4 458 free(session);
5b74c7b1 459
f73fabfd 460 return LTTNG_OK;
5b74c7b1
DG
461}
462
463/*
050349bb 464 * Create a brand new session and add it to the session list.
5b74c7b1 465 */
dec56f6c 466int session_create(char *name, uid_t uid, gid_t gid)
5b74c7b1 467{
f3ed775e 468 int ret;
5b74c7b1 469 struct ltt_session *new_session;
e07ae692 470
5b74c7b1 471 /* Allocate session data structure */
ba7f0ae5 472 new_session = zmalloc(sizeof(struct ltt_session));
5b74c7b1 473 if (new_session == NULL) {
df0f840b 474 PERROR("zmalloc");
f73fabfd 475 ret = LTTNG_ERR_FATAL;
f3ed775e 476 goto error_malloc;
5b74c7b1
DG
477 }
478
f3ed775e 479 /* Define session name */
5b74c7b1 480 if (name != NULL) {
74babd95 481 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
f73fabfd 482 ret = LTTNG_ERR_FATAL;
f3ed775e 483 goto error_asprintf;
5b74c7b1
DG
484 }
485 } else {
f3ed775e 486 ERR("No session name given");
f73fabfd 487 ret = LTTNG_ERR_FATAL;
f3ed775e
DG
488 goto error;
489 }
490
1c1c3634
DG
491 ret = validate_name(name);
492 if (ret < 0) {
493 ret = LTTNG_ERR_SESSION_INVALID_CHAR;
494 goto error;
495 }
496
d3e2ba59 497 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
73184835
DG
498 if (ret < 0) {
499 if (errno == ENAMETOOLONG) {
500 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
501 } else {
502 ret = LTTNG_ERR_FATAL;
503 goto error;
504 }
d3e2ba59
JD
505 }
506
1d4b027a
DG
507 /* Init kernel session */
508 new_session->kernel_session = NULL;
f6a9efaa 509 new_session->ust_session = NULL;
1657e9bb 510
0177d773
DG
511 /* Init lock */
512 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 513
6df2e2c9
MD
514 new_session->uid = uid;
515 new_session->gid = gid;
516
6dc3064a
DG
517 ret = snapshot_init(&new_session->snapshot);
518 if (ret < 0) {
519 ret = LTTNG_ERR_NOMEM;
520 goto error;
521 }
522
5c408ad8 523 new_session->rotate_pending = false;
4f23c583 524 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
5c408ad8 525 new_session->rotate_pending_relay = false;
d88744a4 526 new_session->rotate_relay_pending_timer_enabled = false;
259c2674 527 new_session->rotate_timer = false;
5c408ad8 528
b5541356 529 /* Add new session to the session list */
54d01ffb 530 session_lock_list();
a991f516 531 new_session->id = add_session_list(new_session);
23324029
JD
532 /*
533 * Add the new session to the ltt_sessions_ht_by_id.
534 * No ownership is taken by the hash table; it is merely
535 * a wrapper around the session list used for faster access
536 * by session id.
537 */
538 add_session_ht(new_session);
54d01ffb 539 session_unlock_list();
b5541356 540
a4b92340
DG
541 /*
542 * Consumer is let to NULL since the create_session_uri command will set it
543 * up and, if valid, assign it to the session.
544 */
d022620a
DG
545 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
546 name, new_session->id, new_session->uid, new_session->gid);
b082db07 547
f73fabfd 548 return LTTNG_OK;
5b74c7b1
DG
549
550error:
f3ed775e 551error_asprintf:
0e428499 552 free(new_session);
5b74c7b1 553
f3ed775e
DG
554error_malloc:
555 return ret;
5b74c7b1 556}
2f77fc4b
DG
557
558/*
559 * Check if the UID or GID match the session. Root user has access to all
560 * sessions.
561 */
562int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
563{
564 assert(session);
565
566 if (uid != session->uid && gid != session->gid && uid != 0) {
567 return 0;
568 } else {
569 return 1;
570 }
571}
This page took 0.079605 seconds and 4 git commands to generate.