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 credentials
->uid
= geteuid();
970 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 const bool reference_acquired
=
1081 lttng_directory_handle_get(
1082 session_output_directory
);
1084 assert(reference_acquired
);
1085 chunk_directory_handle
= session_output_directory
;
1087 chunk
->chunk_directory
= chunk_directory_handle
;
1088 chunk_directory_handle
= NULL
;
1089 reference_acquired
= lttng_directory_handle_get(
1090 session_output_directory
);
1091 assert(reference_acquired
);
1092 chunk
->session_output_directory
= session_output_directory
;
1093 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
1095 pthread_mutex_unlock(&chunk
->lock
);
1100 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
1101 struct lttng_trace_chunk
*chunk
,
1102 struct lttng_directory_handle
*chunk_directory
)
1104 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1105 bool reference_acquired
;
1107 pthread_mutex_lock(&chunk
->lock
);
1108 if (chunk
->mode
.is_set
) {
1109 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
1112 if (!chunk
->credentials
.is_set
) {
1113 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
1114 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1117 reference_acquired
= lttng_directory_handle_get(chunk_directory
);
1118 assert(reference_acquired
);
1119 chunk
->chunk_directory
= chunk_directory
;
1120 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
1122 pthread_mutex_unlock(&chunk
->lock
);
1127 enum lttng_trace_chunk_status
1128 lttng_trace_chunk_get_session_output_directory_handle(
1129 struct lttng_trace_chunk
*chunk
,
1130 struct lttng_directory_handle
**handle
)
1132 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1134 pthread_mutex_lock(&chunk
->lock
);
1135 if (!chunk
->session_output_directory
) {
1136 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1140 const bool reference_acquired
= lttng_directory_handle_get(
1141 chunk
->session_output_directory
);
1143 assert(reference_acquired
);
1144 *handle
= chunk
->session_output_directory
;
1147 pthread_mutex_unlock(&chunk
->lock
);
1152 enum lttng_trace_chunk_status
lttng_trace_chunk_borrow_chunk_directory_handle(
1153 struct lttng_trace_chunk
*chunk
,
1154 const struct lttng_directory_handle
**handle
)
1156 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1158 pthread_mutex_lock(&chunk
->lock
);
1159 if (!chunk
->chunk_directory
) {
1160 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1164 *handle
= chunk
->chunk_directory
;
1166 pthread_mutex_unlock(&chunk
->lock
);
1170 /* Add a top-level directory to the trace chunk if it was previously unknown. */
1172 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
1173 const char *new_path
)
1177 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
1178 &chunk
->top_level_directories
);
1179 const char *new_path_separator_pos
= strchr(new_path
, '/');
1180 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
1181 new_path_separator_pos
- new_path
: strlen(new_path
);
1183 for (i
= 0; i
< count
; i
++) {
1184 const char *path
= lttng_dynamic_pointer_array_get_pointer(
1185 &chunk
->top_level_directories
, i
);
1186 const ptrdiff_t path_top_level_len
= strlen(path
);
1188 if (path_top_level_len
!= new_path_top_level_len
) {
1191 if (!strncmp(path
, new_path
, path_top_level_len
)) {
1198 char *copy
= lttng_strndup(new_path
, new_path_top_level_len
);
1200 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
1201 new_path
, chunk
->name
? : "(unnamed)");
1203 PERROR("Failed to copy path");
1207 ret
= lttng_dynamic_pointer_array_add_pointer(
1208 &chunk
->top_level_directories
, copy
);
1210 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
1220 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
1221 struct lttng_trace_chunk
*chunk
,
1225 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1227 DBG("Creating trace chunk subdirectory \"%s\"", path
);
1228 pthread_mutex_lock(&chunk
->lock
);
1229 if (!chunk
->credentials
.is_set
) {
1231 * Fatal error, credentials must be set before a
1232 * directory is created.
1234 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
1236 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1239 if (!chunk
->mode
.is_set
||
1240 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
1241 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
1243 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
1246 if (!chunk
->chunk_directory
) {
1247 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
1249 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1253 ERR("Refusing to create absolute trace chunk directory \"%s\"",
1255 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1258 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
1259 chunk
->chunk_directory
, path
,
1261 chunk
->credentials
.value
.use_current_user
?
1262 NULL
: &chunk
->credentials
.value
.user
);
1264 PERROR("Failed to create trace chunk subdirectory \"%s\"",
1266 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1269 ret
= add_top_level_directory_unique(chunk
, path
);
1271 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1275 pthread_mutex_unlock(&chunk
->lock
);
1280 * TODO: Implement O(1) lookup.
1283 bool lttng_trace_chunk_find_file(struct lttng_trace_chunk
*chunk
,
1284 const char *path
, size_t *index
)
1288 count
= lttng_dynamic_pointer_array_get_count(&chunk
->files
);
1289 for (i
= 0; i
< count
; i
++) {
1290 const char *iter_path
=
1291 lttng_dynamic_pointer_array_get_pointer(
1293 if (!strcmp(iter_path
, path
)) {
1304 enum lttng_trace_chunk_status
lttng_trace_chunk_add_file(
1305 struct lttng_trace_chunk
*chunk
,
1310 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1312 if (lttng_trace_chunk_find_file(chunk
, path
, NULL
)) {
1313 return LTTNG_TRACE_CHUNK_STATUS_OK
;
1315 DBG("Adding new file \"%s\" to trace chunk \"%s\"",
1316 path
, chunk
->name
? : "(unnamed)");
1317 copy
= strdup(path
);
1319 PERROR("Failed to copy path");
1320 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1323 ret
= lttng_dynamic_pointer_array_add_pointer(
1324 &chunk
->files
, copy
);
1326 ERR("Allocation failure while adding file to a trace chunk");
1328 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1336 void lttng_trace_chunk_remove_file(
1337 struct lttng_trace_chunk
*chunk
,
1344 found
= lttng_trace_chunk_find_file(chunk
, path
, &index
);
1348 ret
= lttng_dynamic_pointer_array_remove_pointer(
1349 &chunk
->files
, index
);
1354 enum lttng_trace_chunk_status
_lttng_trace_chunk_open_fs_handle_locked(
1355 struct lttng_trace_chunk
*chunk
,
1356 const char *file_path
,
1359 struct fs_handle
**out_handle
,
1360 bool expect_no_file
)
1363 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1365 DBG("Opening trace chunk file \"%s\"", file_path
);
1366 if (!chunk
->credentials
.is_set
) {
1368 * Fatal error, credentials must be set before a
1371 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
1373 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1376 if (!chunk
->chunk_directory
) {
1377 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
1379 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1382 status
= lttng_trace_chunk_add_file(chunk
, file_path
);
1383 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1386 if (chunk
->fd_tracker
) {
1387 assert(chunk
->credentials
.value
.use_current_user
);
1388 *out_handle
= fd_tracker_open_fs_handle(chunk
->fd_tracker
,
1389 chunk
->chunk_directory
, file_path
, flags
, &mode
);
1390 ret
= *out_handle
? 0 : -1;
1392 ret
= lttng_directory_handle_open_file_as_user(
1393 chunk
->chunk_directory
, file_path
, flags
, mode
,
1394 chunk
->credentials
.value
.use_current_user
?
1396 &chunk
->credentials
.value
.user
);
1398 *out_handle
= fs_handle_untracked_create(
1399 chunk
->chunk_directory
, file_path
, ret
);
1401 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1407 if (errno
== ENOENT
&& expect_no_file
) {
1408 status
= LTTNG_TRACE_CHUNK_STATUS_NO_FILE
;
1410 PERROR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
1411 file_path
, flags
, (int) mode
);
1412 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1414 lttng_trace_chunk_remove_file(chunk
, file_path
);
1422 enum lttng_trace_chunk_status
lttng_trace_chunk_open_fs_handle(
1423 struct lttng_trace_chunk
*chunk
,
1424 const char *file_path
,
1427 struct fs_handle
**out_handle
,
1428 bool expect_no_file
)
1430 enum lttng_trace_chunk_status status
;
1432 pthread_mutex_lock(&chunk
->lock
);
1433 status
= _lttng_trace_chunk_open_fs_handle_locked(chunk
, file_path
,
1434 flags
, mode
, out_handle
, expect_no_file
);
1435 pthread_mutex_unlock(&chunk
->lock
);
1440 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
1441 struct lttng_trace_chunk
*chunk
,
1442 const char *file_path
,
1446 bool expect_no_file
)
1448 enum lttng_trace_chunk_status status
;
1449 struct fs_handle
*fs_handle
;
1451 pthread_mutex_lock(&chunk
->lock
);
1453 * Using this method is never valid when an fd_tracker is being
1454 * used since the resulting file descriptor would not be tracked.
1456 assert(!chunk
->fd_tracker
);
1457 status
= _lttng_trace_chunk_open_fs_handle_locked(chunk
, file_path
,
1458 flags
, mode
, &fs_handle
, expect_no_file
);
1459 pthread_mutex_unlock(&chunk
->lock
);
1461 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1462 *out_fd
= fs_handle_get_fd(fs_handle
);
1464 * Does not close the fd; we just "unbox" it from the fs_handle.
1466 fs_handle_untracked_destroy(container_of(
1467 fs_handle
, struct fs_handle_untracked
, parent
));
1474 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
1475 const char *file_path
)
1478 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1480 DBG("Unlinking trace chunk file \"%s\"", file_path
);
1481 pthread_mutex_lock(&chunk
->lock
);
1482 if (!chunk
->credentials
.is_set
) {
1484 * Fatal error, credentials must be set before a
1487 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
1489 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1492 if (!chunk
->chunk_directory
) {
1493 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
1495 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1498 ret
= lttng_directory_handle_unlink_file_as_user(
1499 chunk
->chunk_directory
, file_path
,
1500 chunk
->credentials
.value
.use_current_user
?
1501 NULL
: &chunk
->credentials
.value
.user
);
1503 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1506 lttng_trace_chunk_remove_file(chunk
, file_path
);
1508 pthread_mutex_unlock(&chunk
->lock
);
1513 int lttng_trace_chunk_remove_subdirectory_recursive(struct lttng_trace_chunk
*chunk
,
1517 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1519 DBG("Recursively removing trace chunk directory \"%s\"", path
);
1520 pthread_mutex_lock(&chunk
->lock
);
1521 if (!chunk
->credentials
.is_set
) {
1523 * Fatal error, credentials must be set before a
1524 * directory is removed.
1526 ERR("Credentials of trace chunk are unset: refusing to recursively remove directory \"%s\"",
1528 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1531 if (!chunk
->chunk_directory
) {
1532 ERR("Attempted to recursively remove trace chunk directory \"%s\" before setting the chunk output directory",
1534 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1537 ret
= lttng_directory_handle_remove_subdirectory_recursive_as_user(
1538 chunk
->chunk_directory
, path
,
1539 chunk
->credentials
.value
.use_current_user
?
1540 NULL
: &chunk
->credentials
.value
.user
,
1541 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
1543 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
1547 pthread_mutex_unlock(&chunk
->lock
);
1552 int lttng_trace_chunk_move_to_completed_post_release(
1553 struct lttng_trace_chunk
*trace_chunk
)
1556 char *archived_chunk_name
= NULL
;
1557 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
1558 const time_t creation_timestamp
=
1559 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
1560 const time_t close_timestamp
=
1561 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
1562 struct lttng_directory_handle
*archived_chunks_directory
= NULL
;
1563 enum lttng_trace_chunk_status status
;
1565 if (!trace_chunk
->mode
.is_set
||
1566 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
1567 !trace_chunk
->session_output_directory
) {
1569 * This command doesn't need to run if the output is remote
1570 * or if the trace chunk is not owned by this process.
1575 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
1576 assert(!trace_chunk
->name_overridden
);
1577 assert(trace_chunk
->path
);
1579 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
1581 if (!archived_chunk_name
) {
1582 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
1587 ret
= lttng_directory_handle_create_subdirectory_as_user(
1588 trace_chunk
->session_output_directory
,
1589 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1591 !trace_chunk
->credentials
.value
.use_current_user
?
1592 &trace_chunk
->credentials
.value
.user
:
1595 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
1596 "\" directory for archived trace chunks");
1600 archived_chunks_directory
= trace_chunk
->fd_tracker
?
1601 fd_tracker_create_directory_handle_from_handle(
1602 trace_chunk
->fd_tracker
,
1603 trace_chunk
->session_output_directory
,
1604 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
) :
1605 lttng_directory_handle_create_from_handle(
1606 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1607 trace_chunk
->session_output_directory
);
1608 if (!archived_chunks_directory
) {
1609 PERROR("Failed to get handle to archived trace chunks directory");
1615 * Make sure chunk is renamed to old directory if not already done by
1616 * the creation of the next chunk. This happens if a rotation is
1617 * performed while tracing is stopped.
1619 if (!trace_chunk
->path
|| strcmp(trace_chunk
->path
,
1620 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
)) {
1621 status
= lttng_trace_chunk_rename_path_no_lock(trace_chunk
,
1622 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
1623 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1624 ERR("Failed to rename chunk to %s", DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
1630 ret
= lttng_directory_handle_rename_as_user(
1631 trace_chunk
->session_output_directory
,
1633 archived_chunks_directory
,
1634 archived_chunk_name
,
1635 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
1637 &trace_chunk
->credentials
.value
.user
);
1639 PERROR("Failed to rename folder \"%s\" to \"%s\"",
1641 archived_chunk_name
);
1645 lttng_directory_handle_put(archived_chunks_directory
);
1646 free(archived_chunk_name
);
1651 int lttng_trace_chunk_no_operation(struct lttng_trace_chunk
*trace_chunk
)
1657 int lttng_trace_chunk_delete_post_release_user(
1658 struct lttng_trace_chunk
*trace_chunk
)
1662 DBG("Trace chunk \"delete\" close command post-release (User)");
1664 /* Unlink all files. */
1665 while (lttng_dynamic_pointer_array_get_count(&trace_chunk
->files
) != 0) {
1666 enum lttng_trace_chunk_status status
;
1670 path
= lttng_dynamic_pointer_array_get_pointer(
1671 &trace_chunk
->files
, 0);
1672 DBG("Unlink file: %s", path
);
1673 status
= lttng_trace_chunk_unlink_file(trace_chunk
, path
);
1674 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1675 ERR("Error unlinking file '%s' when deleting chunk", path
);
1685 int lttng_trace_chunk_delete_post_release_owner(
1686 struct lttng_trace_chunk
*trace_chunk
)
1688 enum lttng_trace_chunk_status status
;
1692 ret
= lttng_trace_chunk_delete_post_release_user(trace_chunk
);
1697 DBG("Trace chunk \"delete\" close command post-release (Owner)");
1699 assert(trace_chunk
->session_output_directory
);
1700 assert(trace_chunk
->chunk_directory
);
1702 /* Remove empty directories. */
1703 count
= lttng_dynamic_pointer_array_get_count(
1704 &trace_chunk
->top_level_directories
);
1706 for (i
= 0; i
< count
; i
++) {
1707 const char *top_level_name
=
1708 lttng_dynamic_pointer_array_get_pointer(
1709 &trace_chunk
->top_level_directories
, i
);
1711 status
= lttng_trace_chunk_remove_subdirectory_recursive(trace_chunk
, top_level_name
);
1712 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1713 ERR("Error recursively removing subdirectory '%s' file when deleting chunk",
1720 lttng_directory_handle_put(trace_chunk
->chunk_directory
);
1721 trace_chunk
->chunk_directory
= NULL
;
1723 if (trace_chunk
->path
&& trace_chunk
->path
[0] != '\0') {
1724 status
= lttng_directory_handle_remove_subdirectory(
1725 trace_chunk
->session_output_directory
,
1727 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1728 ERR("Error removing subdirectory '%s' file when deleting chunk",
1734 free(trace_chunk
->path
);
1735 trace_chunk
->path
= NULL
;
1741 * For local files, session and consumer daemons all run the delete hook. The
1742 * consumer daemons have the list of files to unlink, and technically the
1743 * session daemon is the owner of the chunk. Unlink all files owned by each
1747 int lttng_trace_chunk_delete_post_release(
1748 struct lttng_trace_chunk
*trace_chunk
)
1750 if (!trace_chunk
->chunk_directory
) {
1754 if (trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
) {
1755 return lttng_trace_chunk_delete_post_release_owner(trace_chunk
);
1757 return lttng_trace_chunk_delete_post_release_user(trace_chunk
);
1762 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
1763 struct lttng_trace_chunk
*chunk
,
1764 enum lttng_trace_chunk_command_type
*command_type
)
1766 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1768 pthread_mutex_lock(&chunk
->lock
);
1769 if (chunk
->close_command
.is_set
) {
1770 *command_type
= chunk
->close_command
.value
;
1771 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1773 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1775 pthread_mutex_unlock(&chunk
->lock
);
1780 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
1781 struct lttng_trace_chunk
*chunk
,
1782 enum lttng_trace_chunk_command_type close_command
)
1784 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1786 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
1787 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
1788 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1792 pthread_mutex_lock(&chunk
->lock
);
1793 if (chunk
->close_command
.is_set
) {
1794 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
1795 close_command_names
[chunk
->close_command
.value
],
1796 close_command_names
[close_command
]);
1798 DBG("Setting trace chunk close command to \"%s\"",
1799 close_command_names
[close_command
]);
1802 * Unset close command for no-op for backward compatibility with relayd
1805 if (close_command
!= LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
1806 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1808 LTTNG_OPTIONAL_UNSET(&chunk
->close_command
);
1810 pthread_mutex_unlock(&chunk
->lock
);
1816 const char *lttng_trace_chunk_command_type_get_name(
1817 enum lttng_trace_chunk_command_type command
)
1820 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1821 return "move to completed trace chunk folder";
1822 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
:
1823 return "no operation";
1824 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
:
1832 bool lttng_trace_chunk_ids_equal(const struct lttng_trace_chunk
*chunk_a
,
1833 const struct lttng_trace_chunk
*chunk_b
)
1837 if (!chunk_a
|| !chunk_b
) {
1841 if (chunk_a
->id
.is_set
^ chunk_a
->id
.is_set
) {
1842 /* One id is set and not the other, thus they are not equal. */
1846 if (!chunk_a
->id
.is_set
) {
1847 /* Both ids are unset. */
1850 equal
= chunk_a
->id
.value
== chunk_b
->id
.value
;
1858 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1860 return urcu_ref_get_unless_zero(&chunk
->ref
);
1864 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1866 struct lttng_trace_chunk_registry_element
*element
=
1867 container_of(node
, typeof(*element
), rcu_node
);
1869 lttng_trace_chunk_fini(&element
->chunk
);
1874 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1876 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1879 if (chunk
->close_command
.is_set
) {
1880 if (close_command_post_release_funcs
[
1881 chunk
->close_command
.value
](chunk
)) {
1882 ERR("Trace chunk post-release command %s has failed.",
1883 close_command_names
[chunk
->close_command
.value
]);
1887 if (chunk
->in_registry_element
) {
1888 struct lttng_trace_chunk_registry_element
*element
;
1890 element
= container_of(chunk
, typeof(*element
), chunk
);
1891 if (element
->registry
) {
1893 cds_lfht_del(element
->registry
->ht
,
1894 &element
->trace_chunk_registry_ht_node
);
1896 call_rcu(&element
->rcu_node
,
1897 free_lttng_trace_chunk_registry_element
);
1899 /* Never published, can be free'd immediately. */
1900 free_lttng_trace_chunk_registry_element(
1901 &element
->rcu_node
);
1904 /* Not RCU-protected, free immediately. */
1905 lttng_trace_chunk_fini(chunk
);
1911 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1916 assert(chunk
->ref
.refcount
);
1917 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1921 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1923 struct lttng_trace_chunk_registry
*registry
;
1925 registry
= zmalloc(sizeof(*registry
));
1930 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1931 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1932 if (!registry
->ht
) {
1938 lttng_trace_chunk_registry_destroy(registry
);
1943 void lttng_trace_chunk_registry_destroy(
1944 struct lttng_trace_chunk_registry
*registry
)
1950 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1957 struct lttng_trace_chunk_registry_element
*
1958 lttng_trace_chunk_registry_element_create_from_chunk(
1959 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1961 struct lttng_trace_chunk_registry_element
*element
=
1962 zmalloc(sizeof(*element
));
1967 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1968 element
->session_id
= session_id
;
1970 element
->chunk
= *chunk
;
1971 lttng_trace_chunk_init(&element
->chunk
);
1972 if (chunk
->session_output_directory
) {
1973 /* Transferred ownership. */
1974 element
->chunk
.session_output_directory
=
1975 chunk
->session_output_directory
;
1976 chunk
->session_output_directory
= NULL
;
1978 if (chunk
->chunk_directory
) {
1979 /* Transferred ownership. */
1980 element
->chunk
.chunk_directory
= chunk
->chunk_directory
;
1981 chunk
->chunk_directory
= NULL
;
1984 * The original chunk becomes invalid; the name and path attributes are
1985 * transferred to the new chunk instance.
1989 element
->chunk
.fd_tracker
= chunk
->fd_tracker
;
1990 element
->chunk
.in_registry_element
= true;
1996 struct lttng_trace_chunk
*
1997 lttng_trace_chunk_registry_publish_chunk(
1998 struct lttng_trace_chunk_registry
*registry
,
1999 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
2001 struct lttng_trace_chunk_registry_element
*element
;
2002 unsigned long element_hash
;
2004 pthread_mutex_lock(&chunk
->lock
);
2005 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
2007 pthread_mutex_unlock(&chunk
->lock
);
2012 * chunk is now invalid, the only valid operation is a 'put' from the
2016 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
2020 struct cds_lfht_node
*published_node
;
2021 struct lttng_trace_chunk
*published_chunk
;
2022 struct lttng_trace_chunk_registry_element
*published_element
;
2024 published_node
= cds_lfht_add_unique(registry
->ht
,
2026 lttng_trace_chunk_registry_element_match
,
2028 &element
->trace_chunk_registry_ht_node
);
2029 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
2030 /* Successfully published the new element. */
2031 element
->registry
= registry
;
2032 /* Acquire a reference for the caller. */
2033 if (lttng_trace_chunk_get(&element
->chunk
)) {
2037 * Another thread concurrently unpublished the
2038 * trace chunk. This is currently unexpected.
2040 * Re-attempt to publish.
2042 ERR("Attempt to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
2048 * An equivalent trace chunk was published before this trace
2049 * chunk. Attempt to acquire a reference to the one that was
2050 * already published and release the reference to the copy we
2051 * created if successful.
2053 published_element
= container_of(published_node
,
2054 typeof(*published_element
),
2055 trace_chunk_registry_ht_node
);
2056 published_chunk
= &published_element
->chunk
;
2057 if (lttng_trace_chunk_get(published_chunk
)) {
2058 lttng_trace_chunk_put(&element
->chunk
);
2059 element
= published_element
;
2063 * A reference to the previously published trace chunk could not
2064 * be acquired. Hence, retry to publish our copy of the trace
2070 return element
? &element
->chunk
: NULL
;
2074 * Note that the caller must be registered as an RCU thread.
2075 * However, it does not need to hold the RCU read lock. The RCU read lock is
2076 * acquired to perform the look-up in the registry's hash table and held until
2077 * after a reference to the "found" trace chunk is acquired.
2079 * IOW, holding a reference guarantees the existence of the object for the
2083 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
2084 const struct lttng_trace_chunk_registry
*registry
,
2085 uint64_t session_id
, uint64_t *chunk_id
)
2087 const struct lttng_trace_chunk_registry_element target_element
= {
2088 .chunk
.id
.is_set
= !!chunk_id
,
2089 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
2090 .session_id
= session_id
,
2092 const unsigned long element_hash
=
2093 lttng_trace_chunk_registry_element_hash(
2095 struct cds_lfht_node
*published_node
;
2096 struct lttng_trace_chunk_registry_element
*published_element
;
2097 struct lttng_trace_chunk
*published_chunk
= NULL
;
2098 struct cds_lfht_iter iter
;
2101 cds_lfht_lookup(registry
->ht
,
2103 lttng_trace_chunk_registry_element_match
,
2106 published_node
= cds_lfht_iter_get_node(&iter
);
2107 if (!published_node
) {
2111 published_element
= container_of(published_node
,
2112 typeof(*published_element
),
2113 trace_chunk_registry_ht_node
);
2114 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
2115 published_chunk
= &published_element
->chunk
;
2119 return published_chunk
;
2123 struct lttng_trace_chunk
*
2124 lttng_trace_chunk_registry_find_chunk(
2125 const struct lttng_trace_chunk_registry
*registry
,
2126 uint64_t session_id
, uint64_t chunk_id
)
2128 return _lttng_trace_chunk_registry_find_chunk(registry
,
2129 session_id
, &chunk_id
);
2133 int lttng_trace_chunk_registry_chunk_exists(
2134 const struct lttng_trace_chunk_registry
*registry
,
2135 uint64_t session_id
, uint64_t chunk_id
, bool *chunk_exists
)
2138 const struct lttng_trace_chunk_registry_element target_element
= {
2139 .chunk
.id
.is_set
= true,
2140 .chunk
.id
.value
= chunk_id
,
2141 .session_id
= session_id
,
2143 const unsigned long element_hash
=
2144 lttng_trace_chunk_registry_element_hash(
2146 struct cds_lfht_node
*published_node
;
2147 struct cds_lfht_iter iter
;
2150 cds_lfht_lookup(registry
->ht
,
2152 lttng_trace_chunk_registry_element_match
,
2155 published_node
= cds_lfht_iter_get_node(&iter
);
2156 if (!published_node
) {
2157 *chunk_exists
= false;
2161 *chunk_exists
= !cds_lfht_is_node_deleted(published_node
);
2168 struct lttng_trace_chunk
*
2169 lttng_trace_chunk_registry_find_anonymous_chunk(
2170 const struct lttng_trace_chunk_registry
*registry
,
2171 uint64_t session_id
)
2173 return _lttng_trace_chunk_registry_find_chunk(registry
,
2178 unsigned int lttng_trace_chunk_registry_put_each_chunk(
2179 const struct lttng_trace_chunk_registry
*registry
)
2181 struct cds_lfht_iter iter
;
2182 struct lttng_trace_chunk_registry_element
*chunk_element
;
2183 unsigned int trace_chunks_left
= 0;
2185 DBG("Releasing trace chunk registry to all trace chunks");
2187 cds_lfht_for_each_entry(registry
->ht
,
2188 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
2189 const char *chunk_id_str
= "none";
2190 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2192 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
2193 if (chunk_element
->chunk
.id
.is_set
) {
2196 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
2198 chunk_element
->chunk
.id
.value
);
2199 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
2200 chunk_id_str
= "formatting error";
2202 chunk_id_str
= chunk_id_buf
;
2206 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
2207 "chunk_id = %s, name = \"%s\", status = %s",
2208 chunk_element
->session_id
,
2210 chunk_element
->chunk
.name
? : "none",
2211 chunk_element
->chunk
.close_command
.is_set
?
2213 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
2214 lttng_trace_chunk_put(&chunk_element
->chunk
);
2215 trace_chunks_left
++;
2218 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
2221 return trace_chunks_left
;