Commit | Line | Data |
---|---|---|
d0b96690 DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@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 | #define _GNU_SOURCE | |
6c1c0768 | 18 | #define _LGPL_SOURCE |
d0b96690 | 19 | #include <assert.h> |
7972aab2 | 20 | #include <inttypes.h> |
d0b96690 DG |
21 | |
22 | #include <common/common.h> | |
7972aab2 DG |
23 | #include <common/hashtable/utils.h> |
24 | #include <lttng/lttng.h> | |
25 | ||
d0b96690 | 26 | #include "ust-registry.h" |
8494bda5 | 27 | #include "ust-app.h" |
0b2dc8df | 28 | #include "utils.h" |
d0b96690 DG |
29 | |
30 | /* | |
31 | * Hash table match function for event in the registry. | |
32 | */ | |
33 | static int ht_match_event(struct cds_lfht_node *node, const void *_key) | |
34 | { | |
35 | struct ust_registry_event *event; | |
36 | const struct ust_registry_event *key; | |
37 | ||
38 | assert(node); | |
39 | assert(_key); | |
40 | ||
41 | event = caa_container_of(node, struct ust_registry_event, node.node); | |
42 | assert(event); | |
43 | key = _key; | |
44 | ||
45 | /* It has to be a perfect match. */ | |
46 | if (strncmp(event->name, key->name, sizeof(event->name)) != 0) { | |
47 | goto no_match; | |
48 | } | |
49 | ||
50 | /* It has to be a perfect match. */ | |
51 | if (strncmp(event->signature, key->signature, | |
52 | strlen(event->signature) != 0)) { | |
53 | goto no_match; | |
54 | } | |
55 | ||
56 | /* Match */ | |
57 | return 1; | |
58 | ||
59 | no_match: | |
60 | return 0; | |
61 | } | |
62 | ||
7972aab2 DG |
63 | static unsigned long ht_hash_event(void *_key, unsigned long seed) |
64 | { | |
65 | uint64_t xored_key; | |
66 | struct ust_registry_event *key = _key; | |
67 | ||
68 | assert(key); | |
69 | ||
70 | xored_key = (uint64_t) (hash_key_str(key->name, seed) ^ | |
71 | hash_key_str(key->signature, seed)); | |
72 | ||
73 | return hash_key_u64(&xored_key, seed); | |
74 | } | |
75 | ||
8494bda5 MD |
76 | /* |
77 | * Return negative value on error, 0 if OK. | |
78 | * | |
79 | * TODO: we could add stricter verification of more types to catch | |
80 | * errors in liblttng-ust implementation earlier than consumption by the | |
81 | * trace reader. | |
82 | */ | |
83 | static | |
84 | int validate_event_field(struct ustctl_field *field, | |
85 | const char *event_name, | |
86 | struct ust_app *app) | |
87 | { | |
88 | switch(field->type.atype) { | |
89 | case ustctl_atype_integer: | |
90 | case ustctl_atype_enum: | |
91 | case ustctl_atype_array: | |
92 | case ustctl_atype_sequence: | |
93 | case ustctl_atype_string: | |
94 | break; | |
95 | ||
96 | case ustctl_atype_float: | |
97 | switch (field->type.u.basic._float.mant_dig) { | |
98 | case 0: | |
99 | WARN("UST application '%s' (pid: %d) has unknown float mantissa '%u' " | |
100 | "in field '%s', rejecting event '%s'", | |
101 | app->name, app->pid, | |
102 | field->type.u.basic._float.mant_dig, | |
103 | field->name, | |
104 | event_name); | |
105 | return -EINVAL; | |
106 | default: | |
107 | break; | |
108 | } | |
109 | break; | |
110 | ||
111 | default: | |
112 | return -ENOENT; | |
113 | } | |
114 | return 0; | |
115 | } | |
116 | ||
117 | static | |
118 | int validate_event_fields(size_t nr_fields, struct ustctl_field *fields, | |
119 | const char *event_name, struct ust_app *app) | |
120 | { | |
121 | unsigned int i; | |
122 | ||
123 | for (i = 0; i < nr_fields; i++) { | |
124 | if (validate_event_field(&fields[i], event_name, app) < 0) | |
125 | return -EINVAL; | |
126 | } | |
127 | return 0; | |
128 | } | |
129 | ||
d0b96690 DG |
130 | /* |
131 | * Allocate event and initialize it. This does NOT set a valid event id from a | |
132 | * registry. | |
133 | */ | |
134 | static struct ust_registry_event *alloc_event(int session_objd, | |
135 | int channel_objd, char *name, char *sig, size_t nr_fields, | |
8494bda5 MD |
136 | struct ustctl_field *fields, int loglevel, char *model_emf_uri, |
137 | struct ust_app *app) | |
d0b96690 DG |
138 | { |
139 | struct ust_registry_event *event = NULL; | |
140 | ||
8494bda5 MD |
141 | /* |
142 | * Ensure that the field content is valid. | |
143 | */ | |
144 | if (validate_event_fields(nr_fields, fields, name, app) < 0) { | |
145 | return NULL; | |
146 | } | |
147 | ||
d0b96690 DG |
148 | event = zmalloc(sizeof(*event)); |
149 | if (!event) { | |
150 | PERROR("zmalloc ust registry event"); | |
151 | goto error; | |
152 | } | |
153 | ||
154 | event->session_objd = session_objd; | |
155 | event->channel_objd = channel_objd; | |
156 | /* Allocated by ustctl. */ | |
157 | event->signature = sig; | |
158 | event->nr_fields = nr_fields; | |
159 | event->fields = fields; | |
160 | event->loglevel = loglevel; | |
161 | event->model_emf_uri = model_emf_uri; | |
162 | if (name) { | |
163 | /* Copy event name and force NULL byte. */ | |
164 | strncpy(event->name, name, sizeof(event->name)); | |
165 | event->name[sizeof(event->name) - 1] = '\0'; | |
166 | } | |
7972aab2 | 167 | cds_lfht_node_init(&event->node.node); |
d0b96690 DG |
168 | |
169 | error: | |
170 | return event; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Free event data structure. This does NOT delete it from any hash table. It's | |
175 | * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the | |
176 | * event is previously deleted from a rcu hash table. | |
177 | */ | |
178 | static void destroy_event(struct ust_registry_event *event) | |
179 | { | |
180 | if (!event) { | |
181 | return; | |
182 | } | |
183 | ||
184 | free(event->fields); | |
185 | free(event->model_emf_uri); | |
186 | free(event->signature); | |
187 | free(event); | |
188 | } | |
189 | ||
190 | /* | |
191 | * Destroy event function call of the call RCU. | |
192 | */ | |
193 | static void destroy_event_rcu(struct rcu_head *head) | |
194 | { | |
7972aab2 DG |
195 | struct lttng_ht_node_u64 *node = |
196 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
d0b96690 DG |
197 | struct ust_registry_event *event = |
198 | caa_container_of(node, struct ust_registry_event, node); | |
199 | ||
200 | destroy_event(event); | |
201 | } | |
202 | ||
203 | /* | |
204 | * Find an event using the name and signature in the given registry. RCU read | |
205 | * side lock MUST be acquired before calling this function and as long as the | |
206 | * event reference is kept by the caller. | |
207 | * | |
208 | * On success, the event pointer is returned else NULL. | |
209 | */ | |
210 | struct ust_registry_event *ust_registry_find_event( | |
211 | struct ust_registry_channel *chan, char *name, char *sig) | |
212 | { | |
7972aab2 | 213 | struct lttng_ht_node_u64 *node; |
d0b96690 DG |
214 | struct lttng_ht_iter iter; |
215 | struct ust_registry_event *event = NULL; | |
216 | struct ust_registry_event key; | |
217 | ||
218 | assert(chan); | |
219 | assert(name); | |
220 | assert(sig); | |
221 | ||
222 | /* Setup key for the match function. */ | |
223 | strncpy(key.name, name, sizeof(key.name)); | |
224 | key.name[sizeof(key.name) - 1] = '\0'; | |
225 | key.signature = sig; | |
226 | ||
7972aab2 | 227 | cds_lfht_lookup(chan->ht->ht, chan->ht->hash_fct(&key, lttng_ht_seed), |
d0b96690 | 228 | chan->ht->match_fct, &key, &iter.iter); |
7972aab2 | 229 | node = lttng_ht_iter_get_node_u64(&iter); |
d0b96690 DG |
230 | if (!node) { |
231 | goto end; | |
232 | } | |
233 | event = caa_container_of(node, struct ust_registry_event, node); | |
234 | ||
235 | end: | |
236 | return event; | |
237 | } | |
238 | ||
239 | /* | |
240 | * Create a ust_registry_event from the given parameters and add it to the | |
241 | * registry hash table. If event_id is valid, it is set with the newly created | |
242 | * event id. | |
243 | * | |
244 | * On success, return 0 else a negative value. The created event MUST be unique | |
245 | * so on duplicate entry -EINVAL is returned. On error, event_id is untouched. | |
246 | * | |
247 | * Should be called with session registry mutex held. | |
248 | */ | |
249 | int ust_registry_create_event(struct ust_registry_session *session, | |
45893984 DG |
250 | uint64_t chan_key, int session_objd, int channel_objd, char *name, |
251 | char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel, | |
8494bda5 MD |
252 | char *model_emf_uri, int buffer_type, uint32_t *event_id_p, |
253 | struct ust_app *app) | |
d0b96690 DG |
254 | { |
255 | int ret; | |
7972aab2 | 256 | uint32_t event_id; |
d0b96690 DG |
257 | struct cds_lfht_node *nptr; |
258 | struct ust_registry_event *event = NULL; | |
45893984 | 259 | struct ust_registry_channel *chan; |
d0b96690 DG |
260 | |
261 | assert(session); | |
d0b96690 DG |
262 | assert(name); |
263 | assert(sig); | |
7972aab2 | 264 | assert(event_id_p); |
d0b96690 | 265 | |
d5d629b5 DG |
266 | rcu_read_lock(); |
267 | ||
d0b96690 DG |
268 | /* |
269 | * This should not happen but since it comes from the UST tracer, an | |
270 | * external party, don't assert and simply validate values. | |
271 | */ | |
272 | if (session_objd < 0 || channel_objd < 0) { | |
273 | ret = -EINVAL; | |
d5d629b5 | 274 | goto error_free; |
d0b96690 DG |
275 | } |
276 | ||
45893984 DG |
277 | chan = ust_registry_channel_find(session, chan_key); |
278 | if (!chan) { | |
279 | ret = -EINVAL; | |
d5d629b5 | 280 | goto error_free; |
45893984 DG |
281 | } |
282 | ||
d0b96690 DG |
283 | /* Check if we've reached the maximum possible id. */ |
284 | if (ust_registry_is_max_id(chan->used_event_id)) { | |
285 | ret = -ENOENT; | |
d5d629b5 | 286 | goto error_free; |
d0b96690 DG |
287 | } |
288 | ||
289 | event = alloc_event(session_objd, channel_objd, name, sig, nr_fields, | |
8494bda5 | 290 | fields, loglevel, model_emf_uri, app); |
d0b96690 DG |
291 | if (!event) { |
292 | ret = -ENOMEM; | |
d5d629b5 | 293 | goto error_free; |
d0b96690 DG |
294 | } |
295 | ||
d0b96690 | 296 | DBG3("UST registry creating event with event: %s, sig: %s, id: %u, " |
7972aab2 DG |
297 | "chan_objd: %u, sess_objd: %u, chan_id: %u", event->name, |
298 | event->signature, event->id, event->channel_objd, | |
299 | event->session_objd, chan->chan_id); | |
d0b96690 | 300 | |
d0b96690 DG |
301 | /* |
302 | * This is an add unique with a custom match function for event. The node | |
303 | * are matched using the event name and signature. | |
304 | */ | |
7972aab2 | 305 | nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event, |
d0b96690 DG |
306 | lttng_ht_seed), chan->ht->match_fct, event, &event->node.node); |
307 | if (nptr != &event->node.node) { | |
7972aab2 DG |
308 | if (buffer_type == LTTNG_BUFFER_PER_UID) { |
309 | /* | |
310 | * This is normal, we just have to send the event id of the | |
311 | * returned node and make sure we destroy the previously allocated | |
312 | * event object. | |
313 | */ | |
314 | destroy_event(event); | |
315 | event = caa_container_of(nptr, struct ust_registry_event, | |
316 | node.node); | |
317 | assert(event); | |
318 | event_id = event->id; | |
319 | } else { | |
320 | ERR("UST registry create event add unique failed for event: %s, " | |
321 | "sig: %s, id: %u, chan_objd: %u, sess_objd: %u", | |
322 | event->name, event->signature, event->id, | |
323 | event->channel_objd, event->session_objd); | |
324 | ret = -EINVAL; | |
325 | goto error_unlock; | |
326 | } | |
327 | } else { | |
328 | /* Request next event id if the node was successfully added. */ | |
329 | event_id = event->id = ust_registry_get_next_event_id(chan); | |
d0b96690 DG |
330 | } |
331 | ||
7972aab2 | 332 | *event_id_p = event_id; |
d0b96690 | 333 | |
7972aab2 DG |
334 | if (!event->metadata_dumped) { |
335 | /* Append to metadata */ | |
336 | ret = ust_metadata_event_statedump(session, chan, event); | |
337 | if (ret) { | |
338 | ERR("Error appending event metadata (errno = %d)", ret); | |
339 | rcu_read_unlock(); | |
340 | return ret; | |
341 | } | |
d0b96690 DG |
342 | } |
343 | ||
45893984 | 344 | rcu_read_unlock(); |
d0b96690 DG |
345 | return 0; |
346 | ||
d5d629b5 DG |
347 | error_free: |
348 | free(sig); | |
349 | free(fields); | |
350 | free(model_emf_uri); | |
d0b96690 DG |
351 | error_unlock: |
352 | rcu_read_unlock(); | |
d0b96690 DG |
353 | destroy_event(event); |
354 | return ret; | |
355 | } | |
356 | ||
357 | /* | |
358 | * For a given event in a registry, delete the entry and destroy the event. | |
359 | * This MUST be called within a RCU read side lock section. | |
360 | */ | |
361 | void ust_registry_destroy_event(struct ust_registry_channel *chan, | |
362 | struct ust_registry_event *event) | |
363 | { | |
364 | int ret; | |
365 | struct lttng_ht_iter iter; | |
366 | ||
367 | assert(chan); | |
368 | assert(event); | |
369 | ||
370 | /* Delete the node first. */ | |
371 | iter.iter.node = &event->node.node; | |
372 | ret = lttng_ht_del(chan->ht, &iter); | |
373 | assert(!ret); | |
374 | ||
375 | call_rcu(&event->node.head, destroy_event_rcu); | |
376 | ||
377 | return; | |
378 | } | |
379 | ||
36b588ed MD |
380 | /* |
381 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
382 | * section and outside of call_rcu thread, so we postpone its execution |
383 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
384 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
385 | */ |
386 | static | |
387 | void destroy_channel_rcu(struct rcu_head *head) | |
388 | { | |
389 | struct ust_registry_channel *chan = | |
390 | caa_container_of(head, struct ust_registry_channel, rcu_head); | |
391 | ||
9dbcf332 | 392 | if (chan->ht) { |
0b2dc8df | 393 | ht_cleanup_push(chan->ht); |
9dbcf332 | 394 | } |
3295105b | 395 | free(chan->ctx_fields); |
36b588ed MD |
396 | free(chan); |
397 | } | |
398 | ||
d0b96690 DG |
399 | /* |
400 | * Destroy every element of the registry and free the memory. This does NOT | |
401 | * free the registry pointer since it might not have been allocated before so | |
402 | * it's the caller responsability. | |
d0b96690 | 403 | */ |
45893984 | 404 | static void destroy_channel(struct ust_registry_channel *chan) |
d0b96690 DG |
405 | { |
406 | struct lttng_ht_iter iter; | |
407 | struct ust_registry_event *event; | |
408 | ||
409 | assert(chan); | |
410 | ||
9209cee7 | 411 | rcu_read_lock(); |
d0b96690 DG |
412 | /* Destroy all event associated with this registry. */ |
413 | cds_lfht_for_each_entry(chan->ht->ht, &iter.iter, event, node.node) { | |
414 | /* Delete the node from the ht and free it. */ | |
415 | ust_registry_destroy_event(chan, event); | |
416 | } | |
9209cee7 | 417 | rcu_read_unlock(); |
36b588ed | 418 | call_rcu(&chan->rcu_head, destroy_channel_rcu); |
d0b96690 DG |
419 | } |
420 | ||
421 | /* | |
422 | * Initialize registry with default values. | |
423 | */ | |
45893984 DG |
424 | int ust_registry_channel_add(struct ust_registry_session *session, |
425 | uint64_t key) | |
426 | { | |
427 | int ret = 0; | |
428 | struct ust_registry_channel *chan; | |
429 | ||
430 | assert(session); | |
431 | ||
432 | chan = zmalloc(sizeof(*chan)); | |
433 | if (!chan) { | |
434 | PERROR("zmalloc ust registry channel"); | |
435 | ret = -ENOMEM; | |
9dbcf332 | 436 | goto error_alloc; |
45893984 DG |
437 | } |
438 | ||
439 | chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
440 | if (!chan->ht) { | |
441 | ret = -ENOMEM; | |
442 | goto error; | |
443 | } | |
444 | ||
445 | /* Set custom match function. */ | |
446 | chan->ht->match_fct = ht_match_event; | |
7972aab2 DG |
447 | chan->ht->hash_fct = ht_hash_event; |
448 | ||
449 | /* | |
450 | * Assign a channel ID right now since the event notification comes | |
451 | * *before* the channel notify so the ID needs to be set at this point so | |
452 | * the metadata can be dumped for that event. | |
453 | */ | |
454 | if (ust_registry_is_max_id(session->used_channel_id)) { | |
455 | ret = -1; | |
456 | goto error; | |
457 | } | |
458 | chan->chan_id = ust_registry_get_next_chan_id(session); | |
45893984 DG |
459 | |
460 | rcu_read_lock(); | |
461 | lttng_ht_node_init_u64(&chan->node, key); | |
462 | lttng_ht_add_unique_u64(session->channels, &chan->node); | |
463 | rcu_read_unlock(); | |
464 | ||
9dbcf332 DG |
465 | return 0; |
466 | ||
45893984 | 467 | error: |
9dbcf332 DG |
468 | destroy_channel(chan); |
469 | error_alloc: | |
45893984 DG |
470 | return ret; |
471 | } | |
472 | ||
473 | /* | |
474 | * Find a channel in the given registry. RCU read side lock MUST be acquired | |
475 | * before calling this function and as long as the event reference is kept by | |
476 | * the caller. | |
477 | * | |
478 | * On success, the pointer is returned else NULL. | |
479 | */ | |
480 | struct ust_registry_channel *ust_registry_channel_find( | |
481 | struct ust_registry_session *session, uint64_t key) | |
482 | { | |
483 | struct lttng_ht_node_u64 *node; | |
484 | struct lttng_ht_iter iter; | |
485 | struct ust_registry_channel *chan = NULL; | |
486 | ||
487 | assert(session); | |
488 | assert(session->channels); | |
489 | ||
7972aab2 DG |
490 | DBG3("UST registry channel finding key %" PRIu64, key); |
491 | ||
45893984 DG |
492 | lttng_ht_lookup(session->channels, &key, &iter); |
493 | node = lttng_ht_iter_get_node_u64(&iter); | |
494 | if (!node) { | |
495 | goto end; | |
496 | } | |
497 | chan = caa_container_of(node, struct ust_registry_channel, node); | |
498 | ||
499 | end: | |
500 | return chan; | |
501 | } | |
502 | ||
503 | /* | |
504 | * Remove channel using key from registry and free memory. | |
505 | */ | |
506 | void ust_registry_channel_del_free(struct ust_registry_session *session, | |
507 | uint64_t key) | |
508 | { | |
509 | struct lttng_ht_iter iter; | |
510 | struct ust_registry_channel *chan; | |
9209cee7 | 511 | int ret; |
45893984 DG |
512 | |
513 | assert(session); | |
514 | ||
515 | rcu_read_lock(); | |
516 | chan = ust_registry_channel_find(session, key); | |
517 | if (!chan) { | |
9209cee7 | 518 | rcu_read_unlock(); |
45893984 DG |
519 | goto end; |
520 | } | |
521 | ||
522 | iter.iter.node = &chan->node.node; | |
9209cee7 MD |
523 | ret = lttng_ht_del(session->channels, &iter); |
524 | assert(!ret); | |
525 | rcu_read_unlock(); | |
45893984 DG |
526 | destroy_channel(chan); |
527 | ||
528 | end: | |
45893984 DG |
529 | return; |
530 | } | |
531 | ||
532 | /* | |
533 | * Initialize registry with default values and set the newly allocated session | |
534 | * pointer to sessionp. | |
535 | * | |
536 | * Return 0 on success and sessionp is set or else return -1 and sessionp is | |
537 | * kept untouched. | |
538 | */ | |
539 | int ust_registry_session_init(struct ust_registry_session **sessionp, | |
d0b96690 DG |
540 | struct ust_app *app, |
541 | uint32_t bits_per_long, | |
542 | uint32_t uint8_t_alignment, | |
543 | uint32_t uint16_t_alignment, | |
544 | uint32_t uint32_t_alignment, | |
545 | uint32_t uint64_t_alignment, | |
546 | uint32_t long_alignment, | |
af6142cf MD |
547 | int byte_order, |
548 | uint32_t major, | |
549 | uint32_t minor) | |
d0b96690 DG |
550 | { |
551 | int ret; | |
45893984 | 552 | struct ust_registry_session *session; |
d0b96690 | 553 | |
45893984 | 554 | assert(sessionp); |
d0b96690 | 555 | |
45893984 DG |
556 | session = zmalloc(sizeof(*session)); |
557 | if (!session) { | |
558 | PERROR("zmalloc ust registry session"); | |
9dbcf332 | 559 | goto error_alloc; |
45893984 | 560 | } |
d0b96690 DG |
561 | |
562 | pthread_mutex_init(&session->lock, NULL); | |
563 | session->bits_per_long = bits_per_long; | |
564 | session->uint8_t_alignment = uint8_t_alignment; | |
565 | session->uint16_t_alignment = uint16_t_alignment; | |
566 | session->uint32_t_alignment = uint32_t_alignment; | |
567 | session->uint64_t_alignment = uint64_t_alignment; | |
568 | session->long_alignment = long_alignment; | |
569 | session->byte_order = byte_order; | |
570 | ||
45893984 DG |
571 | session->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
572 | if (!session->channels) { | |
573 | goto error; | |
574 | } | |
575 | ||
d0b96690 DG |
576 | ret = lttng_uuid_generate(session->uuid); |
577 | if (ret) { | |
578 | ERR("Failed to generate UST uuid (errno = %d)", ret); | |
579 | goto error; | |
580 | } | |
581 | ||
582 | pthread_mutex_lock(&session->lock); | |
af6142cf | 583 | ret = ust_metadata_session_statedump(session, app, major, minor); |
d0b96690 DG |
584 | pthread_mutex_unlock(&session->lock); |
585 | if (ret) { | |
586 | ERR("Failed to generate session metadata (errno = %d)", ret); | |
587 | goto error; | |
588 | } | |
589 | ||
45893984 DG |
590 | *sessionp = session; |
591 | ||
d0b96690 DG |
592 | return 0; |
593 | ||
594 | error: | |
9dbcf332 | 595 | ust_registry_session_destroy(session); |
d24ff3fd | 596 | free(session); |
9dbcf332 | 597 | error_alloc: |
d0b96690 DG |
598 | return -1; |
599 | } | |
600 | ||
601 | /* | |
602 | * Destroy session registry. This does NOT free the given pointer since it | |
603 | * might get passed as a reference. The registry lock should NOT be acquired. | |
604 | */ | |
605 | void ust_registry_session_destroy(struct ust_registry_session *reg) | |
606 | { | |
607 | int ret; | |
45893984 DG |
608 | struct lttng_ht_iter iter; |
609 | struct ust_registry_channel *chan; | |
d0b96690 | 610 | |
9d8efb0e DG |
611 | assert(reg); |
612 | ||
d0b96690 DG |
613 | /* On error, EBUSY can be returned if lock. Code flow error. */ |
614 | ret = pthread_mutex_destroy(®->lock); | |
615 | assert(!ret); | |
616 | ||
9d8efb0e DG |
617 | if (reg->channels) { |
618 | rcu_read_lock(); | |
619 | /* Destroy all event associated with this registry. */ | |
620 | cds_lfht_for_each_entry(reg->channels->ht, &iter.iter, chan, | |
621 | node.node) { | |
622 | /* Delete the node from the ht and free it. */ | |
623 | ret = lttng_ht_del(reg->channels, &iter); | |
624 | assert(!ret); | |
625 | destroy_channel(chan); | |
626 | } | |
627 | rcu_read_unlock(); | |
628 | ht_cleanup_push(reg->channels); | |
45893984 | 629 | } |
45893984 | 630 | |
d0b96690 DG |
631 | free(reg->metadata); |
632 | } |