#include <glib.h>
#include <inttypes.h>
#include <unistd.h>
+#include <signal.h>
#include "babeltrace-cfg.h"
#include "babeltrace-cfg-cli-args.h"
#include "babeltrace-cfg-cli-args-default.h"
#define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
+/* Application's processing graph (weak) */
+static struct bt_graph *the_graph;
+static bool canceled = false;
+
GPtrArray *loaded_plugins;
BT_HIDDEN
int bt_cli_log_level = BT_LOG_NONE;
+static
+void sigint_handler(int signum)
+{
+ if (signum != SIGINT) {
+ return;
+ }
+
+ if (the_graph) {
+ bt_graph_cancel(the_graph);
+ }
+
+ canceled = true;
+}
+
static
void init_static_data(void)
{
static
int cmd_query(struct bt_config *cfg)
{
- int ret;
+ int ret = 0;
struct bt_component_class *comp_cls = NULL;
struct bt_value *results = NULL;
- ret = load_all_plugins(cfg->plugin_paths);
- if (ret) {
- goto end;
- }
-
comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
cfg->cmd_data.query.cfg_component->comp_cls_name->str,
cfg->cmd_data.query.cfg_component->type);
static
int cmd_help(struct bt_config *cfg)
{
- int ret;
+ int ret = 0;
struct bt_plugin *plugin = NULL;
size_t i;
- ret = load_all_plugins(cfg->plugin_paths);
- if (ret) {
- goto end;
- }
-
plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
if (!plugin) {
BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
int ret = 0;
int plugins_count, component_classes_count = 0, i;
- ret = load_all_plugins(cfg->plugin_paths);
- if (ret) {
- goto end;
- }
-
printf("From the following plugin paths:\n\n");
print_value(stdout, cfg->plugin_paths, 2);
printf("\n");
}
BT_PUT(ctx->graph);
+ the_graph = NULL;
ctx->cfg = NULL;
}
goto error;
}
+ the_graph = ctx->graph;
ret = bt_graph_add_port_added_listener(ctx->graph,
graph_port_added_listener, ctx);
if (ret) {
int ret = 0;
struct cmd_run_ctx ctx = { 0 };
- ret = load_all_plugins(cfg->plugin_paths);
- if (ret) {
- goto error;
- }
-
/* Initialize the command's context and the graph object */
if (cmd_run_ctx_init(&ctx, cfg)) {
BT_LOGE_STR("Cannot initialize the command's context.");
goto error;
}
+ if (canceled) {
+ goto end;
+ }
+
BT_LOGI_STR("Running the graph.");
/* Run the graph */
switch (graph_status) {
case BT_GRAPH_STATUS_OK:
break;
+ case BT_GRAPH_STATUS_CANCELED:
+ BT_LOGI("Graph was canceled by user: status=%d",
+ graph_status);
+ goto error;
case BT_GRAPH_STATUS_AGAIN:
+ if (bt_graph_is_canceled(ctx.graph)) {
+ BT_LOGI("Graph was canceled by user: status=%d",
+ graph_status);
+ goto error;
+ }
+
if (cfg->cmd_data.run.retry_duration_us > 0) {
BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
"time-us=%" PRIu64,
bt_cli_log_level = log_level;
}
+void set_sigint_handler(void)
+{
+ struct sigaction new_action, old_action;
+
+ new_action.sa_handler = sigint_handler;
+ sigemptyset(&new_action.sa_mask);
+ new_action.sa_flags = 0;
+ sigaction(SIGINT, NULL, &old_action);
+
+ if (old_action.sa_handler != SIG_IGN) {
+ sigaction(SIGINT, &new_action, NULL);
+ }
+}
+
int main(int argc, const char **argv)
{
int ret;
struct bt_config *cfg;
init_log_level();
+ set_sigint_handler();
init_static_data();
cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
assert(false);
}
+ BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
+ cfg->command, cfg->command_name, ret);
warn_command_name_and_directory_clash(cfg);
retcode = ret ? 1 : 0;