Fix: cleanup relayd on any I/O error in read subbuffer mmap
[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>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
1c20f0e2 21#include <sys/stat.h>
2f8f53af
DG
22#include <sys/types.h>
23#include <fcntl.h>
309167d2
JD
24
25#include <common/common.h>
26#include <common/defaults.h>
27#include <common/utils.h>
28
29#include "index.h"
30
31/*
32 * Create the index file associated with a trace file.
33 *
34 * Return fd on success, a negative value on error.
35 */
36int index_create_file(char *path_name, char *stream_name, int uid, int gid,
37 uint64_t size, uint64_t count)
38{
39 int ret, fd = -1;
6cd525e8 40 ssize_t size_ret;
50adc264 41 struct ctf_packet_index_file_hdr hdr;
1c20f0e2 42 char fullpath[PATH_MAX];
309167d2 43
1c20f0e2
JD
44 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR,
45 path_name);
46 if (ret < 0) {
47 PERROR("snprintf index path");
48 goto error;
49 }
50
51 /* Create index directory if necessary. */
52 ret = run_as_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid);
53 if (ret < 0) {
54 if (ret != -EEXIST) {
55 ERR("Index trace directory creation error");
56 goto error;
57 }
58 }
59
60 ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
309167d2
JD
61 gid, DEFAULT_INDEX_FILE_SUFFIX);
62 if (ret < 0) {
63 goto error;
64 }
65 fd = ret;
66
50adc264
JD
67 hdr.magic = htobe32(CTF_INDEX_MAGIC);
68 hdr.index_major = htobe32(CTF_INDEX_MAJOR);
69 hdr.index_minor = htobe32(CTF_INDEX_MINOR);
70 hdr.packet_index_len = sizeof(struct ctf_packet_index);
309167d2 71
6cd525e8
MD
72 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
73 if (size_ret < sizeof(hdr)) {
309167d2 74 PERROR("write index header");
6cd525e8 75 ret = -1;
309167d2
JD
76 goto error;
77 }
78
79 return fd;
80
81error:
82 if (fd >= 0) {
83 int close_ret;
84
85 close_ret = close(fd);
86 if (close_ret < 0) {
87 PERROR("close index fd");
88 }
89 }
90 return ret;
91}
92
93/*
94 * Write index values to the given fd of size len.
95 *
6cd525e8
MD
96 * Return "len" on success or else < len on error. errno contains error
97 * details.
309167d2 98 */
50adc264 99ssize_t index_write(int fd, struct ctf_packet_index *index, size_t len)
309167d2 100{
6cd525e8 101 ssize_t ret;
309167d2
JD
102
103 assert(fd >= 0);
104 assert(index);
105
6cd525e8
MD
106 ret = lttng_write(fd, index, len);
107 if (ret < len) {
309167d2
JD
108 PERROR("writing index file");
109 }
110
111 return ret;
112}
2f8f53af
DG
113
114/*
115 * Open index file using a given path, channel name and tracefile count.
116 *
117 * Return read only FD on success or else a negative value.
118 */
119int index_open(const char *path_name, const char *channel_name,
120 uint64_t tracefile_count, uint64_t tracefile_count_current)
121{
122 int ret, read_fd;
123 ssize_t read_len;
124 char fullpath[PATH_MAX];
125 struct ctf_packet_index_file_hdr hdr;
126
127 assert(path_name);
128 assert(channel_name);
129
130 if (tracefile_count > 0) {
131 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%"
132 PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name,
133 channel_name, tracefile_count_current);
134 } else {
135 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
136 DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name);
137 }
138 if (ret < 0) {
139 PERROR("snprintf index path");
140 goto error;
141 }
142
143 DBG("Index opening file %s in read only", fullpath);
144 read_fd = open(fullpath, O_RDONLY);
145 if (read_fd < 0) {
146 if (errno == ENOENT) {
147 ret = -ENOENT;
148 } else {
149 PERROR("opening index in read-only");
150 }
151 goto error;
152 }
153
154 read_len = lttng_read(read_fd, &hdr, sizeof(hdr));
155 if (read_len < 0) {
156 PERROR("Reading index header");
157 goto error_close;
158 }
159
160 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
161 ERR("Invalid header magic");
162 goto error_close;
163 }
164 if (be32toh(hdr.index_major) != CTF_INDEX_MAJOR ||
165 be32toh(hdr.index_minor) != CTF_INDEX_MINOR) {
166 ERR("Invalid header version");
167 goto error_close;
168 }
169
170 return read_fd;
171
172error_close:
173 if (read_fd >= 0) {
174 int close_ret;
175
176 close_ret = close(read_fd);
177 if (close_ret < 0) {
178 PERROR("close read fd %d", read_fd);
179 }
180 }
181 ret = -1;
182
183error:
184 return ret;
185}
This page took 0.044706 seconds and 4 git commands to generate.