| 1 | #include <stdio.h> |
| 2 | |
| 3 | #include "../command.h" |
| 4 | |
| 5 | #include "common/argpar/argpar.h" |
| 6 | |
| 7 | #ifdef LTTNG_EMBED_HELP |
| 8 | static const char help_msg[] = |
| 9 | #include <lttng-remove-trigger.1.h> |
| 10 | ; |
| 11 | #endif |
| 12 | |
| 13 | enum { |
| 14 | OPT_HELP, |
| 15 | OPT_LIST_OPTIONS, |
| 16 | }; |
| 17 | |
| 18 | static const |
| 19 | struct argpar_opt_descr remove_trigger_options[] = { |
| 20 | { OPT_HELP, 'h', "help", false }, |
| 21 | { OPT_LIST_OPTIONS, '\0', "list-options", false }, |
| 22 | ARGPAR_OPT_DESCR_SENTINEL, |
| 23 | }; |
| 24 | |
| 25 | int cmd_remove_trigger(int argc, const char **argv) |
| 26 | { |
| 27 | int ret; |
| 28 | struct argpar_parse_ret argpar_parse_ret = { 0 }; |
| 29 | const char *id = NULL; |
| 30 | int i; |
| 31 | struct lttng_triggers *triggers = NULL; |
| 32 | unsigned int triggers_count; |
| 33 | enum lttng_trigger_status trigger_status; |
| 34 | const struct lttng_trigger *trigger_to_remove = NULL; |
| 35 | |
| 36 | argpar_parse_ret = argpar_parse(argc - 1, argv + 1, |
| 37 | remove_trigger_options, true); |
| 38 | if (!argpar_parse_ret.items) { |
| 39 | fprintf(stderr, "Error: %s\n", argpar_parse_ret.error); |
| 40 | goto error; |
| 41 | } |
| 42 | |
| 43 | for (i = 0; i < argpar_parse_ret.items->n_items; i++) { |
| 44 | struct argpar_item *item = argpar_parse_ret.items->items[i]; |
| 45 | |
| 46 | if (item->type == ARGPAR_ITEM_TYPE_OPT) { |
| 47 | struct argpar_item_opt *item_opt = |
| 48 | (struct argpar_item_opt *) item; |
| 49 | |
| 50 | switch (item_opt->descr->id) { |
| 51 | case OPT_HELP: |
| 52 | SHOW_HELP(); |
| 53 | ret = 0; |
| 54 | goto end; |
| 55 | |
| 56 | case OPT_LIST_OPTIONS: |
| 57 | list_cmd_options_argpar(stdout, |
| 58 | remove_trigger_options); |
| 59 | ret = 0; |
| 60 | goto end; |
| 61 | |
| 62 | default: |
| 63 | abort(); |
| 64 | } |
| 65 | |
| 66 | } else { |
| 67 | struct argpar_item_non_opt *item_non_opt = |
| 68 | (struct argpar_item_non_opt *) item; |
| 69 | |
| 70 | if (id) { |
| 71 | fprintf(stderr, "Unexpected argument: %s\n", item_non_opt->arg); |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | id = item_non_opt->arg; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (!id) { |
| 80 | fprintf(stderr, "Missing `id` argument.\n"); |
| 81 | goto error; |
| 82 | } |
| 83 | |
| 84 | ret = lttng_list_triggers(&triggers); |
| 85 | if (ret != 0) { |
| 86 | fprintf(stderr, "Failed to get the list of triggers.\n"); |
| 87 | goto error; |
| 88 | } |
| 89 | |
| 90 | trigger_status = lttng_triggers_get_count(triggers, &triggers_count); |
| 91 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 92 | |
| 93 | for (i = 0; i < triggers_count; i++) { |
| 94 | const struct lttng_trigger *trigger; |
| 95 | const char *trigger_name; |
| 96 | |
| 97 | trigger = lttng_triggers_get_at_index(triggers, i); |
| 98 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 99 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 100 | |
| 101 | if (strcmp(trigger_name, id) == 0) { |
| 102 | trigger_to_remove = trigger; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!trigger_to_remove) { |
| 108 | fprintf(stderr, "Couldn't find trigger with id `%s`.\n", id); |
| 109 | goto error; |
| 110 | } |
| 111 | |
| 112 | ret = lttng_unregister_trigger(trigger_to_remove); |
| 113 | if (ret != 0) { |
| 114 | fprintf(stderr, "Failed to unregister trigger `%s`.\n", id); |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | printf("Removed trigger `%s`.\n", id); |
| 119 | |
| 120 | ret = 0; |
| 121 | goto end; |
| 122 | |
| 123 | error: |
| 124 | ret = 1; |
| 125 | |
| 126 | end: |
| 127 | argpar_parse_ret_fini(&argpar_parse_ret); |
| 128 | lttng_triggers_destroy(triggers); |
| 129 | |
| 130 | return ret; |
| 131 | } |