2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "../command.h"
9 #include "common/argpar/argpar.h"
10 #include <lttng/lttng.h>
13 #ifdef LTTNG_EMBED_HELP
14 static const char help_msg
[] =
15 #include <lttng-remove-trigger.1.h>
26 struct argpar_opt_descr remove_trigger_options
[] = {
27 { OPT_HELP
, 'h', "help", false },
28 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
29 { OPT_OWNER_UID
, '\0', "owner-uid", true },
30 ARGPAR_OPT_DESCR_SENTINEL
,
34 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
39 ERR("Duplicate option '%s' given.", opt_name
);
45 ERR("Failed to allocate '%s' string.", opt_name
);
59 int cmd_remove_trigger(int argc
, const char **argv
)
62 struct argpar_parse_ret argpar_parse_ret
= {};
63 const char *name
= NULL
;
65 struct lttng_triggers
*triggers
= NULL
;
66 unsigned int triggers_count
;
67 enum lttng_trigger_status trigger_status
;
68 const struct lttng_trigger
*trigger_to_remove
= NULL
;
69 char *owner_uid
= NULL
;
72 argpar_parse_ret
= argpar_parse(argc
- 1, argv
+ 1,
73 remove_trigger_options
, true);
74 if (!argpar_parse_ret
.items
) {
75 ERR("%s", argpar_parse_ret
.error
);
79 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
80 const struct argpar_item
*item
=
81 argpar_parse_ret
.items
->items
[i
];
83 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
84 const struct argpar_item_opt
*item_opt
=
85 (const struct argpar_item_opt
*) item
;
87 switch (item_opt
->descr
->id
) {
92 case OPT_LIST_OPTIONS
:
93 list_cmd_options_argpar(stdout
,
94 remove_trigger_options
);
99 if (!assign_string(&owner_uid
, item_opt
->arg
,
109 const struct argpar_item_non_opt
*item_non_opt
=
110 (const struct argpar_item_non_opt
*) item
;
113 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
117 name
= item_non_opt
->arg
;
122 ERR("Missing `name` argument.");
130 uid
= strtol(owner_uid
, &end
, 10);
131 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
132 ERR("Failed to parse `%s` as an integer.", owner_uid
);
138 ret
= lttng_list_triggers(&triggers
);
139 if (ret
!= LTTNG_OK
) {
140 ERR("Failed to get the list of triggers.");
144 trigger_status
= lttng_triggers_get_count(triggers
, &triggers_count
);
145 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
147 for (i
= 0; i
< triggers_count
; i
++) {
148 const struct lttng_trigger
*trigger
;
149 const char *trigger_name
;
152 trigger
= lttng_triggers_get_at_index(triggers
, i
);
153 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
154 switch (trigger_status
) {
155 case LTTNG_TRIGGER_STATUS_OK
:
157 case LTTNG_TRIGGER_STATUS_UNSET
:
158 /* Don't compare against anonymous triggers. */
164 trigger_status
= lttng_trigger_get_owner_uid(
165 trigger
, &trigger_uid
);
166 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
168 if (trigger_uid
== uid
&& strcmp(trigger_name
, name
) == 0) {
169 trigger_to_remove
= trigger
;
174 if (!trigger_to_remove
) {
175 ERR("Couldn't find trigger with name `%s`.", name
);
179 ret
= lttng_unregister_trigger(trigger_to_remove
);
181 ERR("Failed to unregister trigger `%s`.", name
);
185 MSG("Removed trigger `%s`.", name
);
194 argpar_parse_ret_fini(&argpar_parse_ret
);
195 lttng_triggers_destroy(triggers
);