| 1 | /* |
| 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_TRACE_CHUNK_H |
| 9 | #define LTTNG_TRACE_CHUNK_H |
| 10 | |
| 11 | #include <common/compat/directory-handle.hpp> |
| 12 | #include <common/credentials.hpp> |
| 13 | #include <common/fd-tracker/fd-tracker.hpp> |
| 14 | #include <common/macros.hpp> |
| 15 | #include <stdbool.h> |
| 16 | #include <stddef.h> |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | /* |
| 20 | * A trace chunk is a group of directories and files forming a (or a set of) |
| 21 | * complete and independant trace(s). For instance, a trace archive chunk, |
| 22 | * a snapshot, or a regular LTTng trace are all instances of a trace archive. |
| 23 | * |
| 24 | * A trace chunk is always contained within a session output directory. |
| 25 | * |
| 26 | * This facility is used by the session daemon, consumer daemon(s), and relay |
| 27 | * daemon to: |
| 28 | * - Control file (data stream, metadata, and index) creation relative to |
| 29 | * a given output directory, |
| 30 | * - Track the use of an output directory by other objects in order to |
| 31 | * know if/when an output directory can be safely consumed, renamed, |
| 32 | * deleted, etc. |
| 33 | * |
| 34 | * |
| 35 | * OWNER VS USER |
| 36 | * --- |
| 37 | * |
| 38 | * A trace chunk can either be a owner or a user of its |
| 39 | * "chunk output directory". |
| 40 | * |
| 41 | * A "user" trace chunk is provided with a handle to the chunk output directory |
| 42 | * which can then be used to create subdirectories and files. |
| 43 | * |
| 44 | * An "owner" chunk, on top of being able to perform the operations of a "user" |
| 45 | * chunk can perform operations on its chunk output directory, such as renaming |
| 46 | * or deleting it. |
| 47 | * |
| 48 | * A trace chunk becomes an "owner" or "user" chunk based on which of |
| 49 | * 'lttng_trace_chunk_set_as_owner()' or 'lttng_trace_chunk_set_as_user()' is |
| 50 | * used. These methods are _exclusive_ and must only be used once on a |
| 51 | * trace chunk. |
| 52 | */ |
| 53 | |
| 54 | struct lttng_trace_chunk; |
| 55 | struct fd_tracker; |
| 56 | |
| 57 | enum lttng_trace_chunk_status { |
| 58 | LTTNG_TRACE_CHUNK_STATUS_OK, |
| 59 | LTTNG_TRACE_CHUNK_STATUS_NONE, |
| 60 | LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT, |
| 61 | LTTNG_TRACE_CHUNK_STATUS_INVALID_OPERATION, |
| 62 | LTTNG_TRACE_CHUNK_STATUS_ERROR, |
| 63 | LTTNG_TRACE_CHUNK_STATUS_NO_FILE, |
| 64 | }; |
| 65 | |
| 66 | enum lttng_trace_chunk_command_type { |
| 67 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED = 0, |
| 68 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION = 1, |
| 69 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE = 2, |
| 70 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MAX, |
| 71 | }; |
| 72 | |
| 73 | struct lttng_trace_chunk *lttng_trace_chunk_create_anonymous(); |
| 74 | |
| 75 | struct lttng_trace_chunk *lttng_trace_chunk_create( |
| 76 | uint64_t chunk_id, |
| 77 | time_t chunk_creation_time, |
| 78 | const char *path); |
| 79 | |
| 80 | void lttng_trace_chunk_set_fd_tracker(struct lttng_trace_chunk *chunk, |
| 81 | struct fd_tracker *fd_tracker); |
| 82 | |
| 83 | /* |
| 84 | * Copy a trace chunk. The copy that is returned is always a _user_ |
| 85 | * mode chunk even if the source chunk was an _owner_ as there can never be |
| 86 | * two _owners_ of the same trace output. |
| 87 | */ |
| 88 | struct lttng_trace_chunk *lttng_trace_chunk_copy( |
| 89 | struct lttng_trace_chunk *source_chunk); |
| 90 | |
| 91 | enum lttng_trace_chunk_status lttng_trace_chunk_get_id( |
| 92 | struct lttng_trace_chunk *chunk, uint64_t *id); |
| 93 | |
| 94 | enum lttng_trace_chunk_status lttng_trace_chunk_get_creation_timestamp( |
| 95 | struct lttng_trace_chunk *chunk, time_t *creation_ts); |
| 96 | |
| 97 | enum lttng_trace_chunk_status lttng_trace_chunk_get_close_timestamp( |
| 98 | struct lttng_trace_chunk *chunk, time_t *close_ts); |
| 99 | |
| 100 | enum lttng_trace_chunk_status lttng_trace_chunk_set_close_timestamp( |
| 101 | struct lttng_trace_chunk *chunk, time_t close_ts); |
| 102 | |
| 103 | enum lttng_trace_chunk_status lttng_trace_chunk_get_name( |
| 104 | struct lttng_trace_chunk *chunk, const char **name, |
| 105 | bool *name_overridden); |
| 106 | |
| 107 | bool lttng_trace_chunk_get_name_overridden(struct lttng_trace_chunk *chunk); |
| 108 | |
| 109 | enum lttng_trace_chunk_status lttng_trace_chunk_override_name( |
| 110 | struct lttng_trace_chunk *chunk, const char *name); |
| 111 | |
| 112 | enum lttng_trace_chunk_status lttng_trace_chunk_rename_path( |
| 113 | struct lttng_trace_chunk *chunk, const char *path); |
| 114 | |
| 115 | enum lttng_trace_chunk_status lttng_trace_chunk_get_credentials( |
| 116 | struct lttng_trace_chunk *chunk, |
| 117 | struct lttng_credentials *credentials); |
| 118 | |
| 119 | enum lttng_trace_chunk_status lttng_trace_chunk_set_credentials( |
| 120 | struct lttng_trace_chunk *chunk, |
| 121 | const struct lttng_credentials *credentials); |
| 122 | |
| 123 | enum lttng_trace_chunk_status lttng_trace_chunk_set_credentials_current_user( |
| 124 | struct lttng_trace_chunk *chunk); |
| 125 | |
| 126 | enum lttng_trace_chunk_status lttng_trace_chunk_set_as_owner( |
| 127 | struct lttng_trace_chunk *chunk, |
| 128 | struct lttng_directory_handle *session_output_directory); |
| 129 | |
| 130 | enum lttng_trace_chunk_status lttng_trace_chunk_set_as_user( |
| 131 | struct lttng_trace_chunk *chunk, |
| 132 | struct lttng_directory_handle *chunk_directory); |
| 133 | |
| 134 | enum lttng_trace_chunk_status |
| 135 | lttng_trace_chunk_get_session_output_directory_handle( |
| 136 | struct lttng_trace_chunk *chunk, |
| 137 | struct lttng_directory_handle **handle); |
| 138 | |
| 139 | enum lttng_trace_chunk_status lttng_trace_chunk_borrow_chunk_directory_handle( |
| 140 | struct lttng_trace_chunk *chunk, |
| 141 | const struct lttng_directory_handle **handle); |
| 142 | |
| 143 | enum lttng_trace_chunk_status lttng_trace_chunk_create_subdirectory( |
| 144 | struct lttng_trace_chunk *chunk, |
| 145 | const char *subdirectory_path); |
| 146 | |
| 147 | enum lttng_trace_chunk_status lttng_trace_chunk_open_file( |
| 148 | struct lttng_trace_chunk *chunk, |
| 149 | const char *filename, |
| 150 | int flags, |
| 151 | mode_t mode, |
| 152 | int *out_fd, |
| 153 | bool expect_no_file); |
| 154 | |
| 155 | enum lttng_trace_chunk_status lttng_trace_chunk_open_fs_handle( |
| 156 | struct lttng_trace_chunk *chunk, |
| 157 | const char *filename, |
| 158 | int flags, |
| 159 | mode_t mode, |
| 160 | struct fs_handle **out_handle, |
| 161 | bool expect_no_file); |
| 162 | |
| 163 | int lttng_trace_chunk_unlink_file(struct lttng_trace_chunk *chunk, |
| 164 | const char *filename); |
| 165 | |
| 166 | enum lttng_trace_chunk_status lttng_trace_chunk_get_close_command( |
| 167 | struct lttng_trace_chunk *chunk, |
| 168 | enum lttng_trace_chunk_command_type *command_type); |
| 169 | |
| 170 | enum lttng_trace_chunk_status lttng_trace_chunk_set_close_command( |
| 171 | struct lttng_trace_chunk *chunk, |
| 172 | enum lttng_trace_chunk_command_type command_type); |
| 173 | |
| 174 | const char *lttng_trace_chunk_command_type_get_name( |
| 175 | enum lttng_trace_chunk_command_type command); |
| 176 | |
| 177 | bool lttng_trace_chunk_ids_equal(const struct lttng_trace_chunk *chunk_a, |
| 178 | const struct lttng_trace_chunk *chunk_b); |
| 179 | |
| 180 | /* Returns true on success. */ |
| 181 | bool lttng_trace_chunk_get(struct lttng_trace_chunk *chunk); |
| 182 | |
| 183 | void lttng_trace_chunk_put(struct lttng_trace_chunk *chunk); |
| 184 | |
| 185 | #endif /* LTTNG_TRACE_CHUNK_H */ |