Add the GMT offset in the rotated chunk path
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.c
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 <lttng/rotate-internal.h>
36
37 #include "session.h"
38 #include "rotate.h"
39 #include "rotation-thread.h"
40 #include "lttng-sessiond.h"
41 #include "health-sessiond.h"
42 #include "cmd.h"
43 #include "utils.h"
44
45 #include <urcu.h>
46 #include <urcu/list.h>
47 #include <urcu/rculfhash.h>
48
49 unsigned long hash_channel_key(struct rotation_channel_key *key)
50 {
51 return hash_key_u64(&key->key, lttng_ht_seed) ^ hash_key_ulong(
52 (void *) (unsigned long) key->domain, lttng_ht_seed);
53 }
54
55 int rotate_add_channel_pending(uint64_t key, enum lttng_domain_type domain,
56 struct ltt_session *session)
57 {
58 int ret;
59 struct rotation_channel_info *new_info;
60 struct rotation_channel_key channel_key = { .key = key,
61 .domain = domain };
62
63 new_info = zmalloc(sizeof(struct rotation_channel_info));
64 if (!new_info) {
65 goto error;
66 }
67
68 new_info->channel_key.key = key;
69 new_info->channel_key.domain = domain;
70 new_info->session_id = session->id;
71 cds_lfht_node_init(&new_info->rotate_channels_ht_node);
72
73 session->nr_chan_rotate_pending++;
74 cds_lfht_add(channel_pending_rotate_ht,
75 hash_channel_key(&channel_key),
76 &new_info->rotate_channels_ht_node);
77
78 ret = 0;
79 goto end;
80
81 error:
82 ret = -1;
83 end:
84 return ret;
85 }
86
87 /* The session's lock must be held by the caller. */
88 static
89 int session_rename_chunk(struct ltt_session *session, char *current_path,
90 char *new_path)
91 {
92 int ret;
93 struct consumer_socket *socket;
94 struct consumer_output *output;
95 struct lttng_ht_iter iter;
96 uid_t uid;
97 gid_t gid;
98
99 DBG("Renaming session chunk path of session \"%s\" from %s to %s",
100 session->name, current_path, new_path);
101
102 /*
103 * Either one of the sessions is enough to find the consumer_output
104 * and uid/gid.
105 */
106 if (session->kernel_session) {
107 output = session->kernel_session->consumer;
108 uid = session->kernel_session->uid;
109 gid = session->kernel_session->gid;
110 } else if (session->ust_session) {
111 output = session->ust_session->consumer;
112 uid = session->ust_session->uid;
113 gid = session->ust_session->gid;
114 } else {
115 assert(0);
116 }
117
118 if (!output || !output->socks) {
119 ERR("No consumer output found for session \"%s\"",
120 session->name);
121 ret = -1;
122 goto end;
123 }
124
125 rcu_read_lock();
126 /*
127 * We have to iterate to find a socket, but we only need to send the
128 * rename command to one consumer, so we break after the first one.
129 */
130 cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) {
131 pthread_mutex_lock(socket->lock);
132 ret = consumer_rotate_rename(socket, session->id, output,
133 current_path, new_path, uid, gid);
134 pthread_mutex_unlock(socket->lock);
135 if (ret) {
136 ret = -1;
137 goto end_unlock;
138 }
139 break;
140 }
141
142 ret = 0;
143
144 end_unlock:
145 rcu_read_unlock();
146 end:
147 return ret;
148 }
149
150 /* The session's lock must be held by the caller. */
151 static
152 int rename_first_chunk(struct ltt_session *session,
153 struct consumer_output *consumer, char *new_path)
154 {
155 int ret;
156 char current_full_path[LTTNG_PATH_MAX], new_full_path[LTTNG_PATH_MAX];
157
158 /* Current domain path: <session>/kernel */
159 if (session->net_handle > 0) {
160 ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s",
161 consumer->dst.net.base_dir, consumer->subdir);
162 if (ret < 0 || ret >= sizeof(current_full_path)) {
163 ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"",
164 session->name);
165 ret = -1;
166 goto error;
167 }
168 } else {
169 ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s",
170 consumer->dst.session_root_path, consumer->subdir);
171 if (ret < 0 || ret >= sizeof(current_full_path)) {
172 ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"",
173 session->name);
174 ret = -1;
175 goto error;
176 }
177 }
178 /* New domain path: <session>/<start-date>-<end-date>-<rotate-count>/kernel */
179 ret = snprintf(new_full_path, sizeof(new_full_path), "%s/%s",
180 new_path, consumer->subdir);
181 if (ret < 0 || ret >= sizeof(new_full_path)) {
182 ERR("Failed to initialize new full path while renaming first rotation chunk of session \"%s\"",
183 session->name);
184 ret = -1;
185 goto error;
186 }
187 /*
188 * Move the per-domain fcurrenter inside the first rotation
189 * fcurrenter.
190 */
191 ret = session_rename_chunk(session, current_full_path, new_full_path);
192 if (ret < 0) {
193 ret = -LTTNG_ERR_UNK;
194 goto error;
195 }
196
197 ret = 0;
198
199 error:
200 return ret;
201 }
202
203 /*
204 * Rename a chunk folder after a rotation is complete.
205 * session_lock_list and session lock must be held.
206 *
207 * Returns 0 on success, a negative value on error.
208 */
209 int rename_complete_chunk(struct ltt_session *session, time_t ts)
210 {
211 struct tm *timeinfo;
212 char new_path[LTTNG_PATH_MAX];
213 char datetime[21], start_datetime[21];
214 int ret;
215 size_t strf_ret;
216
217 DBG("Renaming completed chunk for session %s", session->name);
218 timeinfo = localtime(&ts);
219 if (!timeinfo) {
220 ERR("Failed to retrieve local time while renaming completed chunk");
221 ret = -1;
222 goto end;
223 }
224
225 strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%dT%H%M%S%z",
226 timeinfo);
227 if (strf_ret == 0) {
228 ERR("Failed to format timestamp while renaming completed session chunk");
229 ret = -1;
230 goto end;
231 }
232
233 if (session->rotate_count == 1) {
234 char start_time[21];
235
236 timeinfo = localtime(&session->last_chunk_start_ts);
237 if (!timeinfo) {
238 ERR("Failed to retrieve local time while renaming completed chunk");
239 ret = -1;
240 goto end;
241 }
242
243 strf_ret = strftime(start_time, sizeof(start_time),
244 "%Y%m%dT%H%M%S%z", timeinfo);
245 if (strf_ret == 0) {
246 ERR("Failed to format timestamp while renaming completed session chunk");
247 ret = -1;
248 goto end;
249 }
250
251 /*
252 * On the first rotation, the current_rotate_path is the
253 * session_root_path, so we need to create the chunk folder
254 * and move the domain-specific folders inside it.
255 */
256 ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64,
257 session->rotation_chunk.current_rotate_path,
258 start_time,
259 datetime, session->rotate_count);
260 if (ret < 0 || ret >= sizeof(new_path)) {
261 ERR("Failed to format new chunk path while renaming session \"%s\"'s first chunk",
262 session->name);
263 ret = -1;
264 goto end;
265 }
266
267 if (session->kernel_session) {
268 ret = rename_first_chunk(session,
269 session->kernel_session->consumer,
270 new_path);
271 if (ret) {
272 ERR("Failed to rename kernel session trace folder to %s", new_path);
273 /*
274 * This is not a fatal error for the rotation
275 * thread, we just need to inform the client
276 * that a problem occurred with the rotation.
277 * Returning 0, same for the other errors
278 * below.
279 */
280 ret = 0;
281 goto error;
282 }
283 }
284 if (session->ust_session) {
285 ret = rename_first_chunk(session,
286 session->ust_session->consumer,
287 new_path);
288 if (ret) {
289 ERR("Failed to rename userspace session trace folder to %s", new_path);
290 ret = 0;
291 goto error;
292 }
293 }
294 } else {
295 /*
296 * After the first rotation, all the trace data is already in
297 * its own chunk folder, we just need to append the suffix.
298 */
299 /* Recreate the session->rotation_chunk.current_rotate_path */
300 timeinfo = localtime(&session->last_chunk_start_ts);
301 if (!timeinfo) {
302 ERR("Failed to retrieve local time while renaming completed chunk");
303 ret = -1;
304 goto end;
305 }
306 strf_ret = strftime(start_datetime, sizeof(start_datetime),
307 "%Y%m%dT%H%M%S%z", timeinfo);
308 if (!strf_ret) {
309 ERR("Failed to format timestamp while renaming completed session chunk");
310 ret = -1;
311 goto end;
312 }
313 ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64,
314 session_get_base_path(session),
315 start_datetime,
316 datetime, session->rotate_count);
317 if (ret < 0 || ret >= sizeof(new_path)) {
318 ERR("Failed to format new chunk path while renaming chunk of session \"%s\"",
319 session->name);
320 ret = -1;
321 goto error;
322 }
323 ret = session_rename_chunk(session,
324 session->rotation_chunk.current_rotate_path,
325 new_path);
326 if (ret) {
327 ERR("Failed to rename session trace folder from %s to %s",
328 session->rotation_chunk.current_rotate_path,
329 new_path);
330 ret = 0;
331 goto error;
332 }
333 }
334
335 /*
336 * Store the path where the readable chunk is. This path is valid
337 * and can be queried by the client with rotate_pending until the next
338 * rotation is started.
339 */
340 ret = lttng_strncpy(session->rotation_chunk.current_rotate_path,
341 new_path,
342 sizeof(session->rotation_chunk.current_rotate_path));
343 if (ret) {
344 ERR("Failed the current chunk's path of session \"%s\"",
345 session->name);
346 ret = -1;
347 goto error;
348 }
349
350 goto end;
351
352 error:
353 session->rotation_state = LTTNG_ROTATION_STATE_ERROR;
354 end:
355 return ret;
356 }
357
358 int relay_rotate_pending(struct ltt_session *session, uint64_t chunk_id)
359 {
360 int ret;
361 struct consumer_socket *socket;
362 struct consumer_output *output;
363 struct lttng_ht_iter iter;
364
365 /*
366 * Either one of the sessions is enough to find the consumer_output
367 * and uid/gid.
368 */
369 if (session->kernel_session) {
370 output = session->kernel_session->consumer;
371 } else if (session->ust_session) {
372 output = session->ust_session->consumer;
373 } else {
374 assert(0);
375 }
376
377 if (!output || !output->socks) {
378 ERR("No consumer output found");
379 ret = -1;
380 goto end;
381 }
382
383 ret = -1;
384
385 rcu_read_lock();
386 /*
387 * We have to iterate to find a socket, but we only need to send the
388 * rotate pending command to one consumer, so we break after the first
389 * one.
390 */
391 cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket,
392 node.node) {
393 pthread_mutex_lock(socket->lock);
394 ret = consumer_rotate_pending_relay(socket, output, session->id,
395 chunk_id);
396 pthread_mutex_unlock(socket->lock);
397 break;
398 }
399 rcu_read_unlock();
400
401 end:
402 return ret;
403 }
This page took 0.039089 seconds and 6 git commands to generate.