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 struct lttng_trace_chunk
{
72 LTTNG_OPTIONAL(enum trace_chunk_mode
) mode
;
74 * First-level directories created within the trace chunk.
75 * Elements are of type 'char *'.
77 struct lttng_dynamic_pointer_array top_level_directories
;
78 /* Is contained within an lttng_trace_chunk_registry_element? */
79 bool in_registry_element
;
82 /* An unset id means the chunk is anonymous. */
83 LTTNG_OPTIONAL(uint64_t) id
;
84 LTTNG_OPTIONAL(time_t) timestamp_creation
;
85 LTTNG_OPTIONAL(time_t) timestamp_close
;
86 LTTNG_OPTIONAL(struct chunk_credentials
) credentials
;
87 LTTNG_OPTIONAL(struct lttng_directory_handle
) session_output_directory
;
88 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory
;
89 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
;
92 /* A trace chunk is uniquely identified by its (session id, chunk id) tuple. */
93 struct lttng_trace_chunk_registry_element
{
95 struct lttng_trace_chunk chunk
;
96 /* Weak and only set when added. */
97 struct lttng_trace_chunk_registry
*registry
;
98 struct cds_lfht_node trace_chunk_registry_ht_node
;
99 /* call_rcu delayed reclaim. */
100 struct rcu_head rcu_node
;
103 struct lttng_trace_chunk_registry
{
107 const char *close_command_names
[] = {
108 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
109 "move to completed chunk folder",
112 chunk_close_command close_command_funcs
[] = {
113 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
114 lttng_trace_chunk_move_to_completed
,
118 bool lttng_trace_chunk_registry_element_equals(
119 const struct lttng_trace_chunk_registry_element
*a
,
120 const struct lttng_trace_chunk_registry_element
*b
)
122 if (a
->session_id
!= b
->session_id
) {
125 if (a
->chunk
.id
.is_set
!= b
->chunk
.id
.is_set
) {
128 if (a
->chunk
.id
.is_set
&& a
->chunk
.id
.value
!= b
->chunk
.id
.value
) {
137 int lttng_trace_chunk_registry_element_match(struct cds_lfht_node
*node
,
140 const struct lttng_trace_chunk_registry_element
*element_a
, *element_b
;
142 element_a
= (const struct lttng_trace_chunk_registry_element
*) key
;
143 element_b
= caa_container_of(node
, typeof(*element_b
),
144 trace_chunk_registry_ht_node
);
145 return lttng_trace_chunk_registry_element_equals(element_a
, element_b
);
149 unsigned long lttng_trace_chunk_registry_element_hash(
150 const struct lttng_trace_chunk_registry_element
*element
)
152 unsigned long hash
= hash_key_u64(&element
->session_id
,
155 if (element
->chunk
.id
.is_set
) {
156 hash
^= hash_key_u64(&element
->chunk
.id
.value
, lttng_ht_seed
);
163 char *generate_chunk_name(uint64_t chunk_id
, time_t creation_timestamp
,
164 const time_t *close_timestamp
)
167 char *new_name
= NULL
;
168 char start_datetime
[sizeof("YYYYmmddTHHMMSS+HHMM")] = {};
169 char end_datetime_suffix
[sizeof("-YYYYmmddTHHMMSS+HHMM")] = {};
171 ret
= time_to_iso8601_str(
173 start_datetime
, sizeof(start_datetime
));
175 ERR("Failed to format trace chunk start date time");
178 if (close_timestamp
) {
179 *end_datetime_suffix
= '-';
180 ret
= time_to_iso8601_str(
182 end_datetime_suffix
+ 1,
183 sizeof(end_datetime_suffix
));
185 ERR("Failed to format trace chunk end date time");
189 new_name
= zmalloc(GENERATED_CHUNK_NAME_LEN
);
191 ERR("Failed to allocate buffer for automatically-generated trace chunk name");
194 ret
= snprintf(new_name
, GENERATED_CHUNK_NAME_LEN
, "%s%s-%" PRIu64
,
195 start_datetime
, end_datetime_suffix
, chunk_id
);
196 if (ret
>= GENERATED_CHUNK_NAME_LEN
|| ret
== -1) {
197 ERR("Failed to format trace chunk name");
208 void lttng_trace_chunk_init(struct lttng_trace_chunk
*chunk
)
210 urcu_ref_init(&chunk
->ref
);
211 pthread_mutex_init(&chunk
->lock
, NULL
);
212 lttng_dynamic_pointer_array_init(&chunk
->top_level_directories
, free
);
216 void lttng_trace_chunk_fini(struct lttng_trace_chunk
*chunk
)
218 if (chunk
->session_output_directory
.is_set
) {
219 lttng_directory_handle_fini(
220 &chunk
->session_output_directory
.value
);
222 if (chunk
->chunk_directory
.is_set
) {
223 lttng_directory_handle_fini(&chunk
->chunk_directory
.value
);
227 lttng_dynamic_pointer_array_reset(&chunk
->top_level_directories
);
228 pthread_mutex_destroy(&chunk
->lock
);
232 struct lttng_trace_chunk
*lttng_trace_chunk_allocate(void)
234 struct lttng_trace_chunk
*chunk
= NULL
;
236 chunk
= zmalloc(sizeof(*chunk
));
238 ERR("Failed to allocate trace chunk");
241 lttng_trace_chunk_init(chunk
);
247 struct lttng_trace_chunk
*lttng_trace_chunk_create_anonymous(void)
249 DBG("Creating anonymous trace chunk");
250 return lttng_trace_chunk_allocate();
254 struct lttng_trace_chunk
*lttng_trace_chunk_create(
255 uint64_t chunk_id
, time_t chunk_creation_time
)
257 struct lttng_trace_chunk
*chunk
;
258 char chunk_creation_datetime_buf
[16] = {};
259 const char *chunk_creation_datetime_str
= "(formatting error)";
260 struct tm timeinfo_buf
, *timeinfo
;
262 timeinfo
= localtime_r(&chunk_creation_time
, &timeinfo_buf
);
266 /* Don't fail because of this; it is only used for logging. */
267 strftime_ret
= strftime(chunk_creation_datetime_buf
,
268 sizeof(chunk_creation_datetime_buf
),
269 "%Y%m%d-%H%M%S", timeinfo
);
271 chunk_creation_datetime_str
=
272 chunk_creation_datetime_buf
;
276 DBG("Creating trace chunk: chunk_id = %" PRIu64
", creation time = %s",
277 chunk_id
, chunk_creation_datetime_str
);
278 chunk
= lttng_trace_chunk_allocate();
283 LTTNG_OPTIONAL_SET(&chunk
->id
, chunk_id
);
284 LTTNG_OPTIONAL_SET(&chunk
->timestamp_creation
, chunk_creation_time
);
286 chunk
->name
= generate_chunk_name(chunk_id
,
287 chunk_creation_time
, NULL
);
289 ERR("Failed to allocate trace chunk name storage");
294 DBG("Chunk name set to \"%s\"", chunk
->name
? : "(none)");
298 lttng_trace_chunk_put(chunk
);
303 enum lttng_trace_chunk_status
lttng_trace_chunk_get_id(
304 struct lttng_trace_chunk
*chunk
, uint64_t *id
)
306 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
308 pthread_mutex_lock(&chunk
->lock
);
309 if (chunk
->id
.is_set
) {
310 *id
= chunk
->id
.value
;
312 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
314 pthread_mutex_unlock(&chunk
->lock
);
319 enum lttng_trace_chunk_status
lttng_trace_chunk_get_creation_timestamp(
320 struct lttng_trace_chunk
*chunk
, time_t *creation_ts
)
323 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
325 pthread_mutex_lock(&chunk
->lock
);
326 if (chunk
->timestamp_creation
.is_set
) {
327 *creation_ts
= chunk
->timestamp_creation
.value
;
329 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
331 pthread_mutex_unlock(&chunk
->lock
);
336 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_timestamp(
337 struct lttng_trace_chunk
*chunk
, time_t *close_ts
)
339 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
341 pthread_mutex_lock(&chunk
->lock
);
342 if (chunk
->timestamp_close
.is_set
) {
343 *close_ts
= chunk
->timestamp_close
.value
;
345 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
347 pthread_mutex_unlock(&chunk
->lock
);
352 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_timestamp(
353 struct lttng_trace_chunk
*chunk
, time_t close_ts
)
355 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
357 pthread_mutex_lock(&chunk
->lock
);
358 if (!chunk
->timestamp_creation
.is_set
) {
359 ERR("Failed to set trace chunk close timestamp: creation timestamp is unset");
360 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
363 if (chunk
->timestamp_creation
.value
> close_ts
) {
364 ERR("Failed to set trace chunk close timestamp: close timestamp is before creation timestamp");
365 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
368 LTTNG_OPTIONAL_SET(&chunk
->timestamp_close
, close_ts
);
369 if (!chunk
->name_overridden
) {
371 chunk
->name
= generate_chunk_name(LTTNG_OPTIONAL_GET(chunk
->id
),
372 LTTNG_OPTIONAL_GET(chunk
->timestamp_creation
),
375 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
379 pthread_mutex_unlock(&chunk
->lock
);
384 enum lttng_trace_chunk_status
lttng_trace_chunk_get_name(
385 struct lttng_trace_chunk
*chunk
, const char **name
,
386 bool *name_overridden
)
388 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
390 pthread_mutex_lock(&chunk
->lock
);
391 if (name_overridden
) {
392 *name_overridden
= chunk
->name_overridden
;
395 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
400 pthread_mutex_unlock(&chunk
->lock
);
405 bool is_valid_chunk_name(const char *name
)
413 len
= strnlen(name
, LTTNG_NAME_MAX
);
414 if (len
== 0 || len
== LTTNG_NAME_MAX
) {
418 if (strchr(name
, '/') || strchr(name
, '.')) {
426 enum lttng_trace_chunk_status
lttng_trace_chunk_override_name(
427 struct lttng_trace_chunk
*chunk
, const char *name
)
431 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
433 if (!is_valid_chunk_name(name
)) {
434 ERR("Attempted to set an invalid name on a trace chunk: name = %s",
436 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
440 pthread_mutex_lock(&chunk
->lock
);
441 if (!chunk
->id
.is_set
) {
442 ERR("Attempted to set an override name on an anonymous trace chunk: name = %s",
444 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
447 new_name
= strdup(name
);
449 ERR("Failed to allocate new trace chunk name");
450 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
454 chunk
->name
= new_name
;
455 chunk
->name_overridden
= true;
457 pthread_mutex_unlock(&chunk
->lock
);
463 enum lttng_trace_chunk_status
lttng_trace_chunk_get_credentials(
464 struct lttng_trace_chunk
*chunk
,
465 struct lttng_credentials
*credentials
)
467 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
469 pthread_mutex_lock(&chunk
->lock
);
470 if (chunk
->credentials
.is_set
) {
471 if (chunk
->credentials
.value
.use_current_user
) {
472 credentials
->uid
= geteuid();
473 credentials
->gid
= getegid();
475 *credentials
= chunk
->credentials
.value
.user
;
478 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
480 pthread_mutex_unlock(&chunk
->lock
);
485 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials(
486 struct lttng_trace_chunk
*chunk
,
487 const struct lttng_credentials
*user_credentials
)
489 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
490 const struct chunk_credentials credentials
= {
491 .user
= *user_credentials
,
492 .use_current_user
= false,
495 pthread_mutex_lock(&chunk
->lock
);
496 if (chunk
->credentials
.is_set
) {
497 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
500 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
502 pthread_mutex_unlock(&chunk
->lock
);
507 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials_current_user(
508 struct lttng_trace_chunk
*chunk
)
510 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
511 const struct chunk_credentials credentials
= {
512 .use_current_user
= true,
515 pthread_mutex_lock(&chunk
->lock
);
516 if (chunk
->credentials
.is_set
) {
517 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
520 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
522 pthread_mutex_unlock(&chunk
->lock
);
528 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_owner(
529 struct lttng_trace_chunk
*chunk
,
530 struct lttng_directory_handle
*session_output_directory
)
533 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
534 struct lttng_directory_handle chunk_directory_handle
;
536 pthread_mutex_lock(&chunk
->lock
);
537 if (chunk
->mode
.is_set
) {
538 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
541 if (!chunk
->credentials
.is_set
) {
543 * Fatal error, credentials must be set before a
544 * directory is created.
546 ERR("Credentials of trace chunk are unset: refusing to set session output directory");
547 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
553 * A nameless chunk does not need its own output directory.
554 * The session's output directory will be used.
556 ret
= lttng_directory_handle_create_subdirectory_as_user(
557 session_output_directory
,
560 !chunk
->credentials
.value
.use_current_user
?
561 &chunk
->credentials
.value
.user
: NULL
);
563 PERROR("Failed to create chunk output directory \"%s\"",
565 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
569 ret
= lttng_directory_handle_init_from_handle(&chunk_directory_handle
,
571 session_output_directory
);
573 /* The function already logs on all error paths. */
574 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
577 LTTNG_OPTIONAL_SET(&chunk
->session_output_directory
,
578 lttng_directory_handle_move(session_output_directory
));
579 LTTNG_OPTIONAL_SET(&chunk
->chunk_directory
,
580 lttng_directory_handle_move(&chunk_directory_handle
));
581 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
583 pthread_mutex_unlock(&chunk
->lock
);
588 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
589 struct lttng_trace_chunk
*chunk
,
590 struct lttng_directory_handle
*chunk_directory
)
592 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
594 pthread_mutex_lock(&chunk
->lock
);
595 if (chunk
->mode
.is_set
) {
596 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
599 if (!chunk
->credentials
.is_set
) {
600 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
601 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
604 LTTNG_OPTIONAL_SET(&chunk
->chunk_directory
,
605 lttng_directory_handle_move(chunk_directory
));
606 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
608 pthread_mutex_unlock(&chunk
->lock
);
613 enum lttng_trace_chunk_status
lttng_trace_chunk_get_chunk_directory_handle(
614 struct lttng_trace_chunk
*chunk
,
615 const struct lttng_directory_handle
**handle
)
617 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
619 pthread_mutex_lock(&chunk
->lock
);
620 if (!chunk
->chunk_directory
.is_set
) {
621 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
625 *handle
= &chunk
->chunk_directory
.value
;
627 pthread_mutex_unlock(&chunk
->lock
);
631 /* Add a top-level directory to the trace chunk if it was previously unknown. */
633 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
634 const char *new_path
)
638 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
639 &chunk
->top_level_directories
);
640 const char *new_path_separator_pos
= strchr(new_path
, '/');
641 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
642 new_path_separator_pos
- new_path
: strlen(new_path
);
644 for (i
= 0; i
< count
; i
++) {
645 const char *path
= lttng_dynamic_pointer_array_get_pointer(
646 &chunk
->top_level_directories
, i
);
647 const ptrdiff_t path_top_level_len
= strlen(path
);
649 if (path_top_level_len
!= new_path_top_level_len
) {
652 if (!strncmp(path
, new_path
, path_top_level_len
)) {
659 char *copy
= strndup(new_path
, new_path_top_level_len
);
661 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
662 new_path
, chunk
->name
? : "(unnamed)");
664 PERROR("Failed to copy path");
668 ret
= lttng_dynamic_pointer_array_add_pointer(
669 &chunk
->top_level_directories
, copy
);
671 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
681 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
682 struct lttng_trace_chunk
*chunk
,
686 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
688 DBG("Creating trace chunk subdirectory \"%s\"", path
);
689 pthread_mutex_lock(&chunk
->lock
);
690 if (!chunk
->credentials
.is_set
) {
692 * Fatal error, credentials must be set before a
693 * directory is created.
695 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
697 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
700 if (!chunk
->mode
.is_set
||
701 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
702 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
704 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
707 if (!chunk
->chunk_directory
.is_set
) {
708 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
710 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
714 ERR("Refusing to create absolute trace chunk directory \"%s\"",
716 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
719 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
720 &chunk
->chunk_directory
.value
, path
,
722 chunk
->credentials
.value
.use_current_user
?
723 NULL
: &chunk
->credentials
.value
.user
);
725 PERROR("Failed to create trace chunk subdirectory \"%s\"",
727 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
730 ret
= add_top_level_directory_unique(chunk
, path
);
732 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
736 pthread_mutex_unlock(&chunk
->lock
);
741 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
742 struct lttng_trace_chunk
*chunk
, const char *file_path
,
743 int flags
, mode_t mode
, int *out_fd
)
746 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
748 DBG("Opening trace chunk file \"%s\"", file_path
);
749 pthread_mutex_lock(&chunk
->lock
);
750 if (!chunk
->credentials
.is_set
) {
752 * Fatal error, credentials must be set before a
755 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
757 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
760 if (!chunk
->chunk_directory
.is_set
) {
761 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
763 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
766 ret
= lttng_directory_handle_open_file_as_user(
767 &chunk
->chunk_directory
.value
, file_path
, flags
, mode
,
768 chunk
->credentials
.value
.use_current_user
?
769 NULL
: &chunk
->credentials
.value
.user
);
771 ERR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
772 file_path
, flags
, (int) mode
);
773 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
778 pthread_mutex_unlock(&chunk
->lock
);
783 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
784 const char *file_path
)
787 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
789 DBG("Unlinking trace chunk file \"%s\"", file_path
);
790 pthread_mutex_lock(&chunk
->lock
);
791 if (!chunk
->credentials
.is_set
) {
793 * Fatal error, credentials must be set before a
794 * directory is created.
796 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
798 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
801 if (!chunk
->chunk_directory
.is_set
) {
802 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
804 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
807 ret
= lttng_directory_handle_unlink_file_as_user(
808 &chunk
->chunk_directory
.value
, file_path
,
809 chunk
->credentials
.value
.use_current_user
?
810 NULL
: &chunk
->credentials
.value
.user
);
812 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
816 pthread_mutex_unlock(&chunk
->lock
);
821 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
)
824 char *directory_to_rename
= NULL
;
825 bool free_directory_to_rename
= false;
826 char *archived_chunk_name
= NULL
;
827 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
828 const time_t creation_timestamp
=
829 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
830 const time_t close_timestamp
=
831 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
832 LTTNG_OPTIONAL(struct lttng_directory_handle
) archived_chunks_directory
= {};
834 if (!trace_chunk
->mode
.is_set
||
835 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
836 !trace_chunk
->session_output_directory
.is_set
) {
838 * This command doesn't need to run if the output is remote
839 * or if the trace chunk is not owned by this process.
844 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
845 assert(!trace_chunk
->name_overridden
);
848 * The fist trace chunk of a session is directly output to the
849 * session's output folder. In this case, the top level directories
850 * must be moved to a temporary folder before that temporary directory
851 * is renamed to match the chunk's name.
854 struct lttng_directory_handle temporary_rename_directory
;
855 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
856 &trace_chunk
->top_level_directories
);
858 ret
= lttng_directory_handle_create_subdirectory_as_user(
859 &trace_chunk
->session_output_directory
.value
,
860 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
862 !trace_chunk
->credentials
.value
.use_current_user
?
863 &trace_chunk
->credentials
.value
.user
: NULL
);
865 PERROR("Failed to create temporary trace chunk rename directory \"%s\"",
866 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
);
869 ret
= lttng_directory_handle_init_from_handle(&temporary_rename_directory
,
870 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
871 &trace_chunk
->session_output_directory
.value
);
873 ERR("Failed to get handle to temporary trace chunk rename directory");
877 for (i
= 0; i
< count
; i
++) {
878 const char *top_level_name
=
879 lttng_dynamic_pointer_array_get_pointer(
880 &trace_chunk
->top_level_directories
, i
);
882 ret
= lttng_directory_handle_rename_as_user(
883 &trace_chunk
->session_output_directory
.value
,
885 &temporary_rename_directory
,
887 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
889 &trace_chunk
->credentials
.value
.user
);
891 PERROR("Failed to move \"%s\" to temporary trace chunk rename directory",
893 lttng_directory_handle_fini(
894 &temporary_rename_directory
);
898 lttng_directory_handle_fini(&temporary_rename_directory
);
899 directory_to_rename
= DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
;
900 free_directory_to_rename
= false;
902 directory_to_rename
= generate_chunk_name(chunk_id
,
903 creation_timestamp
, NULL
);
904 if (!directory_to_rename
) {
905 ERR("Failed to generate initial trace chunk name while renaming trace chunk");
907 free_directory_to_rename
= true;
910 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
912 if (!archived_chunk_name
) {
913 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
917 ret
= lttng_directory_handle_create_subdirectory_as_user(
918 &trace_chunk
->session_output_directory
.value
,
919 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
921 !trace_chunk
->credentials
.value
.use_current_user
?
922 &trace_chunk
->credentials
.value
.user
:
925 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
926 "\" directory for archived trace chunks");
930 ret
= lttng_directory_handle_init_from_handle(
931 &archived_chunks_directory
.value
,
932 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
933 &trace_chunk
->session_output_directory
.value
);
935 PERROR("Failed to get handle to archived trace chunks directory");
938 archived_chunks_directory
.is_set
= true;
940 ret
= lttng_directory_handle_rename_as_user(
941 &trace_chunk
->session_output_directory
.value
,
943 &archived_chunks_directory
.value
,
945 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
947 &trace_chunk
->credentials
.value
.user
);
949 PERROR("Failed to rename folder \"%s\" to \"%s\"",
950 directory_to_rename
, archived_chunk_name
);
954 if (archived_chunks_directory
.is_set
) {
955 lttng_directory_handle_fini(&archived_chunks_directory
.value
);
957 free(archived_chunk_name
);
958 if (free_directory_to_rename
) {
959 free(directory_to_rename
);
964 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
965 struct lttng_trace_chunk
*chunk
,
966 enum lttng_trace_chunk_command_type
*command_type
)
968 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
970 pthread_mutex_lock(&chunk
->lock
);
971 if (chunk
->close_command
.is_set
) {
972 *command_type
= chunk
->close_command
.value
;
973 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
975 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
977 pthread_mutex_unlock(&chunk
->lock
);
982 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
983 struct lttng_trace_chunk
*chunk
,
984 enum lttng_trace_chunk_command_type close_command
)
986 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
988 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
989 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
990 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
994 pthread_mutex_lock(&chunk
->lock
);
995 if (chunk
->close_command
.is_set
) {
996 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
997 close_command_names
[chunk
->close_command
.value
],
998 close_command_names
[close_command
]);
1000 DBG("Setting trace chunk close command to \"%s\"",
1001 close_command_names
[close_command
]);
1003 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1004 pthread_mutex_unlock(&chunk
->lock
);
1010 const char *lttng_trace_chunk_command_type_get_name(
1011 enum lttng_trace_chunk_command_type command
)
1014 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1015 return "move to completed trace chunk folder";
1022 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1024 return urcu_ref_get_unless_zero(&chunk
->ref
);
1028 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1030 struct lttng_trace_chunk_registry_element
*element
=
1031 container_of(node
, typeof(*element
), rcu_node
);
1033 lttng_trace_chunk_fini(&element
->chunk
);
1038 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1040 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1043 if (chunk
->close_command
.is_set
) {
1044 close_command_funcs
[chunk
->close_command
.value
](chunk
);
1047 if (chunk
->in_registry_element
) {
1048 struct lttng_trace_chunk_registry_element
*element
;
1050 element
= container_of(chunk
, typeof(*element
), chunk
);
1051 if (element
->registry
) {
1053 cds_lfht_del(element
->registry
->ht
,
1054 &element
->trace_chunk_registry_ht_node
);
1056 call_rcu(&element
->rcu_node
,
1057 free_lttng_trace_chunk_registry_element
);
1059 /* Never published, can be free'd immediately. */
1060 free_lttng_trace_chunk_registry_element(
1061 &element
->rcu_node
);
1064 /* Not RCU-protected, free immediately. */
1065 lttng_trace_chunk_fini(chunk
);
1071 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1076 assert(chunk
->ref
.refcount
);
1077 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1081 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1083 struct lttng_trace_chunk_registry
*registry
;
1085 registry
= zmalloc(sizeof(*registry
));
1090 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1091 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1092 if (!registry
->ht
) {
1098 lttng_trace_chunk_registry_destroy(registry
);
1103 void lttng_trace_chunk_registry_destroy(
1104 struct lttng_trace_chunk_registry
*registry
)
1110 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1117 struct lttng_trace_chunk_registry_element
*
1118 lttng_trace_chunk_registry_element_create_from_chunk(
1119 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1121 struct lttng_trace_chunk_registry_element
*element
=
1122 zmalloc(sizeof(*element
));
1127 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1128 element
->session_id
= session_id
;
1130 element
->chunk
= *chunk
;
1131 lttng_trace_chunk_init(&element
->chunk
);
1132 if (chunk
->session_output_directory
.is_set
) {
1133 element
->chunk
.session_output_directory
.value
=
1134 lttng_directory_handle_move(
1135 &chunk
->session_output_directory
.value
);
1137 if (chunk
->chunk_directory
.is_set
) {
1138 element
->chunk
.chunk_directory
.value
=
1139 lttng_directory_handle_move(
1140 &chunk
->chunk_directory
.value
);
1143 * The original chunk becomes invalid; the name attribute is transferred
1144 * to the new chunk instance.
1147 element
->chunk
.in_registry_element
= true;
1153 struct lttng_trace_chunk
*
1154 lttng_trace_chunk_registry_publish_chunk(
1155 struct lttng_trace_chunk_registry
*registry
,
1156 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
1158 struct lttng_trace_chunk_registry_element
*element
;
1159 unsigned long element_hash
;
1161 pthread_mutex_lock(&chunk
->lock
);
1162 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
1164 pthread_mutex_unlock(&chunk
->lock
);
1169 * chunk is now invalid, the only valid operation is a 'put' from the
1173 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
1177 struct cds_lfht_node
*published_node
;
1178 struct lttng_trace_chunk
*published_chunk
;
1179 struct lttng_trace_chunk_registry_element
*published_element
;
1181 published_node
= cds_lfht_add_unique(registry
->ht
,
1183 lttng_trace_chunk_registry_element_match
,
1185 &element
->trace_chunk_registry_ht_node
);
1186 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
1187 /* Successfully published the new element. */
1188 element
->registry
= registry
;
1189 /* Acquire a reference for the caller. */
1190 if (lttng_trace_chunk_get(&element
->chunk
)) {
1194 * Another thread concurrently unpublished the
1195 * trace chunk. This is currently unexpected.
1197 * Re-attempt to publish.
1199 ERR("Attemp to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
1205 * An equivalent trace chunk was published before this trace
1206 * chunk. Attempt to acquire a reference to the one that was
1207 * already published and release the reference to the copy we
1208 * created if successful.
1210 published_element
= container_of(published_node
,
1211 typeof(*published_element
),
1212 trace_chunk_registry_ht_node
);
1213 published_chunk
= &published_element
->chunk
;
1214 if (lttng_trace_chunk_get(published_chunk
)) {
1215 lttng_trace_chunk_put(&element
->chunk
);
1216 element
= published_element
;
1220 * A reference to the previously published trace chunk could not
1221 * be acquired. Hence, retry to publish our copy of the trace
1227 return element
? &element
->chunk
: NULL
;
1231 * Note that the caller must be registered as an RCU thread.
1232 * However, it does not need to hold the RCU read lock. The RCU read lock is
1233 * acquired to perform the look-up in the registry's hash table and held until
1234 * after a reference to the "found" trace chunk is acquired.
1236 * IOW, holding a reference guarantees the existence of the object for the
1240 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
1241 const struct lttng_trace_chunk_registry
*registry
,
1242 uint64_t session_id
, uint64_t *chunk_id
)
1244 const struct lttng_trace_chunk_registry_element target_element
= {
1245 .chunk
.id
.is_set
= !!chunk_id
,
1246 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
1247 .session_id
= session_id
,
1249 const unsigned long element_hash
=
1250 lttng_trace_chunk_registry_element_hash(
1252 struct cds_lfht_node
*published_node
;
1253 struct lttng_trace_chunk_registry_element
*published_element
;
1254 struct lttng_trace_chunk
*published_chunk
= NULL
;
1255 struct cds_lfht_iter iter
;
1258 cds_lfht_lookup(registry
->ht
,
1260 lttng_trace_chunk_registry_element_match
,
1263 published_node
= cds_lfht_iter_get_node(&iter
);
1264 if (!published_node
) {
1268 published_element
= container_of(published_node
,
1269 typeof(*published_element
),
1270 trace_chunk_registry_ht_node
);
1271 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
1272 published_chunk
= &published_element
->chunk
;
1276 return published_chunk
;
1280 struct lttng_trace_chunk
*
1281 lttng_trace_chunk_registry_find_chunk(
1282 const struct lttng_trace_chunk_registry
*registry
,
1283 uint64_t session_id
, uint64_t chunk_id
)
1285 return _lttng_trace_chunk_registry_find_chunk(registry
,
1286 session_id
, &chunk_id
);
1290 struct lttng_trace_chunk
*
1291 lttng_trace_chunk_registry_find_anonymous_chunk(
1292 const struct lttng_trace_chunk_registry
*registry
,
1293 uint64_t session_id
)
1295 return _lttng_trace_chunk_registry_find_chunk(registry
,
1299 unsigned int lttng_trace_chunk_registry_put_each_chunk(
1300 struct lttng_trace_chunk_registry
*registry
)
1302 struct cds_lfht_iter iter
;
1303 struct lttng_trace_chunk_registry_element
*chunk_element
;
1304 unsigned int trace_chunks_left
= 0;
1306 DBG("Releasing trace chunk registry to all trace chunks");
1308 cds_lfht_for_each_entry(registry
->ht
,
1309 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
1310 const char *chunk_id_str
= "none";
1311 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
1313 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
1314 if (chunk_element
->chunk
.id
.is_set
) {
1317 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
1319 chunk_element
->chunk
.id
.value
);
1320 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
1321 chunk_id_str
= "formatting error";
1323 chunk_id_str
= chunk_id_buf
;
1327 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
1328 "chunk_id = %s, name = \"%s\", status = %s",
1329 chunk_element
->session_id
,
1331 chunk_element
->chunk
.name
? : "none",
1332 chunk_element
->chunk
.close_command
.is_set
?
1334 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
1335 lttng_trace_chunk_put(&chunk_element
->chunk
);
1336 trace_chunks_left
++;
1339 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
1342 return trace_chunks_left
;