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