Fix: sessiond: rotation thread: fatal error when not finding a session
[lttng-tools.git] / src / bin / lttng-sessiond / snapshot.cpp
CommitLineData
6dc3064a 1/*
ab5be9fa 2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
6dc3064a 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
6dc3064a 5 *
6dc3064a
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
6dc3064a
DG
9#include <inttypes.h>
10#include <string.h>
11#include <urcu/uatomic.h>
12
13#include <common/defaults.h>
14
15#include "snapshot.h"
53efb85a 16#include "utils.h"
6dc3064a
DG
17
18/*
19 * Return the atomically incremented value of next_output_id.
20 */
21static inline unsigned long get_next_output_id(struct snapshot *snapshot)
22{
23 return uatomic_add_return(&snapshot->next_output_id, 1);
24}
25
26/*
5288612f 27 * Initialized snapshot output with the given values.
6dc3064a
DG
28 *
29 * Return 0 on success or else a negative value.
30 */
b178f53e
JG
31static int output_init(const struct ltt_session *session,
32 uint64_t max_size, const char *name,
5288612f 33 struct lttng_uri *uris, size_t nb_uri,
6dc3064a
DG
34 struct consumer_output *consumer, struct snapshot_output *output,
35 struct snapshot *snapshot)
36{
5288612f 37 int ret = 0, i;
6dc3064a 38
d4b5a90c
DG
39 memset(output, 0, sizeof(struct snapshot_output));
40
d07ceecd
MD
41 /*
42 * max_size of -1ULL means unset. Set to default (unlimited).
43 */
e1986656
DG
44 if (max_size == (uint64_t) -1ULL) {
45 max_size = 0;
46 }
6dc3064a 47 output->max_size = max_size;
e1986656 48
6dc3064a
DG
49 if (snapshot) {
50 output->id = get_next_output_id(snapshot);
51 }
52 lttng_ht_node_init_ulong(&output->node, (unsigned long) output->id);
53
ee91bab2 54 if (name && name[0] != '\0') {
35f3ec96
MD
55 if (lttng_strncpy(output->name, name, sizeof(output->name))) {
56 ret = -LTTNG_ERR_INVALID;
57 goto error;
58 }
6dc3064a
DG
59 } else {
60 /* Set default name. */
61 ret = snprintf(output->name, sizeof(output->name), "%s-%" PRIu32,
62 DEFAULT_SNAPSHOT_NAME, output->id);
63 if (ret < 0) {
64 ret = -ENOMEM;
65 goto error;
66 }
67 }
68
69 if (!consumer) {
70 goto end;
71 }
72
6dc3064a
DG
73 output->consumer = consumer_copy_output(consumer);
74 if (!output->consumer) {
75 ret = -ENOMEM;
76 goto error;
77 }
7d2f7452 78 output->consumer->snapshot = 1;
6dc3064a
DG
79
80 /* No URL given. */
81 if (nb_uri == 0) {
82 ret = 0;
83 goto end;
84 }
85
86 if (uris[0].dtype == LTTNG_DST_PATH) {
366a9222
JD
87 memset(output->consumer->dst.session_root_path, 0,
88 sizeof(output->consumer->dst.session_root_path));
89 if (lttng_strncpy(output->consumer->dst.session_root_path,
35f3ec96 90 uris[0].dst.path,
366a9222 91 sizeof(output->consumer->dst.session_root_path))) {
35f3ec96
MD
92 ret = -LTTNG_ERR_INVALID;
93 goto error;
94 }
6dc3064a
DG
95 output->consumer->type = CONSUMER_DST_LOCAL;
96 ret = 0;
97 goto end;
98 }
99
100 if (nb_uri != 2) {
101 /* Absolutely needs two URIs for network. */
102 ret = -LTTNG_ERR_INVALID;
103 goto error;
104 }
105
106 for (i = 0; i < nb_uri; i ++) {
107 /* Network URIs */
b178f53e
JG
108 ret = consumer_set_network_uri(session, output->consumer,
109 &uris[i]);
6dc3064a
DG
110 if (ret < 0) {
111 goto error;
112 }
113 }
114
115error:
116end:
5288612f
DG
117 return ret;
118}
119
120/*
121 * Initialize a snapshot output object using the given parameters and URI(s).
122 * The name value and uris can be NULL.
123 *
124 * Return 0 on success or else a negative value.
125 */
b178f53e
JG
126int snapshot_output_init_with_uri(const struct ltt_session *session,
127 uint64_t max_size, const char *name,
5288612f
DG
128 struct lttng_uri *uris, size_t nb_uri,
129 struct consumer_output *consumer, struct snapshot_output *output,
130 struct snapshot *snapshot)
131{
b178f53e
JG
132 return output_init(session, max_size, name, uris, nb_uri, consumer,
133 output, snapshot);
5288612f
DG
134}
135
136/*
137 * Initialize a snapshot output object using the given parameters. The name
138 * value and url can be NULL.
139 *
140 * Return 0 on success or else a negative value.
141 */
b178f53e
JG
142int snapshot_output_init(const struct ltt_session *session,
143 uint64_t max_size, const char *name,
5288612f
DG
144 const char *ctrl_url, const char *data_url,
145 struct consumer_output *consumer, struct snapshot_output *output,
146 struct snapshot *snapshot)
147{
148 int ret = 0, nb_uri;
149 struct lttng_uri *uris = NULL;
150
151 /* Create an array of URIs from URLs. */
152 nb_uri = uri_parse_str_urls(ctrl_url, data_url, &uris);
153 if (nb_uri < 0) {
154 ret = nb_uri;
155 goto error;
156 }
157
b178f53e
JG
158 ret = output_init(session, max_size, name, uris, nb_uri, consumer,
159 output, snapshot);
5288612f
DG
160
161error:
6dc3064a
DG
162 free(uris);
163 return ret;
164}
165
166struct snapshot_output *snapshot_output_alloc(void)
167{
7966af57 168 return (snapshot_output *) zmalloc(sizeof(struct snapshot_output));
6dc3064a
DG
169}
170
171/*
172 * Delete output from the snapshot object.
173 */
174void snapshot_delete_output(struct snapshot *snapshot,
175 struct snapshot_output *output)
176{
177 int ret;
178 struct lttng_ht_iter iter;
179
a0377dfe
FD
180 LTTNG_ASSERT(snapshot);
181 LTTNG_ASSERT(snapshot->output_ht);
182 LTTNG_ASSERT(output);
6dc3064a
DG
183
184 iter.iter.node = &output->node.node;
185 rcu_read_lock();
186 ret = lttng_ht_del(snapshot->output_ht, &iter);
187 rcu_read_unlock();
a0377dfe 188 LTTNG_ASSERT(!ret);
6dc3064a
DG
189 /*
190 * This is safe because the ownership of a snapshot object is in a session
191 * for which the session lock need to be acquired to read and modify it.
192 */
193 snapshot->nb_output--;
194}
195
196/*
197 * Add output object to the snapshot.
198 */
199void snapshot_add_output(struct snapshot *snapshot,
200 struct snapshot_output *output)
201{
a0377dfe
FD
202 LTTNG_ASSERT(snapshot);
203 LTTNG_ASSERT(snapshot->output_ht);
204 LTTNG_ASSERT(output);
6dc3064a
DG
205
206 rcu_read_lock();
207 lttng_ht_add_unique_ulong(snapshot->output_ht, &output->node);
208 rcu_read_unlock();
209 /*
210 * This is safe because the ownership of a snapshot object is in a session
211 * for which the session lock need to be acquired to read and modify it.
212 */
213 snapshot->nb_output++;
214}
215
216/*
217 * Destroy and free a snapshot output object.
218 */
219void snapshot_output_destroy(struct snapshot_output *obj)
220{
a0377dfe 221 LTTNG_ASSERT(obj);
6dc3064a
DG
222
223 if (obj->consumer) {
224 consumer_output_send_destroy_relayd(obj->consumer);
6addfa37 225 consumer_output_put(obj->consumer);
6dc3064a
DG
226 }
227 free(obj);
228}
229
eb240553
DG
230/*
231 * RCU read side lock MUST be acquired before calling this since the returned
232 * pointer is in a RCU hash table.
233 *
234 * Return the reference on success or else NULL.
235 */
236struct snapshot_output *snapshot_find_output_by_name(const char *name,
237 struct snapshot *snapshot)
238{
239 struct lttng_ht_iter iter;
240 struct snapshot_output *output = NULL;
241
a0377dfe
FD
242 LTTNG_ASSERT(snapshot);
243 LTTNG_ASSERT(name);
eb240553
DG
244
245 cds_lfht_for_each_entry(snapshot->output_ht->ht, &iter.iter, output,
246 node.node) {
247 if (!strncmp(output->name, name, strlen(name))) {
248 return output;
249 }
250 }
251
252 /* Not found */
253 return NULL;
254}
255
6dc3064a
DG
256/*
257 * RCU read side lock MUST be acquired before calling this since the returned
258 * pointer is in a RCU hash table.
259 *
260 * Return the reference on success or else NULL.
261 */
262struct snapshot_output *snapshot_find_output_by_id(uint32_t id,
263 struct snapshot *snapshot)
264{
265 struct lttng_ht_node_ulong *node;
266 struct lttng_ht_iter iter;
267 struct snapshot_output *output = NULL;
268
a0377dfe 269 LTTNG_ASSERT(snapshot);
6dc3064a
DG
270
271 lttng_ht_lookup(snapshot->output_ht, (void *)((unsigned long) id), &iter);
272 node = lttng_ht_iter_get_node_ulong(&iter);
273 if (!node) {
274 DBG3("Snapshot output not found with id %" PRId32, id);
275 goto error;
276 }
277 output = caa_container_of(node, struct snapshot_output, node);
278
279error:
280 return output;
281}
282
6dc3064a
DG
283/*
284 * Initialized a snapshot object that was already allocated.
285 *
286 * Return 0 on success or else a negative errno value.
287 */
288int snapshot_init(struct snapshot *obj)
289{
290 int ret;
291
a0377dfe 292 LTTNG_ASSERT(obj);
6dc3064a
DG
293
294 memset(obj, 0, sizeof(struct snapshot));
295
296 obj->output_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
297 if (!obj->output_ht) {
298 ret = -ENOMEM;
299 goto error;
300 }
301
302 ret = 0;
303
304error:
305 return ret;
306}
307
308/*
309 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
310 * static reference.
311 */
312void snapshot_destroy(struct snapshot *obj)
313{
314 struct lttng_ht_iter iter;
315 struct snapshot_output *output;
316
f4cc5e83
JG
317 if (!obj->output_ht) {
318 return;
319 }
6dc3064a
DG
320
321 rcu_read_lock();
322 cds_lfht_for_each_entry(obj->output_ht->ht, &iter.iter, output,
323 node.node) {
324 snapshot_delete_output(obj, output);
325 snapshot_output_destroy(output);
326 }
327 rcu_read_unlock();
53efb85a 328 ht_cleanup_push(obj->output_ht);
6dc3064a 329}
This page took 0.072091 seconds and 4 git commands to generate.