b5591d137c923a97617005f9a85145e09264cba4
[lttng-tools.git] / src / common / index / index.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <fcntl.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/utils.h>
30
31 #include "index.h"
32
33 /*
34 * Create the index file associated with a trace file.
35 *
36 * Return allocated struct lttng_index_file, NULL on error.
37 */
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)
41 {
42 struct lttng_index_file *index_file;
43 int ret, fd = -1;
44 ssize_t size_ret;
45 struct ctf_packet_index_file_hdr hdr;
46 char fullpath[PATH_MAX];
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 }
54
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. */
63 ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid);
64 if (ret < 0) {
65 if (errno != EEXIST) {
66 PERROR("Index trace directory creation error");
67 goto error;
68 }
69 }
70
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 }
84 ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
85 gid, DEFAULT_INDEX_FILE_SUFFIX);
86 if (ret < 0) {
87 goto error;
88 }
89 fd = ret;
90
91 hdr.magic = htobe32(CTF_INDEX_MAGIC);
92 hdr.index_major = htobe32(major);
93 hdr.index_minor = htobe32(minor);
94 hdr.packet_index_len = htobe32(element_len);
95
96 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
97 if (size_ret < sizeof(hdr)) {
98 PERROR("write index header");
99 ret = -1;
100 goto error;
101 }
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);
107
108 return index_file;
109
110 error:
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 }
119 free(index_file);
120 return NULL;
121 }
122
123 /*
124 * Write index values to the given index file.
125 *
126 * Return 0 on success, -1 on error.
127 */
128 int lttng_index_file_write(const struct lttng_index_file *index_file,
129 const struct ctf_packet_index *element)
130 {
131 ssize_t ret;
132 int fd = index_file->fd;
133 size_t len = index_file->element_len;
134
135 assert(element);
136
137 if (fd < 0) {
138 goto error;
139 }
140
141 ret = lttng_write(fd, element, len);
142 if (ret < len) {
143 PERROR("writing index file");
144 goto error;
145 }
146 return 0;
147
148 error:
149 return -1;
150 }
151
152 /*
153 * Read index values from the given index file.
154 *
155 * Return 0 on success, -1 on error.
156 */
157 int lttng_index_file_read(const struct lttng_index_file *index_file,
158 struct ctf_packet_index *element)
159 {
160 ssize_t ret;
161 int fd = index_file->fd;
162 size_t len = index_file->element_len;
163
164 assert(element);
165
166 if (fd < 0) {
167 goto error;
168 }
169
170 ret = lttng_read(fd, element, len);
171 if (ret < len) {
172 PERROR("read index file");
173 goto error;
174 }
175 return 0;
176
177 error:
178 return -1;
179 }
180
181 /*
182 * Open index file using a given path, channel name and tracefile count.
183 *
184 * Return allocated struct lttng_index_file, NULL on error.
185 */
186 struct lttng_index_file *lttng_index_file_open(const char *path_name,
187 const char *channel_name, uint64_t tracefile_count,
188 uint64_t tracefile_count_current)
189 {
190 struct lttng_index_file *index_file;
191 int ret, read_fd;
192 ssize_t read_len;
193 char fullpath[PATH_MAX];
194 struct ctf_packet_index_file_hdr hdr;
195 uint32_t major, minor, element_len;
196
197 assert(path_name);
198 assert(channel_name);
199
200 index_file = zmalloc(sizeof(*index_file));
201 if (!index_file) {
202 PERROR("allocating lttng_index_file");
203 goto error;
204 }
205
206 if (tracefile_count > 0) {
207 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%"
208 PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name,
209 channel_name, tracefile_count_current);
210 } else {
211 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
212 DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name);
213 }
214 if (ret < 0) {
215 PERROR("snprintf index path");
216 goto error;
217 }
218
219 DBG("Index opening file %s in read only", fullpath);
220 read_fd = open(fullpath, O_RDONLY);
221 if (read_fd < 0) {
222 if (errno == ENOENT) {
223 ret = -ENOENT;
224 } else {
225 PERROR("opening index in read-only");
226 }
227 goto error;
228 }
229
230 read_len = lttng_read(read_fd, &hdr, sizeof(hdr));
231 if (read_len < 0) {
232 PERROR("Reading index header");
233 goto error_close;
234 }
235
236 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
237 ERR("Invalid header magic");
238 goto error_close;
239 }
240 major = be32toh(hdr.index_major);
241 minor = be32toh(hdr.index_minor);
242 element_len = be32toh(hdr.packet_index_len);
243
244 if (major != CTF_INDEX_MAJOR) {
245 ERR("Invalid header version");
246 goto error_close;
247 }
248
249 index_file->fd = read_fd;
250 index_file->major = major;
251 index_file->minor = minor;
252 index_file->element_len = element_len;
253 urcu_ref_init(&index_file->ref);
254
255 return index_file;
256
257 error_close:
258 if (read_fd >= 0) {
259 int close_ret;
260
261 close_ret = close(read_fd);
262 if (close_ret < 0) {
263 PERROR("close read fd %d", read_fd);
264 }
265 }
266 ret = -1;
267
268 error:
269 free(index_file);
270 return NULL;
271 }
272
273 void lttng_index_file_get(struct lttng_index_file *index_file)
274 {
275 urcu_ref_get(&index_file->ref);
276 }
277
278 static void lttng_index_file_release(struct urcu_ref *ref)
279 {
280 struct lttng_index_file *index_file = caa_container_of(ref,
281 struct lttng_index_file, ref);
282
283 if (close(index_file->fd)) {
284 PERROR("close index fd");
285 }
286 free(index_file);
287 }
288
289 void lttng_index_file_put(struct lttng_index_file *index_file)
290 {
291 urcu_ref_put(&index_file->ref, lttng_index_file_release);
292 }
This page took 0.036803 seconds and 3 git commands to generate.