Commit | Line | Data |
---|---|---|
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 | |
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 | * | |
36 | * Return fd on success, a negative value on error. | |
37 | */ | |
38 | int index_create_file(char *path_name, char *stream_name, int uid, int gid, | |
39 | uint64_t size, uint64_t count) | |
40 | { | |
41 | int ret, fd = -1; | |
6cd525e8 | 42 | ssize_t size_ret; |
50adc264 | 43 | struct ctf_packet_index_file_hdr hdr; |
1c20f0e2 | 44 | char fullpath[PATH_MAX]; |
309167d2 | 45 | |
1c20f0e2 JD |
46 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR, |
47 | path_name); | |
48 | if (ret < 0) { | |
49 | PERROR("snprintf index path"); | |
50 | goto error; | |
51 | } | |
52 | ||
53 | /* Create index directory if necessary. */ | |
7591bab1 | 54 | ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid); |
1c20f0e2 JD |
55 | if (ret < 0) { |
56 | if (ret != -EEXIST) { | |
6347a8b5 | 57 | PERROR("Index trace directory creation error"); |
1c20f0e2 JD |
58 | goto error; |
59 | } | |
60 | } | |
61 | ||
7591bab1 MD |
62 | /* |
63 | * For tracefile rotation. We need to unlink the old | |
64 | * file if present to synchronize with the tail of the | |
65 | * live viewer which could be working on this same file. | |
66 | * By doing so, any reference to the old index file | |
67 | * stays valid even if we re-create a new file with the | |
68 | * same name afterwards. | |
69 | */ | |
70 | ret = utils_unlink_stream_file(fullpath, stream_name, size, count, uid, | |
71 | gid, DEFAULT_INDEX_FILE_SUFFIX); | |
72 | if (ret < 0 && errno != ENOENT) { | |
73 | goto error; | |
74 | } | |
1c20f0e2 | 75 | ret = utils_create_stream_file(fullpath, stream_name, size, count, uid, |
309167d2 JD |
76 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
77 | if (ret < 0) { | |
78 | goto error; | |
79 | } | |
80 | fd = ret; | |
81 | ||
50adc264 JD |
82 | hdr.magic = htobe32(CTF_INDEX_MAGIC); |
83 | hdr.index_major = htobe32(CTF_INDEX_MAJOR); | |
84 | hdr.index_minor = htobe32(CTF_INDEX_MINOR); | |
4bc5e7c5 | 85 | hdr.packet_index_len = htobe32(sizeof(struct ctf_packet_index)); |
309167d2 | 86 | |
6cd525e8 MD |
87 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); |
88 | if (size_ret < sizeof(hdr)) { | |
309167d2 | 89 | PERROR("write index header"); |
6cd525e8 | 90 | ret = -1; |
309167d2 JD |
91 | goto error; |
92 | } | |
93 | ||
94 | return fd; | |
95 | ||
96 | error: | |
97 | if (fd >= 0) { | |
98 | int close_ret; | |
99 | ||
100 | close_ret = close(fd); | |
101 | if (close_ret < 0) { | |
102 | PERROR("close index fd"); | |
103 | } | |
104 | } | |
105 | return ret; | |
106 | } | |
107 | ||
108 | /* | |
109 | * Write index values to the given fd of size len. | |
110 | * | |
6cd525e8 MD |
111 | * Return "len" on success or else < len on error. errno contains error |
112 | * details. | |
309167d2 | 113 | */ |
50adc264 | 114 | ssize_t index_write(int fd, struct ctf_packet_index *index, size_t len) |
309167d2 | 115 | { |
6cd525e8 | 116 | ssize_t ret; |
309167d2 | 117 | |
309167d2 JD |
118 | assert(index); |
119 | ||
183f6fa2 DG |
120 | if (fd < 0) { |
121 | ret = -EINVAL; | |
122 | goto error; | |
123 | } | |
124 | ||
6cd525e8 MD |
125 | ret = lttng_write(fd, index, len); |
126 | if (ret < len) { | |
309167d2 JD |
127 | PERROR("writing index file"); |
128 | } | |
129 | ||
183f6fa2 | 130 | error: |
309167d2 JD |
131 | return ret; |
132 | } | |
2f8f53af DG |
133 | |
134 | /* | |
135 | * Open index file using a given path, channel name and tracefile count. | |
136 | * | |
137 | * Return read only FD on success or else a negative value. | |
138 | */ | |
139 | int index_open(const char *path_name, const char *channel_name, | |
140 | uint64_t tracefile_count, uint64_t tracefile_count_current) | |
141 | { | |
142 | int ret, read_fd; | |
143 | ssize_t read_len; | |
144 | char fullpath[PATH_MAX]; | |
145 | struct ctf_packet_index_file_hdr hdr; | |
146 | ||
147 | assert(path_name); | |
148 | assert(channel_name); | |
149 | ||
150 | if (tracefile_count > 0) { | |
151 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%" | |
152 | PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name, | |
153 | channel_name, tracefile_count_current); | |
154 | } else { | |
155 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s" | |
156 | DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name); | |
157 | } | |
158 | if (ret < 0) { | |
159 | PERROR("snprintf index path"); | |
160 | goto error; | |
161 | } | |
162 | ||
163 | DBG("Index opening file %s in read only", fullpath); | |
164 | read_fd = open(fullpath, O_RDONLY); | |
165 | if (read_fd < 0) { | |
166 | if (errno == ENOENT) { | |
167 | ret = -ENOENT; | |
168 | } else { | |
169 | PERROR("opening index in read-only"); | |
170 | } | |
171 | goto error; | |
172 | } | |
173 | ||
174 | read_len = lttng_read(read_fd, &hdr, sizeof(hdr)); | |
175 | if (read_len < 0) { | |
176 | PERROR("Reading index header"); | |
177 | goto error_close; | |
178 | } | |
179 | ||
180 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { | |
181 | ERR("Invalid header magic"); | |
182 | goto error_close; | |
183 | } | |
184 | if (be32toh(hdr.index_major) != CTF_INDEX_MAJOR || | |
185 | be32toh(hdr.index_minor) != CTF_INDEX_MINOR) { | |
186 | ERR("Invalid header version"); | |
187 | goto error_close; | |
188 | } | |
189 | ||
190 | return read_fd; | |
191 | ||
192 | error_close: | |
193 | if (read_fd >= 0) { | |
194 | int close_ret; | |
195 | ||
196 | close_ret = close(read_fd); | |
197 | if (close_ret < 0) { | |
198 | PERROR("close read fd %d", read_fd); | |
199 | } | |
200 | } | |
201 | ret = -1; | |
202 | ||
203 | error: | |
204 | return ret; | |
205 | } |