2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
23 #include <sys/types.h>
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>
34 #define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
35 #define READ_ONLY_FILE_FLAGS O_RDONLY
37 static struct lttng_index_file
*_lttng_index_file_create_from_trace_chunk(
38 struct lttng_trace_chunk
*chunk
,
39 const char *channel_path
, const char *stream_name
,
40 uint64_t stream_file_size
, uint64_t stream_file_index
,
41 uint32_t index_major
, uint32_t index_minor
,
42 bool unlink_existing_file
,
45 struct lttng_index_file
*index_file
;
46 enum lttng_trace_chunk_status chunk_status
;
49 struct ctf_packet_index_file_hdr hdr
;
50 char index_directory_path
[LTTNG_PATH_MAX
];
51 char index_file_path
[LTTNG_PATH_MAX
];
52 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
53 const bool acquired_reference
= lttng_trace_chunk_get(chunk
);
55 assert(acquired_reference
);
57 index_file
= zmalloc(sizeof(*index_file
));
59 PERROR("Failed to allocate lttng_index_file");
63 index_file
->trace_chunk
= chunk
;
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");
71 ret
= utils_stream_file_path(index_directory_path
, stream_name
,
72 stream_file_size
, stream_file_index
,
73 DEFAULT_INDEX_FILE_SUFFIX
,
74 index_file_path
, sizeof(index_file_path
));
79 if (unlink_existing_file
) {
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.
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
&&
97 chunk_status
= lttng_trace_chunk_open_file(chunk
, index_file_path
,
99 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
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");
110 index_file
->element_len
= ctf_packet_index_len(index_major
, index_minor
);
112 uint32_t element_len
;
114 size_ret
= lttng_read(fd
, &hdr
, sizeof(hdr
));
116 PERROR("Failed to read index header");
119 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
120 ERR("Invalid header magic");
123 if (index_major
!= be32toh(hdr
.index_major
)) {
124 ERR("Index major number mismatch: %u, expect %u",
125 be32toh(hdr
.index_major
), index_major
);
128 if (index_minor
!= be32toh(hdr
.index_minor
)) {
129 ERR("Index minor number mismatch: %u, expect %u",
130 be32toh(hdr
.index_minor
), index_minor
);
133 element_len
= be32toh(hdr
.packet_index_len
);
134 if (element_len
> sizeof(struct ctf_packet_index
)) {
135 ERR("Index element length too long");
138 index_file
->element_len
= element_len
;
141 index_file
->major
= index_major
;
142 index_file
->minor
= index_minor
;
143 urcu_ref_init(&index_file
->ref
);
151 PERROR("Failed to close file descriptor of index file");
158 struct lttng_index_file
*lttng_index_file_create_from_trace_chunk(
159 struct lttng_trace_chunk
*chunk
,
160 const char *channel_path
, const char *stream_name
,
161 uint64_t stream_file_size
, uint64_t stream_file_index
,
162 uint32_t index_major
, uint32_t index_minor
,
163 bool unlink_existing_file
)
165 return _lttng_index_file_create_from_trace_chunk(chunk
, channel_path
,
166 stream_name
, stream_file_size
, stream_file_index
,
167 index_major
, index_minor
, unlink_existing_file
,
171 struct lttng_index_file
*lttng_index_file_create_from_trace_chunk_read_only(
172 struct lttng_trace_chunk
*chunk
,
173 const char *channel_path
, const char *stream_name
,
174 uint64_t stream_file_size
, uint64_t stream_file_index
,
175 uint32_t index_major
, uint32_t index_minor
)
177 return _lttng_index_file_create_from_trace_chunk(chunk
, channel_path
,
178 stream_name
, stream_file_size
, stream_file_index
,
179 index_major
, index_minor
, false,
180 READ_ONLY_FILE_FLAGS
);
184 * Write index values to the given index file.
186 * Return 0 on success, -1 on error.
188 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
189 const struct ctf_packet_index
*element
)
199 len
= index_file
->element_len
;
205 ret
= lttng_write(fd
, element
, len
);
207 PERROR("writing index file");
217 * Read index values from the given index file.
219 * Return 0 on success, -1 on error.
221 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
222 struct ctf_packet_index
*element
)
225 int fd
= index_file
->fd
;
226 size_t len
= index_file
->element_len
;
234 ret
= lttng_read(fd
, element
, len
);
236 PERROR("read index file");
240 ERR("lttng_read expected %zu, returned %zd", len
, ret
);
249 void lttng_index_file_get(struct lttng_index_file
*index_file
)
251 urcu_ref_get(&index_file
->ref
);
254 static void lttng_index_file_release(struct urcu_ref
*ref
)
256 struct lttng_index_file
*index_file
= caa_container_of(ref
,
257 struct lttng_index_file
, ref
);
259 if (close(index_file
->fd
)) {
260 PERROR("close index fd");
262 lttng_trace_chunk_put(index_file
->trace_chunk
);
266 void lttng_index_file_put(struct lttng_index_file
*index_file
)
268 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);