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 JD |
25 | |
26 | #include <common/common.h> | |
27 | #include <common/defaults.h> | |
f263b7fd | 28 | #include <common/compat/endian.h> |
309167d2 JD |
29 | #include <common/utils.h> |
30 | ||
31 | #include "index.h" | |
32 | ||
33 | /* | |
34 | * Create the index file associated with a trace file. | |
35 | * | |
f8f3885c | 36 | * Return allocated struct lttng_index_file, NULL on error. |
309167d2 | 37 | */ |
f8f3885c MD |
38 | struct lttng_index_file *lttng_index_file_create(char *path_name, |
39 | char *stream_name, int uid, int gid, | |
40 | uint64_t size, uint64_t count, uint32_t major, uint32_t minor) | |
309167d2 | 41 | { |
f8f3885c | 42 | struct lttng_index_file *index_file; |
309167d2 | 43 | int ret, fd = -1; |
6cd525e8 | 44 | ssize_t size_ret; |
50adc264 | 45 | struct ctf_packet_index_file_hdr hdr; |
1c20f0e2 | 46 | char fullpath[PATH_MAX]; |
f8f3885c MD |
47 | uint32_t element_len = ctf_packet_index_len(major, minor); |
48 | ||
49 | index_file = zmalloc(sizeof(*index_file)); | |
50 | if (!index_file) { | |
51 | PERROR("allocating lttng_index_file"); | |
52 | goto error; | |
53 | } | |
309167d2 | 54 | |
1c20f0e2 JD |
55 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR, |
56 | path_name); | |
57 | if (ret < 0) { | |
58 | PERROR("snprintf index path"); | |
59 | goto error; | |
60 | } | |
61 | ||
62 | /* Create index directory if necessary. */ | |
7591bab1 | 63 | ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid); |
1c20f0e2 | 64 | if (ret < 0) { |
df5b86c8 | 65 | if (errno != EEXIST) { |
6347a8b5 | 66 | PERROR("Index trace directory creation error"); |
1c20f0e2 JD |
67 | goto error; |
68 | } | |
69 | } | |
70 | ||
7591bab1 MD |
71 | /* |
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. | |
78 | */ | |
79 | ret = utils_unlink_stream_file(fullpath, stream_name, size, count, uid, | |
80 | gid, DEFAULT_INDEX_FILE_SUFFIX); | |
7a5e50b1 MD |
81 | DBG("utils_unlink_stream_file %s %s ret %d errno %d", |
82 | fullpath, stream_name, ret, errno); | |
7591bab1 MD |
83 | if (ret < 0 && errno != ENOENT) { |
84 | goto error; | |
85 | } | |
1c20f0e2 | 86 | ret = utils_create_stream_file(fullpath, stream_name, size, count, uid, |
309167d2 | 87 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
7a5e50b1 MD |
88 | DBG("utils_create_stream_file %s %s ret %d errno %d", |
89 | fullpath, stream_name, ret, errno); | |
309167d2 JD |
90 | if (ret < 0) { |
91 | goto error; | |
92 | } | |
93 | fd = ret; | |
94 | ||
50adc264 | 95 | hdr.magic = htobe32(CTF_INDEX_MAGIC); |
f8f3885c MD |
96 | hdr.index_major = htobe32(major); |
97 | hdr.index_minor = htobe32(minor); | |
98 | hdr.packet_index_len = htobe32(element_len); | |
309167d2 | 99 | |
6cd525e8 MD |
100 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); |
101 | if (size_ret < sizeof(hdr)) { | |
309167d2 JD |
102 | PERROR("write index header"); |
103 | goto error; | |
104 | } | |
f8f3885c MD |
105 | index_file->fd = fd; |
106 | index_file->major = major; | |
107 | index_file->minor = minor; | |
108 | index_file->element_len = element_len; | |
109 | urcu_ref_init(&index_file->ref); | |
309167d2 | 110 | |
f8f3885c | 111 | return index_file; |
309167d2 JD |
112 | |
113 | error: | |
114 | if (fd >= 0) { | |
115 | int close_ret; | |
116 | ||
117 | close_ret = close(fd); | |
118 | if (close_ret < 0) { | |
119 | PERROR("close index fd"); | |
120 | } | |
121 | } | |
f8f3885c MD |
122 | free(index_file); |
123 | return NULL; | |
309167d2 JD |
124 | } |
125 | ||
7a5e50b1 MD |
126 | int lttng_index_file_unlink(char *path_name, |
127 | char *stream_name, int uid, int gid, | |
128 | uint64_t tracefile_size, | |
129 | uint64_t tracefile_count_current) | |
130 | { | |
131 | int ret; | |
132 | char fullpath[PATH_MAX]; | |
133 | ||
134 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR, | |
135 | path_name); | |
136 | if (ret < 0) { | |
137 | PERROR("snprintf index path"); | |
138 | goto error; | |
139 | } | |
140 | ||
141 | ret = utils_unlink_stream_file(fullpath, stream_name, | |
142 | tracefile_size, tracefile_count_current, uid, | |
143 | gid, DEFAULT_INDEX_FILE_SUFFIX); | |
144 | if (ret < 0 && errno != ENOENT) { | |
145 | goto error; | |
146 | } | |
147 | ||
148 | return 0; | |
149 | ||
150 | error: | |
151 | return -1; | |
152 | } | |
153 | ||
309167d2 | 154 | /* |
f8f3885c | 155 | * Write index values to the given index file. |
309167d2 | 156 | * |
f8f3885c | 157 | * Return 0 on success, -1 on error. |
309167d2 | 158 | */ |
f8f3885c MD |
159 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
160 | const struct ctf_packet_index *element) | |
309167d2 | 161 | { |
1ce22976 JR |
162 | int fd; |
163 | size_t len; | |
6cd525e8 | 164 | ssize_t ret; |
309167d2 | 165 | |
1ce22976 | 166 | assert(index_file); |
f8f3885c | 167 | assert(element); |
309167d2 | 168 | |
1ce22976 JR |
169 | fd = index_file->fd; |
170 | len = index_file->element_len; | |
171 | ||
183f6fa2 | 172 | if (fd < 0) { |
183f6fa2 DG |
173 | goto error; |
174 | } | |
175 | ||
f8f3885c | 176 | ret = lttng_write(fd, element, len); |
6cd525e8 | 177 | if (ret < len) { |
309167d2 | 178 | PERROR("writing index file"); |
f8f3885c MD |
179 | goto error; |
180 | } | |
181 | return 0; | |
182 | ||
183 | error: | |
184 | return -1; | |
185 | } | |
186 | ||
187 | /* | |
188 | * Read index values from the given index file. | |
189 | * | |
190 | * Return 0 on success, -1 on error. | |
191 | */ | |
192 | int lttng_index_file_read(const struct lttng_index_file *index_file, | |
193 | struct ctf_packet_index *element) | |
194 | { | |
195 | ssize_t ret; | |
196 | int fd = index_file->fd; | |
197 | size_t len = index_file->element_len; | |
198 | ||
199 | assert(element); | |
200 | ||
201 | if (fd < 0) { | |
202 | goto error; | |
203 | } | |
204 | ||
205 | ret = lttng_read(fd, element, len); | |
6dabdfa2 | 206 | if (ret < 0) { |
f8f3885c MD |
207 | PERROR("read index file"); |
208 | goto error; | |
309167d2 | 209 | } |
6dabdfa2 MD |
210 | if (ret < len) { |
211 | ERR("lttng_read expected %zu, returned %zd", len, ret); | |
212 | goto error; | |
213 | } | |
f8f3885c | 214 | return 0; |
309167d2 | 215 | |
183f6fa2 | 216 | error: |
f8f3885c | 217 | return -1; |
309167d2 | 218 | } |
2f8f53af DG |
219 | |
220 | /* | |
221 | * Open index file using a given path, channel name and tracefile count. | |
222 | * | |
f8f3885c | 223 | * Return allocated struct lttng_index_file, NULL on error. |
2f8f53af | 224 | */ |
f8f3885c MD |
225 | struct lttng_index_file *lttng_index_file_open(const char *path_name, |
226 | const char *channel_name, uint64_t tracefile_count, | |
227 | uint64_t tracefile_count_current) | |
2f8f53af | 228 | { |
f8f3885c | 229 | struct lttng_index_file *index_file; |
2f8f53af DG |
230 | int ret, read_fd; |
231 | ssize_t read_len; | |
232 | char fullpath[PATH_MAX]; | |
233 | struct ctf_packet_index_file_hdr hdr; | |
f8f3885c | 234 | uint32_t major, minor, element_len; |
2f8f53af DG |
235 | |
236 | assert(path_name); | |
237 | assert(channel_name); | |
238 | ||
f8f3885c MD |
239 | index_file = zmalloc(sizeof(*index_file)); |
240 | if (!index_file) { | |
241 | PERROR("allocating lttng_index_file"); | |
242 | goto error; | |
243 | } | |
244 | ||
2f8f53af DG |
245 | if (tracefile_count > 0) { |
246 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%" | |
247 | PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name, | |
248 | channel_name, tracefile_count_current); | |
249 | } else { | |
250 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s" | |
251 | DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name); | |
252 | } | |
253 | if (ret < 0) { | |
254 | PERROR("snprintf index path"); | |
255 | goto error; | |
256 | } | |
257 | ||
258 | DBG("Index opening file %s in read only", fullpath); | |
259 | read_fd = open(fullpath, O_RDONLY); | |
260 | if (read_fd < 0) { | |
cb945f85 | 261 | PERROR("opening index in read-only"); |
2f8f53af DG |
262 | goto error; |
263 | } | |
264 | ||
265 | read_len = lttng_read(read_fd, &hdr, sizeof(hdr)); | |
266 | if (read_len < 0) { | |
267 | PERROR("Reading index header"); | |
268 | goto error_close; | |
269 | } | |
270 | ||
271 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { | |
272 | ERR("Invalid header magic"); | |
273 | goto error_close; | |
274 | } | |
f8f3885c MD |
275 | major = be32toh(hdr.index_major); |
276 | minor = be32toh(hdr.index_minor); | |
277 | element_len = be32toh(hdr.packet_index_len); | |
278 | ||
279 | if (major != CTF_INDEX_MAJOR) { | |
2f8f53af DG |
280 | ERR("Invalid header version"); |
281 | goto error_close; | |
282 | } | |
a3461e94 MD |
283 | if (element_len > sizeof(struct ctf_packet_index)) { |
284 | ERR("Index element length too long"); | |
285 | goto error_close; | |
286 | } | |
2f8f53af | 287 | |
f8f3885c MD |
288 | index_file->fd = read_fd; |
289 | index_file->major = major; | |
290 | index_file->minor = minor; | |
291 | index_file->element_len = element_len; | |
292 | urcu_ref_init(&index_file->ref); | |
293 | ||
294 | return index_file; | |
2f8f53af DG |
295 | |
296 | error_close: | |
297 | if (read_fd >= 0) { | |
298 | int close_ret; | |
299 | ||
300 | close_ret = close(read_fd); | |
301 | if (close_ret < 0) { | |
302 | PERROR("close read fd %d", read_fd); | |
303 | } | |
304 | } | |
2f8f53af DG |
305 | |
306 | error: | |
f8f3885c MD |
307 | free(index_file); |
308 | return NULL; | |
309 | } | |
310 | ||
311 | void lttng_index_file_get(struct lttng_index_file *index_file) | |
312 | { | |
313 | urcu_ref_get(&index_file->ref); | |
314 | } | |
315 | ||
316 | static void lttng_index_file_release(struct urcu_ref *ref) | |
317 | { | |
318 | struct lttng_index_file *index_file = caa_container_of(ref, | |
319 | struct lttng_index_file, ref); | |
320 | ||
321 | if (close(index_file->fd)) { | |
322 | PERROR("close index fd"); | |
323 | } | |
324 | free(index_file); | |
325 | } | |
326 | ||
327 | void lttng_index_file_put(struct lttng_index_file *index_file) | |
328 | { | |
329 | urcu_ref_put(&index_file->ref, lttng_index_file_release); | |
2f8f53af | 330 | } |