2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "../command.hpp"
9 #include "common/argpar-utils/argpar-utils.hpp"
10 #include "common/argpar/argpar.h"
11 #include "common/mi-lttng.hpp"
13 #include <lttng/lttng.h>
17 #ifdef LTTNG_EMBED_HELP
18 static const char help_msg
[] =
19 #include <lttng-remove-trigger.1.h>
29 static const struct argpar_opt_descr remove_trigger_options
[] = {
30 { OPT_HELP
, 'h', "help", false },
31 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
32 { OPT_OWNER_UID
, '\0', "owner-uid", true },
33 ARGPAR_OPT_DESCR_SENTINEL
,
36 static bool assign_string(char **dest
, const char *src
, const char *opt_name
)
41 ERR("Duplicate option '%s' given.", opt_name
);
47 ERR("Failed to allocate '%s' string.", opt_name
);
61 int cmd_remove_trigger(int argc
, const char **argv
)
63 enum lttng_error_code ret_code
;
65 struct argpar_iter
*argpar_iter
= nullptr;
66 const struct argpar_item
*argpar_item
= nullptr;
67 const char *name
= nullptr;
69 struct lttng_triggers
*triggers
= nullptr;
70 unsigned int triggers_count
;
71 enum lttng_trigger_status trigger_status
;
72 const struct lttng_trigger
*trigger_to_remove
= nullptr;
73 char *owner_uid
= nullptr;
75 struct mi_writer
*mi_writer
= nullptr;
79 mi_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
85 /* Open command element. */
86 ret
= mi_lttng_writer_command_open(mi_writer
,
87 mi_lttng_element_command_remove_trigger
);
93 /* Open output element. */
94 ret
= mi_lttng_writer_open_element(mi_writer
, mi_lttng_element_command_output
);
103 argpar_iter
= argpar_iter_create(argc
- 1, args
, remove_trigger_options
);
105 ERR("Failed to allocate an argpar iter.");
110 enum parse_next_item_status status
;
113 parse_next_item(argpar_iter
, &argpar_item
, 1, args
, true, nullptr, nullptr);
114 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
115 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
117 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
121 assert(status
== PARSE_NEXT_ITEM_STATUS_OK
);
123 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
124 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
125 const char *arg
= argpar_item_opt_arg(argpar_item
);
132 case OPT_LIST_OPTIONS
:
133 list_cmd_options_argpar(stdout
, remove_trigger_options
);
138 if (!assign_string(&owner_uid
, arg
, "--owner-uid")) {
147 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
150 ERR("Unexpected argument '%s'", arg
);
159 ERR("Missing `name` argument.");
167 uid
= strtol(owner_uid
, &end
, 10);
168 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
169 ERR("Failed to parse `%s` as an integer.", owner_uid
);
175 ret
= lttng_list_triggers(&triggers
);
176 if (ret
!= LTTNG_OK
) {
177 ERR("Failed to get the list of triggers.");
181 trigger_status
= lttng_triggers_get_count(triggers
, &triggers_count
);
182 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
184 for (i
= 0; i
< triggers_count
; i
++) {
185 const struct lttng_trigger
*trigger
;
186 const char *trigger_name
;
189 trigger
= lttng_triggers_get_at_index(triggers
, i
);
190 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
191 switch (trigger_status
) {
192 case LTTNG_TRIGGER_STATUS_OK
:
194 case LTTNG_TRIGGER_STATUS_UNSET
:
195 /* Don't compare against anonymous triggers. */
201 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_uid
);
202 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
204 if (trigger_uid
== uid
&& strcmp(trigger_name
, name
) == 0) {
205 trigger_to_remove
= trigger
;
210 if (!trigger_to_remove
) {
211 ERR("Couldn't find trigger with name `%s`.", name
);
215 ret
= lttng_unregister_trigger(trigger_to_remove
);
217 ERR("Failed to unregister trigger `%s`.", name
);
222 ret_code
= lttng_trigger_mi_serialize(trigger_to_remove
, mi_writer
, nullptr);
223 if (ret_code
!= LTTNG_OK
) {
227 MSG("Removed trigger `%s`.", name
);
237 if (lttng_opt_mi
&& mi_writer
) {
238 /* Close output element. */
239 int mi_ret
= mi_lttng_writer_close_element(mi_writer
);
245 mi_ret
= mi_lttng_writer_write_element_bool(
246 mi_writer
, mi_lttng_element_command_success
, ret
? 0 : 1);
252 /* Command element close. */
253 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
261 argpar_item_destroy(argpar_item
);
262 argpar_iter_destroy(argpar_iter
);
263 lttng_triggers_destroy(triggers
);
266 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
267 /* Preserve original error code. */
268 ret
= ret
? ret
: CMD_ERROR
;