2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
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.
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
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.
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>
37 #include "rotation-thread.h"
38 #include "lttng-sessiond.h"
39 #include "health-sessiond.h"
43 #include <urcu/list.h>
44 #include <urcu/rculfhash.h>
46 unsigned long hash_channel_key(struct rotation_channel_key
*key
)
48 return hash_key_u64(&key
->key
, lttng_ht_seed
) ^ hash_key_ulong(
49 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
52 int rotate_add_channel_pending(uint64_t key
, enum lttng_domain_type domain
,
53 struct ltt_session
*session
)
56 struct rotation_channel_info
*new_info
;
57 struct rotation_channel_key channel_key
= { .key
= key
,
60 new_info
= zmalloc(sizeof(struct rotation_channel_info
));
65 new_info
->channel_key
.key
= key
;
66 new_info
->channel_key
.domain
= domain
;
67 new_info
->session
= session
;
68 cds_lfht_node_init(&new_info
->rotate_channels_ht_node
);
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
);
84 int session_rename_chunk(struct ltt_session
*session
, char *current_path
,
85 char *new_path
, uint32_t create
)
88 struct consumer_socket
*socket
;
89 struct consumer_output
*output
;
90 struct lttng_ht_iter iter
;
95 * Either one of the sessions is enough to find the consumer_output
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
;
110 if (!output
|| !output
->socks
) {
111 ERR("No consumer output found");
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.
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
,
124 current_path
, new_path
, create
, uid
, gid
);
125 pthread_mutex_unlock(socket
->lock
);
127 ERR("Consumer rename chunk");
142 int rename_complete_chunk(struct ltt_session
*session
, time_t ts
)
146 char *tmppath
= NULL
;
149 timeinfo
= localtime(&ts
);
150 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
152 tmppath
= zmalloc(PATH_MAX
* sizeof(char));
154 ERR("Alloc tmppath");
159 snprintf(tmppath
, PATH_MAX
, "%s%s-%" PRIu64
,
160 session
->rotation_chunk
.current_rotate_path
,
161 datetime
, session
->rotate_count
);
163 fprintf(stderr
, "rename %s to %s\n", session
->rotation_chunk
.current_rotate_path
,
166 ret
= session_rename_chunk(session
,
167 session
->rotation_chunk
.current_rotate_path
,
170 ERR("Session rename");
176 * Store the path where the readable chunk is. This path is valid
177 * and can be queried by the client with rotate_pending until the next
178 * rotation is started.
180 snprintf(session
->rotation_chunk
.current_rotate_path
, PATH_MAX
,
182 session
->rotate_pending
= 0;