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 "common/mi-lttng.h"
11 #include <lttng/lttng.h>
14 #ifdef LTTNG_EMBED_HELP
15 static const char help_msg
[] =
16 #include <lttng-remove-trigger.1.h>
27 struct argpar_opt_descr remove_trigger_options
[] = {
28 { OPT_HELP
, 'h', "help", false },
29 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
30 { OPT_OWNER_UID
, '\0', "owner-uid", true },
31 ARGPAR_OPT_DESCR_SENTINEL
,
35 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
40 ERR("Duplicate option '%s' given.", opt_name
);
46 ERR("Failed to allocate '%s' string.", opt_name
);
60 int cmd_remove_trigger(int argc
, const char **argv
)
62 enum lttng_error_code ret_code
;
64 struct argpar_parse_ret argpar_parse_ret
= {};
65 const char *name
= NULL
;
67 struct lttng_triggers
*triggers
= NULL
;
68 unsigned int triggers_count
;
69 enum lttng_trigger_status trigger_status
;
70 const struct lttng_trigger
*trigger_to_remove
= NULL
;
71 char *owner_uid
= NULL
;
73 struct mi_writer
*mi_writer
= NULL
;
76 mi_writer
= mi_lttng_writer_create(
77 fileno(stdout
), lttng_opt_mi
);
83 /* Open command element. */
84 ret
= mi_lttng_writer_command_open(mi_writer
,
85 mi_lttng_element_command_remove_trigger
);
91 /* Open output element. */
92 ret
= mi_lttng_writer_open_element(
93 mi_writer
, mi_lttng_element_command_output
);
100 argpar_parse_ret
= argpar_parse(argc
- 1, argv
+ 1,
101 remove_trigger_options
, true);
102 if (!argpar_parse_ret
.items
) {
103 ERR("%s", argpar_parse_ret
.error
);
107 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
108 const struct argpar_item
*item
=
109 argpar_parse_ret
.items
->items
[i
];
111 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
112 const struct argpar_item_opt
*item_opt
=
113 (const struct argpar_item_opt
*) item
;
115 switch (item_opt
->descr
->id
) {
120 case OPT_LIST_OPTIONS
:
121 list_cmd_options_argpar(stdout
,
122 remove_trigger_options
);
127 if (!assign_string(&owner_uid
, item_opt
->arg
,
137 const struct argpar_item_non_opt
*item_non_opt
=
138 (const struct argpar_item_non_opt
*) item
;
141 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
145 name
= item_non_opt
->arg
;
150 ERR("Missing `name` argument.");
158 uid
= strtol(owner_uid
, &end
, 10);
159 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
160 ERR("Failed to parse `%s` as an integer.", owner_uid
);
166 ret
= lttng_list_triggers(&triggers
);
167 if (ret
!= LTTNG_OK
) {
168 ERR("Failed to get the list of triggers.");
172 trigger_status
= lttng_triggers_get_count(triggers
, &triggers_count
);
173 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
175 for (i
= 0; i
< triggers_count
; i
++) {
176 const struct lttng_trigger
*trigger
;
177 const char *trigger_name
;
180 trigger
= lttng_triggers_get_at_index(triggers
, i
);
181 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
182 switch (trigger_status
) {
183 case LTTNG_TRIGGER_STATUS_OK
:
185 case LTTNG_TRIGGER_STATUS_UNSET
:
186 /* Don't compare against anonymous triggers. */
192 trigger_status
= lttng_trigger_get_owner_uid(
193 trigger
, &trigger_uid
);
194 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
196 if (trigger_uid
== uid
&& strcmp(trigger_name
, name
) == 0) {
197 trigger_to_remove
= trigger
;
202 if (!trigger_to_remove
) {
203 ERR("Couldn't find trigger with name `%s`.", name
);
207 ret
= lttng_unregister_trigger(trigger_to_remove
);
209 ERR("Failed to unregister trigger `%s`.", name
);
214 ret_code
= lttng_trigger_mi_serialize(
215 trigger_to_remove
, mi_writer
, NULL
);
216 if (ret_code
!= LTTNG_OK
) {
220 MSG("Removed trigger `%s`.", name
);
230 if (lttng_opt_mi
&& mi_writer
) {
231 /* Close output element. */
232 int mi_ret
= mi_lttng_writer_close_element(mi_writer
);
238 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
239 mi_lttng_element_command_success
, ret
? 0 : 1);
245 /* Command element close. */
246 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
254 argpar_parse_ret_fini(&argpar_parse_ret
);
255 lttng_triggers_destroy(triggers
);
258 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
259 /* Preserve original error code. */
260 ret
= ret
? ret
: CMD_ERROR
;