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
;
89 * The creation and close timestamps are NOT monotonic.
90 * They must not be used in context were monotonicity is required.
92 LTTNG_OPTIONAL(time_t) timestamp_creation
;
93 LTTNG_OPTIONAL(time_t) timestamp_close
;
95 LTTNG_OPTIONAL(struct chunk_credentials
) credentials
;
96 LTTNG_OPTIONAL(struct lttng_directory_handle
) session_output_directory
;
97 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory
;
98 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
;
101 /* A trace chunk is uniquely identified by its (session id, chunk id) tuple. */
102 struct lttng_trace_chunk_registry_element
{
103 struct lttng_trace_chunk chunk
;
105 /* Weak and only set when added. */
106 struct lttng_trace_chunk_registry
*registry
;
107 struct cds_lfht_node trace_chunk_registry_ht_node
;
108 /* call_rcu delayed reclaim. */
109 struct rcu_head rcu_node
;
112 struct lttng_trace_chunk_registry
{
117 char *close_command_names
[] = {
118 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
119 "move to completed chunk folder",
123 chunk_close_command close_command_funcs
[] = {
124 [LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
] =
125 lttng_trace_chunk_move_to_completed
,
129 bool lttng_trace_chunk_registry_element_equals(
130 const struct lttng_trace_chunk_registry_element
*a
,
131 const struct lttng_trace_chunk_registry_element
*b
)
133 if (a
->session_id
!= b
->session_id
) {
136 if (a
->chunk
.id
.is_set
!= b
->chunk
.id
.is_set
) {
139 if (a
->chunk
.id
.is_set
&& a
->chunk
.id
.value
!= b
->chunk
.id
.value
) {
148 int lttng_trace_chunk_registry_element_match(struct cds_lfht_node
*node
,
151 const struct lttng_trace_chunk_registry_element
*element_a
, *element_b
;
153 element_a
= (const struct lttng_trace_chunk_registry_element
*) key
;
154 element_b
= caa_container_of(node
, typeof(*element_b
),
155 trace_chunk_registry_ht_node
);
156 return lttng_trace_chunk_registry_element_equals(element_a
, element_b
);
160 unsigned long lttng_trace_chunk_registry_element_hash(
161 const struct lttng_trace_chunk_registry_element
*element
)
163 unsigned long hash
= hash_key_u64(&element
->session_id
,
166 if (element
->chunk
.id
.is_set
) {
167 hash
^= hash_key_u64(&element
->chunk
.id
.value
, lttng_ht_seed
);
174 char *generate_chunk_name(uint64_t chunk_id
, time_t creation_timestamp
,
175 const time_t *close_timestamp
)
178 char *new_name
= NULL
;
179 char start_datetime
[ISO8601_STR_LEN
] = {};
180 /* Add 1 for a '-' prefix. */
181 char end_datetime_suffix
[ISO8601_STR_LEN
+ 1] = {};
183 ret
= time_to_iso8601_str(
185 start_datetime
, sizeof(start_datetime
));
187 ERR("Failed to format trace chunk start date time");
190 if (close_timestamp
) {
191 *end_datetime_suffix
= '-';
192 ret
= time_to_iso8601_str(
194 end_datetime_suffix
+ 1,
195 sizeof(end_datetime_suffix
) - 1);
197 ERR("Failed to format trace chunk end date time");
201 new_name
= zmalloc(GENERATED_CHUNK_NAME_LEN
);
203 ERR("Failed to allocate buffer for automatically-generated trace chunk name");
206 ret
= snprintf(new_name
, GENERATED_CHUNK_NAME_LEN
, "%s%s-%" PRIu64
,
207 start_datetime
, end_datetime_suffix
, chunk_id
);
208 if (ret
>= GENERATED_CHUNK_NAME_LEN
|| ret
== -1) {
209 ERR("Failed to format trace chunk name");
220 void lttng_trace_chunk_init(struct lttng_trace_chunk
*chunk
)
222 urcu_ref_init(&chunk
->ref
);
223 pthread_mutex_init(&chunk
->lock
, NULL
);
224 lttng_dynamic_pointer_array_init(&chunk
->top_level_directories
, free
);
228 void lttng_trace_chunk_fini(struct lttng_trace_chunk
*chunk
)
230 if (chunk
->session_output_directory
.is_set
) {
231 lttng_directory_handle_fini(
232 &chunk
->session_output_directory
.value
);
234 if (chunk
->chunk_directory
.is_set
) {
235 lttng_directory_handle_fini(&chunk
->chunk_directory
.value
);
239 lttng_dynamic_pointer_array_reset(&chunk
->top_level_directories
);
240 pthread_mutex_destroy(&chunk
->lock
);
244 struct lttng_trace_chunk
*lttng_trace_chunk_allocate(void)
246 struct lttng_trace_chunk
*chunk
= NULL
;
248 chunk
= zmalloc(sizeof(*chunk
));
250 ERR("Failed to allocate trace chunk");
253 lttng_trace_chunk_init(chunk
);
259 struct lttng_trace_chunk
*lttng_trace_chunk_create_anonymous(void)
261 DBG("Creating anonymous trace chunk");
262 return lttng_trace_chunk_allocate();
266 struct lttng_trace_chunk
*lttng_trace_chunk_create(
267 uint64_t chunk_id
, time_t chunk_creation_time
)
269 struct lttng_trace_chunk
*chunk
;
270 char chunk_creation_datetime_buf
[16] = {};
271 const char *chunk_creation_datetime_str
= "(formatting error)";
272 struct tm timeinfo_buf
, *timeinfo
;
274 timeinfo
= localtime_r(&chunk_creation_time
, &timeinfo_buf
);
278 /* Don't fail because of this; it is only used for logging. */
279 strftime_ret
= strftime(chunk_creation_datetime_buf
,
280 sizeof(chunk_creation_datetime_buf
),
281 "%Y%m%d-%H%M%S", timeinfo
);
283 chunk_creation_datetime_str
=
284 chunk_creation_datetime_buf
;
288 DBG("Creating trace chunk: chunk_id = %" PRIu64
", creation time = %s",
289 chunk_id
, chunk_creation_datetime_str
);
290 chunk
= lttng_trace_chunk_allocate();
295 LTTNG_OPTIONAL_SET(&chunk
->id
, chunk_id
);
296 LTTNG_OPTIONAL_SET(&chunk
->timestamp_creation
, chunk_creation_time
);
298 chunk
->name
= generate_chunk_name(chunk_id
,
299 chunk_creation_time
, NULL
);
301 ERR("Failed to allocate trace chunk name storage");
306 DBG("Chunk name set to \"%s\"", chunk
->name
? : "(none)");
310 lttng_trace_chunk_put(chunk
);
315 struct lttng_trace_chunk
*lttng_trace_chunk_copy(
316 struct lttng_trace_chunk
*source_chunk
)
318 struct lttng_trace_chunk
*new_chunk
= lttng_trace_chunk_allocate();
324 pthread_mutex_lock(&source_chunk
->lock
);
326 * A new chunk is always a user; it shall create no new trace
329 new_chunk
->mode
= (typeof(new_chunk
->mode
)) {
331 .value
= TRACE_CHUNK_MODE_USER
,
334 * top_level_directories is not copied as it is never used
335 * by _user_ mode chunks.
337 /* The new chunk is not part of a registry (yet, at least). */
338 new_chunk
->in_registry_element
= false;
339 new_chunk
->name_overridden
= source_chunk
->name_overridden
;
340 if (source_chunk
->name
) {
341 new_chunk
->name
= strdup(source_chunk
->name
);
342 if (!new_chunk
->name
) {
343 ERR("Failed to copy source trace chunk name in %s()",
348 new_chunk
->id
= source_chunk
->id
;
349 new_chunk
->timestamp_creation
= source_chunk
->timestamp_creation
;
350 new_chunk
->timestamp_close
= source_chunk
->timestamp_close
;
351 new_chunk
->credentials
= source_chunk
->credentials
;
352 if (source_chunk
->session_output_directory
.is_set
) {
353 if (lttng_directory_handle_copy(
354 &source_chunk
->session_output_directory
.value
,
355 &new_chunk
->session_output_directory
.value
)) {
358 new_chunk
->session_output_directory
.is_set
= true;
361 if (source_chunk
->chunk_directory
.is_set
) {
362 if (lttng_directory_handle_copy(
363 &source_chunk
->chunk_directory
.value
,
364 &new_chunk
->chunk_directory
.value
)) {
367 new_chunk
->chunk_directory
.is_set
= true;
370 new_chunk
->close_command
= source_chunk
->close_command
;
371 pthread_mutex_unlock(&source_chunk
->lock
);
375 pthread_mutex_unlock(&source_chunk
->lock
);
376 lttng_trace_chunk_put(new_chunk
);
381 enum lttng_trace_chunk_status
lttng_trace_chunk_get_id(
382 struct lttng_trace_chunk
*chunk
, uint64_t *id
)
384 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
386 pthread_mutex_lock(&chunk
->lock
);
387 if (chunk
->id
.is_set
) {
388 *id
= chunk
->id
.value
;
390 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
392 pthread_mutex_unlock(&chunk
->lock
);
397 enum lttng_trace_chunk_status
lttng_trace_chunk_get_creation_timestamp(
398 struct lttng_trace_chunk
*chunk
, time_t *creation_ts
)
401 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
403 pthread_mutex_lock(&chunk
->lock
);
404 if (chunk
->timestamp_creation
.is_set
) {
405 *creation_ts
= chunk
->timestamp_creation
.value
;
407 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
409 pthread_mutex_unlock(&chunk
->lock
);
414 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_timestamp(
415 struct lttng_trace_chunk
*chunk
, time_t *close_ts
)
417 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
419 pthread_mutex_lock(&chunk
->lock
);
420 if (chunk
->timestamp_close
.is_set
) {
421 *close_ts
= chunk
->timestamp_close
.value
;
423 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
425 pthread_mutex_unlock(&chunk
->lock
);
430 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_timestamp(
431 struct lttng_trace_chunk
*chunk
, time_t close_ts
)
433 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
435 pthread_mutex_lock(&chunk
->lock
);
436 if (!chunk
->timestamp_creation
.is_set
) {
437 ERR("Failed to set trace chunk close timestamp: creation timestamp is unset");
438 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
443 * Note: we do not enforce that the closing timestamp be greater or
444 * equal to the begin timestamp. These timestamps are used for
445 * generating the chunk name and should only be used in context where
446 * the monotonicity of time is not important. The source of those
447 * timestamps is NOT monotonic and represent the system calendar time,
448 * also know as the wall time.
450 if (chunk
->timestamp_creation
.value
> close_ts
) {
451 WARN("Set trace chunk close timestamp: close timestamp is before creation timestamp, begin : %ld, close : %ld",
452 chunk
->timestamp_creation
.value
, close_ts
);
455 LTTNG_OPTIONAL_SET(&chunk
->timestamp_close
, close_ts
);
456 if (!chunk
->name_overridden
) {
458 chunk
->name
= generate_chunk_name(LTTNG_OPTIONAL_GET(chunk
->id
),
459 LTTNG_OPTIONAL_GET(chunk
->timestamp_creation
),
462 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
466 pthread_mutex_unlock(&chunk
->lock
);
471 enum lttng_trace_chunk_status
lttng_trace_chunk_get_name(
472 struct lttng_trace_chunk
*chunk
, const char **name
,
473 bool *name_overridden
)
475 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
477 pthread_mutex_lock(&chunk
->lock
);
478 if (name_overridden
) {
479 *name_overridden
= chunk
->name_overridden
;
482 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
487 pthread_mutex_unlock(&chunk
->lock
);
492 bool is_valid_chunk_name(const char *name
)
500 len
= lttng_strnlen(name
, LTTNG_NAME_MAX
);
501 if (len
== 0 || len
== LTTNG_NAME_MAX
) {
505 if (strchr(name
, '/') || strchr(name
, '.')) {
513 enum lttng_trace_chunk_status
lttng_trace_chunk_override_name(
514 struct lttng_trace_chunk
*chunk
, const char *name
)
518 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
520 if (!is_valid_chunk_name(name
)) {
521 ERR("Attempted to set an invalid name on a trace chunk: name = %s",
523 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
527 pthread_mutex_lock(&chunk
->lock
);
528 if (!chunk
->id
.is_set
) {
529 ERR("Attempted to set an override name on an anonymous trace chunk: name = %s",
531 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
534 new_name
= strdup(name
);
536 ERR("Failed to allocate new trace chunk name");
537 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
541 chunk
->name
= new_name
;
542 chunk
->name_overridden
= true;
544 pthread_mutex_unlock(&chunk
->lock
);
550 enum lttng_trace_chunk_status
lttng_trace_chunk_get_credentials(
551 struct lttng_trace_chunk
*chunk
,
552 struct lttng_credentials
*credentials
)
554 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
556 pthread_mutex_lock(&chunk
->lock
);
557 if (chunk
->credentials
.is_set
) {
558 if (chunk
->credentials
.value
.use_current_user
) {
559 credentials
->uid
= geteuid();
560 credentials
->gid
= getegid();
562 *credentials
= chunk
->credentials
.value
.user
;
565 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
567 pthread_mutex_unlock(&chunk
->lock
);
572 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials(
573 struct lttng_trace_chunk
*chunk
,
574 const struct lttng_credentials
*user_credentials
)
576 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
577 const struct chunk_credentials credentials
= {
578 .user
= *user_credentials
,
579 .use_current_user
= false,
582 pthread_mutex_lock(&chunk
->lock
);
583 if (chunk
->credentials
.is_set
) {
584 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
587 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
589 pthread_mutex_unlock(&chunk
->lock
);
594 enum lttng_trace_chunk_status
lttng_trace_chunk_set_credentials_current_user(
595 struct lttng_trace_chunk
*chunk
)
597 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
598 const struct chunk_credentials credentials
= {
599 .use_current_user
= true,
602 pthread_mutex_lock(&chunk
->lock
);
603 if (chunk
->credentials
.is_set
) {
604 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
607 LTTNG_OPTIONAL_SET(&chunk
->credentials
, credentials
);
609 pthread_mutex_unlock(&chunk
->lock
);
615 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_owner(
616 struct lttng_trace_chunk
*chunk
,
617 struct lttng_directory_handle
*session_output_directory
)
620 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
621 struct lttng_directory_handle chunk_directory_handle
;
623 pthread_mutex_lock(&chunk
->lock
);
624 if (chunk
->mode
.is_set
) {
625 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
628 if (!chunk
->credentials
.is_set
) {
630 * Fatal error, credentials must be set before a
631 * directory is created.
633 ERR("Credentials of trace chunk are unset: refusing to set session output directory");
634 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
640 * A nameless chunk does not need its own output directory.
641 * The session's output directory will be used.
643 ret
= lttng_directory_handle_create_subdirectory_as_user(
644 session_output_directory
,
647 !chunk
->credentials
.value
.use_current_user
?
648 &chunk
->credentials
.value
.user
: NULL
);
650 PERROR("Failed to create chunk output directory \"%s\"",
652 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
656 ret
= lttng_directory_handle_init_from_handle(&chunk_directory_handle
,
658 session_output_directory
);
660 /* The function already logs on all error paths. */
661 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
664 LTTNG_OPTIONAL_SET(&chunk
->session_output_directory
,
665 lttng_directory_handle_move(session_output_directory
));
666 LTTNG_OPTIONAL_SET(&chunk
->chunk_directory
,
667 lttng_directory_handle_move(&chunk_directory_handle
));
668 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
670 pthread_mutex_unlock(&chunk
->lock
);
675 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
676 struct lttng_trace_chunk
*chunk
,
677 struct lttng_directory_handle
*chunk_directory
)
679 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
681 pthread_mutex_lock(&chunk
->lock
);
682 if (chunk
->mode
.is_set
) {
683 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
686 if (!chunk
->credentials
.is_set
) {
687 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
688 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
691 LTTNG_OPTIONAL_SET(&chunk
->chunk_directory
,
692 lttng_directory_handle_move(chunk_directory
));
693 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
695 pthread_mutex_unlock(&chunk
->lock
);
700 enum lttng_trace_chunk_status
lttng_trace_chunk_get_chunk_directory_handle(
701 struct lttng_trace_chunk
*chunk
,
702 const struct lttng_directory_handle
**handle
)
704 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
706 pthread_mutex_lock(&chunk
->lock
);
707 if (!chunk
->chunk_directory
.is_set
) {
708 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
712 *handle
= &chunk
->chunk_directory
.value
;
714 pthread_mutex_unlock(&chunk
->lock
);
718 /* Add a top-level directory to the trace chunk if it was previously unknown. */
720 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
721 const char *new_path
)
725 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
726 &chunk
->top_level_directories
);
727 const char *new_path_separator_pos
= strchr(new_path
, '/');
728 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
729 new_path_separator_pos
- new_path
: strlen(new_path
);
731 for (i
= 0; i
< count
; i
++) {
732 const char *path
= lttng_dynamic_pointer_array_get_pointer(
733 &chunk
->top_level_directories
, i
);
734 const ptrdiff_t path_top_level_len
= strlen(path
);
736 if (path_top_level_len
!= new_path_top_level_len
) {
739 if (!strncmp(path
, new_path
, path_top_level_len
)) {
746 char *copy
= lttng_strndup(new_path
, new_path_top_level_len
);
748 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
749 new_path
, chunk
->name
? : "(unnamed)");
751 PERROR("Failed to copy path");
755 ret
= lttng_dynamic_pointer_array_add_pointer(
756 &chunk
->top_level_directories
, copy
);
758 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
768 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
769 struct lttng_trace_chunk
*chunk
,
773 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
775 DBG("Creating trace chunk subdirectory \"%s\"", path
);
776 pthread_mutex_lock(&chunk
->lock
);
777 if (!chunk
->credentials
.is_set
) {
779 * Fatal error, credentials must be set before a
780 * directory is created.
782 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
784 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
787 if (!chunk
->mode
.is_set
||
788 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
789 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
791 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
794 if (!chunk
->chunk_directory
.is_set
) {
795 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
797 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
801 ERR("Refusing to create absolute trace chunk directory \"%s\"",
803 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
806 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
807 &chunk
->chunk_directory
.value
, path
,
809 chunk
->credentials
.value
.use_current_user
?
810 NULL
: &chunk
->credentials
.value
.user
);
812 PERROR("Failed to create trace chunk subdirectory \"%s\"",
814 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
817 ret
= add_top_level_directory_unique(chunk
, path
);
819 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
823 pthread_mutex_unlock(&chunk
->lock
);
828 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
829 struct lttng_trace_chunk
*chunk
, const char *file_path
,
830 int flags
, mode_t mode
, int *out_fd
)
833 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
835 DBG("Opening trace chunk file \"%s\"", file_path
);
836 pthread_mutex_lock(&chunk
->lock
);
837 if (!chunk
->credentials
.is_set
) {
839 * Fatal error, credentials must be set before a
842 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
844 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
847 if (!chunk
->chunk_directory
.is_set
) {
848 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
850 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
853 ret
= lttng_directory_handle_open_file_as_user(
854 &chunk
->chunk_directory
.value
, file_path
, flags
, mode
,
855 chunk
->credentials
.value
.use_current_user
?
856 NULL
: &chunk
->credentials
.value
.user
);
858 PERROR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
859 file_path
, flags
, (int) mode
);
860 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
865 pthread_mutex_unlock(&chunk
->lock
);
870 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
871 const char *file_path
)
874 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
876 DBG("Unlinking trace chunk file \"%s\"", file_path
);
877 pthread_mutex_lock(&chunk
->lock
);
878 if (!chunk
->credentials
.is_set
) {
880 * Fatal error, credentials must be set before a
881 * directory is created.
883 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
885 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
888 if (!chunk
->chunk_directory
.is_set
) {
889 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
891 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
894 ret
= lttng_directory_handle_unlink_file_as_user(
895 &chunk
->chunk_directory
.value
, file_path
,
896 chunk
->credentials
.value
.use_current_user
?
897 NULL
: &chunk
->credentials
.value
.user
);
899 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
903 pthread_mutex_unlock(&chunk
->lock
);
908 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
)
911 char *directory_to_rename
= NULL
;
912 bool free_directory_to_rename
= false;
913 char *archived_chunk_name
= NULL
;
914 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
915 const time_t creation_timestamp
=
916 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
917 const time_t close_timestamp
=
918 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
919 LTTNG_OPTIONAL(struct lttng_directory_handle
) archived_chunks_directory
= {};
921 if (!trace_chunk
->mode
.is_set
||
922 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
923 !trace_chunk
->session_output_directory
.is_set
) {
925 * This command doesn't need to run if the output is remote
926 * or if the trace chunk is not owned by this process.
931 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
932 assert(!trace_chunk
->name_overridden
);
935 * The fist trace chunk of a session is directly output to the
936 * session's output folder. In this case, the top level directories
937 * must be moved to a temporary folder before that temporary directory
938 * is renamed to match the chunk's name.
941 struct lttng_directory_handle temporary_rename_directory
;
942 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
943 &trace_chunk
->top_level_directories
);
945 ret
= lttng_directory_handle_create_subdirectory_as_user(
946 &trace_chunk
->session_output_directory
.value
,
947 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
949 !trace_chunk
->credentials
.value
.use_current_user
?
950 &trace_chunk
->credentials
.value
.user
: NULL
);
952 PERROR("Failed to create temporary trace chunk rename directory \"%s\"",
953 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
);
956 ret
= lttng_directory_handle_init_from_handle(&temporary_rename_directory
,
957 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
958 &trace_chunk
->session_output_directory
.value
);
960 ERR("Failed to get handle to temporary trace chunk rename directory");
964 for (i
= 0; i
< count
; i
++) {
965 const char *top_level_name
=
966 lttng_dynamic_pointer_array_get_pointer(
967 &trace_chunk
->top_level_directories
, i
);
969 ret
= lttng_directory_handle_rename_as_user(
970 &trace_chunk
->session_output_directory
.value
,
972 &temporary_rename_directory
,
974 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
976 &trace_chunk
->credentials
.value
.user
);
978 PERROR("Failed to move \"%s\" to temporary trace chunk rename directory",
980 lttng_directory_handle_fini(
981 &temporary_rename_directory
);
985 lttng_directory_handle_fini(&temporary_rename_directory
);
986 directory_to_rename
= DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
;
987 free_directory_to_rename
= false;
989 directory_to_rename
= generate_chunk_name(chunk_id
,
990 creation_timestamp
, NULL
);
991 if (!directory_to_rename
) {
992 ERR("Failed to generate initial trace chunk name while renaming trace chunk");
995 free_directory_to_rename
= true;
998 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
1000 if (!archived_chunk_name
) {
1001 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
1005 ret
= lttng_directory_handle_create_subdirectory_as_user(
1006 &trace_chunk
->session_output_directory
.value
,
1007 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1009 !trace_chunk
->credentials
.value
.use_current_user
?
1010 &trace_chunk
->credentials
.value
.user
:
1013 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
1014 "\" directory for archived trace chunks");
1018 ret
= lttng_directory_handle_init_from_handle(
1019 &archived_chunks_directory
.value
,
1020 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1021 &trace_chunk
->session_output_directory
.value
);
1023 PERROR("Failed to get handle to archived trace chunks directory");
1026 archived_chunks_directory
.is_set
= true;
1028 ret
= lttng_directory_handle_rename_as_user(
1029 &trace_chunk
->session_output_directory
.value
,
1030 directory_to_rename
,
1031 &archived_chunks_directory
.value
,
1032 archived_chunk_name
,
1033 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
1035 &trace_chunk
->credentials
.value
.user
);
1037 PERROR("Failed to rename folder \"%s\" to \"%s\"",
1038 directory_to_rename
, archived_chunk_name
);
1042 if (archived_chunks_directory
.is_set
) {
1043 lttng_directory_handle_fini(&archived_chunks_directory
.value
);
1045 free(archived_chunk_name
);
1046 if (free_directory_to_rename
) {
1047 free(directory_to_rename
);
1052 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
1053 struct lttng_trace_chunk
*chunk
,
1054 enum lttng_trace_chunk_command_type
*command_type
)
1056 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1058 pthread_mutex_lock(&chunk
->lock
);
1059 if (chunk
->close_command
.is_set
) {
1060 *command_type
= chunk
->close_command
.value
;
1061 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1063 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1065 pthread_mutex_unlock(&chunk
->lock
);
1070 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
1071 struct lttng_trace_chunk
*chunk
,
1072 enum lttng_trace_chunk_command_type close_command
)
1074 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1076 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
1077 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
1078 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1082 pthread_mutex_lock(&chunk
->lock
);
1083 if (chunk
->close_command
.is_set
) {
1084 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
1085 close_command_names
[chunk
->close_command
.value
],
1086 close_command_names
[close_command
]);
1088 DBG("Setting trace chunk close command to \"%s\"",
1089 close_command_names
[close_command
]);
1091 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1092 pthread_mutex_unlock(&chunk
->lock
);
1098 const char *lttng_trace_chunk_command_type_get_name(
1099 enum lttng_trace_chunk_command_type command
)
1102 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1103 return "move to completed trace chunk folder";
1110 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1112 return urcu_ref_get_unless_zero(&chunk
->ref
);
1116 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1118 struct lttng_trace_chunk_registry_element
*element
=
1119 container_of(node
, typeof(*element
), rcu_node
);
1121 lttng_trace_chunk_fini(&element
->chunk
);
1126 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1128 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1131 if (chunk
->close_command
.is_set
) {
1132 close_command_funcs
[chunk
->close_command
.value
](chunk
);
1135 if (chunk
->in_registry_element
) {
1136 struct lttng_trace_chunk_registry_element
*element
;
1138 element
= container_of(chunk
, typeof(*element
), chunk
);
1139 if (element
->registry
) {
1141 cds_lfht_del(element
->registry
->ht
,
1142 &element
->trace_chunk_registry_ht_node
);
1144 call_rcu(&element
->rcu_node
,
1145 free_lttng_trace_chunk_registry_element
);
1147 /* Never published, can be free'd immediately. */
1148 free_lttng_trace_chunk_registry_element(
1149 &element
->rcu_node
);
1152 /* Not RCU-protected, free immediately. */
1153 lttng_trace_chunk_fini(chunk
);
1159 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1164 assert(chunk
->ref
.refcount
);
1165 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1169 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1171 struct lttng_trace_chunk_registry
*registry
;
1173 registry
= zmalloc(sizeof(*registry
));
1178 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1179 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1180 if (!registry
->ht
) {
1186 lttng_trace_chunk_registry_destroy(registry
);
1191 void lttng_trace_chunk_registry_destroy(
1192 struct lttng_trace_chunk_registry
*registry
)
1198 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1205 struct lttng_trace_chunk_registry_element
*
1206 lttng_trace_chunk_registry_element_create_from_chunk(
1207 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1209 struct lttng_trace_chunk_registry_element
*element
=
1210 zmalloc(sizeof(*element
));
1215 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1216 element
->session_id
= session_id
;
1218 element
->chunk
= *chunk
;
1219 lttng_trace_chunk_init(&element
->chunk
);
1220 if (chunk
->session_output_directory
.is_set
) {
1221 element
->chunk
.session_output_directory
.value
=
1222 lttng_directory_handle_move(
1223 &chunk
->session_output_directory
.value
);
1225 if (chunk
->chunk_directory
.is_set
) {
1226 element
->chunk
.chunk_directory
.value
=
1227 lttng_directory_handle_move(
1228 &chunk
->chunk_directory
.value
);
1231 * The original chunk becomes invalid; the name attribute is transferred
1232 * to the new chunk instance.
1235 element
->chunk
.in_registry_element
= true;
1241 struct lttng_trace_chunk
*
1242 lttng_trace_chunk_registry_publish_chunk(
1243 struct lttng_trace_chunk_registry
*registry
,
1244 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
1246 struct lttng_trace_chunk_registry_element
*element
;
1247 unsigned long element_hash
;
1249 pthread_mutex_lock(&chunk
->lock
);
1250 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
1252 pthread_mutex_unlock(&chunk
->lock
);
1257 * chunk is now invalid, the only valid operation is a 'put' from the
1261 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
1265 struct cds_lfht_node
*published_node
;
1266 struct lttng_trace_chunk
*published_chunk
;
1267 struct lttng_trace_chunk_registry_element
*published_element
;
1269 published_node
= cds_lfht_add_unique(registry
->ht
,
1271 lttng_trace_chunk_registry_element_match
,
1273 &element
->trace_chunk_registry_ht_node
);
1274 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
1275 /* Successfully published the new element. */
1276 element
->registry
= registry
;
1277 /* Acquire a reference for the caller. */
1278 if (lttng_trace_chunk_get(&element
->chunk
)) {
1282 * Another thread concurrently unpublished the
1283 * trace chunk. This is currently unexpected.
1285 * Re-attempt to publish.
1287 ERR("Attempt to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
1293 * An equivalent trace chunk was published before this trace
1294 * chunk. Attempt to acquire a reference to the one that was
1295 * already published and release the reference to the copy we
1296 * created if successful.
1298 published_element
= container_of(published_node
,
1299 typeof(*published_element
),
1300 trace_chunk_registry_ht_node
);
1301 published_chunk
= &published_element
->chunk
;
1302 if (lttng_trace_chunk_get(published_chunk
)) {
1303 lttng_trace_chunk_put(&element
->chunk
);
1304 element
= published_element
;
1308 * A reference to the previously published trace chunk could not
1309 * be acquired. Hence, retry to publish our copy of the trace
1315 return element
? &element
->chunk
: NULL
;
1319 * Note that the caller must be registered as an RCU thread.
1320 * However, it does not need to hold the RCU read lock. The RCU read lock is
1321 * acquired to perform the look-up in the registry's hash table and held until
1322 * after a reference to the "found" trace chunk is acquired.
1324 * IOW, holding a reference guarantees the existence of the object for the
1328 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
1329 const struct lttng_trace_chunk_registry
*registry
,
1330 uint64_t session_id
, uint64_t *chunk_id
)
1332 const struct lttng_trace_chunk_registry_element target_element
= {
1333 .chunk
.id
.is_set
= !!chunk_id
,
1334 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
1335 .session_id
= session_id
,
1337 const unsigned long element_hash
=
1338 lttng_trace_chunk_registry_element_hash(
1340 struct cds_lfht_node
*published_node
;
1341 struct lttng_trace_chunk_registry_element
*published_element
;
1342 struct lttng_trace_chunk
*published_chunk
= NULL
;
1343 struct cds_lfht_iter iter
;
1346 cds_lfht_lookup(registry
->ht
,
1348 lttng_trace_chunk_registry_element_match
,
1351 published_node
= cds_lfht_iter_get_node(&iter
);
1352 if (!published_node
) {
1356 published_element
= container_of(published_node
,
1357 typeof(*published_element
),
1358 trace_chunk_registry_ht_node
);
1359 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
1360 published_chunk
= &published_element
->chunk
;
1364 return published_chunk
;
1368 struct lttng_trace_chunk
*
1369 lttng_trace_chunk_registry_find_chunk(
1370 const struct lttng_trace_chunk_registry
*registry
,
1371 uint64_t session_id
, uint64_t chunk_id
)
1373 return _lttng_trace_chunk_registry_find_chunk(registry
,
1374 session_id
, &chunk_id
);
1378 int lttng_trace_chunk_registry_chunk_exists(
1379 const struct lttng_trace_chunk_registry
*registry
,
1380 uint64_t session_id
, uint64_t chunk_id
, bool *chunk_exists
)
1383 const struct lttng_trace_chunk_registry_element target_element
= {
1384 .chunk
.id
.is_set
= true,
1385 .chunk
.id
.value
= chunk_id
,
1386 .session_id
= session_id
,
1388 const unsigned long element_hash
=
1389 lttng_trace_chunk_registry_element_hash(
1391 struct cds_lfht_node
*published_node
;
1392 struct cds_lfht_iter iter
;
1395 cds_lfht_lookup(registry
->ht
,
1397 lttng_trace_chunk_registry_element_match
,
1400 published_node
= cds_lfht_iter_get_node(&iter
);
1401 if (!published_node
) {
1402 *chunk_exists
= false;
1406 *chunk_exists
= !cds_lfht_is_node_deleted(published_node
);
1413 struct lttng_trace_chunk
*
1414 lttng_trace_chunk_registry_find_anonymous_chunk(
1415 const struct lttng_trace_chunk_registry
*registry
,
1416 uint64_t session_id
)
1418 return _lttng_trace_chunk_registry_find_chunk(registry
,
1422 unsigned int lttng_trace_chunk_registry_put_each_chunk(
1423 struct lttng_trace_chunk_registry
*registry
)
1425 struct cds_lfht_iter iter
;
1426 struct lttng_trace_chunk_registry_element
*chunk_element
;
1427 unsigned int trace_chunks_left
= 0;
1429 DBG("Releasing trace chunk registry to all trace chunks");
1431 cds_lfht_for_each_entry(registry
->ht
,
1432 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
1433 const char *chunk_id_str
= "none";
1434 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
1436 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
1437 if (chunk_element
->chunk
.id
.is_set
) {
1440 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
1442 chunk_element
->chunk
.id
.value
);
1443 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
1444 chunk_id_str
= "formatting error";
1446 chunk_id_str
= chunk_id_buf
;
1450 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
1451 "chunk_id = %s, name = \"%s\", status = %s",
1452 chunk_element
->session_id
,
1454 chunk_element
->chunk
.name
? : "none",
1455 chunk_element
->chunk
.close_command
.is_set
?
1457 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
1458 lttng_trace_chunk_put(&chunk_element
->chunk
);
1459 trace_chunks_left
++;
1462 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
1465 return trace_chunks_left
;