- const bt_value *result = NULL;
- bt_query_executor_query_status query_status;
- bt_query_executor *query_exec;
- *fail_reason = "unknown error";
- int ret = 0;
-
- BT_ASSERT(fail_reason);
- BT_ASSERT(user_result);
- query_exec = bt_query_executor_create();
- if (!query_exec) {
- BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
- goto error;
- }
-
- bt_query_executor_add_interrupter(query_exec, the_interrupter);
-
- if (interrupted) {
- BT_CLI_LOGW_APPEND_CAUSE(
- "Interrupted by user before executing the query: "
- "comp-cls-addr=%p, comp-cls-name=\"%s\", "
- "query-obj=\"%s\"", comp_cls,
- bt_component_class_get_name(comp_cls), obj);
- *fail_reason = "interrupted by user";
- goto error;
- }
-
- while (true) {
- query_status = bt_query_executor_query(
- query_exec, comp_cls, obj, params,
- cfg->log_level, &result);
- switch (query_status) {
- case BT_QUERY_EXECUTOR_QUERY_STATUS_OK:
- goto ok;
- case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN:
- {
- const uint64_t sleep_time_us = 100000;
-
- if (bt_interrupter_is_set(the_interrupter)) {
- *fail_reason = "interrupted by user";
- goto error;
- }
-
- /* Wait 100 ms and retry */
- BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
- "time-us=%" PRIu64, sleep_time_us);
-
- if (usleep(sleep_time_us)) {
- if (bt_interrupter_is_set(the_interrupter)) {
- BT_CLI_LOGW_APPEND_CAUSE(
- "Query was interrupted by user: "
- "comp-cls-addr=%p, comp-cls-name=\"%s\", "
- "query-obj=\"%s\"", comp_cls,
- bt_component_class_get_name(comp_cls),
- obj);
- *fail_reason = "interrupted by user";
- goto error;
- }
- }
-
- continue;
- }
- case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR:
- if (bt_interrupter_is_set(the_interrupter)) {
- *fail_reason = "interrupted by user";
- goto error;
- }
-
- goto error;
- case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT:
- *fail_reason = "invalid or unknown query object";
- goto error;
- case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS:
- *fail_reason = "invalid query parameters";
- goto error;
- case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR:
- *fail_reason = "not enough memory";
- goto error;
- default:
- BT_LOGF("Unknown query status: status=%s",
- bt_common_func_status_string(query_status));
- abort();
- }
- }
-
-ok:
- *user_result = result;
- result = NULL;
- goto end;
-
-error:
- ret = -1;
-
-end:
- bt_query_executor_put_ref(query_exec);
- bt_value_put_ref(result);
- return ret;