2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/constant.h>
19 #include <common/string-utils/format.h>
20 #include <common/trace-chunk.h>
21 #include <common/trace-chunk-registry.h>
22 #include <common/hashtable/utils.h>
23 #include <common/hashtable/hashtable.h>
24 #include <common/error.h>
25 #include <common/utils.h>
26 #include <common/time.h>
27 #include <common/optional.h>
28 #include <common/compat/directory-handle.h>
29 #include <common/credentials.h>
30 #include <common/defaults.h>
31 #include <common/dynamic-array.h>
34 #include <urcu/rculfhash.h>
41 * Two ISO 8601-compatible timestamps, separated by a hypen, followed an
42 * index, i.e. <start-iso-8601>-<end-iso-8601>-<id-uint64_t>.
44 #define GENERATED_CHUNK_NAME_LEN (2 * sizeof("YYYYmmddTHHMMSS+HHMM") + MAX_INT_DEC_LEN(uint64_t))
45 #define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
47 enum trace_chunk_mode
{
48 TRACE_CHUNK_MODE_USER
,
49 TRACE_CHUNK_MODE_OWNER
,
53 * Callback to invoke on release of a trace chunk. Note that there is no
54 * need to 'lock' the trace chunk during the execution of these callbacks
55 * since only one thread may access a chunk during its destruction (the last
56 * to release its reference to the chunk).
58 typedef void (*chunk_close_command
)(struct lttng_trace_chunk
*trace_chunk
);
60 /* Move a completed trace chunk to the 'completed' trace archive folder. */
62 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
);
64 struct chunk_credentials
{
65 bool use_current_user
;
66 struct lttng_credentials user
;
69 /* NOTE: Make sure to update lttng_trace_chunk_copy if you modify this. */
70 struct lttng_trace_chunk
{
73 LTTNG_OPTIONAL(enum trace_chunk_mode
) mode
;
75 * First-level directories created within the trace chunk.
76 * Elements are of type 'char *'.
78 * Only used by _owner_ mode chunks.
80 struct lttng_dynamic_pointer_array top_level_directories
;
81 /* Is contained within an lttng_trace_chunk_registry_element? */
82 bool in_registry_element
;
85 /* An unset id means the chunk is anonymous. */
86 LTTNG_OPTIONAL(uint64_t) id
;
87 LTTNG_OPTIONAL(time_t) timestamp_creation
;
88 LTTNG_OPTIONAL(time_t) timestamp_close
;
89 LTTNG_OPTIONAL(struct chunk_credentials
) credentials
;
90 LTTNG_OPTIONAL(struct lttng_directory_handle
) session_output_directory
;
91 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory
;
92 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
;
95 /* A trace chunk is uniquely identified by its (session id, chunk id) tuple. */
96 struct lttng_trace_chunk_registry_element
{
97 struct lttng_trace_chunk chunk
;
99 /* Weak and only set when added. */
100 struct lttng_trace_chunk_registry
*registry
;
101 struct cds_lfht_node trace_chunk_registry_ht_node
;
102 /* call_rcu delayed reclaim. */
103 struct rcu_head rcu_node
;
106 struct lttng_trace_chunk_registry
{
111 char *close_command_names
[] = {
112 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
113 "move to completed chunk folder",
117 chunk_close_command close_command_funcs
[] = {
118 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
119 lttng_trace_chunk_move_to_completed
,
123 bool lttng_trace_chunk_registry_element_equals(
124 const struct lttng_trace_chunk_registry_element
*a
,
125 const struct lttng_trace_chunk_registry_element
*b
)
127 if (a
->session_id
!= b
->session_id
) {
130 if (a
->chunk
.id
.is_set
!= b
->chunk
.id
.is_set
) {
133 if (a
->chunk
.id
.is_set
&& a
->chunk
.id
.value
!= b
->chunk
.id
.value
) {
142 int lttng_trace_chunk_registry_element_match(struct cds_lfht_node
*node
,
145 const struct lttng_trace_chunk_registry_element
*element_a
, *element_b
;
147 element_a
= (const struct lttng_trace_chunk_registry_element
*) key
;
148 element_b
= caa_container_of(node
, typeof(*element_b
),
149 trace_chunk_registry_ht_node
);
150 return lttng_trace_chunk_registry_element_equals(element_a
, element_b
);
154 unsigned long lttng_trace_chunk_registry_element_hash(
155 const struct lttng_trace_chunk_registry_element
*element
)
157 unsigned long hash
= hash_key_u64(&element
->session_id
,
160 if (element
->chunk
.id
.is_set
) {
161 hash
^= hash_key_u64(&element
->chunk
.id
.value
, lttng_ht_seed
);
168 char *generate_chunk_name(uint64_t chunk_id
, time_t creation_timestamp
,
169 const time_t *close_timestamp
)
172 char *new_name
= NULL
;
173 char start_datetime
[ISO8601_STR_LEN
] = {};
174 /* Add 1 for a '-' prefix. */
175 char end_datetime_suffix
[ISO8601_STR_LEN
+ 1] = {};
177 ret
= time_to_iso8601_str(
179 start_datetime
, sizeof(start_datetime
));
181 ERR("Failed to format trace chunk start date time");
184 if (close_timestamp
) {
185 *end_datetime_suffix
= '-';
186 ret
= time_to_iso8601_str(
188 end_datetime_suffix
+ 1,
189 sizeof(end_datetime_suffix
) - 1);
191 ERR("Failed to format trace chunk end date time");
195 new_name
= zmalloc(GENERATED_CHUNK_NAME_LEN
);
197 ERR("Failed to allocate buffer for automatically-generated trace chunk name");
200 ret
= snprintf(new_name
, GENERATED_CHUNK_NAME_LEN
, "%s%s-%" PRIu64
,
201 start_datetime
, end_datetime_suffix
, chunk_id
);
202 if (ret
>= GENERATED_CHUNK_NAME_LEN
|| ret
== -1) {
203 ERR("Failed to format trace chunk name");
214 void lttng_trace_chunk_init(struct lttng_trace_chunk
*chunk
)
216 urcu_ref_init(&chunk
->ref
);
217 pthread_mutex_init(&chunk
->lock
, NULL
);
218 lttng_dynamic_pointer_array_init(&chunk
->top_level_directories
, free
);
222 void lttng_trace_chunk_fini(struct lttng_trace_chunk
*chunk
)
224 if (chunk
->session_output_directory
.is_set
) {
225 lttng_directory_handle_fini(
226 &chunk
->session_output_directory
.value
);
228 if (chunk
->chunk_directory
.is_set
) {
229 lttng_directory_handle_fini(&chunk
->chunk_directory
.value
);
233 lttng_dynamic_pointer_array_reset(&chunk
->top_level_directories
);
234 pthread_mutex_destroy(&chunk
->lock
);
238 struct lttng_trace_chunk
*lttng_trace_chunk_allocate(void)
240 struct lttng_trace_chunk
*chunk
= NULL
;
242 chunk
= zmalloc(sizeof(*chunk
));
244 ERR("Failed to allocate trace chunk");
247 lttng_trace_chunk_init(chunk
);
253 struct lttng_trace_chunk
*lttng_trace_chunk_create_anonymous(void)
255 DBG("Creating anonymous trace chunk");
256 return lttng_trace_chunk_allocate();
260 struct lttng_trace_chunk
*lttng_trace_chunk_create(
261 uint64_t chunk_id
, time_t chunk_creation_time
)
263 struct lttng_trace_chunk
*chunk
;
264 char chunk_creation_datetime_buf
[16] = {};
265 const char *chunk_creation_datetime_str
= "(formatting error)";
266 struct tm timeinfo_buf
, *timeinfo
;
268 timeinfo
= localtime_r(&chunk_creation_time
, &timeinfo_buf
);
272 /* Don't fail because of this; it is only used for logging. */
273 strftime_ret
= strftime(chunk_creation_datetime_buf
,
274 sizeof(chunk_creation_datetime_buf
),
275 "%Y%m%d-%H%M%S", timeinfo
);
277 chunk_creation_datetime_str
=
278 chunk_creation_datetime_buf
;
282 DBG("Creating trace chunk: chunk_id = %" PRIu64
", creation time = %s",
283 chunk_id
, chunk_creation_datetime_str
);
284 chunk
= lttng_trace_chunk_allocate();
289 LTTNG_OPTIONAL_SET(&chunk
->id
, chunk_id
);
290 LTTNG_OPTIONAL_SET(&chunk
->timestamp_creation
, chunk_creation_time
);
292 chunk
->name
= generate_chunk_name(chunk_id
,
293 chunk_creation_time
, NULL
);
295 ERR("Failed to allocate trace chunk name storage");
300 DBG("Chunk name set to \"%s\"", chunk
->name
? : "(none)");
304 lttng_trace_chunk_put(chunk
);
309 struct lttng_trace_chunk
*lttng_trace_chunk_copy(
310 struct lttng_trace_chunk
*source_chunk
)
312 struct lttng_trace_chunk
*new_chunk
= lttng_trace_chunk_allocate();
318 pthread_mutex_lock(&source_chunk
->lock
);
320 * A new chunk is always a user; it shall create no new trace
323 new_chunk
->mode
= (typeof(new_chunk
->mode
)) {
325 .value
= TRACE_CHUNK_MODE_USER
,
328 * top_level_directories is not copied as it is never used
329 * by _user_ mode chunks.
331 /* The new chunk is not part of a registry (yet, at least). */
332 new_chunk
->in_registry_element
= false;
333 new_chunk
->name_overridden
= source_chunk
->name_overridden
;
334 if (source_chunk
->name
) {
335 new_chunk
->name
= strdup(source_chunk
->name
);
336 if (!new_chunk
->name
) {
337 ERR("Failed to copy source trace chunk name in %s()",
342 new_chunk
->id
= source_chunk
->id
;
343 new_chunk
->timestamp_creation
= source_chunk
->timestamp_creation
;
344 new_chunk
->timestamp_close
= source_chunk
->timestamp_close
;
345 new_chunk
->credentials
= source_chunk
->credentials
;
346 if (source_chunk
->session_output_directory
.is_set
) {
347 if (lttng_directory_handle_copy(
348 &source_chunk
->session_output_directory
.value
,
349 &new_chunk
->session_output_directory
.value
)) {
352 new_chunk
->session_output_directory
.is_set
= true;
355 if (source_chunk
->chunk_directory
.is_set
) {
356 if (lttng_directory_handle_copy(
357 &source_chunk
->chunk_directory
.value
,
358 &new_chunk
->chunk_directory
.value
)) {
361 new_chunk
->chunk_directory
.is_set
= true;
364 new_chunk
->close_command
= source_chunk
->close_command
;
365 pthread_mutex_unlock(&source_chunk
->lock
);
369 pthread_mutex_unlock(&source_chunk
->lock
);
374 enum lttng_trace_chunk_status
lttng_trace_chunk_get_id(
375 struct lttng_trace_chunk
*chunk
, uint64_t *id
)
377 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
379 pthread_mutex_lock(&chunk
->lock
);
380 if (chunk
->id
.is_set
) {
381 *id
= chunk
->id
.value
;
383 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
385 pthread_mutex_unlock(&chunk
->lock
);
390 enum lttng_trace_chunk_status
lttng_trace_chunk_get_creation_timestamp(
391 struct lttng_trace_chunk
*chunk
, time_t *creation_ts
)
394 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
396 pthread_mutex_lock(&chunk
->lock
);
397 if (chunk
->timestamp_creation
.is_set
) {
398 *creation_ts
= chunk
->timestamp_creation
.value
;
400 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
402 pthread_mutex_unlock(&chunk
->lock
);
407 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_timestamp(
408 struct lttng_trace_chunk
*chunk
, time_t *close_ts
)
410 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
412 pthread_mutex_lock(&chunk
->lock
);
413 if (chunk
->timestamp_close
.is_set
) {
414 *close_ts
= chunk
->timestamp_close
.value
;
416 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
418 pthread_mutex_unlock(&chunk
->lock
);
423 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_timestamp(
424 struct lttng_trace_chunk
*chunk
, time_t close_ts
)
426 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
428 pthread_mutex_lock(&chunk
->lock
);
429 if (!chunk
->timestamp_creation
.is_set
) {
430 ERR("Failed to set trace chunk close timestamp: creation timestamp is unset");
431 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
434 if (chunk
->timestamp_creation
.value
> close_ts
) {
435 ERR("Failed to set trace chunk close timestamp: close timestamp is before creation timestamp");
436 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
439 LTTNG_OPTIONAL_SET(&chunk
->timestamp_close
, close_ts
);
440 if (!chunk
->name_overridden
) {
442 chunk
->name
= generate_chunk_name(LTTNG_OPTIONAL_GET(chunk
->id
),
443 LTTNG_OPTIONAL_GET(chunk
->timestamp_creation
),
446 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
450 pthread_mutex_unlock(&chunk
->lock
);
455 enum lttng_trace_chunk_status
lttng_trace_chunk_get_name(
456 struct lttng_trace_chunk
*chunk
, const char **name
,
457 bool *name_overridden
)
459 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
461 pthread_mutex_lock(&chunk
->lock
);
462 if (name_overridden
) {
463 *name_overridden
= chunk
->name_overridden
;
466 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
471 pthread_mutex_unlock(&chunk
->lock
);
476 bool is_valid_chunk_name(const char *name
)
484 len
= lttng_strnlen(name
, LTTNG_NAME_MAX
);
485 if (len
== 0 || len
== LTTNG_NAME_MAX
) {
489 if (strchr(name
, '/') || strchr(name
, '.')) {
497 enum lttng_trace_chunk_status
lttng_trace_chunk_override_name(
498 struct lttng_trace_chunk
*chunk
, const char *name
)
502 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
504 if (!is_valid_chunk_name(name
)) {
505 ERR("Attempted to set an invalid name on a trace chunk: name = %s",
507 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
511 pthread_mutex_lock(&chunk
->lock
);
512 if (!chunk
->id
.is_set
) {
513 ERR("Attempted to set an override name on an anonymous trace chunk: name = %s",
515 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
518 new_name
= strdup(name
);
520 ERR("Failed to allocate new trace chunk name");
521 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
525 chunk
->name
= new_name
;
526 chunk
->name_overridden
= true;
528 pthread_mutex_unlock(&chunk
->lock
);
534 enum lttng_trace_chunk_status
lttng_trace_chunk_get_credentials(
535 struct lttng_trace_chunk
*chunk
,
536 struct lttng_credentials
*credentials
)
538 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
540 pthread_mutex_lock(&chunk
->lock
);
541 if (chunk
->credentials
.is_set
) {
542 if (chunk
->credentials
.value
.use_current_user
) {
543 credentials
->uid
= geteuid();
544 credentials
->gid
= getegid();
546 *credentials
= chunk
->credentials
.value
.user
;
549 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
551 pthread_mutex_unlock(&chunk
->lock
);
556 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials(
557 struct lttng_trace_chunk
*chunk
,
558 const struct lttng_credentials
*user_credentials
)
560 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
561 const struct chunk_credentials credentials
= {
562 .user
= *user_credentials
,
563 .use_current_user
= false,
566 pthread_mutex_lock(&chunk
->lock
);
567 if (chunk
->credentials
.is_set
) {
568 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
571 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
573 pthread_mutex_unlock(&chunk
->lock
);
578 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials_current_user(
579 struct lttng_trace_chunk
*chunk
)
581 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
582 const struct chunk_credentials credentials
= {
583 .use_current_user
= true,
586 pthread_mutex_lock(&chunk
->lock
);
587 if (chunk
->credentials
.is_set
) {
588 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
591 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
593 pthread_mutex_unlock(&chunk
->lock
);
599 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_owner(
600 struct lttng_trace_chunk
*chunk
,
601 struct lttng_directory_handle
*session_output_directory
)
604 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
605 struct lttng_directory_handle chunk_directory_handle
;
607 pthread_mutex_lock(&chunk
->lock
);
608 if (chunk
->mode
.is_set
) {
609 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
612 if (!chunk
->credentials
.is_set
) {
614 * Fatal error, credentials must be set before a
615 * directory is created.
617 ERR("Credentials of trace chunk are unset: refusing to set session output directory");
618 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
624 * A nameless chunk does not need its own output directory.
625 * The session's output directory will be used.
627 ret
= lttng_directory_handle_create_subdirectory_as_user(
628 session_output_directory
,
631 !chunk
->credentials
.value
.use_current_user
?
632 &chunk
->credentials
.value
.user
: NULL
);
634 PERROR("Failed to create chunk output directory \"%s\"",
636 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
640 ret
= lttng_directory_handle_init_from_handle(&chunk_directory_handle
,
642 session_output_directory
);
644 /* The function already logs on all error paths. */
645 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
648 LTTNG_OPTIONAL_SET(&chunk
->session_output_directory
,
649 lttng_directory_handle_move(session_output_directory
));
650 LTTNG_OPTIONAL_SET(&chunk
->chunk_directory
,
651 lttng_directory_handle_move(&chunk_directory_handle
));
652 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
654 pthread_mutex_unlock(&chunk
->lock
);
659 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
660 struct lttng_trace_chunk
*chunk
,
661 struct lttng_directory_handle
*chunk_directory
)
663 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
665 pthread_mutex_lock(&chunk
->lock
);
666 if (chunk
->mode
.is_set
) {
667 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
670 if (!chunk
->credentials
.is_set
) {
671 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
672 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
675 LTTNG_OPTIONAL_SET(&chunk
->chunk_directory
,
676 lttng_directory_handle_move(chunk_directory
));
677 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
679 pthread_mutex_unlock(&chunk
->lock
);
684 enum lttng_trace_chunk_status
lttng_trace_chunk_get_chunk_directory_handle(
685 struct lttng_trace_chunk
*chunk
,
686 const struct lttng_directory_handle
**handle
)
688 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
690 pthread_mutex_lock(&chunk
->lock
);
691 if (!chunk
->chunk_directory
.is_set
) {
692 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
696 *handle
= &chunk
->chunk_directory
.value
;
698 pthread_mutex_unlock(&chunk
->lock
);
702 /* Add a top-level directory to the trace chunk if it was previously unknown. */
704 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
705 const char *new_path
)
709 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
710 &chunk
->top_level_directories
);
711 const char *new_path_separator_pos
= strchr(new_path
, '/');
712 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
713 new_path_separator_pos
- new_path
: strlen(new_path
);
715 for (i
= 0; i
< count
; i
++) {
716 const char *path
= lttng_dynamic_pointer_array_get_pointer(
717 &chunk
->top_level_directories
, i
);
718 const ptrdiff_t path_top_level_len
= strlen(path
);
720 if (path_top_level_len
!= new_path_top_level_len
) {
723 if (!strncmp(path
, new_path
, path_top_level_len
)) {
730 char *copy
= lttng_strndup(new_path
, new_path_top_level_len
);
732 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
733 new_path
, chunk
->name
? : "(unnamed)");
735 PERROR("Failed to copy path");
739 ret
= lttng_dynamic_pointer_array_add_pointer(
740 &chunk
->top_level_directories
, copy
);
742 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
752 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
753 struct lttng_trace_chunk
*chunk
,
757 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
759 DBG("Creating trace chunk subdirectory \"%s\"", path
);
760 pthread_mutex_lock(&chunk
->lock
);
761 if (!chunk
->credentials
.is_set
) {
763 * Fatal error, credentials must be set before a
764 * directory is created.
766 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
768 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
771 if (!chunk
->mode
.is_set
||
772 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
773 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
775 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
778 if (!chunk
->chunk_directory
.is_set
) {
779 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
781 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
785 ERR("Refusing to create absolute trace chunk directory \"%s\"",
787 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
790 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
791 &chunk
->chunk_directory
.value
, path
,
793 chunk
->credentials
.value
.use_current_user
?
794 NULL
: &chunk
->credentials
.value
.user
);
796 PERROR("Failed to create trace chunk subdirectory \"%s\"",
798 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
801 ret
= add_top_level_directory_unique(chunk
, path
);
803 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
807 pthread_mutex_unlock(&chunk
->lock
);
812 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
813 struct lttng_trace_chunk
*chunk
, const char *file_path
,
814 int flags
, mode_t mode
, int *out_fd
)
817 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
819 DBG("Opening trace chunk file \"%s\"", file_path
);
820 pthread_mutex_lock(&chunk
->lock
);
821 if (!chunk
->credentials
.is_set
) {
823 * Fatal error, credentials must be set before a
826 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
828 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
831 if (!chunk
->chunk_directory
.is_set
) {
832 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
834 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
837 ret
= lttng_directory_handle_open_file_as_user(
838 &chunk
->chunk_directory
.value
, file_path
, flags
, mode
,
839 chunk
->credentials
.value
.use_current_user
?
840 NULL
: &chunk
->credentials
.value
.user
);
842 ERR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
843 file_path
, flags
, (int) mode
);
844 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
849 pthread_mutex_unlock(&chunk
->lock
);
854 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
855 const char *file_path
)
858 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
860 DBG("Unlinking trace chunk file \"%s\"", file_path
);
861 pthread_mutex_lock(&chunk
->lock
);
862 if (!chunk
->credentials
.is_set
) {
864 * Fatal error, credentials must be set before a
865 * directory is created.
867 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
869 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
872 if (!chunk
->chunk_directory
.is_set
) {
873 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
875 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
878 ret
= lttng_directory_handle_unlink_file_as_user(
879 &chunk
->chunk_directory
.value
, file_path
,
880 chunk
->credentials
.value
.use_current_user
?
881 NULL
: &chunk
->credentials
.value
.user
);
883 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
887 pthread_mutex_unlock(&chunk
->lock
);
892 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
)
895 char *directory_to_rename
= NULL
;
896 bool free_directory_to_rename
= false;
897 char *archived_chunk_name
= NULL
;
898 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
899 const time_t creation_timestamp
=
900 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
901 const time_t close_timestamp
=
902 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
903 LTTNG_OPTIONAL(struct lttng_directory_handle
) archived_chunks_directory
= {};
905 if (!trace_chunk
->mode
.is_set
||
906 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
907 !trace_chunk
->session_output_directory
.is_set
) {
909 * This command doesn't need to run if the output is remote
910 * or if the trace chunk is not owned by this process.
915 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
916 assert(!trace_chunk
->name_overridden
);
919 * The fist trace chunk of a session is directly output to the
920 * session's output folder. In this case, the top level directories
921 * must be moved to a temporary folder before that temporary directory
922 * is renamed to match the chunk's name.
925 struct lttng_directory_handle temporary_rename_directory
;
926 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
927 &trace_chunk
->top_level_directories
);
929 ret
= lttng_directory_handle_create_subdirectory_as_user(
930 &trace_chunk
->session_output_directory
.value
,
931 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
933 !trace_chunk
->credentials
.value
.use_current_user
?
934 &trace_chunk
->credentials
.value
.user
: NULL
);
936 PERROR("Failed to create temporary trace chunk rename directory \"%s\"",
937 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
);
940 ret
= lttng_directory_handle_init_from_handle(&temporary_rename_directory
,
941 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
942 &trace_chunk
->session_output_directory
.value
);
944 ERR("Failed to get handle to temporary trace chunk rename directory");
948 for (i
= 0; i
< count
; i
++) {
949 const char *top_level_name
=
950 lttng_dynamic_pointer_array_get_pointer(
951 &trace_chunk
->top_level_directories
, i
);
953 ret
= lttng_directory_handle_rename_as_user(
954 &trace_chunk
->session_output_directory
.value
,
956 &temporary_rename_directory
,
958 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
960 &trace_chunk
->credentials
.value
.user
);
962 PERROR("Failed to move \"%s\" to temporary trace chunk rename directory",
964 lttng_directory_handle_fini(
965 &temporary_rename_directory
);
969 lttng_directory_handle_fini(&temporary_rename_directory
);
970 directory_to_rename
= DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
;
971 free_directory_to_rename
= false;
973 directory_to_rename
= generate_chunk_name(chunk_id
,
974 creation_timestamp
, NULL
);
975 if (!directory_to_rename
) {
976 ERR("Failed to generate initial trace chunk name while renaming trace chunk");
978 free_directory_to_rename
= true;
981 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
983 if (!archived_chunk_name
) {
984 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
988 ret
= lttng_directory_handle_create_subdirectory_as_user(
989 &trace_chunk
->session_output_directory
.value
,
990 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
992 !trace_chunk
->credentials
.value
.use_current_user
?
993 &trace_chunk
->credentials
.value
.user
:
996 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
997 "\" directory for archived trace chunks");
1001 ret
= lttng_directory_handle_init_from_handle(
1002 &archived_chunks_directory
.value
,
1003 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1004 &trace_chunk
->session_output_directory
.value
);
1006 PERROR("Failed to get handle to archived trace chunks directory");
1009 archived_chunks_directory
.is_set
= true;
1011 ret
= lttng_directory_handle_rename_as_user(
1012 &trace_chunk
->session_output_directory
.value
,
1013 directory_to_rename
,
1014 &archived_chunks_directory
.value
,
1015 archived_chunk_name
,
1016 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
1018 &trace_chunk
->credentials
.value
.user
);
1020 PERROR("Failed to rename folder \"%s\" to \"%s\"",
1021 directory_to_rename
, archived_chunk_name
);
1025 if (archived_chunks_directory
.is_set
) {
1026 lttng_directory_handle_fini(&archived_chunks_directory
.value
);
1028 free(archived_chunk_name
);
1029 if (free_directory_to_rename
) {
1030 free(directory_to_rename
);
1035 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
1036 struct lttng_trace_chunk
*chunk
,
1037 enum lttng_trace_chunk_command_type
*command_type
)
1039 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1041 pthread_mutex_lock(&chunk
->lock
);
1042 if (chunk
->close_command
.is_set
) {
1043 *command_type
= chunk
->close_command
.value
;
1044 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1046 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1048 pthread_mutex_unlock(&chunk
->lock
);
1053 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
1054 struct lttng_trace_chunk
*chunk
,
1055 enum lttng_trace_chunk_command_type close_command
)
1057 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1059 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
1060 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
1061 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1065 pthread_mutex_lock(&chunk
->lock
);
1066 if (chunk
->close_command
.is_set
) {
1067 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
1068 close_command_names
[chunk
->close_command
.value
],
1069 close_command_names
[close_command
]);
1071 DBG("Setting trace chunk close command to \"%s\"",
1072 close_command_names
[close_command
]);
1074 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1075 pthread_mutex_unlock(&chunk
->lock
);
1081 const char *lttng_trace_chunk_command_type_get_name(
1082 enum lttng_trace_chunk_command_type command
)
1085 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1086 return "move to completed trace chunk folder";
1093 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1095 return urcu_ref_get_unless_zero(&chunk
->ref
);
1099 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1101 struct lttng_trace_chunk_registry_element
*element
=
1102 container_of(node
, typeof(*element
), rcu_node
);
1104 lttng_trace_chunk_fini(&element
->chunk
);
1109 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1111 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1114 if (chunk
->close_command
.is_set
) {
1115 close_command_funcs
[chunk
->close_command
.value
](chunk
);
1118 if (chunk
->in_registry_element
) {
1119 struct lttng_trace_chunk_registry_element
*element
;
1121 element
= container_of(chunk
, typeof(*element
), chunk
);
1122 if (element
->registry
) {
1124 cds_lfht_del(element
->registry
->ht
,
1125 &element
->trace_chunk_registry_ht_node
);
1127 call_rcu(&element
->rcu_node
,
1128 free_lttng_trace_chunk_registry_element
);
1130 /* Never published, can be free'd immediately. */
1131 free_lttng_trace_chunk_registry_element(
1132 &element
->rcu_node
);
1135 /* Not RCU-protected, free immediately. */
1136 lttng_trace_chunk_fini(chunk
);
1142 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1147 assert(chunk
->ref
.refcount
);
1148 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1152 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1154 struct lttng_trace_chunk_registry
*registry
;
1156 registry
= zmalloc(sizeof(*registry
));
1161 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1162 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1163 if (!registry
->ht
) {
1169 lttng_trace_chunk_registry_destroy(registry
);
1174 void lttng_trace_chunk_registry_destroy(
1175 struct lttng_trace_chunk_registry
*registry
)
1181 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1188 struct lttng_trace_chunk_registry_element
*
1189 lttng_trace_chunk_registry_element_create_from_chunk(
1190 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1192 struct lttng_trace_chunk_registry_element
*element
=
1193 zmalloc(sizeof(*element
));
1198 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1199 element
->session_id
= session_id
;
1201 element
->chunk
= *chunk
;
1202 lttng_trace_chunk_init(&element
->chunk
);
1203 if (chunk
->session_output_directory
.is_set
) {
1204 element
->chunk
.session_output_directory
.value
=
1205 lttng_directory_handle_move(
1206 &chunk
->session_output_directory
.value
);
1208 if (chunk
->chunk_directory
.is_set
) {
1209 element
->chunk
.chunk_directory
.value
=
1210 lttng_directory_handle_move(
1211 &chunk
->chunk_directory
.value
);
1214 * The original chunk becomes invalid; the name attribute is transferred
1215 * to the new chunk instance.
1218 element
->chunk
.in_registry_element
= true;
1224 struct lttng_trace_chunk
*
1225 lttng_trace_chunk_registry_publish_chunk(
1226 struct lttng_trace_chunk_registry
*registry
,
1227 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
1229 struct lttng_trace_chunk_registry_element
*element
;
1230 unsigned long element_hash
;
1232 pthread_mutex_lock(&chunk
->lock
);
1233 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
1235 pthread_mutex_unlock(&chunk
->lock
);
1240 * chunk is now invalid, the only valid operation is a 'put' from the
1244 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
1248 struct cds_lfht_node
*published_node
;
1249 struct lttng_trace_chunk
*published_chunk
;
1250 struct lttng_trace_chunk_registry_element
*published_element
;
1252 published_node
= cds_lfht_add_unique(registry
->ht
,
1254 lttng_trace_chunk_registry_element_match
,
1256 &element
->trace_chunk_registry_ht_node
);
1257 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
1258 /* Successfully published the new element. */
1259 element
->registry
= registry
;
1260 /* Acquire a reference for the caller. */
1261 if (lttng_trace_chunk_get(&element
->chunk
)) {
1265 * Another thread concurrently unpublished the
1266 * trace chunk. This is currently unexpected.
1268 * Re-attempt to publish.
1270 ERR("Attemp to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
1276 * An equivalent trace chunk was published before this trace
1277 * chunk. Attempt to acquire a reference to the one that was
1278 * already published and release the reference to the copy we
1279 * created if successful.
1281 published_element
= container_of(published_node
,
1282 typeof(*published_element
),
1283 trace_chunk_registry_ht_node
);
1284 published_chunk
= &published_element
->chunk
;
1285 if (lttng_trace_chunk_get(published_chunk
)) {
1286 lttng_trace_chunk_put(&element
->chunk
);
1287 element
= published_element
;
1291 * A reference to the previously published trace chunk could not
1292 * be acquired. Hence, retry to publish our copy of the trace
1298 return element
? &element
->chunk
: NULL
;
1302 * Note that the caller must be registered as an RCU thread.
1303 * However, it does not need to hold the RCU read lock. The RCU read lock is
1304 * acquired to perform the look-up in the registry's hash table and held until
1305 * after a reference to the "found" trace chunk is acquired.
1307 * IOW, holding a reference guarantees the existence of the object for the
1311 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
1312 const struct lttng_trace_chunk_registry
*registry
,
1313 uint64_t session_id
, uint64_t *chunk_id
)
1315 const struct lttng_trace_chunk_registry_element target_element
= {
1316 .chunk
.id
.is_set
= !!chunk_id
,
1317 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
1318 .session_id
= session_id
,
1320 const unsigned long element_hash
=
1321 lttng_trace_chunk_registry_element_hash(
1323 struct cds_lfht_node
*published_node
;
1324 struct lttng_trace_chunk_registry_element
*published_element
;
1325 struct lttng_trace_chunk
*published_chunk
= NULL
;
1326 struct cds_lfht_iter iter
;
1329 cds_lfht_lookup(registry
->ht
,
1331 lttng_trace_chunk_registry_element_match
,
1334 published_node
= cds_lfht_iter_get_node(&iter
);
1335 if (!published_node
) {
1339 published_element
= container_of(published_node
,
1340 typeof(*published_element
),
1341 trace_chunk_registry_ht_node
);
1342 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
1343 published_chunk
= &published_element
->chunk
;
1347 return published_chunk
;
1351 struct lttng_trace_chunk
*
1352 lttng_trace_chunk_registry_find_chunk(
1353 const struct lttng_trace_chunk_registry
*registry
,
1354 uint64_t session_id
, uint64_t chunk_id
)
1356 return _lttng_trace_chunk_registry_find_chunk(registry
,
1357 session_id
, &chunk_id
);
1361 struct lttng_trace_chunk
*
1362 lttng_trace_chunk_registry_find_anonymous_chunk(
1363 const struct lttng_trace_chunk_registry
*registry
,
1364 uint64_t session_id
)
1366 return _lttng_trace_chunk_registry_find_chunk(registry
,
1370 unsigned int lttng_trace_chunk_registry_put_each_chunk(
1371 struct lttng_trace_chunk_registry
*registry
)
1373 struct cds_lfht_iter iter
;
1374 struct lttng_trace_chunk_registry_element
*chunk_element
;
1375 unsigned int trace_chunks_left
= 0;
1377 DBG("Releasing trace chunk registry to all trace chunks");
1379 cds_lfht_for_each_entry(registry
->ht
,
1380 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
1381 const char *chunk_id_str
= "none";
1382 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
1384 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
1385 if (chunk_element
->chunk
.id
.is_set
) {
1388 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
1390 chunk_element
->chunk
.id
.value
);
1391 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
1392 chunk_id_str
= "formatting error";
1394 chunk_id_str
= chunk_id_buf
;
1398 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
1399 "chunk_id = %s, name = \"%s\", status = %s",
1400 chunk_element
->session_id
,
1402 chunk_element
->chunk
.name
? : "none",
1403 chunk_element
->chunk
.close_command
.is_set
?
1405 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
1406 lttng_trace_chunk_put(&chunk_element
->chunk
);
1407 trace_chunks_left
++;
1410 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
1413 return trace_chunks_left
;