2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <common/common.h>
23 #include <common/hashtable/utils.h>
24 #include <lttng/lttng.h>
26 #include "ust-registry.h"
31 * Hash table match function for event in the registry.
33 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
35 struct ust_registry_event
*event
;
36 const struct ust_registry_event
*key
;
41 event
= caa_container_of(node
, struct ust_registry_event
, node
.node
);
45 /* It has to be a perfect match. */
46 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
))) {
50 /* It has to be a perfect match. */
51 if (strncmp(event
->signature
, key
->signature
,
52 strlen(event
->signature
))) {
63 static unsigned long ht_hash_event(void *_key
, unsigned long seed
)
66 struct ust_registry_event
*key
= _key
;
70 xored_key
= (uint64_t) (hash_key_str(key
->name
, seed
) ^
71 hash_key_str(key
->signature
, seed
));
73 return hash_key_u64(&xored_key
, seed
);
77 * Return negative value on error, 0 if OK.
79 * TODO: we could add stricter verification of more types to catch
80 * errors in liblttng-ust implementation earlier than consumption by the
84 int validate_event_field(struct ustctl_field
*field
,
85 const char *event_name
,
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
:
96 case ustctl_atype_float
:
97 switch (field
->type
.u
.basic
._float
.mant_dig
) {
99 WARN("UST application '%s' (pid: %d) has unknown float mantissa '%u' "
100 "in field '%s', rejecting event '%s'",
102 field
->type
.u
.basic
._float
.mant_dig
,
118 int validate_event_fields(size_t nr_fields
, struct ustctl_field
*fields
,
119 const char *event_name
, struct ust_app
*app
)
123 for (i
= 0; i
< nr_fields
; i
++) {
124 if (validate_event_field(&fields
[i
], event_name
, app
) < 0)
131 * Allocate event and initialize it. This does NOT set a valid event id from a
134 static struct ust_registry_event
*alloc_event(int session_objd
,
135 int channel_objd
, char *name
, char *sig
, size_t nr_fields
,
136 struct ustctl_field
*fields
, int loglevel_value
,
137 char *model_emf_uri
, struct ust_app
*app
)
139 struct ust_registry_event
*event
= NULL
;
142 * Ensure that the field content is valid.
144 if (validate_event_fields(nr_fields
, fields
, name
, app
) < 0) {
148 event
= zmalloc(sizeof(*event
));
150 PERROR("zmalloc ust registry event");
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_value
= loglevel_value
;
161 event
->model_emf_uri
= model_emf_uri
;
163 /* Copy event name and force NULL byte. */
164 strncpy(event
->name
, name
, sizeof(event
->name
));
165 event
->name
[sizeof(event
->name
) - 1] = '\0';
167 cds_lfht_node_init(&event
->node
.node
);
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.
178 static void destroy_event(struct ust_registry_event
*event
)
185 free(event
->model_emf_uri
);
186 free(event
->signature
);
191 * Destroy event function call of the call RCU.
193 static void destroy_event_rcu(struct rcu_head
*head
)
195 struct lttng_ht_node_u64
*node
=
196 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
197 struct ust_registry_event
*event
=
198 caa_container_of(node
, struct ust_registry_event
, node
);
200 destroy_event(event
);
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.
208 * On success, the event pointer is returned else NULL.
210 struct ust_registry_event
*ust_registry_find_event(
211 struct ust_registry_channel
*chan
, char *name
, char *sig
)
213 struct lttng_ht_node_u64
*node
;
214 struct lttng_ht_iter iter
;
215 struct ust_registry_event
*event
= NULL
;
216 struct ust_registry_event key
;
222 /* Setup key for the match function. */
223 strncpy(key
.name
, name
, sizeof(key
.name
));
224 key
.name
[sizeof(key
.name
) - 1] = '\0';
227 cds_lfht_lookup(chan
->ht
->ht
, chan
->ht
->hash_fct(&key
, lttng_ht_seed
),
228 chan
->ht
->match_fct
, &key
, &iter
.iter
);
229 node
= lttng_ht_iter_get_node_u64(&iter
);
233 event
= caa_container_of(node
, struct ust_registry_event
, node
);
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
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.
247 * Should be called with session registry mutex held.
249 int ust_registry_create_event(struct ust_registry_session
*session
,
250 uint64_t chan_key
, int session_objd
, int channel_objd
, char *name
,
251 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
252 int loglevel_value
, char *model_emf_uri
, int buffer_type
,
253 uint32_t *event_id_p
, struct ust_app
*app
)
257 struct cds_lfht_node
*nptr
;
258 struct ust_registry_event
*event
= NULL
;
259 struct ust_registry_channel
*chan
;
269 * This should not happen but since it comes from the UST tracer, an
270 * external party, don't assert and simply validate values.
272 if (session_objd
< 0 || channel_objd
< 0) {
277 chan
= ust_registry_channel_find(session
, chan_key
);
283 /* Check if we've reached the maximum possible id. */
284 if (ust_registry_is_max_id(chan
->used_event_id
)) {
289 event
= alloc_event(session_objd
, channel_objd
, name
, sig
, nr_fields
,
290 fields
, loglevel_value
, model_emf_uri
, app
);
296 DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
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
);
302 * This is an add unique with a custom match function for event. The node
303 * are matched using the event name and signature.
305 nptr
= cds_lfht_add_unique(chan
->ht
->ht
, chan
->ht
->hash_fct(event
,
306 lttng_ht_seed
), chan
->ht
->match_fct
, event
, &event
->node
.node
);
307 if (nptr
!= &event
->node
.node
) {
308 if (buffer_type
== LTTNG_BUFFER_PER_UID
) {
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
314 destroy_event(event
);
315 event
= caa_container_of(nptr
, struct ust_registry_event
,
318 event_id
= event
->id
;
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
);
328 /* Request next event id if the node was successfully added. */
329 event_id
= event
->id
= ust_registry_get_next_event_id(chan
);
332 *event_id_p
= event_id
;
334 if (!event
->metadata_dumped
) {
335 /* Append to metadata */
336 ret
= ust_metadata_event_statedump(session
, chan
, event
);
338 ERR("Error appending event metadata (errno = %d)", ret
);
353 destroy_event(event
);
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.
361 void ust_registry_destroy_event(struct ust_registry_channel
*chan
,
362 struct ust_registry_event
*event
)
365 struct lttng_ht_iter iter
;
370 /* Delete the node first. */
371 iter
.iter
.node
= &event
->node
.node
;
372 ret
= lttng_ht_del(chan
->ht
, &iter
);
375 call_rcu(&event
->node
.head
, destroy_event_rcu
);
381 * We need to execute ht_destroy outside of RCU read-side critical
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().
387 void destroy_channel_rcu(struct rcu_head
*head
)
389 struct ust_registry_channel
*chan
=
390 caa_container_of(head
, struct ust_registry_channel
, rcu_head
);
393 ht_cleanup_push(chan
->ht
);
395 free(chan
->ctx_fields
);
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.
404 static void destroy_channel(struct ust_registry_channel
*chan
)
406 struct lttng_ht_iter iter
;
407 struct ust_registry_event
*event
;
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
);
418 call_rcu(&chan
->rcu_head
, destroy_channel_rcu
);
422 * Initialize registry with default values.
424 int ust_registry_channel_add(struct ust_registry_session
*session
,
428 struct ust_registry_channel
*chan
;
432 chan
= zmalloc(sizeof(*chan
));
434 PERROR("zmalloc ust registry channel");
439 chan
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
445 /* Set custom match function. */
446 chan
->ht
->match_fct
= ht_match_event
;
447 chan
->ht
->hash_fct
= ht_hash_event
;
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.
454 if (ust_registry_is_max_id(session
->used_channel_id
)) {
458 chan
->chan_id
= ust_registry_get_next_chan_id(session
);
461 lttng_ht_node_init_u64(&chan
->node
, key
);
462 lttng_ht_add_unique_u64(session
->channels
, &chan
->node
);
468 destroy_channel(chan
);
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
478 * On success, the pointer is returned else NULL.
480 struct ust_registry_channel
*ust_registry_channel_find(
481 struct ust_registry_session
*session
, uint64_t key
)
483 struct lttng_ht_node_u64
*node
;
484 struct lttng_ht_iter iter
;
485 struct ust_registry_channel
*chan
= NULL
;
488 assert(session
->channels
);
490 DBG3("UST registry channel finding key %" PRIu64
, key
);
492 lttng_ht_lookup(session
->channels
, &key
, &iter
);
493 node
= lttng_ht_iter_get_node_u64(&iter
);
497 chan
= caa_container_of(node
, struct ust_registry_channel
, node
);
504 * Remove channel using key from registry and free memory.
506 void ust_registry_channel_del_free(struct ust_registry_session
*session
,
509 struct lttng_ht_iter iter
;
510 struct ust_registry_channel
*chan
;
516 chan
= ust_registry_channel_find(session
, key
);
522 iter
.iter
.node
= &chan
->node
.node
;
523 ret
= lttng_ht_del(session
->channels
, &iter
);
526 destroy_channel(chan
);
533 * Initialize registry with default values and set the newly allocated session
534 * pointer to sessionp.
536 * Return 0 on success and sessionp is set or else return -1 and sessionp is
539 int ust_registry_session_init(struct ust_registry_session
**sessionp
,
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
,
550 const char *root_shm_path
,
551 const char *shm_path
,
556 struct ust_registry_session
*session
;
560 session
= zmalloc(sizeof(*session
));
562 PERROR("zmalloc ust registry session");
566 pthread_mutex_init(&session
->lock
, NULL
);
567 session
->bits_per_long
= bits_per_long
;
568 session
->uint8_t_alignment
= uint8_t_alignment
;
569 session
->uint16_t_alignment
= uint16_t_alignment
;
570 session
->uint32_t_alignment
= uint32_t_alignment
;
571 session
->uint64_t_alignment
= uint64_t_alignment
;
572 session
->long_alignment
= long_alignment
;
573 session
->byte_order
= byte_order
;
574 session
->metadata_fd
= -1;
577 strncpy(session
->root_shm_path
, root_shm_path
,
578 sizeof(session
->root_shm_path
));
579 session
->root_shm_path
[sizeof(session
->root_shm_path
) - 1] = '\0';
581 strncpy(session
->shm_path
, shm_path
,
582 sizeof(session
->shm_path
));
583 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
584 strncpy(session
->metadata_path
, shm_path
,
585 sizeof(session
->metadata_path
));
586 session
->metadata_path
[sizeof(session
->metadata_path
) - 1] = '\0';
587 strncat(session
->metadata_path
, "/metadata",
588 sizeof(session
->metadata_path
)
589 - strlen(session
->metadata_path
) - 1);
591 if (session
->shm_path
[0]) {
592 ret
= run_as_mkdir_recursive(session
->shm_path
,
596 PERROR("run_as_mkdir_recursive");
600 if (session
->metadata_path
[0]) {
601 /* Create metadata file */
602 ret
= run_as_open(session
->metadata_path
,
603 O_WRONLY
| O_CREAT
| O_EXCL
,
604 S_IRUSR
| S_IWUSR
, euid
, egid
);
606 PERROR("Opening metadata file");
609 session
->metadata_fd
= ret
;
612 session
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
613 if (!session
->channels
) {
617 ret
= lttng_uuid_generate(session
->uuid
);
619 ERR("Failed to generate UST uuid (errno = %d)", ret
);
623 pthread_mutex_lock(&session
->lock
);
624 ret
= ust_metadata_session_statedump(session
, app
, major
, minor
);
625 pthread_mutex_unlock(&session
->lock
);
627 ERR("Failed to generate session metadata (errno = %d)", ret
);
636 ust_registry_session_destroy(session
);
643 * Destroy session registry. This does NOT free the given pointer since it
644 * might get passed as a reference. The registry lock should NOT be acquired.
646 void ust_registry_session_destroy(struct ust_registry_session
*reg
)
649 struct lttng_ht_iter iter
;
650 struct ust_registry_channel
*chan
;
656 /* On error, EBUSY can be returned if lock. Code flow error. */
657 ret
= pthread_mutex_destroy(®
->lock
);
662 /* Destroy all event associated with this registry. */
663 cds_lfht_for_each_entry(reg
->channels
->ht
, &iter
.iter
, chan
,
665 /* Delete the node from the ht and free it. */
666 ret
= lttng_ht_del(reg
->channels
, &iter
);
668 destroy_channel(chan
);
671 ht_cleanup_push(reg
->channels
);
675 if (reg
->metadata_fd
>= 0) {
676 ret
= close(reg
->metadata_fd
);
680 ret
= run_as_unlink(reg
->metadata_path
,
686 if (reg
->root_shm_path
[0]) {
688 * Try deleting the directory hierarchy.
690 (void) run_as_recursive_rmdir(reg
->root_shm_path
,
This page took 0.065314 seconds and 4 git commands to generate.