Commit | Line | Data |
---|---|---|
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 | */ | |
27 | static 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++) { | |
b623cb6a | 57 | if (!match_lttng_ust_ctl_field(&event->fields[i], &key->fields[i])) { |
98b73e88 FD |
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 | ||
76 | no_match: | |
77 | return 0; | |
78 | } | |
79 | ||
bcd52dd9 | 80 | static 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 |
92 | static 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++) { | |
b623cb6a | 104 | const struct lttng_ust_ctl_enum_entry *entries_a, *entries_b; |
10b56aef MD |
105 | |
106 | entries_a = ®_enum_a->entries[i]; | |
107 | entries_b = ®_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 | } | |
130 | end: | |
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 | */ | |
139 | static 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 | ||
fc4b93fa | 152 | if (strncmp(_enum->name, key->name, LTTNG_UST_ABI_SYM_NAME_LEN)) { |
10b56aef MD |
153 | goto no_match; |
154 | } | |
155 | if (compare_enums(_enum, key)) { | |
156 | goto no_match; | |
157 | } | |
158 | ||
159 | /* Match. */ | |
160 | return 1; | |
161 | ||
162 | no_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 | */ | |
170 | static 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 | ||
188 | no_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 | */ | |
196 | static 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 | */ | |
211 | static | |
b623cb6a | 212 | int validate_event_field(struct lttng_ust_ctl_field *field, |
8494bda5 MD |
213 | const char *event_name, |
214 | struct ust_app *app) | |
215 | { | |
da860cab MD |
216 | int ret = 0; |
217 | ||
8494bda5 | 218 | switch(field->type.atype) { |
b623cb6a MJ |
219 | case lttng_ust_ctl_atype_integer: |
220 | case lttng_ust_ctl_atype_enum: | |
221 | case lttng_ust_ctl_atype_array: | |
222 | case lttng_ust_ctl_atype_sequence: | |
223 | case lttng_ust_ctl_atype_string: | |
224 | case lttng_ust_ctl_atype_variant: | |
225 | case lttng_ust_ctl_atype_array_nestable: | |
226 | case lttng_ust_ctl_atype_sequence_nestable: | |
227 | case lttng_ust_ctl_atype_enum_nestable: | |
228 | case lttng_ust_ctl_atype_variant_nestable: | |
da860cab | 229 | break; |
b623cb6a | 230 | case lttng_ust_ctl_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; | |
b623cb6a | 237 | case lttng_ust_ctl_atype_struct_nestable: |
0d32d1a9 | 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 | ||
b623cb6a | 245 | case lttng_ust_ctl_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 |
265 | end: |
266 | return ret; | |
8494bda5 MD |
267 | } |
268 | ||
269 | static | |
b623cb6a | 270 | int validate_event_fields(size_t nr_fields, struct lttng_ust_ctl_field *fields, |
8494bda5 MD |
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 | */ | |
286 | static struct ust_registry_event *alloc_event(int session_objd, | |
287 | int channel_objd, char *name, char *sig, size_t nr_fields, | |
b623cb6a | 288 | struct lttng_ust_ctl_field *fields, int loglevel_value, |
2106efa0 | 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 | |
321 | error: | |
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 | */ | |
330 | static 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 | */ | |
345 | static 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 | */ | |
362 | struct 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 | ||
387 | end: | |
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 | */ | |
401 | int ust_registry_create_event(struct ust_registry_session *session, | |
45893984 | 402 | uint64_t chan_key, int session_objd, int channel_objd, char *name, |
b623cb6a | 403 | char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields, |
2106efa0 PP |
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 |
499 | error_free: |
500 | free(sig); | |
501 | free(fields); | |
502 | free(model_emf_uri); | |
d0b96690 DG |
503 | error_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 | */ | |
513 | void 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 |
532 | static 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 | ||
541 | static 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 |
553 | static 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); | |
569 | end: | |
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 | */ | |
577 | struct 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(®_enum_lookup, 0, sizeof(reg_enum_lookup)); | |
fc4b93fa MD |
587 | strncpy(reg_enum_lookup.name, enum_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
588 | reg_enum_lookup.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; | |
10b56aef MD |
589 | reg_enum_lookup.id = enum_id; |
590 | cds_lfht_lookup(session->enums->ht, | |
591 | ht_hash_enum((void *) ®_enum_lookup, lttng_ht_seed), | |
592 | ht_match_enum_id, ®_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); | |
598 | end: | |
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 | */ | |
612 | int ust_registry_create_or_find_enum(struct ust_registry_session *session, | |
613 | int session_objd, char *enum_name, | |
b623cb6a | 614 | struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries, |
10b56aef MD |
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 | } | |
fc4b93fa MD |
642 | strncpy(reg_enum->name, enum_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
643 | reg_enum->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; | |
10b56aef MD |
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(®_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 | ®_enum->node.node); | |
669 | assert(nodep == ®_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; | |
674 | end: | |
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 | 685 | static 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 = ®_enum->node.node; | |
696 | ret = lttng_ht_del(reg_session->enums, &iter); | |
697 | assert(!ret); | |
698 | call_rcu(®_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 | */ |
707 | static | |
708 | void 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 | 725 | static 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( | |
412d7227 SM |
735 | the_notification_thread_handle, |
736 | chan->consumer_key, LTTNG_DOMAIN_UST); | |
e9404c27 JG |
737 | if (cmd_ret != LTTNG_OK) { |
738 | ERR("Failed to remove channel from notification thread"); | |
739 | } | |
740 | } | |
741 | ||
70987ead JG |
742 | if (chan->ht) { |
743 | rcu_read_lock(); | |
744 | /* Destroy all event associated with this registry. */ | |
745 | cds_lfht_for_each_entry( | |
746 | chan->ht->ht, &iter.iter, event, node.node) { | |
747 | /* Delete the node from the ht and free it. */ | |
748 | ust_registry_destroy_event(chan, event); | |
749 | } | |
750 | rcu_read_unlock(); | |
d0b96690 | 751 | } |
36b588ed | 752 | call_rcu(&chan->rcu_head, destroy_channel_rcu); |
d0b96690 DG |
753 | } |
754 | ||
755 | /* | |
756 | * Initialize registry with default values. | |
757 | */ | |
45893984 DG |
758 | int ust_registry_channel_add(struct ust_registry_session *session, |
759 | uint64_t key) | |
760 | { | |
761 | int ret = 0; | |
762 | struct ust_registry_channel *chan; | |
763 | ||
764 | assert(session); | |
765 | ||
766 | chan = zmalloc(sizeof(*chan)); | |
767 | if (!chan) { | |
768 | PERROR("zmalloc ust registry channel"); | |
769 | ret = -ENOMEM; | |
9dbcf332 | 770 | goto error_alloc; |
45893984 DG |
771 | } |
772 | ||
773 | chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
774 | if (!chan->ht) { | |
775 | ret = -ENOMEM; | |
776 | goto error; | |
777 | } | |
778 | ||
779 | /* Set custom match function. */ | |
780 | chan->ht->match_fct = ht_match_event; | |
7972aab2 DG |
781 | chan->ht->hash_fct = ht_hash_event; |
782 | ||
783 | /* | |
784 | * Assign a channel ID right now since the event notification comes | |
785 | * *before* the channel notify so the ID needs to be set at this point so | |
786 | * the metadata can be dumped for that event. | |
787 | */ | |
788 | if (ust_registry_is_max_id(session->used_channel_id)) { | |
789 | ret = -1; | |
790 | goto error; | |
791 | } | |
792 | chan->chan_id = ust_registry_get_next_chan_id(session); | |
45893984 DG |
793 | |
794 | rcu_read_lock(); | |
795 | lttng_ht_node_init_u64(&chan->node, key); | |
796 | lttng_ht_add_unique_u64(session->channels, &chan->node); | |
797 | rcu_read_unlock(); | |
798 | ||
9dbcf332 DG |
799 | return 0; |
800 | ||
45893984 | 801 | error: |
e9404c27 | 802 | destroy_channel(chan, false); |
9dbcf332 | 803 | error_alloc: |
45893984 DG |
804 | return ret; |
805 | } | |
806 | ||
807 | /* | |
808 | * Find a channel in the given registry. RCU read side lock MUST be acquired | |
809 | * before calling this function and as long as the event reference is kept by | |
810 | * the caller. | |
811 | * | |
812 | * On success, the pointer is returned else NULL. | |
813 | */ | |
814 | struct ust_registry_channel *ust_registry_channel_find( | |
815 | struct ust_registry_session *session, uint64_t key) | |
816 | { | |
817 | struct lttng_ht_node_u64 *node; | |
818 | struct lttng_ht_iter iter; | |
819 | struct ust_registry_channel *chan = NULL; | |
820 | ||
821 | assert(session); | |
822 | assert(session->channels); | |
823 | ||
7972aab2 DG |
824 | DBG3("UST registry channel finding key %" PRIu64, key); |
825 | ||
45893984 DG |
826 | lttng_ht_lookup(session->channels, &key, &iter); |
827 | node = lttng_ht_iter_get_node_u64(&iter); | |
828 | if (!node) { | |
829 | goto end; | |
830 | } | |
831 | chan = caa_container_of(node, struct ust_registry_channel, node); | |
832 | ||
833 | end: | |
834 | return chan; | |
835 | } | |
836 | ||
837 | /* | |
838 | * Remove channel using key from registry and free memory. | |
839 | */ | |
840 | void ust_registry_channel_del_free(struct ust_registry_session *session, | |
e9404c27 | 841 | uint64_t key, bool notif) |
45893984 DG |
842 | { |
843 | struct lttng_ht_iter iter; | |
844 | struct ust_registry_channel *chan; | |
9209cee7 | 845 | int ret; |
45893984 DG |
846 | |
847 | assert(session); | |
848 | ||
849 | rcu_read_lock(); | |
850 | chan = ust_registry_channel_find(session, key); | |
851 | if (!chan) { | |
9209cee7 | 852 | rcu_read_unlock(); |
45893984 DG |
853 | goto end; |
854 | } | |
855 | ||
856 | iter.iter.node = &chan->node.node; | |
9209cee7 MD |
857 | ret = lttng_ht_del(session->channels, &iter); |
858 | assert(!ret); | |
859 | rcu_read_unlock(); | |
e9404c27 | 860 | destroy_channel(chan, notif); |
45893984 DG |
861 | |
862 | end: | |
45893984 DG |
863 | return; |
864 | } | |
865 | ||
866 | /* | |
867 | * Initialize registry with default values and set the newly allocated session | |
868 | * pointer to sessionp. | |
869 | * | |
870 | * Return 0 on success and sessionp is set or else return -1 and sessionp is | |
871 | * kept untouched. | |
872 | */ | |
873 | int ust_registry_session_init(struct ust_registry_session **sessionp, | |
d0b96690 DG |
874 | struct ust_app *app, |
875 | uint32_t bits_per_long, | |
876 | uint32_t uint8_t_alignment, | |
877 | uint32_t uint16_t_alignment, | |
878 | uint32_t uint32_t_alignment, | |
879 | uint32_t uint64_t_alignment, | |
880 | uint32_t long_alignment, | |
af6142cf MD |
881 | int byte_order, |
882 | uint32_t major, | |
d7ba1388 | 883 | uint32_t minor, |
3d071855 | 884 | const char *root_shm_path, |
d7ba1388 MD |
885 | const char *shm_path, |
886 | uid_t euid, | |
8de88061 JR |
887 | gid_t egid, |
888 | uint64_t tracing_id, | |
889 | uid_t tracing_uid) | |
d0b96690 DG |
890 | { |
891 | int ret; | |
45893984 | 892 | struct ust_registry_session *session; |
d0b96690 | 893 | |
45893984 | 894 | assert(sessionp); |
d0b96690 | 895 | |
45893984 DG |
896 | session = zmalloc(sizeof(*session)); |
897 | if (!session) { | |
898 | PERROR("zmalloc ust registry session"); | |
9dbcf332 | 899 | goto error_alloc; |
45893984 | 900 | } |
d0b96690 DG |
901 | |
902 | pthread_mutex_init(&session->lock, NULL); | |
903 | session->bits_per_long = bits_per_long; | |
904 | session->uint8_t_alignment = uint8_t_alignment; | |
905 | session->uint16_t_alignment = uint16_t_alignment; | |
906 | session->uint32_t_alignment = uint32_t_alignment; | |
907 | session->uint64_t_alignment = uint64_t_alignment; | |
908 | session->long_alignment = long_alignment; | |
909 | session->byte_order = byte_order; | |
d7ba1388 | 910 | session->metadata_fd = -1; |
4628484a MD |
911 | session->uid = euid; |
912 | session->gid = egid; | |
10b56aef | 913 | session->next_enum_id = 0; |
7062f070 JD |
914 | session->major = major; |
915 | session->minor = minor; | |
3d071855 MD |
916 | strncpy(session->root_shm_path, root_shm_path, |
917 | sizeof(session->root_shm_path)); | |
918 | session->root_shm_path[sizeof(session->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
919 | if (shm_path[0]) { |
920 | strncpy(session->shm_path, shm_path, | |
921 | sizeof(session->shm_path)); | |
922 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
923 | strncpy(session->metadata_path, shm_path, | |
924 | sizeof(session->metadata_path)); | |
925 | session->metadata_path[sizeof(session->metadata_path) - 1] = '\0'; | |
926 | strncat(session->metadata_path, "/metadata", | |
927 | sizeof(session->metadata_path) | |
928 | - strlen(session->metadata_path) - 1); | |
929 | } | |
930 | if (session->shm_path[0]) { | |
931 | ret = run_as_mkdir_recursive(session->shm_path, | |
932 | S_IRWXU | S_IRWXG, | |
933 | euid, egid); | |
934 | if (ret) { | |
935 | PERROR("run_as_mkdir_recursive"); | |
936 | goto error; | |
937 | } | |
938 | } | |
939 | if (session->metadata_path[0]) { | |
940 | /* Create metadata file */ | |
4628484a | 941 | ret = run_as_open(session->metadata_path, |
d7ba1388 | 942 | O_WRONLY | O_CREAT | O_EXCL, |
4628484a | 943 | S_IRUSR | S_IWUSR, euid, egid); |
d7ba1388 MD |
944 | if (ret < 0) { |
945 | PERROR("Opening metadata file"); | |
946 | goto error; | |
947 | } | |
948 | session->metadata_fd = ret; | |
949 | } | |
d0b96690 | 950 | |
10b56aef MD |
951 | session->enums = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
952 | if (!session->enums) { | |
de64322e | 953 | ERR("Failed to create enums hash table"); |
10b56aef MD |
954 | goto error; |
955 | } | |
956 | /* hash/match functions are specified at call site. */ | |
957 | session->enums->match_fct = NULL; | |
958 | session->enums->hash_fct = NULL; | |
959 | ||
45893984 DG |
960 | session->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
961 | if (!session->channels) { | |
962 | goto error; | |
963 | } | |
964 | ||
d0b96690 DG |
965 | ret = lttng_uuid_generate(session->uuid); |
966 | if (ret) { | |
967 | ERR("Failed to generate UST uuid (errno = %d)", ret); | |
968 | goto error; | |
969 | } | |
970 | ||
8de88061 JR |
971 | session->tracing_id = tracing_id; |
972 | session->tracing_uid = tracing_uid; | |
973 | ||
d0b96690 | 974 | pthread_mutex_lock(&session->lock); |
af6142cf | 975 | ret = ust_metadata_session_statedump(session, app, major, minor); |
d0b96690 DG |
976 | pthread_mutex_unlock(&session->lock); |
977 | if (ret) { | |
978 | ERR("Failed to generate session metadata (errno = %d)", ret); | |
979 | goto error; | |
980 | } | |
981 | ||
45893984 DG |
982 | *sessionp = session; |
983 | ||
d0b96690 DG |
984 | return 0; |
985 | ||
986 | error: | |
9dbcf332 | 987 | ust_registry_session_destroy(session); |
d24ff3fd | 988 | free(session); |
9dbcf332 | 989 | error_alloc: |
d0b96690 DG |
990 | return -1; |
991 | } | |
992 | ||
993 | /* | |
994 | * Destroy session registry. This does NOT free the given pointer since it | |
995 | * might get passed as a reference. The registry lock should NOT be acquired. | |
996 | */ | |
997 | void ust_registry_session_destroy(struct ust_registry_session *reg) | |
998 | { | |
999 | int ret; | |
45893984 DG |
1000 | struct lttng_ht_iter iter; |
1001 | struct ust_registry_channel *chan; | |
10b56aef | 1002 | struct ust_registry_enum *reg_enum; |
d0b96690 | 1003 | |
286c991a MD |
1004 | if (!reg) { |
1005 | return; | |
1006 | } | |
9d8efb0e | 1007 | |
d0b96690 DG |
1008 | /* On error, EBUSY can be returned if lock. Code flow error. */ |
1009 | ret = pthread_mutex_destroy(®->lock); | |
1010 | assert(!ret); | |
1011 | ||
9d8efb0e DG |
1012 | if (reg->channels) { |
1013 | rcu_read_lock(); | |
1014 | /* Destroy all event associated with this registry. */ | |
1015 | cds_lfht_for_each_entry(reg->channels->ht, &iter.iter, chan, | |
1016 | node.node) { | |
1017 | /* Delete the node from the ht and free it. */ | |
1018 | ret = lttng_ht_del(reg->channels, &iter); | |
1019 | assert(!ret); | |
e9404c27 | 1020 | destroy_channel(chan, true); |
9d8efb0e DG |
1021 | } |
1022 | rcu_read_unlock(); | |
1023 | ht_cleanup_push(reg->channels); | |
45893984 | 1024 | } |
45893984 | 1025 | |
d0b96690 | 1026 | free(reg->metadata); |
d7ba1388 MD |
1027 | if (reg->metadata_fd >= 0) { |
1028 | ret = close(reg->metadata_fd); | |
1029 | if (ret) { | |
1030 | PERROR("close"); | |
1031 | } | |
4628484a MD |
1032 | ret = run_as_unlink(reg->metadata_path, |
1033 | reg->uid, reg->gid); | |
d7ba1388 MD |
1034 | if (ret) { |
1035 | PERROR("unlink"); | |
1036 | } | |
1037 | } | |
3d071855 MD |
1038 | if (reg->root_shm_path[0]) { |
1039 | /* | |
1040 | * Try deleting the directory hierarchy. | |
1041 | */ | |
602766ec | 1042 | (void) run_as_rmdir_recursive(reg->root_shm_path, |
f75c5439 MD |
1043 | reg->uid, reg->gid, |
1044 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); | |
3d071855 | 1045 | } |
10b56aef MD |
1046 | /* Destroy the enum hash table */ |
1047 | if (reg->enums) { | |
1048 | rcu_read_lock(); | |
1049 | /* Destroy all enum entries associated with this registry. */ | |
1050 | cds_lfht_for_each_entry(reg->enums->ht, &iter.iter, reg_enum, | |
1051 | node.node) { | |
1052 | ust_registry_destroy_enum(reg, reg_enum); | |
1053 | } | |
1054 | rcu_read_unlock(); | |
1055 | ht_cleanup_push(reg->enums); | |
1056 | } | |
d0b96690 | 1057 | } |