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