2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/compat/directory-handle.h>
9 #include <common/credentials.h>
10 #include <common/defaults.h>
11 #include <common/dynamic-array.h>
12 #include <common/error.h>
13 #include <common/fd-tracker/fd-tracker.h>
14 #include <common/fd-tracker/utils.h>
15 #include <common/fs-handle-internal.h>
16 #include <common/hashtable/hashtable.h>
17 #include <common/hashtable/utils.h>
18 #include <common/optional.h>
19 #include <common/string-utils/format.h>
20 #include <common/time.h>
21 #include <common/trace-chunk-registry.h>
22 #include <common/trace-chunk.h>
23 #include <common/utils.h>
24 #include <lttng/constant.h>
30 #include <urcu/rculfhash.h>
34 * Two ISO 8601-compatible timestamps, separated by a hypen, followed an
35 * index, i.e. <start-iso-8601>-<end-iso-8601>-<id-uint64_t>.
37 #define GENERATED_CHUNK_NAME_LEN (2 * sizeof("YYYYmmddTHHMMSS+HHMM") + MAX_INT_DEC_LEN(uint64_t))
38 #define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
40 enum trace_chunk_mode
{
41 TRACE_CHUNK_MODE_USER
,
42 TRACE_CHUNK_MODE_OWNER
,
46 * Callback to invoke on release of a trace chunk. Note that there is no
47 * need to 'lock' the trace chunk during the execution of these callbacks
48 * since only one thread may access a chunk during its destruction (the last
49 * to release its reference to the chunk).
51 typedef int (*chunk_command
)(struct lttng_trace_chunk
*trace_chunk
);
53 /* Move a completed trace chunk to the 'completed' trace archive folder. */
55 int lttng_trace_chunk_move_to_completed_post_release(struct lttng_trace_chunk
*trace_chunk
);
58 int lttng_trace_chunk_no_operation(struct lttng_trace_chunk
*trace_chunk
);
59 /* Unlink old chunk files. */
61 int lttng_trace_chunk_delete_post_release(struct lttng_trace_chunk
*trace_chunk
);
63 enum lttng_trace_chunk_status
lttng_trace_chunk_rename_path_no_lock(
64 struct lttng_trace_chunk
*chunk
, const char *path
);
66 struct chunk_credentials
{
67 bool use_current_user
;
68 struct lttng_credentials user
;
72 * NOTE: Make sure to update:
73 * - lttng_trace_chunk_copy(),
74 * - lttng_trace_chunk_registry_element_create_from_chunk()
75 * if you modify this structure.
77 struct lttng_trace_chunk
{
80 LTTNG_OPTIONAL(enum trace_chunk_mode
) mode
;
82 * First-level directories created within the trace chunk.
83 * Elements are of type 'char *'.
85 * Only used by _owner_ mode chunks.
87 struct lttng_dynamic_pointer_array top_level_directories
;
89 * All files contained within the trace chunk.
90 * Array of paths (char *).
92 struct lttng_dynamic_pointer_array files
;
93 /* Is contained within an lttng_trace_chunk_registry_element? */
94 bool in_registry_element
;
98 /* An unset id means the chunk is anonymous. */
99 LTTNG_OPTIONAL(uint64_t) id
;
102 * The creation and close timestamps are NOT monotonic.
103 * They must not be used in context were monotonicity is required.
105 LTTNG_OPTIONAL(time_t) timestamp_creation
;
106 LTTNG_OPTIONAL(time_t) timestamp_close
;
108 LTTNG_OPTIONAL(struct chunk_credentials
) credentials
;
109 struct lttng_directory_handle
*session_output_directory
;
110 struct lttng_directory_handle
*chunk_directory
;
111 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
;
113 * fd_tracker instance through which file descriptors should be
116 * An fd_tracker always outlives any trace chunk; there is no
117 * need to perform any reference counting of that object.
119 struct fd_tracker
*fd_tracker
;
122 /* A trace chunk is uniquely identified by its (session id, chunk id) tuple. */
123 struct lttng_trace_chunk_registry_element
{
124 struct lttng_trace_chunk chunk
;
126 /* Weak and only set when added. */
127 struct lttng_trace_chunk_registry
*registry
;
128 struct cds_lfht_node trace_chunk_registry_ht_node
;
129 /* call_rcu delayed reclaim. */
130 struct rcu_head rcu_node
;
133 struct lttng_trace_chunk_registry
{
137 struct fs_handle_untracked
{
138 struct fs_handle parent
;
141 struct lttng_directory_handle
*directory_handle
;
147 int fs_handle_untracked_get_fd(struct fs_handle
*handle
);
149 void fs_handle_untracked_put_fd(struct fs_handle
*handle
);
151 int fs_handle_untracked_unlink(struct fs_handle
*handle
);
153 int fs_handle_untracked_close(struct fs_handle
*handle
);
156 char *close_command_names
[] = {
157 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
158 "move to completed chunk folder",
159 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
] =
161 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
] =
166 chunk_command close_command_post_release_funcs
[] = {
167 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
168 lttng_trace_chunk_move_to_completed_post_release
,
169 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
] =
170 lttng_trace_chunk_no_operation
,
171 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
] =
172 lttng_trace_chunk_delete_post_release
,
176 struct fs_handle
*fs_handle_untracked_create(
177 struct lttng_directory_handle
*directory_handle
,
181 struct fs_handle_untracked
*handle
= NULL
;
182 bool reference_acquired
;
183 char *path_copy
= strdup(path
);
187 PERROR("Failed to copy file path while creating untracked filesystem handle");
191 handle
= zmalloc(sizeof(typeof(*handle
)));
193 PERROR("Failed to allocate untracked filesystem handle");
197 handle
->parent
= (typeof(handle
->parent
)) {
198 .get_fd
= fs_handle_untracked_get_fd
,
199 .put_fd
= fs_handle_untracked_put_fd
,
200 .unlink
= fs_handle_untracked_unlink
,
201 .close
= fs_handle_untracked_close
,
205 reference_acquired
= lttng_directory_handle_get(directory_handle
);
206 assert(reference_acquired
);
207 handle
->location
.directory_handle
= directory_handle
;
208 /* Ownership is transferred. */
209 handle
->location
.path
= path_copy
;
213 return handle
? &handle
->parent
: NULL
;
217 int fs_handle_untracked_get_fd(struct fs_handle
*_handle
)
219 struct fs_handle_untracked
*handle
= container_of(
220 _handle
, struct fs_handle_untracked
, parent
);
226 void fs_handle_untracked_put_fd(struct fs_handle
*_handle
)
232 int fs_handle_untracked_unlink(struct fs_handle
*_handle
)
234 struct fs_handle_untracked
*handle
= container_of(
235 _handle
, struct fs_handle_untracked
, parent
);
237 return lttng_directory_handle_unlink_file(
238 handle
->location
.directory_handle
,
239 handle
->location
.path
);
243 void fs_handle_untracked_destroy(struct fs_handle_untracked
*handle
)
245 lttng_directory_handle_put(handle
->location
.directory_handle
);
246 free(handle
->location
.path
);
251 int fs_handle_untracked_close(struct fs_handle
*_handle
)
253 struct fs_handle_untracked
*handle
= container_of(
254 _handle
, struct fs_handle_untracked
, parent
);
255 int ret
= close(handle
->fd
);
257 fs_handle_untracked_destroy(handle
);
262 bool lttng_trace_chunk_registry_element_equals(
263 const struct lttng_trace_chunk_registry_element
*a
,
264 const struct lttng_trace_chunk_registry_element
*b
)
266 if (a
->session_id
!= b
->session_id
) {
269 if (a
->chunk
.id
.is_set
!= b
->chunk
.id
.is_set
) {
272 if (a
->chunk
.id
.is_set
&& a
->chunk
.id
.value
!= b
->chunk
.id
.value
) {
281 int lttng_trace_chunk_registry_element_match(struct cds_lfht_node
*node
,
284 const struct lttng_trace_chunk_registry_element
*element_a
, *element_b
;
286 element_a
= (const struct lttng_trace_chunk_registry_element
*) key
;
287 element_b
= caa_container_of(node
, typeof(*element_b
),
288 trace_chunk_registry_ht_node
);
289 return lttng_trace_chunk_registry_element_equals(element_a
, element_b
);
293 unsigned long lttng_trace_chunk_registry_element_hash(
294 const struct lttng_trace_chunk_registry_element
*element
)
296 unsigned long hash
= hash_key_u64(&element
->session_id
,
299 if (element
->chunk
.id
.is_set
) {
300 hash
^= hash_key_u64(&element
->chunk
.id
.value
, lttng_ht_seed
);
307 char *generate_chunk_name(uint64_t chunk_id
, time_t creation_timestamp
,
308 const time_t *close_timestamp
)
311 char *new_name
= NULL
;
312 char start_datetime
[ISO8601_STR_LEN
] = {};
313 /* Add 1 for a '-' prefix. */
314 char end_datetime_suffix
[ISO8601_STR_LEN
+ 1] = {};
316 ret
= time_to_iso8601_str(
318 start_datetime
, sizeof(start_datetime
));
320 ERR("Failed to format trace chunk start date time");
323 if (close_timestamp
) {
324 *end_datetime_suffix
= '-';
325 ret
= time_to_iso8601_str(
327 end_datetime_suffix
+ 1,
328 sizeof(end_datetime_suffix
) - 1);
330 ERR("Failed to format trace chunk end date time");
334 new_name
= zmalloc(GENERATED_CHUNK_NAME_LEN
);
336 ERR("Failed to allocate buffer for automatically-generated trace chunk name");
339 ret
= snprintf(new_name
, GENERATED_CHUNK_NAME_LEN
, "%s%s-%" PRIu64
,
340 start_datetime
, end_datetime_suffix
, chunk_id
);
341 if (ret
>= GENERATED_CHUNK_NAME_LEN
|| ret
== -1) {
342 ERR("Failed to format trace chunk name");
353 void lttng_trace_chunk_init(struct lttng_trace_chunk
*chunk
)
355 urcu_ref_init(&chunk
->ref
);
356 pthread_mutex_init(&chunk
->lock
, NULL
);
357 lttng_dynamic_pointer_array_init(&chunk
->top_level_directories
, free
);
358 lttng_dynamic_pointer_array_init(&chunk
->files
, free
);
362 void lttng_trace_chunk_fini(struct lttng_trace_chunk
*chunk
)
364 if (chunk
->session_output_directory
) {
365 lttng_directory_handle_put(
366 chunk
->session_output_directory
);
367 chunk
->session_output_directory
= NULL
;
369 if (chunk
->chunk_directory
) {
370 lttng_directory_handle_put(chunk
->chunk_directory
);
371 chunk
->chunk_directory
= NULL
;
377 lttng_dynamic_pointer_array_reset(&chunk
->top_level_directories
);
378 lttng_dynamic_pointer_array_reset(&chunk
->files
);
379 pthread_mutex_destroy(&chunk
->lock
);
383 struct lttng_trace_chunk
*lttng_trace_chunk_allocate(void)
385 struct lttng_trace_chunk
*chunk
= NULL
;
387 chunk
= zmalloc(sizeof(*chunk
));
389 ERR("Failed to allocate trace chunk");
392 lttng_trace_chunk_init(chunk
);
398 struct lttng_trace_chunk
*lttng_trace_chunk_create_anonymous(void)
400 DBG("Creating anonymous trace chunk");
401 return lttng_trace_chunk_allocate();
405 struct lttng_trace_chunk
*lttng_trace_chunk_create(
406 uint64_t chunk_id
, time_t chunk_creation_time
, const char *path
)
408 struct lttng_trace_chunk
*chunk
;
409 char chunk_creation_datetime_buf
[16] = {};
410 const char *chunk_creation_datetime_str
= "(formatting error)";
411 struct tm timeinfo_buf
, *timeinfo
;
413 timeinfo
= localtime_r(&chunk_creation_time
, &timeinfo_buf
);
417 /* Don't fail because of this; it is only used for logging. */
418 strftime_ret
= strftime(chunk_creation_datetime_buf
,
419 sizeof(chunk_creation_datetime_buf
),
420 "%Y%m%d-%H%M%S", timeinfo
);
422 chunk_creation_datetime_str
=
423 chunk_creation_datetime_buf
;
427 DBG("Creating trace chunk: chunk_id = %" PRIu64
", creation time = %s",
428 chunk_id
, chunk_creation_datetime_str
);
429 chunk
= lttng_trace_chunk_allocate();
434 LTTNG_OPTIONAL_SET(&chunk
->id
, chunk_id
);
435 LTTNG_OPTIONAL_SET(&chunk
->timestamp_creation
, chunk_creation_time
);
437 chunk
->name
= generate_chunk_name(chunk_id
,
438 chunk_creation_time
, NULL
);
440 ERR("Failed to allocate trace chunk name storage");
445 chunk
->path
= strdup(path
);
451 chunk
->path
= strdup(chunk
->name
);
458 DBG("Chunk name set to \"%s\"", chunk
->name
? : "(none)");
462 lttng_trace_chunk_put(chunk
);
467 void lttng_trace_chunk_set_fd_tracker(struct lttng_trace_chunk
*chunk
,
468 struct fd_tracker
*fd_tracker
)
470 assert(!chunk
->session_output_directory
);
471 assert(!chunk
->chunk_directory
);
472 assert(lttng_dynamic_pointer_array_get_count(&chunk
->files
) == 0);
473 chunk
->fd_tracker
= fd_tracker
;
477 struct lttng_trace_chunk
*lttng_trace_chunk_copy(
478 struct lttng_trace_chunk
*source_chunk
)
480 struct lttng_trace_chunk
*new_chunk
= lttng_trace_chunk_allocate();
486 pthread_mutex_lock(&source_chunk
->lock
);
488 * A new chunk is always a user; it shall create no new trace
491 new_chunk
->mode
= (typeof(new_chunk
->mode
)) {
493 .value
= TRACE_CHUNK_MODE_USER
,
496 * top_level_directories is not copied as it is never used
497 * by _user_ mode chunks.
499 /* The new chunk is not part of a registry (yet, at least). */
500 new_chunk
->in_registry_element
= false;
501 new_chunk
->name_overridden
= source_chunk
->name_overridden
;
502 if (source_chunk
->name
) {
503 new_chunk
->name
= strdup(source_chunk
->name
);
504 if (!new_chunk
->name
) {
505 ERR("Failed to copy source trace chunk name in %s()",
510 if (source_chunk
->path
) {
511 new_chunk
->path
= strdup(source_chunk
->path
);
512 if (!new_chunk
->path
) {
513 ERR("Failed to copy source trace chunk path in %s()",
517 new_chunk
->id
= source_chunk
->id
;
518 new_chunk
->timestamp_creation
= source_chunk
->timestamp_creation
;
519 new_chunk
->timestamp_close
= source_chunk
->timestamp_close
;
520 new_chunk
->credentials
= source_chunk
->credentials
;
521 if (source_chunk
->session_output_directory
) {
522 const bool reference_acquired
= lttng_directory_handle_get(
523 source_chunk
->session_output_directory
);
525 assert(reference_acquired
);
526 new_chunk
->session_output_directory
=
527 source_chunk
->session_output_directory
;
529 if (source_chunk
->chunk_directory
) {
530 const bool reference_acquired
= lttng_directory_handle_get(
531 source_chunk
->chunk_directory
);
533 assert(reference_acquired
);
534 new_chunk
->chunk_directory
= source_chunk
->chunk_directory
;
536 new_chunk
->close_command
= source_chunk
->close_command
;
537 new_chunk
->fd_tracker
= source_chunk
->fd_tracker
;
538 pthread_mutex_unlock(&source_chunk
->lock
);
542 pthread_mutex_unlock(&source_chunk
->lock
);
543 lttng_trace_chunk_put(new_chunk
);
548 enum lttng_trace_chunk_status
lttng_trace_chunk_get_id(
549 struct lttng_trace_chunk
*chunk
, uint64_t *id
)
551 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
553 pthread_mutex_lock(&chunk
->lock
);
554 if (chunk
->id
.is_set
) {
555 *id
= chunk
->id
.value
;
557 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
559 pthread_mutex_unlock(&chunk
->lock
);
564 enum lttng_trace_chunk_status
lttng_trace_chunk_get_creation_timestamp(
565 struct lttng_trace_chunk
*chunk
, time_t *creation_ts
)
568 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
570 pthread_mutex_lock(&chunk
->lock
);
571 if (chunk
->timestamp_creation
.is_set
) {
572 *creation_ts
= chunk
->timestamp_creation
.value
;
574 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
576 pthread_mutex_unlock(&chunk
->lock
);
581 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_timestamp(
582 struct lttng_trace_chunk
*chunk
, time_t *close_ts
)
584 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
586 pthread_mutex_lock(&chunk
->lock
);
587 if (chunk
->timestamp_close
.is_set
) {
588 *close_ts
= chunk
->timestamp_close
.value
;
590 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
592 pthread_mutex_unlock(&chunk
->lock
);
597 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_timestamp(
598 struct lttng_trace_chunk
*chunk
, time_t close_ts
)
600 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
602 pthread_mutex_lock(&chunk
->lock
);
603 if (!chunk
->timestamp_creation
.is_set
) {
604 ERR("Failed to set trace chunk close timestamp: creation timestamp is unset");
605 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
610 * Note: we do not enforce that the closing timestamp be greater or
611 * equal to the begin timestamp. These timestamps are used for
612 * generating the chunk name and should only be used in context where
613 * the monotonicity of time is not important. The source of those
614 * timestamps is NOT monotonic and represent the system calendar time,
615 * also know as the wall time.
617 if (chunk
->timestamp_creation
.value
> close_ts
) {
618 WARN("Set trace chunk close timestamp: close timestamp is before creation timestamp, begin : %ld, close : %ld",
619 chunk
->timestamp_creation
.value
, close_ts
);
622 LTTNG_OPTIONAL_SET(&chunk
->timestamp_close
, close_ts
);
623 if (!chunk
->name_overridden
) {
625 chunk
->name
= generate_chunk_name(LTTNG_OPTIONAL_GET(chunk
->id
),
626 LTTNG_OPTIONAL_GET(chunk
->timestamp_creation
),
629 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
633 pthread_mutex_unlock(&chunk
->lock
);
638 enum lttng_trace_chunk_status
lttng_trace_chunk_get_name(
639 struct lttng_trace_chunk
*chunk
, const char **name
,
640 bool *name_overridden
)
642 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
644 pthread_mutex_lock(&chunk
->lock
);
645 if (name_overridden
) {
646 *name_overridden
= chunk
->name_overridden
;
649 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
654 pthread_mutex_unlock(&chunk
->lock
);
659 bool lttng_trace_chunk_get_name_overridden(struct lttng_trace_chunk
*chunk
)
661 bool name_overridden
;
663 pthread_mutex_lock(&chunk
->lock
);
664 name_overridden
= chunk
->name_overridden
;
665 pthread_mutex_unlock(&chunk
->lock
);
666 return name_overridden
;
670 bool is_valid_chunk_name(const char *name
)
678 len
= lttng_strnlen(name
, LTTNG_NAME_MAX
);
679 if (len
== 0 || len
== LTTNG_NAME_MAX
) {
683 if (strchr(name
, '/') || strchr(name
, '.')) {
691 enum lttng_trace_chunk_status
lttng_trace_chunk_override_name(
692 struct lttng_trace_chunk
*chunk
, const char *name
)
695 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
696 char *new_name
, *new_path
;
698 DBG("Override trace chunk name from %s to %s", chunk
->name
, name
);
699 if (!is_valid_chunk_name(name
)) {
700 ERR("Attempted to set an invalid name on a trace chunk: name = %s",
702 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
706 pthread_mutex_lock(&chunk
->lock
);
707 if (!chunk
->id
.is_set
) {
708 ERR("Attempted to set an override name on an anonymous trace chunk: name = %s",
710 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
714 new_name
= strdup(name
);
716 ERR("Failed to allocate new trace chunk name");
717 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
721 chunk
->name
= new_name
;
723 new_path
= strdup(name
);
725 ERR("Failed to allocate new trace chunk path");
726 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
730 chunk
->path
= new_path
;
732 chunk
->name_overridden
= true;
734 pthread_mutex_unlock(&chunk
->lock
);
740 enum lttng_trace_chunk_status
lttng_trace_chunk_rename_path_no_lock(
741 struct lttng_trace_chunk
*chunk
, const char *path
)
744 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
745 struct lttng_directory_handle
*rename_directory
= NULL
;
746 char *new_path
, *old_path
;
749 if (chunk
->name_overridden
) {
750 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
754 old_path
= chunk
->path
;
755 DBG("lttng_trace_chunk_rename_path from %s to %s", old_path
, path
);
757 if ((!old_path
&& !path
) ||
758 (old_path
&& path
&& !strcmp(old_path
, path
))) {
762 * Use chunk name as path if NULL path is specified.
768 /* Renaming from "" to "" is not accepted. */
769 if (path
[0] == '\0' && old_path
[0] == '\0') {
770 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
775 * If a rename is performed on a chunk for which the chunk_directory
776 * is not set (yet), or the session_output_directory is not set
777 * (interacting with a relay daemon), there is no rename to perform.
779 if (!chunk
->chunk_directory
||
780 !chunk
->session_output_directory
) {
784 if (old_path
&& old_path
[0] != '\0' && path
[0] != '\0') {
785 /* Rename chunk directory. */
786 ret
= lttng_directory_handle_rename_as_user(
787 chunk
->session_output_directory
,
789 chunk
->session_output_directory
,
791 LTTNG_OPTIONAL_GET(chunk
->credentials
).use_current_user
?
793 &chunk
->credentials
.value
.user
);
795 PERROR("Failed to move trace chunk directory \"%s\" to \"%s\"",
797 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
800 rename_directory
= chunk
->fd_tracker
?
801 fd_tracker_create_directory_handle_from_handle(
803 chunk
->session_output_directory
,
805 lttng_directory_handle_create_from_handle(
807 chunk
->session_output_directory
);
808 if (!rename_directory
) {
809 ERR("Failed to get handle to trace chunk rename directory");
810 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
814 /* Release old handle. */
815 lttng_directory_handle_put(chunk
->chunk_directory
);
817 * Transfer new handle reference to chunk as the current chunk
820 chunk
->chunk_directory
= rename_directory
;
821 rename_directory
= NULL
;
822 } else if (old_path
&& old_path
[0] == '\0') {
823 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
824 &chunk
->top_level_directories
);
826 ret
= lttng_directory_handle_create_subdirectory_as_user(
827 chunk
->session_output_directory
,
830 LTTNG_OPTIONAL_GET(chunk
->credentials
).use_current_user
?
832 &chunk
->credentials
.value
.user
);
834 PERROR("Failed to create trace chunk rename directory \"%s\"",
836 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
840 rename_directory
= lttng_directory_handle_create_from_handle(
841 path
, chunk
->session_output_directory
);
842 if (!rename_directory
) {
843 ERR("Failed to get handle to trace chunk rename directory");
844 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
848 /* Move toplevel directories. */
849 for (i
= 0; i
< count
; i
++) {
850 const char *top_level_name
=
851 lttng_dynamic_pointer_array_get_pointer(
852 &chunk
->top_level_directories
, i
);
854 ret
= lttng_directory_handle_rename_as_user(
855 chunk
->chunk_directory
,
859 LTTNG_OPTIONAL_GET(chunk
->credentials
).use_current_user
?
861 &chunk
->credentials
.value
.user
);
863 PERROR("Failed to move \"%s\" to trace chunk rename directory",
865 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
869 /* Release old handle. */
870 lttng_directory_handle_put(chunk
->chunk_directory
);
872 * Transfer new handle reference to chunk as the current chunk
875 chunk
->chunk_directory
= rename_directory
;
876 rename_directory
= NULL
;
877 } else if (old_path
) {
878 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
879 &chunk
->top_level_directories
);
880 const bool reference_acquired
= lttng_directory_handle_get(
881 chunk
->session_output_directory
);
883 assert(reference_acquired
);
884 rename_directory
= chunk
->session_output_directory
;
886 /* Move toplevel directories. */
887 for (i
= 0; i
< count
; i
++) {
888 const char *top_level_name
=
889 lttng_dynamic_pointer_array_get_pointer(
890 &chunk
->top_level_directories
, i
);
892 ret
= lttng_directory_handle_rename_as_user(
893 chunk
->chunk_directory
,
897 LTTNG_OPTIONAL_GET(chunk
->credentials
).use_current_user
?
899 &chunk
->credentials
.value
.user
);
901 PERROR("Failed to move \"%s\" to trace chunk rename directory",
903 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
907 /* Release old handle. */
908 lttng_directory_handle_put(chunk
->chunk_directory
);
910 * Transfer new handle reference to chunk as the current chunk
913 chunk
->chunk_directory
= rename_directory
;
914 rename_directory
= NULL
;
916 /* Remove old directory. */
917 status
= lttng_directory_handle_remove_subdirectory(
918 chunk
->session_output_directory
,
920 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
921 ERR("Error removing subdirectory '%s' file when deleting chunk",
926 /* Unexpected !old_path && !path. */
927 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
932 new_path
= strdup(path
);
934 ERR("Failed to allocate new trace chunk path");
935 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
939 chunk
->path
= new_path
;
941 lttng_directory_handle_put(rename_directory
);
946 enum lttng_trace_chunk_status
lttng_trace_chunk_rename_path(
947 struct lttng_trace_chunk
*chunk
, const char *path
)
950 enum lttng_trace_chunk_status status
;
952 pthread_mutex_lock(&chunk
->lock
);
953 status
= lttng_trace_chunk_rename_path_no_lock(chunk
, path
);
954 pthread_mutex_unlock(&chunk
->lock
);
960 enum lttng_trace_chunk_status
lttng_trace_chunk_get_credentials(
961 struct lttng_trace_chunk
*chunk
,
962 struct lttng_credentials
*credentials
)
964 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
966 pthread_mutex_lock(&chunk
->lock
);
967 if (chunk
->credentials
.is_set
) {
968 if (chunk
->credentials
.value
.use_current_user
) {
969 LTTNG_OPTIONAL_SET(&credentials
->uid
, geteuid());
970 LTTNG_OPTIONAL_SET(&credentials
->gid
, getegid());
972 *credentials
= chunk
->credentials
.value
.user
;
975 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
977 pthread_mutex_unlock(&chunk
->lock
);
982 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials(
983 struct lttng_trace_chunk
*chunk
,
984 const struct lttng_credentials
*user_credentials
)
986 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
987 const struct chunk_credentials credentials
= {
988 .user
= *user_credentials
,
989 .use_current_user
= false,
992 pthread_mutex_lock(&chunk
->lock
);
993 if (chunk
->credentials
.is_set
) {
994 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
997 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
999 pthread_mutex_unlock(&chunk
->lock
);
1004 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials_current_user(
1005 struct lttng_trace_chunk
*chunk
)
1007 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1008 const struct chunk_credentials credentials
= {
1009 .use_current_user
= true,
1012 pthread_mutex_lock(&chunk
->lock
);
1013 if (chunk
->credentials
.is_set
) {
1014 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1017 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
1019 pthread_mutex_unlock(&chunk
->lock
);
1025 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_owner(
1026 struct lttng_trace_chunk
*chunk
,
1027 struct lttng_directory_handle
*session_output_directory
)
1030 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1031 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1032 bool reference_acquired
;
1034 pthread_mutex_lock(&chunk
->lock
);
1035 if (chunk
->mode
.is_set
) {
1036 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
1039 if (!chunk
->credentials
.is_set
) {
1041 * Fatal error, credentials must be set before a
1042 * directory is created.
1044 ERR("Credentials of trace chunk are unset: refusing to set session output directory");
1045 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1048 if (chunk
->path
&& chunk
->path
[0] != '\0') {
1049 ret
= lttng_directory_handle_create_subdirectory_as_user(
1050 session_output_directory
,
1053 !chunk
->credentials
.value
.use_current_user
?
1054 &chunk
->credentials
.value
.user
: NULL
);
1056 PERROR("Failed to create chunk output directory \"%s\"",
1058 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1061 chunk_directory_handle
=
1063 fd_tracker_create_directory_handle_from_handle(
1065 session_output_directory
,
1067 lttng_directory_handle_create_from_handle(
1069 session_output_directory
);
1070 if (!chunk_directory_handle
) {
1071 /* The function already logs on all error paths. */
1072 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1077 * A nameless chunk does not need its own output directory.
1078 * The session's output directory will be used.
1080 reference_acquired
= lttng_directory_handle_get(
1081 session_output_directory
);
1083 assert(reference_acquired
);
1084 chunk_directory_handle
= session_output_directory
;
1086 chunk
->chunk_directory
= chunk_directory_handle
;
1087 chunk_directory_handle
= NULL
;
1088 reference_acquired
= lttng_directory_handle_get(
1089 session_output_directory
);
1090 assert(reference_acquired
);
1091 chunk
->session_output_directory
= session_output_directory
;
1092 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
1094 pthread_mutex_unlock(&chunk
->lock
);
1099 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
1100 struct lttng_trace_chunk
*chunk
,
1101 struct lttng_directory_handle
*chunk_directory
)
1103 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1104 bool reference_acquired
;
1106 pthread_mutex_lock(&chunk
->lock
);
1107 if (chunk
->mode
.is_set
) {
1108 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
1111 if (!chunk
->credentials
.is_set
) {
1112 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
1113 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1116 reference_acquired
= lttng_directory_handle_get(chunk_directory
);
1117 assert(reference_acquired
);
1118 chunk
->chunk_directory
= chunk_directory
;
1119 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
1121 pthread_mutex_unlock(&chunk
->lock
);
1126 enum lttng_trace_chunk_status
1127 lttng_trace_chunk_get_session_output_directory_handle(
1128 struct lttng_trace_chunk
*chunk
,
1129 struct lttng_directory_handle
**handle
)
1131 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1133 pthread_mutex_lock(&chunk
->lock
);
1134 if (!chunk
->session_output_directory
) {
1135 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1139 const bool reference_acquired
= lttng_directory_handle_get(
1140 chunk
->session_output_directory
);
1142 assert(reference_acquired
);
1143 *handle
= chunk
->session_output_directory
;
1146 pthread_mutex_unlock(&chunk
->lock
);
1151 enum lttng_trace_chunk_status
lttng_trace_chunk_borrow_chunk_directory_handle(
1152 struct lttng_trace_chunk
*chunk
,
1153 const struct lttng_directory_handle
**handle
)
1155 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1157 pthread_mutex_lock(&chunk
->lock
);
1158 if (!chunk
->chunk_directory
) {
1159 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1163 *handle
= chunk
->chunk_directory
;
1165 pthread_mutex_unlock(&chunk
->lock
);
1169 /* Add a top-level directory to the trace chunk if it was previously unknown. */
1171 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
1172 const char *new_path
)
1176 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
1177 &chunk
->top_level_directories
);
1178 const char *new_path_separator_pos
= strchr(new_path
, '/');
1179 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
1180 new_path_separator_pos
- new_path
: strlen(new_path
);
1182 for (i
= 0; i
< count
; i
++) {
1183 const char *path
= lttng_dynamic_pointer_array_get_pointer(
1184 &chunk
->top_level_directories
, i
);
1185 const ptrdiff_t path_top_level_len
= strlen(path
);
1187 if (path_top_level_len
!= new_path_top_level_len
) {
1190 if (!strncmp(path
, new_path
, path_top_level_len
)) {
1197 char *copy
= lttng_strndup(new_path
, new_path_top_level_len
);
1199 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
1200 new_path
, chunk
->name
? : "(unnamed)");
1202 PERROR("Failed to copy path");
1206 ret
= lttng_dynamic_pointer_array_add_pointer(
1207 &chunk
->top_level_directories
, copy
);
1209 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
1219 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
1220 struct lttng_trace_chunk
*chunk
,
1224 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1226 DBG("Creating trace chunk subdirectory \"%s\"", path
);
1227 pthread_mutex_lock(&chunk
->lock
);
1228 if (!chunk
->credentials
.is_set
) {
1230 * Fatal error, credentials must be set before a
1231 * directory is created.
1233 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
1235 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1238 if (!chunk
->mode
.is_set
||
1239 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
1240 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
1242 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
1245 if (!chunk
->chunk_directory
) {
1246 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
1248 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1252 ERR("Refusing to create absolute trace chunk directory \"%s\"",
1254 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1257 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
1258 chunk
->chunk_directory
, path
,
1260 chunk
->credentials
.value
.use_current_user
?
1261 NULL
: &chunk
->credentials
.value
.user
);
1263 PERROR("Failed to create trace chunk subdirectory \"%s\"",
1265 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1268 ret
= add_top_level_directory_unique(chunk
, path
);
1270 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1274 pthread_mutex_unlock(&chunk
->lock
);
1279 * TODO: Implement O(1) lookup.
1282 bool lttng_trace_chunk_find_file(struct lttng_trace_chunk
*chunk
,
1283 const char *path
, size_t *index
)
1287 count
= lttng_dynamic_pointer_array_get_count(&chunk
->files
);
1288 for (i
= 0; i
< count
; i
++) {
1289 const char *iter_path
=
1290 lttng_dynamic_pointer_array_get_pointer(
1292 if (!strcmp(iter_path
, path
)) {
1303 enum lttng_trace_chunk_status
lttng_trace_chunk_add_file(
1304 struct lttng_trace_chunk
*chunk
,
1309 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1311 if (lttng_trace_chunk_find_file(chunk
, path
, NULL
)) {
1312 return LTTNG_TRACE_CHUNK_STATUS_OK
;
1314 DBG("Adding new file \"%s\" to trace chunk \"%s\"",
1315 path
, chunk
->name
? : "(unnamed)");
1316 copy
= strdup(path
);
1318 PERROR("Failed to copy path");
1319 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1322 ret
= lttng_dynamic_pointer_array_add_pointer(
1323 &chunk
->files
, copy
);
1325 ERR("Allocation failure while adding file to a trace chunk");
1327 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1335 void lttng_trace_chunk_remove_file(
1336 struct lttng_trace_chunk
*chunk
,
1343 found
= lttng_trace_chunk_find_file(chunk
, path
, &index
);
1347 ret
= lttng_dynamic_pointer_array_remove_pointer(
1348 &chunk
->files
, index
);
1353 enum lttng_trace_chunk_status
_lttng_trace_chunk_open_fs_handle_locked(
1354 struct lttng_trace_chunk
*chunk
,
1355 const char *file_path
,
1358 struct fs_handle
**out_handle
,
1359 bool expect_no_file
)
1362 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1364 DBG("Opening trace chunk file \"%s\"", file_path
);
1365 if (!chunk
->credentials
.is_set
) {
1367 * Fatal error, credentials must be set before a
1370 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
1372 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1375 if (!chunk
->chunk_directory
) {
1376 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
1378 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1381 status
= lttng_trace_chunk_add_file(chunk
, file_path
);
1382 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1385 if (chunk
->fd_tracker
) {
1386 assert(chunk
->credentials
.value
.use_current_user
);
1387 *out_handle
= fd_tracker_open_fs_handle(chunk
->fd_tracker
,
1388 chunk
->chunk_directory
, file_path
, flags
, &mode
);
1389 ret
= *out_handle
? 0 : -1;
1391 ret
= lttng_directory_handle_open_file_as_user(
1392 chunk
->chunk_directory
, file_path
, flags
, mode
,
1393 chunk
->credentials
.value
.use_current_user
?
1395 &chunk
->credentials
.value
.user
);
1397 *out_handle
= fs_handle_untracked_create(
1398 chunk
->chunk_directory
, file_path
, ret
);
1400 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1406 if (errno
== ENOENT
&& expect_no_file
) {
1407 status
= LTTNG_TRACE_CHUNK_STATUS_NO_FILE
;
1409 PERROR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
1410 file_path
, flags
, (int) mode
);
1411 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1413 lttng_trace_chunk_remove_file(chunk
, file_path
);
1421 enum lttng_trace_chunk_status
lttng_trace_chunk_open_fs_handle(
1422 struct lttng_trace_chunk
*chunk
,
1423 const char *file_path
,
1426 struct fs_handle
**out_handle
,
1427 bool expect_no_file
)
1429 enum lttng_trace_chunk_status status
;
1431 pthread_mutex_lock(&chunk
->lock
);
1432 status
= _lttng_trace_chunk_open_fs_handle_locked(chunk
, file_path
,
1433 flags
, mode
, out_handle
, expect_no_file
);
1434 pthread_mutex_unlock(&chunk
->lock
);
1439 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
1440 struct lttng_trace_chunk
*chunk
,
1441 const char *file_path
,
1445 bool expect_no_file
)
1447 enum lttng_trace_chunk_status status
;
1448 struct fs_handle
*fs_handle
;
1450 pthread_mutex_lock(&chunk
->lock
);
1452 * Using this method is never valid when an fd_tracker is being
1453 * used since the resulting file descriptor would not be tracked.
1455 assert(!chunk
->fd_tracker
);
1456 status
= _lttng_trace_chunk_open_fs_handle_locked(chunk
, file_path
,
1457 flags
, mode
, &fs_handle
, expect_no_file
);
1458 pthread_mutex_unlock(&chunk
->lock
);
1460 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1461 *out_fd
= fs_handle_get_fd(fs_handle
);
1463 * Does not close the fd; we just "unbox" it from the fs_handle.
1465 fs_handle_untracked_destroy(container_of(
1466 fs_handle
, struct fs_handle_untracked
, parent
));
1473 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
1474 const char *file_path
)
1477 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1479 DBG("Unlinking trace chunk file \"%s\"", file_path
);
1480 pthread_mutex_lock(&chunk
->lock
);
1481 if (!chunk
->credentials
.is_set
) {
1483 * Fatal error, credentials must be set before a
1486 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
1488 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1491 if (!chunk
->chunk_directory
) {
1492 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
1494 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1497 ret
= lttng_directory_handle_unlink_file_as_user(
1498 chunk
->chunk_directory
, file_path
,
1499 chunk
->credentials
.value
.use_current_user
?
1500 NULL
: &chunk
->credentials
.value
.user
);
1502 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1505 lttng_trace_chunk_remove_file(chunk
, file_path
);
1507 pthread_mutex_unlock(&chunk
->lock
);
1512 int lttng_trace_chunk_remove_subdirectory_recursive(struct lttng_trace_chunk
*chunk
,
1516 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1518 DBG("Recursively removing trace chunk directory \"%s\"", path
);
1519 pthread_mutex_lock(&chunk
->lock
);
1520 if (!chunk
->credentials
.is_set
) {
1522 * Fatal error, credentials must be set before a
1523 * directory is removed.
1525 ERR("Credentials of trace chunk are unset: refusing to recursively remove directory \"%s\"",
1527 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1530 if (!chunk
->chunk_directory
) {
1531 ERR("Attempted to recursively remove trace chunk directory \"%s\" before setting the chunk output directory",
1533 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1536 ret
= lttng_directory_handle_remove_subdirectory_recursive_as_user(
1537 chunk
->chunk_directory
, path
,
1538 chunk
->credentials
.value
.use_current_user
?
1539 NULL
: &chunk
->credentials
.value
.user
,
1540 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
1542 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1546 pthread_mutex_unlock(&chunk
->lock
);
1551 int lttng_trace_chunk_move_to_completed_post_release(
1552 struct lttng_trace_chunk
*trace_chunk
)
1555 char *archived_chunk_name
= NULL
;
1556 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
1557 const time_t creation_timestamp
=
1558 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
1559 const time_t close_timestamp
=
1560 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
1561 struct lttng_directory_handle
*archived_chunks_directory
= NULL
;
1562 enum lttng_trace_chunk_status status
;
1564 if (!trace_chunk
->mode
.is_set
||
1565 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
1566 !trace_chunk
->session_output_directory
) {
1568 * This command doesn't need to run if the output is remote
1569 * or if the trace chunk is not owned by this process.
1574 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
1575 assert(!trace_chunk
->name_overridden
);
1576 assert(trace_chunk
->path
);
1578 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
1580 if (!archived_chunk_name
) {
1581 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
1586 ret
= lttng_directory_handle_create_subdirectory_as_user(
1587 trace_chunk
->session_output_directory
,
1588 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1590 !trace_chunk
->credentials
.value
.use_current_user
?
1591 &trace_chunk
->credentials
.value
.user
:
1594 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
1595 "\" directory for archived trace chunks");
1599 archived_chunks_directory
= trace_chunk
->fd_tracker
?
1600 fd_tracker_create_directory_handle_from_handle(
1601 trace_chunk
->fd_tracker
,
1602 trace_chunk
->session_output_directory
,
1603 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
) :
1604 lttng_directory_handle_create_from_handle(
1605 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1606 trace_chunk
->session_output_directory
);
1607 if (!archived_chunks_directory
) {
1608 PERROR("Failed to get handle to archived trace chunks directory");
1614 * Make sure chunk is renamed to old directory if not already done by
1615 * the creation of the next chunk. This happens if a rotation is
1616 * performed while tracing is stopped.
1618 if (!trace_chunk
->path
|| strcmp(trace_chunk
->path
,
1619 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
)) {
1620 status
= lttng_trace_chunk_rename_path_no_lock(trace_chunk
,
1621 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
1622 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1623 ERR("Failed to rename chunk to %s", DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
1629 ret
= lttng_directory_handle_rename_as_user(
1630 trace_chunk
->session_output_directory
,
1632 archived_chunks_directory
,
1633 archived_chunk_name
,
1634 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
1636 &trace_chunk
->credentials
.value
.user
);
1638 PERROR("Failed to rename folder \"%s\" to \"%s\"",
1640 archived_chunk_name
);
1644 lttng_directory_handle_put(archived_chunks_directory
);
1645 free(archived_chunk_name
);
1650 int lttng_trace_chunk_no_operation(struct lttng_trace_chunk
*trace_chunk
)
1656 int lttng_trace_chunk_delete_post_release_user(
1657 struct lttng_trace_chunk
*trace_chunk
)
1661 DBG("Trace chunk \"delete\" close command post-release (User)");
1663 /* Unlink all files. */
1664 while (lttng_dynamic_pointer_array_get_count(&trace_chunk
->files
) != 0) {
1665 enum lttng_trace_chunk_status status
;
1669 path
= lttng_dynamic_pointer_array_get_pointer(
1670 &trace_chunk
->files
, 0);
1671 DBG("Unlink file: %s", path
);
1672 status
= lttng_trace_chunk_unlink_file(trace_chunk
, path
);
1673 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1674 ERR("Error unlinking file '%s' when deleting chunk", path
);
1684 int lttng_trace_chunk_delete_post_release_owner(
1685 struct lttng_trace_chunk
*trace_chunk
)
1687 enum lttng_trace_chunk_status status
;
1691 ret
= lttng_trace_chunk_delete_post_release_user(trace_chunk
);
1696 DBG("Trace chunk \"delete\" close command post-release (Owner)");
1698 assert(trace_chunk
->session_output_directory
);
1699 assert(trace_chunk
->chunk_directory
);
1701 /* Remove empty directories. */
1702 count
= lttng_dynamic_pointer_array_get_count(
1703 &trace_chunk
->top_level_directories
);
1705 for (i
= 0; i
< count
; i
++) {
1706 const char *top_level_name
=
1707 lttng_dynamic_pointer_array_get_pointer(
1708 &trace_chunk
->top_level_directories
, i
);
1710 status
= lttng_trace_chunk_remove_subdirectory_recursive(trace_chunk
, top_level_name
);
1711 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1712 ERR("Error recursively removing subdirectory '%s' file when deleting chunk",
1719 lttng_directory_handle_put(trace_chunk
->chunk_directory
);
1720 trace_chunk
->chunk_directory
= NULL
;
1722 if (trace_chunk
->path
&& trace_chunk
->path
[0] != '\0') {
1723 status
= lttng_directory_handle_remove_subdirectory(
1724 trace_chunk
->session_output_directory
,
1726 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1727 ERR("Error removing subdirectory '%s' file when deleting chunk",
1733 free(trace_chunk
->path
);
1734 trace_chunk
->path
= NULL
;
1740 * For local files, session and consumer daemons all run the delete hook. The
1741 * consumer daemons have the list of files to unlink, and technically the
1742 * session daemon is the owner of the chunk. Unlink all files owned by each
1746 int lttng_trace_chunk_delete_post_release(
1747 struct lttng_trace_chunk
*trace_chunk
)
1749 if (!trace_chunk
->chunk_directory
) {
1753 if (trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
) {
1754 return lttng_trace_chunk_delete_post_release_owner(trace_chunk
);
1756 return lttng_trace_chunk_delete_post_release_user(trace_chunk
);
1761 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
1762 struct lttng_trace_chunk
*chunk
,
1763 enum lttng_trace_chunk_command_type
*command_type
)
1765 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1767 pthread_mutex_lock(&chunk
->lock
);
1768 if (chunk
->close_command
.is_set
) {
1769 *command_type
= chunk
->close_command
.value
;
1770 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1772 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1774 pthread_mutex_unlock(&chunk
->lock
);
1779 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
1780 struct lttng_trace_chunk
*chunk
,
1781 enum lttng_trace_chunk_command_type close_command
)
1783 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1785 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
1786 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
1787 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1791 pthread_mutex_lock(&chunk
->lock
);
1792 if (chunk
->close_command
.is_set
) {
1793 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
1794 close_command_names
[chunk
->close_command
.value
],
1795 close_command_names
[close_command
]);
1797 DBG("Setting trace chunk close command to \"%s\"",
1798 close_command_names
[close_command
]);
1801 * Unset close command for no-op for backward compatibility with relayd
1804 if (close_command
!= LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
1805 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1807 LTTNG_OPTIONAL_UNSET(&chunk
->close_command
);
1809 pthread_mutex_unlock(&chunk
->lock
);
1815 const char *lttng_trace_chunk_command_type_get_name(
1816 enum lttng_trace_chunk_command_type command
)
1819 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1820 return "move to completed trace chunk folder";
1821 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
:
1822 return "no operation";
1823 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
:
1831 bool lttng_trace_chunk_ids_equal(const struct lttng_trace_chunk
*chunk_a
,
1832 const struct lttng_trace_chunk
*chunk_b
)
1836 if (chunk_a
== chunk_b
) {
1841 if (!!chunk_a
^ !!chunk_b
) {
1845 if (chunk_a
->id
.is_set
^ chunk_a
->id
.is_set
) {
1846 /* One id is set and not the other, thus they are not equal. */
1850 if (!chunk_a
->id
.is_set
) {
1851 /* Both ids are unset. */
1854 equal
= chunk_a
->id
.value
== chunk_b
->id
.value
;
1862 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1864 return urcu_ref_get_unless_zero(&chunk
->ref
);
1868 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1870 struct lttng_trace_chunk_registry_element
*element
=
1871 container_of(node
, typeof(*element
), rcu_node
);
1873 lttng_trace_chunk_fini(&element
->chunk
);
1878 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1880 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1883 if (chunk
->close_command
.is_set
) {
1884 if (close_command_post_release_funcs
[
1885 chunk
->close_command
.value
](chunk
)) {
1886 ERR("Trace chunk post-release command %s has failed.",
1887 close_command_names
[chunk
->close_command
.value
]);
1891 if (chunk
->in_registry_element
) {
1892 struct lttng_trace_chunk_registry_element
*element
;
1894 element
= container_of(chunk
, typeof(*element
), chunk
);
1895 if (element
->registry
) {
1897 cds_lfht_del(element
->registry
->ht
,
1898 &element
->trace_chunk_registry_ht_node
);
1900 call_rcu(&element
->rcu_node
,
1901 free_lttng_trace_chunk_registry_element
);
1903 /* Never published, can be free'd immediately. */
1904 free_lttng_trace_chunk_registry_element(
1905 &element
->rcu_node
);
1908 /* Not RCU-protected, free immediately. */
1909 lttng_trace_chunk_fini(chunk
);
1915 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1920 assert(chunk
->ref
.refcount
);
1921 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1925 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1927 struct lttng_trace_chunk_registry
*registry
;
1929 registry
= zmalloc(sizeof(*registry
));
1934 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1935 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1936 if (!registry
->ht
) {
1942 lttng_trace_chunk_registry_destroy(registry
);
1947 void lttng_trace_chunk_registry_destroy(
1948 struct lttng_trace_chunk_registry
*registry
)
1954 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1961 struct lttng_trace_chunk_registry_element
*
1962 lttng_trace_chunk_registry_element_create_from_chunk(
1963 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1965 struct lttng_trace_chunk_registry_element
*element
=
1966 zmalloc(sizeof(*element
));
1971 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1972 element
->session_id
= session_id
;
1974 element
->chunk
= *chunk
;
1975 lttng_trace_chunk_init(&element
->chunk
);
1976 if (chunk
->session_output_directory
) {
1977 /* Transferred ownership. */
1978 element
->chunk
.session_output_directory
=
1979 chunk
->session_output_directory
;
1980 chunk
->session_output_directory
= NULL
;
1982 if (chunk
->chunk_directory
) {
1983 /* Transferred ownership. */
1984 element
->chunk
.chunk_directory
= chunk
->chunk_directory
;
1985 chunk
->chunk_directory
= NULL
;
1988 * The original chunk becomes invalid; the name and path attributes are
1989 * transferred to the new chunk instance.
1993 element
->chunk
.fd_tracker
= chunk
->fd_tracker
;
1994 element
->chunk
.in_registry_element
= true;
2000 struct lttng_trace_chunk
*
2001 lttng_trace_chunk_registry_publish_chunk(
2002 struct lttng_trace_chunk_registry
*registry
,
2003 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
2005 struct lttng_trace_chunk_registry_element
*element
;
2006 unsigned long element_hash
;
2008 pthread_mutex_lock(&chunk
->lock
);
2009 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
2011 pthread_mutex_unlock(&chunk
->lock
);
2016 * chunk is now invalid, the only valid operation is a 'put' from the
2020 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
2024 struct cds_lfht_node
*published_node
;
2025 struct lttng_trace_chunk
*published_chunk
;
2026 struct lttng_trace_chunk_registry_element
*published_element
;
2028 published_node
= cds_lfht_add_unique(registry
->ht
,
2030 lttng_trace_chunk_registry_element_match
,
2032 &element
->trace_chunk_registry_ht_node
);
2033 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
2034 /* Successfully published the new element. */
2035 element
->registry
= registry
;
2036 /* Acquire a reference for the caller. */
2037 if (lttng_trace_chunk_get(&element
->chunk
)) {
2041 * Another thread concurrently unpublished the
2042 * trace chunk. This is currently unexpected.
2044 * Re-attempt to publish.
2046 ERR("Attempt to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
2052 * An equivalent trace chunk was published before this trace
2053 * chunk. Attempt to acquire a reference to the one that was
2054 * already published and release the reference to the copy we
2055 * created if successful.
2057 published_element
= container_of(published_node
,
2058 typeof(*published_element
),
2059 trace_chunk_registry_ht_node
);
2060 published_chunk
= &published_element
->chunk
;
2061 if (lttng_trace_chunk_get(published_chunk
)) {
2062 lttng_trace_chunk_put(&element
->chunk
);
2063 element
= published_element
;
2067 * A reference to the previously published trace chunk could not
2068 * be acquired. Hence, retry to publish our copy of the trace
2074 return element
? &element
->chunk
: NULL
;
2078 * Note that the caller must be registered as an RCU thread.
2079 * However, it does not need to hold the RCU read lock. The RCU read lock is
2080 * acquired to perform the look-up in the registry's hash table and held until
2081 * after a reference to the "found" trace chunk is acquired.
2083 * IOW, holding a reference guarantees the existence of the object for the
2087 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
2088 const struct lttng_trace_chunk_registry
*registry
,
2089 uint64_t session_id
, uint64_t *chunk_id
)
2091 const struct lttng_trace_chunk_registry_element target_element
= {
2092 .chunk
.id
.is_set
= !!chunk_id
,
2093 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
2094 .session_id
= session_id
,
2096 const unsigned long element_hash
=
2097 lttng_trace_chunk_registry_element_hash(
2099 struct cds_lfht_node
*published_node
;
2100 struct lttng_trace_chunk_registry_element
*published_element
;
2101 struct lttng_trace_chunk
*published_chunk
= NULL
;
2102 struct cds_lfht_iter iter
;
2105 cds_lfht_lookup(registry
->ht
,
2107 lttng_trace_chunk_registry_element_match
,
2110 published_node
= cds_lfht_iter_get_node(&iter
);
2111 if (!published_node
) {
2115 published_element
= container_of(published_node
,
2116 typeof(*published_element
),
2117 trace_chunk_registry_ht_node
);
2118 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
2119 published_chunk
= &published_element
->chunk
;
2123 return published_chunk
;
2127 struct lttng_trace_chunk
*
2128 lttng_trace_chunk_registry_find_chunk(
2129 const struct lttng_trace_chunk_registry
*registry
,
2130 uint64_t session_id
, uint64_t chunk_id
)
2132 return _lttng_trace_chunk_registry_find_chunk(registry
,
2133 session_id
, &chunk_id
);
2137 int lttng_trace_chunk_registry_chunk_exists(
2138 const struct lttng_trace_chunk_registry
*registry
,
2139 uint64_t session_id
, uint64_t chunk_id
, bool *chunk_exists
)
2142 const struct lttng_trace_chunk_registry_element target_element
= {
2143 .chunk
.id
.is_set
= true,
2144 .chunk
.id
.value
= chunk_id
,
2145 .session_id
= session_id
,
2147 const unsigned long element_hash
=
2148 lttng_trace_chunk_registry_element_hash(
2150 struct cds_lfht_node
*published_node
;
2151 struct cds_lfht_iter iter
;
2154 cds_lfht_lookup(registry
->ht
,
2156 lttng_trace_chunk_registry_element_match
,
2159 published_node
= cds_lfht_iter_get_node(&iter
);
2160 if (!published_node
) {
2161 *chunk_exists
= false;
2165 *chunk_exists
= !cds_lfht_is_node_deleted(published_node
);
2172 struct lttng_trace_chunk
*
2173 lttng_trace_chunk_registry_find_anonymous_chunk(
2174 const struct lttng_trace_chunk_registry
*registry
,
2175 uint64_t session_id
)
2177 return _lttng_trace_chunk_registry_find_chunk(registry
,
2182 unsigned int lttng_trace_chunk_registry_put_each_chunk(
2183 const struct lttng_trace_chunk_registry
*registry
)
2185 struct cds_lfht_iter iter
;
2186 struct lttng_trace_chunk_registry_element
*chunk_element
;
2187 unsigned int trace_chunks_left
= 0;
2189 DBG("Releasing trace chunk registry to all trace chunks");
2191 cds_lfht_for_each_entry(registry
->ht
,
2192 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
2193 const char *chunk_id_str
= "none";
2194 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2196 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
2197 if (chunk_element
->chunk
.id
.is_set
) {
2200 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
2202 chunk_element
->chunk
.id
.value
);
2203 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
2204 chunk_id_str
= "formatting error";
2206 chunk_id_str
= chunk_id_buf
;
2210 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
2211 "chunk_id = %s, name = \"%s\", status = %s",
2212 chunk_element
->session_id
,
2214 chunk_element
->chunk
.name
? : "none",
2215 chunk_element
->chunk
.close_command
.is_set
?
2217 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
2218 lttng_trace_chunk_put(&chunk_element
->chunk
);
2219 trace_chunks_left
++;
2222 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
2225 return trace_chunks_left
;