Commit | Line | Data |
---|---|---|
5b74c7b1 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
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 DG |
58 | static struct ltt_session_list ltt_session_list = { |
59 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
60 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
99d688f2 | 61 | .removal_cond = PTHREAD_COND_INITIALIZER, |
a24f7994 | 62 | .next_uuid = 0, |
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 | ||
83 | assert(name); | |
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 | { |
0525e9ae DG |
114 | assert(ls); |
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 | { | |
0525e9ae DG |
127 | assert(ls); |
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 | ||
355 | assert(ls); | |
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. */ | |
366 | assert(ltt_sessions_ht_by_name); | |
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 | ||
e1bbf989 JR |
392 | assert(ltt_sessions_ht_by_name); |
393 | ||
394 | count = lttng_ht_get_count(ltt_sessions_ht_by_id); | |
395 | 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 | /* | |
e1bbf989 JR |
401 | * Remove a ltt_session from the ltt_sessions_ht_by_id/name. |
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 | ||
410 | assert(ls); | |
411 | assert(ltt_sessions_ht_by_id); | |
e1bbf989 | 412 | 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); | |
416 | assert(!ret); | |
417 | ||
e1bbf989 JR |
418 | iter.iter.node = &ls->node_by_name.node; |
419 | ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter); | |
420 | assert(!ret); | |
421 | ||
23324029 | 422 | if (ltt_sessions_ht_empty()) { |
e1bbf989 | 423 | DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables"); |
23324029 JD |
424 | ltt_sessions_ht_destroy(); |
425 | } | |
426 | } | |
427 | ||
b5541356 | 428 | /* |
6c9cc2ab | 429 | * Acquire session lock |
b5541356 | 430 | */ |
54d01ffb | 431 | void session_lock(struct ltt_session *session) |
b5541356 | 432 | { |
0525e9ae DG |
433 | assert(session); |
434 | ||
6c9cc2ab DG |
435 | pthread_mutex_lock(&session->lock); |
436 | } | |
b5541356 | 437 | |
6c9cc2ab DG |
438 | /* |
439 | * Release session lock | |
440 | */ | |
54d01ffb | 441 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 442 | { |
0525e9ae DG |
443 | assert(session); |
444 | ||
6c9cc2ab | 445 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
446 | } |
447 | ||
82b69413 JG |
448 | static |
449 | int _session_set_trace_chunk_no_lock_check(struct ltt_session *session, | |
d2956687 JG |
450 | struct lttng_trace_chunk *new_trace_chunk, |
451 | struct lttng_trace_chunk **_current_trace_chunk) | |
82b69413 | 452 | { |
78fc586b | 453 | int ret = 0; |
82b69413 | 454 | unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0; |
82b69413 JG |
455 | struct cds_lfht_iter iter; |
456 | struct consumer_socket *socket; | |
d2956687 JG |
457 | struct lttng_trace_chunk *current_trace_chunk; |
458 | uint64_t chunk_id; | |
459 | enum lttng_trace_chunk_status chunk_status; | |
82b69413 | 460 | |
d2956687 | 461 | rcu_read_lock(); |
82b69413 | 462 | /* |
d2956687 JG |
463 | * Ownership of current trace chunk is transferred to |
464 | * `current_trace_chunk`. | |
82b69413 | 465 | */ |
d2956687 JG |
466 | current_trace_chunk = session->current_trace_chunk; |
467 | session->current_trace_chunk = NULL; | |
82b69413 | 468 | if (session->ust_session) { |
d2956687 JG |
469 | lttng_trace_chunk_put( |
470 | session->ust_session->current_trace_chunk); | |
471 | session->ust_session->current_trace_chunk = NULL; | |
82b69413 JG |
472 | } |
473 | if (session->kernel_session) { | |
d2956687 JG |
474 | lttng_trace_chunk_put( |
475 | session->kernel_session->current_trace_chunk); | |
476 | session->kernel_session->current_trace_chunk = NULL; | |
82b69413 | 477 | } |
d2956687 JG |
478 | if (!new_trace_chunk) { |
479 | ret = 0; | |
480 | goto end; | |
82b69413 | 481 | } |
d2956687 JG |
482 | chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id); |
483 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
82b69413 | 484 | |
d2956687 JG |
485 | refs_to_acquire = 1; |
486 | refs_to_acquire += !!session->ust_session; | |
487 | refs_to_acquire += !!session->kernel_session; | |
488 | ||
489 | for (refs_acquired = 0; refs_acquired < refs_to_acquire; | |
490 | refs_acquired++) { | |
491 | if (!lttng_trace_chunk_get(new_trace_chunk)) { | |
492 | ERR("Failed to acquire reference to new trace chunk of session \"%s\"", | |
493 | session->name); | |
494 | goto error; | |
82b69413 JG |
495 | } |
496 | } | |
497 | ||
d2956687 | 498 | if (session->ust_session) { |
e5add6d0 JG |
499 | const uint64_t relayd_id = |
500 | session->ust_session->consumer->net_seq_index; | |
501 | const bool is_local_trace = | |
502 | session->ust_session->consumer->type == | |
503 | CONSUMER_DST_LOCAL; | |
504 | ||
d2956687 | 505 | session->ust_session->current_trace_chunk = new_trace_chunk; |
f8a37411 | 506 | if (is_local_trace) { |
d2956687 | 507 | enum lttng_error_code ret_error_code; |
82b69413 | 508 | |
d2956687 JG |
509 | ret_error_code = ust_app_create_channel_subdirectories( |
510 | session->ust_session); | |
511 | if (ret_error_code != LTTNG_OK) { | |
82b69413 JG |
512 | goto error; |
513 | } | |
f8a37411 | 514 | } |
d2956687 JG |
515 | cds_lfht_for_each_entry( |
516 | session->ust_session->consumer->socks->ht, | |
517 | &iter, socket, node.node) { | |
518 | pthread_mutex_lock(socket->lock); | |
519 | ret = consumer_create_trace_chunk(socket, | |
520 | relayd_id, | |
5da88b0f MD |
521 | session->id, new_trace_chunk, |
522 | DEFAULT_UST_TRACE_DIR); | |
d2956687 | 523 | pthread_mutex_unlock(socket->lock); |
f8a37411 | 524 | if (ret) { |
d2956687 | 525 | goto error; |
f8a37411 JG |
526 | } |
527 | } | |
528 | } | |
82b69413 | 529 | if (session->kernel_session) { |
e5add6d0 JG |
530 | const uint64_t relayd_id = |
531 | session->kernel_session->consumer->net_seq_index; | |
532 | const bool is_local_trace = | |
533 | session->kernel_session->consumer->type == | |
534 | CONSUMER_DST_LOCAL; | |
535 | ||
d2956687 JG |
536 | session->kernel_session->current_trace_chunk = new_trace_chunk; |
537 | if (is_local_trace) { | |
538 | enum lttng_error_code ret_error_code; | |
539 | ||
540 | ret_error_code = kernel_create_channel_subdirectories( | |
541 | session->kernel_session); | |
542 | if (ret_error_code != LTTNG_OK) { | |
d2956687 JG |
543 | goto error; |
544 | } | |
f8a37411 | 545 | } |
d2956687 JG |
546 | cds_lfht_for_each_entry( |
547 | session->kernel_session->consumer->socks->ht, | |
548 | &iter, socket, node.node) { | |
549 | pthread_mutex_lock(socket->lock); | |
550 | ret = consumer_create_trace_chunk(socket, | |
551 | relayd_id, | |
5da88b0f MD |
552 | session->id, new_trace_chunk, |
553 | DEFAULT_KERNEL_TRACE_DIR); | |
d2956687 | 554 | pthread_mutex_unlock(socket->lock); |
f8a37411 | 555 | if (ret) { |
d2956687 | 556 | goto error; |
f8a37411 JG |
557 | } |
558 | } | |
559 | } | |
82b69413 JG |
560 | |
561 | /* | |
562 | * Update local current trace chunk state last, only if all remote | |
d2956687 | 563 | * creations succeeded. |
82b69413 JG |
564 | */ |
565 | session->current_trace_chunk = new_trace_chunk; | |
d2956687 JG |
566 | LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id); |
567 | end: | |
568 | if (_current_trace_chunk) { | |
569 | *_current_trace_chunk = current_trace_chunk; | |
570 | current_trace_chunk = NULL; | |
571 | } | |
572 | end_no_move: | |
573 | rcu_read_unlock(); | |
574 | lttng_trace_chunk_put(current_trace_chunk); | |
575 | return ret; | |
576 | error: | |
82b69413 | 577 | if (session->ust_session) { |
d2956687 | 578 | session->ust_session->current_trace_chunk = NULL; |
82b69413 JG |
579 | } |
580 | if (session->kernel_session) { | |
d2956687 | 581 | session->kernel_session->current_trace_chunk = NULL; |
82b69413 | 582 | } |
f8a37411 | 583 | /* |
82b69413 JG |
584 | * Release references taken in the case where all references could not |
585 | * be acquired. | |
586 | */ | |
587 | refs_to_release = refs_to_acquire - refs_acquired; | |
588 | for (i = 0; i < refs_to_release; i++) { | |
589 | lttng_trace_chunk_put(new_trace_chunk); | |
590 | } | |
d2956687 JG |
591 | ret = -1; |
592 | goto end_no_move; | |
82b69413 JG |
593 | } |
594 | ||
d2956687 | 595 | struct lttng_trace_chunk *session_create_new_trace_chunk( |
348a81dc JG |
596 | const struct ltt_session *session, |
597 | const struct consumer_output *consumer_output_override, | |
82b69413 JG |
598 | const char *session_base_path_override, |
599 | const char *chunk_name_override) | |
600 | { | |
601 | int ret; | |
82b69413 JG |
602 | struct lttng_trace_chunk *trace_chunk = NULL; |
603 | enum lttng_trace_chunk_status chunk_status; | |
d2956687 | 604 | const time_t chunk_creation_ts = time(NULL); |
348a81dc JG |
605 | bool is_local_trace; |
606 | const char *base_path; | |
cbf53d23 | 607 | struct lttng_directory_handle *session_output_directory = NULL; |
82b69413 | 608 | const struct lttng_credentials session_credentials = { |
ff588497 JR |
609 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid), |
610 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid), | |
82b69413 JG |
611 | }; |
612 | uint64_t next_chunk_id; | |
348a81dc | 613 | const struct consumer_output *output; |
a7ceb342 | 614 | const char *new_path; |
348a81dc JG |
615 | |
616 | if (consumer_output_override) { | |
617 | output = consumer_output_override; | |
618 | } else { | |
619 | assert(session->ust_session || session->kernel_session); | |
620 | output = session->ust_session ? | |
621 | session->ust_session->consumer : | |
622 | session->kernel_session->consumer; | |
623 | } | |
624 | ||
625 | is_local_trace = output->type == CONSUMER_DST_LOCAL; | |
626 | base_path = session_base_path_override ? : | |
627 | consumer_output_get_base_path(output); | |
82b69413 | 628 | |
d2956687 JG |
629 | if (chunk_creation_ts == (time_t) -1) { |
630 | PERROR("Failed to sample time while creation session \"%s\" trace chunk", | |
82b69413 | 631 | session->name); |
82b69413 JG |
632 | goto error; |
633 | } | |
82b69413 | 634 | |
d2956687 JG |
635 | next_chunk_id = session->most_recent_chunk_id.is_set ? |
636 | session->most_recent_chunk_id.value + 1 : 0; | |
82b69413 | 637 | |
a7ceb342 MD |
638 | if (session->current_trace_chunk && |
639 | !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) { | |
640 | chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk, | |
641 | DEFAULT_CHUNK_TMP_OLD_DIRECTORY); | |
642 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
643 | goto error; | |
644 | } | |
645 | } | |
646 | if (!session->current_trace_chunk) { | |
647 | if (!session->rotated) { | |
648 | new_path = ""; | |
649 | } else { | |
650 | new_path = NULL; | |
651 | } | |
652 | } else { | |
653 | new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY; | |
654 | } | |
655 | ||
d2956687 | 656 | trace_chunk = lttng_trace_chunk_create(next_chunk_id, |
a7ceb342 | 657 | chunk_creation_ts, new_path); |
82b69413 | 658 | if (!trace_chunk) { |
82b69413 JG |
659 | goto error; |
660 | } | |
661 | ||
662 | if (chunk_name_override) { | |
663 | chunk_status = lttng_trace_chunk_override_name(trace_chunk, | |
664 | chunk_name_override); | |
d2956687 | 665 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
82b69413 JG |
666 | goto error; |
667 | } | |
668 | } | |
669 | ||
670 | if (!is_local_trace) { | |
671 | /* | |
672 | * No need to set crendentials and output directory | |
673 | * for remote trace chunks. | |
674 | */ | |
d2956687 | 675 | goto end; |
82b69413 JG |
676 | } |
677 | ||
678 | chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, | |
679 | &session_credentials); | |
680 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
82b69413 JG |
681 | goto error; |
682 | } | |
683 | ||
d2956687 JG |
684 | DBG("Creating base output directory of session \"%s\" at %s", |
685 | session->name, base_path); | |
82b69413 JG |
686 | ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, |
687 | session->uid, session->gid); | |
688 | if (ret) { | |
82b69413 JG |
689 | goto error; |
690 | } | |
cbf53d23 JG |
691 | session_output_directory = lttng_directory_handle_create(base_path); |
692 | if (!session_output_directory) { | |
82b69413 JG |
693 | goto error; |
694 | } | |
695 | chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, | |
cbf53d23 JG |
696 | session_output_directory); |
697 | lttng_directory_handle_put(session_output_directory); | |
698 | session_output_directory = NULL; | |
82b69413 | 699 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
82b69413 JG |
700 | goto error; |
701 | } | |
d2956687 JG |
702 | end: |
703 | return trace_chunk; | |
82b69413 | 704 | error: |
cbf53d23 | 705 | lttng_directory_handle_put(session_output_directory); |
82b69413 | 706 | lttng_trace_chunk_put(trace_chunk); |
d2956687 JG |
707 | trace_chunk = NULL; |
708 | goto end; | |
709 | } | |
710 | ||
343defc2 | 711 | int session_close_trace_chunk(struct ltt_session *session, |
bbc4768c | 712 | struct lttng_trace_chunk *trace_chunk, |
343defc2 | 713 | enum lttng_trace_chunk_command_type close_command, |
ecd1a12f | 714 | char *closed_trace_chunk_path) |
d2956687 JG |
715 | { |
716 | int ret = 0; | |
717 | bool error_occurred = false; | |
718 | struct cds_lfht_iter iter; | |
719 | struct consumer_socket *socket; | |
720 | enum lttng_trace_chunk_status chunk_status; | |
721 | const time_t chunk_close_timestamp = time(NULL); | |
a7ceb342 | 722 | const char *new_path; |
d2956687 | 723 | |
343defc2 MD |
724 | chunk_status = lttng_trace_chunk_set_close_command( |
725 | trace_chunk, close_command); | |
726 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
727 | ret = -1; | |
728 | goto end; | |
bbc4768c JG |
729 | } |
730 | ||
d2956687 JG |
731 | if (chunk_close_timestamp == (time_t) -1) { |
732 | ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"", | |
733 | session->name); | |
734 | ret = -1; | |
735 | goto end; | |
736 | } | |
a7ceb342 MD |
737 | |
738 | if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && !session->rotated) { | |
739 | /* New chunk stays in session output directory. */ | |
740 | new_path = ""; | |
741 | } else { | |
742 | /* Use chunk name for new chunk. */ | |
743 | new_path = NULL; | |
744 | } | |
745 | if (session->current_trace_chunk && | |
746 | !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) { | |
747 | /* Rename new chunk path. */ | |
748 | chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk, | |
749 | new_path); | |
750 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
751 | ret = -1; | |
752 | goto end; | |
753 | } | |
754 | } | |
755 | if (!lttng_trace_chunk_get_name_overridden(trace_chunk) && | |
756 | close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) { | |
757 | const char *old_path; | |
758 | ||
759 | if (!session->rotated) { | |
760 | old_path = ""; | |
761 | } else { | |
762 | old_path = NULL; | |
763 | } | |
764 | /* We need to move back the .tmp_old_chunk to its rightful place. */ | |
765 | chunk_status = lttng_trace_chunk_rename_path(trace_chunk, | |
766 | old_path); | |
767 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
768 | ret = -1; | |
769 | goto end; | |
770 | } | |
771 | } | |
772 | if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) { | |
773 | session->rotated = true; | |
774 | } | |
d2956687 JG |
775 | chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, |
776 | chunk_close_timestamp); | |
777 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
778 | ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"", | |
779 | session->name); | |
780 | ret = -1; | |
781 | goto end; | |
782 | } | |
783 | ||
784 | if (session->ust_session) { | |
0a184d4e JG |
785 | const uint64_t relayd_id = |
786 | session->ust_session->consumer->net_seq_index; | |
787 | ||
d2956687 JG |
788 | cds_lfht_for_each_entry( |
789 | session->ust_session->consumer->socks->ht, | |
790 | &iter, socket, node.node) { | |
791 | pthread_mutex_lock(socket->lock); | |
792 | ret = consumer_close_trace_chunk(socket, | |
0a184d4e | 793 | relayd_id, |
d2956687 | 794 | session->id, |
ecd1a12f | 795 | trace_chunk, closed_trace_chunk_path); |
d2956687 JG |
796 | pthread_mutex_unlock(socket->lock); |
797 | if (ret) { | |
798 | ERR("Failed to close trace chunk on user space consumer"); | |
799 | error_occurred = true; | |
800 | } | |
801 | } | |
802 | } | |
803 | if (session->kernel_session) { | |
0a184d4e JG |
804 | const uint64_t relayd_id = |
805 | session->kernel_session->consumer->net_seq_index; | |
806 | ||
d2956687 JG |
807 | cds_lfht_for_each_entry( |
808 | session->kernel_session->consumer->socks->ht, | |
809 | &iter, socket, node.node) { | |
810 | pthread_mutex_lock(socket->lock); | |
811 | ret = consumer_close_trace_chunk(socket, | |
0a184d4e | 812 | relayd_id, |
d2956687 | 813 | session->id, |
ecd1a12f | 814 | trace_chunk, closed_trace_chunk_path); |
d2956687 JG |
815 | pthread_mutex_unlock(socket->lock); |
816 | if (ret) { | |
817 | ERR("Failed to close trace chunk on kernel consumer"); | |
818 | error_occurred = true; | |
819 | } | |
820 | } | |
821 | } | |
822 | ret = error_occurred ? -1 : 0; | |
82b69413 | 823 | end: |
d2956687 | 824 | return ret; |
82b69413 JG |
825 | } |
826 | ||
04ed9e10 JG |
827 | /* |
828 | * This function skips the metadata channel as the begin/end timestamps of a | |
829 | * metadata packet are useless. | |
830 | * | |
831 | * Moreover, opening a packet after a "clear" will cause problems for live | |
832 | * sessions as it will introduce padding that was not part of the first trace | |
833 | * chunk. The relay daemon expects the content of the metadata stream of | |
834 | * successive metadata trace chunks to be strict supersets of one another. | |
835 | * | |
836 | * For example, flushing a packet at the beginning of the metadata stream of | |
837 | * a trace chunk resulting from a "clear" session command will cause the | |
838 | * size of the metadata stream of the new trace chunk to not match the size of | |
839 | * the metadata stream of the original chunk. This will confuse the relay | |
840 | * daemon as the same "offset" in a metadata stream will no longer point | |
841 | * to the same content. | |
842 | */ | |
843 | static | |
844 | enum lttng_error_code session_kernel_open_packets(struct ltt_session *session) | |
845 | { | |
846 | enum lttng_error_code ret = LTTNG_OK; | |
847 | struct consumer_socket *socket; | |
848 | struct lttng_ht_iter iter; | |
849 | struct cds_lfht_node *node; | |
850 | struct ltt_kernel_channel *chan; | |
851 | ||
852 | rcu_read_lock(); | |
853 | ||
854 | cds_lfht_first(session->kernel_session->consumer->socks->ht, &iter.iter); | |
855 | node = cds_lfht_iter_get_node(&iter.iter); | |
856 | socket = container_of(node, typeof(*socket), node.node); | |
857 | ||
858 | cds_list_for_each_entry(chan, | |
859 | &session->kernel_session->channel_list.head, list) { | |
860 | int open_ret; | |
861 | ||
862 | DBG("Open packet of kernel channel: channel key = %" PRIu64 | |
863 | ", session name = %s, session_id = %" PRIu64, | |
864 | chan->key, session->name, session->id); | |
865 | ||
866 | open_ret = consumer_open_channel_packets(socket, chan->key); | |
867 | if (open_ret < 0) { | |
868 | /* General error (no known error expected). */ | |
869 | ret = LTTNG_ERR_UNK; | |
870 | goto end; | |
871 | } | |
872 | } | |
873 | ||
874 | end: | |
875 | rcu_read_unlock(); | |
876 | return ret; | |
877 | } | |
878 | ||
879 | enum lttng_error_code session_open_packets(struct ltt_session *session) | |
880 | { | |
881 | enum lttng_error_code ret = LTTNG_OK; | |
882 | ||
883 | DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64, | |
884 | session->name, session->id); | |
885 | ||
886 | if (session->ust_session) { | |
887 | ret = ust_app_open_packets(session); | |
888 | if (ret != LTTNG_OK) { | |
889 | goto end; | |
890 | } | |
891 | } | |
892 | ||
893 | if (session->kernel_session) { | |
894 | ret = session_kernel_open_packets(session); | |
895 | if (ret != LTTNG_OK) { | |
896 | goto end; | |
897 | } | |
898 | } | |
899 | ||
900 | end: | |
901 | return ret; | |
902 | } | |
903 | ||
82b69413 JG |
904 | /* |
905 | * Set a session's current trace chunk. | |
906 | * | |
907 | * Must be called with the session lock held. | |
908 | */ | |
909 | int session_set_trace_chunk(struct ltt_session *session, | |
d2956687 JG |
910 | struct lttng_trace_chunk *new_trace_chunk, |
911 | struct lttng_trace_chunk **current_trace_chunk) | |
82b69413 JG |
912 | { |
913 | ASSERT_LOCKED(session->lock); | |
d2956687 JG |
914 | return _session_set_trace_chunk_no_lock_check(session, new_trace_chunk, |
915 | current_trace_chunk); | |
82b69413 JG |
916 | } |
917 | ||
3e3665b8 JG |
918 | static |
919 | void session_notify_destruction(const struct ltt_session *session) | |
920 | { | |
921 | size_t i; | |
922 | const size_t count = lttng_dynamic_array_get_count( | |
923 | &session->destroy_notifiers); | |
924 | ||
925 | for (i = 0; i < count; i++) { | |
926 | const struct ltt_session_destroy_notifier_element *element = | |
927 | lttng_dynamic_array_get_element( | |
928 | &session->destroy_notifiers, i); | |
929 | ||
930 | element->notifier(session, element->user_data); | |
931 | } | |
932 | } | |
933 | ||
ccbdaca4 MD |
934 | /* |
935 | * Fire each clear notifier once, and remove them from the array. | |
936 | */ | |
937 | void session_notify_clear(struct ltt_session *session) | |
938 | { | |
939 | size_t i; | |
940 | const size_t count = lttng_dynamic_array_get_count( | |
941 | &session->clear_notifiers); | |
942 | ||
943 | for (i = 0; i < count; i++) { | |
944 | const struct ltt_session_clear_notifier_element *element = | |
945 | lttng_dynamic_array_get_element( | |
946 | &session->clear_notifiers, i); | |
947 | ||
948 | element->notifier(session, element->user_data); | |
949 | } | |
950 | lttng_dynamic_array_clear(&session->clear_notifiers); | |
951 | } | |
952 | ||
e32d7f27 JG |
953 | static |
954 | void session_release(struct urcu_ref *ref) | |
955 | { | |
956 | int ret; | |
957 | struct ltt_ust_session *usess; | |
958 | struct ltt_kernel_session *ksess; | |
959 | struct ltt_session *session = container_of(ref, typeof(*session), ref); | |
7fdbed1c | 960 | const bool session_published = session->published; |
e32d7f27 | 961 | |
d2956687 JG |
962 | assert(!session->chunk_being_archived); |
963 | ||
e32d7f27 JG |
964 | usess = session->ust_session; |
965 | ksess = session->kernel_session; | |
3e3665b8 | 966 | |
f8a37411 | 967 | /* Clean kernel session teardown, keeping data for destroy notifier. */ |
e32d7f27 JG |
968 | kernel_destroy_session(ksess); |
969 | ||
d070c424 | 970 | /* UST session teardown, keeping data for destroy notifier. */ |
e32d7f27 JG |
971 | if (usess) { |
972 | /* Close any relayd session */ | |
973 | consumer_output_send_destroy_relayd(usess->consumer); | |
974 | ||
975 | /* Destroy every UST application related to this session. */ | |
976 | ret = ust_app_destroy_trace_all(usess); | |
977 | if (ret) { | |
978 | ERR("Error in ust_app_destroy_trace_all"); | |
979 | } | |
980 | ||
d070c424 | 981 | /* Clean up the rest, keeping destroy notifier data. */ |
e32d7f27 JG |
982 | trace_ust_destroy_session(usess); |
983 | } | |
984 | ||
985 | /* | |
986 | * Must notify the kernel thread here to update it's poll set in order to | |
987 | * remove the channel(s)' fd just destroyed. | |
988 | */ | |
412d7227 | 989 | ret = notify_thread_pipe(the_kernel_poll_pipe[1]); |
e32d7f27 JG |
990 | if (ret < 0) { |
991 | PERROR("write kernel poll pipe"); | |
992 | } | |
993 | ||
994 | DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id); | |
e32d7f27 | 995 | |
e32d7f27 | 996 | snapshot_destroy(&session->snapshot); |
99d688f2 | 997 | |
82b69413 JG |
998 | pthread_mutex_destroy(&session->lock); |
999 | ||
7fdbed1c | 1000 | if (session_published) { |
f4cc5e83 JG |
1001 | ASSERT_LOCKED(ltt_session_list.lock); |
1002 | del_session_list(session); | |
1003 | del_session_ht(session); | |
f4cc5e83 | 1004 | } |
7fdbed1c | 1005 | session_notify_destruction(session); |
d070c424 | 1006 | |
1ac9cb73 | 1007 | consumer_output_put(session->consumer); |
d070c424 MD |
1008 | kernel_free_session(ksess); |
1009 | session->kernel_session = NULL; | |
1010 | if (usess) { | |
1011 | trace_ust_free_session(usess); | |
1012 | session->ust_session = NULL; | |
1013 | } | |
7fdbed1c | 1014 | lttng_dynamic_array_reset(&session->destroy_notifiers); |
ccbdaca4 | 1015 | lttng_dynamic_array_reset(&session->clear_notifiers); |
d2956687 | 1016 | free(session->last_archived_chunk_name); |
6fa5fe7c | 1017 | free(session->base_path); |
e32d7f27 | 1018 | free(session); |
7fdbed1c JG |
1019 | if (session_published) { |
1020 | /* | |
1021 | * Broadcast after free-ing to ensure the memory is | |
1022 | * reclaimed before the main thread exits. | |
1023 | */ | |
8db3acaf | 1024 | ASSERT_LOCKED(ltt_session_list.lock); |
7fdbed1c JG |
1025 | pthread_cond_broadcast(<t_session_list.removal_cond); |
1026 | } | |
e32d7f27 JG |
1027 | } |
1028 | ||
1029 | /* | |
1030 | * Acquire a reference to a session. | |
1031 | * This function may fail (return false); its return value must be checked. | |
1032 | */ | |
1033 | bool session_get(struct ltt_session *session) | |
1034 | { | |
1035 | return urcu_ref_get_unless_zero(&session->ref); | |
1036 | } | |
1037 | ||
1038 | /* | |
1039 | * Release a reference to a session. | |
1040 | */ | |
1041 | void session_put(struct ltt_session *session) | |
1042 | { | |
b178f53e JG |
1043 | if (!session) { |
1044 | return; | |
1045 | } | |
e32d7f27 JG |
1046 | /* |
1047 | * The session list lock must be held as any session_put() | |
1048 | * may cause the removal of the session from the session_list. | |
1049 | */ | |
1050 | ASSERT_LOCKED(ltt_session_list.lock); | |
1051 | assert(session->ref.refcount); | |
1052 | urcu_ref_put(&session->ref, session_release); | |
1053 | } | |
1054 | ||
1055 | /* | |
1056 | * Destroy a session. | |
1057 | * | |
1058 | * This method does not immediately release/free the session as other | |
1059 | * components may still hold a reference to the session. However, | |
1060 | * the session should no longer be presented to the user. | |
1061 | * | |
1062 | * Releases the session list's reference to the session | |
1063 | * and marks it as destroyed. Iterations on the session list should be | |
1064 | * mindful of the "destroyed" flag. | |
1065 | */ | |
1066 | void session_destroy(struct ltt_session *session) | |
1067 | { | |
1068 | assert(!session->destroyed); | |
1069 | session->destroyed = true; | |
1070 | session_put(session); | |
1071 | } | |
1072 | ||
3e3665b8 JG |
1073 | int session_add_destroy_notifier(struct ltt_session *session, |
1074 | ltt_session_destroy_notifier notifier, void *user_data) | |
1075 | { | |
1076 | const struct ltt_session_destroy_notifier_element element = { | |
1077 | .notifier = notifier, | |
1078 | .user_data = user_data | |
1079 | }; | |
1080 | ||
1081 | return lttng_dynamic_array_add_element(&session->destroy_notifiers, | |
1082 | &element); | |
1083 | } | |
1084 | ||
ccbdaca4 MD |
1085 | int session_add_clear_notifier(struct ltt_session *session, |
1086 | ltt_session_clear_notifier notifier, void *user_data) | |
1087 | { | |
1088 | const struct ltt_session_clear_notifier_element element = { | |
1089 | .notifier = notifier, | |
1090 | .user_data = user_data | |
1091 | }; | |
1092 | ||
1093 | return lttng_dynamic_array_add_element(&session->clear_notifiers, | |
1094 | &element); | |
1095 | } | |
1096 | ||
5b74c7b1 | 1097 | /* |
74babd95 | 1098 | * Return a ltt_session structure ptr that matches name. If no session found, |
23324029 | 1099 | * NULL is returned. This must be called with the session list lock held using |
74babd95 | 1100 | * session_lock_list and session_unlock_list. |
e32d7f27 | 1101 | * A reference to the session is implicitly acquired by this function. |
5b74c7b1 | 1102 | */ |
58a1a227 | 1103 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 1104 | { |
5b74c7b1 DG |
1105 | struct ltt_session *iter; |
1106 | ||
0525e9ae | 1107 | assert(name); |
e32d7f27 | 1108 | ASSERT_LOCKED(ltt_session_list.lock); |
0525e9ae | 1109 | |
5f822d0a DG |
1110 | DBG2("Trying to find session by name %s", name); |
1111 | ||
5b74c7b1 | 1112 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
e32d7f27 JG |
1113 | if (!strncmp(iter->name, name, NAME_MAX) && |
1114 | !iter->destroyed) { | |
74babd95 | 1115 | goto found; |
5b74c7b1 DG |
1116 | } |
1117 | } | |
1118 | ||
e32d7f27 | 1119 | return NULL; |
74babd95 | 1120 | found: |
e32d7f27 | 1121 | return session_get(iter) ? iter : NULL; |
5b74c7b1 DG |
1122 | } |
1123 | ||
23324029 JD |
1124 | /* |
1125 | * Return an ltt_session that matches the id. If no session is found, | |
1126 | * NULL is returned. This must be called with rcu_read_lock and | |
1127 | * session list lock held (to guarantee the lifetime of the session). | |
1128 | */ | |
1129 | struct ltt_session *session_find_by_id(uint64_t id) | |
1130 | { | |
1131 | struct lttng_ht_node_u64 *node; | |
1132 | struct lttng_ht_iter iter; | |
1133 | struct ltt_session *ls; | |
1134 | ||
e32d7f27 JG |
1135 | ASSERT_LOCKED(ltt_session_list.lock); |
1136 | ||
d68ec974 JG |
1137 | if (!ltt_sessions_ht_by_id) { |
1138 | goto end; | |
1139 | } | |
1140 | ||
23324029 JD |
1141 | lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter); |
1142 | node = lttng_ht_iter_get_node_u64(&iter); | |
1143 | if (node == NULL) { | |
d68ec974 | 1144 | goto end; |
23324029 JD |
1145 | } |
1146 | ls = caa_container_of(node, struct ltt_session, node); | |
1147 | ||
1148 | DBG3("Session %" PRIu64 " found by id.", id); | |
e32d7f27 | 1149 | return session_get(ls) ? ls : NULL; |
23324029 | 1150 | |
d68ec974 | 1151 | end: |
23324029 JD |
1152 | DBG3("Session %" PRIu64 " NOT found by id", id); |
1153 | return NULL; | |
1154 | } | |
1155 | ||
5b74c7b1 | 1156 | /* |
b178f53e JG |
1157 | * Create a new session and add it to the session list. |
1158 | * Session list lock must be held by the caller. | |
5b74c7b1 | 1159 | */ |
b178f53e | 1160 | enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, |
e3876bf0 | 1161 | struct ltt_session **out_session) |
5b74c7b1 | 1162 | { |
f3ed775e | 1163 | int ret; |
b178f53e JG |
1164 | enum lttng_error_code ret_code; |
1165 | struct ltt_session *new_session = NULL; | |
e07ae692 | 1166 | |
b178f53e JG |
1167 | ASSERT_LOCKED(ltt_session_list.lock); |
1168 | if (name) { | |
1169 | struct ltt_session *clashing_session; | |
1170 | ||
1171 | clashing_session = session_find_by_name(name); | |
1172 | if (clashing_session) { | |
1173 | session_put(clashing_session); | |
1174 | ret_code = LTTNG_ERR_EXIST_SESS; | |
1175 | goto error; | |
1176 | } | |
1177 | } | |
ba7f0ae5 | 1178 | new_session = zmalloc(sizeof(struct ltt_session)); |
b178f53e JG |
1179 | if (!new_session) { |
1180 | PERROR("Failed to allocate an ltt_session structure"); | |
1181 | ret_code = LTTNG_ERR_NOMEM; | |
1182 | goto error; | |
5b74c7b1 DG |
1183 | } |
1184 | ||
3e3665b8 | 1185 | lttng_dynamic_array_init(&new_session->destroy_notifiers, |
93bed9fe JG |
1186 | sizeof(struct ltt_session_destroy_notifier_element), |
1187 | NULL); | |
ccbdaca4 MD |
1188 | lttng_dynamic_array_init(&new_session->clear_notifiers, |
1189 | sizeof(struct ltt_session_clear_notifier_element), | |
1190 | NULL); | |
e32d7f27 | 1191 | urcu_ref_init(&new_session->ref); |
b178f53e | 1192 | pthread_mutex_init(&new_session->lock, NULL); |
e32d7f27 | 1193 | |
b178f53e JG |
1194 | new_session->creation_time = time(NULL); |
1195 | if (new_session->creation_time == (time_t) -1) { | |
1196 | PERROR("Failed to sample session creation time"); | |
1197 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
f3ed775e DG |
1198 | goto error; |
1199 | } | |
1200 | ||
b178f53e JG |
1201 | /* Create default consumer output. */ |
1202 | new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
1203 | if (new_session->consumer == NULL) { | |
1204 | ret_code = LTTNG_ERR_NOMEM; | |
1c1c3634 DG |
1205 | goto error; |
1206 | } | |
1207 | ||
b178f53e JG |
1208 | if (name) { |
1209 | ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name)); | |
1210 | if (ret) { | |
1211 | ret_code = LTTNG_ERR_SESSION_INVALID_CHAR; | |
1212 | goto error; | |
1213 | } | |
1214 | ret = validate_name(name); | |
1215 | if (ret < 0) { | |
1216 | ret_code = LTTNG_ERR_SESSION_INVALID_CHAR; | |
1217 | goto error; | |
1218 | } | |
1219 | } else { | |
1220 | int i = 0; | |
1221 | bool found_name = false; | |
1222 | char datetime[16]; | |
1223 | struct tm *timeinfo; | |
1224 | ||
1225 | timeinfo = localtime(&new_session->creation_time); | |
1226 | if (!timeinfo) { | |
1227 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1228 | goto error; | |
1229 | } | |
1230 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
1231 | for (i = 0; i < INT_MAX; i++) { | |
1232 | struct ltt_session *clashing_session; | |
1233 | ||
1234 | if (i == 0) { | |
1235 | ret = snprintf(new_session->name, | |
1236 | sizeof(new_session->name), | |
1237 | "%s-%s", | |
1238 | DEFAULT_SESSION_NAME, | |
1239 | datetime); | |
1240 | } else { | |
1241 | ret = snprintf(new_session->name, | |
1242 | sizeof(new_session->name), | |
1243 | "%s%d-%s", | |
1244 | DEFAULT_SESSION_NAME, i, | |
1245 | datetime); | |
1246 | } | |
46ef2188 | 1247 | new_session->name_contains_creation_time = true; |
b178f53e JG |
1248 | if (ret == -1 || ret >= sizeof(new_session->name)) { |
1249 | /* | |
1250 | * Null-terminate in case the name is used | |
1251 | * in logging statements. | |
1252 | */ | |
1253 | new_session->name[sizeof(new_session->name) - 1] = '\0'; | |
1254 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1255 | goto error; | |
1256 | } | |
1257 | ||
1258 | clashing_session = | |
1259 | session_find_by_name(new_session->name); | |
1260 | session_put(clashing_session); | |
1261 | if (!clashing_session) { | |
1262 | found_name = true; | |
1263 | break; | |
1264 | } | |
1265 | } | |
1266 | if (found_name) { | |
1267 | DBG("Generated session name \"%s\"", new_session->name); | |
1268 | new_session->has_auto_generated_name = true; | |
1269 | } else { | |
1270 | ERR("Failed to auto-generate a session name"); | |
1271 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1272 | goto error; | |
1273 | } | |
1274 | } | |
1275 | ||
d3e2ba59 | 1276 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
1277 | if (ret < 0) { |
1278 | if (errno == ENAMETOOLONG) { | |
1279 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
b178f53e JG |
1280 | ERR("Hostname exceeds the maximal permitted length and has been truncated to %s", |
1281 | new_session->hostname); | |
73184835 | 1282 | } else { |
b178f53e | 1283 | ret_code = LTTNG_ERR_SESSION_FAIL; |
73184835 DG |
1284 | goto error; |
1285 | } | |
d3e2ba59 JD |
1286 | } |
1287 | ||
6df2e2c9 MD |
1288 | new_session->uid = uid; |
1289 | new_session->gid = gid; | |
1290 | ||
6dc3064a DG |
1291 | ret = snapshot_init(&new_session->snapshot); |
1292 | if (ret < 0) { | |
b178f53e | 1293 | ret_code = LTTNG_ERR_NOMEM; |
6dc3064a DG |
1294 | goto error; |
1295 | } | |
1296 | ||
4f23c583 | 1297 | new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION; |
92816cc3 | 1298 | |
b178f53e | 1299 | /* Add new session to the session list. */ |
a991f516 | 1300 | new_session->id = add_session_list(new_session); |
b178f53e | 1301 | |
23324029 JD |
1302 | /* |
1303 | * Add the new session to the ltt_sessions_ht_by_id. | |
1304 | * No ownership is taken by the hash table; it is merely | |
1305 | * a wrapper around the session list used for faster access | |
1306 | * by session id. | |
1307 | */ | |
1308 | add_session_ht(new_session); | |
f4cc5e83 | 1309 | new_session->published = true; |
b5541356 | 1310 | |
a4b92340 | 1311 | /* |
b178f53e JG |
1312 | * Consumer is left to NULL since the create_session_uri command will |
1313 | * set it up and, if valid, assign it to the session. | |
a4b92340 | 1314 | */ |
b178f53e JG |
1315 | DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d", |
1316 | new_session->name, new_session->id, new_session->uid, | |
1317 | new_session->gid); | |
1318 | ret_code = LTTNG_OK; | |
1319 | end: | |
1320 | if (new_session) { | |
1321 | (void) session_get(new_session); | |
1322 | *out_session = new_session; | |
1323 | } | |
1324 | return ret_code; | |
5b74c7b1 | 1325 | error: |
f4cc5e83 | 1326 | session_put(new_session); |
b178f53e JG |
1327 | new_session = NULL; |
1328 | goto end; | |
5b74c7b1 | 1329 | } |
2f77fc4b DG |
1330 | |
1331 | /* | |
d7b377ed | 1332 | * Check if the UID matches the session. Root user has access to all |
2f77fc4b DG |
1333 | * sessions. |
1334 | */ | |
d7b377ed | 1335 | bool session_access_ok(struct ltt_session *session, uid_t uid) |
2f77fc4b DG |
1336 | { |
1337 | assert(session); | |
d7b377ed | 1338 | return (uid == session->uid) || uid == 0; |
2f77fc4b | 1339 | } |
2961f09e JG |
1340 | |
1341 | /* | |
1342 | * Set a session's rotation state and reset all associated state. | |
1343 | * | |
1344 | * This function resets the rotation state (check timers, pending | |
1345 | * flags, etc.) and sets the result of the last rotation. The result | |
1346 | * can be queries by a liblttng-ctl client. | |
1347 | * | |
1348 | * Be careful of the result passed to this function. For instance, | |
1349 | * on failure to launch a rotation, a client will expect the rotation | |
83ed9e90 | 1350 | * state to be set to "NO_ROTATION". If an error occurred while the |
2961f09e JG |
1351 | * rotation was "ONGOING", result should be set to "ERROR", which will |
1352 | * allow a client to report it. | |
1353 | * | |
1354 | * Must be called with the session and session_list locks held. | |
1355 | */ | |
1356 | int session_reset_rotation_state(struct ltt_session *session, | |
1357 | enum lttng_rotation_state result) | |
1358 | { | |
1359 | int ret = 0; | |
1360 | ||
1361 | ASSERT_LOCKED(ltt_session_list.lock); | |
1362 | ASSERT_LOCKED(session->lock); | |
1363 | ||
2961f09e JG |
1364 | session->rotation_state = result; |
1365 | if (session->rotation_pending_check_timer_enabled) { | |
1366 | ret = timer_session_rotation_pending_check_stop(session); | |
1367 | } | |
d2956687 JG |
1368 | if (session->chunk_being_archived) { |
1369 | uint64_t chunk_id; | |
1370 | enum lttng_trace_chunk_status chunk_status; | |
1371 | ||
1372 | chunk_status = lttng_trace_chunk_get_id( | |
1373 | session->chunk_being_archived, | |
1374 | &chunk_id); | |
1375 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
1376 | LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id, | |
1377 | chunk_id); | |
1378 | lttng_trace_chunk_put(session->chunk_being_archived); | |
1379 | session->chunk_being_archived = NULL; | |
ccbdaca4 MD |
1380 | /* |
1381 | * Fire the clear reply notifiers if we are completing a clear | |
1382 | * rotation. | |
1383 | */ | |
1384 | session_notify_clear(session); | |
d2956687 | 1385 | } |
2961f09e JG |
1386 | return ret; |
1387 | } | |
e1bbf989 JR |
1388 | |
1389 | /* | |
1390 | * Sample the id of a session looked up via its name. | |
1391 | * Here the term "sampling" hint the caller that this return the id at a given | |
1392 | * point in time with no guarantee that the session for which the id was | |
1393 | * sampled still exist at that point. | |
1394 | * | |
1395 | * Return 0 when the session is not found, | |
1396 | * Return 1 when the session is found and set `id`. | |
1397 | */ | |
1398 | bool sample_session_id_by_name(const char *name, uint64_t *id) | |
1399 | { | |
1400 | bool found = false; | |
1401 | struct lttng_ht_node_str *node; | |
1402 | struct lttng_ht_iter iter; | |
1403 | struct ltt_session *ls; | |
1404 | ||
1405 | rcu_read_lock(); | |
1406 | ||
1407 | if (!ltt_sessions_ht_by_name) { | |
1408 | found = false; | |
1409 | goto end; | |
1410 | } | |
1411 | ||
1412 | lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter); | |
1413 | node = lttng_ht_iter_get_node_str(&iter); | |
1414 | if (node == NULL) { | |
1415 | found = false; | |
1416 | goto end; | |
1417 | } | |
1418 | ||
1419 | ls = caa_container_of(node, struct ltt_session, node_by_name); | |
1420 | *id = ls->id; | |
1421 | found = true; | |
1422 | ||
1423 | DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name); | |
1424 | end: | |
1425 | rcu_read_unlock(); | |
1426 | return found; | |
1427 | } |