Commit | Line | Data |
---|---|---|
5b74c7b1 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
91d76f53 | 7 | * |
5b74c7b1 DG |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
d14d33bf | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5b74c7b1 DG |
11 | * GNU General Public License for more details. |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
5b74c7b1 DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
6c9cc2ab | 19 | #include <limits.h> |
d022620a | 20 | #include <inttypes.h> |
5b74c7b1 DG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
a304c14c | 24 | #include <sys/stat.h> |
f6a9efaa | 25 | #include <urcu.h> |
3d071855 MD |
26 | #include <dirent.h> |
27 | #include <sys/types.h> | |
99d688f2 | 28 | #include <pthread.h> |
5b74c7b1 | 29 | |
990570ed | 30 | #include <common/common.h> |
82b69413 JG |
31 | #include <common/utils.h> |
32 | #include <common/trace-chunk.h> | |
db758600 | 33 | #include <common/sessiond-comm/sessiond-comm.h> |
5d65beab | 34 | #include <lttng/location-internal.h> |
e32d7f27 JG |
35 | #include "lttng-sessiond.h" |
36 | #include "kernel.h" | |
1e307fab | 37 | |
5b74c7b1 | 38 | #include "session.h" |
23324029 | 39 | #include "utils.h" |
dd73d57b | 40 | #include "trace-ust.h" |
a7333da7 | 41 | #include "timer.h" |
5b74c7b1 | 42 | |
3e3665b8 JG |
43 | struct ltt_session_destroy_notifier_element { |
44 | ltt_session_destroy_notifier notifier; | |
45 | void *user_data; | |
46 | }; | |
47 | ||
8c0faa1d | 48 | /* |
b5541356 | 49 | * NOTES: |
8c0faa1d | 50 | * |
b5541356 DG |
51 | * No ltt_session.lock is taken here because those data structure are widely |
52 | * spread across the lttng-tools code base so before caling functions below | |
53 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 54 | * using session_lock() and session_unlock(). |
8c0faa1d | 55 | */ |
8c0faa1d | 56 | |
5b74c7b1 | 57 | /* |
b5541356 | 58 | * Init tracing session list. |
5b74c7b1 | 59 | * |
b5541356 | 60 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 61 | */ |
b5541356 DG |
62 | static struct ltt_session_list ltt_session_list = { |
63 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
64 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
99d688f2 | 65 | .removal_cond = PTHREAD_COND_INITIALIZER, |
a24f7994 | 66 | .next_uuid = 0, |
b5541356 | 67 | }; |
5b74c7b1 | 68 | |
1c1c3634 DG |
69 | /* These characters are forbidden in a session name. Used by validate_name. */ |
70 | static const char *forbidden_name_chars = "/"; | |
71 | ||
23324029 JD |
72 | /* Global hash table to keep the sessions, indexed by id. */ |
73 | static struct lttng_ht *ltt_sessions_ht_by_id = NULL; | |
74 | ||
1c1c3634 DG |
75 | /* |
76 | * Validate the session name for forbidden characters. | |
77 | * | |
78 | * Return 0 on success else -1 meaning a forbidden char. has been found. | |
79 | */ | |
80 | static int validate_name(const char *name) | |
81 | { | |
82 | int ret; | |
83 | char *tok, *tmp_name; | |
84 | ||
85 | assert(name); | |
86 | ||
87 | tmp_name = strdup(name); | |
88 | if (!tmp_name) { | |
89 | /* ENOMEM here. */ | |
90 | ret = -1; | |
91 | goto error; | |
92 | } | |
93 | ||
94 | tok = strpbrk(tmp_name, forbidden_name_chars); | |
95 | if (tok) { | |
96 | DBG("Session name %s contains a forbidden character", name); | |
97 | /* Forbidden character has been found. */ | |
98 | ret = -1; | |
99 | goto error; | |
100 | } | |
101 | ret = 0; | |
102 | ||
103 | error: | |
104 | free(tmp_name); | |
105 | return ret; | |
106 | } | |
107 | ||
5b74c7b1 | 108 | /* |
050349bb | 109 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 110 | * |
050349bb | 111 | * The caller MUST acquire the session list lock before. |
44e96653 | 112 | * Returns the unique identifier for the session. |
5b74c7b1 | 113 | */ |
d022620a | 114 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 115 | { |
0525e9ae DG |
116 | assert(ls); |
117 | ||
5b74c7b1 | 118 | cds_list_add(&ls->list, <t_session_list.head); |
a24f7994 | 119 | return ltt_session_list.next_uuid++; |
5b74c7b1 DG |
120 | } |
121 | ||
122 | /* | |
050349bb | 123 | * Delete a ltt_session structure to the global list. |
b5541356 | 124 | * |
050349bb | 125 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
126 | */ |
127 | static void del_session_list(struct ltt_session *ls) | |
128 | { | |
0525e9ae DG |
129 | assert(ls); |
130 | ||
5b74c7b1 | 131 | cds_list_del(&ls->list); |
5b74c7b1 DG |
132 | } |
133 | ||
b5541356 | 134 | /* |
050349bb | 135 | * Return a pointer to the session list. |
b5541356 | 136 | */ |
54d01ffb | 137 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
138 | { |
139 | return <t_session_list; | |
140 | } | |
141 | ||
99d688f2 JG |
142 | /* |
143 | * Returns once the session list is empty. | |
144 | */ | |
145 | void session_list_wait_empty(void) | |
146 | { | |
147 | pthread_mutex_lock(<t_session_list.lock); | |
148 | while (!cds_list_empty(<t_session_list.head)) { | |
149 | pthread_cond_wait(<t_session_list.removal_cond, | |
150 | <t_session_list.lock); | |
151 | } | |
152 | pthread_mutex_unlock(<t_session_list.lock); | |
153 | } | |
154 | ||
b5541356 | 155 | /* |
6c9cc2ab | 156 | * Acquire session list lock |
b5541356 | 157 | */ |
54d01ffb | 158 | void session_lock_list(void) |
b5541356 | 159 | { |
6c9cc2ab | 160 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
161 | } |
162 | ||
71e0a100 JG |
163 | /* |
164 | * Try to acquire session list lock | |
165 | */ | |
166 | int session_trylock_list(void) | |
167 | { | |
168 | return pthread_mutex_trylock(<t_session_list.lock); | |
169 | } | |
170 | ||
b5541356 | 171 | /* |
6c9cc2ab | 172 | * Release session list lock |
b5541356 | 173 | */ |
54d01ffb | 174 | void session_unlock_list(void) |
b5541356 | 175 | { |
6c9cc2ab | 176 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
177 | } |
178 | ||
dd73d57b JG |
179 | /* |
180 | * Get the session's consumer destination type. | |
181 | * | |
182 | * The caller must hold the session lock. | |
183 | */ | |
184 | enum consumer_dst_type session_get_consumer_destination_type( | |
185 | const struct ltt_session *session) | |
186 | { | |
187 | /* | |
188 | * The output information is duplicated in both of those session types. | |
189 | * Hence, it doesn't matter from which it is retrieved. However, it is | |
190 | * possible for only one of them to be set. | |
191 | */ | |
192 | return session->kernel_session ? | |
193 | session->kernel_session->consumer->type : | |
194 | session->ust_session->consumer->type; | |
195 | } | |
196 | ||
197 | /* | |
198 | * Get the session's consumer network hostname. | |
199 | * The caller must ensure that the destination is of type "net". | |
200 | * | |
201 | * The caller must hold the session lock. | |
202 | */ | |
203 | const char *session_get_net_consumer_hostname(const struct ltt_session *session) | |
204 | { | |
205 | const char *hostname = NULL; | |
206 | const struct consumer_output *output; | |
207 | ||
208 | output = session->kernel_session ? | |
209 | session->kernel_session->consumer : | |
210 | session->ust_session->consumer; | |
211 | ||
212 | /* | |
213 | * hostname is assumed to be the same for both control and data | |
214 | * connections. | |
215 | */ | |
216 | switch (output->dst.net.control.dtype) { | |
217 | case LTTNG_DST_IPV4: | |
218 | hostname = output->dst.net.control.dst.ipv4; | |
219 | break; | |
220 | case LTTNG_DST_IPV6: | |
221 | hostname = output->dst.net.control.dst.ipv6; | |
222 | break; | |
223 | default: | |
224 | abort(); | |
225 | } | |
226 | return hostname; | |
227 | } | |
228 | ||
229 | /* | |
230 | * Get the session's consumer network control and data ports. | |
231 | * The caller must ensure that the destination is of type "net". | |
232 | * | |
233 | * The caller must hold the session lock. | |
234 | */ | |
235 | void session_get_net_consumer_ports(const struct ltt_session *session, | |
236 | uint16_t *control_port, uint16_t *data_port) | |
237 | { | |
238 | const struct consumer_output *output; | |
239 | ||
240 | output = session->kernel_session ? | |
241 | session->kernel_session->consumer : | |
242 | session->ust_session->consumer; | |
243 | *control_port = output->dst.net.control.port; | |
244 | *data_port = output->dst.net.data.port; | |
245 | } | |
246 | ||
5d65beab JG |
247 | /* |
248 | * Get the location of the latest trace archive produced by a rotation. | |
249 | * | |
250 | * The caller must hold the session lock. | |
251 | */ | |
252 | struct lttng_trace_archive_location *session_get_trace_archive_location( | |
3e3665b8 | 253 | const struct ltt_session *session) |
5d65beab | 254 | { |
d2956687 | 255 | int ret; |
5d65beab | 256 | struct lttng_trace_archive_location *location = NULL; |
d2956687 JG |
257 | char *chunk_path = NULL; |
258 | ||
259 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED || | |
260 | !session->last_archived_chunk_name) { | |
261 | goto end; | |
262 | } | |
5d65beab | 263 | |
d2956687 JG |
264 | ret = asprintf(&chunk_path, "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", |
265 | session_get_base_path(session), | |
266 | session->last_archived_chunk_name); | |
267 | if (ret == -1) { | |
5d65beab JG |
268 | goto end; |
269 | } | |
270 | ||
271 | switch (session_get_consumer_destination_type(session)) { | |
272 | case CONSUMER_DST_LOCAL: | |
273 | location = lttng_trace_archive_location_local_create( | |
d2956687 | 274 | chunk_path); |
5d65beab JG |
275 | break; |
276 | case CONSUMER_DST_NET: | |
277 | { | |
278 | const char *hostname; | |
279 | uint16_t control_port, data_port; | |
280 | ||
281 | hostname = session_get_net_consumer_hostname(session); | |
282 | session_get_net_consumer_ports(session, | |
283 | &control_port, | |
284 | &data_port); | |
285 | location = lttng_trace_archive_location_relay_create( | |
286 | hostname, | |
287 | LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP, | |
d2956687 | 288 | control_port, data_port, chunk_path); |
5d65beab JG |
289 | break; |
290 | } | |
291 | default: | |
292 | abort(); | |
293 | } | |
294 | end: | |
d2956687 | 295 | free(chunk_path); |
5d65beab JG |
296 | return location; |
297 | } | |
298 | ||
23324029 JD |
299 | /* |
300 | * Allocate the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
301 | * |
302 | * The session list lock must be held. | |
23324029 JD |
303 | */ |
304 | int ltt_sessions_ht_alloc(void) | |
305 | { | |
306 | int ret = 0; | |
307 | ||
308 | DBG("Allocating ltt_sessions_ht_by_id"); | |
309 | ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
310 | if (!ltt_sessions_ht_by_id) { | |
311 | ret = -1; | |
312 | ERR("Failed to allocate ltt_sessions_ht_by_id"); | |
313 | goto end; | |
314 | } | |
315 | end: | |
316 | return ret; | |
317 | } | |
318 | ||
319 | /* | |
320 | * Destroy the ltt_sessions_ht_by_id HT. | |
9c6518bc JG |
321 | * |
322 | * The session list lock must be held. | |
23324029 | 323 | */ |
accdc9bf | 324 | static void ltt_sessions_ht_destroy(void) |
23324029 JD |
325 | { |
326 | if (!ltt_sessions_ht_by_id) { | |
327 | return; | |
328 | } | |
329 | ht_cleanup_push(ltt_sessions_ht_by_id); | |
330 | ltt_sessions_ht_by_id = NULL; | |
331 | } | |
332 | ||
333 | /* | |
334 | * Add a ltt_session to the ltt_sessions_ht_by_id. | |
335 | * If unallocated, the ltt_sessions_ht_by_id HT is allocated. | |
336 | * The session list lock must be held. | |
337 | */ | |
338 | static void add_session_ht(struct ltt_session *ls) | |
339 | { | |
340 | int ret; | |
341 | ||
342 | assert(ls); | |
343 | ||
344 | if (!ltt_sessions_ht_by_id) { | |
345 | ret = ltt_sessions_ht_alloc(); | |
346 | if (ret) { | |
347 | ERR("Error allocating the sessions HT"); | |
348 | goto end; | |
349 | } | |
350 | } | |
351 | lttng_ht_node_init_u64(&ls->node, ls->id); | |
352 | lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node); | |
353 | ||
354 | end: | |
355 | return; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Test if ltt_sessions_ht_by_id is empty. | |
360 | * Return 1 if empty, 0 if not empty. | |
361 | * The session list lock must be held. | |
362 | */ | |
def88971 | 363 | static int ltt_sessions_ht_empty(void) |
23324029 JD |
364 | { |
365 | int ret; | |
366 | ||
367 | if (!ltt_sessions_ht_by_id) { | |
368 | ret = 1; | |
369 | goto end; | |
370 | } | |
371 | ||
372 | ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1; | |
373 | end: | |
374 | return ret; | |
375 | } | |
376 | ||
377 | /* | |
378 | * Remove a ltt_session from the ltt_sessions_ht_by_id. | |
379 | * If empty, the ltt_sessions_ht_by_id HT is freed. | |
380 | * The session list lock must be held. | |
381 | */ | |
382 | static void del_session_ht(struct ltt_session *ls) | |
383 | { | |
384 | struct lttng_ht_iter iter; | |
385 | int ret; | |
386 | ||
387 | assert(ls); | |
388 | assert(ltt_sessions_ht_by_id); | |
389 | ||
390 | iter.iter.node = &ls->node.node; | |
391 | ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter); | |
392 | assert(!ret); | |
393 | ||
394 | if (ltt_sessions_ht_empty()) { | |
395 | DBG("Empty ltt_sessions_ht_by_id, destroying it"); | |
396 | ltt_sessions_ht_destroy(); | |
397 | } | |
398 | } | |
399 | ||
b5541356 | 400 | /* |
6c9cc2ab | 401 | * Acquire session lock |
b5541356 | 402 | */ |
54d01ffb | 403 | void session_lock(struct ltt_session *session) |
b5541356 | 404 | { |
0525e9ae DG |
405 | assert(session); |
406 | ||
6c9cc2ab DG |
407 | pthread_mutex_lock(&session->lock); |
408 | } | |
b5541356 | 409 | |
6c9cc2ab DG |
410 | /* |
411 | * Release session lock | |
412 | */ | |
54d01ffb | 413 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 414 | { |
0525e9ae DG |
415 | assert(session); |
416 | ||
6c9cc2ab | 417 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
418 | } |
419 | ||
82b69413 JG |
420 | static |
421 | int _session_set_trace_chunk_no_lock_check(struct ltt_session *session, | |
d2956687 JG |
422 | struct lttng_trace_chunk *new_trace_chunk, |
423 | struct lttng_trace_chunk **_current_trace_chunk) | |
82b69413 JG |
424 | { |
425 | int ret; | |
426 | unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0; | |
82b69413 JG |
427 | struct cds_lfht_iter iter; |
428 | struct consumer_socket *socket; | |
d2956687 JG |
429 | struct lttng_trace_chunk *current_trace_chunk; |
430 | uint64_t chunk_id; | |
431 | enum lttng_trace_chunk_status chunk_status; | |
82b69413 | 432 | |
d2956687 | 433 | rcu_read_lock(); |
82b69413 | 434 | /* |
d2956687 JG |
435 | * Ownership of current trace chunk is transferred to |
436 | * `current_trace_chunk`. | |
82b69413 | 437 | */ |
d2956687 JG |
438 | current_trace_chunk = session->current_trace_chunk; |
439 | session->current_trace_chunk = NULL; | |
82b69413 | 440 | if (session->ust_session) { |
d2956687 JG |
441 | lttng_trace_chunk_put( |
442 | session->ust_session->current_trace_chunk); | |
443 | session->ust_session->current_trace_chunk = NULL; | |
82b69413 JG |
444 | } |
445 | if (session->kernel_session) { | |
d2956687 JG |
446 | lttng_trace_chunk_put( |
447 | session->kernel_session->current_trace_chunk); | |
448 | session->kernel_session->current_trace_chunk = NULL; | |
82b69413 | 449 | } |
d2956687 JG |
450 | if (!new_trace_chunk) { |
451 | ret = 0; | |
452 | goto end; | |
82b69413 | 453 | } |
d2956687 JG |
454 | chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id); |
455 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
82b69413 | 456 | |
d2956687 JG |
457 | refs_to_acquire = 1; |
458 | refs_to_acquire += !!session->ust_session; | |
459 | refs_to_acquire += !!session->kernel_session; | |
460 | ||
461 | for (refs_acquired = 0; refs_acquired < refs_to_acquire; | |
462 | refs_acquired++) { | |
463 | if (!lttng_trace_chunk_get(new_trace_chunk)) { | |
464 | ERR("Failed to acquire reference to new trace chunk of session \"%s\"", | |
465 | session->name); | |
466 | goto error; | |
82b69413 JG |
467 | } |
468 | } | |
469 | ||
d2956687 | 470 | if (session->ust_session) { |
e5add6d0 JG |
471 | const uint64_t relayd_id = |
472 | session->ust_session->consumer->net_seq_index; | |
473 | const bool is_local_trace = | |
474 | session->ust_session->consumer->type == | |
475 | CONSUMER_DST_LOCAL; | |
476 | ||
d2956687 JG |
477 | session->ust_session->current_trace_chunk = new_trace_chunk; |
478 | if (is_local_trace) { | |
479 | enum lttng_error_code ret_error_code; | |
82b69413 | 480 | |
d2956687 JG |
481 | ret_error_code = ust_app_create_channel_subdirectories( |
482 | session->ust_session); | |
483 | if (ret_error_code != LTTNG_OK) { | |
484 | ret = -ret_error_code; | |
82b69413 JG |
485 | goto error; |
486 | } | |
d2956687 JG |
487 | } |
488 | cds_lfht_for_each_entry( | |
489 | session->ust_session->consumer->socks->ht, | |
490 | &iter, socket, node.node) { | |
491 | pthread_mutex_lock(socket->lock); | |
492 | ret = consumer_create_trace_chunk(socket, | |
493 | relayd_id, | |
494 | session->id, new_trace_chunk); | |
495 | pthread_mutex_unlock(socket->lock); | |
496 | if (ret) { | |
497 | goto error; | |
498 | } | |
499 | } | |
500 | } | |
82b69413 | 501 | if (session->kernel_session) { |
e5add6d0 JG |
502 | const uint64_t relayd_id = |
503 | session->kernel_session->consumer->net_seq_index; | |
504 | const bool is_local_trace = | |
505 | session->kernel_session->consumer->type == | |
506 | CONSUMER_DST_LOCAL; | |
507 | ||
d2956687 JG |
508 | session->kernel_session->current_trace_chunk = new_trace_chunk; |
509 | if (is_local_trace) { | |
510 | enum lttng_error_code ret_error_code; | |
511 | ||
512 | ret_error_code = kernel_create_channel_subdirectories( | |
513 | session->kernel_session); | |
514 | if (ret_error_code != LTTNG_OK) { | |
515 | ret = -ret_error_code; | |
516 | goto error; | |
517 | } | |
518 | } | |
519 | cds_lfht_for_each_entry( | |
520 | session->kernel_session->consumer->socks->ht, | |
521 | &iter, socket, node.node) { | |
522 | pthread_mutex_lock(socket->lock); | |
523 | ret = consumer_create_trace_chunk(socket, | |
524 | relayd_id, | |
525 | session->id, new_trace_chunk); | |
526 | pthread_mutex_unlock(socket->lock); | |
527 | if (ret) { | |
528 | goto error; | |
529 | } | |
530 | } | |
531 | } | |
82b69413 JG |
532 | |
533 | /* | |
534 | * Update local current trace chunk state last, only if all remote | |
d2956687 | 535 | * creations succeeded. |
82b69413 JG |
536 | */ |
537 | session->current_trace_chunk = new_trace_chunk; | |
d2956687 JG |
538 | LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id); |
539 | end: | |
540 | if (_current_trace_chunk) { | |
541 | *_current_trace_chunk = current_trace_chunk; | |
542 | current_trace_chunk = NULL; | |
543 | } | |
544 | end_no_move: | |
545 | rcu_read_unlock(); | |
546 | lttng_trace_chunk_put(current_trace_chunk); | |
547 | return ret; | |
548 | error: | |
82b69413 | 549 | if (session->ust_session) { |
d2956687 | 550 | session->ust_session->current_trace_chunk = NULL; |
82b69413 JG |
551 | } |
552 | if (session->kernel_session) { | |
d2956687 | 553 | session->kernel_session->current_trace_chunk = NULL; |
82b69413 | 554 | } |
d2956687 | 555 | /* |
82b69413 JG |
556 | * Release references taken in the case where all references could not |
557 | * be acquired. | |
558 | */ | |
559 | refs_to_release = refs_to_acquire - refs_acquired; | |
560 | for (i = 0; i < refs_to_release; i++) { | |
561 | lttng_trace_chunk_put(new_trace_chunk); | |
562 | } | |
d2956687 JG |
563 | ret = -1; |
564 | goto end_no_move; | |
82b69413 JG |
565 | } |
566 | ||
e2b6b28e | 567 | bool session_output_supports_trace_chunks(const struct ltt_session *session) |
82b69413 | 568 | { |
e2b6b28e JG |
569 | const struct consumer_output *output = session->kernel_session ? |
570 | session->kernel_session->consumer : | |
571 | session->ust_session->consumer; | |
572 | ||
348a81dc | 573 | if (output->type == CONSUMER_DST_LOCAL) { |
82b69413 JG |
574 | return true; |
575 | } else { | |
82b69413 JG |
576 | if (output->relay_major_version > 2) { |
577 | return true; | |
578 | } else if (output->relay_major_version == 2 && | |
579 | output->relay_minor_version >= 11) { | |
580 | return true; | |
581 | } | |
582 | } | |
583 | return false; | |
584 | } | |
585 | ||
d2956687 | 586 | struct lttng_trace_chunk *session_create_new_trace_chunk( |
348a81dc JG |
587 | const struct ltt_session *session, |
588 | const struct consumer_output *consumer_output_override, | |
82b69413 JG |
589 | const char *session_base_path_override, |
590 | const char *chunk_name_override) | |
591 | { | |
592 | int ret; | |
82b69413 JG |
593 | struct lttng_trace_chunk *trace_chunk = NULL; |
594 | enum lttng_trace_chunk_status chunk_status; | |
d2956687 | 595 | const time_t chunk_creation_ts = time(NULL); |
348a81dc JG |
596 | bool is_local_trace; |
597 | const char *base_path; | |
82b69413 JG |
598 | struct lttng_directory_handle session_output_directory; |
599 | const struct lttng_credentials session_credentials = { | |
600 | .uid = session->uid, | |
601 | .gid = session->gid, | |
602 | }; | |
603 | uint64_t next_chunk_id; | |
348a81dc JG |
604 | const struct consumer_output *output; |
605 | ||
606 | if (consumer_output_override) { | |
607 | output = consumer_output_override; | |
608 | } else { | |
609 | assert(session->ust_session || session->kernel_session); | |
610 | output = session->ust_session ? | |
611 | session->ust_session->consumer : | |
612 | session->kernel_session->consumer; | |
613 | } | |
614 | ||
615 | is_local_trace = output->type == CONSUMER_DST_LOCAL; | |
616 | base_path = session_base_path_override ? : | |
617 | consumer_output_get_base_path(output); | |
82b69413 | 618 | |
d2956687 JG |
619 | if (chunk_creation_ts == (time_t) -1) { |
620 | PERROR("Failed to sample time while creation session \"%s\" trace chunk", | |
82b69413 | 621 | session->name); |
82b69413 JG |
622 | goto error; |
623 | } | |
82b69413 | 624 | |
d2956687 JG |
625 | next_chunk_id = session->most_recent_chunk_id.is_set ? |
626 | session->most_recent_chunk_id.value + 1 : 0; | |
82b69413 | 627 | |
d2956687 JG |
628 | trace_chunk = lttng_trace_chunk_create(next_chunk_id, |
629 | chunk_creation_ts); | |
82b69413 | 630 | if (!trace_chunk) { |
82b69413 JG |
631 | goto error; |
632 | } | |
633 | ||
634 | if (chunk_name_override) { | |
635 | chunk_status = lttng_trace_chunk_override_name(trace_chunk, | |
636 | chunk_name_override); | |
d2956687 | 637 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
82b69413 JG |
638 | goto error; |
639 | } | |
640 | } | |
641 | ||
642 | if (!is_local_trace) { | |
643 | /* | |
644 | * No need to set crendentials and output directory | |
645 | * for remote trace chunks. | |
646 | */ | |
d2956687 | 647 | goto end; |
82b69413 JG |
648 | } |
649 | ||
650 | chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, | |
651 | &session_credentials); | |
652 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
82b69413 JG |
653 | goto error; |
654 | } | |
655 | ||
d2956687 JG |
656 | DBG("Creating base output directory of session \"%s\" at %s", |
657 | session->name, base_path); | |
82b69413 JG |
658 | ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, |
659 | session->uid, session->gid); | |
660 | if (ret) { | |
82b69413 JG |
661 | goto error; |
662 | } | |
663 | ret = lttng_directory_handle_init(&session_output_directory, | |
664 | base_path); | |
665 | if (ret) { | |
82b69413 JG |
666 | goto error; |
667 | } | |
668 | chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, | |
669 | &session_output_directory); | |
670 | lttng_directory_handle_fini(&session_output_directory); | |
671 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
82b69413 JG |
672 | goto error; |
673 | } | |
d2956687 JG |
674 | end: |
675 | return trace_chunk; | |
82b69413 JG |
676 | error: |
677 | lttng_trace_chunk_put(trace_chunk); | |
d2956687 JG |
678 | trace_chunk = NULL; |
679 | goto end; | |
680 | } | |
681 | ||
682 | int session_close_trace_chunk(const struct ltt_session *session, | |
bbc4768c JG |
683 | struct lttng_trace_chunk *trace_chunk, |
684 | const enum lttng_trace_chunk_command_type *close_command) | |
d2956687 JG |
685 | { |
686 | int ret = 0; | |
687 | bool error_occurred = false; | |
688 | struct cds_lfht_iter iter; | |
689 | struct consumer_socket *socket; | |
690 | enum lttng_trace_chunk_status chunk_status; | |
691 | const time_t chunk_close_timestamp = time(NULL); | |
692 | ||
bbc4768c JG |
693 | if (close_command) { |
694 | chunk_status = lttng_trace_chunk_set_close_command( | |
695 | trace_chunk, *close_command); | |
696 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
697 | ret = -1; | |
698 | goto end; | |
699 | } | |
700 | } | |
701 | ||
d2956687 JG |
702 | if (chunk_close_timestamp == (time_t) -1) { |
703 | ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"", | |
704 | session->name); | |
705 | ret = -1; | |
706 | goto end; | |
707 | } | |
708 | chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, | |
709 | chunk_close_timestamp); | |
710 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
711 | ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"", | |
712 | session->name); | |
713 | ret = -1; | |
714 | goto end; | |
715 | } | |
716 | ||
717 | if (session->ust_session) { | |
718 | cds_lfht_for_each_entry( | |
719 | session->ust_session->consumer->socks->ht, | |
720 | &iter, socket, node.node) { | |
721 | pthread_mutex_lock(socket->lock); | |
722 | ret = consumer_close_trace_chunk(socket, | |
723 | session->consumer->net_seq_index, | |
724 | session->id, | |
725 | trace_chunk); | |
726 | pthread_mutex_unlock(socket->lock); | |
727 | if (ret) { | |
728 | ERR("Failed to close trace chunk on user space consumer"); | |
729 | error_occurred = true; | |
730 | } | |
731 | } | |
732 | } | |
733 | if (session->kernel_session) { | |
734 | cds_lfht_for_each_entry( | |
735 | session->kernel_session->consumer->socks->ht, | |
736 | &iter, socket, node.node) { | |
737 | pthread_mutex_lock(socket->lock); | |
738 | ret = consumer_close_trace_chunk(socket, | |
739 | session->consumer->net_seq_index, | |
740 | session->id, | |
741 | trace_chunk); | |
742 | pthread_mutex_unlock(socket->lock); | |
743 | if (ret) { | |
744 | ERR("Failed to close trace chunk on kernel consumer"); | |
745 | error_occurred = true; | |
746 | } | |
747 | } | |
748 | } | |
749 | ret = error_occurred ? -1 : 0; | |
82b69413 | 750 | end: |
d2956687 | 751 | return ret; |
82b69413 JG |
752 | } |
753 | ||
754 | /* | |
755 | * Set a session's current trace chunk. | |
756 | * | |
757 | * Must be called with the session lock held. | |
758 | */ | |
759 | int session_set_trace_chunk(struct ltt_session *session, | |
d2956687 JG |
760 | struct lttng_trace_chunk *new_trace_chunk, |
761 | struct lttng_trace_chunk **current_trace_chunk) | |
82b69413 JG |
762 | { |
763 | ASSERT_LOCKED(session->lock); | |
d2956687 JG |
764 | return _session_set_trace_chunk_no_lock_check(session, new_trace_chunk, |
765 | current_trace_chunk); | |
82b69413 JG |
766 | } |
767 | ||
3e3665b8 JG |
768 | static |
769 | void session_notify_destruction(const struct ltt_session *session) | |
770 | { | |
771 | size_t i; | |
772 | const size_t count = lttng_dynamic_array_get_count( | |
773 | &session->destroy_notifiers); | |
774 | ||
775 | for (i = 0; i < count; i++) { | |
776 | const struct ltt_session_destroy_notifier_element *element = | |
777 | lttng_dynamic_array_get_element( | |
778 | &session->destroy_notifiers, i); | |
779 | ||
780 | element->notifier(session, element->user_data); | |
781 | } | |
782 | } | |
783 | ||
e32d7f27 JG |
784 | static |
785 | void session_release(struct urcu_ref *ref) | |
786 | { | |
787 | int ret; | |
788 | struct ltt_ust_session *usess; | |
789 | struct ltt_kernel_session *ksess; | |
790 | struct ltt_session *session = container_of(ref, typeof(*session), ref); | |
791 | ||
d2956687 JG |
792 | assert(!session->chunk_being_archived); |
793 | ||
e32d7f27 JG |
794 | usess = session->ust_session; |
795 | ksess = session->kernel_session; | |
3e3665b8 JG |
796 | |
797 | session_notify_destruction(session); | |
93bed9fe | 798 | lttng_dynamic_array_reset(&session->destroy_notifiers); |
d2956687 | 799 | if (session->current_trace_chunk) { |
bbc4768c | 800 | ret = session_close_trace_chunk(session, session->current_trace_chunk, NULL); |
d2956687 JG |
801 | if (ret) { |
802 | ERR("Failed to close the current trace chunk of session \"%s\" during its release", | |
803 | session->name); | |
804 | } | |
805 | ret = _session_set_trace_chunk_no_lock_check(session, NULL, NULL); | |
806 | if (ret) { | |
807 | ERR("Failed to release the current trace chunk of session \"%s\" during its release", | |
808 | session->name); | |
809 | } | |
810 | } | |
e32d7f27 | 811 | |
d2956687 | 812 | /* Clean kernel session teardown */ |
e32d7f27 | 813 | kernel_destroy_session(ksess); |
82b69413 | 814 | session->kernel_session = NULL; |
e32d7f27 JG |
815 | |
816 | /* UST session teardown */ | |
817 | if (usess) { | |
818 | /* Close any relayd session */ | |
819 | consumer_output_send_destroy_relayd(usess->consumer); | |
820 | ||
821 | /* Destroy every UST application related to this session. */ | |
822 | ret = ust_app_destroy_trace_all(usess); | |
823 | if (ret) { | |
824 | ERR("Error in ust_app_destroy_trace_all"); | |
825 | } | |
826 | ||
827 | /* Clean up the rest. */ | |
828 | trace_ust_destroy_session(usess); | |
82b69413 | 829 | session->ust_session = NULL; |
e32d7f27 JG |
830 | } |
831 | ||
832 | /* | |
833 | * Must notify the kernel thread here to update it's poll set in order to | |
834 | * remove the channel(s)' fd just destroyed. | |
835 | */ | |
836 | ret = notify_thread_pipe(kernel_poll_pipe[1]); | |
837 | if (ret < 0) { | |
838 | PERROR("write kernel poll pipe"); | |
839 | } | |
840 | ||
841 | DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id); | |
e32d7f27 JG |
842 | |
843 | consumer_output_put(session->consumer); | |
844 | snapshot_destroy(&session->snapshot); | |
99d688f2 | 845 | |
82b69413 JG |
846 | pthread_mutex_destroy(&session->lock); |
847 | ||
f4cc5e83 JG |
848 | if (session->published) { |
849 | ASSERT_LOCKED(ltt_session_list.lock); | |
850 | del_session_list(session); | |
851 | del_session_ht(session); | |
852 | pthread_cond_broadcast(<t_session_list.removal_cond); | |
853 | } | |
d2956687 | 854 | free(session->last_archived_chunk_name); |
e32d7f27 JG |
855 | free(session); |
856 | } | |
857 | ||
858 | /* | |
859 | * Acquire a reference to a session. | |
860 | * This function may fail (return false); its return value must be checked. | |
861 | */ | |
862 | bool session_get(struct ltt_session *session) | |
863 | { | |
864 | return urcu_ref_get_unless_zero(&session->ref); | |
865 | } | |
866 | ||
867 | /* | |
868 | * Release a reference to a session. | |
869 | */ | |
870 | void session_put(struct ltt_session *session) | |
871 | { | |
b178f53e JG |
872 | if (!session) { |
873 | return; | |
874 | } | |
e32d7f27 JG |
875 | /* |
876 | * The session list lock must be held as any session_put() | |
877 | * may cause the removal of the session from the session_list. | |
878 | */ | |
879 | ASSERT_LOCKED(ltt_session_list.lock); | |
880 | assert(session->ref.refcount); | |
881 | urcu_ref_put(&session->ref, session_release); | |
882 | } | |
883 | ||
884 | /* | |
885 | * Destroy a session. | |
886 | * | |
887 | * This method does not immediately release/free the session as other | |
888 | * components may still hold a reference to the session. However, | |
889 | * the session should no longer be presented to the user. | |
890 | * | |
891 | * Releases the session list's reference to the session | |
892 | * and marks it as destroyed. Iterations on the session list should be | |
893 | * mindful of the "destroyed" flag. | |
894 | */ | |
895 | void session_destroy(struct ltt_session *session) | |
896 | { | |
897 | assert(!session->destroyed); | |
898 | session->destroyed = true; | |
899 | session_put(session); | |
900 | } | |
901 | ||
3e3665b8 JG |
902 | int session_add_destroy_notifier(struct ltt_session *session, |
903 | ltt_session_destroy_notifier notifier, void *user_data) | |
904 | { | |
905 | const struct ltt_session_destroy_notifier_element element = { | |
906 | .notifier = notifier, | |
907 | .user_data = user_data | |
908 | }; | |
909 | ||
910 | return lttng_dynamic_array_add_element(&session->destroy_notifiers, | |
911 | &element); | |
912 | } | |
913 | ||
5b74c7b1 | 914 | /* |
74babd95 | 915 | * Return a ltt_session structure ptr that matches name. If no session found, |
23324029 | 916 | * NULL is returned. This must be called with the session list lock held using |
74babd95 | 917 | * session_lock_list and session_unlock_list. |
e32d7f27 | 918 | * A reference to the session is implicitly acquired by this function. |
5b74c7b1 | 919 | */ |
58a1a227 | 920 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 921 | { |
5b74c7b1 DG |
922 | struct ltt_session *iter; |
923 | ||
0525e9ae | 924 | assert(name); |
e32d7f27 | 925 | ASSERT_LOCKED(ltt_session_list.lock); |
0525e9ae | 926 | |
5f822d0a DG |
927 | DBG2("Trying to find session by name %s", name); |
928 | ||
5b74c7b1 | 929 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
e32d7f27 JG |
930 | if (!strncmp(iter->name, name, NAME_MAX) && |
931 | !iter->destroyed) { | |
74babd95 | 932 | goto found; |
5b74c7b1 DG |
933 | } |
934 | } | |
935 | ||
e32d7f27 | 936 | return NULL; |
74babd95 | 937 | found: |
e32d7f27 | 938 | return session_get(iter) ? iter : NULL; |
5b74c7b1 DG |
939 | } |
940 | ||
23324029 JD |
941 | /* |
942 | * Return an ltt_session that matches the id. If no session is found, | |
943 | * NULL is returned. This must be called with rcu_read_lock and | |
944 | * session list lock held (to guarantee the lifetime of the session). | |
945 | */ | |
946 | struct ltt_session *session_find_by_id(uint64_t id) | |
947 | { | |
948 | struct lttng_ht_node_u64 *node; | |
949 | struct lttng_ht_iter iter; | |
950 | struct ltt_session *ls; | |
951 | ||
e32d7f27 JG |
952 | ASSERT_LOCKED(ltt_session_list.lock); |
953 | ||
d68ec974 JG |
954 | if (!ltt_sessions_ht_by_id) { |
955 | goto end; | |
956 | } | |
957 | ||
23324029 JD |
958 | lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter); |
959 | node = lttng_ht_iter_get_node_u64(&iter); | |
960 | if (node == NULL) { | |
d68ec974 | 961 | goto end; |
23324029 JD |
962 | } |
963 | ls = caa_container_of(node, struct ltt_session, node); | |
964 | ||
965 | DBG3("Session %" PRIu64 " found by id.", id); | |
e32d7f27 | 966 | return session_get(ls) ? ls : NULL; |
23324029 | 967 | |
d68ec974 | 968 | end: |
23324029 JD |
969 | DBG3("Session %" PRIu64 " NOT found by id", id); |
970 | return NULL; | |
971 | } | |
972 | ||
5b74c7b1 | 973 | /* |
b178f53e JG |
974 | * Create a new session and add it to the session list. |
975 | * Session list lock must be held by the caller. | |
5b74c7b1 | 976 | */ |
b178f53e JG |
977 | enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, |
978 | struct ltt_session **out_session) | |
5b74c7b1 | 979 | { |
f3ed775e | 980 | int ret; |
b178f53e JG |
981 | enum lttng_error_code ret_code; |
982 | struct ltt_session *new_session = NULL; | |
e07ae692 | 983 | |
b178f53e JG |
984 | ASSERT_LOCKED(ltt_session_list.lock); |
985 | if (name) { | |
986 | struct ltt_session *clashing_session; | |
987 | ||
988 | clashing_session = session_find_by_name(name); | |
989 | if (clashing_session) { | |
990 | session_put(clashing_session); | |
991 | ret_code = LTTNG_ERR_EXIST_SESS; | |
992 | goto error; | |
993 | } | |
994 | } | |
ba7f0ae5 | 995 | new_session = zmalloc(sizeof(struct ltt_session)); |
b178f53e JG |
996 | if (!new_session) { |
997 | PERROR("Failed to allocate an ltt_session structure"); | |
998 | ret_code = LTTNG_ERR_NOMEM; | |
999 | goto error; | |
5b74c7b1 DG |
1000 | } |
1001 | ||
3e3665b8 | 1002 | lttng_dynamic_array_init(&new_session->destroy_notifiers, |
93bed9fe JG |
1003 | sizeof(struct ltt_session_destroy_notifier_element), |
1004 | NULL); | |
e32d7f27 | 1005 | urcu_ref_init(&new_session->ref); |
b178f53e | 1006 | pthread_mutex_init(&new_session->lock, NULL); |
e32d7f27 | 1007 | |
b178f53e JG |
1008 | new_session->creation_time = time(NULL); |
1009 | if (new_session->creation_time == (time_t) -1) { | |
1010 | PERROR("Failed to sample session creation time"); | |
1011 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
f3ed775e DG |
1012 | goto error; |
1013 | } | |
1014 | ||
b178f53e JG |
1015 | /* Create default consumer output. */ |
1016 | new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
1017 | if (new_session->consumer == NULL) { | |
1018 | ret_code = LTTNG_ERR_NOMEM; | |
1c1c3634 DG |
1019 | goto error; |
1020 | } | |
1021 | ||
b178f53e JG |
1022 | if (name) { |
1023 | ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name)); | |
1024 | if (ret) { | |
1025 | ret_code = LTTNG_ERR_SESSION_INVALID_CHAR; | |
1026 | goto error; | |
1027 | } | |
1028 | ret = validate_name(name); | |
1029 | if (ret < 0) { | |
1030 | ret_code = LTTNG_ERR_SESSION_INVALID_CHAR; | |
1031 | goto error; | |
1032 | } | |
1033 | } else { | |
1034 | int i = 0; | |
1035 | bool found_name = false; | |
1036 | char datetime[16]; | |
1037 | struct tm *timeinfo; | |
1038 | ||
1039 | timeinfo = localtime(&new_session->creation_time); | |
1040 | if (!timeinfo) { | |
1041 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1042 | goto error; | |
1043 | } | |
1044 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
1045 | for (i = 0; i < INT_MAX; i++) { | |
1046 | struct ltt_session *clashing_session; | |
1047 | ||
1048 | if (i == 0) { | |
1049 | ret = snprintf(new_session->name, | |
1050 | sizeof(new_session->name), | |
1051 | "%s-%s", | |
1052 | DEFAULT_SESSION_NAME, | |
1053 | datetime); | |
1054 | } else { | |
1055 | ret = snprintf(new_session->name, | |
1056 | sizeof(new_session->name), | |
1057 | "%s%d-%s", | |
1058 | DEFAULT_SESSION_NAME, i, | |
1059 | datetime); | |
1060 | } | |
1061 | if (ret == -1 || ret >= sizeof(new_session->name)) { | |
1062 | /* | |
1063 | * Null-terminate in case the name is used | |
1064 | * in logging statements. | |
1065 | */ | |
1066 | new_session->name[sizeof(new_session->name) - 1] = '\0'; | |
1067 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1068 | goto error; | |
1069 | } | |
1070 | ||
1071 | clashing_session = | |
1072 | session_find_by_name(new_session->name); | |
1073 | session_put(clashing_session); | |
1074 | if (!clashing_session) { | |
1075 | found_name = true; | |
1076 | break; | |
1077 | } | |
1078 | } | |
1079 | if (found_name) { | |
1080 | DBG("Generated session name \"%s\"", new_session->name); | |
1081 | new_session->has_auto_generated_name = true; | |
1082 | } else { | |
1083 | ERR("Failed to auto-generate a session name"); | |
1084 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
1085 | goto error; | |
1086 | } | |
1087 | } | |
1088 | ||
d3e2ba59 | 1089 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
1090 | if (ret < 0) { |
1091 | if (errno == ENAMETOOLONG) { | |
1092 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
b178f53e JG |
1093 | ERR("Hostname exceeds the maximal permitted length and has been truncated to %s", |
1094 | new_session->hostname); | |
73184835 | 1095 | } else { |
b178f53e | 1096 | ret_code = LTTNG_ERR_SESSION_FAIL; |
73184835 DG |
1097 | goto error; |
1098 | } | |
d3e2ba59 JD |
1099 | } |
1100 | ||
6df2e2c9 MD |
1101 | new_session->uid = uid; |
1102 | new_session->gid = gid; | |
1103 | ||
6dc3064a DG |
1104 | ret = snapshot_init(&new_session->snapshot); |
1105 | if (ret < 0) { | |
b178f53e | 1106 | ret_code = LTTNG_ERR_NOMEM; |
6dc3064a DG |
1107 | goto error; |
1108 | } | |
1109 | ||
4f23c583 | 1110 | new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION; |
92816cc3 | 1111 | |
b178f53e | 1112 | /* Add new session to the session list. */ |
a991f516 | 1113 | new_session->id = add_session_list(new_session); |
b178f53e | 1114 | |
23324029 JD |
1115 | /* |
1116 | * Add the new session to the ltt_sessions_ht_by_id. | |
1117 | * No ownership is taken by the hash table; it is merely | |
1118 | * a wrapper around the session list used for faster access | |
1119 | * by session id. | |
1120 | */ | |
1121 | add_session_ht(new_session); | |
f4cc5e83 | 1122 | new_session->published = true; |
b5541356 | 1123 | |
a4b92340 | 1124 | /* |
b178f53e JG |
1125 | * Consumer is left to NULL since the create_session_uri command will |
1126 | * set it up and, if valid, assign it to the session. | |
a4b92340 | 1127 | */ |
b178f53e JG |
1128 | DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d", |
1129 | new_session->name, new_session->id, new_session->uid, | |
1130 | new_session->gid); | |
1131 | ret_code = LTTNG_OK; | |
1132 | end: | |
1133 | if (new_session) { | |
1134 | (void) session_get(new_session); | |
1135 | *out_session = new_session; | |
1136 | } | |
1137 | return ret_code; | |
5b74c7b1 | 1138 | error: |
f4cc5e83 | 1139 | session_put(new_session); |
b178f53e JG |
1140 | new_session = NULL; |
1141 | goto end; | |
5b74c7b1 | 1142 | } |
2f77fc4b DG |
1143 | |
1144 | /* | |
1145 | * Check if the UID or GID match the session. Root user has access to all | |
1146 | * sessions. | |
1147 | */ | |
1148 | int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) | |
1149 | { | |
1150 | assert(session); | |
1151 | ||
1152 | if (uid != session->uid && gid != session->gid && uid != 0) { | |
1153 | return 0; | |
1154 | } else { | |
1155 | return 1; | |
1156 | } | |
1157 | } | |
2961f09e JG |
1158 | |
1159 | /* | |
1160 | * Set a session's rotation state and reset all associated state. | |
1161 | * | |
1162 | * This function resets the rotation state (check timers, pending | |
1163 | * flags, etc.) and sets the result of the last rotation. The result | |
1164 | * can be queries by a liblttng-ctl client. | |
1165 | * | |
1166 | * Be careful of the result passed to this function. For instance, | |
1167 | * on failure to launch a rotation, a client will expect the rotation | |
1168 | * state to be set to "NO_ROTATION". If an error occured while the | |
1169 | * rotation was "ONGOING", result should be set to "ERROR", which will | |
1170 | * allow a client to report it. | |
1171 | * | |
1172 | * Must be called with the session and session_list locks held. | |
1173 | */ | |
1174 | int session_reset_rotation_state(struct ltt_session *session, | |
1175 | enum lttng_rotation_state result) | |
1176 | { | |
1177 | int ret = 0; | |
1178 | ||
1179 | ASSERT_LOCKED(ltt_session_list.lock); | |
1180 | ASSERT_LOCKED(session->lock); | |
1181 | ||
2961f09e JG |
1182 | session->rotation_state = result; |
1183 | if (session->rotation_pending_check_timer_enabled) { | |
1184 | ret = timer_session_rotation_pending_check_stop(session); | |
1185 | } | |
d2956687 JG |
1186 | if (session->chunk_being_archived) { |
1187 | uint64_t chunk_id; | |
1188 | enum lttng_trace_chunk_status chunk_status; | |
1189 | ||
1190 | chunk_status = lttng_trace_chunk_get_id( | |
1191 | session->chunk_being_archived, | |
1192 | &chunk_id); | |
1193 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
1194 | LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id, | |
1195 | chunk_id); | |
1196 | lttng_trace_chunk_put(session->chunk_being_archived); | |
1197 | session->chunk_being_archived = NULL; | |
1198 | } | |
2961f09e JG |
1199 | return ret; |
1200 | } |