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