Commit | Line | Data |
---|---|---|
5b74c7b1 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
5b74c7b1 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
91d76f53 | 5 | * |
5b74c7b1 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
e99e3664 | 9 | #include <dirent.h> |
d022620a | 10 | #include <inttypes.h> |
e99e3664 JG |
11 | #include <limits.h> |
12 | #include <pthread.h> | |
5b74c7b1 DG |
13 | #include <stdio.h> |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
a304c14c | 16 | #include <sys/stat.h> |
3d071855 | 17 | #include <sys/types.h> |
e99e3664 | 18 | #include <urcu.h> |
5b74c7b1 | 19 | |
c9e313bc | 20 | #include <common/common.hpp> |
c9e313bc | 21 | #include <common/sessiond-comm/sessiond-comm.hpp> |
e99e3664 JG |
22 | #include <common/trace-chunk.hpp> |
23 | #include <common/urcu.hpp> | |
24 | #include <common/utils.hpp> | |
25 | ||
c9e313bc | 26 | #include "lttng-sessiond.hpp" |
e99e3664 | 27 | #include <lttng/location-internal.hpp> |
1e307fab | 28 | |
e99e3664 JG |
29 | #include "cmd.hpp" |
30 | #include "kernel.hpp" | |
c9e313bc | 31 | #include "session.hpp" |
c9e313bc | 32 | #include "timer.hpp" |
e99e3664 JG |
33 | #include "trace-ust.hpp" |
34 | #include "utils.hpp" | |
5b74c7b1 | 35 | |
f1494934 | 36 | namespace { |
3e3665b8 JG |
37 | struct ltt_session_destroy_notifier_element { |
38 | ltt_session_destroy_notifier notifier; | |
39 | void *user_data; | |
40 | }; | |
41 | ||
ccbdaca4 MD |
42 | struct ltt_session_clear_notifier_element { |
43 | ltt_session_clear_notifier notifier; | |
44 | void *user_data; | |
45 | }; | |
46 | ||
e99e3664 JG |
47 | namespace ls = lttng::sessiond; |
48 | ||
8c0faa1d | 49 | /* |
b5541356 | 50 | * NOTES: |
8c0faa1d | 51 | * |
b5541356 | 52 | * No ltt_session.lock is taken here because those data structure are widely |
e1bbf989 | 53 | * spread across the lttng-tools code base so before calling functions below |
b5541356 | 54 | * that can read/write a session, the caller MUST acquire the session lock |
54d01ffb | 55 | * using session_lock() and session_unlock(). |
8c0faa1d | 56 | */ |
8c0faa1d | 57 | |
f1494934 JG |
58 | /* These characters are forbidden in a session name. Used by validate_name. */ |
59 | const char *forbidden_name_chars = "/"; | |
60 | ||
61 | /* Global hash table to keep the sessions, indexed by id. */ | |
62 | struct lttng_ht *ltt_sessions_ht_by_id = NULL; | |
63 | /* Global hash table to keep the sessions, indexed by name. */ | |
64 | struct lttng_ht *ltt_sessions_ht_by_name = NULL; | |
65 | ||
5b74c7b1 | 66 | /* |
b5541356 | 67 | * Init tracing session list. |
5b74c7b1 | 68 | * |
b5541356 | 69 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 70 | */ |
f1494934 | 71 | struct ltt_session_list the_session_list = { |
b5541356 | 72 | .lock = PTHREAD_MUTEX_INITIALIZER, |
99d688f2 | 73 | .removal_cond = PTHREAD_COND_INITIALIZER, |
a24f7994 | 74 | .next_uuid = 0, |
f1494934 | 75 | .head = CDS_LIST_HEAD_INIT(the_session_list.head), |
b5541356 | 76 | }; |
f1494934 | 77 | } /* namespace */ |
23324029 | 78 | |
1c1c3634 DG |
79 | /* |
80 | * Validate the session name for forbidden characters. | |
81 | * | |
82 | * Return 0 on success else -1 meaning a forbidden char. has been found. | |
83 | */ | |
84 | static int validate_name(const char *name) | |
85 | { | |
86 | int ret; | |
87 | char *tok, *tmp_name; | |
88 | ||
a0377dfe | 89 | LTTNG_ASSERT(name); |
1c1c3634 DG |
90 | |
91 | tmp_name = strdup(name); | |
92 | if (!tmp_name) { | |
93 | /* ENOMEM here. */ | |
94 | ret = -1; | |
95 | goto error; | |
96 | } | |
97 | ||
98 | tok = strpbrk(tmp_name, forbidden_name_chars); | |
99 | if (tok) { | |
100 | DBG("Session name %s contains a forbidden character", name); | |
101 | /* Forbidden character has been found. */ | |
102 | ret = -1; | |
103 | goto error; | |
104 | } | |
105 | ret = 0; | |
106 | ||
107 | error: | |
108 | free(tmp_name); | |
109 | return ret; | |
110 | } | |
111 | ||
5b74c7b1 | 112 | /* |
050349bb | 113 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 114 | * |
050349bb | 115 | * The caller MUST acquire the session list lock before. |
44e96653 | 116 | * Returns the unique identifier for the session. |
5b74c7b1 | 117 | */ |
d022620a | 118 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 119 | { |
a0377dfe | 120 | LTTNG_ASSERT(ls); |
0525e9ae | 121 | |
f1494934 JG |
122 | cds_list_add(&ls->list, &the_session_list.head); |
123 | return the_session_list.next_uuid++; | |
5b74c7b1 DG |
124 | } |
125 | ||
126 | /* | |
050349bb | 127 | * Delete a ltt_session structure to the global list. |
b5541356 | 128 | * |
050349bb | 129 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
130 | */ |
131 | static void del_session_list(struct ltt_session *ls) | |
132 | { | |
a0377dfe | 133 | LTTNG_ASSERT(ls); |
0525e9ae | 134 | |
5b74c7b1 | 135 | cds_list_del(&ls->list); |
5b74c7b1 DG |
136 | } |
137 | ||
b5541356 | 138 | /* |
050349bb | 139 | * Return a pointer to the session list. |
b5541356 | 140 | */ |
54d01ffb | 141 | struct ltt_session_list *session_get_list(void) |
b5541356 | 142 | { |
f1494934 | 143 | return &the_session_list; |
b5541356 DG |
144 | } |
145 | ||
99d688f2 JG |
146 | /* |
147 | * Returns once the session list is empty. | |
148 | */ | |
149 | void session_list_wait_empty(void) | |
150 | { | |
f1494934 JG |
151 | pthread_mutex_lock(&the_session_list.lock); |
152 | while (!cds_list_empty(&the_session_list.head)) { | |
153 | pthread_cond_wait(&the_session_list.removal_cond, | |
154 | &the_session_list.lock); | |
99d688f2 | 155 | } |
f1494934 | 156 | pthread_mutex_unlock(&the_session_list.lock); |
99d688f2 JG |
157 | } |
158 | ||
b5541356 | 159 | /* |
6c9cc2ab | 160 | * Acquire session list lock |
b5541356 | 161 | */ |
54d01ffb | 162 | void session_lock_list(void) |
b5541356 | 163 | { |
f1494934 | 164 | pthread_mutex_lock(&the_session_list.lock); |
b5541356 DG |
165 | } |
166 | ||
71e0a100 JG |
167 | /* |
168 | * Try to acquire session list lock | |
169 | */ | |
170 | int session_trylock_list(void) | |
171 | { | |
f1494934 | 172 | return pthread_mutex_trylock(&the_session_list.lock); |
71e0a100 JG |
173 | } |
174 | ||
b5541356 | 175 | /* |
6c9cc2ab | 176 | * Release session list lock |
b5541356 | 177 | */ |
54d01ffb | 178 | void session_unlock_list(void) |
b5541356 | 179 | { |
f1494934 | 180 | pthread_mutex_unlock(&the_session_list.lock); |
b5541356 DG |
181 | } |
182 | ||
dd73d57b JG |
183 | /* |
184 | * Get the session's consumer destination type. | |
185 | * | |
186 | * The caller must hold the session lock. | |
187 | */ | |
188 | enum consumer_dst_type session_get_consumer_destination_type( | |
189 | const struct ltt_session *session) | |
190 | { | |
191 | /* | |
192 | * The output information is duplicated in both of those session types. | |
193 | * Hence, it doesn't matter from which it is retrieved. However, it is | |
194 | * possible for only one of them to be set. | |
195 | */ | |
196 | return session->kernel_session ? | |
197 | session->kernel_session->consumer->type : | |
198 | session->ust_session->consumer->type; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Get the session's consumer network hostname. | |
203 | * The caller must ensure that the destination is of type "net". | |
204 | * | |
205 | * The caller must hold the session lock. | |
206 | */ | |
207 | const char *session_get_net_consumer_hostname(const struct ltt_session *session) | |
208 | { | |
209 | const char *hostname = NULL; | |
210 | const struct consumer_output *output; | |
211 | ||
212 | output = session->kernel_session ? | |
213 | session->kernel_session->consumer : | |
214 | session->ust_session->consumer; | |
215 | ||
216 | /* | |
217 | * hostname is assumed to be the same for both control and data | |
218 | * connections. | |
219 | */ | |
220 | switch (output->dst.net.control.dtype) { | |
221 | case LTTNG_DST_IPV4: | |
222 | hostname = output->dst.net.control.dst.ipv4; | |
223 | break; | |
224 | case LTTNG_DST_IPV6: | |
225 | hostname = output->dst.net.control.dst.ipv6; | |
226 | break; | |
227 | default: | |
228 | abort(); | |
229 | } | |
230 | return hostname; | |
231 | } | |
232 | ||
233 | /* | |
234 | * Get the session's consumer network control and data ports. | |
235 | * The caller must ensure that the destination is of type "net". | |
236 | * | |
237 | * The caller must hold the session lock. | |
238 | */ | |
239 | void session_get_net_consumer_ports(const struct ltt_session *session, | |
240 | uint16_t *control_port, uint16_t *data_port) | |
241 | { | |
242 | const struct consumer_output *output; | |
243 | ||
244 | output = session->kernel_session ? | |
245 | session->kernel_session->consumer : | |
246 | session->ust_session->consumer; | |
247 | *control_port = output->dst.net.control.port; | |
248 | *data_port = output->dst.net.data.port; | |
249 | } | |
250 | ||
5d65beab JG |
251 | /* |
252 | * Get the location of the latest trace archive produced by a rotation. | |
253 | * | |
254 | * The caller must hold the session lock. | |
255 | */ | |
256 | struct lttng_trace_archive_location *session_get_trace_archive_location( | |
3e3665b8 | 257 | const struct ltt_session *session) |
5d65beab | 258 | { |
d2956687 | 259 | int ret; |
5d65beab | 260 | struct lttng_trace_archive_location *location = NULL; |
d2956687 JG |
261 | char *chunk_path = NULL; |
262 | ||
263 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED || | |
264 | !session->last_archived_chunk_name) { | |
265 | goto end; | |
266 | } | |
5d65beab | 267 | |
5d65beab JG |
268 | switch (session_get_consumer_destination_type(session)) { |
269 | case CONSUMER_DST_LOCAL: | |
ecd1a12f MD |
270 | ret = asprintf(&chunk_path, |
271 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", | |
272 | session_get_base_path(session), | |
273 | session->last_archived_chunk_name); | |
274 | if (ret == -1) { | |
275 | goto end; | |
276 | } | |
5d65beab | 277 | location = lttng_trace_archive_location_local_create( |
d2956687 | 278 | chunk_path); |
5d65beab JG |
279 | break; |
280 | case CONSUMER_DST_NET: | |
281 | { | |
282 | const char *hostname; | |
283 | uint16_t control_port, data_port; | |
284 | ||
285 | hostname = session_get_net_consumer_hostname(session); | |
286 | session_get_net_consumer_ports(session, | |
287 | &control_port, | |
288 | &data_port); | |
289 | location = lttng_trace_archive_location_relay_create( | |
290 | hostname, | |
291 | LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP, | |
ecd1a12f | 292 | control_port, data_port, session->last_chunk_path); |
5d65beab JG |
293 | break; |
294 | } | |
295 | default: | |
296 | abort(); | |
297 | } | |
298 | end: | |
d2956687 | 299 | free(chunk_path); |
5d65beab JG |
300 | return location; |
301 | } | |
302 | ||
23324029 | 303 | /* |
e1bbf989 | 304 | * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT. |
9c6518bc JG |
305 | * |
306 | * The session list lock must be held. | |
23324029 | 307 | */ |
f848281f | 308 | static int ltt_sessions_ht_alloc(void) |
23324029 JD |
309 | { |
310 | int ret = 0; | |
311 | ||
312 | DBG("Allocating ltt_sessions_ht_by_id"); | |
313 | ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
314 | if (!ltt_sessions_ht_by_id) { | |
315 | ret = -1; | |
316 | ERR("Failed to allocate ltt_sessions_ht_by_id"); | |
317 | goto end; | |
318 | } | |
e1bbf989 JR |
319 | |
320 | DBG("Allocating ltt_sessions_ht_by_name"); | |
321 | ltt_sessions_ht_by_name = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
322 | if (!ltt_sessions_ht_by_name) { | |
323 | ret = -1; | |
324 | ERR("Failed to allocate ltt_sessions_ht_by_name"); | |
325 | goto end; | |
326 | } | |
327 | ||
23324029 JD |
328 | end: |
329 | return ret; | |
330 | } | |
331 | ||
332 | /* | |
333 | * Destroy the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
334 | * |
335 | * The session list lock must be held. | |
23324029 | 336 | */ |
accdc9bf | 337 | static void ltt_sessions_ht_destroy(void) |
23324029 | 338 | { |
e1bbf989 | 339 | if (ltt_sessions_ht_by_id) { |
3c339053 | 340 | lttng_ht_destroy(ltt_sessions_ht_by_id); |
e1bbf989 JR |
341 | ltt_sessions_ht_by_id = NULL; |
342 | } | |
343 | ||
344 | if (ltt_sessions_ht_by_name) { | |
3c339053 | 345 | lttng_ht_destroy(ltt_sessions_ht_by_name); |
e1bbf989 | 346 | ltt_sessions_ht_by_name = NULL; |
23324029 | 347 | } |
e1bbf989 JR |
348 | |
349 | return; | |
23324029 JD |
350 | } |
351 | ||
352 | /* | |
e1bbf989 JR |
353 | * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. |
354 | * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs | |
355 | * are allocated. The session list lock must be held. | |
23324029 JD |
356 | */ |
357 | static void add_session_ht(struct ltt_session *ls) | |
358 | { | |
359 | int ret; | |
360 | ||
a0377dfe | 361 | LTTNG_ASSERT(ls); |
23324029 JD |
362 | |
363 | if (!ltt_sessions_ht_by_id) { | |
364 | ret = ltt_sessions_ht_alloc(); | |
365 | if (ret) { | |
366 | ERR("Error allocating the sessions HT"); | |
367 | goto end; | |
368 | } | |
369 | } | |
e1bbf989 JR |
370 | |
371 | /* Should always be present with ltt_sessions_ht_by_id. */ | |
a0377dfe | 372 | LTTNG_ASSERT(ltt_sessions_ht_by_name); |
e1bbf989 | 373 | |
23324029 JD |
374 | lttng_ht_node_init_u64(&ls->node, ls->id); |
375 | lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node); | |
376 | ||
e1bbf989 JR |
377 | lttng_ht_node_init_str(&ls->node_by_name, ls->name); |
378 | lttng_ht_add_unique_str(ltt_sessions_ht_by_name, &ls->node_by_name); | |
379 | ||
23324029 JD |
380 | end: |
381 | return; | |
382 | } | |
383 | ||
384 | /* | |
e1bbf989 | 385 | * Test if ltt_sessions_ht_by_id/name are empty. |
2a6ebf6b | 386 | * Return `false` if hash table objects are null. |
23324029 JD |
387 | * The session list lock must be held. |
388 | */ | |
2a6ebf6b | 389 | static bool ltt_sessions_ht_empty(void) |
23324029 | 390 | { |
2a6ebf6b | 391 | bool empty = false; |
23324029 JD |
392 | |
393 | if (!ltt_sessions_ht_by_id) { | |
2a6ebf6b | 394 | /* The hash tables do not exist yet. */ |
23324029 JD |
395 | goto end; |
396 | } | |
397 | ||
a0377dfe | 398 | LTTNG_ASSERT(ltt_sessions_ht_by_name); |
e1bbf989 | 399 | |
6f729565 | 400 | if (lttng_ht_get_count(ltt_sessions_ht_by_id) != 0) { |
2a6ebf6b JR |
401 | /* Not empty.*/ |
402 | goto end; | |
403 | } | |
404 | ||
405 | /* | |
406 | * At this point it is expected that the `ltt_sessions_ht_by_name` ht is | |
407 | * empty. | |
408 | * | |
409 | * The removal from both hash tables is done in two different | |
410 | * places: | |
411 | * - removal from `ltt_sessions_ht_by_name` is done during | |
412 | * `session_destroy()` | |
413 | * - removal from `ltt_sessions_ht_by_id` is done later | |
414 | * in `session_release()` on the last reference put. | |
415 | * | |
416 | * This means that it is possible for `ltt_sessions_ht_by_name` to be | |
417 | * empty but for `ltt_sessions_ht_by_id` to still contain objects when | |
418 | * multiple sessions exists. The reverse is false, hence this sanity | |
419 | * check. | |
420 | */ | |
421 | LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name) == 0); | |
422 | empty = true; | |
23324029 | 423 | end: |
2a6ebf6b | 424 | return empty; |
23324029 JD |
425 | } |
426 | ||
427 | /* | |
ed41e570 | 428 | * Remove a ltt_session from the ltt_sessions_ht_by_id. |
e1bbf989 | 429 | * If empty, the ltt_sessions_ht_by_id/name HTs are freed. |
23324029 JD |
430 | * The session list lock must be held. |
431 | */ | |
432 | static void del_session_ht(struct ltt_session *ls) | |
433 | { | |
434 | struct lttng_ht_iter iter; | |
435 | int ret; | |
436 | ||
a0377dfe FD |
437 | LTTNG_ASSERT(ls); |
438 | LTTNG_ASSERT(ltt_sessions_ht_by_id); | |
439 | LTTNG_ASSERT(ltt_sessions_ht_by_name); | |
23324029 JD |
440 | |
441 | iter.iter.node = &ls->node.node; | |
442 | ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter); | |
a0377dfe | 443 | LTTNG_ASSERT(!ret); |
23324029 JD |
444 | |
445 | if (ltt_sessions_ht_empty()) { | |
e1bbf989 | 446 | DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables"); |
23324029 JD |
447 | ltt_sessions_ht_destroy(); |
448 | } | |
449 | } | |
450 | ||
b5541356 | 451 | /* |
6c9cc2ab | 452 | * Acquire session lock |
b5541356 | 453 | */ |
54d01ffb | 454 | void session_lock(struct ltt_session *session) |
b5541356 | 455 | { |
a0377dfe | 456 | LTTNG_ASSERT(session); |
0525e9ae | 457 | |
6c9cc2ab DG |
458 | pthread_mutex_lock(&session->lock); |
459 | } | |
b5541356 | 460 | |
6c9cc2ab DG |
461 | /* |
462 | * Release session lock | |
463 | */ | |
54d01ffb | 464 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 465 | { |
a0377dfe | 466 | LTTNG_ASSERT(session); |
0525e9ae | 467 | |
6c9cc2ab | 468 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
469 | } |
470 | ||
82b69413 JG |
471 | static |
472 | int _session_set_trace_chunk_no_lock_check(struct ltt_session *session, | |
d2956687 JG |
473 | struct lttng_trace_chunk *new_trace_chunk, |
474 | struct lttng_trace_chunk **_current_trace_chunk) | |
82b69413 | 475 | { |
78fc586b | 476 | int ret = 0; |
82b69413 | 477 | unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0; |
82b69413 JG |
478 | struct cds_lfht_iter iter; |
479 | struct consumer_socket *socket; | |
d2956687 JG |
480 | struct lttng_trace_chunk *current_trace_chunk; |
481 | uint64_t chunk_id; | |
482 | enum lttng_trace_chunk_status chunk_status; | |
82b69413 | 483 | |
d2956687 | 484 | rcu_read_lock(); |
82b69413 | 485 | /* |
d2956687 JG |
486 | * Ownership of current trace chunk is transferred to |
487 | * `current_trace_chunk`. | |
82b69413 | 488 | */ |
d2956687 JG |
489 | current_trace_chunk = session->current_trace_chunk; |
490 | session->current_trace_chunk = NULL; | |
82b69413 | 491 | if (session->ust_session) { |
d2956687 JG |
492 | lttng_trace_chunk_put( |
493 | session->ust_session->current_trace_chunk); | |
494 | session->ust_session->current_trace_chunk = NULL; | |
82b69413 JG |
495 | } |
496 | if (session->kernel_session) { | |
d2956687 JG |
497 | lttng_trace_chunk_put( |
498 | session->kernel_session->current_trace_chunk); | |
499 | session->kernel_session->current_trace_chunk = NULL; | |
82b69413 | 500 | } |
d2956687 JG |
501 | if (!new_trace_chunk) { |
502 | ret = 0; | |
503 | goto end; | |
82b69413 | 504 | } |
d2956687 | 505 | chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id); |
a0377dfe | 506 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
82b69413 | 507 | |
d2956687 JG |
508 | refs_to_acquire = 1; |
509 | refs_to_acquire += !!session->ust_session; | |
510 | refs_to_acquire += !!session->kernel_session; | |
511 | ||
512 | for (refs_acquired = 0; refs_acquired < refs_to_acquire; | |
513 | refs_acquired++) { | |
514 | if (!lttng_trace_chunk_get(new_trace_chunk)) { | |
515 | ERR("Failed to acquire reference to new trace chunk of session \"%s\"", | |
516 | session->name); | |
517 | goto error; | |
82b69413 JG |
518 | } |
519 | } | |
520 | ||
d2956687 | 521 | if (session->ust_session) { |
e5add6d0 JG |
522 | const uint64_t relayd_id = |
523 | session->ust_session->consumer->net_seq_index; | |
524 | const bool is_local_trace = | |
525 | session->ust_session->consumer->type == | |
526 | CONSUMER_DST_LOCAL; | |
527 | ||
d2956687 | 528 | session->ust_session->current_trace_chunk = new_trace_chunk; |
f8a37411 | 529 | if (is_local_trace) { |
d2956687 | 530 | enum lttng_error_code ret_error_code; |
82b69413 | 531 | |
d2956687 JG |
532 | ret_error_code = ust_app_create_channel_subdirectories( |
533 | session->ust_session); | |
534 | if (ret_error_code != LTTNG_OK) { | |
82b69413 JG |
535 | goto error; |
536 | } | |
f8a37411 | 537 | } |
d2956687 JG |
538 | cds_lfht_for_each_entry( |
539 | session->ust_session->consumer->socks->ht, | |
540 | &iter, socket, node.node) { | |
541 | pthread_mutex_lock(socket->lock); | |
542 | ret = consumer_create_trace_chunk(socket, | |
543 | relayd_id, | |
5da88b0f MD |
544 | session->id, new_trace_chunk, |
545 | DEFAULT_UST_TRACE_DIR); | |
d2956687 | 546 | pthread_mutex_unlock(socket->lock); |
f8a37411 | 547 | if (ret) { |
d2956687 | 548 | goto error; |
f8a37411 JG |
549 | } |
550 | } | |
551 | } | |
82b69413 | 552 | if (session->kernel_session) { |
e5add6d0 JG |
553 | const uint64_t relayd_id = |
554 | session->kernel_session->consumer->net_seq_index; | |
555 | const bool is_local_trace = | |
556 | session->kernel_session->consumer->type == | |
557 | CONSUMER_DST_LOCAL; | |
558 | ||
d2956687 JG |
559 | session->kernel_session->current_trace_chunk = new_trace_chunk; |
560 | if (is_local_trace) { | |
561 | enum lttng_error_code ret_error_code; | |
562 | ||
563 | ret_error_code = kernel_create_channel_subdirectories( | |
564 | session->kernel_session); | |
565 | if (ret_error_code != LTTNG_OK) { | |
d2956687 JG |
566 | goto error; |
567 | } | |
f8a37411 | 568 | } |
d2956687 JG |
569 | cds_lfht_for_each_entry( |
570 | session->kernel_session->consumer->socks->ht, | |
571 | &iter, socket, node.node) { | |
572 | pthread_mutex_lock(socket->lock); | |
573 | ret = consumer_create_trace_chunk(socket, | |
574 | relayd_id, | |
5da88b0f MD |
575 | session->id, new_trace_chunk, |
576 | DEFAULT_KERNEL_TRACE_DIR); | |
d2956687 | 577 | pthread_mutex_unlock(socket->lock); |
f8a37411 | 578 | if (ret) { |
d2956687 | 579 | goto error; |
f8a37411 JG |
580 | } |
581 | } | |
582 | } | |
82b69413 JG |
583 | |
584 | /* | |
585 | * Update local current trace chunk state last, only if all remote | |
d2956687 | 586 | * creations succeeded. |
82b69413 JG |
587 | */ |
588 | session->current_trace_chunk = new_trace_chunk; | |
d2956687 JG |
589 | LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id); |
590 | end: | |
591 | if (_current_trace_chunk) { | |
592 | *_current_trace_chunk = current_trace_chunk; | |
593 | current_trace_chunk = NULL; | |
594 | } | |
595 | end_no_move: | |
596 | rcu_read_unlock(); | |
597 | lttng_trace_chunk_put(current_trace_chunk); | |
598 | return ret; | |
599 | error: | |
82b69413 | 600 | if (session->ust_session) { |
d2956687 | 601 | session->ust_session->current_trace_chunk = NULL; |
82b69413 JG |
602 | } |
603 | if (session->kernel_session) { | |
d2956687 | 604 | session->kernel_session->current_trace_chunk = NULL; |
82b69413 | 605 | } |
f8a37411 | 606 | /* |
82b69413 JG |
607 | * Release references taken in the case where all references could not |
608 | * be acquired. | |
609 | */ | |
610 | refs_to_release = refs_to_acquire - refs_acquired; | |
611 | for (i = 0; i < refs_to_release; i++) { | |
612 | lttng_trace_chunk_put(new_trace_chunk); | |
613 | } | |
d2956687 JG |
614 | ret = -1; |
615 | goto end_no_move; | |
82b69413 JG |
616 | } |
617 | ||
d2956687 | 618 | struct lttng_trace_chunk *session_create_new_trace_chunk( |
348a81dc JG |
619 | const struct ltt_session *session, |
620 | const struct consumer_output *consumer_output_override, | |
82b69413 JG |
621 | const char *session_base_path_override, |
622 | const char *chunk_name_override) | |
623 | { | |
624 | int ret; | |
82b69413 JG |
625 | struct lttng_trace_chunk *trace_chunk = NULL; |
626 | enum lttng_trace_chunk_status chunk_status; | |
d2956687 | 627 | const time_t chunk_creation_ts = time(NULL); |
348a81dc JG |
628 | bool is_local_trace; |
629 | const char *base_path; | |
cbf53d23 | 630 | struct lttng_directory_handle *session_output_directory = NULL; |
82b69413 | 631 | const struct lttng_credentials session_credentials = { |
ff588497 JR |
632 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid), |
633 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid), | |
82b69413 JG |
634 | }; |
635 | uint64_t next_chunk_id; | |
348a81dc | 636 | const struct consumer_output *output; |
a7ceb342 | 637 | const char *new_path; |
348a81dc JG |
638 | |
639 | if (consumer_output_override) { | |
640 | output = consumer_output_override; | |
641 | } else { | |
a0377dfe | 642 | LTTNG_ASSERT(session->ust_session || session->kernel_session); |
348a81dc JG |
643 | output = session->ust_session ? |
644 | session->ust_session->consumer : | |
645 | session->kernel_session->consumer; | |
646 | } | |
647 | ||
648 | is_local_trace = output->type == CONSUMER_DST_LOCAL; | |
649 | base_path = session_base_path_override ? : | |
650 | consumer_output_get_base_path(output); | |
82b69413 | 651 | |
d2956687 JG |
652 | if (chunk_creation_ts == (time_t) -1) { |
653 | PERROR("Failed to sample time while creation session \"%s\" trace chunk", | |
82b69413 | 654 | session->name); |
82b69413 JG |
655 | goto error; |
656 | } | |
82b69413 | 657 | |
d2956687 JG |
658 | next_chunk_id = session->most_recent_chunk_id.is_set ? |
659 | session->most_recent_chunk_id.value + 1 : 0; | |
82b69413 | 660 | |
a7ceb342 MD |
661 | if (session->current_trace_chunk && |
662 | !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) { | |
663 | chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk, | |
664 | DEFAULT_CHUNK_TMP_OLD_DIRECTORY); | |
665 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
666 | goto error; | |
667 | } | |
668 | } | |
669 | if (!session->current_trace_chunk) { | |
670 | if (!session->rotated) { | |
671 | new_path = ""; | |
672 | } else { | |
673 | new_path = NULL; | |
674 | } | |
675 | } else { | |
676 | new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY; | |
677 | } | |
678 | ||
d2956687 | 679 | trace_chunk = lttng_trace_chunk_create(next_chunk_id, |
a7ceb342 | 680 | chunk_creation_ts, new_path); |
82b69413 | 681 | if (!trace_chunk) { |
82b69413 JG |
682 | goto error; |
683 | } | |
684 | ||
685 | if (chunk_name_override) { | |
686 | chunk_status = lttng_trace_chunk_override_name(trace_chunk, | |
687 | chunk_name_override); | |
d2956687 | 688 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
82b69413 JG |
689 | goto error; |
690 | } | |
691 | } | |
692 | ||
693 | if (!is_local_trace) { | |
694 | /* | |
695 | * No need to set crendentials and output directory | |
696 | * for remote trace chunks. | |
697 | */ | |
d2956687 | 698 | goto end; |
82b69413 JG |
699 | } |
700 | ||
701 | chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, | |
702 | &session_credentials); | |
703 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
82b69413 JG |
704 | goto error; |
705 | } | |
706 | ||
d2956687 JG |
707 | DBG("Creating base output directory of session \"%s\" at %s", |
708 | session->name, base_path); | |
82b69413 JG |
709 | ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, |
710 | session->uid, session->gid); | |
711 | if (ret) { | |
82b69413 JG |
712 | goto error; |
713 | } | |
cbf53d23 JG |
714 | session_output_directory = lttng_directory_handle_create(base_path); |
715 | if (!session_output_directory) { | |
82b69413 JG |
716 | goto error; |
717 | } | |
718 | chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, | |
cbf53d23 JG |
719 | session_output_directory); |
720 | lttng_directory_handle_put(session_output_directory); | |
721 | session_output_directory = NULL; | |
82b69413 | 722 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
82b69413 JG |
723 | goto error; |
724 | } | |
d2956687 JG |
725 | end: |
726 | return trace_chunk; | |
82b69413 | 727 | error: |
cbf53d23 | 728 | lttng_directory_handle_put(session_output_directory); |
82b69413 | 729 | lttng_trace_chunk_put(trace_chunk); |
d2956687 JG |
730 | trace_chunk = NULL; |
731 | goto end; | |
732 | } | |
733 | ||
343defc2 | 734 | int session_close_trace_chunk(struct ltt_session *session, |
bbc4768c | 735 | struct lttng_trace_chunk *trace_chunk, |
343defc2 | 736 | enum lttng_trace_chunk_command_type close_command, |
ecd1a12f | 737 | char *closed_trace_chunk_path) |
d2956687 JG |
738 | { |
739 | int ret = 0; | |
740 | bool error_occurred = false; | |
741 | struct cds_lfht_iter iter; | |
742 | struct consumer_socket *socket; | |
743 | enum lttng_trace_chunk_status chunk_status; | |
744 | const time_t chunk_close_timestamp = time(NULL); | |
a7ceb342 | 745 | const char *new_path; |
d2956687 | 746 | |
343defc2 MD |
747 | chunk_status = lttng_trace_chunk_set_close_command( |
748 | trace_chunk, close_command); | |
749 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
750 | ret = -1; | |
751 | goto end; | |
bbc4768c JG |
752 | } |
753 | ||
d2956687 JG |
754 | if (chunk_close_timestamp == (time_t) -1) { |
755 | ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"", | |
756 | session->name); | |
757 | ret = -1; | |
758 | goto end; | |
759 | } | |
a7ceb342 MD |
760 | |
761 | if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && !session->rotated) { | |
762 | /* New chunk stays in session output directory. */ | |
763 | new_path = ""; | |
764 | } else { | |
765 | /* Use chunk name for new chunk. */ | |
766 | new_path = NULL; | |
767 | } | |
768 | if (session->current_trace_chunk && | |
769 | !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) { | |
770 | /* Rename new chunk path. */ | |
771 | chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk, | |
772 | new_path); | |
773 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
774 | ret = -1; | |
775 | goto end; | |
776 | } | |
777 | } | |
778 | if (!lttng_trace_chunk_get_name_overridden(trace_chunk) && | |
779 | close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) { | |
780 | const char *old_path; | |
781 | ||
782 | if (!session->rotated) { | |
783 | old_path = ""; | |
784 | } else { | |
785 | old_path = NULL; | |
786 | } | |
787 | /* We need to move back the .tmp_old_chunk to its rightful place. */ | |
788 | chunk_status = lttng_trace_chunk_rename_path(trace_chunk, | |
789 | old_path); | |
790 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
791 | ret = -1; | |
792 | goto end; | |
793 | } | |
794 | } | |
795 | if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) { | |
796 | session->rotated = true; | |
797 | } | |
d2956687 JG |
798 | chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, |
799 | chunk_close_timestamp); | |
800 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
801 | ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"", | |
802 | session->name); | |
803 | ret = -1; | |
804 | goto end; | |
805 | } | |
806 | ||
807 | if (session->ust_session) { | |
0a184d4e JG |
808 | const uint64_t relayd_id = |
809 | session->ust_session->consumer->net_seq_index; | |
810 | ||
d2956687 JG |
811 | cds_lfht_for_each_entry( |
812 | session->ust_session->consumer->socks->ht, | |
813 | &iter, socket, node.node) { | |
814 | pthread_mutex_lock(socket->lock); | |
815 | ret = consumer_close_trace_chunk(socket, | |
0a184d4e | 816 | relayd_id, |
d2956687 | 817 | session->id, |
ecd1a12f | 818 | trace_chunk, closed_trace_chunk_path); |
d2956687 JG |
819 | pthread_mutex_unlock(socket->lock); |
820 | if (ret) { | |
821 | ERR("Failed to close trace chunk on user space consumer"); | |
822 | error_occurred = true; | |
823 | } | |
824 | } | |
825 | } | |
826 | if (session->kernel_session) { | |
0a184d4e JG |
827 | const uint64_t relayd_id = |
828 | session->kernel_session->consumer->net_seq_index; | |
829 | ||
d2956687 JG |
830 | cds_lfht_for_each_entry( |
831 | session->kernel_session->consumer->socks->ht, | |
832 | &iter, socket, node.node) { | |
833 | pthread_mutex_lock(socket->lock); | |
834 | ret = consumer_close_trace_chunk(socket, | |
0a184d4e | 835 | relayd_id, |
d2956687 | 836 | session->id, |
ecd1a12f | 837 | trace_chunk, closed_trace_chunk_path); |
d2956687 JG |
838 | pthread_mutex_unlock(socket->lock); |
839 | if (ret) { | |
840 | ERR("Failed to close trace chunk on kernel consumer"); | |
841 | error_occurred = true; | |
842 | } | |
843 | } | |
844 | } | |
845 | ret = error_occurred ? -1 : 0; | |
82b69413 | 846 | end: |
d2956687 | 847 | return ret; |
82b69413 JG |
848 | } |
849 | ||
04ed9e10 JG |
850 | /* |
851 | * This function skips the metadata channel as the begin/end timestamps of a | |
852 | * metadata packet are useless. | |
853 | * | |
854 | * Moreover, opening a packet after a "clear" will cause problems for live | |
855 | * sessions as it will introduce padding that was not part of the first trace | |
856 | * chunk. The relay daemon expects the content of the metadata stream of | |
857 | * successive metadata trace chunks to be strict supersets of one another. | |
858 | * | |
859 | * For example, flushing a packet at the beginning of the metadata stream of | |
860 | * a trace chunk resulting from a "clear" session command will cause the | |
861 | * size of the metadata stream of the new trace chunk to not match the size of | |
862 | * the metadata stream of the original chunk. This will confuse the relay | |
863 | * daemon as the same "offset" in a metadata stream will no longer point | |
864 | * to the same content. | |
865 | */ | |
866 | static | |
867 | enum lttng_error_code session_kernel_open_packets(struct ltt_session *session) | |
868 | { | |
869 | enum lttng_error_code ret = LTTNG_OK; | |
870 | struct consumer_socket *socket; | |
871 | struct lttng_ht_iter iter; | |
872 | struct cds_lfht_node *node; | |
873 | struct ltt_kernel_channel *chan; | |
874 | ||
875 | rcu_read_lock(); | |
876 | ||
877 | cds_lfht_first(session->kernel_session->consumer->socks->ht, &iter.iter); | |
878 | node = cds_lfht_iter_get_node(&iter.iter); | |
879 | socket = container_of(node, typeof(*socket), node.node); | |
880 | ||
881 | cds_list_for_each_entry(chan, | |
882 | &session->kernel_session->channel_list.head, list) { | |
883 | int open_ret; | |
884 | ||
885 | DBG("Open packet of kernel channel: channel key = %" PRIu64 | |
886 | ", session name = %s, session_id = %" PRIu64, | |
887 | chan->key, session->name, session->id); | |
888 | ||
889 | open_ret = consumer_open_channel_packets(socket, chan->key); | |
890 | if (open_ret < 0) { | |
891 | /* General error (no known error expected). */ | |
892 | ret = LTTNG_ERR_UNK; | |
893 | goto end; | |
894 | } | |
895 | } | |
896 | ||
897 | end: | |
898 | rcu_read_unlock(); | |
899 | return ret; | |
900 | } | |
901 | ||
902 | enum lttng_error_code session_open_packets(struct ltt_session *session) | |
903 | { | |
904 | enum lttng_error_code ret = LTTNG_OK; | |
905 | ||
906 | DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64, | |
907 | session->name, session->id); | |
908 | ||
909 | if (session->ust_session) { | |
910 | ret = ust_app_open_packets(session); | |
911 | if (ret != LTTNG_OK) { | |
912 | goto end; | |
913 | } | |
914 | } | |
915 | ||
916 | if (session->kernel_session) { | |
917 | ret = session_kernel_open_packets(session); | |
918 | if (ret != LTTNG_OK) { | |
919 | goto end; | |
920 | } | |
921 | } | |
922 | ||
923 | end: | |
924 | return ret; | |
925 | } | |
926 | ||
82b69413 JG |
927 | /* |
928 | * Set a session's current trace chunk. | |
929 | * | |
930 | * Must be called with the session lock held. | |
931 | */ | |
932 | int session_set_trace_chunk(struct ltt_session *session, | |
d2956687 JG |
933 | struct lttng_trace_chunk *new_trace_chunk, |
934 | struct lttng_trace_chunk **current_trace_chunk) | |
82b69413 JG |
935 | { |
936 | ASSERT_LOCKED(session->lock); | |
d2956687 JG |
937 | return _session_set_trace_chunk_no_lock_check(session, new_trace_chunk, |
938 | current_trace_chunk); | |
82b69413 JG |
939 | } |
940 | ||
3e3665b8 JG |
941 | static |
942 | void session_notify_destruction(const struct ltt_session *session) | |
943 | { | |
944 | size_t i; | |
945 | const size_t count = lttng_dynamic_array_get_count( | |
946 | &session->destroy_notifiers); | |
947 | ||
948 | for (i = 0; i < count; i++) { | |
949 | const struct ltt_session_destroy_notifier_element *element = | |
7966af57 | 950 | (ltt_session_destroy_notifier_element *) lttng_dynamic_array_get_element( |
3e3665b8 JG |
951 | &session->destroy_notifiers, i); |
952 | ||
953 | element->notifier(session, element->user_data); | |
954 | } | |
955 | } | |
956 | ||
ccbdaca4 MD |
957 | /* |
958 | * Fire each clear notifier once, and remove them from the array. | |
959 | */ | |
960 | void session_notify_clear(struct ltt_session *session) | |
961 | { | |
962 | size_t i; | |
963 | const size_t count = lttng_dynamic_array_get_count( | |
964 | &session->clear_notifiers); | |
965 | ||
966 | for (i = 0; i < count; i++) { | |
967 | const struct ltt_session_clear_notifier_element *element = | |
7966af57 | 968 | (ltt_session_clear_notifier_element *) lttng_dynamic_array_get_element( |
ccbdaca4 MD |
969 | &session->clear_notifiers, i); |
970 | ||
971 | element->notifier(session, element->user_data); | |
972 | } | |
973 | lttng_dynamic_array_clear(&session->clear_notifiers); | |
974 | } | |
975 | ||
e32d7f27 JG |
976 | static |
977 | void session_release(struct urcu_ref *ref) | |
978 | { | |
979 | int ret; | |
980 | struct ltt_ust_session *usess; | |
981 | struct ltt_kernel_session *ksess; | |
982 | struct ltt_session *session = container_of(ref, typeof(*session), ref); | |
7fdbed1c | 983 | const bool session_published = session->published; |
e32d7f27 | 984 | |
a0377dfe | 985 | LTTNG_ASSERT(!session->chunk_being_archived); |
d2956687 | 986 | |
e32d7f27 JG |
987 | usess = session->ust_session; |
988 | ksess = session->kernel_session; | |
3e3665b8 | 989 | |
f8a37411 | 990 | /* Clean kernel session teardown, keeping data for destroy notifier. */ |
e32d7f27 JG |
991 | kernel_destroy_session(ksess); |
992 | ||
d070c424 | 993 | /* UST session teardown, keeping data for destroy notifier. */ |
e32d7f27 JG |
994 | if (usess) { |
995 | /* Close any relayd session */ | |
996 | consumer_output_send_destroy_relayd(usess->consumer); | |
997 | ||
998 | /* Destroy every UST application related to this session. */ | |
999 | ret = ust_app_destroy_trace_all(usess); | |
1000 | if (ret) { | |
1001 | ERR("Error in ust_app_destroy_trace_all"); | |
1002 | } | |
1003 | ||
d070c424 | 1004 | /* Clean up the rest, keeping destroy notifier data. */ |
e32d7f27 JG |
1005 | trace_ust_destroy_session(usess); |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Must notify the kernel thread here to update it's poll set in order to | |
1010 | * remove the channel(s)' fd just destroyed. | |
1011 | */ | |
412d7227 | 1012 | ret = notify_thread_pipe(the_kernel_poll_pipe[1]); |
e32d7f27 JG |
1013 | if (ret < 0) { |
1014 | PERROR("write kernel poll pipe"); | |
1015 | } | |
1016 | ||
1017 | DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id); | |
e32d7f27 | 1018 | |
e32d7f27 | 1019 | snapshot_destroy(&session->snapshot); |
99d688f2 | 1020 | |
82b69413 JG |
1021 | pthread_mutex_destroy(&session->lock); |
1022 | ||
7fdbed1c | 1023 | if (session_published) { |
f1494934 | 1024 | ASSERT_LOCKED(the_session_list.lock); |
f4cc5e83 JG |
1025 | del_session_list(session); |
1026 | del_session_ht(session); | |
f4cc5e83 | 1027 | } |
7fdbed1c | 1028 | session_notify_destruction(session); |
d070c424 | 1029 | |
1ac9cb73 | 1030 | consumer_output_put(session->consumer); |
d070c424 MD |
1031 | kernel_free_session(ksess); |
1032 | session->kernel_session = NULL; | |
1033 | if (usess) { | |
1034 | trace_ust_free_session(usess); | |
1035 | session->ust_session = NULL; | |
1036 | } | |
7fdbed1c | 1037 | lttng_dynamic_array_reset(&session->destroy_notifiers); |
ccbdaca4 | 1038 | lttng_dynamic_array_reset(&session->clear_notifiers); |
d2956687 | 1039 | free(session->last_archived_chunk_name); |
6fa5fe7c | 1040 | free(session->base_path); |
c08136a3 | 1041 | lttng_trigger_put(session->rotate_trigger); |
e32d7f27 | 1042 | free(session); |
7fdbed1c JG |
1043 | if (session_published) { |
1044 | /* | |
1045 | * Broadcast after free-ing to ensure the memory is | |
1046 | * reclaimed before the main thread exits. | |
1047 | */ | |
f1494934 JG |
1048 | ASSERT_LOCKED(the_session_list.lock); |
1049 | pthread_cond_broadcast(&the_session_list.removal_cond); | |
7fdbed1c | 1050 | } |
e32d7f27 JG |
1051 | } |
1052 | ||
1053 | /* | |
1054 | * Acquire a reference to a session. | |
1055 | * This function may fail (return false); its return value must be checked. | |
1056 | */ | |
1057 | bool session_get(struct ltt_session *session) | |
1058 | { | |
1059 | return urcu_ref_get_unless_zero(&session->ref); | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Release a reference to a session. | |
1064 | */ | |
1065 | void session_put(struct ltt_session *session) | |
1066 | { | |
b178f53e JG |
1067 | if (!session) { |
1068 | return; | |
1069 | } | |
e32d7f27 JG |
1070 | /* |
1071 | * The session list lock must be held as any session_put() | |
1072 | * may cause the removal of the session from the session_list. | |
1073 | */ | |
f1494934 | 1074 | ASSERT_LOCKED(the_session_list.lock); |
a0377dfe | 1075 | LTTNG_ASSERT(session->ref.refcount); |
e32d7f27 JG |
1076 | urcu_ref_put(&session->ref, session_release); |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * Destroy a session. | |
1081 | * | |
1082 | * This method does not immediately release/free the session as other | |
1083 | * components may still hold a reference to the session. However, | |
1084 | * the session should no longer be presented to the user. | |
1085 | * | |
1086 | * Releases the session list's reference to the session | |
1087 | * and marks it as destroyed. Iterations on the session list should be | |
1088 | * mindful of the "destroyed" flag. | |
1089 | */ | |
1090 | void session_destroy(struct ltt_session *session) | |
1091 | { | |
ed41e570 JR |
1092 | int ret; |
1093 | struct lttng_ht_iter iter; | |
1094 | ||
a0377dfe | 1095 | LTTNG_ASSERT(!session->destroyed); |
e32d7f27 | 1096 | session->destroyed = true; |
ed41e570 JR |
1097 | |
1098 | /* | |
1099 | * Remove immediately from the "session by name" hash table. Only one | |
1100 | * session is expected to exist with a given name for at any given time. | |
1101 | * | |
1102 | * Even if a session still technically exists for a little while longer, | |
1103 | * there is no point in performing action on a "destroyed" session. | |
1104 | */ | |
1105 | iter.iter.node = &session->node_by_name.node; | |
1106 | ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter); | |
1107 | LTTNG_ASSERT(!ret); | |
1108 | ||
e32d7f27 JG |
1109 | session_put(session); |
1110 | } | |
1111 | ||
3e3665b8 JG |
1112 | int session_add_destroy_notifier(struct ltt_session *session, |
1113 | ltt_session_destroy_notifier notifier, void *user_data) | |
1114 | { | |
1115 | const struct ltt_session_destroy_notifier_element element = { | |
1116 | .notifier = notifier, | |
1117 | .user_data = user_data | |
1118 | }; | |
1119 | ||
1120 | return lttng_dynamic_array_add_element(&session->destroy_notifiers, | |
1121 | &element); | |
1122 | } | |
1123 | ||
ccbdaca4 MD |
1124 | int session_add_clear_notifier(struct ltt_session *session, |
1125 | ltt_session_clear_notifier notifier, void *user_data) | |
1126 | { | |
1127 | const struct ltt_session_clear_notifier_element element = { | |
1128 | .notifier = notifier, | |
1129 | .user_data = user_data | |
1130 | }; | |
1131 | ||
1132 | return lttng_dynamic_array_add_element(&session->clear_notifiers, | |
1133 | &element); | |
1134 | } | |
1135 | ||
5b74c7b1 | 1136 | /* |
74babd95 | 1137 | * Return a ltt_session structure ptr that matches name. If no session found, |
23324029 | 1138 | * NULL is returned. This must be called with the session list lock held using |
74babd95 | 1139 | * session_lock_list and session_unlock_list. |
e32d7f27 | 1140 | * A reference to the session is implicitly acquired by this function. |
5b74c7b1 | 1141 | */ |
58a1a227 | 1142 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 1143 | { |
5b74c7b1 DG |
1144 | struct ltt_session *iter; |
1145 | ||
a0377dfe | 1146 | LTTNG_ASSERT(name); |
f1494934 | 1147 | ASSERT_LOCKED(the_session_list.lock); |
0525e9ae | 1148 | |
5f822d0a DG |
1149 | DBG2("Trying to find session by name %s", name); |
1150 | ||
f1494934 | 1151 | cds_list_for_each_entry(iter, &the_session_list.head, list) { |
e32d7f27 JG |
1152 | if (!strncmp(iter->name, name, NAME_MAX) && |
1153 | !iter->destroyed) { | |
74babd95 | 1154 | goto found; |
5b74c7b1 DG |
1155 | } |
1156 | } | |
1157 | ||
e32d7f27 | 1158 | return NULL; |
74babd95 | 1159 | found: |
e32d7f27 | 1160 | return session_get(iter) ? iter : NULL; |
5b74c7b1 DG |
1161 | } |
1162 | ||
23324029 JD |
1163 | /* |
1164 | * Return an ltt_session that matches the id. If no session is found, | |
1165 | * NULL is returned. This must be called with rcu_read_lock and | |
1166 | * session list lock held (to guarantee the lifetime of the session). | |
1167 | */ | |
1168 | struct ltt_session *session_find_by_id(uint64_t id) | |
1169 | { | |
1170 | struct lttng_ht_node_u64 *node; | |
1171 | struct lttng_ht_iter iter; | |
1172 | struct ltt_session *ls; | |
1173 | ||
48b7cdc2 | 1174 | ASSERT_RCU_READ_LOCKED(); |
f1494934 | 1175 | ASSERT_LOCKED(the_session_list.lock); |
e32d7f27 | 1176 | |
d68ec974 JG |
1177 | if (!ltt_sessions_ht_by_id) { |
1178 | goto end; | |
1179 | } | |
1180 | ||
23324029 JD |
1181 | lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter); |
1182 | node = lttng_ht_iter_get_node_u64(&iter); | |
1183 | if (node == NULL) { | |
d68ec974 | 1184 | goto end; |
23324029 JD |
1185 | } |
1186 | ls = caa_container_of(node, struct ltt_session, node); | |
1187 | ||
1188 | DBG3("Session %" PRIu64 " found by id.", id); | |
e32d7f27 | 1189 | return session_get(ls) ? ls : NULL; |
23324029 | 1190 | |
d68ec974 | 1191 | end: |
23324029 JD |
1192 | DBG3("Session %" PRIu64 " NOT found by id", id); |
1193 | return NULL; | |
1194 | } | |
1195 | ||
5b74c7b1 | 1196 | /* |
b178f53e JG |
1197 | * Create a new session and add it to the session list. |
1198 | * Session list lock must be held by the caller. | |
5b74c7b1 | 1199 | */ |
b178f53e | 1200 | enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, |
e3876bf0 | 1201 | struct ltt_session **out_session) |
5b74c7b1 | 1202 | { |
f3ed775e | 1203 | int ret; |
b178f53e JG |
1204 | enum lttng_error_code ret_code; |
1205 | struct ltt_session *new_session = NULL; | |
e07ae692 | 1206 | |
f1494934 | 1207 | ASSERT_LOCKED(the_session_list.lock); |
b178f53e JG |
1208 | if (name) { |
1209 | struct ltt_session *clashing_session; | |
1210 | ||
1211 | clashing_session = session_find_by_name(name); | |
1212 | if (clashing_session) { | |
1213 | session_put(clashing_session); | |
1214 | ret_code = LTTNG_ERR_EXIST_SESS; | |
1215 | goto error; | |
1216 | } | |
1217 | } | |
64803277 | 1218 | new_session = zmalloc<ltt_session>(); |
b178f53e JG |
1219 | if (!new_session) { |
1220 | PERROR("Failed to allocate an ltt_session structure"); | |
1221 | ret_code = LTTNG_ERR_NOMEM; | |
1222 | goto error; | |
5b74c7b1 DG |
1223 | } |
1224 | ||
3e3665b8 | 1225 | lttng_dynamic_array_init(&new_session->destroy_notifiers, |
93bed9fe JG |
1226 | sizeof(struct ltt_session_destroy_notifier_element), |
1227 | NULL); | |
ccbdaca4 MD |
1228 | lttng_dynamic_array_init(&new_session->clear_notifiers, |
1229 | sizeof(struct ltt_session_clear_notifier_element), | |
1230 | NULL); | |
e32d7f27 | 1231 | urcu_ref_init(&new_session->ref); |
b178f53e | 1232 | pthread_mutex_init(&new_session->lock, NULL); |
e32d7f27 | 1233 | |
b178f53e JG |
1234 | new_session->creation_time = time(NULL); |
1235 | if (new_session->creation_time == (time_t) -1) { | |
1236 | PERROR("Failed to sample session creation time"); | |
1237 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
f3ed775e DG |
1238 | goto error; |
1239 | } | |
1240 | ||
b178f53e JG |
1241 | /* Create default consumer output. */ |
1242 | new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
1243 | if (new_session->consumer == NULL) { | |
1244 | ret_code = LTTNG_ERR_NOMEM; | |
1c1c3634 DG |
1245 | goto error; |
1246 | } | |
1247 | ||
b178f53e JG |
1248 | if (name) { |
1249 | ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name)); | |
1250 | if (ret) { | |
1251 | ret_code = LTTNG_ERR_SESSION_INVALID_CHAR; | |
1252 | goto error; | |
1253 | } | |
1254 | ret = validate_name(name); | |
1255 | if (ret < 0) { | |
1256 | ret_code = LTTNG_ERR_SESSION_INVALID_CHAR; | |
1257 | goto error; | |
1258 | } | |
1259 | } else { | |
1260 | int i = 0; | |
1261 | bool found_name = false; | |
1262 | char datetime[16]; | |
1263 | struct tm *timeinfo; | |
1264 | ||
1265 | timeinfo = localtime(&new_session->creation_time); | |
1266 | if (!timeinfo) { | |
1267 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1268 | goto error; | |
1269 | } | |
1270 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
1271 | for (i = 0; i < INT_MAX; i++) { | |
1272 | struct ltt_session *clashing_session; | |
1273 | ||
1274 | if (i == 0) { | |
1275 | ret = snprintf(new_session->name, | |
1276 | sizeof(new_session->name), | |
1277 | "%s-%s", | |
1278 | DEFAULT_SESSION_NAME, | |
1279 | datetime); | |
1280 | } else { | |
1281 | ret = snprintf(new_session->name, | |
1282 | sizeof(new_session->name), | |
1283 | "%s%d-%s", | |
1284 | DEFAULT_SESSION_NAME, i, | |
1285 | datetime); | |
1286 | } | |
46ef2188 | 1287 | new_session->name_contains_creation_time = true; |
b178f53e JG |
1288 | if (ret == -1 || ret >= sizeof(new_session->name)) { |
1289 | /* | |
1290 | * Null-terminate in case the name is used | |
1291 | * in logging statements. | |
1292 | */ | |
1293 | new_session->name[sizeof(new_session->name) - 1] = '\0'; | |
1294 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1295 | goto error; | |
1296 | } | |
1297 | ||
1298 | clashing_session = | |
1299 | session_find_by_name(new_session->name); | |
1300 | session_put(clashing_session); | |
1301 | if (!clashing_session) { | |
1302 | found_name = true; | |
1303 | break; | |
1304 | } | |
1305 | } | |
1306 | if (found_name) { | |
1307 | DBG("Generated session name \"%s\"", new_session->name); | |
1308 | new_session->has_auto_generated_name = true; | |
1309 | } else { | |
1310 | ERR("Failed to auto-generate a session name"); | |
1311 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1312 | goto error; | |
1313 | } | |
1314 | } | |
1315 | ||
d3e2ba59 | 1316 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
1317 | if (ret < 0) { |
1318 | if (errno == ENAMETOOLONG) { | |
1319 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
b178f53e JG |
1320 | ERR("Hostname exceeds the maximal permitted length and has been truncated to %s", |
1321 | new_session->hostname); | |
73184835 | 1322 | } else { |
b178f53e | 1323 | ret_code = LTTNG_ERR_SESSION_FAIL; |
73184835 DG |
1324 | goto error; |
1325 | } | |
d3e2ba59 JD |
1326 | } |
1327 | ||
6df2e2c9 MD |
1328 | new_session->uid = uid; |
1329 | new_session->gid = gid; | |
1330 | ||
6dc3064a DG |
1331 | ret = snapshot_init(&new_session->snapshot); |
1332 | if (ret < 0) { | |
b178f53e | 1333 | ret_code = LTTNG_ERR_NOMEM; |
6dc3064a DG |
1334 | goto error; |
1335 | } | |
1336 | ||
4f23c583 | 1337 | new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION; |
92816cc3 | 1338 | |
b178f53e | 1339 | /* Add new session to the session list. */ |
a991f516 | 1340 | new_session->id = add_session_list(new_session); |
b178f53e | 1341 | |
23324029 JD |
1342 | /* |
1343 | * Add the new session to the ltt_sessions_ht_by_id. | |
1344 | * No ownership is taken by the hash table; it is merely | |
1345 | * a wrapper around the session list used for faster access | |
1346 | * by session id. | |
1347 | */ | |
1348 | add_session_ht(new_session); | |
f4cc5e83 | 1349 | new_session->published = true; |
b5541356 | 1350 | |
a4b92340 | 1351 | /* |
b178f53e JG |
1352 | * Consumer is left to NULL since the create_session_uri command will |
1353 | * set it up and, if valid, assign it to the session. | |
a4b92340 | 1354 | */ |
b178f53e JG |
1355 | DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d", |
1356 | new_session->name, new_session->id, new_session->uid, | |
1357 | new_session->gid); | |
1358 | ret_code = LTTNG_OK; | |
1359 | end: | |
1360 | if (new_session) { | |
1361 | (void) session_get(new_session); | |
1362 | *out_session = new_session; | |
1363 | } | |
1364 | return ret_code; | |
5b74c7b1 | 1365 | error: |
f4cc5e83 | 1366 | session_put(new_session); |
b178f53e JG |
1367 | new_session = NULL; |
1368 | goto end; | |
5b74c7b1 | 1369 | } |
2f77fc4b DG |
1370 | |
1371 | /* | |
d7b377ed | 1372 | * Check if the UID matches the session. Root user has access to all |
2f77fc4b DG |
1373 | * sessions. |
1374 | */ | |
d7b377ed | 1375 | bool session_access_ok(struct ltt_session *session, uid_t uid) |
2f77fc4b | 1376 | { |
a0377dfe | 1377 | LTTNG_ASSERT(session); |
d7b377ed | 1378 | return (uid == session->uid) || uid == 0; |
2f77fc4b | 1379 | } |
2961f09e JG |
1380 | |
1381 | /* | |
1382 | * Set a session's rotation state and reset all associated state. | |
1383 | * | |
1384 | * This function resets the rotation state (check timers, pending | |
1385 | * flags, etc.) and sets the result of the last rotation. The result | |
1386 | * can be queries by a liblttng-ctl client. | |
1387 | * | |
1388 | * Be careful of the result passed to this function. For instance, | |
1389 | * on failure to launch a rotation, a client will expect the rotation | |
83ed9e90 | 1390 | * state to be set to "NO_ROTATION". If an error occurred while the |
2961f09e JG |
1391 | * rotation was "ONGOING", result should be set to "ERROR", which will |
1392 | * allow a client to report it. | |
1393 | * | |
1394 | * Must be called with the session and session_list locks held. | |
1395 | */ | |
1396 | int session_reset_rotation_state(struct ltt_session *session, | |
1397 | enum lttng_rotation_state result) | |
1398 | { | |
1399 | int ret = 0; | |
1400 | ||
f1494934 | 1401 | ASSERT_LOCKED(the_session_list.lock); |
2961f09e JG |
1402 | ASSERT_LOCKED(session->lock); |
1403 | ||
2961f09e JG |
1404 | session->rotation_state = result; |
1405 | if (session->rotation_pending_check_timer_enabled) { | |
1406 | ret = timer_session_rotation_pending_check_stop(session); | |
1407 | } | |
d2956687 JG |
1408 | if (session->chunk_being_archived) { |
1409 | uint64_t chunk_id; | |
1410 | enum lttng_trace_chunk_status chunk_status; | |
1411 | ||
1412 | chunk_status = lttng_trace_chunk_get_id( | |
1413 | session->chunk_being_archived, | |
1414 | &chunk_id); | |
a0377dfe | 1415 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
d2956687 JG |
1416 | LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id, |
1417 | chunk_id); | |
1418 | lttng_trace_chunk_put(session->chunk_being_archived); | |
1419 | session->chunk_being_archived = NULL; | |
ccbdaca4 MD |
1420 | /* |
1421 | * Fire the clear reply notifiers if we are completing a clear | |
1422 | * rotation. | |
1423 | */ | |
1424 | session_notify_clear(session); | |
d2956687 | 1425 | } |
2961f09e JG |
1426 | return ret; |
1427 | } | |
e1bbf989 JR |
1428 | |
1429 | /* | |
1430 | * Sample the id of a session looked up via its name. | |
1431 | * Here the term "sampling" hint the caller that this return the id at a given | |
1432 | * point in time with no guarantee that the session for which the id was | |
1433 | * sampled still exist at that point. | |
1434 | * | |
1435 | * Return 0 when the session is not found, | |
1436 | * Return 1 when the session is found and set `id`. | |
1437 | */ | |
1438 | bool sample_session_id_by_name(const char *name, uint64_t *id) | |
1439 | { | |
1440 | bool found = false; | |
1441 | struct lttng_ht_node_str *node; | |
1442 | struct lttng_ht_iter iter; | |
1443 | struct ltt_session *ls; | |
1444 | ||
1445 | rcu_read_lock(); | |
1446 | ||
1447 | if (!ltt_sessions_ht_by_name) { | |
1448 | found = false; | |
1449 | goto end; | |
1450 | } | |
1451 | ||
1452 | lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter); | |
1453 | node = lttng_ht_iter_get_node_str(&iter); | |
1454 | if (node == NULL) { | |
1455 | found = false; | |
1456 | goto end; | |
1457 | } | |
1458 | ||
1459 | ls = caa_container_of(node, struct ltt_session, node_by_name); | |
1460 | *id = ls->id; | |
1461 | found = true; | |
1462 | ||
1463 | DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name); | |
1464 | end: | |
1465 | rcu_read_unlock(); | |
1466 | return found; | |
1467 | } | |
e99e3664 JG |
1468 | |
1469 | void ls::details::locked_session_release(ltt_session *session) | |
1470 | { | |
1471 | session_unlock(session); | |
1472 | session_put(session); | |
1473 | } | |
1474 | ||
1475 | ltt_session::locked_ptr ls::find_locked_session_by_id(ltt_session::id_t id) | |
1476 | { | |
1477 | lttng::urcu::read_lock_guard rcu_lock; | |
1478 | auto session = session_find_by_id(id); | |
1479 | ||
1480 | if (!session) { | |
1481 | return nullptr; | |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * The pointer falling out of scope will unlock and release the reference to the | |
1486 | * session. | |
1487 | */ | |
1488 | session_lock(session); | |
1489 | return ltt_session::locked_ptr(session); | |
1490 | } | |
1491 | ||
1492 | ltt_session::sptr ls::find_session_by_id(ltt_session::id_t id) | |
1493 | { | |
1494 | lttng::urcu::read_lock_guard rcu_lock; | |
1495 | auto session = session_find_by_id(id); | |
1496 | ||
1497 | if (!session) { | |
1498 | return nullptr; | |
1499 | } | |
1500 | ||
1501 | return {session, session_put}; | |
1502 | } |