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_count
,
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
;
53 index_file
= zmalloc(sizeof(*index_file
));
55 PERROR("Failed to allocate lttng_index_file");
59 ret
= snprintf(index_directory_path
, sizeof(index_directory_path
),
60 "%s/" DEFAULT_INDEX_DIR
, channel_path
);
61 if (ret
< 0 || ret
>= sizeof(index_directory_path
)) {
62 ERR("Failed to format index directory path");
66 ret
= utils_stream_file_path(index_directory_path
, stream_name
,
67 stream_file_size
, stream_count
,
68 DEFAULT_INDEX_FILE_SUFFIX
,
69 index_file_path
, sizeof(index_file_path
));
74 if (unlink_existing_file
) {
76 * For tracefile rotation. We need to unlink the old
77 * file if present to synchronize with the tail of the
78 * live viewer which could be working on this same file.
79 * By doing so, any reference to the old index file
80 * stays valid even if we re-create a new file with the
81 * same name afterwards.
83 chunk_status
= lttng_trace_chunk_unlink_file(
84 chunk
, index_file_path
);
85 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
86 !(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_ERROR
&&
92 chunk_status
= lttng_trace_chunk_open_file(chunk
, index_file_path
,
94 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
98 ctf_packet_index_file_hdr_init(&hdr
, index_major
, index_minor
);
99 size_ret
= lttng_write(fd
, &hdr
, sizeof(hdr
));
100 if (size_ret
< sizeof(hdr
)) {
101 PERROR("Failed to write index header");
105 index_file
->major
= index_major
;
106 index_file
->minor
= index_minor
;
107 index_file
->element_len
= element_len
;
108 urcu_ref_init(&index_file
->ref
);
116 PERROR("Failed to close file descriptor of index file");
124 * Write index values to the given index file.
126 * Return 0 on success, -1 on error.
128 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
129 const struct ctf_packet_index
*element
)
139 len
= index_file
->element_len
;
145 ret
= lttng_write(fd
, element
, len
);
147 PERROR("writing index file");
157 * Read index values from the given index file.
159 * Return 0 on success, -1 on error.
161 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
162 struct ctf_packet_index
*element
)
165 int fd
= index_file
->fd
;
166 size_t len
= index_file
->element_len
;
174 ret
= lttng_read(fd
, element
, len
);
176 PERROR("read index file");
180 ERR("lttng_read expected %zu, returned %zd", len
, ret
);
190 * Open index file using a given path, channel name and tracefile count.
192 * Return allocated struct lttng_index_file, NULL on error.
194 struct lttng_index_file
*lttng_index_file_open(const char *path_name
,
195 const char *channel_name
, uint64_t tracefile_count
,
196 uint64_t tracefile_count_current
)
198 struct lttng_index_file
*index_file
;
201 char fullpath
[PATH_MAX
];
202 struct ctf_packet_index_file_hdr hdr
;
203 uint32_t major
, minor
, element_len
;
206 assert(channel_name
);
208 index_file
= zmalloc(sizeof(*index_file
));
210 PERROR("allocating lttng_index_file");
214 if (tracefile_count
> 0) {
215 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s_%"
216 PRIu64 DEFAULT_INDEX_FILE_SUFFIX
, path_name
,
217 channel_name
, tracefile_count_current
);
219 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
220 DEFAULT_INDEX_FILE_SUFFIX
, path_name
, channel_name
);
223 PERROR("snprintf index path");
227 DBG("Index opening file %s in read only", fullpath
);
228 read_fd
= open(fullpath
, O_RDONLY
);
230 PERROR("opening index in read-only");
234 read_len
= lttng_read(read_fd
, &hdr
, sizeof(hdr
));
236 PERROR("Reading index header");
240 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
241 ERR("Invalid header magic");
244 major
= be32toh(hdr
.index_major
);
245 minor
= be32toh(hdr
.index_minor
);
246 element_len
= be32toh(hdr
.packet_index_len
);
248 if (major
!= CTF_INDEX_MAJOR
) {
249 ERR("Invalid header version");
252 if (element_len
> sizeof(struct ctf_packet_index
)) {
253 ERR("Index element length too long");
257 index_file
->fd
= read_fd
;
258 index_file
->major
= major
;
259 index_file
->minor
= minor
;
260 index_file
->element_len
= element_len
;
261 urcu_ref_init(&index_file
->ref
);
269 close_ret
= close(read_fd
);
271 PERROR("close read fd %d", read_fd
);
280 void lttng_index_file_get(struct lttng_index_file
*index_file
)
282 urcu_ref_get(&index_file
->ref
);
285 static void lttng_index_file_release(struct urcu_ref
*ref
)
287 struct lttng_index_file
*index_file
= caa_container_of(ref
,
288 struct lttng_index_file
, ref
);
290 if (close(index_file
->fd
)) {
291 PERROR("close index fd");
296 void lttng_index_file_put(struct lttng_index_file
*index_file
)
298 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);