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 | ||
34 | /* | |
35 | * Create the index file associated with a trace file. | |
36 | * | |
f8f3885c | 37 | * Return allocated struct lttng_index_file, NULL on error. |
309167d2 | 38 | */ |
cb523e02 | 39 | struct lttng_index_file *lttng_index_file_create(const char *path_name, |
f8f3885c MD |
40 | char *stream_name, int uid, int gid, |
41 | uint64_t size, uint64_t count, uint32_t major, uint32_t minor) | |
309167d2 | 42 | { |
f8f3885c | 43 | struct lttng_index_file *index_file; |
309167d2 | 44 | int ret, fd = -1; |
6cd525e8 | 45 | ssize_t size_ret; |
50adc264 | 46 | struct ctf_packet_index_file_hdr hdr; |
1c20f0e2 | 47 | char fullpath[PATH_MAX]; |
f8f3885c MD |
48 | uint32_t element_len = ctf_packet_index_len(major, minor); |
49 | ||
50 | index_file = zmalloc(sizeof(*index_file)); | |
51 | if (!index_file) { | |
52 | PERROR("allocating lttng_index_file"); | |
53 | goto error; | |
54 | } | |
309167d2 | 55 | |
1c20f0e2 JD |
56 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR, |
57 | path_name); | |
58 | if (ret < 0) { | |
59 | PERROR("snprintf index path"); | |
60 | goto error; | |
61 | } | |
62 | ||
63 | /* Create index directory if necessary. */ | |
7591bab1 | 64 | ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid); |
1c20f0e2 | 65 | if (ret < 0) { |
df5b86c8 | 66 | if (errno != EEXIST) { |
6347a8b5 | 67 | PERROR("Index trace directory creation error"); |
1c20f0e2 JD |
68 | goto error; |
69 | } | |
70 | } | |
71 | ||
7591bab1 MD |
72 | /* |
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. | |
79 | */ | |
80 | ret = utils_unlink_stream_file(fullpath, stream_name, size, count, uid, | |
81 | gid, DEFAULT_INDEX_FILE_SUFFIX); | |
82 | if (ret < 0 && errno != ENOENT) { | |
83 | goto error; | |
84 | } | |
1c20f0e2 | 85 | ret = utils_create_stream_file(fullpath, stream_name, size, count, uid, |
309167d2 JD |
86 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
87 | if (ret < 0) { | |
88 | goto error; | |
89 | } | |
90 | fd = ret; | |
91 | ||
71b4cdb4 | 92 | ctf_packet_index_file_hdr_init(&hdr, major, minor); |
6cd525e8 MD |
93 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); |
94 | if (size_ret < sizeof(hdr)) { | |
309167d2 JD |
95 | PERROR("write index header"); |
96 | goto error; | |
97 | } | |
f8f3885c MD |
98 | index_file->fd = fd; |
99 | index_file->major = major; | |
100 | index_file->minor = minor; | |
101 | index_file->element_len = element_len; | |
102 | urcu_ref_init(&index_file->ref); | |
309167d2 | 103 | |
f8f3885c | 104 | return index_file; |
309167d2 JD |
105 | |
106 | error: | |
107 | if (fd >= 0) { | |
108 | int close_ret; | |
109 | ||
110 | close_ret = close(fd); | |
111 | if (close_ret < 0) { | |
112 | PERROR("close index fd"); | |
113 | } | |
114 | } | |
f8f3885c MD |
115 | free(index_file); |
116 | return NULL; | |
309167d2 JD |
117 | } |
118 | ||
d2956687 JG |
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) | |
125 | { | |
126 | struct lttng_index_file *index_file; | |
127 | enum lttng_trace_chunk_status chunk_status; | |
128 | int ret, fd = -1; | |
129 | ssize_t size_ret; | |
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, | |
134 | index_minor); | |
135 | const int flags = O_WRONLY | O_CREAT | O_TRUNC; | |
136 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
137 | ||
138 | index_file = zmalloc(sizeof(*index_file)); | |
139 | if (!index_file) { | |
140 | PERROR("Failed to allocate lttng_index_file"); | |
141 | goto error; | |
142 | } | |
143 | ||
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"); | |
148 | goto error; | |
149 | } | |
150 | ||
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)); | |
155 | if (ret) { | |
156 | goto error; | |
157 | } | |
158 | ||
159 | if (unlink_existing_file) { | |
160 | /* | |
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. | |
167 | */ | |
168 | chunk_status = lttng_trace_chunk_unlink_file(chunk, | |
169 | index_file_path); | |
170 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
171 | goto error; | |
172 | } | |
173 | } | |
174 | ||
175 | chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path, | |
176 | flags, mode, &fd); | |
177 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
178 | goto error; | |
179 | } | |
180 | ||
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"); | |
185 | goto error; | |
186 | } | |
187 | index_file->fd = fd; | |
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); | |
192 | ||
193 | return index_file; | |
194 | ||
195 | error: | |
196 | if (fd >= 0) { | |
197 | ret = close(fd); | |
198 | if (ret < 0) { | |
199 | PERROR("Failed to close file descriptor of index file"); | |
200 | } | |
201 | } | |
202 | free(index_file); | |
203 | return NULL; | |
204 | } | |
205 | ||
309167d2 | 206 | /* |
f8f3885c | 207 | * Write index values to the given index file. |
309167d2 | 208 | * |
f8f3885c | 209 | * Return 0 on success, -1 on error. |
309167d2 | 210 | */ |
f8f3885c MD |
211 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
212 | const struct ctf_packet_index *element) | |
309167d2 | 213 | { |
0b0ac4a9 JR |
214 | int fd; |
215 | size_t len; | |
6cd525e8 | 216 | ssize_t ret; |
309167d2 | 217 | |
0b0ac4a9 | 218 | assert(index_file); |
f8f3885c | 219 | assert(element); |
309167d2 | 220 | |
0b0ac4a9 JR |
221 | fd = index_file->fd; |
222 | len = index_file->element_len; | |
223 | ||
183f6fa2 | 224 | if (fd < 0) { |
183f6fa2 DG |
225 | goto error; |
226 | } | |
227 | ||
f8f3885c | 228 | ret = lttng_write(fd, element, len); |
6cd525e8 | 229 | if (ret < len) { |
309167d2 | 230 | PERROR("writing index file"); |
f8f3885c MD |
231 | goto error; |
232 | } | |
233 | return 0; | |
234 | ||
235 | error: | |
236 | return -1; | |
237 | } | |
238 | ||
239 | /* | |
240 | * Read index values from the given index file. | |
241 | * | |
242 | * Return 0 on success, -1 on error. | |
243 | */ | |
244 | int lttng_index_file_read(const struct lttng_index_file *index_file, | |
245 | struct ctf_packet_index *element) | |
246 | { | |
247 | ssize_t ret; | |
248 | int fd = index_file->fd; | |
249 | size_t len = index_file->element_len; | |
250 | ||
251 | assert(element); | |
252 | ||
253 | if (fd < 0) { | |
254 | goto error; | |
255 | } | |
256 | ||
257 | ret = lttng_read(fd, element, len); | |
2239b15a | 258 | if (ret < 0) { |
f8f3885c MD |
259 | PERROR("read index file"); |
260 | goto error; | |
309167d2 | 261 | } |
2239b15a MD |
262 | if (ret < len) { |
263 | ERR("lttng_read expected %zu, returned %zd", len, ret); | |
264 | goto error; | |
265 | } | |
f8f3885c | 266 | return 0; |
309167d2 | 267 | |
183f6fa2 | 268 | error: |
f8f3885c | 269 | return -1; |
309167d2 | 270 | } |
2f8f53af DG |
271 | |
272 | /* | |
273 | * Open index file using a given path, channel name and tracefile count. | |
274 | * | |
f8f3885c | 275 | * Return allocated struct lttng_index_file, NULL on error. |
2f8f53af | 276 | */ |
f8f3885c MD |
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) | |
2f8f53af | 280 | { |
f8f3885c | 281 | struct lttng_index_file *index_file; |
2f8f53af DG |
282 | int ret, read_fd; |
283 | ssize_t read_len; | |
284 | char fullpath[PATH_MAX]; | |
285 | struct ctf_packet_index_file_hdr hdr; | |
f8f3885c | 286 | uint32_t major, minor, element_len; |
2f8f53af DG |
287 | |
288 | assert(path_name); | |
289 | assert(channel_name); | |
290 | ||
f8f3885c MD |
291 | index_file = zmalloc(sizeof(*index_file)); |
292 | if (!index_file) { | |
293 | PERROR("allocating lttng_index_file"); | |
294 | goto error; | |
295 | } | |
296 | ||
2f8f53af DG |
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); | |
301 | } else { | |
302 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s" | |
303 | DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name); | |
304 | } | |
305 | if (ret < 0) { | |
306 | PERROR("snprintf index path"); | |
307 | goto error; | |
308 | } | |
309 | ||
310 | DBG("Index opening file %s in read only", fullpath); | |
311 | read_fd = open(fullpath, O_RDONLY); | |
312 | if (read_fd < 0) { | |
4a3a61c1 | 313 | PERROR("opening index in read-only"); |
2f8f53af DG |
314 | goto error; |
315 | } | |
316 | ||
317 | read_len = lttng_read(read_fd, &hdr, sizeof(hdr)); | |
318 | if (read_len < 0) { | |
319 | PERROR("Reading index header"); | |
320 | goto error_close; | |
321 | } | |
322 | ||
323 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { | |
324 | ERR("Invalid header magic"); | |
325 | goto error_close; | |
326 | } | |
f8f3885c MD |
327 | major = be32toh(hdr.index_major); |
328 | minor = be32toh(hdr.index_minor); | |
329 | element_len = be32toh(hdr.packet_index_len); | |
330 | ||
331 | if (major != CTF_INDEX_MAJOR) { | |
2f8f53af DG |
332 | ERR("Invalid header version"); |
333 | goto error_close; | |
334 | } | |
a3461e94 MD |
335 | if (element_len > sizeof(struct ctf_packet_index)) { |
336 | ERR("Index element length too long"); | |
337 | goto error_close; | |
338 | } | |
2f8f53af | 339 | |
f8f3885c MD |
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); | |
345 | ||
346 | return index_file; | |
2f8f53af DG |
347 | |
348 | error_close: | |
349 | if (read_fd >= 0) { | |
350 | int close_ret; | |
351 | ||
352 | close_ret = close(read_fd); | |
353 | if (close_ret < 0) { | |
354 | PERROR("close read fd %d", read_fd); | |
355 | } | |
356 | } | |
2f8f53af DG |
357 | |
358 | error: | |
f8f3885c MD |
359 | free(index_file); |
360 | return NULL; | |
361 | } | |
362 | ||
363 | void lttng_index_file_get(struct lttng_index_file *index_file) | |
364 | { | |
365 | urcu_ref_get(&index_file->ref); | |
366 | } | |
367 | ||
368 | static void lttng_index_file_release(struct urcu_ref *ref) | |
369 | { | |
370 | struct lttng_index_file *index_file = caa_container_of(ref, | |
371 | struct lttng_index_file, ref); | |
372 | ||
373 | if (close(index_file->fd)) { | |
374 | PERROR("close index fd"); | |
375 | } | |
376 | free(index_file); | |
377 | } | |
378 | ||
379 | void lttng_index_file_put(struct lttng_index_file *index_file) | |
380 | { | |
381 | urcu_ref_put(&index_file->ref, lttng_index_file_release); | |
2f8f53af | 382 | } |