From: Jérémie Galarneau Date: Tue, 20 Apr 2021 02:38:49 +0000 (-0400) Subject: lttng-ctl: separate support of named/unnamed trigger registration X-Git-Tag: v2.13.0-rc1~29 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=a5c2d2a71919b8d1542b62f6d32579125cc2c8f8;p=lttng-tools.git lttng-ctl: separate support of named/unnamed trigger registration Signed-off-by: Jérémie Galarneau Change-Id: I37e78344dd14d00c617cd462914dee287e3b24bb --- diff --git a/include/lttng/trigger/trigger-internal.h b/include/lttng/trigger/trigger-internal.h index 6fba66684..c58787ceb 100644 --- a/include/lttng/trigger/trigger-internal.h +++ b/include/lttng/trigger/trigger-internal.h @@ -242,4 +242,19 @@ enum lttng_trigger_status lttng_trigger_add_action_error_query_results( struct lttng_trigger *trigger, struct lttng_error_query_results *results); +/* + * Set the trigger name. + * + * A name is optional. + * A name will be assigned on trigger registration if no name is set. + * + * The name is copied. + * + * Return LTTNG_TRIGGER_STATUS_OK on success, LTTNG_TRIGGER_STATUS_INVALID + * if invalid parameters are passed. + */ +LTTNG_HIDDEN +enum lttng_trigger_status lttng_trigger_set_name( + struct lttng_trigger *trigger, const char *name); + #endif /* LTTNG_TRIGGER_INTERNAL_H */ diff --git a/include/lttng/trigger/trigger.h b/include/lttng/trigger/trigger.h index 6490af118..921ff11be 100644 --- a/include/lttng/trigger/trigger.h +++ b/include/lttng/trigger/trigger.h @@ -9,6 +9,7 @@ #define LTTNG_TRIGGER_H #include +#include #include struct lttng_action; @@ -120,38 +121,40 @@ const struct lttng_action *lttng_trigger_get_const_action( * * Returns LTTNG_TRIGGER_STATUS_OK and a pointer to the trigger's name on * success, LTTNG_TRIGGER_STATUS_INVALID if an invalid parameter is passed, - * or LTTNG_TRIGGER_STATUS_UNSET if a name was not set prior to this call. + * or LTTNG_TRIGGER_STATUS_UNSET if the trigger is unnamed. */ extern enum lttng_trigger_status lttng_trigger_get_name( const struct lttng_trigger *trigger, const char **name); /* - * Set the trigger name. - * - * A name is optional. - * A name will be assigned on trigger registration if no name is set. - * - * The name is copied. - * - * Return LTTNG_TRIGGER_STATUS_OK on success, LTTNG_TRIGGER_STATUS_INVALID - * if invalid parameters are passed. + * Destroy (frees) a trigger object. */ -extern enum lttng_trigger_status lttng_trigger_set_name( - struct lttng_trigger *trigger, const char *name); +extern void lttng_trigger_destroy(struct lttng_trigger *trigger); /* - * Destroy (frees) a trigger object. + * Register a trigger to the session daemon with a given name. + * + * The trigger object can be destroyed after this call. + * On success, this function will set the trigger's name to `name`. + * + * Returns an LTTng status code. */ -extern void lttng_trigger_destroy(struct lttng_trigger *trigger); +extern enum lttng_error_code lttng_register_trigger_with_name( + struct lttng_trigger *trigger, + const char *name); /* - * Register a trigger to the session daemon. + * Register a trigger to the session daemon, generating a unique name for its + * owner. * * The trigger can be destroyed after this call. + * On success, this function will set the trigger's name to the generated + * name. * - * Return 0 on success, a negative LTTng error code on error. + * Returns an LTTng status code. */ -extern int lttng_register_trigger(struct lttng_trigger *trigger); +extern enum lttng_error_code lttng_register_trigger_with_automatic_name( + struct lttng_trigger *trigger); /* * Unregister a trigger from the session daemon. @@ -201,6 +204,18 @@ extern enum lttng_trigger_status lttng_triggers_get_count( */ extern void lttng_triggers_destroy(struct lttng_triggers *triggers); +/* + * Deprecated: invocations should be replaced by + * lttng_register_trigger_with_automatic_name(). + * + * Register a trigger to the session daemon. + * + * The trigger can be destroyed after this call. + * + * Return 0 on success, a negative LTTng error code on error. + */ +LTTNG_DEPRECATED("Use lttng_register_trigger_with_automatic_name") +extern int lttng_register_trigger(struct lttng_trigger *trigger); #ifdef __cplusplus } diff --git a/src/bin/lttng/commands/add_trigger.c b/src/bin/lttng/commands/add_trigger.c index 32aff65d3..ceaf3e807 100644 --- a/src/bin/lttng/commands/add_trigger.c +++ b/src/bin/lttng/commands/add_trigger.c @@ -2019,6 +2019,7 @@ int cmd_add_trigger(int argc, const char **argv) char *name = NULL; int i; char *owner_uid = NULL; + enum lttng_error_code ret_code; lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor); @@ -2176,16 +2177,6 @@ int cmd_add_trigger(int argc, const char **argv) goto error; } - if (name) { - enum lttng_trigger_status trigger_status = - lttng_trigger_set_name(trigger, name); - - if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { - ERR("Failed to set trigger name."); - goto error; - } - } - if (owner_uid) { enum lttng_trigger_status trigger_status; char *end; @@ -2204,13 +2195,20 @@ int cmd_add_trigger(int argc, const char **argv) } } - ret = lttng_register_trigger(trigger); - if (ret) { - ERR("Failed to register trigger: %s.", lttng_strerror(ret)); + if (name) { + ret_code = lttng_register_trigger_with_name(trigger, name); + } else { + ret_code = lttng_register_trigger_with_automatic_name(trigger); + } + + if (ret_code != LTTNG_OK) { + ERR("Failed to register trigger: %s.", + lttng_strerror(-ret_code)); goto error; } MSG("Trigger registered successfully."); + ret = 0; goto end; diff --git a/src/common/trigger.c b/src/common/trigger.c index 5c48609ea..d740c80c7 100644 --- a/src/common/trigger.c +++ b/src/common/trigger.c @@ -371,22 +371,24 @@ bool lttng_trigger_is_equal( return true; } +LTTNG_HIDDEN enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger, const char* name) { char *name_copy = NULL; enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; - if (!trigger || !name || - strlen(name) == 0) { + if (!trigger) { status = LTTNG_TRIGGER_STATUS_INVALID; goto end; } - name_copy = strdup(name); - if (!name_copy) { - status = LTTNG_TRIGGER_STATUS_ERROR; - goto end; + if (name) { + name_copy = strdup(name); + if (!name_copy) { + status = LTTNG_TRIGGER_STATUS_ERROR; + goto end; + } } free(trigger->name); diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 7efc6bd2e..7a38699d9 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -3082,11 +3082,14 @@ end: return ret; } -int lttng_register_trigger(struct lttng_trigger *trigger) +static +int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, + bool generate_name) { int ret; struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_REGISTER_TRIGGER, + .u.trigger.is_trigger_anonymous = !name && !generate_name, }; struct lttcomm_session_msg *message_lsm; struct lttng_payload message; @@ -3097,6 +3100,8 @@ int lttng_register_trigger(struct lttng_trigger *trigger) .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), .gid = LTTNG_OPTIONAL_INIT_UNSET, }; + const char *unused_trigger_name = NULL; + enum lttng_trigger_status trigger_status; lttng_payload_init(&message); lttng_payload_init(&reply); @@ -3106,6 +3111,21 @@ int lttng_register_trigger(struct lttng_trigger *trigger) goto end; } + trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name); + if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) { + /* Re-using already registered trigger. */ + ret = -LTTNG_ERR_INVALID; + goto end; + } + + if (name) { + trigger_status = lttng_trigger_set_name(trigger, name); + if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } + } + if (!trigger->creds.uid.is_set) { /* Use the client's credentials as the trigger credentials. */ lttng_trigger_set_credentials(trigger, &user_creds); @@ -3126,14 +3146,14 @@ int lttng_register_trigger(struct lttng_trigger *trigger) if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { if (lttng_credentials_get_uid(&user_creds) != 0) { ret = -LTTNG_ERR_EPERM; - goto end; + goto end_unset_name; } } } if (!lttng_trigger_validate(trigger)) { ret = -LTTNG_ERR_INVALID_TRIGGER; - goto end; + goto end_unset_name; } domain_type = lttng_trigger_get_underlying_domain_type_restriction( @@ -3144,13 +3164,13 @@ int lttng_register_trigger(struct lttng_trigger *trigger) ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); if (ret) { ret = -LTTNG_ERR_NOMEM; - goto end; + goto end_unset_name; } ret = lttng_trigger_serialize(trigger, &message); if (ret < 0) { ret = -LTTNG_ERR_UNK; - goto end; + goto end_unset_name; } /* @@ -3170,7 +3190,7 @@ int lttng_register_trigger(struct lttng_trigger *trigger) &message_view); ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); if (ret < 0) { - goto end; + goto end_unset_name; } } @@ -3182,18 +3202,27 @@ int lttng_register_trigger(struct lttng_trigger *trigger) ret = lttng_trigger_create_from_payload( &reply_view, &reply_trigger); if (ret < 0) { - ret = -LTTNG_ERR_FATAL; - goto end; + ret = -LTTNG_ERR_INVALID_PROTOCOL; + goto end_unset_name; } } - ret = lttng_trigger_assign_name(trigger, reply_trigger); - if (ret < 0) { - ret = -LTTNG_ERR_FATAL; - goto end; + if (name || generate_name) { + ret = lttng_trigger_assign_name(trigger, reply_trigger); + if (ret < 0) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } } ret = 0; + goto end; + +end_unset_name: + trigger_status = lttng_trigger_set_name(trigger, NULL); + if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { + ret = -LTTNG_ERR_UNK; + } end: lttng_payload_reset(&message); lttng_payload_reset(&reply); @@ -3201,6 +3230,28 @@ end: return ret; } +int lttng_register_trigger(struct lttng_trigger *trigger) +{ + /* Register an anonymous trigger. */ + return _lttng_register_trigger(trigger, NULL, false); +} + +enum lttng_error_code lttng_register_trigger_with_name( + struct lttng_trigger *trigger, const char *name) +{ + const int ret = _lttng_register_trigger(trigger, name, false); + + return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret; +} + +enum lttng_error_code lttng_register_trigger_with_automatic_name( + struct lttng_trigger *trigger) +{ + const int ret = _lttng_register_trigger(trigger, false, true); + + return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret; +} + enum lttng_error_code lttng_error_query_execute( const struct lttng_error_query *query, const struct lttng_endpoint *endpoint, diff --git a/tests/regression/tools/notification/Makefile.am b/tests/regression/tools/notification/Makefile.am index 84e75d307..b9e4b8ac2 100644 --- a/tests/regression/tools/notification/Makefile.am +++ b/tests/regression/tools/notification/Makefile.am @@ -55,6 +55,8 @@ base_client_SOURCES = base_client.c base_client_LDADD = $(LIB_LTTNG_CTL) notification_SOURCES = notification.c +# Tests the deprecated lttng_register_trigger() interface +notification_CFLAGS = -Wno-deprecated-declarations $(AM_CFLAGS) notification_LDADD = $(LIB_LTTNG_CTL) $(LIBTAP) -lm rotation_SOURCES = rotation.c diff --git a/tests/regression/tools/notification/base_client.c b/tests/regression/tools/notification/base_client.c index bb12410e8..eecdb4788 100644 --- a/tests/regression/tools/notification/base_client.c +++ b/tests/regression/tools/notification/base_client.c @@ -129,6 +129,7 @@ int main(int argc, char **argv) struct lttng_condition *condition = NULL; struct lttng_action *action = NULL; struct lttng_trigger *trigger = NULL; + enum lttng_error_code ret_code; /* * Disable buffering on stdout. @@ -257,14 +258,14 @@ int main(int argc, char **argv) goto end; } - ret = lttng_register_trigger(trigger); + ret_code = lttng_register_trigger_with_automatic_name(trigger); /* * An equivalent trigger might already be registered if an other app * registered an equivalent trigger. */ - if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) { - printf("error: %s\n", lttng_strerror(ret)); + if (ret_code != LTTNG_OK && ret_code != LTTNG_ERR_TRIGGER_EXISTS) { + printf("error: %s\n", lttng_strerror(-ret_code)); ret = 1; goto end; } diff --git a/tests/regression/tools/notification/notification.c b/tests/regression/tools/notification/notification.c index c097c1026..092ac5f1e 100644 --- a/tests/regression/tools/notification/notification.c +++ b/tests/regression/tools/notification/notification.c @@ -1449,13 +1449,12 @@ static void create_tracepoint_event_rule_trigger(const char *event_pattern, struct lttng_trigger **trigger) { enum lttng_event_rule_status event_rule_status; - enum lttng_trigger_status trigger_status; - struct lttng_action *tmp_action = NULL; struct lttng_event_rule *event_rule = NULL; struct lttng_condition *tmp_condition = NULL; struct lttng_trigger *tmp_trigger = NULL; int ret; + enum lttng_error_code ret_code; assert(event_pattern); assert(trigger_name); @@ -1518,12 +1517,8 @@ static void create_tracepoint_event_rule_trigger(const char *event_pattern, tmp_trigger = lttng_trigger_create(tmp_condition, tmp_action); ok(tmp_trigger, "Trigger object creation %s", trigger_name); - trigger_status = lttng_trigger_set_name(tmp_trigger, trigger_name); - ok(trigger_status == LTTNG_TRIGGER_STATUS_OK, - "Setting name to trigger %s", trigger_name); - - ret = lttng_register_trigger(tmp_trigger); - ok(ret == 0, "Trigger registration %s", trigger_name); + ret_code = lttng_register_trigger_with_name(tmp_trigger, trigger_name); + ok(ret_code == LTTNG_OK, "Trigger registration %s", trigger_name); lttng_event_rule_destroy(event_rule); @@ -1832,10 +1827,10 @@ static void test_kprobe_event_rule_notification( enum lttng_domain_type domain_type) { int i, ret; + enum lttng_error_code ret_code; const int notification_count = 3; enum lttng_notification_channel_status nc_status; enum lttng_event_rule_status event_rule_status; - enum lttng_trigger_status trigger_status; struct lttng_notification_channel *notification_channel = NULL; struct lttng_condition *condition = NULL; struct lttng_kernel_probe_location *location = NULL; @@ -1879,12 +1874,8 @@ static void test_kprobe_event_rule_notification( goto end; } - trigger_status = lttng_trigger_set_name(trigger, trigger_name); - ok(trigger_status == LTTNG_TRIGGER_STATUS_OK, - "Setting trigger name to '%s'", trigger_name); - - ret = lttng_register_trigger(trigger); - if (ret) { + ret_code = lttng_register_trigger_with_name(trigger, trigger_name); + if (ret_code != LTTNG_OK) { fail("Failed to register trigger with kernel probe event rule condition and notify action"); goto end; } @@ -1933,10 +1924,10 @@ static void test_uprobe_event_rule_notification( const char *test_symbol_name) { int i, ret; + enum lttng_error_code ret_code; const int notification_count = 3; enum lttng_notification_channel_status nc_status; enum lttng_event_rule_status event_rule_status; - enum lttng_trigger_status trigger_status; struct lttng_notification_channel *notification_channel = NULL; struct lttng_userspace_probe_location *probe_location = NULL; struct lttng_userspace_probe_location_lookup_method *lookup_method = @@ -1988,12 +1979,8 @@ static void test_uprobe_event_rule_notification( goto end; } - trigger_status = lttng_trigger_set_name(trigger, trigger_name); - ok(trigger_status == LTTNG_TRIGGER_STATUS_OK, - "Setting name to trigger '%s'", trigger_name); - - ret = lttng_register_trigger(trigger); - if (ret) { + ret_code = lttng_register_trigger_with_name(trigger, trigger_name); + if (ret_code != LTTNG_OK) { fail("Failed to register trigger with userspace probe event rule condition and notify action"); goto end; } @@ -2040,10 +2027,10 @@ static void test_syscall_event_rule_notification( enum lttng_domain_type domain_type) { int i, ret; + enum lttng_error_code ret_code; const int notification_count = 3; enum lttng_notification_channel_status nc_status; enum lttng_event_rule_status event_rule_status; - enum lttng_trigger_status trigger_status; struct lttng_notification_channel *notification_channel = NULL; struct lttng_condition *condition = NULL; struct lttng_event_rule *event_rule = NULL; @@ -2080,12 +2067,8 @@ static void test_syscall_event_rule_notification( goto end; } - trigger_status = lttng_trigger_set_name(trigger, trigger_name); - ok(trigger_status == LTTNG_TRIGGER_STATUS_OK, - "Setting name to trigger '%s'", trigger_name); - - ret = lttng_register_trigger(trigger); - if (ret) { + ret_code = lttng_register_trigger_with_name(trigger, trigger_name); + if (ret_code != LTTNG_OK) { fail("Failed to register trigger with syscall event rule condition and notify action"); goto end; } @@ -2129,10 +2112,10 @@ static void test_syscall_event_rule_notification_filter( enum lttng_domain_type domain_type) { int i, ret; + enum lttng_error_code ret_code; const int notification_count = 3; enum lttng_notification_channel_status nc_status; enum lttng_event_rule_status event_rule_status; - enum lttng_trigger_status trigger_status; struct lttng_notification_channel *notification_channel = NULL; struct lttng_condition *condition = NULL; struct lttng_event_rule *event_rule = NULL; @@ -2175,12 +2158,8 @@ static void test_syscall_event_rule_notification_filter( goto end; } - trigger_status = lttng_trigger_set_name(trigger, trigger_name); - ok(trigger_status == LTTNG_TRIGGER_STATUS_OK, - "Setting name to trigger '%s'", trigger_name); - - ret = lttng_register_trigger(trigger); - if (ret) { + ret_code = lttng_register_trigger_with_name(trigger, trigger_name); + if (ret_code != LTTNG_OK) { fail("Failed to register trigger with syscall filtering event rule condition and notify action"); goto end; } @@ -2509,7 +2488,7 @@ int main(int argc, const char *argv[]) switch (test_scenario) { case 1: { - plan_tests(44); + plan_tests(41); /* Test cases that need gen-ust-event testapp. */ diag("Test basic notification error paths for %s domain", @@ -2575,7 +2554,7 @@ int main(int argc, const char *argv[]) * Test cases that need a test app with more than one event * type. */ - plan_tests(25); + plan_tests(23); /* * At the moment, the only test case of this scenario is @@ -2647,7 +2626,7 @@ int main(int argc, const char *argv[]) { switch(domain_type) { case LTTNG_DOMAIN_UST: - plan_tests(222); + plan_tests(221); break; case LTTNG_DOMAIN_KERNEL: plan_tests(216); diff --git a/tests/regression/tools/notification/rotation.c b/tests/regression/tools/notification/rotation.c index b8089d5c9..f32a0d773 100644 --- a/tests/regression/tools/notification/rotation.c +++ b/tests/regression/tools/notification/rotation.c @@ -14,17 +14,8 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include #define TEST_COUNT 36 @@ -79,6 +70,7 @@ int setup_rotation_trigger(const struct session *session, struct lttng_trigger *rotation_completed_trigger = NULL; enum lttng_condition_status condition_status; enum lttng_notification_channel_status notification_channel_status; + enum lttng_error_code ret_code; notify = lttng_action_notify_create(); if (!notify) { @@ -162,17 +154,23 @@ int setup_rotation_trigger(const struct session *session, } /* Register rotation ongoing and completed triggers. */ - ret = lttng_register_trigger(rotation_ongoing_trigger); - ok(ret == 0, "Registered session rotation ongoing trigger"); - if (ret) { + ret_code = lttng_register_trigger_with_automatic_name( + rotation_ongoing_trigger); + ok(ret_code == LTTNG_OK, "Registered session rotation ongoing trigger"); + if (ret_code != LTTNG_OK) { + ret = -ret_code; goto end; } - ret = lttng_register_trigger(rotation_completed_trigger); - ok(ret == 0, "Registered session rotation completed trigger"); - if (ret) { + ret_code = lttng_register_trigger_with_automatic_name( + rotation_completed_trigger); + ok(ret_code == LTTNG_OK, + "Registered session rotation completed trigger"); + if (ret_code != LTTNG_OK) { + ret = -ret_code; goto end; } + end: lttng_trigger_destroy(rotation_ongoing_trigger); lttng_trigger_destroy(rotation_completed_trigger); diff --git a/tests/regression/tools/trigger/utils/register-some-triggers.c b/tests/regression/tools/trigger/utils/register-some-triggers.c index c92ee0f5a..f4bba2df4 100644 --- a/tests/regression/tools/trigger/utils/register-some-triggers.c +++ b/tests/regression/tools/trigger/utils/register-some-triggers.c @@ -20,14 +20,11 @@ static void register_trigger(const char *trigger_name, struct lttng_action *action) { struct lttng_trigger *trigger; - enum lttng_trigger_status trigger_status; - int ret; + enum lttng_error_code ret; trigger = lttng_trigger_create(condition, action); - trigger_status = lttng_trigger_set_name(trigger, trigger_name); - assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); - ret = lttng_register_trigger(trigger); - assert(ret == 0); + ret = lttng_register_trigger_with_name(trigger, trigger_name); + assert(ret == LTTNG_OK); } /*