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 | ||
ebb29c10 JG |
34 | #define WRITE_FILE_FLAGS O_WRONLY | O_CREAT | O_TRUNC |
35 | #define READ_ONLY_FILE_FLAGS O_RDONLY | |
36 | ||
37 | static struct lttng_index_file *_lttng_index_file_create_from_trace_chunk( | |
d2956687 | 38 | struct lttng_trace_chunk *chunk, |
ebb29c10 | 39 | const char *channel_path, const char *stream_name, |
c35f9726 | 40 | uint64_t stream_file_size, uint64_t stream_file_index, |
d2956687 | 41 | uint32_t index_major, uint32_t index_minor, |
ebb29c10 JG |
42 | bool unlink_existing_file, |
43 | int flags) | |
d2956687 JG |
44 | { |
45 | struct lttng_index_file *index_file; | |
46 | enum lttng_trace_chunk_status chunk_status; | |
47 | int ret, fd = -1; | |
48 | ssize_t size_ret; | |
49 | struct ctf_packet_index_file_hdr hdr; | |
50 | char index_directory_path[LTTNG_PATH_MAX]; | |
51 | char index_file_path[LTTNG_PATH_MAX]; | |
52 | const uint32_t element_len = ctf_packet_index_len(index_major, | |
53 | index_minor); | |
d2956687 | 54 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
ebb29c10 | 55 | const bool acquired_reference = lttng_trace_chunk_get(chunk); |
c35f9726 JG |
56 | |
57 | assert(acquired_reference); | |
d2956687 JG |
58 | |
59 | index_file = zmalloc(sizeof(*index_file)); | |
60 | if (!index_file) { | |
61 | PERROR("Failed to allocate lttng_index_file"); | |
62 | goto error; | |
63 | } | |
64 | ||
c35f9726 | 65 | index_file->trace_chunk = chunk; |
d2956687 JG |
66 | ret = snprintf(index_directory_path, sizeof(index_directory_path), |
67 | "%s/" DEFAULT_INDEX_DIR, channel_path); | |
68 | if (ret < 0 || ret >= sizeof(index_directory_path)) { | |
69 | ERR("Failed to format index directory path"); | |
70 | goto error; | |
71 | } | |
72 | ||
73 | ret = utils_stream_file_path(index_directory_path, stream_name, | |
c35f9726 | 74 | stream_file_size, stream_file_index, |
d2956687 JG |
75 | DEFAULT_INDEX_FILE_SUFFIX, |
76 | index_file_path, sizeof(index_file_path)); | |
77 | if (ret) { | |
78 | goto error; | |
79 | } | |
80 | ||
81 | if (unlink_existing_file) { | |
82 | /* | |
83 | * For tracefile rotation. We need to unlink the old | |
84 | * file if present to synchronize with the tail of the | |
85 | * live viewer which could be working on this same file. | |
86 | * By doing so, any reference to the old index file | |
87 | * stays valid even if we re-create a new file with the | |
88 | * same name afterwards. | |
89 | */ | |
348a81dc JG |
90 | chunk_status = lttng_trace_chunk_unlink_file( |
91 | chunk, index_file_path); | |
92 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK && | |
93 | !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR && | |
94 | errno == ENOENT)) { | |
d2956687 JG |
95 | goto error; |
96 | } | |
348a81dc | 97 | } |
d2956687 JG |
98 | |
99 | chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path, | |
100 | flags, mode, &fd); | |
101 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
102 | goto error; | |
103 | } | |
104 | ||
105 | ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor); | |
106 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); | |
107 | if (size_ret < sizeof(hdr)) { | |
108 | PERROR("Failed to write index header"); | |
109 | goto error; | |
110 | } | |
111 | index_file->fd = fd; | |
112 | index_file->major = index_major; | |
113 | index_file->minor = index_minor; | |
114 | index_file->element_len = element_len; | |
115 | urcu_ref_init(&index_file->ref); | |
116 | ||
117 | return index_file; | |
118 | ||
119 | error: | |
120 | if (fd >= 0) { | |
121 | ret = close(fd); | |
122 | if (ret < 0) { | |
123 | PERROR("Failed to close file descriptor of index file"); | |
124 | } | |
125 | } | |
126 | free(index_file); | |
127 | return NULL; | |
128 | } | |
129 | ||
ebb29c10 JG |
130 | struct lttng_index_file *lttng_index_file_create_from_trace_chunk( |
131 | struct lttng_trace_chunk *chunk, | |
132 | const char *channel_path, const char *stream_name, | |
133 | uint64_t stream_file_size, uint64_t stream_file_index, | |
134 | uint32_t index_major, uint32_t index_minor, | |
135 | bool unlink_existing_file) | |
136 | { | |
137 | return _lttng_index_file_create_from_trace_chunk(chunk, channel_path, | |
138 | stream_name, stream_file_size, stream_file_index, | |
139 | index_major, index_minor, unlink_existing_file, | |
140 | WRITE_FILE_FLAGS); | |
141 | } | |
142 | ||
143 | struct lttng_index_file *lttng_index_file_create_from_trace_chunk_read_only( | |
144 | struct lttng_trace_chunk *chunk, | |
145 | const char *channel_path, const char *stream_name, | |
146 | uint64_t stream_file_size, uint64_t stream_file_index, | |
147 | uint32_t index_major, uint32_t index_minor) | |
148 | { | |
149 | return _lttng_index_file_create_from_trace_chunk(chunk, channel_path, | |
150 | stream_name, stream_file_size, stream_file_index, | |
151 | index_major, index_minor, false, | |
152 | READ_ONLY_FILE_FLAGS); | |
153 | } | |
154 | ||
309167d2 | 155 | /* |
f8f3885c | 156 | * Write index values to the given index file. |
309167d2 | 157 | * |
f8f3885c | 158 | * Return 0 on success, -1 on error. |
309167d2 | 159 | */ |
f8f3885c MD |
160 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
161 | const struct ctf_packet_index *element) | |
309167d2 | 162 | { |
0b0ac4a9 JR |
163 | int fd; |
164 | size_t len; | |
6cd525e8 | 165 | ssize_t ret; |
309167d2 | 166 | |
0b0ac4a9 | 167 | assert(index_file); |
f8f3885c | 168 | assert(element); |
309167d2 | 169 | |
0b0ac4a9 JR |
170 | fd = index_file->fd; |
171 | len = index_file->element_len; | |
172 | ||
183f6fa2 | 173 | if (fd < 0) { |
183f6fa2 DG |
174 | goto error; |
175 | } | |
176 | ||
f8f3885c | 177 | ret = lttng_write(fd, element, len); |
6cd525e8 | 178 | if (ret < len) { |
309167d2 | 179 | PERROR("writing index file"); |
f8f3885c MD |
180 | goto error; |
181 | } | |
182 | return 0; | |
183 | ||
184 | error: | |
185 | return -1; | |
186 | } | |
187 | ||
188 | /* | |
189 | * Read index values from the given index file. | |
190 | * | |
191 | * Return 0 on success, -1 on error. | |
192 | */ | |
193 | int lttng_index_file_read(const struct lttng_index_file *index_file, | |
194 | struct ctf_packet_index *element) | |
195 | { | |
196 | ssize_t ret; | |
197 | int fd = index_file->fd; | |
198 | size_t len = index_file->element_len; | |
199 | ||
200 | assert(element); | |
201 | ||
202 | if (fd < 0) { | |
203 | goto error; | |
204 | } | |
205 | ||
206 | ret = lttng_read(fd, element, len); | |
2239b15a | 207 | if (ret < 0) { |
f8f3885c MD |
208 | PERROR("read index file"); |
209 | goto error; | |
309167d2 | 210 | } |
2239b15a MD |
211 | if (ret < len) { |
212 | ERR("lttng_read expected %zu, returned %zd", len, ret); | |
213 | goto error; | |
214 | } | |
f8f3885c | 215 | return 0; |
309167d2 | 216 | |
183f6fa2 | 217 | error: |
f8f3885c | 218 | return -1; |
309167d2 | 219 | } |
2f8f53af | 220 | |
f8f3885c MD |
221 | void lttng_index_file_get(struct lttng_index_file *index_file) |
222 | { | |
223 | urcu_ref_get(&index_file->ref); | |
224 | } | |
225 | ||
226 | static void lttng_index_file_release(struct urcu_ref *ref) | |
227 | { | |
228 | struct lttng_index_file *index_file = caa_container_of(ref, | |
229 | struct lttng_index_file, ref); | |
230 | ||
231 | if (close(index_file->fd)) { | |
232 | PERROR("close index fd"); | |
233 | } | |
c35f9726 | 234 | lttng_trace_chunk_put(index_file->trace_chunk); |
f8f3885c MD |
235 | free(index_file); |
236 | } | |
237 | ||
238 | void lttng_index_file_put(struct lttng_index_file *index_file) | |
239 | { | |
240 | urcu_ref_put(&index_file->ref, lttng_index_file_release); | |
2f8f53af | 241 | } |