ust registry: Refactor representation of nested types
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry.c
CommitLineData
d0b96690 1/*
ab5be9fa 2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
d0b96690 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
d0b96690 5 *
d0b96690 6 */
890d8fe4 7
6c1c0768 8#define _LGPL_SOURCE
d0b96690 9#include <assert.h>
7972aab2 10#include <inttypes.h>
d0b96690
DG
11
12#include <common/common.h>
7972aab2
DG
13#include <common/hashtable/utils.h>
14#include <lttng/lttng.h>
15
d0b96690 16#include "ust-registry.h"
8494bda5 17#include "ust-app.h"
98b73e88 18#include "ust-field-utils.h"
0b2dc8df 19#include "utils.h"
e9404c27
JG
20#include "lttng-sessiond.h"
21#include "notification-thread-commands.h"
d0b96690 22
98b73e88 23
d0b96690
DG
24/*
25 * Hash table match function for event in the registry.
26 */
27static int ht_match_event(struct cds_lfht_node *node, const void *_key)
28{
d0b96690 29 const struct ust_registry_event *key;
98b73e88
FD
30 struct ust_registry_event *event;
31 int i;
d0b96690
DG
32
33 assert(node);
34 assert(_key);
35
36 event = caa_container_of(node, struct ust_registry_event, node.node);
37 assert(event);
38 key = _key;
39
98b73e88 40 /* It has to be a perfect match. First, compare the event names. */
b4d096a6 41 if (strncmp(event->name, key->name, sizeof(event->name))) {
d0b96690
DG
42 goto no_match;
43 }
44
98b73e88
FD
45 /* Compare log levels. */
46 if (event->loglevel_value != key->loglevel_value) {
47 goto no_match;
48 }
49
50 /* Compare the number of fields. */
51 if (event->nr_fields != key->nr_fields) {
52 goto no_match;
53 }
54
55 /* Compare each field individually. */
56 for (i = 0; i < event->nr_fields; i++) {
57 if (!match_ustctl_field(&event->fields[i], &key->fields[i])) {
58 goto no_match;
59 }
60 }
61
62 /* Compare model URI. */
63 if (event->model_emf_uri != NULL && key->model_emf_uri == NULL) {
64 goto no_match;
65 } else if(event->model_emf_uri == NULL && key->model_emf_uri != NULL) {
d0b96690 66 goto no_match;
98b73e88
FD
67 } else if (event->model_emf_uri != NULL && key->model_emf_uri != NULL) {
68 if (strcmp(event->model_emf_uri, key->model_emf_uri)) {
69 goto no_match;
70 }
d0b96690
DG
71 }
72
73 /* Match */
74 return 1;
75
76no_match:
77 return 0;
78}
79
bcd52dd9 80static unsigned long ht_hash_event(const void *_key, unsigned long seed)
7972aab2 81{
98b73e88 82 uint64_t hashed_key;
bcd52dd9 83 const struct ust_registry_event *key = _key;
7972aab2
DG
84
85 assert(key);
86
98b73e88 87 hashed_key = (uint64_t) hash_key_str(key->name, seed);
7972aab2 88
98b73e88 89 return hash_key_u64(&hashed_key, seed);
7972aab2
DG
90}
91
10b56aef
MD
92static int compare_enums(const struct ust_registry_enum *reg_enum_a,
93 const struct ust_registry_enum *reg_enum_b)
94{
95 int ret = 0;
96 size_t i;
97
98 assert(strcmp(reg_enum_a->name, reg_enum_b->name) == 0);
99 if (reg_enum_a->nr_entries != reg_enum_b->nr_entries) {
100 ret = -1;
101 goto end;
102 }
103 for (i = 0; i < reg_enum_a->nr_entries; i++) {
104 const struct ustctl_enum_entry *entries_a, *entries_b;
105
106 entries_a = &reg_enum_a->entries[i];
107 entries_b = &reg_enum_b->entries[i];
3b016e58 108 if (entries_a->start.value != entries_b->start.value) {
10b56aef
MD
109 ret = -1;
110 goto end;
111 }
3b016e58 112 if (entries_a->end.value != entries_b->end.value) {
10b56aef
MD
113 ret = -1;
114 goto end;
115 }
3b016e58
MD
116 if (entries_a->start.signedness != entries_b->start.signedness) {
117 ret = -1;
118 goto end;
119 }
120 if (entries_a->end.signedness != entries_b->end.signedness) {
121 ret = -1;
122 goto end;
123 }
124
10b56aef
MD
125 if (strcmp(entries_a->string, entries_b->string)) {
126 ret = -1;
127 goto end;
128 }
129 }
130end:
131 return ret;
132}
133
134/*
135 * Hash table match function for enumerations in the session. Match is
136 * performed on enumeration name, and confirmed by comparing the enum
137 * entries.
138 */
139static int ht_match_enum(struct cds_lfht_node *node, const void *_key)
140{
141 struct ust_registry_enum *_enum;
142 const struct ust_registry_enum *key;
143
144 assert(node);
145 assert(_key);
146
147 _enum = caa_container_of(node, struct ust_registry_enum,
148 node.node);
149 assert(_enum);
150 key = _key;
151
152 if (strncmp(_enum->name, key->name, LTTNG_UST_SYM_NAME_LEN)) {
153 goto no_match;
154 }
155 if (compare_enums(_enum, key)) {
156 goto no_match;
157 }
158
159 /* Match. */
160 return 1;
161
162no_match:
163 return 0;
164}
165
166/*
167 * Hash table match function for enumerations in the session. Match is
168 * performed by enumeration ID.
169 */
170static int ht_match_enum_id(struct cds_lfht_node *node, const void *_key)
171{
172 struct ust_registry_enum *_enum;
173 const struct ust_registry_enum *key = _key;
174
175 assert(node);
176 assert(_key);
177
178 _enum = caa_container_of(node, struct ust_registry_enum, node.node);
179 assert(_enum);
180
181 if (_enum->id != key->id) {
182 goto no_match;
183 }
184
185 /* Match. */
186 return 1;
187
188no_match:
189 return 0;
190}
191
192/*
193 * Hash table hash function for enumerations in the session. The
194 * enumeration name is used for hashing.
195 */
196static unsigned long ht_hash_enum(void *_key, unsigned long seed)
197{
198 struct ust_registry_enum *key = _key;
199
200 assert(key);
201 return hash_key_str(key->name, seed);
202}
203
8494bda5
MD
204/*
205 * Return negative value on error, 0 if OK.
206 *
207 * TODO: we could add stricter verification of more types to catch
208 * errors in liblttng-ust implementation earlier than consumption by the
209 * trace reader.
210 */
211static
212int validate_event_field(struct ustctl_field *field,
213 const char *event_name,
214 struct ust_app *app)
215{
da860cab
MD
216 int ret = 0;
217
8494bda5
MD
218 switch(field->type.atype) {
219 case ustctl_atype_integer:
220 case ustctl_atype_enum:
221 case ustctl_atype_array:
222 case ustctl_atype_sequence:
223 case ustctl_atype_string:
da860cab 224 case ustctl_atype_variant:
0d32d1a9
MD
225 case ustctl_atype_array_nestable:
226 case ustctl_atype_sequence_nestable:
227 case ustctl_atype_enum_nestable:
228 case ustctl_atype_variant_nestable:
da860cab
MD
229 break;
230 case ustctl_atype_struct:
0d32d1a9
MD
231 if (field->type.u.legacy._struct.nr_fields != 0) {
232 WARN("Unsupported non-empty struct field.");
233 ret = -EINVAL;
234 goto end;
235 }
236 break;
237 case ustctl_atype_struct_nestable:
238 if (field->type.u.struct_nestable.nr_fields != 0) {
da860cab
MD
239 WARN("Unsupported non-empty struct field.");
240 ret = -EINVAL;
241 goto end;
242 }
8494bda5
MD
243 break;
244
245 case ustctl_atype_float:
0d32d1a9 246 switch (field->type.u._float.mant_dig) {
8494bda5
MD
247 case 0:
248 WARN("UST application '%s' (pid: %d) has unknown float mantissa '%u' "
249 "in field '%s', rejecting event '%s'",
250 app->name, app->pid,
0d32d1a9 251 field->type.u._float.mant_dig,
8494bda5
MD
252 field->name,
253 event_name);
da860cab
MD
254 ret = -EINVAL;
255 goto end;
8494bda5
MD
256 default:
257 break;
258 }
259 break;
260
261 default:
da860cab
MD
262 ret = -ENOENT;
263 goto end;
8494bda5 264 }
da860cab
MD
265end:
266 return ret;
8494bda5
MD
267}
268
269static
270int validate_event_fields(size_t nr_fields, struct ustctl_field *fields,
271 const char *event_name, struct ust_app *app)
272{
273 unsigned int i;
274
275 for (i = 0; i < nr_fields; i++) {
276 if (validate_event_field(&fields[i], event_name, app) < 0)
277 return -EINVAL;
278 }
279 return 0;
280}
281
d0b96690
DG
282/*
283 * Allocate event and initialize it. This does NOT set a valid event id from a
284 * registry.
285 */
286static struct ust_registry_event *alloc_event(int session_objd,
287 int channel_objd, char *name, char *sig, size_t nr_fields,
2106efa0
PP
288 struct ustctl_field *fields, int loglevel_value,
289 char *model_emf_uri, struct ust_app *app)
d0b96690
DG
290{
291 struct ust_registry_event *event = NULL;
292
8494bda5
MD
293 /*
294 * Ensure that the field content is valid.
295 */
296 if (validate_event_fields(nr_fields, fields, name, app) < 0) {
297 return NULL;
298 }
299
d0b96690
DG
300 event = zmalloc(sizeof(*event));
301 if (!event) {
302 PERROR("zmalloc ust registry event");
303 goto error;
304 }
305
306 event->session_objd = session_objd;
307 event->channel_objd = channel_objd;
308 /* Allocated by ustctl. */
309 event->signature = sig;
310 event->nr_fields = nr_fields;
311 event->fields = fields;
2106efa0 312 event->loglevel_value = loglevel_value;
d0b96690
DG
313 event->model_emf_uri = model_emf_uri;
314 if (name) {
315 /* Copy event name and force NULL byte. */
316 strncpy(event->name, name, sizeof(event->name));
317 event->name[sizeof(event->name) - 1] = '\0';
318 }
7972aab2 319 cds_lfht_node_init(&event->node.node);
d0b96690
DG
320
321error:
322 return event;
323}
324
325/*
326 * Free event data structure. This does NOT delete it from any hash table. It's
327 * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the
328 * event is previously deleted from a rcu hash table.
329 */
330static void destroy_event(struct ust_registry_event *event)
331{
332 if (!event) {
333 return;
334 }
335
336 free(event->fields);
337 free(event->model_emf_uri);
338 free(event->signature);
339 free(event);
340}
341
342/*
343 * Destroy event function call of the call RCU.
344 */
345static void destroy_event_rcu(struct rcu_head *head)
346{
7972aab2
DG
347 struct lttng_ht_node_u64 *node =
348 caa_container_of(head, struct lttng_ht_node_u64, head);
d0b96690
DG
349 struct ust_registry_event *event =
350 caa_container_of(node, struct ust_registry_event, node);
351
352 destroy_event(event);
353}
354
355/*
356 * Find an event using the name and signature in the given registry. RCU read
357 * side lock MUST be acquired before calling this function and as long as the
358 * event reference is kept by the caller.
359 *
360 * On success, the event pointer is returned else NULL.
361 */
362struct ust_registry_event *ust_registry_find_event(
363 struct ust_registry_channel *chan, char *name, char *sig)
364{
7972aab2 365 struct lttng_ht_node_u64 *node;
d0b96690
DG
366 struct lttng_ht_iter iter;
367 struct ust_registry_event *event = NULL;
368 struct ust_registry_event key;
369
370 assert(chan);
371 assert(name);
372 assert(sig);
373
374 /* Setup key for the match function. */
375 strncpy(key.name, name, sizeof(key.name));
376 key.name[sizeof(key.name) - 1] = '\0';
377 key.signature = sig;
378
7972aab2 379 cds_lfht_lookup(chan->ht->ht, chan->ht->hash_fct(&key, lttng_ht_seed),
d0b96690 380 chan->ht->match_fct, &key, &iter.iter);
7972aab2 381 node = lttng_ht_iter_get_node_u64(&iter);
d0b96690
DG
382 if (!node) {
383 goto end;
384 }
385 event = caa_container_of(node, struct ust_registry_event, node);
386
387end:
388 return event;
389}
390
391/*
392 * Create a ust_registry_event from the given parameters and add it to the
393 * registry hash table. If event_id is valid, it is set with the newly created
394 * event id.
395 *
396 * On success, return 0 else a negative value. The created event MUST be unique
397 * so on duplicate entry -EINVAL is returned. On error, event_id is untouched.
398 *
399 * Should be called with session registry mutex held.
400 */
401int ust_registry_create_event(struct ust_registry_session *session,
45893984 402 uint64_t chan_key, int session_objd, int channel_objd, char *name,
2106efa0
PP
403 char *sig, size_t nr_fields, struct ustctl_field *fields,
404 int loglevel_value, char *model_emf_uri, int buffer_type,
405 uint32_t *event_id_p, struct ust_app *app)
d0b96690
DG
406{
407 int ret;
7972aab2 408 uint32_t event_id;
d0b96690
DG
409 struct cds_lfht_node *nptr;
410 struct ust_registry_event *event = NULL;
45893984 411 struct ust_registry_channel *chan;
d0b96690
DG
412
413 assert(session);
d0b96690
DG
414 assert(name);
415 assert(sig);
7972aab2 416 assert(event_id_p);
d0b96690 417
d5d629b5
DG
418 rcu_read_lock();
419
d0b96690
DG
420 /*
421 * This should not happen but since it comes from the UST tracer, an
422 * external party, don't assert and simply validate values.
423 */
424 if (session_objd < 0 || channel_objd < 0) {
425 ret = -EINVAL;
d5d629b5 426 goto error_free;
d0b96690
DG
427 }
428
45893984
DG
429 chan = ust_registry_channel_find(session, chan_key);
430 if (!chan) {
431 ret = -EINVAL;
d5d629b5 432 goto error_free;
45893984
DG
433 }
434
d0b96690
DG
435 /* Check if we've reached the maximum possible id. */
436 if (ust_registry_is_max_id(chan->used_event_id)) {
437 ret = -ENOENT;
d5d629b5 438 goto error_free;
d0b96690
DG
439 }
440
441 event = alloc_event(session_objd, channel_objd, name, sig, nr_fields,
2106efa0 442 fields, loglevel_value, model_emf_uri, app);
d0b96690
DG
443 if (!event) {
444 ret = -ENOMEM;
d5d629b5 445 goto error_free;
d0b96690
DG
446 }
447
d0b96690 448 DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
7972aab2
DG
449 "chan_objd: %u, sess_objd: %u, chan_id: %u", event->name,
450 event->signature, event->id, event->channel_objd,
451 event->session_objd, chan->chan_id);
d0b96690 452
d0b96690
DG
453 /*
454 * This is an add unique with a custom match function for event. The node
455 * are matched using the event name and signature.
456 */
7972aab2 457 nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event,
d0b96690
DG
458 lttng_ht_seed), chan->ht->match_fct, event, &event->node.node);
459 if (nptr != &event->node.node) {
7972aab2
DG
460 if (buffer_type == LTTNG_BUFFER_PER_UID) {
461 /*
462 * This is normal, we just have to send the event id of the
463 * returned node and make sure we destroy the previously allocated
464 * event object.
465 */
466 destroy_event(event);
467 event = caa_container_of(nptr, struct ust_registry_event,
468 node.node);
469 assert(event);
470 event_id = event->id;
471 } else {
472 ERR("UST registry create event add unique failed for event: %s, "
473 "sig: %s, id: %u, chan_objd: %u, sess_objd: %u",
474 event->name, event->signature, event->id,
475 event->channel_objd, event->session_objd);
476 ret = -EINVAL;
477 goto error_unlock;
478 }
479 } else {
480 /* Request next event id if the node was successfully added. */
481 event_id = event->id = ust_registry_get_next_event_id(chan);
d0b96690
DG
482 }
483
7972aab2 484 *event_id_p = event_id;
d0b96690 485
7972aab2
DG
486 if (!event->metadata_dumped) {
487 /* Append to metadata */
488 ret = ust_metadata_event_statedump(session, chan, event);
489 if (ret) {
490 ERR("Error appending event metadata (errno = %d)", ret);
491 rcu_read_unlock();
492 return ret;
493 }
d0b96690
DG
494 }
495
45893984 496 rcu_read_unlock();
d0b96690
DG
497 return 0;
498
d5d629b5
DG
499error_free:
500 free(sig);
501 free(fields);
502 free(model_emf_uri);
d0b96690
DG
503error_unlock:
504 rcu_read_unlock();
d0b96690
DG
505 destroy_event(event);
506 return ret;
507}
508
509/*
510 * For a given event in a registry, delete the entry and destroy the event.
511 * This MUST be called within a RCU read side lock section.
512 */
513void ust_registry_destroy_event(struct ust_registry_channel *chan,
514 struct ust_registry_event *event)
515{
516 int ret;
517 struct lttng_ht_iter iter;
518
519 assert(chan);
520 assert(event);
521
522 /* Delete the node first. */
523 iter.iter.node = &event->node.node;
524 ret = lttng_ht_del(chan->ht, &iter);
525 assert(!ret);
526
527 call_rcu(&event->node.head, destroy_event_rcu);
528
529 return;
530}
531
10b56aef
MD
532static void destroy_enum(struct ust_registry_enum *reg_enum)
533{
534 if (!reg_enum) {
535 return;
536 }
537 free(reg_enum->entries);
538 free(reg_enum);
539}
540
541static void destroy_enum_rcu(struct rcu_head *head)
542{
543 struct ust_registry_enum *reg_enum =
544 caa_container_of(head, struct ust_registry_enum, rcu_head);
545
546 destroy_enum(reg_enum);
547}
548
549/*
550 * Lookup enumeration by name and comparing enumeration entries.
551 * Needs to be called from RCU read-side critical section.
552 */
10dc2d10
SM
553static struct ust_registry_enum *ust_registry_lookup_enum(
554 struct ust_registry_session *session,
10b56aef
MD
555 const struct ust_registry_enum *reg_enum_lookup)
556{
557 struct ust_registry_enum *reg_enum = NULL;
558 struct lttng_ht_node_str *node;
559 struct lttng_ht_iter iter;
560
561 cds_lfht_lookup(session->enums->ht,
a752d6fa
FD
562 ht_hash_enum((void *) reg_enum_lookup, lttng_ht_seed),
563 ht_match_enum, reg_enum_lookup, &iter.iter);
10b56aef
MD
564 node = lttng_ht_iter_get_node_str(&iter);
565 if (!node) {
566 goto end;
567 }
568 reg_enum = caa_container_of(node, struct ust_registry_enum, node);
569end:
570 return reg_enum;
571}
572
573/*
574 * Lookup enumeration by enum ID.
575 * Needs to be called from RCU read-side critical section.
576 */
577struct ust_registry_enum *
578 ust_registry_lookup_enum_by_id(struct ust_registry_session *session,
579 const char *enum_name, uint64_t enum_id)
580{
581 struct ust_registry_enum *reg_enum = NULL;
582 struct lttng_ht_node_str *node;
583 struct lttng_ht_iter iter;
584 struct ust_registry_enum reg_enum_lookup;
585
586 memset(&reg_enum_lookup, 0, sizeof(reg_enum_lookup));
587 strncpy(reg_enum_lookup.name, enum_name, LTTNG_UST_SYM_NAME_LEN);
588 reg_enum_lookup.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
589 reg_enum_lookup.id = enum_id;
590 cds_lfht_lookup(session->enums->ht,
591 ht_hash_enum((void *) &reg_enum_lookup, lttng_ht_seed),
592 ht_match_enum_id, &reg_enum_lookup, &iter.iter);
593 node = lttng_ht_iter_get_node_str(&iter);
594 if (!node) {
595 goto end;
596 }
597 reg_enum = caa_container_of(node, struct ust_registry_enum, node);
598end:
599 return reg_enum;
600}
601
602/*
603 * Create a ust_registry_enum from the given parameters and add it to the
604 * registry hash table, or find it if already there.
605 *
606 * On success, return 0 else a negative value.
607 *
608 * Should be called with session registry mutex held.
609 *
610 * We receive ownership of entries.
611 */
612int ust_registry_create_or_find_enum(struct ust_registry_session *session,
613 int session_objd, char *enum_name,
614 struct ustctl_enum_entry *entries, size_t nr_entries,
615 uint64_t *enum_id)
616{
617 int ret = 0;
618 struct cds_lfht_node *nodep;
619 struct ust_registry_enum *reg_enum = NULL, *old_reg_enum;
620
621 assert(session);
622 assert(enum_name);
623
624 rcu_read_lock();
625
626 /*
627 * This should not happen but since it comes from the UST tracer, an
628 * external party, don't assert and simply validate values.
629 */
630 if (session_objd < 0) {
631 ret = -EINVAL;
632 goto end;
633 }
634
635 /* Check if the enumeration was already dumped */
636 reg_enum = zmalloc(sizeof(*reg_enum));
637 if (!reg_enum) {
638 PERROR("zmalloc ust registry enumeration");
639 ret = -ENOMEM;
640 goto end;
641 }
642 strncpy(reg_enum->name, enum_name, LTTNG_UST_SYM_NAME_LEN);
643 reg_enum->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
644 /* entries will be owned by reg_enum. */
645 reg_enum->entries = entries;
646 reg_enum->nr_entries = nr_entries;
647 entries = NULL;
648
649 old_reg_enum = ust_registry_lookup_enum(session, reg_enum);
650 if (old_reg_enum) {
651 DBG("enum %s already in sess_objd: %u", enum_name, session_objd);
652 /* Fall through. Use prior enum. */
653 destroy_enum(reg_enum);
654 reg_enum = old_reg_enum;
655 } else {
656 DBG("UST registry creating enum: %s, sess_objd: %u",
657 enum_name, session_objd);
658 if (session->next_enum_id == -1ULL) {
659 ret = -EOVERFLOW;
660 destroy_enum(reg_enum);
661 goto end;
662 }
663 reg_enum->id = session->next_enum_id++;
664 cds_lfht_node_init(&reg_enum->node.node);
665 nodep = cds_lfht_add_unique(session->enums->ht,
666 ht_hash_enum(reg_enum, lttng_ht_seed),
667 ht_match_enum_id, reg_enum,
668 &reg_enum->node.node);
669 assert(nodep == &reg_enum->node.node);
670 }
671 DBG("UST registry reply with enum %s with id %" PRIu64 " in sess_objd: %u",
672 enum_name, reg_enum->id, session_objd);
673 *enum_id = reg_enum->id;
674end:
675 free(entries);
676 rcu_read_unlock();
677 return ret;
678}
679
680/*
681 * For a given enumeration in a registry, delete the entry and destroy
682 * the enumeration.
683 * This MUST be called within a RCU read side lock section.
684 */
10dc2d10 685static void ust_registry_destroy_enum(struct ust_registry_session *reg_session,
10b56aef
MD
686 struct ust_registry_enum *reg_enum)
687{
688 int ret;
689 struct lttng_ht_iter iter;
690
691 assert(reg_session);
692 assert(reg_enum);
693
694 /* Delete the node first. */
695 iter.iter.node = &reg_enum->node.node;
696 ret = lttng_ht_del(reg_session->enums, &iter);
697 assert(!ret);
698 call_rcu(&reg_enum->rcu_head, destroy_enum_rcu);
699}
700
36b588ed
MD
701/*
702 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
703 * section and outside of call_rcu thread, so we postpone its execution
704 * using ht_cleanup_push. It is simpler than to change the semantic of
705 * the many callers of delete_ust_app_session().
36b588ed
MD
706 */
707static
708void destroy_channel_rcu(struct rcu_head *head)
709{
710 struct ust_registry_channel *chan =
711 caa_container_of(head, struct ust_registry_channel, rcu_head);
712
9dbcf332 713 if (chan->ht) {
0b2dc8df 714 ht_cleanup_push(chan->ht);
9dbcf332 715 }
3295105b 716 free(chan->ctx_fields);
36b588ed
MD
717 free(chan);
718}
719
d0b96690
DG
720/*
721 * Destroy every element of the registry and free the memory. This does NOT
722 * free the registry pointer since it might not have been allocated before so
723 * it's the caller responsability.
d0b96690 724 */
e9404c27 725static void destroy_channel(struct ust_registry_channel *chan, bool notif)
d0b96690
DG
726{
727 struct lttng_ht_iter iter;
728 struct ust_registry_event *event;
e9404c27 729 enum lttng_error_code cmd_ret;
d0b96690
DG
730
731 assert(chan);
732
e9404c27
JG
733 if (notif) {
734 cmd_ret = notification_thread_command_remove_channel(
735 notification_thread_handle, chan->consumer_key,
736 LTTNG_DOMAIN_UST);
737 if (cmd_ret != LTTNG_OK) {
738 ERR("Failed to remove channel from notification thread");
739 }
740 }
741
9209cee7 742 rcu_read_lock();
d0b96690
DG
743 /* Destroy all event associated with this registry. */
744 cds_lfht_for_each_entry(chan->ht->ht, &iter.iter, event, node.node) {
745 /* Delete the node from the ht and free it. */
746 ust_registry_destroy_event(chan, event);
747 }
9209cee7 748 rcu_read_unlock();
36b588ed 749 call_rcu(&chan->rcu_head, destroy_channel_rcu);
d0b96690
DG
750}
751
752/*
753 * Initialize registry with default values.
754 */
45893984
DG
755int ust_registry_channel_add(struct ust_registry_session *session,
756 uint64_t key)
757{
758 int ret = 0;
759 struct ust_registry_channel *chan;
760
761 assert(session);
762
763 chan = zmalloc(sizeof(*chan));
764 if (!chan) {
765 PERROR("zmalloc ust registry channel");
766 ret = -ENOMEM;
9dbcf332 767 goto error_alloc;
45893984
DG
768 }
769
770 chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
771 if (!chan->ht) {
772 ret = -ENOMEM;
773 goto error;
774 }
775
776 /* Set custom match function. */
777 chan->ht->match_fct = ht_match_event;
7972aab2
DG
778 chan->ht->hash_fct = ht_hash_event;
779
780 /*
781 * Assign a channel ID right now since the event notification comes
782 * *before* the channel notify so the ID needs to be set at this point so
783 * the metadata can be dumped for that event.
784 */
785 if (ust_registry_is_max_id(session->used_channel_id)) {
786 ret = -1;
787 goto error;
788 }
789 chan->chan_id = ust_registry_get_next_chan_id(session);
45893984
DG
790
791 rcu_read_lock();
792 lttng_ht_node_init_u64(&chan->node, key);
793 lttng_ht_add_unique_u64(session->channels, &chan->node);
794 rcu_read_unlock();
795
9dbcf332
DG
796 return 0;
797
45893984 798error:
e9404c27 799 destroy_channel(chan, false);
9dbcf332 800error_alloc:
45893984
DG
801 return ret;
802}
803
804/*
805 * Find a channel in the given registry. RCU read side lock MUST be acquired
806 * before calling this function and as long as the event reference is kept by
807 * the caller.
808 *
809 * On success, the pointer is returned else NULL.
810 */
811struct ust_registry_channel *ust_registry_channel_find(
812 struct ust_registry_session *session, uint64_t key)
813{
814 struct lttng_ht_node_u64 *node;
815 struct lttng_ht_iter iter;
816 struct ust_registry_channel *chan = NULL;
817
818 assert(session);
819 assert(session->channels);
820
7972aab2
DG
821 DBG3("UST registry channel finding key %" PRIu64, key);
822
45893984
DG
823 lttng_ht_lookup(session->channels, &key, &iter);
824 node = lttng_ht_iter_get_node_u64(&iter);
825 if (!node) {
826 goto end;
827 }
828 chan = caa_container_of(node, struct ust_registry_channel, node);
829
830end:
831 return chan;
832}
833
834/*
835 * Remove channel using key from registry and free memory.
836 */
837void ust_registry_channel_del_free(struct ust_registry_session *session,
e9404c27 838 uint64_t key, bool notif)
45893984
DG
839{
840 struct lttng_ht_iter iter;
841 struct ust_registry_channel *chan;
9209cee7 842 int ret;
45893984
DG
843
844 assert(session);
845
846 rcu_read_lock();
847 chan = ust_registry_channel_find(session, key);
848 if (!chan) {
9209cee7 849 rcu_read_unlock();
45893984
DG
850 goto end;
851 }
852
853 iter.iter.node = &chan->node.node;
9209cee7
MD
854 ret = lttng_ht_del(session->channels, &iter);
855 assert(!ret);
856 rcu_read_unlock();
e9404c27 857 destroy_channel(chan, notif);
45893984
DG
858
859end:
45893984
DG
860 return;
861}
862
863/*
864 * Initialize registry with default values and set the newly allocated session
865 * pointer to sessionp.
866 *
867 * Return 0 on success and sessionp is set or else return -1 and sessionp is
868 * kept untouched.
869 */
870int ust_registry_session_init(struct ust_registry_session **sessionp,
d0b96690
DG
871 struct ust_app *app,
872 uint32_t bits_per_long,
873 uint32_t uint8_t_alignment,
874 uint32_t uint16_t_alignment,
875 uint32_t uint32_t_alignment,
876 uint32_t uint64_t_alignment,
877 uint32_t long_alignment,
af6142cf
MD
878 int byte_order,
879 uint32_t major,
d7ba1388 880 uint32_t minor,
3d071855 881 const char *root_shm_path,
d7ba1388
MD
882 const char *shm_path,
883 uid_t euid,
8de88061
JR
884 gid_t egid,
885 uint64_t tracing_id,
886 uid_t tracing_uid)
d0b96690
DG
887{
888 int ret;
45893984 889 struct ust_registry_session *session;
d0b96690 890
45893984 891 assert(sessionp);
d0b96690 892
45893984
DG
893 session = zmalloc(sizeof(*session));
894 if (!session) {
895 PERROR("zmalloc ust registry session");
9dbcf332 896 goto error_alloc;
45893984 897 }
d0b96690
DG
898
899 pthread_mutex_init(&session->lock, NULL);
900 session->bits_per_long = bits_per_long;
901 session->uint8_t_alignment = uint8_t_alignment;
902 session->uint16_t_alignment = uint16_t_alignment;
903 session->uint32_t_alignment = uint32_t_alignment;
904 session->uint64_t_alignment = uint64_t_alignment;
905 session->long_alignment = long_alignment;
906 session->byte_order = byte_order;
d7ba1388 907 session->metadata_fd = -1;
4628484a
MD
908 session->uid = euid;
909 session->gid = egid;
10b56aef 910 session->next_enum_id = 0;
7062f070
JD
911 session->major = major;
912 session->minor = minor;
3d071855
MD
913 strncpy(session->root_shm_path, root_shm_path,
914 sizeof(session->root_shm_path));
915 session->root_shm_path[sizeof(session->root_shm_path) - 1] = '\0';
d7ba1388
MD
916 if (shm_path[0]) {
917 strncpy(session->shm_path, shm_path,
918 sizeof(session->shm_path));
919 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
920 strncpy(session->metadata_path, shm_path,
921 sizeof(session->metadata_path));
922 session->metadata_path[sizeof(session->metadata_path) - 1] = '\0';
923 strncat(session->metadata_path, "/metadata",
924 sizeof(session->metadata_path)
925 - strlen(session->metadata_path) - 1);
926 }
927 if (session->shm_path[0]) {
928 ret = run_as_mkdir_recursive(session->shm_path,
929 S_IRWXU | S_IRWXG,
930 euid, egid);
931 if (ret) {
932 PERROR("run_as_mkdir_recursive");
933 goto error;
934 }
935 }
936 if (session->metadata_path[0]) {
937 /* Create metadata file */
4628484a 938 ret = run_as_open(session->metadata_path,
d7ba1388 939 O_WRONLY | O_CREAT | O_EXCL,
4628484a 940 S_IRUSR | S_IWUSR, euid, egid);
d7ba1388
MD
941 if (ret < 0) {
942 PERROR("Opening metadata file");
943 goto error;
944 }
945 session->metadata_fd = ret;
946 }
d0b96690 947
10b56aef
MD
948 session->enums = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
949 if (!session->enums) {
de64322e 950 ERR("Failed to create enums hash table");
10b56aef
MD
951 goto error;
952 }
953 /* hash/match functions are specified at call site. */
954 session->enums->match_fct = NULL;
955 session->enums->hash_fct = NULL;
956
45893984
DG
957 session->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
958 if (!session->channels) {
959 goto error;
960 }
961
d0b96690
DG
962 ret = lttng_uuid_generate(session->uuid);
963 if (ret) {
964 ERR("Failed to generate UST uuid (errno = %d)", ret);
965 goto error;
966 }
967
8de88061
JR
968 session->tracing_id = tracing_id;
969 session->tracing_uid = tracing_uid;
970
d0b96690 971 pthread_mutex_lock(&session->lock);
af6142cf 972 ret = ust_metadata_session_statedump(session, app, major, minor);
d0b96690
DG
973 pthread_mutex_unlock(&session->lock);
974 if (ret) {
975 ERR("Failed to generate session metadata (errno = %d)", ret);
976 goto error;
977 }
978
45893984
DG
979 *sessionp = session;
980
d0b96690
DG
981 return 0;
982
983error:
9dbcf332 984 ust_registry_session_destroy(session);
d24ff3fd 985 free(session);
9dbcf332 986error_alloc:
d0b96690
DG
987 return -1;
988}
989
990/*
991 * Destroy session registry. This does NOT free the given pointer since it
992 * might get passed as a reference. The registry lock should NOT be acquired.
993 */
994void ust_registry_session_destroy(struct ust_registry_session *reg)
995{
996 int ret;
45893984
DG
997 struct lttng_ht_iter iter;
998 struct ust_registry_channel *chan;
10b56aef 999 struct ust_registry_enum *reg_enum;
d0b96690 1000
286c991a
MD
1001 if (!reg) {
1002 return;
1003 }
9d8efb0e 1004
d0b96690
DG
1005 /* On error, EBUSY can be returned if lock. Code flow error. */
1006 ret = pthread_mutex_destroy(&reg->lock);
1007 assert(!ret);
1008
9d8efb0e
DG
1009 if (reg->channels) {
1010 rcu_read_lock();
1011 /* Destroy all event associated with this registry. */
1012 cds_lfht_for_each_entry(reg->channels->ht, &iter.iter, chan,
1013 node.node) {
1014 /* Delete the node from the ht and free it. */
1015 ret = lttng_ht_del(reg->channels, &iter);
1016 assert(!ret);
e9404c27 1017 destroy_channel(chan, true);
9d8efb0e
DG
1018 }
1019 rcu_read_unlock();
1020 ht_cleanup_push(reg->channels);
45893984 1021 }
45893984 1022
d0b96690 1023 free(reg->metadata);
d7ba1388
MD
1024 if (reg->metadata_fd >= 0) {
1025 ret = close(reg->metadata_fd);
1026 if (ret) {
1027 PERROR("close");
1028 }
4628484a
MD
1029 ret = run_as_unlink(reg->metadata_path,
1030 reg->uid, reg->gid);
d7ba1388
MD
1031 if (ret) {
1032 PERROR("unlink");
1033 }
1034 }
3d071855
MD
1035 if (reg->root_shm_path[0]) {
1036 /*
1037 * Try deleting the directory hierarchy.
1038 */
602766ec 1039 (void) run_as_rmdir_recursive(reg->root_shm_path,
f75c5439
MD
1040 reg->uid, reg->gid,
1041 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
3d071855 1042 }
10b56aef
MD
1043 /* Destroy the enum hash table */
1044 if (reg->enums) {
1045 rcu_read_lock();
1046 /* Destroy all enum entries associated with this registry. */
1047 cds_lfht_for_each_entry(reg->enums->ht, &iter.iter, reg_enum,
1048 node.node) {
1049 ust_registry_destroy_enum(reg, reg_enum);
1050 }
1051 rcu_read_unlock();
1052 ht_cleanup_push(reg->enums);
1053 }
d0b96690 1054}
This page took 0.165118 seconds and 4 git commands to generate.