Fix: tests: `-Wstringop-overflow` warning
[lttng-tools.git] / src / common / index / index.c
CommitLineData
309167d2
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
f8f3885c 4 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
309167d2
JD
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
6c1c0768 20#define _LGPL_SOURCE
309167d2 21#include <assert.h>
1c20f0e2 22#include <sys/stat.h>
2f8f53af
DG
23#include <sys/types.h>
24#include <fcntl.h>
309167d2 25
e5148e25 26#include <lttng/constant.h>
309167d2
JD
27#include <common/common.h>
28#include <common/defaults.h>
f263b7fd 29#include <common/compat/endian.h>
309167d2
JD
30#include <common/utils.h>
31
32#include "index.h"
33
247e3c78 34#define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
1bfb0c63
JG
35#define READ_ONLY_FILE_FLAGS O_RDONLY
36
37static struct lttng_index_file *_lttng_index_file_create_from_trace_chunk(
e5148e25 38 struct lttng_trace_chunk *chunk,
1bfb0c63 39 const char *channel_path, const char *stream_name,
84a93c08 40 uint64_t stream_file_size, uint64_t stream_file_index,
e5148e25 41 uint32_t index_major, uint32_t index_minor,
1bfb0c63
JG
42 bool unlink_existing_file,
43 int flags)
e5148e25
JG
44{
45 struct lttng_index_file *index_file;
46 enum lttng_trace_chunk_status chunk_status;
47 int ret, fd = -1;
48 ssize_t size_ret;
49 struct ctf_packet_index_file_hdr hdr;
50 char index_directory_path[LTTNG_PATH_MAX];
51 char index_file_path[LTTNG_PATH_MAX];
e5148e25 52 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1bfb0c63 53 const bool acquired_reference = lttng_trace_chunk_get(chunk);
84a93c08
JG
54
55 assert(acquired_reference);
e5148e25
JG
56
57 index_file = zmalloc(sizeof(*index_file));
58 if (!index_file) {
59 PERROR("Failed to allocate lttng_index_file");
60 goto error;
61 }
62
84a93c08 63 index_file->trace_chunk = chunk;
e5148e25
JG
64 ret = snprintf(index_directory_path, sizeof(index_directory_path),
65 "%s/" DEFAULT_INDEX_DIR, channel_path);
66 if (ret < 0 || ret >= sizeof(index_directory_path)) {
67 ERR("Failed to format index directory path");
68 goto error;
69 }
70
71 ret = utils_stream_file_path(index_directory_path, stream_name,
84a93c08 72 stream_file_size, stream_file_index,
e5148e25
JG
73 DEFAULT_INDEX_FILE_SUFFIX,
74 index_file_path, sizeof(index_file_path));
75 if (ret) {
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 */
61ace1d3
JG
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)) {
e5148e25
JG
93 goto error;
94 }
61ace1d3 95 }
e5148e25
JG
96
97 chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path,
98 flags, mode, &fd);
99 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
100 goto error;
101 }
102
0ee403c1
MD
103 if (flags == WRITE_FILE_FLAGS) {
104 ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor);
105 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
106 if (size_ret < sizeof(hdr)) {
107 PERROR("Failed to write index header");
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 = lttng_read(fd, &hdr, sizeof(hdr));
115 if (size_ret < 0) {
116 PERROR("Failed to read index header");
117 goto error;
118 }
119 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
120 ERR("Invalid header magic");
121 goto error;
122 }
123 if (index_major != be32toh(hdr.index_major)) {
124 ERR("Index major number mismatch: %u, expect %u",
125 be32toh(hdr.index_major), index_major);
126 goto error;
127 }
128 if (index_minor != be32toh(hdr.index_minor)) {
129 ERR("Index minor number mismatch: %u, expect %u",
130 be32toh(hdr.index_minor), index_minor);
131 goto error;
132 }
133 element_len = be32toh(hdr.packet_index_len);
134 if (element_len > sizeof(struct ctf_packet_index)) {
135 ERR("Index element length too long");
136 goto error;
137 }
138 index_file->element_len = element_len;
e5148e25
JG
139 }
140 index_file->fd = fd;
141 index_file->major = index_major;
142 index_file->minor = index_minor;
e5148e25
JG
143 urcu_ref_init(&index_file->ref);
144
145 return index_file;
146
147error:
148 if (fd >= 0) {
149 ret = close(fd);
150 if (ret < 0) {
151 PERROR("Failed to close file descriptor of index file");
152 }
153 }
92e41592 154 lttng_trace_chunk_put(chunk);
e5148e25
JG
155 free(index_file);
156 return NULL;
157}
158
1bfb0c63
JG
159struct lttng_index_file *lttng_index_file_create_from_trace_chunk(
160 struct lttng_trace_chunk *chunk,
161 const char *channel_path, const char *stream_name,
162 uint64_t stream_file_size, uint64_t stream_file_index,
163 uint32_t index_major, uint32_t index_minor,
164 bool unlink_existing_file)
165{
166 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
167 stream_name, stream_file_size, stream_file_index,
168 index_major, index_minor, unlink_existing_file,
169 WRITE_FILE_FLAGS);
170}
171
172struct lttng_index_file *lttng_index_file_create_from_trace_chunk_read_only(
173 struct lttng_trace_chunk *chunk,
174 const char *channel_path, const char *stream_name,
175 uint64_t stream_file_size, uint64_t stream_file_index,
176 uint32_t index_major, uint32_t index_minor)
177{
178 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
179 stream_name, stream_file_size, stream_file_index,
180 index_major, index_minor, false,
181 READ_ONLY_FILE_FLAGS);
182}
183
309167d2 184/*
f8f3885c 185 * Write index values to the given index file.
309167d2 186 *
f8f3885c 187 * Return 0 on success, -1 on error.
309167d2 188 */
f8f3885c
MD
189int lttng_index_file_write(const struct lttng_index_file *index_file,
190 const struct ctf_packet_index *element)
309167d2 191{
0b0ac4a9
JR
192 int fd;
193 size_t len;
6cd525e8 194 ssize_t ret;
309167d2 195
0b0ac4a9 196 assert(index_file);
f8f3885c 197 assert(element);
309167d2 198
0b0ac4a9
JR
199 fd = index_file->fd;
200 len = index_file->element_len;
201
183f6fa2 202 if (fd < 0) {
183f6fa2
DG
203 goto error;
204 }
205
f8f3885c 206 ret = lttng_write(fd, element, len);
6cd525e8 207 if (ret < len) {
309167d2 208 PERROR("writing index file");
f8f3885c
MD
209 goto error;
210 }
211 return 0;
212
213error:
214 return -1;
215}
216
217/*
218 * Read index values from the given index file.
219 *
220 * Return 0 on success, -1 on error.
221 */
222int lttng_index_file_read(const struct lttng_index_file *index_file,
223 struct ctf_packet_index *element)
224{
225 ssize_t ret;
226 int fd = index_file->fd;
227 size_t len = index_file->element_len;
228
229 assert(element);
230
231 if (fd < 0) {
232 goto error;
233 }
234
235 ret = lttng_read(fd, element, len);
5b7446a7 236 if (ret < 0) {
f8f3885c
MD
237 PERROR("read index file");
238 goto error;
309167d2 239 }
5b7446a7
MD
240 if (ret < len) {
241 ERR("lttng_read expected %zu, returned %zd", len, ret);
242 goto error;
243 }
f8f3885c 244 return 0;
309167d2 245
183f6fa2 246error:
f8f3885c 247 return -1;
309167d2 248}
2f8f53af 249
f8f3885c
MD
250void lttng_index_file_get(struct lttng_index_file *index_file)
251{
252 urcu_ref_get(&index_file->ref);
253}
254
255static void lttng_index_file_release(struct urcu_ref *ref)
256{
257 struct lttng_index_file *index_file = caa_container_of(ref,
258 struct lttng_index_file, ref);
259
260 if (close(index_file->fd)) {
261 PERROR("close index fd");
262 }
84a93c08 263 lttng_trace_chunk_put(index_file->trace_chunk);
f8f3885c
MD
264 free(index_file);
265}
266
267void lttng_index_file_put(struct lttng_index_file *index_file)
268{
269 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 270}
This page took 0.051764 seconds and 4 git commands to generate.