Fix: don't perform an automatic session rotation in snapshot mode
[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
d2956687 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
d2956687
JG
34struct lttng_index_file *lttng_index_file_create_from_trace_chunk(
35 struct lttng_trace_chunk *chunk,
36 const char *channel_path, char *stream_name,
c35f9726 37 uint64_t stream_file_size, uint64_t stream_file_index,
d2956687
JG
38 uint32_t index_major, uint32_t index_minor,
39 bool unlink_existing_file)
40{
41 struct lttng_index_file *index_file;
42 enum lttng_trace_chunk_status chunk_status;
43 int ret, fd = -1;
44 ssize_t size_ret;
45 struct ctf_packet_index_file_hdr hdr;
46 char index_directory_path[LTTNG_PATH_MAX];
47 char index_file_path[LTTNG_PATH_MAX];
48 const uint32_t element_len = ctf_packet_index_len(index_major,
49 index_minor);
50 const int flags = O_WRONLY | O_CREAT | O_TRUNC;
51 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
c35f9726
JG
52 bool acquired_reference = lttng_trace_chunk_get(chunk);
53
54 assert(acquired_reference);
d2956687
JG
55
56 index_file = zmalloc(sizeof(*index_file));
57 if (!index_file) {
58 PERROR("Failed to allocate lttng_index_file");
59 goto error;
60 }
61
c35f9726 62 index_file->trace_chunk = chunk;
d2956687
JG
63 ret = snprintf(index_directory_path, sizeof(index_directory_path),
64 "%s/" DEFAULT_INDEX_DIR, channel_path);
65 if (ret < 0 || ret >= sizeof(index_directory_path)) {
66 ERR("Failed to format index directory path");
67 goto error;
68 }
69
70 ret = utils_stream_file_path(index_directory_path, stream_name,
c35f9726 71 stream_file_size, stream_file_index,
d2956687
JG
72 DEFAULT_INDEX_FILE_SUFFIX,
73 index_file_path, sizeof(index_file_path));
74 if (ret) {
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 */
348a81dc
JG
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)) {
d2956687
JG
92 goto error;
93 }
348a81dc 94 }
d2956687
JG
95
96 chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path,
97 flags, mode, &fd);
98 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
99 goto error;
100 }
101
102 ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor);
103 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
104 if (size_ret < sizeof(hdr)) {
105 PERROR("Failed to write index header");
106 goto error;
107 }
108 index_file->fd = fd;
109 index_file->major = index_major;
110 index_file->minor = index_minor;
111 index_file->element_len = element_len;
112 urcu_ref_init(&index_file->ref);
113
114 return index_file;
115
116error:
117 if (fd >= 0) {
118 ret = close(fd);
119 if (ret < 0) {
120 PERROR("Failed to close file descriptor of index file");
121 }
122 }
123 free(index_file);
124 return NULL;
125}
126
309167d2 127/*
f8f3885c 128 * Write index values to the given index file.
309167d2 129 *
f8f3885c 130 * Return 0 on success, -1 on error.
309167d2 131 */
f8f3885c
MD
132int lttng_index_file_write(const struct lttng_index_file *index_file,
133 const struct ctf_packet_index *element)
309167d2 134{
0b0ac4a9
JR
135 int fd;
136 size_t len;
6cd525e8 137 ssize_t ret;
309167d2 138
0b0ac4a9 139 assert(index_file);
f8f3885c 140 assert(element);
309167d2 141
0b0ac4a9
JR
142 fd = index_file->fd;
143 len = index_file->element_len;
144
183f6fa2 145 if (fd < 0) {
183f6fa2
DG
146 goto error;
147 }
148
f8f3885c 149 ret = lttng_write(fd, element, len);
6cd525e8 150 if (ret < len) {
309167d2 151 PERROR("writing index file");
f8f3885c
MD
152 goto error;
153 }
154 return 0;
155
156error:
157 return -1;
158}
159
160/*
161 * Read index values from the given index file.
162 *
163 * Return 0 on success, -1 on error.
164 */
165int lttng_index_file_read(const struct lttng_index_file *index_file,
166 struct ctf_packet_index *element)
167{
168 ssize_t ret;
169 int fd = index_file->fd;
170 size_t len = index_file->element_len;
171
172 assert(element);
173
174 if (fd < 0) {
175 goto error;
176 }
177
178 ret = lttng_read(fd, element, len);
2239b15a 179 if (ret < 0) {
f8f3885c
MD
180 PERROR("read index file");
181 goto error;
309167d2 182 }
2239b15a
MD
183 if (ret < len) {
184 ERR("lttng_read expected %zu, returned %zd", len, ret);
185 goto error;
186 }
f8f3885c 187 return 0;
309167d2 188
183f6fa2 189error:
f8f3885c 190 return -1;
309167d2 191}
2f8f53af
DG
192
193/*
194 * Open index file using a given path, channel name and tracefile count.
195 *
f8f3885c 196 * Return allocated struct lttng_index_file, NULL on error.
2f8f53af 197 */
f8f3885c
MD
198struct lttng_index_file *lttng_index_file_open(const char *path_name,
199 const char *channel_name, uint64_t tracefile_count,
200 uint64_t tracefile_count_current)
2f8f53af 201{
f8f3885c 202 struct lttng_index_file *index_file;
2f8f53af
DG
203 int ret, read_fd;
204 ssize_t read_len;
205 char fullpath[PATH_MAX];
206 struct ctf_packet_index_file_hdr hdr;
f8f3885c 207 uint32_t major, minor, element_len;
2f8f53af
DG
208
209 assert(path_name);
210 assert(channel_name);
211
f8f3885c
MD
212 index_file = zmalloc(sizeof(*index_file));
213 if (!index_file) {
214 PERROR("allocating lttng_index_file");
215 goto error;
216 }
217
2f8f53af
DG
218 if (tracefile_count > 0) {
219 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%"
220 PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name,
221 channel_name, tracefile_count_current);
222 } else {
223 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
224 DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name);
225 }
226 if (ret < 0) {
227 PERROR("snprintf index path");
228 goto error;
229 }
230
231 DBG("Index opening file %s in read only", fullpath);
232 read_fd = open(fullpath, O_RDONLY);
233 if (read_fd < 0) {
4a3a61c1 234 PERROR("opening index in read-only");
2f8f53af
DG
235 goto error;
236 }
237
238 read_len = lttng_read(read_fd, &hdr, sizeof(hdr));
239 if (read_len < 0) {
240 PERROR("Reading index header");
241 goto error_close;
242 }
243
244 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
245 ERR("Invalid header magic");
246 goto error_close;
247 }
f8f3885c
MD
248 major = be32toh(hdr.index_major);
249 minor = be32toh(hdr.index_minor);
250 element_len = be32toh(hdr.packet_index_len);
251
252 if (major != CTF_INDEX_MAJOR) {
2f8f53af
DG
253 ERR("Invalid header version");
254 goto error_close;
255 }
a3461e94
MD
256 if (element_len > sizeof(struct ctf_packet_index)) {
257 ERR("Index element length too long");
258 goto error_close;
259 }
2f8f53af 260
f8f3885c
MD
261 index_file->fd = read_fd;
262 index_file->major = major;
263 index_file->minor = minor;
264 index_file->element_len = element_len;
265 urcu_ref_init(&index_file->ref);
266
267 return index_file;
2f8f53af
DG
268
269error_close:
270 if (read_fd >= 0) {
271 int close_ret;
272
273 close_ret = close(read_fd);
274 if (close_ret < 0) {
275 PERROR("close read fd %d", read_fd);
276 }
277 }
2f8f53af
DG
278
279error:
f8f3885c
MD
280 free(index_file);
281 return NULL;
282}
283
284void lttng_index_file_get(struct lttng_index_file *index_file)
285{
286 urcu_ref_get(&index_file->ref);
287}
288
289static void lttng_index_file_release(struct urcu_ref *ref)
290{
291 struct lttng_index_file *index_file = caa_container_of(ref,
292 struct lttng_index_file, ref);
293
294 if (close(index_file->fd)) {
295 PERROR("close index fd");
296 }
c35f9726 297 lttng_trace_chunk_put(index_file->trace_chunk);
f8f3885c
MD
298 free(index_file);
299}
300
301void lttng_index_file_put(struct lttng_index_file *index_file)
302{
303 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 304}
This page took 0.081813 seconds and 4 git commands to generate.