| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <assert.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <fcntl.h> |
| 15 | |
| 16 | #include <lttng/constant.h> |
| 17 | #include <common/common.h> |
| 18 | #include <common/defaults.h> |
| 19 | #include <common/compat/endian.h> |
| 20 | #include <common/utils.h> |
| 21 | |
| 22 | #include "index.h" |
| 23 | |
| 24 | #define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC) |
| 25 | #define READ_ONLY_FILE_FLAGS O_RDONLY |
| 26 | |
| 27 | static enum lttng_trace_chunk_status _lttng_index_file_create_from_trace_chunk( |
| 28 | struct lttng_trace_chunk *chunk, |
| 29 | const char *channel_path, const char *stream_name, |
| 30 | uint64_t stream_file_size, uint64_t stream_file_index, |
| 31 | uint32_t index_major, uint32_t index_minor, |
| 32 | bool unlink_existing_file, |
| 33 | int flags, bool expect_no_file, struct lttng_index_file **file) |
| 34 | { |
| 35 | struct lttng_index_file *index_file; |
| 36 | enum lttng_trace_chunk_status chunk_status; |
| 37 | int ret; |
| 38 | struct fs_handle *fs_handle = NULL; |
| 39 | ssize_t size_ret; |
| 40 | struct ctf_packet_index_file_hdr hdr; |
| 41 | char index_directory_path[LTTNG_PATH_MAX]; |
| 42 | char index_file_path[LTTNG_PATH_MAX]; |
| 43 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 44 | const bool acquired_reference = lttng_trace_chunk_get(chunk); |
| 45 | const char *separator; |
| 46 | |
| 47 | assert(acquired_reference); |
| 48 | |
| 49 | index_file = zmalloc(sizeof(*index_file)); |
| 50 | if (!index_file) { |
| 51 | PERROR("Failed to allocate lttng_index_file"); |
| 52 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | index_file->trace_chunk = chunk; |
| 57 | if (channel_path[0] == '\0') { |
| 58 | separator = ""; |
| 59 | } else { |
| 60 | separator = "/"; |
| 61 | } |
| 62 | ret = snprintf(index_directory_path, sizeof(index_directory_path), |
| 63 | "%s%s" DEFAULT_INDEX_DIR, channel_path, separator); |
| 64 | if (ret < 0 || ret >= sizeof(index_directory_path)) { |
| 65 | ERR("Failed to format index directory path"); |
| 66 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 67 | goto error; |
| 68 | } |
| 69 | |
| 70 | ret = utils_stream_file_path(index_directory_path, stream_name, |
| 71 | stream_file_size, stream_file_index, |
| 72 | DEFAULT_INDEX_FILE_SUFFIX, |
| 73 | index_file_path, sizeof(index_file_path)); |
| 74 | if (ret) { |
| 75 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 76 | goto error; |
| 77 | } |
| 78 | |
| 79 | if (unlink_existing_file) { |
| 80 | /* |
| 81 | * For tracefile rotation. We need to unlink the old |
| 82 | * file if present to synchronize with the tail of the |
| 83 | * live viewer which could be working on this same file. |
| 84 | * By doing so, any reference to the old index file |
| 85 | * stays valid even if we re-create a new file with the |
| 86 | * same name afterwards. |
| 87 | */ |
| 88 | chunk_status = lttng_trace_chunk_unlink_file( |
| 89 | chunk, index_file_path); |
| 90 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK && |
| 91 | !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR && |
| 92 | errno == ENOENT)) { |
| 93 | goto error; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | chunk_status = lttng_trace_chunk_open_fs_handle(chunk, index_file_path, |
| 98 | flags, mode, &fs_handle, expect_no_file); |
| 99 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | if (flags == WRITE_FILE_FLAGS) { |
| 104 | ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor); |
| 105 | size_ret = fs_handle_write(fs_handle, &hdr, sizeof(hdr)); |
| 106 | if (size_ret < sizeof(hdr)) { |
| 107 | PERROR("Failed to write index header"); |
| 108 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 109 | goto error; |
| 110 | } |
| 111 | index_file->element_len = ctf_packet_index_len(index_major, index_minor); |
| 112 | } else { |
| 113 | uint32_t element_len; |
| 114 | |
| 115 | size_ret = fs_handle_read(fs_handle, &hdr, sizeof(hdr)); |
| 116 | if (size_ret < 0) { |
| 117 | PERROR("Failed to read index header"); |
| 118 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 119 | goto error; |
| 120 | } |
| 121 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { |
| 122 | ERR("Invalid header magic"); |
| 123 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 124 | goto error; |
| 125 | } |
| 126 | if (index_major != be32toh(hdr.index_major)) { |
| 127 | ERR("Index major number mismatch: %u, expect %u", |
| 128 | be32toh(hdr.index_major), index_major); |
| 129 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 130 | goto error; |
| 131 | } |
| 132 | if (index_minor != be32toh(hdr.index_minor)) { |
| 133 | ERR("Index minor number mismatch: %u, expect %u", |
| 134 | be32toh(hdr.index_minor), index_minor); |
| 135 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 136 | goto error; |
| 137 | } |
| 138 | element_len = be32toh(hdr.packet_index_len); |
| 139 | if (element_len > sizeof(struct ctf_packet_index)) { |
| 140 | ERR("Index element length too long"); |
| 141 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 142 | goto error; |
| 143 | } |
| 144 | index_file->element_len = element_len; |
| 145 | } |
| 146 | index_file->file = fs_handle; |
| 147 | index_file->major = index_major; |
| 148 | index_file->minor = index_minor; |
| 149 | urcu_ref_init(&index_file->ref); |
| 150 | |
| 151 | *file = index_file; |
| 152 | return LTTNG_TRACE_CHUNK_STATUS_OK; |
| 153 | |
| 154 | error: |
| 155 | if (fs_handle) { |
| 156 | ret = fs_handle_close(fs_handle); |
| 157 | if (ret < 0) { |
| 158 | PERROR("Failed to close file descriptor of index file"); |
| 159 | } |
| 160 | } |
| 161 | lttng_trace_chunk_put(chunk); |
| 162 | free(index_file); |
| 163 | return chunk_status; |
| 164 | } |
| 165 | |
| 166 | enum lttng_trace_chunk_status lttng_index_file_create_from_trace_chunk( |
| 167 | struct lttng_trace_chunk *chunk, |
| 168 | const char *channel_path, const char *stream_name, |
| 169 | uint64_t stream_file_size, uint64_t stream_file_index, |
| 170 | uint32_t index_major, uint32_t index_minor, |
| 171 | bool unlink_existing_file, struct lttng_index_file **file) |
| 172 | { |
| 173 | return _lttng_index_file_create_from_trace_chunk(chunk, channel_path, |
| 174 | stream_name, stream_file_size, stream_file_index, |
| 175 | index_major, index_minor, unlink_existing_file, |
| 176 | WRITE_FILE_FLAGS, false, file); |
| 177 | } |
| 178 | |
| 179 | enum lttng_trace_chunk_status lttng_index_file_create_from_trace_chunk_read_only( |
| 180 | struct lttng_trace_chunk *chunk, |
| 181 | const char *channel_path, const char *stream_name, |
| 182 | uint64_t stream_file_size, uint64_t stream_file_index, |
| 183 | uint32_t index_major, uint32_t index_minor, |
| 184 | bool expect_no_file, struct lttng_index_file **file) |
| 185 | { |
| 186 | return _lttng_index_file_create_from_trace_chunk(chunk, channel_path, |
| 187 | stream_name, stream_file_size, stream_file_index, |
| 188 | index_major, index_minor, false, |
| 189 | READ_ONLY_FILE_FLAGS, expect_no_file, file); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Write index values to the given index file. |
| 194 | * |
| 195 | * Return 0 on success, -1 on error. |
| 196 | */ |
| 197 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
| 198 | const struct ctf_packet_index *element) |
| 199 | { |
| 200 | ssize_t ret; |
| 201 | const size_t len = index_file->element_len;; |
| 202 | |
| 203 | assert(index_file); |
| 204 | assert(element); |
| 205 | |
| 206 | if (!index_file->file) { |
| 207 | goto error; |
| 208 | } |
| 209 | |
| 210 | ret = fs_handle_write(index_file->file, element, len); |
| 211 | if (ret < len) { |
| 212 | PERROR("writing index file"); |
| 213 | goto error; |
| 214 | } |
| 215 | return 0; |
| 216 | |
| 217 | error: |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Read index values from the given index file. |
| 223 | * |
| 224 | * Return 0 on success, -1 on error. |
| 225 | */ |
| 226 | int lttng_index_file_read(const struct lttng_index_file *index_file, |
| 227 | struct ctf_packet_index *element) |
| 228 | { |
| 229 | ssize_t ret; |
| 230 | const size_t len = index_file->element_len; |
| 231 | |
| 232 | assert(element); |
| 233 | |
| 234 | if (!index_file->file) { |
| 235 | goto error; |
| 236 | } |
| 237 | |
| 238 | ret = fs_handle_read(index_file->file, element, len); |
| 239 | if (ret < 0) { |
| 240 | PERROR("read index file"); |
| 241 | goto error; |
| 242 | } |
| 243 | if (ret < len) { |
| 244 | ERR("lttng_read expected %zu, returned %zd", len, ret); |
| 245 | goto error; |
| 246 | } |
| 247 | return 0; |
| 248 | |
| 249 | error: |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | void lttng_index_file_get(struct lttng_index_file *index_file) |
| 254 | { |
| 255 | urcu_ref_get(&index_file->ref); |
| 256 | } |
| 257 | |
| 258 | static void lttng_index_file_release(struct urcu_ref *ref) |
| 259 | { |
| 260 | struct lttng_index_file *index_file = caa_container_of(ref, |
| 261 | struct lttng_index_file, ref); |
| 262 | |
| 263 | if (fs_handle_close(index_file->file)) { |
| 264 | PERROR("close index fd"); |
| 265 | } |
| 266 | lttng_trace_chunk_put(index_file->trace_chunk); |
| 267 | free(index_file); |
| 268 | } |
| 269 | |
| 270 | void lttng_index_file_put(struct lttng_index_file *index_file) |
| 271 | { |
| 272 | urcu_ref_put(&index_file->ref, lttng_index_file_release); |
| 273 | } |