Commit | Line | Data |
---|---|---|
e8ed1ed1 JD |
1 | /* |
2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _LGPL_SOURCE | |
19 | #include <lttng/trigger/trigger.h> | |
20 | #include <common/error.h> | |
21 | #include <common/config/session-config.h> | |
22 | #include <common/defaults.h> | |
23 | #include <common/utils.h> | |
24 | #include <common/futex.h> | |
25 | #include <common/align.h> | |
26 | #include <common/time.h> | |
27 | #include <common/hashtable/utils.h> | |
28 | #include <common/kernel-ctl/kernel-ctl.h> | |
29 | #include <sys/eventfd.h> | |
30 | #include <sys/stat.h> | |
31 | #include <time.h> | |
32 | #include <signal.h> | |
33 | #include <inttypes.h> | |
34 | ||
35 | #include "session.h" | |
36 | #include "rotate.h" | |
37 | #include "rotation-thread.h" | |
38 | #include "lttng-sessiond.h" | |
39 | #include "health-sessiond.h" | |
40 | #include "cmd.h" | |
41 | ||
42 | #include <urcu.h> | |
43 | #include <urcu/list.h> | |
44 | #include <urcu/rculfhash.h> | |
45 | ||
46 | unsigned long hash_channel_key(struct rotation_channel_key *key) | |
47 | { | |
48 | return hash_key_u64(&key->key, lttng_ht_seed) ^ hash_key_ulong( | |
49 | (void *) (unsigned long) key->domain, lttng_ht_seed); | |
50 | } | |
51 | ||
52 | int rotate_add_channel_pending(uint64_t key, enum lttng_domain_type domain, | |
53 | struct ltt_session *session) | |
54 | { | |
55 | int ret; | |
56 | struct rotation_channel_info *new_info; | |
57 | struct rotation_channel_key channel_key = { .key = key, | |
58 | .domain = domain }; | |
59 | ||
60 | new_info = zmalloc(sizeof(struct rotation_channel_info)); | |
61 | if (!new_info) { | |
62 | goto error; | |
63 | } | |
64 | ||
65 | new_info->channel_key.key = key; | |
66 | new_info->channel_key.domain = domain; | |
69cda452 | 67 | new_info->session_id = session->id; |
e8ed1ed1 JD |
68 | cds_lfht_node_init(&new_info->rotate_channels_ht_node); |
69 | ||
70 | session->nr_chan_rotate_pending++; | |
71 | cds_lfht_add(channel_pending_rotate_ht, | |
72 | hash_channel_key(&channel_key), | |
73 | &new_info->rotate_channels_ht_node); | |
74 | ||
75 | ret = 0; | |
76 | goto end; | |
77 | ||
78 | error: | |
79 | ret = -1; | |
80 | end: | |
81 | return ret; | |
82 | } | |
83 | ||
84 | int session_rename_chunk(struct ltt_session *session, char *current_path, | |
0ee5eef6 | 85 | char *new_path) |
e8ed1ed1 JD |
86 | { |
87 | int ret; | |
88 | struct consumer_socket *socket; | |
89 | struct consumer_output *output; | |
90 | struct lttng_ht_iter iter; | |
91 | uid_t uid; | |
92 | gid_t gid; | |
93 | ||
94 | /* | |
95 | * Either one of the sessions is enough to find the consumer_output | |
96 | * and uid/gid. | |
97 | */ | |
98 | if (session->kernel_session) { | |
99 | output = session->kernel_session->consumer; | |
100 | uid = session->kernel_session->uid; | |
101 | gid = session->kernel_session->gid; | |
102 | } else if (session->ust_session) { | |
103 | output = session->ust_session->consumer; | |
104 | uid = session->ust_session->uid; | |
105 | gid = session->ust_session->gid; | |
106 | } else { | |
107 | assert(0); | |
108 | } | |
109 | ||
110 | if (!output || !output->socks) { | |
111 | ERR("No consumer output found"); | |
112 | ret = -1; | |
113 | goto end; | |
114 | } | |
115 | ||
116 | rcu_read_lock(); | |
117 | /* | |
118 | * We have to iterate to find a socket, but we only need to send the | |
119 | * rename command to one consumer, so we break after the first one. | |
120 | */ | |
121 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) { | |
122 | pthread_mutex_lock(socket->lock); | |
123 | ret = consumer_rotate_rename(socket, session->id, output, | |
0ee5eef6 | 124 | current_path, new_path, uid, gid); |
e8ed1ed1 JD |
125 | pthread_mutex_unlock(socket->lock); |
126 | if (ret) { | |
127 | ERR("Consumer rename chunk"); | |
128 | ret = -1; | |
129 | rcu_read_unlock(); | |
130 | goto end; | |
131 | } | |
132 | break; | |
133 | } | |
134 | rcu_read_unlock(); | |
135 | ||
136 | ret = 0; | |
137 | ||
138 | end: | |
139 | return ret; | |
140 | } | |
141 | ||
103373d7 JD |
142 | static |
143 | int rename_first_chunk(struct ltt_session *session, | |
144 | struct consumer_output *consumer, char *new_path) | |
145 | { | |
146 | int ret; | |
147 | char *tmppath = NULL, *tmppath2 = NULL; | |
148 | ||
149 | tmppath = zmalloc(PATH_MAX * sizeof(char)); | |
150 | if (!tmppath) { | |
151 | ret = -LTTNG_ERR_NOMEM; | |
152 | goto error; | |
153 | } | |
154 | tmppath2 = zmalloc(PATH_MAX * sizeof(char)); | |
155 | if (!tmppath2) { | |
156 | ret = -LTTNG_ERR_NOMEM; | |
157 | goto error; | |
158 | } | |
159 | ||
160 | /* Current domain path: <session>/kernel */ | |
0ee5eef6 JD |
161 | if (session->net_handle > 0) { |
162 | snprintf(tmppath, PATH_MAX, "%s/%s", | |
163 | consumer->dst.net.base_dir, consumer->subdir); | |
164 | } else { | |
165 | snprintf(tmppath, PATH_MAX, "%s/%s", | |
166 | consumer->dst.session_root_path, consumer->subdir); | |
167 | } | |
103373d7 JD |
168 | /* New domain path: <session>/<start-date>-<end-date>-<rotate-count>/kernel */ |
169 | snprintf(tmppath2, PATH_MAX, "%s/%s", | |
170 | new_path, consumer->subdir); | |
171 | /* | |
172 | * Move the per-domain folder inside the first rotation | |
173 | * folder. | |
174 | */ | |
0ee5eef6 | 175 | ret = session_rename_chunk(session, tmppath, tmppath2); |
103373d7 | 176 | if (ret < 0) { |
4a478f45 | 177 | ERR("Rename first trace directory"); |
103373d7 JD |
178 | ret = -LTTNG_ERR_ROTATE_NO_DATA; |
179 | goto error; | |
180 | } | |
181 | ||
182 | ret = 0; | |
183 | ||
184 | error: | |
185 | free(tmppath); | |
186 | free(tmppath2); | |
187 | ||
188 | return ret; | |
189 | } | |
190 | ||
e8ed1ed1 JD |
191 | int rename_complete_chunk(struct ltt_session *session, time_t ts) |
192 | { | |
193 | struct tm *timeinfo; | |
194 | char datetime[16]; | |
103373d7 | 195 | char *new_path = NULL; |
e8ed1ed1 JD |
196 | int ret; |
197 | ||
198 | timeinfo = localtime(&ts); | |
199 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
200 | ||
103373d7 JD |
201 | new_path = zmalloc(PATH_MAX * sizeof(char)); |
202 | if (!new_path) { | |
4a478f45 | 203 | session->rotate_status = LTTNG_ROTATE_ERROR; |
103373d7 | 204 | ERR("Alloc new_path"); |
e8ed1ed1 JD |
205 | ret = -1; |
206 | goto end; | |
207 | } | |
208 | ||
103373d7 JD |
209 | if (session->rotate_count == 1) { |
210 | char start_time[16]; | |
e8ed1ed1 | 211 | |
103373d7 JD |
212 | timeinfo = localtime(&session->last_chunk_start_ts); |
213 | strftime(start_time, sizeof(start_time), "%Y%m%d-%H%M%S", timeinfo); | |
214 | ||
215 | /* | |
216 | * On the first rotation, the current_rotate_path is the | |
217 | * session_root_path, so we need to create the chunk folder | |
218 | * and move the domain-specific folders inside it. | |
219 | */ | |
220 | snprintf(new_path, PATH_MAX, "%s/%s-%s-%" PRIu64, | |
221 | session->rotation_chunk.current_rotate_path, | |
222 | start_time, | |
223 | datetime, session->rotate_count); | |
224 | ||
225 | if (session->kernel_session) { | |
103373d7 JD |
226 | ret = rename_first_chunk(session, |
227 | session->kernel_session->consumer, | |
228 | new_path); | |
229 | if (ret) { | |
230 | ERR("Rename kernel session"); | |
4a478f45 JD |
231 | /* |
232 | * This is not a fatal error for the rotation | |
233 | * thread, we just need to inform the client | |
234 | * that a problem occurred with the rotation. | |
235 | * Returning 0, same for the other errors | |
236 | * below. | |
237 | */ | |
238 | ret = 0; | |
239 | goto error; | |
103373d7 JD |
240 | } |
241 | } | |
242 | if (session->ust_session) { | |
103373d7 JD |
243 | ret = rename_first_chunk(session, |
244 | session->ust_session->consumer, | |
245 | new_path); | |
246 | if (ret) { | |
247 | ERR("Rename ust session"); | |
4a478f45 JD |
248 | ret = 0; |
249 | goto error; | |
103373d7 JD |
250 | } |
251 | } | |
252 | } else { | |
253 | /* | |
254 | * After the first rotation, all the trace data is already in | |
255 | * its own chunk folder, we just need to append the suffix. | |
256 | */ | |
257 | snprintf(new_path, PATH_MAX, "%s%s-%" PRIu64, | |
258 | session->rotation_chunk.current_rotate_path, | |
259 | datetime, session->rotate_count); | |
103373d7 JD |
260 | ret = session_rename_chunk(session, |
261 | session->rotation_chunk.current_rotate_path, | |
0ee5eef6 | 262 | new_path); |
103373d7 JD |
263 | if (ret) { |
264 | ERR("Session rename"); | |
4a478f45 JD |
265 | ret = 0; |
266 | goto error; | |
103373d7 | 267 | } |
e8ed1ed1 JD |
268 | } |
269 | ||
270 | /* | |
271 | * Store the path where the readable chunk is. This path is valid | |
272 | * and can be queried by the client with rotate_pending until the next | |
273 | * rotation is started. | |
274 | */ | |
275 | snprintf(session->rotation_chunk.current_rotate_path, PATH_MAX, | |
103373d7 | 276 | "%s", new_path); |
e8ed1ed1 | 277 | |
4a478f45 JD |
278 | goto end; |
279 | ||
280 | error: | |
281 | session->rotate_status = LTTNG_ROTATE_ERROR; | |
e8ed1ed1 | 282 | end: |
103373d7 | 283 | free(new_path); |
e8ed1ed1 JD |
284 | return ret; |
285 | } | |
286 | ||
735c026f | 287 | int relay_rotate_pending(struct ltt_session *session, uint64_t chunk_id) |
d3dedf27 JD |
288 | { |
289 | int ret; | |
290 | struct consumer_socket *socket; | |
291 | struct consumer_output *output; | |
292 | struct lttng_ht_iter iter; | |
293 | ||
294 | /* | |
295 | * Either one of the sessions is enough to find the consumer_output | |
296 | * and uid/gid. | |
297 | */ | |
298 | if (session->kernel_session) { | |
299 | output = session->kernel_session->consumer; | |
300 | } else if (session->ust_session) { | |
301 | output = session->ust_session->consumer; | |
302 | } else { | |
303 | assert(0); | |
304 | } | |
305 | ||
306 | if (!output || !output->socks) { | |
307 | ERR("No consumer output found"); | |
308 | ret = -1; | |
309 | goto end; | |
310 | } | |
311 | ||
735c026f JD |
312 | ret = -1; |
313 | ||
d3dedf27 JD |
314 | rcu_read_lock(); |
315 | /* | |
316 | * We have to iterate to find a socket, but we only need to send the | |
317 | * rotate pending command to one consumer, so we break after the first | |
318 | * one. | |
319 | */ | |
320 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) { | |
321 | pthread_mutex_lock(socket->lock); | |
d3dedf27 | 322 | ret = consumer_rotate_pending_relay(socket, output, session->id, |
735c026f | 323 | chunk_id); |
d3dedf27 | 324 | pthread_mutex_unlock(socket->lock); |
d3dedf27 JD |
325 | break; |
326 | } | |
327 | rcu_read_unlock(); | |
328 | ||
d3dedf27 JD |
329 | end: |
330 | return ret; | |
331 | } |