Commit | Line | Data |
---|---|---|
309167d2 JD |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
f8f3885c | 4 | * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
309167d2 JD |
5 | * |
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. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
309167d2 | 21 | #include <assert.h> |
1c20f0e2 | 22 | #include <sys/stat.h> |
2f8f53af DG |
23 | #include <sys/types.h> |
24 | #include <fcntl.h> | |
309167d2 | 25 | |
d2956687 | 26 | #include <lttng/constant.h> |
309167d2 JD |
27 | #include <common/common.h> |
28 | #include <common/defaults.h> | |
f263b7fd | 29 | #include <common/compat/endian.h> |
309167d2 JD |
30 | #include <common/utils.h> |
31 | ||
32 | #include "index.h" | |
33 | ||
d2956687 JG |
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) | |
40 | { | |
41 | struct lttng_index_file *index_file; | |
42 | enum lttng_trace_chunk_status chunk_status; | |
43 | int ret, fd = -1; | |
44 | ssize_t size_ret; | |
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, | |
49 | index_minor); | |
50 | const int flags = O_WRONLY | O_CREAT | O_TRUNC; | |
51 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
52 | ||
53 | index_file = zmalloc(sizeof(*index_file)); | |
54 | if (!index_file) { | |
55 | PERROR("Failed to allocate lttng_index_file"); | |
56 | goto error; | |
57 | } | |
58 | ||
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"); | |
63 | goto error; | |
64 | } | |
65 | ||
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)); | |
70 | if (ret) { | |
71 | goto error; | |
72 | } | |
73 | ||
74 | if (unlink_existing_file) { | |
75 | /* | |
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. | |
82 | */ | |
348a81dc JG |
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 && | |
87 | errno == ENOENT)) { | |
d2956687 JG |
88 | goto error; |
89 | } | |
348a81dc | 90 | } |
d2956687 JG |
91 | |
92 | chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path, | |
93 | flags, mode, &fd); | |
94 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
95 | goto error; | |
96 | } | |
97 | ||
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"); | |
102 | goto error; | |
103 | } | |
104 | index_file->fd = fd; | |
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); | |
109 | ||
110 | return index_file; | |
111 | ||
112 | error: | |
113 | if (fd >= 0) { | |
114 | ret = close(fd); | |
115 | if (ret < 0) { | |
116 | PERROR("Failed to close file descriptor of index file"); | |
117 | } | |
118 | } | |
119 | free(index_file); | |
120 | return NULL; | |
121 | } | |
122 | ||
309167d2 | 123 | /* |
f8f3885c | 124 | * Write index values to the given index file. |
309167d2 | 125 | * |
f8f3885c | 126 | * Return 0 on success, -1 on error. |
309167d2 | 127 | */ |
f8f3885c MD |
128 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
129 | const struct ctf_packet_index *element) | |
309167d2 | 130 | { |
0b0ac4a9 JR |
131 | int fd; |
132 | size_t len; | |
6cd525e8 | 133 | ssize_t ret; |
309167d2 | 134 | |
0b0ac4a9 | 135 | assert(index_file); |
f8f3885c | 136 | assert(element); |
309167d2 | 137 | |
0b0ac4a9 JR |
138 | fd = index_file->fd; |
139 | len = index_file->element_len; | |
140 | ||
183f6fa2 | 141 | if (fd < 0) { |
183f6fa2 DG |
142 | goto error; |
143 | } | |
144 | ||
f8f3885c | 145 | ret = lttng_write(fd, element, len); |
6cd525e8 | 146 | if (ret < len) { |
309167d2 | 147 | PERROR("writing index file"); |
f8f3885c MD |
148 | goto error; |
149 | } | |
150 | return 0; | |
151 | ||
152 | error: | |
153 | return -1; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Read index values from the given index file. | |
158 | * | |
159 | * Return 0 on success, -1 on error. | |
160 | */ | |
161 | int lttng_index_file_read(const struct lttng_index_file *index_file, | |
162 | struct ctf_packet_index *element) | |
163 | { | |
164 | ssize_t ret; | |
165 | int fd = index_file->fd; | |
166 | size_t len = index_file->element_len; | |
167 | ||
168 | assert(element); | |
169 | ||
170 | if (fd < 0) { | |
171 | goto error; | |
172 | } | |
173 | ||
174 | ret = lttng_read(fd, element, len); | |
2239b15a | 175 | if (ret < 0) { |
f8f3885c MD |
176 | PERROR("read index file"); |
177 | goto error; | |
309167d2 | 178 | } |
2239b15a MD |
179 | if (ret < len) { |
180 | ERR("lttng_read expected %zu, returned %zd", len, ret); | |
181 | goto error; | |
182 | } | |
f8f3885c | 183 | return 0; |
309167d2 | 184 | |
183f6fa2 | 185 | error: |
f8f3885c | 186 | return -1; |
309167d2 | 187 | } |
2f8f53af DG |
188 | |
189 | /* | |
190 | * Open index file using a given path, channel name and tracefile count. | |
191 | * | |
f8f3885c | 192 | * Return allocated struct lttng_index_file, NULL on error. |
2f8f53af | 193 | */ |
f8f3885c MD |
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) | |
2f8f53af | 197 | { |
f8f3885c | 198 | struct lttng_index_file *index_file; |
2f8f53af DG |
199 | int ret, read_fd; |
200 | ssize_t read_len; | |
201 | char fullpath[PATH_MAX]; | |
202 | struct ctf_packet_index_file_hdr hdr; | |
f8f3885c | 203 | uint32_t major, minor, element_len; |
2f8f53af DG |
204 | |
205 | assert(path_name); | |
206 | assert(channel_name); | |
207 | ||
f8f3885c MD |
208 | index_file = zmalloc(sizeof(*index_file)); |
209 | if (!index_file) { | |
210 | PERROR("allocating lttng_index_file"); | |
211 | goto error; | |
212 | } | |
213 | ||
2f8f53af DG |
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); | |
218 | } else { | |
219 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s" | |
220 | DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name); | |
221 | } | |
222 | if (ret < 0) { | |
223 | PERROR("snprintf index path"); | |
224 | goto error; | |
225 | } | |
226 | ||
227 | DBG("Index opening file %s in read only", fullpath); | |
228 | read_fd = open(fullpath, O_RDONLY); | |
229 | if (read_fd < 0) { | |
4a3a61c1 | 230 | PERROR("opening index in read-only"); |
2f8f53af DG |
231 | goto error; |
232 | } | |
233 | ||
234 | read_len = lttng_read(read_fd, &hdr, sizeof(hdr)); | |
235 | if (read_len < 0) { | |
236 | PERROR("Reading index header"); | |
237 | goto error_close; | |
238 | } | |
239 | ||
240 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { | |
241 | ERR("Invalid header magic"); | |
242 | goto error_close; | |
243 | } | |
f8f3885c MD |
244 | major = be32toh(hdr.index_major); |
245 | minor = be32toh(hdr.index_minor); | |
246 | element_len = be32toh(hdr.packet_index_len); | |
247 | ||
248 | if (major != CTF_INDEX_MAJOR) { | |
2f8f53af DG |
249 | ERR("Invalid header version"); |
250 | goto error_close; | |
251 | } | |
a3461e94 MD |
252 | if (element_len > sizeof(struct ctf_packet_index)) { |
253 | ERR("Index element length too long"); | |
254 | goto error_close; | |
255 | } | |
2f8f53af | 256 | |
f8f3885c MD |
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); | |
262 | ||
263 | return index_file; | |
2f8f53af DG |
264 | |
265 | error_close: | |
266 | if (read_fd >= 0) { | |
267 | int close_ret; | |
268 | ||
269 | close_ret = close(read_fd); | |
270 | if (close_ret < 0) { | |
271 | PERROR("close read fd %d", read_fd); | |
272 | } | |
273 | } | |
2f8f53af DG |
274 | |
275 | error: | |
f8f3885c MD |
276 | free(index_file); |
277 | return NULL; | |
278 | } | |
279 | ||
280 | void lttng_index_file_get(struct lttng_index_file *index_file) | |
281 | { | |
282 | urcu_ref_get(&index_file->ref); | |
283 | } | |
284 | ||
285 | static void lttng_index_file_release(struct urcu_ref *ref) | |
286 | { | |
287 | struct lttng_index_file *index_file = caa_container_of(ref, | |
288 | struct lttng_index_file, ref); | |
289 | ||
290 | if (close(index_file->fd)) { | |
291 | PERROR("close index fd"); | |
292 | } | |
293 | free(index_file); | |
294 | } | |
295 | ||
296 | void lttng_index_file_put(struct lttng_index_file *index_file) | |
297 | { | |
298 | urcu_ref_put(&index_file->ref, lttng_index_file_release); | |
2f8f53af | 299 | } |