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 struct lttng_directory_handle
*session_output_directory
;
91 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
) {
225 lttng_directory_handle_put(
226 chunk
->session_output_directory
);
227 chunk
->session_output_directory
= NULL
;
229 if (chunk
->chunk_directory
) {
230 lttng_directory_handle_put(chunk
->chunk_directory
);
231 chunk
->chunk_directory
= NULL
;
235 lttng_dynamic_pointer_array_reset(&chunk
->top_level_directories
);
236 pthread_mutex_destroy(&chunk
->lock
);
240 struct lttng_trace_chunk
*lttng_trace_chunk_allocate(void)
242 struct lttng_trace_chunk
*chunk
= NULL
;
244 chunk
= zmalloc(sizeof(*chunk
));
246 ERR("Failed to allocate trace chunk");
249 lttng_trace_chunk_init(chunk
);
255 struct lttng_trace_chunk
*lttng_trace_chunk_create_anonymous(void)
257 DBG("Creating anonymous trace chunk");
258 return lttng_trace_chunk_allocate();
262 struct lttng_trace_chunk
*lttng_trace_chunk_create(
263 uint64_t chunk_id
, time_t chunk_creation_time
)
265 struct lttng_trace_chunk
*chunk
;
266 char chunk_creation_datetime_buf
[16] = {};
267 const char *chunk_creation_datetime_str
= "(formatting error)";
268 struct tm timeinfo_buf
, *timeinfo
;
270 timeinfo
= localtime_r(&chunk_creation_time
, &timeinfo_buf
);
274 /* Don't fail because of this; it is only used for logging. */
275 strftime_ret
= strftime(chunk_creation_datetime_buf
,
276 sizeof(chunk_creation_datetime_buf
),
277 "%Y%m%d-%H%M%S", timeinfo
);
279 chunk_creation_datetime_str
=
280 chunk_creation_datetime_buf
;
284 DBG("Creating trace chunk: chunk_id = %" PRIu64
", creation time = %s",
285 chunk_id
, chunk_creation_datetime_str
);
286 chunk
= lttng_trace_chunk_allocate();
291 LTTNG_OPTIONAL_SET(&chunk
->id
, chunk_id
);
292 LTTNG_OPTIONAL_SET(&chunk
->timestamp_creation
, chunk_creation_time
);
294 chunk
->name
= generate_chunk_name(chunk_id
,
295 chunk_creation_time
, NULL
);
297 ERR("Failed to allocate trace chunk name storage");
302 DBG("Chunk name set to \"%s\"", chunk
->name
? : "(none)");
306 lttng_trace_chunk_put(chunk
);
311 struct lttng_trace_chunk
*lttng_trace_chunk_copy(
312 struct lttng_trace_chunk
*source_chunk
)
314 struct lttng_trace_chunk
*new_chunk
= lttng_trace_chunk_allocate();
320 pthread_mutex_lock(&source_chunk
->lock
);
322 * A new chunk is always a user; it shall create no new trace
325 new_chunk
->mode
= (typeof(new_chunk
->mode
)) {
327 .value
= TRACE_CHUNK_MODE_USER
,
330 * top_level_directories is not copied as it is never used
331 * by _user_ mode chunks.
333 /* The new chunk is not part of a registry (yet, at least). */
334 new_chunk
->in_registry_element
= false;
335 new_chunk
->name_overridden
= source_chunk
->name_overridden
;
336 if (source_chunk
->name
) {
337 new_chunk
->name
= strdup(source_chunk
->name
);
338 if (!new_chunk
->name
) {
339 ERR("Failed to copy source trace chunk name in %s()",
344 new_chunk
->id
= source_chunk
->id
;
345 new_chunk
->timestamp_creation
= source_chunk
->timestamp_creation
;
346 new_chunk
->timestamp_close
= source_chunk
->timestamp_close
;
347 new_chunk
->credentials
= source_chunk
->credentials
;
348 if (source_chunk
->session_output_directory
) {
349 const bool reference_acquired
= lttng_directory_handle_get(
350 source_chunk
->session_output_directory
);
352 assert(reference_acquired
);
353 new_chunk
->session_output_directory
=
354 source_chunk
->session_output_directory
;
356 if (source_chunk
->chunk_directory
) {
357 const bool reference_acquired
= lttng_directory_handle_get(
358 source_chunk
->chunk_directory
);
360 assert(reference_acquired
);
361 new_chunk
->chunk_directory
= source_chunk
->chunk_directory
;
363 new_chunk
->close_command
= source_chunk
->close_command
;
364 pthread_mutex_unlock(&source_chunk
->lock
);
368 pthread_mutex_unlock(&source_chunk
->lock
);
369 lttng_trace_chunk_put(new_chunk
);
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
= NULL
;
606 bool reference_acquired
;
608 pthread_mutex_lock(&chunk
->lock
);
609 if (chunk
->mode
.is_set
) {
610 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
613 if (!chunk
->credentials
.is_set
) {
615 * Fatal error, credentials must be set before a
616 * directory is created.
618 ERR("Credentials of trace chunk are unset: refusing to set session output directory");
619 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
625 * A nameless chunk does not need its own output directory.
626 * The session's output directory will be used.
628 ret
= lttng_directory_handle_create_subdirectory_as_user(
629 session_output_directory
,
632 !chunk
->credentials
.value
.use_current_user
?
633 &chunk
->credentials
.value
.user
: NULL
);
635 PERROR("Failed to create chunk output directory \"%s\"",
637 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
641 chunk_directory_handle
= lttng_directory_handle_create_from_handle(
643 session_output_directory
);
644 if (!chunk_directory_handle
) {
645 /* The function already logs on all error paths. */
646 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
649 chunk
->chunk_directory
= chunk_directory_handle
;
650 chunk_directory_handle
= NULL
;
651 reference_acquired
= lttng_directory_handle_get(
652 session_output_directory
);
653 assert(reference_acquired
);
654 chunk
->session_output_directory
= session_output_directory
;
655 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_OWNER
);
657 pthread_mutex_unlock(&chunk
->lock
);
662 enum lttng_trace_chunk_status
lttng_trace_chunk_set_as_user(
663 struct lttng_trace_chunk
*chunk
,
664 struct lttng_directory_handle
*chunk_directory
)
666 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
667 bool reference_acquired
;
669 pthread_mutex_lock(&chunk
->lock
);
670 if (chunk
->mode
.is_set
) {
671 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
674 if (!chunk
->credentials
.is_set
) {
675 ERR("Credentials of trace chunk are unset: refusing to set chunk output directory");
676 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
679 reference_acquired
= lttng_directory_handle_get(chunk_directory
);
680 assert(reference_acquired
);
681 chunk
->chunk_directory
= chunk_directory
;
682 LTTNG_OPTIONAL_SET(&chunk
->mode
, TRACE_CHUNK_MODE_USER
);
684 pthread_mutex_unlock(&chunk
->lock
);
689 enum lttng_trace_chunk_status
lttng_trace_chunk_borrow_chunk_directory_handle(
690 struct lttng_trace_chunk
*chunk
,
691 const struct lttng_directory_handle
**handle
)
693 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
695 pthread_mutex_lock(&chunk
->lock
);
696 if (!chunk
->chunk_directory
) {
697 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
701 *handle
= chunk
->chunk_directory
;
703 pthread_mutex_unlock(&chunk
->lock
);
707 /* Add a top-level directory to the trace chunk if it was previously unknown. */
709 int add_top_level_directory_unique(struct lttng_trace_chunk
*chunk
,
710 const char *new_path
)
714 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
715 &chunk
->top_level_directories
);
716 const char *new_path_separator_pos
= strchr(new_path
, '/');
717 const ptrdiff_t new_path_top_level_len
= new_path_separator_pos
?
718 new_path_separator_pos
- new_path
: strlen(new_path
);
720 for (i
= 0; i
< count
; i
++) {
721 const char *path
= lttng_dynamic_pointer_array_get_pointer(
722 &chunk
->top_level_directories
, i
);
723 const ptrdiff_t path_top_level_len
= strlen(path
);
725 if (path_top_level_len
!= new_path_top_level_len
) {
728 if (!strncmp(path
, new_path
, path_top_level_len
)) {
735 char *copy
= lttng_strndup(new_path
, new_path_top_level_len
);
737 DBG("Adding new top-level directory \"%s\" to trace chunk \"%s\"",
738 new_path
, chunk
->name
? : "(unnamed)");
740 PERROR("Failed to copy path");
744 ret
= lttng_dynamic_pointer_array_add_pointer(
745 &chunk
->top_level_directories
, copy
);
747 ERR("Allocation failure while adding top-level directory entry to a trace chunk");
757 enum lttng_trace_chunk_status
lttng_trace_chunk_create_subdirectory(
758 struct lttng_trace_chunk
*chunk
,
762 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
764 DBG("Creating trace chunk subdirectory \"%s\"", path
);
765 pthread_mutex_lock(&chunk
->lock
);
766 if (!chunk
->credentials
.is_set
) {
768 * Fatal error, credentials must be set before a
769 * directory is created.
771 ERR("Credentials of trace chunk are unset: refusing to create subdirectory \"%s\"",
773 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
776 if (!chunk
->mode
.is_set
||
777 chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
) {
778 ERR("Attempted to create trace chunk subdirectory \"%s\" through a non-owner chunk",
780 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION
;
783 if (!chunk
->chunk_directory
) {
784 ERR("Attempted to create trace chunk subdirectory \"%s\" before setting the chunk output directory",
786 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
790 ERR("Refusing to create absolute trace chunk directory \"%s\"",
792 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
795 ret
= lttng_directory_handle_create_subdirectory_recursive_as_user(
796 chunk
->chunk_directory
, path
,
798 chunk
->credentials
.value
.use_current_user
?
799 NULL
: &chunk
->credentials
.value
.user
);
801 PERROR("Failed to create trace chunk subdirectory \"%s\"",
803 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
806 ret
= add_top_level_directory_unique(chunk
, path
);
808 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
812 pthread_mutex_unlock(&chunk
->lock
);
817 enum lttng_trace_chunk_status
lttng_trace_chunk_open_file(
818 struct lttng_trace_chunk
*chunk
, const char *file_path
,
819 int flags
, mode_t mode
, int *out_fd
)
822 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
824 DBG("Opening trace chunk file \"%s\"", file_path
);
825 pthread_mutex_lock(&chunk
->lock
);
826 if (!chunk
->credentials
.is_set
) {
828 * Fatal error, credentials must be set before a
831 ERR("Credentials of trace chunk are unset: refusing to open file \"%s\"",
833 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
836 if (!chunk
->chunk_directory
) {
837 ERR("Attempted to open trace chunk file \"%s\" before setting the chunk output directory",
839 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
842 ret
= lttng_directory_handle_open_file_as_user(
843 chunk
->chunk_directory
, file_path
, flags
, mode
,
844 chunk
->credentials
.value
.use_current_user
?
845 NULL
: &chunk
->credentials
.value
.user
);
847 PERROR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
848 file_path
, flags
, (int) mode
);
849 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
854 pthread_mutex_unlock(&chunk
->lock
);
859 int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk
*chunk
,
860 const char *file_path
)
863 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
865 DBG("Unlinking trace chunk file \"%s\"", file_path
);
866 pthread_mutex_lock(&chunk
->lock
);
867 if (!chunk
->credentials
.is_set
) {
869 * Fatal error, credentials must be set before a
870 * directory is created.
872 ERR("Credentials of trace chunk are unset: refusing to unlink file \"%s\"",
874 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
877 if (!chunk
->chunk_directory
) {
878 ERR("Attempted to unlink trace chunk file \"%s\" before setting the chunk output directory",
880 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
883 ret
= lttng_directory_handle_unlink_file_as_user(
884 chunk
->chunk_directory
, file_path
,
885 chunk
->credentials
.value
.use_current_user
?
886 NULL
: &chunk
->credentials
.value
.user
);
888 status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
892 pthread_mutex_unlock(&chunk
->lock
);
897 void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk
*trace_chunk
)
900 char *directory_to_rename
= NULL
;
901 bool free_directory_to_rename
= false;
902 char *archived_chunk_name
= NULL
;
903 const uint64_t chunk_id
= LTTNG_OPTIONAL_GET(trace_chunk
->id
);
904 const time_t creation_timestamp
=
905 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_creation
);
906 const time_t close_timestamp
=
907 LTTNG_OPTIONAL_GET(trace_chunk
->timestamp_close
);
908 struct lttng_directory_handle
*archived_chunks_directory
= NULL
;
910 if (!trace_chunk
->mode
.is_set
||
911 trace_chunk
->mode
.value
!= TRACE_CHUNK_MODE_OWNER
||
912 !trace_chunk
->session_output_directory
) {
914 * This command doesn't need to run if the output is remote
915 * or if the trace chunk is not owned by this process.
920 assert(trace_chunk
->mode
.value
== TRACE_CHUNK_MODE_OWNER
);
921 assert(!trace_chunk
->name_overridden
);
924 * The fist trace chunk of a session is directly output to the
925 * session's output folder. In this case, the top level directories
926 * must be moved to a temporary folder before that temporary directory
927 * is renamed to match the chunk's name.
930 struct lttng_directory_handle
*temporary_rename_directory
=
932 size_t i
, count
= lttng_dynamic_pointer_array_get_count(
933 &trace_chunk
->top_level_directories
);
935 ret
= lttng_directory_handle_create_subdirectory_as_user(
936 trace_chunk
->session_output_directory
,
937 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
939 !trace_chunk
->credentials
.value
.use_current_user
?
940 &trace_chunk
->credentials
.value
.user
: NULL
);
942 PERROR("Failed to create temporary trace chunk rename directory \"%s\"",
943 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
);
946 temporary_rename_directory
= lttng_directory_handle_create_from_handle(
947 DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
,
948 trace_chunk
->session_output_directory
);
949 if (!temporary_rename_directory
) {
950 ERR("Failed to get handle to temporary trace chunk rename directory");
954 for (i
= 0; i
< count
; i
++) {
955 const char *top_level_name
=
956 lttng_dynamic_pointer_array_get_pointer(
957 &trace_chunk
->top_level_directories
, i
);
959 ret
= lttng_directory_handle_rename_as_user(
960 trace_chunk
->session_output_directory
,
962 temporary_rename_directory
,
964 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
966 &trace_chunk
->credentials
.value
.user
);
968 PERROR("Failed to move \"%s\" to temporary trace chunk rename directory",
970 lttng_directory_handle_put(
971 temporary_rename_directory
);
975 lttng_directory_handle_put(temporary_rename_directory
);
976 directory_to_rename
= DEFAULT_TEMPORARY_CHUNK_RENAME_DIRECTORY
;
977 free_directory_to_rename
= false;
979 directory_to_rename
= generate_chunk_name(chunk_id
,
980 creation_timestamp
, NULL
);
981 if (!directory_to_rename
) {
982 ERR("Failed to generate initial trace chunk name while renaming trace chunk");
985 free_directory_to_rename
= true;
988 archived_chunk_name
= generate_chunk_name(chunk_id
, creation_timestamp
,
990 if (!archived_chunk_name
) {
991 ERR("Failed to generate archived trace chunk name while renaming trace chunk");
995 ret
= lttng_directory_handle_create_subdirectory_as_user(
996 trace_chunk
->session_output_directory
,
997 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
999 !trace_chunk
->credentials
.value
.use_current_user
?
1000 &trace_chunk
->credentials
.value
.user
:
1003 PERROR("Failed to create \"" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
1004 "\" directory for archived trace chunks");
1008 archived_chunks_directory
= lttng_directory_handle_create_from_handle(
1009 DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
,
1010 trace_chunk
->session_output_directory
);
1011 if (!archived_chunks_directory
) {
1012 PERROR("Failed to get handle to archived trace chunks directory");
1016 ret
= lttng_directory_handle_rename_as_user(
1017 trace_chunk
->session_output_directory
,
1018 directory_to_rename
,
1019 archived_chunks_directory
,
1020 archived_chunk_name
,
1021 LTTNG_OPTIONAL_GET(trace_chunk
->credentials
).use_current_user
?
1023 &trace_chunk
->credentials
.value
.user
);
1025 PERROR("Failed to rename folder \"%s\" to \"%s\"",
1026 directory_to_rename
, archived_chunk_name
);
1030 lttng_directory_handle_put(archived_chunks_directory
);
1031 free(archived_chunk_name
);
1032 if (free_directory_to_rename
) {
1033 free(directory_to_rename
);
1038 enum lttng_trace_chunk_status
lttng_trace_chunk_get_close_command(
1039 struct lttng_trace_chunk
*chunk
,
1040 enum lttng_trace_chunk_command_type
*command_type
)
1042 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1044 pthread_mutex_lock(&chunk
->lock
);
1045 if (chunk
->close_command
.is_set
) {
1046 *command_type
= chunk
->close_command
.value
;
1047 status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1049 status
= LTTNG_TRACE_CHUNK_STATUS_NONE
;
1051 pthread_mutex_unlock(&chunk
->lock
);
1056 enum lttng_trace_chunk_status
lttng_trace_chunk_set_close_command(
1057 struct lttng_trace_chunk
*chunk
,
1058 enum lttng_trace_chunk_command_type close_command
)
1060 enum lttng_trace_chunk_status status
= LTTNG_TRACE_CHUNK_STATUS_OK
;
1062 if (close_command
< LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
||
1063 close_command
>= LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX
) {
1064 status
= LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
;
1068 pthread_mutex_lock(&chunk
->lock
);
1069 if (chunk
->close_command
.is_set
) {
1070 DBG("Overriding trace chunk close command from \"%s\" to \"%s\"",
1071 close_command_names
[chunk
->close_command
.value
],
1072 close_command_names
[close_command
]);
1074 DBG("Setting trace chunk close command to \"%s\"",
1075 close_command_names
[close_command
]);
1077 LTTNG_OPTIONAL_SET(&chunk
->close_command
, close_command
);
1078 pthread_mutex_unlock(&chunk
->lock
);
1084 const char *lttng_trace_chunk_command_type_get_name(
1085 enum lttng_trace_chunk_command_type command
)
1088 case LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
:
1089 return "move to completed trace chunk folder";
1096 bool lttng_trace_chunk_get(struct lttng_trace_chunk
*chunk
)
1098 return urcu_ref_get_unless_zero(&chunk
->ref
);
1102 void free_lttng_trace_chunk_registry_element(struct rcu_head
*node
)
1104 struct lttng_trace_chunk_registry_element
*element
=
1105 container_of(node
, typeof(*element
), rcu_node
);
1107 lttng_trace_chunk_fini(&element
->chunk
);
1112 void lttng_trace_chunk_release(struct urcu_ref
*ref
)
1114 struct lttng_trace_chunk
*chunk
= container_of(ref
, typeof(*chunk
),
1117 if (chunk
->close_command
.is_set
) {
1118 close_command_funcs
[chunk
->close_command
.value
](chunk
);
1121 if (chunk
->in_registry_element
) {
1122 struct lttng_trace_chunk_registry_element
*element
;
1124 element
= container_of(chunk
, typeof(*element
), chunk
);
1125 if (element
->registry
) {
1127 cds_lfht_del(element
->registry
->ht
,
1128 &element
->trace_chunk_registry_ht_node
);
1130 call_rcu(&element
->rcu_node
,
1131 free_lttng_trace_chunk_registry_element
);
1133 /* Never published, can be free'd immediately. */
1134 free_lttng_trace_chunk_registry_element(
1135 &element
->rcu_node
);
1138 /* Not RCU-protected, free immediately. */
1139 lttng_trace_chunk_fini(chunk
);
1145 void lttng_trace_chunk_put(struct lttng_trace_chunk
*chunk
)
1150 assert(chunk
->ref
.refcount
);
1151 urcu_ref_put(&chunk
->ref
, lttng_trace_chunk_release
);
1155 struct lttng_trace_chunk_registry
*lttng_trace_chunk_registry_create(void)
1157 struct lttng_trace_chunk_registry
*registry
;
1159 registry
= zmalloc(sizeof(*registry
));
1164 registry
->ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1165 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
1166 if (!registry
->ht
) {
1172 lttng_trace_chunk_registry_destroy(registry
);
1177 void lttng_trace_chunk_registry_destroy(
1178 struct lttng_trace_chunk_registry
*registry
)
1184 int ret
= cds_lfht_destroy(registry
->ht
, NULL
);
1191 struct lttng_trace_chunk_registry_element
*
1192 lttng_trace_chunk_registry_element_create_from_chunk(
1193 struct lttng_trace_chunk
*chunk
, uint64_t session_id
)
1195 struct lttng_trace_chunk_registry_element
*element
=
1196 zmalloc(sizeof(*element
));
1201 cds_lfht_node_init(&element
->trace_chunk_registry_ht_node
);
1202 element
->session_id
= session_id
;
1204 element
->chunk
= *chunk
;
1205 lttng_trace_chunk_init(&element
->chunk
);
1206 if (chunk
->session_output_directory
) {
1207 /* Transferred ownership. */
1208 element
->chunk
.session_output_directory
=
1209 chunk
->session_output_directory
;
1210 chunk
->session_output_directory
= NULL
;
1212 if (chunk
->chunk_directory
) {
1213 /* Transferred ownership. */
1214 element
->chunk
.chunk_directory
= chunk
->chunk_directory
;
1215 chunk
->chunk_directory
= NULL
;
1218 * The original chunk becomes invalid; the name attribute is transferred
1219 * to the new chunk instance.
1222 element
->chunk
.in_registry_element
= true;
1228 struct lttng_trace_chunk
*
1229 lttng_trace_chunk_registry_publish_chunk(
1230 struct lttng_trace_chunk_registry
*registry
,
1231 uint64_t session_id
, struct lttng_trace_chunk
*chunk
)
1233 struct lttng_trace_chunk_registry_element
*element
;
1234 unsigned long element_hash
;
1236 pthread_mutex_lock(&chunk
->lock
);
1237 element
= lttng_trace_chunk_registry_element_create_from_chunk(chunk
,
1239 pthread_mutex_unlock(&chunk
->lock
);
1244 * chunk is now invalid, the only valid operation is a 'put' from the
1248 element_hash
= lttng_trace_chunk_registry_element_hash(element
);
1252 struct cds_lfht_node
*published_node
;
1253 struct lttng_trace_chunk
*published_chunk
;
1254 struct lttng_trace_chunk_registry_element
*published_element
;
1256 published_node
= cds_lfht_add_unique(registry
->ht
,
1258 lttng_trace_chunk_registry_element_match
,
1260 &element
->trace_chunk_registry_ht_node
);
1261 if (published_node
== &element
->trace_chunk_registry_ht_node
) {
1262 /* Successfully published the new element. */
1263 element
->registry
= registry
;
1264 /* Acquire a reference for the caller. */
1265 if (lttng_trace_chunk_get(&element
->chunk
)) {
1269 * Another thread concurrently unpublished the
1270 * trace chunk. This is currently unexpected.
1272 * Re-attempt to publish.
1274 ERR("Attempt to publish a trace chunk to the chunk registry raced with a trace chunk deletion");
1280 * An equivalent trace chunk was published before this trace
1281 * chunk. Attempt to acquire a reference to the one that was
1282 * already published and release the reference to the copy we
1283 * created if successful.
1285 published_element
= container_of(published_node
,
1286 typeof(*published_element
),
1287 trace_chunk_registry_ht_node
);
1288 published_chunk
= &published_element
->chunk
;
1289 if (lttng_trace_chunk_get(published_chunk
)) {
1290 lttng_trace_chunk_put(&element
->chunk
);
1291 element
= published_element
;
1295 * A reference to the previously published trace chunk could not
1296 * be acquired. Hence, retry to publish our copy of the trace
1302 return element
? &element
->chunk
: NULL
;
1306 * Note that the caller must be registered as an RCU thread.
1307 * However, it does not need to hold the RCU read lock. The RCU read lock is
1308 * acquired to perform the look-up in the registry's hash table and held until
1309 * after a reference to the "found" trace chunk is acquired.
1311 * IOW, holding a reference guarantees the existence of the object for the
1315 struct lttng_trace_chunk
*_lttng_trace_chunk_registry_find_chunk(
1316 const struct lttng_trace_chunk_registry
*registry
,
1317 uint64_t session_id
, uint64_t *chunk_id
)
1319 const struct lttng_trace_chunk_registry_element target_element
= {
1320 .chunk
.id
.is_set
= !!chunk_id
,
1321 .chunk
.id
.value
= chunk_id
? *chunk_id
: 0,
1322 .session_id
= session_id
,
1324 const unsigned long element_hash
=
1325 lttng_trace_chunk_registry_element_hash(
1327 struct cds_lfht_node
*published_node
;
1328 struct lttng_trace_chunk_registry_element
*published_element
;
1329 struct lttng_trace_chunk
*published_chunk
= NULL
;
1330 struct cds_lfht_iter iter
;
1333 cds_lfht_lookup(registry
->ht
,
1335 lttng_trace_chunk_registry_element_match
,
1338 published_node
= cds_lfht_iter_get_node(&iter
);
1339 if (!published_node
) {
1343 published_element
= container_of(published_node
,
1344 typeof(*published_element
),
1345 trace_chunk_registry_ht_node
);
1346 if (lttng_trace_chunk_get(&published_element
->chunk
)) {
1347 published_chunk
= &published_element
->chunk
;
1351 return published_chunk
;
1355 struct lttng_trace_chunk
*
1356 lttng_trace_chunk_registry_find_chunk(
1357 const struct lttng_trace_chunk_registry
*registry
,
1358 uint64_t session_id
, uint64_t chunk_id
)
1360 return _lttng_trace_chunk_registry_find_chunk(registry
,
1361 session_id
, &chunk_id
);
1365 int lttng_trace_chunk_registry_chunk_exists(
1366 const struct lttng_trace_chunk_registry
*registry
,
1367 uint64_t session_id
, uint64_t chunk_id
, bool *chunk_exists
)
1370 const struct lttng_trace_chunk_registry_element target_element
= {
1371 .chunk
.id
.is_set
= true,
1372 .chunk
.id
.value
= chunk_id
,
1373 .session_id
= session_id
,
1375 const unsigned long element_hash
=
1376 lttng_trace_chunk_registry_element_hash(
1378 struct cds_lfht_node
*published_node
;
1379 struct cds_lfht_iter iter
;
1382 cds_lfht_lookup(registry
->ht
,
1384 lttng_trace_chunk_registry_element_match
,
1387 published_node
= cds_lfht_iter_get_node(&iter
);
1388 if (!published_node
) {
1389 *chunk_exists
= false;
1393 *chunk_exists
= !cds_lfht_is_node_deleted(published_node
);
1400 struct lttng_trace_chunk
*
1401 lttng_trace_chunk_registry_find_anonymous_chunk(
1402 const struct lttng_trace_chunk_registry
*registry
,
1403 uint64_t session_id
)
1405 return _lttng_trace_chunk_registry_find_chunk(registry
,
1409 unsigned int lttng_trace_chunk_registry_put_each_chunk(
1410 struct lttng_trace_chunk_registry
*registry
)
1412 struct cds_lfht_iter iter
;
1413 struct lttng_trace_chunk_registry_element
*chunk_element
;
1414 unsigned int trace_chunks_left
= 0;
1416 DBG("Releasing trace chunk registry to all trace chunks");
1418 cds_lfht_for_each_entry(registry
->ht
,
1419 &iter
, chunk_element
, trace_chunk_registry_ht_node
) {
1420 const char *chunk_id_str
= "none";
1421 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
1423 pthread_mutex_lock(&chunk_element
->chunk
.lock
);
1424 if (chunk_element
->chunk
.id
.is_set
) {
1427 fmt_ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
),
1429 chunk_element
->chunk
.id
.value
);
1430 if (fmt_ret
< 0 || fmt_ret
>= sizeof(chunk_id_buf
)) {
1431 chunk_id_str
= "formatting error";
1433 chunk_id_str
= chunk_id_buf
;
1437 DBG("Releasing reference to trace chunk: session_id = %" PRIu64
1438 "chunk_id = %s, name = \"%s\", status = %s",
1439 chunk_element
->session_id
,
1441 chunk_element
->chunk
.name
? : "none",
1442 chunk_element
->chunk
.close_command
.is_set
?
1444 pthread_mutex_unlock(&chunk_element
->chunk
.lock
);
1445 lttng_trace_chunk_put(&chunk_element
->chunk
);
1446 trace_chunks_left
++;
1449 DBG("Released reference to %u trace chunks in %s()", trace_chunks_left
,
1452 return trace_chunks_left
;