2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/constant.h>
19 #include <common/string-utils/format.h>
20 #include <common/trace-chunk.h>
21 #include <common/trace-chunk-registry.h>
22 #include <common/hashtable/utils.h>
23 #include <common/hashtable/hashtable.h>
24 #include <common/error.h>
25 #include <common/utils.h>
26 #include <common/time.h>
27 #include <common/optional.h>
28 #include <common/compat/directory-handle.h>
29 #include <common/credentials.h>
30 #include <common/defaults.h>
31 #include <common/dynamic-array.h>
34 #include <urcu/rculfhash.h>
41 * Two ISO 8601-compatible timestamps, separated by a hypen, followed an
42 * index, i.e. <start-iso-8601>-<end-iso-8601>-<id-uint64_t>.
44 #define GENERATED_CHUNK_NAME_LEN (2 * sizeof("YYYYmmddTHHMMSS+HHMM") + MAX_INT_DEC_LEN(uint64_t))
45 #define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
47 enum trace_chunk_mode
{
48 TRACE_CHUNK_MODE_USER
,
49 TRACE_CHUNK_MODE_OWNER
,
53 * Callback to invoke on release of a trace chunk. Note that there is no
54 * need to 'lock' the trace chunk during the execution of these callbacks
55 * since only one thread may access a chunk during its destruction (the last
56 * to release its reference to the chunk).
58 typedef void (*chunk_close_command
)(struct lttng_trace_chunk
*trace_chunk
);
60 /* Move a completed trace chunk to the 'completed' trace archive folder. */
62 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
);
64 struct chunk_credentials
{
65 bool use_current_user
;
66 struct lttng_credentials user
;
69 /* NOTE: Make sure to update lttng_trace_chunk_copy if you modify this. */
70 struct lttng_trace_chunk
{
73 LTTNG_OPTIONAL(enum trace_chunk_mode
) mode
;
75 * First-level directories created within the trace chunk.
76 * Elements are of type 'char *'.
78 * Only used by _owner_ mode chunks.
80 struct lttng_dynamic_pointer_array top_level_directories
;
81 /* Is contained within an lttng_trace_chunk_registry_element? */
82 bool in_registry_element
;
85 /* An unset id means the chunk is anonymous. */
86 LTTNG_OPTIONAL(uint64_t) id
;
87 LTTNG_OPTIONAL(time_t) timestamp_creation
;
88 LTTNG_OPTIONAL(time_t) timestamp_close
;
89 LTTNG_OPTIONAL(struct chunk_credentials
) credentials
;
90 struct lttng_directory_handle
*session_output_directory
;
91 struct lttng_directory_handle
*chunk_directory
;
92 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
;
95 /* A trace chunk is uniquely identified by its (session id, chunk id) tuple. */
96 struct lttng_trace_chunk_registry_element
{
97 struct lttng_trace_chunk chunk
;
99 /* Weak and only set when added. */
100 struct lttng_trace_chunk_registry
*registry
;
101 struct cds_lfht_node trace_chunk_registry_ht_node
;
102 /* call_rcu delayed reclaim. */
103 struct rcu_head rcu_node
;
106 struct lttng_trace_chunk_registry
{
111 char *close_command_names
[] = {
112 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
113 "move to completed chunk folder",
117 chunk_close_command close_command_funcs
[] = {
118 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
119 lttng_trace_chunk_move_to_completed
,
123 bool lttng_trace_chunk_registry_element_equals(
124 const struct lttng_trace_chunk_registry_element
*a
,
125 const struct lttng_trace_chunk_registry_element
*b
)
127 if (a
->session_id
!= b
->session_id
) {
130 if (a
->chunk
.id
.is_set
!= b
->chunk
.id
.is_set
) {
133 if (a
->chunk
.id
.is_set
&& a
->chunk
.id
.value
!= b
->chunk
.id
.value
) {
142 int lttng_trace_chunk_registry_element_match(struct cds_lfht_node
*node
,
145 const struct lttng_trace_chunk_registry_element
*element_a
, *element_b
;
147 element_a
= (const struct lttng_trace_chunk_registry_element
*) key
;
148 element_b
= caa_container_of(node
, typeof(*element_b
),
149 trace_chunk_registry_ht_node
);
150 return lttng_trace_chunk_registry_element_equals(element_a
, element_b
);
154 unsigned long lttng_trace_chunk_registry_element_hash(
155 const struct lttng_trace_chunk_registry_element
*element
)
157 unsigned long hash
= hash_key_u64(&element
->session_id
,
160 if (element
->chunk
.id
.is_set
) {
161 hash
^= hash_key_u64(&element
->chunk
.id
.value
, lttng_ht_seed
);
168 char *generate_chunk_name(uint64_t chunk_id
, time_t creation_timestamp
,
169 const time_t *close_timestamp
)
172 char *new_name
= NULL
;
173 char start_datetime
[ISO8601_STR_LEN
] = {};
174 /* Add 1 for a '-' prefix. */
175 char end_datetime_suffix
[ISO8601_STR_LEN
+ 1] = {};
177 ret
= time_to_iso8601_str(
179 start_datetime
, sizeof(start_datetime
));
181 ERR("Failed to format trace chunk start date time");
184 if (close_timestamp
) {
185 *end_datetime_suffix
= '-';
186 ret
= time_to_iso8601_str(
188 end_datetime_suffix
+ 1,
189 sizeof(end_datetime_suffix
) - 1);
191 ERR("Failed to format trace chunk end date time");
195 new_name
= zmalloc(GENERATED_CHUNK_NAME_LEN
);
197 ERR("Failed to allocate buffer for automatically-generated trace chunk name");
200 ret
= snprintf(new_name
, GENERATED_CHUNK_NAME_LEN
, "%s%s-%" PRIu64
,
201 start_datetime
, end_datetime_suffix
, chunk_id
);
202 if (ret
>= GENERATED_CHUNK_NAME_LEN
|| ret
== -1) {
203 ERR("Failed to format trace chunk name");
214 void lttng_trace_chunk_init(struct lttng_trace_chunk
*chunk
)
216 urcu_ref_init(&chunk
->ref
);
217 pthread_mutex_init(&chunk
->lock
, NULL
);
218 lttng_dynamic_pointer_array_init(&chunk
->top_level_directories
, free
);
222 void lttng_trace_chunk_fini(struct lttng_trace_chunk
*chunk
)
224 if (chunk
->session_output_directory
) {
225 lttng_directory_handle_put(
226 chunk
->session_output_directory
);
227 chunk
->session_output_directory
= NULL
;
229 if (chunk
->chunk_directory
) {
230 lttng_directory_handle_put(chunk
->chunk_directory
);
231 chunk
->chunk_directory
= NULL
;
235 lttng_dynamic_pointer_array_reset(&chunk
->top_level_directories
);
236 pthread_mutex_destroy(&chunk
->lock
);
240 struct lttng_trace_chunk
*lttng_trace_chunk_allocate(void)
242 struct lttng_trace_chunk
*chunk
= NULL
;
244 chunk
= zmalloc(sizeof(*chunk
));
246 ERR("Failed to allocate trace chunk");
249 lttng_trace_chunk_init(chunk
);
255 struct lttng_trace_chunk
*lttng_trace_chunk_create_anonymous(void)
257 DBG("Creating anonymous trace chunk");
258 return lttng_trace_chunk_allocate();
262 struct lttng_trace_chunk
*lttng_trace_chunk_create(
263 uint64_t chunk_id
, time_t chunk_creation_time
)
265 struct lttng_trace_chunk
*chunk
;
266 char chunk_creation_datetime_buf
[16] = {};
267 const char *chunk_creation_datetime_str
= "(formatting error)";
268 struct tm timeinfo_buf
, *timeinfo
;
270 timeinfo
= localtime_r(&chunk_creation_time
, &timeinfo_buf
);
274 /* Don't fail because of this; it is only used for logging. */
275 strftime_ret
= strftime(chunk_creation_datetime_buf
,
276 sizeof(chunk_creation_datetime_buf
),
277 "%Y%m%d-%H%M%S", timeinfo
);
279 chunk_creation_datetime_str
=
280 chunk_creation_datetime_buf
;
284 DBG("Creating trace chunk: chunk_id = %" PRIu64
", creation time = %s",
285 chunk_id
, chunk_creation_datetime_str
);
286 chunk
= lttng_trace_chunk_allocate();
291 LTTNG_OPTIONAL_SET(&chunk
->id
, chunk_id
);
292 LTTNG_OPTIONAL_SET(&chunk
->timestamp_creation
, chunk_creation_time
);
294 chunk
->name
= generate_chunk_name(chunk_id
,
295 chunk_creation_time
, NULL
);
297 ERR("Failed to allocate trace chunk name storage");
302 DBG("Chunk name set to \"%s\"", chunk
->name
? : "(none)");
306 lttng_trace_chunk_put(chunk
);
311 struct lttng_trace_chunk
*lttng_trace_chunk_copy(
312 struct lttng_trace_chunk
*source_chunk
)
314 struct lttng_trace_chunk
*new_chunk
= lttng_trace_chunk_allocate();
320 pthread_mutex_lock(&source_chunk
->lock
);
322 * A new chunk is always a user; it shall create no new trace
325 new_chunk
->mode
= (typeof(new_chunk
->mode
)) {
327 .value
= TRACE_CHUNK_MODE_USER
,
330 * top_level_directories is not copied as it is never used
331 * by _user_ mode chunks.
333 /* The new chunk is not part of a registry (yet, at least). */
334 new_chunk
->in_registry_element
= false;
335 new_chunk
->name_overridden
= source_chunk
->name_overridden
;
336 if (source_chunk
->name
) {
337 new_chunk
->name
= strdup(source_chunk
->name
);
338 if (!new_chunk
->name
) {
339 ERR("Failed to copy source trace chunk name in %s()",
344 new_chunk
->id
= source_chunk
->id
;
345 new_chunk
->timestamp_creation
= source_chunk
->timestamp_creation
;
346 new_chunk
->timestamp_close
= source_chunk
->timestamp_close
;
347 new_chunk
->credentials
= source_chunk
->credentials
;
348 if (source_chunk
->session_output_directory
) {
349 const bool reference_acquired
= lttng_directory_handle_get(
350 source_chunk
->session_output_directory
);
352 assert(reference_acquired
);
353 new_chunk
->session_output_directory
=
354 source_chunk
->session_output_directory
;
356 if (source_chunk
->chunk_directory
) {
357 const bool reference_acquired
= lttng_directory_handle_get(
358 source_chunk
->chunk_directory
);
360 assert(reference_acquired
);
361 new_chunk
->chunk_directory
= source_chunk
->chunk_directory
;
363 new_chunk
->close_command
= source_chunk
->close_command
;
364 pthread_mutex_unlock(&source_chunk
->lock
);
368 pthread_mutex_unlock(&source_chunk
->lock
);
369 lttng_trace_chunk_put(new_chunk
);
374 enum lttng_trace_chunk_status
lttng_trace_chunk_get_id(
375 struct lttng_trace_chunk
*chunk
, uint64_t *id
)
377 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
379 pthread_mutex_lock(&chunk
->lock
);
380 if (chunk
->id
.is_set
) {
381 *id
= chunk
->id
.value
;
383 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
385 pthread_mutex_unlock(&chunk
->lock
);
390 enum lttng_trace_chunk_status
lttng_trace_chunk_get_creation_timestamp(
391 struct lttng_trace_chunk
*chunk
, time_t *creation_ts
)
394 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
396 pthread_mutex_lock(&chunk
->lock
);
397 if (chunk
->timestamp_creation
.is_set
) {
398 *creation_ts
= chunk
->timestamp_creation
.value
;
400 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
402 pthread_mutex_unlock(&chunk
->lock
);
407 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_timestamp(
408 struct lttng_trace_chunk
*chunk
, time_t *close_ts
)
410 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
412 pthread_mutex_lock(&chunk
->lock
);
413 if (chunk
->timestamp_close
.is_set
) {
414 *close_ts
= chunk
->timestamp_close
.value
;
416 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
418 pthread_mutex_unlock(&chunk
->lock
);
423 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_timestamp(
424 struct lttng_trace_chunk
*chunk
, time_t close_ts
)
426 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
428 pthread_mutex_lock(&chunk
->lock
);
429 if (!chunk
->timestamp_creation
.is_set
) {
430 ERR("Failed to set trace chunk close timestamp: creation timestamp is unset");
431 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
434 if (chunk
->timestamp_creation
.value
> close_ts
) {
435 ERR("Failed to set trace chunk close timestamp: close timestamp is before creation timestamp");
436 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
439 LTTNG_OPTIONAL_SET(&chunk
->timestamp_close
, close_ts
);
440 if (!chunk
->name_overridden
) {
442 chunk
->name
= generate_chunk_name(LTTNG_OPTIONAL_GET(chunk
->id
),
443 LTTNG_OPTIONAL_GET(chunk
->timestamp_creation
),
446 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
450 pthread_mutex_unlock(&chunk
->lock
);
455 enum lttng_trace_chunk_status
lttng_trace_chunk_get_name(
456 struct lttng_trace_chunk
*chunk
, const char **name
,
457 bool *name_overridden
)
459 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
461 pthread_mutex_lock(&chunk
->lock
);
462 if (name_overridden
) {
463 *name_overridden
= chunk
->name_overridden
;
466 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
471 pthread_mutex_unlock(&chunk
->lock
);
476 bool is_valid_chunk_name(const char *name
)
484 len
= lttng_strnlen(name
, LTTNG_NAME_MAX
);
485 if (len
== 0 || len
== LTTNG_NAME_MAX
) {
489 if (strchr(name
, '/') || strchr(name
, '.')) {
497 enum lttng_trace_chunk_status
lttng_trace_chunk_override_name(
498 struct lttng_trace_chunk
*chunk
, const char *name
)
502 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
504 if (!is_valid_chunk_name(name
)) {
505 ERR("Attempted to set an invalid name on a trace chunk: name = %s",
507 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
511 pthread_mutex_lock(&chunk
->lock
);
512 if (!chunk
->id
.is_set
) {
513 ERR("Attempted to set an override name on an anonymous trace chunk: name = %s",
515 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
518 new_name
= strdup(name
);
520 ERR("Failed to allocate new trace chunk name");
521 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
525 chunk
->name
= new_name
;
526 chunk
->name_overridden
= true;
528 pthread_mutex_unlock(&chunk
->lock
);
534 enum lttng_trace_chunk_status
lttng_trace_chunk_get_credentials(
535 struct lttng_trace_chunk
*chunk
,
536 struct lttng_credentials
*credentials
)
538 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
540 pthread_mutex_lock(&chunk
->lock
);
541 if (chunk
->credentials
.is_set
) {
542 if (chunk
->credentials
.value
.use_current_user
) {
543 credentials
->uid
= geteuid();
544 credentials
->gid
= getegid();
546 *credentials
= chunk
->credentials
.value
.user
;
549 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
551 pthread_mutex_unlock(&chunk
->lock
);
556 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials(
557 struct lttng_trace_chunk
*chunk
,
558 const struct lttng_credentials
*user_credentials
)
560 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
561 const struct chunk_credentials credentials
= {
562 .user
= *user_credentials
,
563 .use_current_user
= false,
566 pthread_mutex_lock(&chunk
->lock
);
567 if (chunk
->credentials
.is_set
) {
568 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
571 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
573 pthread_mutex_unlock(&chunk
->lock
);
578 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials_current_user(
579 struct lttng_trace_chunk
*chunk
)
581 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
582 const struct chunk_credentials credentials
= {
583 .use_current_user
= true,
586 pthread_mutex_lock(&chunk
->lock
);
587 if (chunk
->credentials
.is_set
) {
588 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
591 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
593 pthread_mutex_unlock(&chunk
->lock
);
599 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_owner(
600 struct lttng_trace_chunk
*chunk
,
601 struct lttng_directory_handle
*session_output_directory
)
604 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
605 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
606 bool reference_acquired
;
608 pthread_mutex_lock(&chunk
->lock
);
609 if (chunk
->mode
.is_set
) {
610 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
613 if (!chunk
->credentials
.is_set
) {
615 * Fatal error, credentials must be set before a
616 * directory is created.
618 ERR("Credentials of trace chunk are unset: refusing to set session output directory");
619 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
625 * A nameless chunk does not need its own output directory.
626 * The session's output directory will be used.
628 ret
= lttng_directory_handle_create_subdirectory_as_user(
629 session_output_directory
,
632 !chunk
->credentials
.value
.use_current_user
?
633 &chunk
->credentials
.value
.user
: NULL
);
635 PERROR("Failed to create chunk output directory \"%s\"",
637 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
641 chunk_directory_handle
= lttng_directory_handle_create_from_handle(
643 session_output_directory
);
644 if (!chunk_directory_handle
) {
645 /* The function already logs on all error paths. */
646 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
649 chunk
->chunk_directory
= chunk_directory_handle
;
650 chunk_directory_handle
= NULL
;
651 reference_acquired
= lttng_directory_handle_get(
652 session_output_directory
);
653 assert(reference_acquired
);
654 chunk
->session_output_directory
= session_output_directory
;
655 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
657 pthread_mutex_unlock(&chunk
->lock
);
662 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
663 struct lttng_trace_chunk
*chunk
,
664 struct lttng_directory_handle
*chunk_directory
)
666 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
667 bool reference_acquired
;
669 pthread_mutex_lock(&chunk
->lock
);
670 if (chunk
->mode
.is_set
) {
671 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
674 if (!chunk
->credentials
.is_set
) {
675 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
676 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
679 reference_acquired
= lttng_directory_handle_get(chunk_directory
);
680 assert(reference_acquired
);
681 chunk
->chunk_directory
= chunk_directory
;
682 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
684 pthread_mutex_unlock(&chunk
->lock
);
689 enum lttng_trace_chunk_status
lttng_trace_chunk_borrow_chunk_directory_handle(
690 struct lttng_trace_chunk
*chunk
,
691 const struct lttng_directory_handle
**handle
)
693 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
695 pthread_mutex_lock(&chunk
->lock
);
696 if (!chunk
->chunk_directory
) {
697 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
701 *handle
= chunk
->chunk_directory
;
703 pthread_mutex_unlock(&chunk
->lock
);
707 /* Add a top-level directory to the trace chunk if it was previously unknown. */
709 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
710 const char *new_path
)
714 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
715 &chunk
->top_level_directories
);
716 const char *new_path_separator_pos
= strchr(new_path
, '/');
717 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
718 new_path_separator_pos
- new_path
: strlen(new_path
);
720 for (i
= 0; i
< count
; i
++) {
721 const char *path
= lttng_dynamic_pointer_array_get_pointer(
722 &chunk
->top_level_directories
, i
);
723 const ptrdiff_t path_top_level_len
= strlen(path
);
725 if (path_top_level_len
!= new_path_top_level_len
) {
728 if (!strncmp(path
, new_path
, path_top_level_len
)) {
735 char *copy
= lttng_strndup(new_path
, new_path_top_level_len
);
737 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
738 new_path
, chunk
->name
? : "(unnamed)");
740 PERROR("Failed to copy path");
744 ret
= lttng_dynamic_pointer_array_add_pointer(
745 &chunk
->top_level_directories
, copy
);
747 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
757 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
758 struct lttng_trace_chunk
*chunk
,
762 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
764 DBG("Creating trace chunk subdirectory \"%s\"", path
);
765 pthread_mutex_lock(&chunk
->lock
);
766 if (!chunk
->credentials
.is_set
) {
768 * Fatal error, credentials must be set before a
769 * directory is created.
771 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
773 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
776 if (!chunk
->mode
.is_set
||
777 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
778 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
780 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
783 if (!chunk
->chunk_directory
) {
784 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
786 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
790 ERR("Refusing to create absolute trace chunk directory \"%s\"",
792 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
795 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
796 chunk
->chunk_directory
, path
,
798 chunk
->credentials
.value
.use_current_user
?
799 NULL
: &chunk
->credentials
.value
.user
);
801 PERROR("Failed to create trace chunk subdirectory \"%s\"",
803 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
806 ret
= add_top_level_directory_unique(chunk
, path
);
808 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
812 pthread_mutex_unlock(&chunk
->lock
);
817 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
818 struct lttng_trace_chunk
*chunk
, const char *file_path
,
819 int flags
, mode_t mode
, int *out_fd
, bool expect_no_file
)
822 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
824 DBG("Opening trace chunk file \"%s\"", file_path
);
825 pthread_mutex_lock(&chunk
->lock
);
826 if (!chunk
->credentials
.is_set
) {
828 * Fatal error, credentials must be set before a
831 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
833 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
836 if (!chunk
->chunk_directory
) {
837 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
839 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
842 ret
= lttng_directory_handle_open_file_as_user(
843 chunk
->chunk_directory
, file_path
, flags
, mode
,
844 chunk
->credentials
.value
.use_current_user
?
845 NULL
: &chunk
->credentials
.value
.user
);
847 if (errno
== ENOENT
&& expect_no_file
) {
848 status
= LTTNG_TRACE_CHUNK_STATUS_NO_FILE
;
850 PERROR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
851 file_path
, flags
, (int) mode
);
852 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
858 pthread_mutex_unlock(&chunk
->lock
);
863 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
864 const char *file_path
)
867 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
869 DBG("Unlinking trace chunk file \"%s\"", file_path
);
870 pthread_mutex_lock(&chunk
->lock
);
871 if (!chunk
->credentials
.is_set
) {
873 * Fatal error, credentials must be set before a
874 * directory is created.
876 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
878 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
881 if (!chunk
->chunk_directory
) {
882 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
884 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
887 ret
= lttng_directory_handle_unlink_file_as_user(
888 chunk
->chunk_directory
, file_path
,
889 chunk
->credentials
.value
.use_current_user
?
890 NULL
: &chunk
->credentials
.value
.user
);
892 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
896 pthread_mutex_unlock(&chunk
->lock
);
901 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
)
904 char *directory_to_rename
= NULL
;
905 bool free_directory_to_rename
= false;
906 char *archived_chunk_name
= NULL
;
907 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
908 const time_t creation_timestamp
=
909 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
910 const time_t close_timestamp
=
911 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
912 struct lttng_directory_handle
*archived_chunks_directory
= NULL
;
914 if (!trace_chunk
->mode
.is_set
||
915 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
916 !trace_chunk
->session_output_directory
) {
918 * This command doesn't need to run if the output is remote
919 * or if the trace chunk is not owned by this process.
924 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
925 assert(!trace_chunk
->name_overridden
);
928 * The fist trace chunk of a session is directly output to the
929 * session's output folder. In this case, the top level directories
930 * must be moved to a temporary folder before that temporary directory
931 * is renamed to match the chunk's name.
934 struct lttng_directory_handle
*temporary_rename_directory
=
936 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
937 &trace_chunk
->top_level_directories
);
939 ret
= lttng_directory_handle_create_subdirectory_as_user(
940 trace_chunk
->session_output_directory
,
941 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
943 !trace_chunk
->credentials
.value
.use_current_user
?
944 &trace_chunk
->credentials
.value
.user
: NULL
);
946 PERROR("Failed to create temporary trace chunk rename directory \"%s\"",
947 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
);
950 temporary_rename_directory
= lttng_directory_handle_create_from_handle(
951 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
952 trace_chunk
->session_output_directory
);
953 if (!temporary_rename_directory
) {
954 ERR("Failed to get handle to temporary trace chunk rename directory");
958 for (i
= 0; i
< count
; i
++) {
959 const char *top_level_name
=
960 lttng_dynamic_pointer_array_get_pointer(
961 &trace_chunk
->top_level_directories
, i
);
963 ret
= lttng_directory_handle_rename_as_user(
964 trace_chunk
->session_output_directory
,
966 temporary_rename_directory
,
968 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
970 &trace_chunk
->credentials
.value
.user
);
972 PERROR("Failed to move \"%s\" to temporary trace chunk rename directory",
974 lttng_directory_handle_put(
975 temporary_rename_directory
);
979 lttng_directory_handle_put(temporary_rename_directory
);
980 directory_to_rename
= DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
;
981 free_directory_to_rename
= false;
983 directory_to_rename
= generate_chunk_name(chunk_id
,
984 creation_timestamp
, NULL
);
985 if (!directory_to_rename
) {
986 ERR("Failed to generate initial trace chunk name while renaming trace chunk");
989 free_directory_to_rename
= true;
992 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
994 if (!archived_chunk_name
) {
995 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
999 ret
= lttng_directory_handle_create_subdirectory_as_user(
1000 trace_chunk
->session_output_directory
,
1001 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1003 !trace_chunk
->credentials
.value
.use_current_user
?
1004 &trace_chunk
->credentials
.value
.user
:
1007 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
1008 "\" directory for archived trace chunks");
1012 archived_chunks_directory
= lttng_directory_handle_create_from_handle(
1013 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1014 trace_chunk
->session_output_directory
);
1015 if (!archived_chunks_directory
) {
1016 PERROR("Failed to get handle to archived trace chunks directory");
1020 ret
= lttng_directory_handle_rename_as_user(
1021 trace_chunk
->session_output_directory
,
1022 directory_to_rename
,
1023 archived_chunks_directory
,
1024 archived_chunk_name
,
1025 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
1027 &trace_chunk
->credentials
.value
.user
);
1029 PERROR("Failed to rename folder \"%s\" to \"%s\"",
1030 directory_to_rename
, archived_chunk_name
);
1034 lttng_directory_handle_put(archived_chunks_directory
);
1035 free(archived_chunk_name
);
1036 if (free_directory_to_rename
) {
1037 free(directory_to_rename
);
1042 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
1043 struct lttng_trace_chunk
*chunk
,
1044 enum lttng_trace_chunk_command_type
*command_type
)
1046 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1048 pthread_mutex_lock(&chunk
->lock
);
1049 if (chunk
->close_command
.is_set
) {
1050 *command_type
= chunk
->close_command
.value
;
1051 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1053 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1055 pthread_mutex_unlock(&chunk
->lock
);
1060 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
1061 struct lttng_trace_chunk
*chunk
,
1062 enum lttng_trace_chunk_command_type close_command
)
1064 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1066 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
1067 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
1068 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1072 pthread_mutex_lock(&chunk
->lock
);
1073 if (chunk
->close_command
.is_set
) {
1074 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
1075 close_command_names
[chunk
->close_command
.value
],
1076 close_command_names
[close_command
]);
1078 DBG("Setting trace chunk close command to \"%s\"",
1079 close_command_names
[close_command
]);
1081 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1082 pthread_mutex_unlock(&chunk
->lock
);
1088 const char *lttng_trace_chunk_command_type_get_name(
1089 enum lttng_trace_chunk_command_type command
)
1092 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1093 return "move to completed trace chunk folder";
1100 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1102 return urcu_ref_get_unless_zero(&chunk
->ref
);
1106 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1108 struct lttng_trace_chunk_registry_element
*element
=
1109 container_of(node
, typeof(*element
), rcu_node
);
1111 lttng_trace_chunk_fini(&element
->chunk
);
1116 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1118 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1121 if (chunk
->close_command
.is_set
) {
1122 close_command_funcs
[chunk
->close_command
.value
](chunk
);
1125 if (chunk
->in_registry_element
) {
1126 struct lttng_trace_chunk_registry_element
*element
;
1128 element
= container_of(chunk
, typeof(*element
), chunk
);
1129 if (element
->registry
) {
1131 cds_lfht_del(element
->registry
->ht
,
1132 &element
->trace_chunk_registry_ht_node
);
1134 call_rcu(&element
->rcu_node
,
1135 free_lttng_trace_chunk_registry_element
);
1137 /* Never published, can be free'd immediately. */
1138 free_lttng_trace_chunk_registry_element(
1139 &element
->rcu_node
);
1142 /* Not RCU-protected, free immediately. */
1143 lttng_trace_chunk_fini(chunk
);
1149 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1154 assert(chunk
->ref
.refcount
);
1155 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1159 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1161 struct lttng_trace_chunk_registry
*registry
;
1163 registry
= zmalloc(sizeof(*registry
));
1168 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1169 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1170 if (!registry
->ht
) {
1176 lttng_trace_chunk_registry_destroy(registry
);
1181 void lttng_trace_chunk_registry_destroy(
1182 struct lttng_trace_chunk_registry
*registry
)
1188 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1195 struct lttng_trace_chunk_registry_element
*
1196 lttng_trace_chunk_registry_element_create_from_chunk(
1197 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1199 struct lttng_trace_chunk_registry_element
*element
=
1200 zmalloc(sizeof(*element
));
1205 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1206 element
->session_id
= session_id
;
1208 element
->chunk
= *chunk
;
1209 lttng_trace_chunk_init(&element
->chunk
);
1210 if (chunk
->session_output_directory
) {
1211 /* Transferred ownership. */
1212 element
->chunk
.session_output_directory
=
1213 chunk
->session_output_directory
;
1214 chunk
->session_output_directory
= NULL
;
1216 if (chunk
->chunk_directory
) {
1217 /* Transferred ownership. */
1218 element
->chunk
.chunk_directory
= chunk
->chunk_directory
;
1219 chunk
->chunk_directory
= NULL
;
1222 * The original chunk becomes invalid; the name attribute is transferred
1223 * to the new chunk instance.
1226 element
->chunk
.in_registry_element
= true;
1232 struct lttng_trace_chunk
*
1233 lttng_trace_chunk_registry_publish_chunk(
1234 struct lttng_trace_chunk_registry
*registry
,
1235 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
1237 struct lttng_trace_chunk_registry_element
*element
;
1238 unsigned long element_hash
;
1240 pthread_mutex_lock(&chunk
->lock
);
1241 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
1243 pthread_mutex_unlock(&chunk
->lock
);
1248 * chunk is now invalid, the only valid operation is a 'put' from the
1252 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
1256 struct cds_lfht_node
*published_node
;
1257 struct lttng_trace_chunk
*published_chunk
;
1258 struct lttng_trace_chunk_registry_element
*published_element
;
1260 published_node
= cds_lfht_add_unique(registry
->ht
,
1262 lttng_trace_chunk_registry_element_match
,
1264 &element
->trace_chunk_registry_ht_node
);
1265 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
1266 /* Successfully published the new element. */
1267 element
->registry
= registry
;
1268 /* Acquire a reference for the caller. */
1269 if (lttng_trace_chunk_get(&element
->chunk
)) {
1273 * Another thread concurrently unpublished the
1274 * trace chunk. This is currently unexpected.
1276 * Re-attempt to publish.
1278 ERR("Attempt to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
1284 * An equivalent trace chunk was published before this trace
1285 * chunk. Attempt to acquire a reference to the one that was
1286 * already published and release the reference to the copy we
1287 * created if successful.
1289 published_element
= container_of(published_node
,
1290 typeof(*published_element
),
1291 trace_chunk_registry_ht_node
);
1292 published_chunk
= &published_element
->chunk
;
1293 if (lttng_trace_chunk_get(published_chunk
)) {
1294 lttng_trace_chunk_put(&element
->chunk
);
1295 element
= published_element
;
1299 * A reference to the previously published trace chunk could not
1300 * be acquired. Hence, retry to publish our copy of the trace
1306 return element
? &element
->chunk
: NULL
;
1310 * Note that the caller must be registered as an RCU thread.
1311 * However, it does not need to hold the RCU read lock. The RCU read lock is
1312 * acquired to perform the look-up in the registry's hash table and held until
1313 * after a reference to the "found" trace chunk is acquired.
1315 * IOW, holding a reference guarantees the existence of the object for the
1319 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
1320 const struct lttng_trace_chunk_registry
*registry
,
1321 uint64_t session_id
, uint64_t *chunk_id
)
1323 const struct lttng_trace_chunk_registry_element target_element
= {
1324 .chunk
.id
.is_set
= !!chunk_id
,
1325 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
1326 .session_id
= session_id
,
1328 const unsigned long element_hash
=
1329 lttng_trace_chunk_registry_element_hash(
1331 struct cds_lfht_node
*published_node
;
1332 struct lttng_trace_chunk_registry_element
*published_element
;
1333 struct lttng_trace_chunk
*published_chunk
= NULL
;
1334 struct cds_lfht_iter iter
;
1337 cds_lfht_lookup(registry
->ht
,
1339 lttng_trace_chunk_registry_element_match
,
1342 published_node
= cds_lfht_iter_get_node(&iter
);
1343 if (!published_node
) {
1347 published_element
= container_of(published_node
,
1348 typeof(*published_element
),
1349 trace_chunk_registry_ht_node
);
1350 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
1351 published_chunk
= &published_element
->chunk
;
1355 return published_chunk
;
1359 struct lttng_trace_chunk
*
1360 lttng_trace_chunk_registry_find_chunk(
1361 const struct lttng_trace_chunk_registry
*registry
,
1362 uint64_t session_id
, uint64_t chunk_id
)
1364 return _lttng_trace_chunk_registry_find_chunk(registry
,
1365 session_id
, &chunk_id
);
1369 int lttng_trace_chunk_registry_chunk_exists(
1370 const struct lttng_trace_chunk_registry
*registry
,
1371 uint64_t session_id
, uint64_t chunk_id
, bool *chunk_exists
)
1374 const struct lttng_trace_chunk_registry_element target_element
= {
1375 .chunk
.id
.is_set
= true,
1376 .chunk
.id
.value
= chunk_id
,
1377 .session_id
= session_id
,
1379 const unsigned long element_hash
=
1380 lttng_trace_chunk_registry_element_hash(
1382 struct cds_lfht_node
*published_node
;
1383 struct cds_lfht_iter iter
;
1386 cds_lfht_lookup(registry
->ht
,
1388 lttng_trace_chunk_registry_element_match
,
1391 published_node
= cds_lfht_iter_get_node(&iter
);
1392 if (!published_node
) {
1393 *chunk_exists
= false;
1397 *chunk_exists
= !cds_lfht_is_node_deleted(published_node
);
1404 struct lttng_trace_chunk
*
1405 lttng_trace_chunk_registry_find_anonymous_chunk(
1406 const struct lttng_trace_chunk_registry
*registry
,
1407 uint64_t session_id
)
1409 return _lttng_trace_chunk_registry_find_chunk(registry
,
1413 unsigned int lttng_trace_chunk_registry_put_each_chunk(
1414 struct lttng_trace_chunk_registry
*registry
)
1416 struct cds_lfht_iter iter
;
1417 struct lttng_trace_chunk_registry_element
*chunk_element
;
1418 unsigned int trace_chunks_left
= 0;
1420 DBG("Releasing trace chunk registry to all trace chunks");
1422 cds_lfht_for_each_entry(registry
->ht
,
1423 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
1424 const char *chunk_id_str
= "none";
1425 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
1427 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
1428 if (chunk_element
->chunk
.id
.is_set
) {
1431 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
1433 chunk_element
->chunk
.id
.value
);
1434 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
1435 chunk_id_str
= "formatting error";
1437 chunk_id_str
= chunk_id_buf
;
1441 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
1442 "chunk_id = %s, name = \"%s\", status = %s",
1443 chunk_element
->session_id
,
1445 chunk_element
->chunk
.name
? : "none",
1446 chunk_element
->chunk
.close_command
.is_set
?
1448 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
1449 lttng_trace_chunk_put(&chunk_element
->chunk
);
1450 trace_chunks_left
++;
1453 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
1456 return trace_chunks_left
;