Fix: relayd: handling of lttng_read errors >= 0
[lttng-tools.git] / src / common / index / index.c
CommitLineData
309167d2
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
e0547b83 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 *
e0547b83 36 * Return allocated struct lttng_index_file, NULL on error.
309167d2 37 */
e0547b83
MD
38struct 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{
e0547b83 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];
e0547b83
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);
81 if (ret < 0 && errno != ENOENT) {
82 goto error;
83 }
1c20f0e2 84 ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
309167d2
JD
85 gid, DEFAULT_INDEX_FILE_SUFFIX);
86 if (ret < 0) {
87 goto error;
88 }
89 fd = ret;
90
50adc264 91 hdr.magic = htobe32(CTF_INDEX_MAGIC);
e0547b83
MD
92 hdr.index_major = htobe32(major);
93 hdr.index_minor = htobe32(minor);
94 hdr.packet_index_len = htobe32(element_len);
309167d2 95
6cd525e8
MD
96 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
97 if (size_ret < sizeof(hdr)) {
309167d2 98 PERROR("write index header");
6cd525e8 99 ret = -1;
309167d2
JD
100 goto error;
101 }
e0547b83
MD
102 index_file->fd = fd;
103 index_file->major = major;
104 index_file->minor = minor;
105 index_file->element_len = element_len;
106 urcu_ref_init(&index_file->ref);
309167d2 107
e0547b83 108 return index_file;
309167d2
JD
109
110error:
111 if (fd >= 0) {
112 int close_ret;
113
114 close_ret = close(fd);
115 if (close_ret < 0) {
116 PERROR("close index fd");
117 }
118 }
e0547b83
MD
119 free(index_file);
120 return NULL;
309167d2
JD
121}
122
123/*
e0547b83 124 * Write index values to the given index file.
309167d2 125 *
e0547b83 126 * Return 0 on success, -1 on error.
309167d2 127 */
e0547b83
MD
128int lttng_index_file_write(const struct lttng_index_file *index_file,
129 const struct ctf_packet_index *element)
309167d2 130{
511a182e
JR
131 int fd;
132 size_t len;
6cd525e8 133 ssize_t ret;
309167d2 134
511a182e 135 assert(index_file);
e0547b83 136 assert(element);
309167d2 137
511a182e
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
e0547b83 145 ret = lttng_write(fd, element, len);
6cd525e8 146 if (ret < len) {
309167d2 147 PERROR("writing index file");
e0547b83
MD
148 goto error;
149 }
150 return 0;
151
152error:
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 */
161int 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);
7427779e 175 if (ret < 0) {
e0547b83
MD
176 PERROR("read index file");
177 goto error;
309167d2 178 }
7427779e
MD
179 if (ret < len) {
180 ERR("lttng_read expected %zu, returned %zd", len, ret);
181 goto error;
182 }
e0547b83 183 return 0;
309167d2 184
183f6fa2 185error:
e0547b83 186 return -1;
309167d2 187}
2f8f53af
DG
188
189/*
190 * Open index file using a given path, channel name and tracefile count.
191 *
e0547b83 192 * Return allocated struct lttng_index_file, NULL on error.
2f8f53af 193 */
e0547b83
MD
194struct 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{
e0547b83 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;
e0547b83 203 uint32_t major, minor, element_len;
2f8f53af
DG
204
205 assert(path_name);
206 assert(channel_name);
207
e0547b83
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) {
230 if (errno == ENOENT) {
231 ret = -ENOENT;
232 } else {
233 PERROR("opening index in read-only");
234 }
235 goto error;
236 }
237
238 read_len = lttng_read(read_fd, &hdr, sizeof(hdr));
239 if (read_len < 0) {
240 PERROR("Reading index header");
241 goto error_close;
242 }
243
244 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
245 ERR("Invalid header magic");
246 goto error_close;
247 }
e0547b83
MD
248 major = be32toh(hdr.index_major);
249 minor = be32toh(hdr.index_minor);
250 element_len = be32toh(hdr.packet_index_len);
251
252 if (major != CTF_INDEX_MAJOR) {
2f8f53af
DG
253 ERR("Invalid header version");
254 goto error_close;
255 }
9dae4b67
MD
256 if (element_len > sizeof(struct ctf_packet_index)) {
257 ERR("Index element length too long");
258 goto error_close;
259 }
2f8f53af 260
e0547b83
MD
261 index_file->fd = read_fd;
262 index_file->major = major;
263 index_file->minor = minor;
264 index_file->element_len = element_len;
265 urcu_ref_init(&index_file->ref);
266
267 return index_file;
2f8f53af
DG
268
269error_close:
270 if (read_fd >= 0) {
271 int close_ret;
272
273 close_ret = close(read_fd);
274 if (close_ret < 0) {
275 PERROR("close read fd %d", read_fd);
276 }
277 }
278 ret = -1;
279
280error:
e0547b83
MD
281 free(index_file);
282 return NULL;
283}
284
285void lttng_index_file_get(struct lttng_index_file *index_file)
286{
287 urcu_ref_get(&index_file->ref);
288}
289
290static void lttng_index_file_release(struct urcu_ref *ref)
291{
292 struct lttng_index_file *index_file = caa_container_of(ref,
293 struct lttng_index_file, ref);
294
295 if (close(index_file->fd)) {
296 PERROR("close index fd");
297 }
298 free(index_file);
299}
300
301void lttng_index_file_put(struct lttng_index_file *index_file)
302{
303 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 304}
This page took 0.048133 seconds and 4 git commands to generate.