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 <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/utils.h>
34 * Create the index file associated with a trace file.
36 * Return allocated struct lttng_index_file, NULL on error.
38 struct lttng_index_file
*lttng_index_file_create(const char *path_name
,
39 char *stream_name
, int uid
, int gid
,
40 uint64_t size
, uint64_t count
, uint32_t major
, uint32_t minor
)
42 struct lttng_index_file
*index_file
;
45 struct ctf_packet_index_file_hdr hdr
;
46 char fullpath
[PATH_MAX
];
47 uint32_t element_len
= ctf_packet_index_len(major
, minor
);
49 index_file
= zmalloc(sizeof(*index_file
));
51 PERROR("allocating lttng_index_file");
55 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
,
58 PERROR("snprintf index path");
62 /* Create index directory if necessary. */
63 ret
= utils_mkdir(fullpath
, S_IRWXU
| S_IRWXG
, uid
, gid
);
65 if (errno
!= EEXIST
) {
66 PERROR("Index trace directory creation error");
72 * For tracefile rotation. We need to unlink the old
73 * file if present to synchronize with the tail of the
74 * live viewer which could be working on this same file.
75 * By doing so, any reference to the old index file
76 * stays valid even if we re-create a new file with the
77 * same name afterwards.
79 ret
= utils_unlink_stream_file(fullpath
, stream_name
, size
, count
, uid
,
80 gid
, DEFAULT_INDEX_FILE_SUFFIX
);
81 if (ret
< 0 && errno
!= ENOENT
) {
84 ret
= utils_create_stream_file(fullpath
, stream_name
, size
, count
, uid
,
85 gid
, DEFAULT_INDEX_FILE_SUFFIX
);
91 hdr
.magic
= htobe32(CTF_INDEX_MAGIC
);
92 hdr
.index_major
= htobe32(major
);
93 hdr
.index_minor
= htobe32(minor
);
94 hdr
.packet_index_len
= htobe32(element_len
);
96 size_ret
= lttng_write(fd
, &hdr
, sizeof(hdr
));
97 if (size_ret
< sizeof(hdr
)) {
98 PERROR("write index header");
102 index_file
->major
= major
;
103 index_file
->minor
= minor
;
104 index_file
->element_len
= element_len
;
105 urcu_ref_init(&index_file
->ref
);
113 close_ret
= close(fd
);
115 PERROR("close index fd");
123 * Write index values to the given index file.
125 * Return 0 on success, -1 on error.
127 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
128 const struct ctf_packet_index
*element
)
138 len
= index_file
->element_len
;
144 ret
= lttng_write(fd
, element
, len
);
146 PERROR("writing index file");
156 * Read index values from the given index file.
158 * Return 0 on success, -1 on error.
160 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
161 struct ctf_packet_index
*element
)
164 int fd
= index_file
->fd
;
165 size_t len
= index_file
->element_len
;
173 ret
= lttng_read(fd
, element
, len
);
175 PERROR("read index file");
185 * Open index file using a given path, channel name and tracefile count.
187 * Return allocated struct lttng_index_file, NULL on error.
189 struct lttng_index_file
*lttng_index_file_open(const char *path_name
,
190 const char *channel_name
, uint64_t tracefile_count
,
191 uint64_t tracefile_count_current
)
193 struct lttng_index_file
*index_file
;
196 char fullpath
[PATH_MAX
];
197 struct ctf_packet_index_file_hdr hdr
;
198 uint32_t major
, minor
, element_len
;
201 assert(channel_name
);
203 index_file
= zmalloc(sizeof(*index_file
));
205 PERROR("allocating lttng_index_file");
209 if (tracefile_count
> 0) {
210 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s_%"
211 PRIu64 DEFAULT_INDEX_FILE_SUFFIX
, path_name
,
212 channel_name
, tracefile_count_current
);
214 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
215 DEFAULT_INDEX_FILE_SUFFIX
, path_name
, channel_name
);
218 PERROR("snprintf index path");
222 DBG("Index opening file %s in read only", fullpath
);
223 read_fd
= open(fullpath
, O_RDONLY
);
225 PERROR("opening index in read-only");
229 read_len
= lttng_read(read_fd
, &hdr
, sizeof(hdr
));
231 PERROR("Reading index header");
235 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
236 ERR("Invalid header magic");
239 major
= be32toh(hdr
.index_major
);
240 minor
= be32toh(hdr
.index_minor
);
241 element_len
= be32toh(hdr
.packet_index_len
);
243 if (major
!= CTF_INDEX_MAJOR
) {
244 ERR("Invalid header version");
247 if (element_len
> sizeof(struct ctf_packet_index
)) {
248 ERR("Index element length too long");
252 index_file
->fd
= read_fd
;
253 index_file
->major
= major
;
254 index_file
->minor
= minor
;
255 index_file
->element_len
= element_len
;
256 urcu_ref_init(&index_file
->ref
);
264 close_ret
= close(read_fd
);
266 PERROR("close read fd %d", read_fd
);
275 void lttng_index_file_get(struct lttng_index_file
*index_file
)
277 urcu_ref_get(&index_file
->ref
);
280 static void lttng_index_file_release(struct urcu_ref
*ref
)
282 struct lttng_index_file
*index_file
= caa_container_of(ref
,
283 struct lttng_index_file
, ref
);
285 if (close(index_file
->fd
)) {
286 PERROR("close index fd");
291 void lttng_index_file_put(struct lttng_index_file
*index_file
)
293 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);