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 struct lttng_index_file
*lttng_index_file_create_from_trace_chunk(
35 struct lttng_trace_chunk
*chunk
,
36 const char *channel_path
, char *stream_name
,
37 uint64_t stream_file_size
, uint64_t stream_file_index
,
38 uint32_t index_major
, uint32_t index_minor
,
39 bool unlink_existing_file
)
41 struct lttng_index_file
*index_file
;
42 enum lttng_trace_chunk_status chunk_status
;
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
,
50 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
51 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
52 bool acquired_reference
= lttng_trace_chunk_get(chunk
);
54 assert(acquired_reference
);
56 index_file
= zmalloc(sizeof(*index_file
));
58 PERROR("Failed to allocate lttng_index_file");
62 index_file
->trace_chunk
= chunk
;
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");
70 ret
= utils_stream_file_path(index_directory_path
, stream_name
,
71 stream_file_size
, stream_file_index
,
72 DEFAULT_INDEX_FILE_SUFFIX
,
73 index_file_path
, sizeof(index_file_path
));
78 if (unlink_existing_file
) {
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.
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
&&
96 chunk_status
= lttng_trace_chunk_open_file(chunk
, index_file_path
,
98 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
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");
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
);
120 PERROR("Failed to close file descriptor of index file");
128 * Write index values to the given index file.
130 * Return 0 on success, -1 on error.
132 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
133 const struct ctf_packet_index
*element
)
143 len
= index_file
->element_len
;
149 ret
= lttng_write(fd
, element
, len
);
151 PERROR("writing index file");
161 * Read index values from the given index file.
163 * Return 0 on success, -1 on error.
165 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
166 struct ctf_packet_index
*element
)
169 int fd
= index_file
->fd
;
170 size_t len
= index_file
->element_len
;
178 ret
= lttng_read(fd
, element
, len
);
180 PERROR("read index file");
184 ERR("lttng_read expected %zu, returned %zd", len
, ret
);
194 * Open index file using a given path, channel name and tracefile count.
196 * Return allocated struct lttng_index_file, NULL on error.
198 struct 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
)
202 struct lttng_index_file
*index_file
;
205 char fullpath
[PATH_MAX
];
206 struct ctf_packet_index_file_hdr hdr
;
207 uint32_t major
, minor
, element_len
;
210 assert(channel_name
);
212 index_file
= zmalloc(sizeof(*index_file
));
214 PERROR("allocating lttng_index_file");
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
);
223 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
224 DEFAULT_INDEX_FILE_SUFFIX
, path_name
, channel_name
);
227 PERROR("snprintf index path");
231 DBG("Index opening file %s in read only", fullpath
);
232 read_fd
= open(fullpath
, O_RDONLY
);
234 PERROR("opening index in read-only");
238 read_len
= lttng_read(read_fd
, &hdr
, sizeof(hdr
));
240 PERROR("Reading index header");
244 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
245 ERR("Invalid header magic");
248 major
= be32toh(hdr
.index_major
);
249 minor
= be32toh(hdr
.index_minor
);
250 element_len
= be32toh(hdr
.packet_index_len
);
252 if (major
!= CTF_INDEX_MAJOR
) {
253 ERR("Invalid header version");
256 if (element_len
> sizeof(struct ctf_packet_index
)) {
257 ERR("Index element length too long");
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
);
273 close_ret
= close(read_fd
);
275 PERROR("close read fd %d", read_fd
);
284 void lttng_index_file_get(struct lttng_index_file
*index_file
)
286 urcu_ref_get(&index_file
->ref
);
289 static void lttng_index_file_release(struct urcu_ref
*ref
)
291 struct lttng_index_file
*index_file
= caa_container_of(ref
,
292 struct lttng_index_file
, ref
);
294 if (close(index_file
->fd
)) {
295 PERROR("close index fd");
297 lttng_trace_chunk_put(index_file
->trace_chunk
);
301 void lttng_index_file_put(struct lttng_index_file
*index_file
)
303 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);