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