| 1 | /* |
| 2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * David Goulet <dgoulet@efficios.com> |
| 4 | * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License, version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _LGPL_SOURCE |
| 21 | #include <assert.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include <lttng/constant.h> |
| 27 | #include <common/common.h> |
| 28 | #include <common/defaults.h> |
| 29 | #include <common/compat/endian.h> |
| 30 | #include <common/utils.h> |
| 31 | |
| 32 | #include "index.h" |
| 33 | |
| 34 | /* |
| 35 | * Create the index file associated with a trace file. |
| 36 | * |
| 37 | * Return allocated struct lttng_index_file, NULL on error. |
| 38 | */ |
| 39 | struct lttng_index_file *lttng_index_file_create(const char *path_name, |
| 40 | char *stream_name, int uid, int gid, |
| 41 | uint64_t size, uint64_t count, uint32_t major, uint32_t minor) |
| 42 | { |
| 43 | struct lttng_index_file *index_file; |
| 44 | int ret, fd = -1; |
| 45 | ssize_t size_ret; |
| 46 | struct ctf_packet_index_file_hdr hdr; |
| 47 | char fullpath[PATH_MAX]; |
| 48 | uint32_t element_len = ctf_packet_index_len(major, minor); |
| 49 | |
| 50 | index_file = zmalloc(sizeof(*index_file)); |
| 51 | if (!index_file) { |
| 52 | PERROR("allocating lttng_index_file"); |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR, |
| 57 | path_name); |
| 58 | if (ret < 0) { |
| 59 | PERROR("snprintf index path"); |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | /* Create index directory if necessary. */ |
| 64 | ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid); |
| 65 | if (ret < 0) { |
| 66 | if (errno != EEXIST) { |
| 67 | PERROR("Index trace directory creation error"); |
| 68 | goto error; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * For tracefile rotation. We need to unlink the old |
| 74 | * file if present to synchronize with the tail of the |
| 75 | * live viewer which could be working on this same file. |
| 76 | * By doing so, any reference to the old index file |
| 77 | * stays valid even if we re-create a new file with the |
| 78 | * same name afterwards. |
| 79 | */ |
| 80 | ret = utils_unlink_stream_file(fullpath, stream_name, size, count, uid, |
| 81 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
| 82 | if (ret < 0 && errno != ENOENT) { |
| 83 | goto error; |
| 84 | } |
| 85 | ret = utils_create_stream_file(fullpath, stream_name, size, count, uid, |
| 86 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
| 87 | if (ret < 0) { |
| 88 | goto error; |
| 89 | } |
| 90 | fd = ret; |
| 91 | |
| 92 | ctf_packet_index_file_hdr_init(&hdr, major, minor); |
| 93 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); |
| 94 | if (size_ret < sizeof(hdr)) { |
| 95 | PERROR("write index header"); |
| 96 | goto error; |
| 97 | } |
| 98 | index_file->fd = fd; |
| 99 | index_file->major = major; |
| 100 | index_file->minor = minor; |
| 101 | index_file->element_len = element_len; |
| 102 | urcu_ref_init(&index_file->ref); |
| 103 | |
| 104 | return index_file; |
| 105 | |
| 106 | error: |
| 107 | if (fd >= 0) { |
| 108 | int close_ret; |
| 109 | |
| 110 | close_ret = close(fd); |
| 111 | if (close_ret < 0) { |
| 112 | PERROR("close index fd"); |
| 113 | } |
| 114 | } |
| 115 | free(index_file); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | struct lttng_index_file *lttng_index_file_create_from_trace_chunk( |
| 120 | struct lttng_trace_chunk *chunk, |
| 121 | const char *channel_path, char *stream_name, |
| 122 | uint64_t stream_file_size, uint64_t stream_count, |
| 123 | uint32_t index_major, uint32_t index_minor, |
| 124 | bool unlink_existing_file) |
| 125 | { |
| 126 | struct lttng_index_file *index_file; |
| 127 | enum lttng_trace_chunk_status chunk_status; |
| 128 | int ret, fd = -1; |
| 129 | ssize_t size_ret; |
| 130 | struct ctf_packet_index_file_hdr hdr; |
| 131 | char index_directory_path[LTTNG_PATH_MAX]; |
| 132 | char index_file_path[LTTNG_PATH_MAX]; |
| 133 | const uint32_t element_len = ctf_packet_index_len(index_major, |
| 134 | index_minor); |
| 135 | const int flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 136 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 137 | |
| 138 | index_file = zmalloc(sizeof(*index_file)); |
| 139 | if (!index_file) { |
| 140 | PERROR("Failed to allocate lttng_index_file"); |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | ret = snprintf(index_directory_path, sizeof(index_directory_path), |
| 145 | "%s/" DEFAULT_INDEX_DIR, channel_path); |
| 146 | if (ret < 0 || ret >= sizeof(index_directory_path)) { |
| 147 | ERR("Failed to format index directory path"); |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | ret = utils_stream_file_path(index_directory_path, stream_name, |
| 152 | stream_file_size, stream_count, |
| 153 | DEFAULT_INDEX_FILE_SUFFIX, |
| 154 | index_file_path, sizeof(index_file_path)); |
| 155 | if (ret) { |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | if (unlink_existing_file) { |
| 160 | /* |
| 161 | * For tracefile rotation. We need to unlink the old |
| 162 | * file if present to synchronize with the tail of the |
| 163 | * live viewer which could be working on this same file. |
| 164 | * By doing so, any reference to the old index file |
| 165 | * stays valid even if we re-create a new file with the |
| 166 | * same name afterwards. |
| 167 | */ |
| 168 | chunk_status = lttng_trace_chunk_unlink_file(chunk, |
| 169 | index_file_path); |
| 170 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 171 | goto error; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path, |
| 176 | flags, mode, &fd); |
| 177 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor); |
| 182 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); |
| 183 | if (size_ret < sizeof(hdr)) { |
| 184 | PERROR("Failed to write index header"); |
| 185 | goto error; |
| 186 | } |
| 187 | index_file->fd = fd; |
| 188 | index_file->major = index_major; |
| 189 | index_file->minor = index_minor; |
| 190 | index_file->element_len = element_len; |
| 191 | urcu_ref_init(&index_file->ref); |
| 192 | |
| 193 | return index_file; |
| 194 | |
| 195 | error: |
| 196 | if (fd >= 0) { |
| 197 | ret = close(fd); |
| 198 | if (ret < 0) { |
| 199 | PERROR("Failed to close file descriptor of index file"); |
| 200 | } |
| 201 | } |
| 202 | free(index_file); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Write index values to the given index file. |
| 208 | * |
| 209 | * Return 0 on success, -1 on error. |
| 210 | */ |
| 211 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
| 212 | const struct ctf_packet_index *element) |
| 213 | { |
| 214 | int fd; |
| 215 | size_t len; |
| 216 | ssize_t ret; |
| 217 | |
| 218 | assert(index_file); |
| 219 | assert(element); |
| 220 | |
| 221 | fd = index_file->fd; |
| 222 | len = index_file->element_len; |
| 223 | |
| 224 | if (fd < 0) { |
| 225 | goto error; |
| 226 | } |
| 227 | |
| 228 | ret = lttng_write(fd, element, len); |
| 229 | if (ret < len) { |
| 230 | PERROR("writing index file"); |
| 231 | goto error; |
| 232 | } |
| 233 | return 0; |
| 234 | |
| 235 | error: |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Read index values from the given index file. |
| 241 | * |
| 242 | * Return 0 on success, -1 on error. |
| 243 | */ |
| 244 | int lttng_index_file_read(const struct lttng_index_file *index_file, |
| 245 | struct ctf_packet_index *element) |
| 246 | { |
| 247 | ssize_t ret; |
| 248 | int fd = index_file->fd; |
| 249 | size_t len = index_file->element_len; |
| 250 | |
| 251 | assert(element); |
| 252 | |
| 253 | if (fd < 0) { |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | ret = lttng_read(fd, element, len); |
| 258 | if (ret < 0) { |
| 259 | PERROR("read index file"); |
| 260 | goto error; |
| 261 | } |
| 262 | if (ret < len) { |
| 263 | ERR("lttng_read expected %zu, returned %zd", len, ret); |
| 264 | goto error; |
| 265 | } |
| 266 | return 0; |
| 267 | |
| 268 | error: |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Open index file using a given path, channel name and tracefile count. |
| 274 | * |
| 275 | * Return allocated struct lttng_index_file, NULL on error. |
| 276 | */ |
| 277 | struct lttng_index_file *lttng_index_file_open(const char *path_name, |
| 278 | const char *channel_name, uint64_t tracefile_count, |
| 279 | uint64_t tracefile_count_current) |
| 280 | { |
| 281 | struct lttng_index_file *index_file; |
| 282 | int ret, read_fd; |
| 283 | ssize_t read_len; |
| 284 | char fullpath[PATH_MAX]; |
| 285 | struct ctf_packet_index_file_hdr hdr; |
| 286 | uint32_t major, minor, element_len; |
| 287 | |
| 288 | assert(path_name); |
| 289 | assert(channel_name); |
| 290 | |
| 291 | index_file = zmalloc(sizeof(*index_file)); |
| 292 | if (!index_file) { |
| 293 | PERROR("allocating lttng_index_file"); |
| 294 | goto error; |
| 295 | } |
| 296 | |
| 297 | if (tracefile_count > 0) { |
| 298 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%" |
| 299 | PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name, |
| 300 | channel_name, tracefile_count_current); |
| 301 | } else { |
| 302 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s" |
| 303 | DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name); |
| 304 | } |
| 305 | if (ret < 0) { |
| 306 | PERROR("snprintf index path"); |
| 307 | goto error; |
| 308 | } |
| 309 | |
| 310 | DBG("Index opening file %s in read only", fullpath); |
| 311 | read_fd = open(fullpath, O_RDONLY); |
| 312 | if (read_fd < 0) { |
| 313 | PERROR("opening index in read-only"); |
| 314 | goto error; |
| 315 | } |
| 316 | |
| 317 | read_len = lttng_read(read_fd, &hdr, sizeof(hdr)); |
| 318 | if (read_len < 0) { |
| 319 | PERROR("Reading index header"); |
| 320 | goto error_close; |
| 321 | } |
| 322 | |
| 323 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { |
| 324 | ERR("Invalid header magic"); |
| 325 | goto error_close; |
| 326 | } |
| 327 | major = be32toh(hdr.index_major); |
| 328 | minor = be32toh(hdr.index_minor); |
| 329 | element_len = be32toh(hdr.packet_index_len); |
| 330 | |
| 331 | if (major != CTF_INDEX_MAJOR) { |
| 332 | ERR("Invalid header version"); |
| 333 | goto error_close; |
| 334 | } |
| 335 | if (element_len > sizeof(struct ctf_packet_index)) { |
| 336 | ERR("Index element length too long"); |
| 337 | goto error_close; |
| 338 | } |
| 339 | |
| 340 | index_file->fd = read_fd; |
| 341 | index_file->major = major; |
| 342 | index_file->minor = minor; |
| 343 | index_file->element_len = element_len; |
| 344 | urcu_ref_init(&index_file->ref); |
| 345 | |
| 346 | return index_file; |
| 347 | |
| 348 | error_close: |
| 349 | if (read_fd >= 0) { |
| 350 | int close_ret; |
| 351 | |
| 352 | close_ret = close(read_fd); |
| 353 | if (close_ret < 0) { |
| 354 | PERROR("close read fd %d", read_fd); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | error: |
| 359 | free(index_file); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | void lttng_index_file_get(struct lttng_index_file *index_file) |
| 364 | { |
| 365 | urcu_ref_get(&index_file->ref); |
| 366 | } |
| 367 | |
| 368 | static void lttng_index_file_release(struct urcu_ref *ref) |
| 369 | { |
| 370 | struct lttng_index_file *index_file = caa_container_of(ref, |
| 371 | struct lttng_index_file, ref); |
| 372 | |
| 373 | if (close(index_file->fd)) { |
| 374 | PERROR("close index fd"); |
| 375 | } |
| 376 | free(index_file); |
| 377 | } |
| 378 | |
| 379 | void lttng_index_file_put(struct lttng_index_file *index_file) |
| 380 | { |
| 381 | urcu_ref_put(&index_file->ref, lttng_index_file_release); |
| 382 | } |