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>
35 * Create the index file associated with a trace file.
37 * Return allocated struct lttng_index_file, NULL on error.
39 struct lttng_index_file
*lttng_index_file_create(const char *path_name
,
40 char *stream_name
, int uid
, int gid
,
41 uint64_t size
, uint64_t count
, uint32_t major
, uint32_t minor
)
43 struct lttng_index_file
*index_file
;
46 struct ctf_packet_index_file_hdr hdr
;
47 char fullpath
[PATH_MAX
];
48 uint32_t element_len
= ctf_packet_index_len(major
, minor
);
50 index_file
= zmalloc(sizeof(*index_file
));
52 PERROR("allocating lttng_index_file");
56 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
,
59 PERROR("snprintf index path");
63 /* Create index directory if necessary. */
64 ret
= utils_mkdir(fullpath
, S_IRWXU
| S_IRWXG
, uid
, gid
);
66 if (errno
!= EEXIST
) {
67 PERROR("Index trace directory creation error");
73 * For tracefile rotation. We need to unlink the old
74 * file if present to synchronize with the tail of the
75 * live viewer which could be working on this same file.
76 * By doing so, any reference to the old index file
77 * stays valid even if we re-create a new file with the
78 * same name afterwards.
80 ret
= utils_unlink_stream_file(fullpath
, stream_name
, size
, count
, uid
,
81 gid
, DEFAULT_INDEX_FILE_SUFFIX
);
82 if (ret
< 0 && errno
!= ENOENT
) {
85 ret
= utils_create_stream_file(fullpath
, stream_name
, size
, count
, uid
,
86 gid
, DEFAULT_INDEX_FILE_SUFFIX
);
92 ctf_packet_index_file_hdr_init(&hdr
, major
, minor
);
93 size_ret
= lttng_write(fd
, &hdr
, sizeof(hdr
));
94 if (size_ret
< sizeof(hdr
)) {
95 PERROR("write index header");
99 index_file
->major
= major
;
100 index_file
->minor
= minor
;
101 index_file
->element_len
= element_len
;
102 urcu_ref_init(&index_file
->ref
);
110 close_ret
= close(fd
);
112 PERROR("close index fd");
119 struct lttng_index_file
*lttng_index_file_create_from_trace_chunk(
120 struct lttng_trace_chunk
*chunk
,
121 const char *channel_path
, char *stream_name
,
122 uint64_t stream_file_size
, uint64_t stream_count
,
123 uint32_t index_major
, uint32_t index_minor
,
124 bool unlink_existing_file
)
126 struct lttng_index_file
*index_file
;
127 enum lttng_trace_chunk_status chunk_status
;
130 struct ctf_packet_index_file_hdr hdr
;
131 char index_directory_path
[LTTNG_PATH_MAX
];
132 char index_file_path
[LTTNG_PATH_MAX
];
133 const uint32_t element_len
= ctf_packet_index_len(index_major
,
135 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
136 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
138 index_file
= zmalloc(sizeof(*index_file
));
140 PERROR("Failed to allocate lttng_index_file");
144 ret
= snprintf(index_directory_path
, sizeof(index_directory_path
),
145 "%s/" DEFAULT_INDEX_DIR
, channel_path
);
146 if (ret
< 0 || ret
>= sizeof(index_directory_path
)) {
147 ERR("Failed to format index directory path");
151 ret
= utils_stream_file_path(index_directory_path
, stream_name
,
152 stream_file_size
, stream_count
,
153 DEFAULT_INDEX_FILE_SUFFIX
,
154 index_file_path
, sizeof(index_file_path
));
159 if (unlink_existing_file
) {
161 * For tracefile rotation. We need to unlink the old
162 * file if present to synchronize with the tail of the
163 * live viewer which could be working on this same file.
164 * By doing so, any reference to the old index file
165 * stays valid even if we re-create a new file with the
166 * same name afterwards.
168 chunk_status
= lttng_trace_chunk_unlink_file(chunk
,
170 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
175 chunk_status
= lttng_trace_chunk_open_file(chunk
, index_file_path
,
177 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
181 ctf_packet_index_file_hdr_init(&hdr
, index_major
, index_minor
);
182 size_ret
= lttng_write(fd
, &hdr
, sizeof(hdr
));
183 if (size_ret
< sizeof(hdr
)) {
184 PERROR("Failed to write index header");
188 index_file
->major
= index_major
;
189 index_file
->minor
= index_minor
;
190 index_file
->element_len
= element_len
;
191 urcu_ref_init(&index_file
->ref
);
199 PERROR("Failed to close file descriptor of index file");
207 * Write index values to the given index file.
209 * Return 0 on success, -1 on error.
211 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
212 const struct ctf_packet_index
*element
)
222 len
= index_file
->element_len
;
228 ret
= lttng_write(fd
, element
, len
);
230 PERROR("writing index file");
240 * Read index values from the given index file.
242 * Return 0 on success, -1 on error.
244 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
245 struct ctf_packet_index
*element
)
248 int fd
= index_file
->fd
;
249 size_t len
= index_file
->element_len
;
257 ret
= lttng_read(fd
, element
, len
);
259 PERROR("read index file");
263 ERR("lttng_read expected %zu, returned %zd", len
, ret
);
273 * Open index file using a given path, channel name and tracefile count.
275 * Return allocated struct lttng_index_file, NULL on error.
277 struct lttng_index_file
*lttng_index_file_open(const char *path_name
,
278 const char *channel_name
, uint64_t tracefile_count
,
279 uint64_t tracefile_count_current
)
281 struct lttng_index_file
*index_file
;
284 char fullpath
[PATH_MAX
];
285 struct ctf_packet_index_file_hdr hdr
;
286 uint32_t major
, minor
, element_len
;
289 assert(channel_name
);
291 index_file
= zmalloc(sizeof(*index_file
));
293 PERROR("allocating lttng_index_file");
297 if (tracefile_count
> 0) {
298 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s_%"
299 PRIu64 DEFAULT_INDEX_FILE_SUFFIX
, path_name
,
300 channel_name
, tracefile_count_current
);
302 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
303 DEFAULT_INDEX_FILE_SUFFIX
, path_name
, channel_name
);
306 PERROR("snprintf index path");
310 DBG("Index opening file %s in read only", fullpath
);
311 read_fd
= open(fullpath
, O_RDONLY
);
313 PERROR("opening index in read-only");
317 read_len
= lttng_read(read_fd
, &hdr
, sizeof(hdr
));
319 PERROR("Reading index header");
323 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
324 ERR("Invalid header magic");
327 major
= be32toh(hdr
.index_major
);
328 minor
= be32toh(hdr
.index_minor
);
329 element_len
= be32toh(hdr
.packet_index_len
);
331 if (major
!= CTF_INDEX_MAJOR
) {
332 ERR("Invalid header version");
335 if (element_len
> sizeof(struct ctf_packet_index
)) {
336 ERR("Index element length too long");
340 index_file
->fd
= read_fd
;
341 index_file
->major
= major
;
342 index_file
->minor
= minor
;
343 index_file
->element_len
= element_len
;
344 urcu_ref_init(&index_file
->ref
);
352 close_ret
= close(read_fd
);
354 PERROR("close read fd %d", read_fd
);
363 void lttng_index_file_get(struct lttng_index_file
*index_file
)
365 urcu_ref_get(&index_file
->ref
);
368 static void lttng_index_file_release(struct urcu_ref
*ref
)
370 struct lttng_index_file
*index_file
= caa_container_of(ref
,
371 struct lttng_index_file
, ref
);
373 if (close(index_file
->fd
)) {
374 PERROR("close index fd");
379 void lttng_index_file_put(struct lttng_index_file
*index_file
)
381 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);