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