From: Jérémie Galarneau Date: Fri, 17 Aug 2018 17:25:51 +0000 (-0400) Subject: Cleanup: enforce const-correctness in notification thread X-Git-Tag: v2.11.0-rc1~125 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=9b63a4aa352c184984395c097f86d5e54c295012;p=lttng-tools.git Cleanup: enforce const-correctness in notification thread Signed-off-by: Jérémie Galarneau --- diff --git a/include/lttng/action/action-internal.h b/include/lttng/action/action-internal.h index 0c862a3a2..df400d5a3 100644 --- a/include/lttng/action/action-internal.h +++ b/include/lttng/action/action-internal.h @@ -53,4 +53,8 @@ LTTNG_HIDDEN ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view, struct lttng_action **action); +LTTNG_HIDDEN +enum lttng_action_type lttng_action_get_type_const( + const struct lttng_action *action); + #endif /* LTTNG_ACTION_INTERNAL_H */ diff --git a/include/lttng/condition/evaluation-internal.h b/include/lttng/condition/evaluation-internal.h index b88a0f712..45a72daa0 100644 --- a/include/lttng/condition/evaluation-internal.h +++ b/include/lttng/condition/evaluation-internal.h @@ -26,7 +26,8 @@ #include typedef void (*evaluation_destroy_cb)(struct lttng_evaluation *evaluation); -typedef int (*evaluation_serialize_cb)(struct lttng_evaluation *evaluation, +typedef int (*evaluation_serialize_cb)( + const struct lttng_evaluation *evaluation, struct lttng_dynamic_buffer *buf); struct lttng_evaluation_comm { @@ -50,7 +51,7 @@ ssize_t lttng_evaluation_create_from_buffer(const struct lttng_buffer_view *view struct lttng_evaluation **evaluation); LTTNG_HIDDEN -int lttng_evaluation_serialize(struct lttng_evaluation *evaluation, +int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, struct lttng_dynamic_buffer *buf); #endif /* LTTNG_EVALUATION_INTERNAL_H */ diff --git a/include/lttng/notification/notification-internal.h b/include/lttng/notification/notification-internal.h index 648977f33..65b036722 100644 --- a/include/lttng/notification/notification-internal.h +++ b/include/lttng/notification/notification-internal.h @@ -29,21 +29,6 @@ struct lttng_notification { struct lttng_condition *condition; struct lttng_evaluation *evaluation; - /* - * The ownership of the notification's inner-elements depends - * on the way it was created. The notification owns both - * the condition and evaluation if it was obtained from a notification - * channel (i.e. created using lttng_notification_create_from_buffer) - * as the user may never access the condition and evaluation, - * thus never getting a chance to free them. - * - * However, when the _private_ lttng_notification_create() function - * is used, no ownership of condition and evaluation is assumed by - * the notification object. The main reason for this change in - * behavior is that internal users of this API only use the object - * to use its serialization facilities. - */ - bool owns_elements; }; struct lttng_notification_comm { @@ -59,7 +44,7 @@ struct lttng_notification *lttng_notification_create( struct lttng_evaluation *evaluation); LTTNG_HIDDEN -int lttng_notification_serialize(struct lttng_notification *notification, +int lttng_notification_serialize(const struct lttng_notification *notification, struct lttng_dynamic_buffer *buf); LTTNG_HIDDEN diff --git a/include/lttng/trigger/trigger-internal.h b/include/lttng/trigger/trigger-internal.h index 22975bf41..8c44f5cdf 100644 --- a/include/lttng/trigger/trigger-internal.h +++ b/include/lttng/trigger/trigger-internal.h @@ -46,6 +46,14 @@ LTTNG_HIDDEN int lttng_trigger_serialize(struct lttng_trigger *trigger, struct lttng_dynamic_buffer *buf); +LTTNG_HIDDEN +const struct lttng_condition *lttng_trigger_get_const_condition( + const struct lttng_trigger *trigger); + +LTTNG_HIDDEN +const struct lttng_action *lttng_trigger_get_const_action( + const struct lttng_trigger *trigger); + LTTNG_HIDDEN bool lttng_trigger_validate(struct lttng_trigger *trigger); diff --git a/src/bin/lttng-sessiond/notification-thread-events.c b/src/bin/lttng-sessiond/notification-thread-events.c index 0f43e380b..98757737d 100644 --- a/src/bin/lttng-sessiond/notification-thread-events.c +++ b/src/bin/lttng-sessiond/notification-thread-events.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include @@ -166,8 +166,8 @@ static int evaluate_condition(const struct lttng_condition *condition, uint64_t latest_session_consumed_total, struct channel_info *channel_info); static -int send_evaluation_to_clients(struct lttng_trigger *trigger, - struct lttng_evaluation *evaluation, +int send_evaluation_to_clients(const struct lttng_trigger *trigger, + const struct lttng_evaluation *evaluation, struct notification_client_list *client_list, struct notification_thread_state *state, uid_t channel_uid, gid_t channel_gid); @@ -292,7 +292,7 @@ int match_client_list_condition(struct cds_lfht_node *node, const void *key) static unsigned long lttng_condition_buffer_usage_hash( - struct lttng_condition *_condition) + const struct lttng_condition *_condition) { unsigned long hash = 0; struct lttng_condition_buffer_usage *condition; @@ -327,7 +327,7 @@ unsigned long lttng_condition_buffer_usage_hash( static unsigned long lttng_condition_session_consumed_size_hash( - struct lttng_condition *_condition) + const struct lttng_condition *_condition) { unsigned long hash = 0; struct lttng_condition_session_consumed_size *condition; @@ -350,7 +350,7 @@ unsigned long lttng_condition_session_consumed_size_hash( * don't want to link in liblttng-ctl. */ static -unsigned long lttng_condition_hash(struct lttng_condition *condition) +unsigned long lttng_condition_hash(const struct lttng_condition *condition) { switch (condition->type) { case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: @@ -547,8 +547,8 @@ int evaluate_condition_for_client(struct lttng_trigger *trigger, struct lttng_trigger_list_element *element; cds_list_for_each_entry(element, &channel_trigger_list->list, node) { - struct lttng_condition *current_condition = - lttng_trigger_get_condition( + const struct lttng_condition *current_condition = + lttng_trigger_get_const_condition( element->trigger); assert(current_condition); @@ -1027,7 +1027,7 @@ struct session_info *find_or_create_session_info( } cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed), - &sessions->sessions_ht_node); + &session->sessions_ht_node); end: rcu_read_unlock(); return session; @@ -1494,8 +1494,8 @@ int handle_notification_thread_command_unregister_trigger( cds_list_for_each_entry_safe(trigger_element, tmp, &trigger_list->list, node) { - struct lttng_condition *current_condition = - lttng_trigger_get_condition( + const struct lttng_condition *current_condition = + lttng_trigger_get_const_condition( trigger_element->trigger); assert(current_condition); @@ -2378,8 +2378,8 @@ int client_enqueue_dropped_notification(struct notification_client *client, } static -int send_evaluation_to_clients(struct lttng_trigger *trigger, - struct lttng_evaluation *evaluation, +int send_evaluation_to_clients(const struct lttng_trigger *trigger, + const struct lttng_evaluation *evaluation, struct notification_client_list* client_list, struct notification_thread_state *state, uid_t channel_uid, gid_t channel_gid) @@ -2387,30 +2387,23 @@ int send_evaluation_to_clients(struct lttng_trigger *trigger, int ret = 0; struct lttng_dynamic_buffer msg_buffer; struct notification_client_list_element *client_list_element, *tmp; - struct lttng_notification *notification; - struct lttng_condition *condition; + const struct lttng_notification notification = { + .condition = (struct lttng_condition *) lttng_trigger_get_const_condition(trigger), + .evaluation = (struct lttng_evaluation *) evaluation, + }; struct lttng_notification_channel_message msg_header = { .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION, }; lttng_dynamic_buffer_init(&msg_buffer); - condition = lttng_trigger_get_condition(trigger); - assert(condition); - - notification = lttng_notification_create(condition, evaluation); - if (!notification) { - ret = -1; - goto end; - } - ret = lttng_dynamic_buffer_append(&msg_buffer, &msg_header, sizeof(msg_header)); if (ret) { goto end; } - ret = lttng_notification_serialize(notification, &msg_buffer); + ret = lttng_notification_serialize(¬ification, &msg_buffer); if (ret) { ERR("[notification-thread] Failed to serialize notification"); ret = -1; @@ -2470,7 +2463,6 @@ int send_evaluation_to_clients(struct lttng_trigger *trigger, } ret = 0; end: - lttng_notification_destroy(notification); lttng_dynamic_buffer_reset(&msg_buffer); return ret; } @@ -2611,19 +2603,19 @@ int handle_notification_thread_channel_sample( channel_triggers_ht_node); cds_list_for_each_entry(trigger_list_element, &trigger_list->list, node) { - struct lttng_condition *condition; - struct lttng_action *action; - struct lttng_trigger *trigger; + const struct lttng_condition *condition; + const struct lttng_action *action; + const struct lttng_trigger *trigger; struct notification_client_list *client_list; struct lttng_evaluation *evaluation = NULL; trigger = trigger_list_element->trigger; - condition = lttng_trigger_get_condition(trigger); + condition = lttng_trigger_get_const_condition(trigger); assert(condition); - action = lttng_trigger_get_action(trigger); + action = lttng_trigger_get_const_action(trigger); /* Notify actions are the only type currently supported. */ - assert(lttng_action_get_type(action) == + assert(lttng_action_get_type_const(action) == LTTNG_ACTION_TYPE_NOTIFY); /* diff --git a/src/common/action.c b/src/common/action.c index 0ce93c852..617bc2806 100644 --- a/src/common/action.c +++ b/src/common/action.c @@ -25,6 +25,13 @@ enum lttng_action_type lttng_action_get_type(struct lttng_action *action) return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; } +LTTNG_HIDDEN +enum lttng_action_type lttng_action_get_type_const( + const struct lttng_action *action) +{ + return action->type; +} + void lttng_action_destroy(struct lttng_action *action) { if (!action) { diff --git a/src/common/buffer-usage.c b/src/common/buffer-usage.c index dbfae89c8..e19a875c9 100644 --- a/src/common/buffer-usage.c +++ b/src/common/buffer-usage.c @@ -739,7 +739,7 @@ end: static int lttng_evaluation_buffer_usage_serialize( - struct lttng_evaluation *evaluation, + const struct lttng_evaluation *evaluation, struct lttng_dynamic_buffer *buf) { struct lttng_evaluation_buffer_usage *usage; diff --git a/src/common/evaluation.c b/src/common/evaluation.c index 91d5c0b1d..e23919302 100644 --- a/src/common/evaluation.c +++ b/src/common/evaluation.c @@ -32,7 +32,7 @@ void lttng_evaluation_init(struct lttng_evaluation *evaluation, } LTTNG_HIDDEN -int lttng_evaluation_serialize(struct lttng_evaluation *evaluation, +int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, struct lttng_dynamic_buffer *buf) { int ret; diff --git a/src/common/notification.c b/src/common/notification.c index 3826534cd..674429692 100644 --- a/src/common/notification.c +++ b/src/common/notification.c @@ -40,13 +40,12 @@ struct lttng_notification *lttng_notification_create( notification->condition = condition; notification->evaluation = evaluation; - notification->owns_elements = false; end: return notification; } LTTNG_HIDDEN -int lttng_notification_serialize(struct lttng_notification *notification, +int lttng_notification_serialize(const struct lttng_notification *notification, struct lttng_dynamic_buffer *buf) { int ret; @@ -134,7 +133,6 @@ ssize_t lttng_notification_create_from_buffer( goto error; } ret = notification_size; - (*notification)->owns_elements = true; end: return ret; error: @@ -149,10 +147,8 @@ void lttng_notification_destroy(struct lttng_notification *notification) return; } - if (notification->owns_elements) { - lttng_condition_destroy(notification->condition); - lttng_evaluation_destroy(notification->evaluation); - } + lttng_condition_destroy(notification->condition); + lttng_evaluation_destroy(notification->evaluation); free(notification); } diff --git a/src/common/session-consumed-size.c b/src/common/session-consumed-size.c index 28bc7ff3b..6ba64249d 100644 --- a/src/common/session-consumed-size.c +++ b/src/common/session-consumed-size.c @@ -402,7 +402,7 @@ end: static int lttng_evaluation_session_consumed_size_serialize( - struct lttng_evaluation *evaluation, + const struct lttng_evaluation *evaluation, struct lttng_dynamic_buffer *buf) { struct lttng_evaluation_session_consumed_size *consumed; diff --git a/src/common/trigger.c b/src/common/trigger.c index 1de1b3b49..a4130ec31 100644 --- a/src/common/trigger.c +++ b/src/common/trigger.c @@ -64,12 +64,26 @@ struct lttng_condition *lttng_trigger_get_condition( return trigger ? trigger->condition : NULL; } +LTTNG_HIDDEN +const struct lttng_condition *lttng_trigger_get_const_condition( + const struct lttng_trigger *trigger) +{ + return trigger->condition; +} + struct lttng_action *lttng_trigger_get_action( struct lttng_trigger *trigger) { return trigger ? trigger->action : NULL; } +LTTNG_HIDDEN +const struct lttng_action *lttng_trigger_get_const_action( + const struct lttng_trigger *trigger) +{ + return trigger->action; +} + void lttng_trigger_destroy(struct lttng_trigger *trigger) { if (!trigger) {