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