173afa8784f47b22e7e24fbc237dbbaae70809d2
[lttng-tools.git] / src / bin / lttng-sessiond / session.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #define _LGPL_SOURCE
19 #include <limits.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <urcu.h>
26 #include <dirent.h>
27 #include <sys/types.h>
28
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <lttng/location-internal.h>
32 #include "lttng-sessiond.h"
33 #include "kernel.h"
34
35 #include "session.h"
36 #include "utils.h"
37 #include "trace-ust.h"
38 #include "timer.h"
39
40 /*
41 * NOTES:
42 *
43 * No ltt_session.lock is taken here because those data structure are widely
44 * spread across the lttng-tools code base so before caling functions below
45 * that can read/write a session, the caller MUST acquire the session lock
46 * using session_lock() and session_unlock().
47 */
48
49 /*
50 * Init tracing session list.
51 *
52 * Please see session.h for more explanation and correct usage of the list.
53 */
54 static struct ltt_session_list ltt_session_list = {
55 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
56 .lock = PTHREAD_MUTEX_INITIALIZER,
57 .next_uuid = 0,
58 };
59
60 /* These characters are forbidden in a session name. Used by validate_name. */
61 static const char *forbidden_name_chars = "/";
62
63 /* Global hash table to keep the sessions, indexed by id. */
64 static struct lttng_ht *ltt_sessions_ht_by_id = NULL;
65
66 /*
67 * Validate the session name for forbidden characters.
68 *
69 * Return 0 on success else -1 meaning a forbidden char. has been found.
70 */
71 static int validate_name(const char *name)
72 {
73 int ret;
74 char *tok, *tmp_name;
75
76 assert(name);
77
78 tmp_name = strdup(name);
79 if (!tmp_name) {
80 /* ENOMEM here. */
81 ret = -1;
82 goto error;
83 }
84
85 tok = strpbrk(tmp_name, forbidden_name_chars);
86 if (tok) {
87 DBG("Session name %s contains a forbidden character", name);
88 /* Forbidden character has been found. */
89 ret = -1;
90 goto error;
91 }
92 ret = 0;
93
94 error:
95 free(tmp_name);
96 return ret;
97 }
98
99 /*
100 * Add a ltt_session structure to the global list.
101 *
102 * The caller MUST acquire the session list lock before.
103 * Returns the unique identifier for the session.
104 */
105 static uint64_t add_session_list(struct ltt_session *ls)
106 {
107 assert(ls);
108
109 cds_list_add(&ls->list, &ltt_session_list.head);
110 return ltt_session_list.next_uuid++;
111 }
112
113 /*
114 * Delete a ltt_session structure to the global list.
115 *
116 * The caller MUST acquire the session list lock before.
117 */
118 static void del_session_list(struct ltt_session *ls)
119 {
120 assert(ls);
121
122 cds_list_del(&ls->list);
123 }
124
125 /*
126 * Return a pointer to the session list.
127 */
128 struct ltt_session_list *session_get_list(void)
129 {
130 return &ltt_session_list;
131 }
132
133 /*
134 * Acquire session list lock
135 */
136 void session_lock_list(void)
137 {
138 pthread_mutex_lock(&ltt_session_list.lock);
139 }
140
141 /*
142 * Try to acquire session list lock
143 */
144 int session_trylock_list(void)
145 {
146 return pthread_mutex_trylock(&ltt_session_list.lock);
147 }
148
149 /*
150 * Release session list lock
151 */
152 void session_unlock_list(void)
153 {
154 pthread_mutex_unlock(&ltt_session_list.lock);
155 }
156
157 /*
158 * Get the session's consumer destination type.
159 *
160 * The caller must hold the session lock.
161 */
162 enum consumer_dst_type session_get_consumer_destination_type(
163 const struct ltt_session *session)
164 {
165 /*
166 * The output information is duplicated in both of those session types.
167 * Hence, it doesn't matter from which it is retrieved. However, it is
168 * possible for only one of them to be set.
169 */
170 return session->kernel_session ?
171 session->kernel_session->consumer->type :
172 session->ust_session->consumer->type;
173 }
174
175 /*
176 * Get the session's consumer network hostname.
177 * The caller must ensure that the destination is of type "net".
178 *
179 * The caller must hold the session lock.
180 */
181 const char *session_get_net_consumer_hostname(const struct ltt_session *session)
182 {
183 const char *hostname = NULL;
184 const struct consumer_output *output;
185
186 output = session->kernel_session ?
187 session->kernel_session->consumer :
188 session->ust_session->consumer;
189
190 /*
191 * hostname is assumed to be the same for both control and data
192 * connections.
193 */
194 switch (output->dst.net.control.dtype) {
195 case LTTNG_DST_IPV4:
196 hostname = output->dst.net.control.dst.ipv4;
197 break;
198 case LTTNG_DST_IPV6:
199 hostname = output->dst.net.control.dst.ipv6;
200 break;
201 default:
202 abort();
203 }
204 return hostname;
205 }
206
207 /*
208 * Get the session's consumer network control and data ports.
209 * The caller must ensure that the destination is of type "net".
210 *
211 * The caller must hold the session lock.
212 */
213 void session_get_net_consumer_ports(const struct ltt_session *session,
214 uint16_t *control_port, uint16_t *data_port)
215 {
216 const struct consumer_output *output;
217
218 output = session->kernel_session ?
219 session->kernel_session->consumer :
220 session->ust_session->consumer;
221 *control_port = output->dst.net.control.port;
222 *data_port = output->dst.net.data.port;
223 }
224
225 /*
226 * Get the location of the latest trace archive produced by a rotation.
227 *
228 * The caller must hold the session lock.
229 */
230 struct lttng_trace_archive_location *session_get_trace_archive_location(
231 struct ltt_session *session)
232 {
233 struct lttng_trace_archive_location *location = NULL;
234
235 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
236 goto end;
237 }
238
239 switch (session_get_consumer_destination_type(session)) {
240 case CONSUMER_DST_LOCAL:
241 location = lttng_trace_archive_location_local_create(
242 session->rotation_chunk.current_rotate_path);
243 break;
244 case CONSUMER_DST_NET:
245 {
246 const char *hostname;
247 uint16_t control_port, data_port;
248
249 hostname = session_get_net_consumer_hostname(session);
250 session_get_net_consumer_ports(session,
251 &control_port,
252 &data_port);
253 location = lttng_trace_archive_location_relay_create(
254 hostname,
255 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
256 control_port, data_port,
257 session->rotation_chunk.current_rotate_path);
258 break;
259 }
260 default:
261 abort();
262 }
263 end:
264 return location;
265 }
266
267 /*
268 * Allocate the ltt_sessions_ht_by_id HT.
269 *
270 * The session list lock must be held.
271 */
272 int ltt_sessions_ht_alloc(void)
273 {
274 int ret = 0;
275
276 DBG("Allocating ltt_sessions_ht_by_id");
277 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
278 if (!ltt_sessions_ht_by_id) {
279 ret = -1;
280 ERR("Failed to allocate ltt_sessions_ht_by_id");
281 goto end;
282 }
283 end:
284 return ret;
285 }
286
287 /*
288 * Destroy the ltt_sessions_ht_by_id HT.
289 *
290 * The session list lock must be held.
291 */
292 static void ltt_sessions_ht_destroy(void)
293 {
294 if (!ltt_sessions_ht_by_id) {
295 return;
296 }
297 ht_cleanup_push(ltt_sessions_ht_by_id);
298 ltt_sessions_ht_by_id = NULL;
299 }
300
301 /*
302 * Add a ltt_session to the ltt_sessions_ht_by_id.
303 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
304 * The session list lock must be held.
305 */
306 static void add_session_ht(struct ltt_session *ls)
307 {
308 int ret;
309
310 assert(ls);
311
312 if (!ltt_sessions_ht_by_id) {
313 ret = ltt_sessions_ht_alloc();
314 if (ret) {
315 ERR("Error allocating the sessions HT");
316 goto end;
317 }
318 }
319 lttng_ht_node_init_u64(&ls->node, ls->id);
320 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
321
322 end:
323 return;
324 }
325
326 /*
327 * Test if ltt_sessions_ht_by_id is empty.
328 * Return 1 if empty, 0 if not empty.
329 * The session list lock must be held.
330 */
331 static int ltt_sessions_ht_empty(void)
332 {
333 int ret;
334
335 if (!ltt_sessions_ht_by_id) {
336 ret = 1;
337 goto end;
338 }
339
340 ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
341 end:
342 return ret;
343 }
344
345 /*
346 * Remove a ltt_session from the ltt_sessions_ht_by_id.
347 * If empty, the ltt_sessions_ht_by_id HT is freed.
348 * The session list lock must be held.
349 */
350 static void del_session_ht(struct ltt_session *ls)
351 {
352 struct lttng_ht_iter iter;
353 int ret;
354
355 assert(ls);
356 assert(ltt_sessions_ht_by_id);
357
358 iter.iter.node = &ls->node.node;
359 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
360 assert(!ret);
361
362 if (ltt_sessions_ht_empty()) {
363 DBG("Empty ltt_sessions_ht_by_id, destroying it");
364 ltt_sessions_ht_destroy();
365 }
366 }
367
368 /*
369 * Acquire session lock
370 */
371 void session_lock(struct ltt_session *session)
372 {
373 assert(session);
374
375 pthread_mutex_lock(&session->lock);
376 }
377
378 /*
379 * Release session lock
380 */
381 void session_unlock(struct ltt_session *session)
382 {
383 assert(session);
384
385 pthread_mutex_unlock(&session->lock);
386 }
387
388 static
389 void session_release(struct urcu_ref *ref)
390 {
391 int ret;
392 struct ltt_ust_session *usess;
393 struct ltt_kernel_session *ksess;
394 struct ltt_session *session = container_of(ref, typeof(*session), ref);
395
396 usess = session->ust_session;
397 ksess = session->kernel_session;
398
399 /* Clean kernel session teardown */
400 kernel_destroy_session(ksess);
401
402 /* UST session teardown */
403 if (usess) {
404 /* Close any relayd session */
405 consumer_output_send_destroy_relayd(usess->consumer);
406
407 /* Destroy every UST application related to this session. */
408 ret = ust_app_destroy_trace_all(usess);
409 if (ret) {
410 ERR("Error in ust_app_destroy_trace_all");
411 }
412
413 /* Clean up the rest. */
414 trace_ust_destroy_session(usess);
415 }
416
417 /*
418 * Must notify the kernel thread here to update it's poll set in order to
419 * remove the channel(s)' fd just destroyed.
420 */
421 ret = notify_thread_pipe(kernel_poll_pipe[1]);
422 if (ret < 0) {
423 PERROR("write kernel poll pipe");
424 }
425
426 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
427 pthread_mutex_destroy(&session->lock);
428
429 consumer_output_put(session->consumer);
430 snapshot_destroy(&session->snapshot);
431 del_session_list(session);
432 del_session_ht(session);
433 free(session);
434 }
435
436 /*
437 * Acquire a reference to a session.
438 * This function may fail (return false); its return value must be checked.
439 */
440 bool session_get(struct ltt_session *session)
441 {
442 return urcu_ref_get_unless_zero(&session->ref);
443 }
444
445 /*
446 * Release a reference to a session.
447 */
448 void session_put(struct ltt_session *session)
449 {
450 /*
451 * The session list lock must be held as any session_put()
452 * may cause the removal of the session from the session_list.
453 */
454 ASSERT_LOCKED(ltt_session_list.lock);
455 assert(session->ref.refcount);
456 urcu_ref_put(&session->ref, session_release);
457 }
458
459 /*
460 * Destroy a session.
461 *
462 * This method does not immediately release/free the session as other
463 * components may still hold a reference to the session. However,
464 * the session should no longer be presented to the user.
465 *
466 * Releases the session list's reference to the session
467 * and marks it as destroyed. Iterations on the session list should be
468 * mindful of the "destroyed" flag.
469 */
470 void session_destroy(struct ltt_session *session)
471 {
472 assert(!session->destroyed);
473 session->destroyed = true;
474 session_put(session);
475 }
476
477 /*
478 * Return a ltt_session structure ptr that matches name. If no session found,
479 * NULL is returned. This must be called with the session list lock held using
480 * session_lock_list and session_unlock_list.
481 * A reference to the session is implicitly acquired by this function.
482 */
483 struct ltt_session *session_find_by_name(const char *name)
484 {
485 struct ltt_session *iter;
486
487 assert(name);
488 ASSERT_LOCKED(ltt_session_list.lock);
489
490 DBG2("Trying to find session by name %s", name);
491
492 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
493 if (!strncmp(iter->name, name, NAME_MAX) &&
494 !iter->destroyed) {
495 goto found;
496 }
497 }
498
499 return NULL;
500 found:
501 return session_get(iter) ? iter : NULL;
502 }
503
504 /*
505 * Return an ltt_session that matches the id. If no session is found,
506 * NULL is returned. This must be called with rcu_read_lock and
507 * session list lock held (to guarantee the lifetime of the session).
508 */
509 struct ltt_session *session_find_by_id(uint64_t id)
510 {
511 struct lttng_ht_node_u64 *node;
512 struct lttng_ht_iter iter;
513 struct ltt_session *ls;
514
515 ASSERT_LOCKED(ltt_session_list.lock);
516
517 if (!ltt_sessions_ht_by_id) {
518 goto end;
519 }
520
521 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
522 node = lttng_ht_iter_get_node_u64(&iter);
523 if (node == NULL) {
524 goto end;
525 }
526 ls = caa_container_of(node, struct ltt_session, node);
527
528 DBG3("Session %" PRIu64 " found by id.", id);
529 return session_get(ls) ? ls : NULL;
530
531 end:
532 DBG3("Session %" PRIu64 " NOT found by id", id);
533 return NULL;
534 }
535
536 /*
537 * Create a brand new session and add it to the session list.
538 */
539 int session_create(char *name, uid_t uid, gid_t gid)
540 {
541 int ret;
542 struct ltt_session *new_session;
543
544 /* Allocate session data structure */
545 new_session = zmalloc(sizeof(struct ltt_session));
546 if (new_session == NULL) {
547 PERROR("zmalloc");
548 ret = LTTNG_ERR_FATAL;
549 goto error_malloc;
550 }
551
552 urcu_ref_init(&new_session->ref);
553
554 /* Define session name */
555 if (name != NULL) {
556 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
557 ret = LTTNG_ERR_FATAL;
558 goto error_asprintf;
559 }
560 } else {
561 ERR("No session name given");
562 ret = LTTNG_ERR_FATAL;
563 goto error;
564 }
565
566 ret = validate_name(name);
567 if (ret < 0) {
568 ret = LTTNG_ERR_SESSION_INVALID_CHAR;
569 goto error;
570 }
571
572 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
573 if (ret < 0) {
574 if (errno == ENAMETOOLONG) {
575 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
576 } else {
577 ret = LTTNG_ERR_FATAL;
578 goto error;
579 }
580 }
581
582 /* Init kernel session */
583 new_session->kernel_session = NULL;
584 new_session->ust_session = NULL;
585
586 /* Init lock */
587 pthread_mutex_init(&new_session->lock, NULL);
588
589 new_session->uid = uid;
590 new_session->gid = gid;
591
592 ret = snapshot_init(&new_session->snapshot);
593 if (ret < 0) {
594 ret = LTTNG_ERR_NOMEM;
595 goto error;
596 }
597
598 new_session->rotation_pending_local = false;
599 new_session->rotation_pending_relay = false;
600 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
601
602 new_session->rotation_pending_check_timer_enabled = false;
603 new_session->rotation_schedule_timer_enabled = false;
604
605 /* Add new session to the session list */
606 session_lock_list();
607 new_session->id = add_session_list(new_session);
608 /*
609 * Add the new session to the ltt_sessions_ht_by_id.
610 * No ownership is taken by the hash table; it is merely
611 * a wrapper around the session list used for faster access
612 * by session id.
613 */
614 add_session_ht(new_session);
615 session_unlock_list();
616
617 /*
618 * Consumer is let to NULL since the create_session_uri command will set it
619 * up and, if valid, assign it to the session.
620 */
621 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
622 name, new_session->id, new_session->uid, new_session->gid);
623
624 return LTTNG_OK;
625
626 error:
627 error_asprintf:
628 free(new_session);
629
630 error_malloc:
631 return ret;
632 }
633
634 /*
635 * Check if the UID or GID match the session. Root user has access to all
636 * sessions.
637 */
638 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
639 {
640 assert(session);
641
642 if (uid != session->uid && gid != session->gid && uid != 0) {
643 return 0;
644 } else {
645 return 1;
646 }
647 }
648
649 /*
650 * Set a session's rotation state and reset all associated state.
651 *
652 * This function resets the rotation state (check timers, pending
653 * flags, etc.) and sets the result of the last rotation. The result
654 * can be queries by a liblttng-ctl client.
655 *
656 * Be careful of the result passed to this function. For instance,
657 * on failure to launch a rotation, a client will expect the rotation
658 * state to be set to "NO_ROTATION". If an error occured while the
659 * rotation was "ONGOING", result should be set to "ERROR", which will
660 * allow a client to report it.
661 *
662 * Must be called with the session and session_list locks held.
663 */
664 int session_reset_rotation_state(struct ltt_session *session,
665 enum lttng_rotation_state result)
666 {
667 int ret = 0;
668
669 ASSERT_LOCKED(ltt_session_list.lock);
670 ASSERT_LOCKED(session->lock);
671
672 session->rotation_pending_local = false;
673 session->rotation_pending_relay = false;
674 session->rotated_after_last_stop = false;
675 session->rotation_state = result;
676 if (session->rotation_pending_check_timer_enabled) {
677 ret = timer_session_rotation_pending_check_stop(session);
678 }
679 return ret;
680 }
This page took 0.066258 seconds and 3 git commands to generate.