| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* Integration test for the QMP client library against a real QEMU instance. |
| 4 |
* |
| 5 |
* Launches QEMU with -machine none (no bootable image needed) to get a live QMP monitor, then exercises the |
| 6 |
* client library against it. Validates the blocking handshake, large response buffering (~200KB for |
| 7 |
* query-qmp-schema), response correlation by id, and async command execution. |
| 8 |
* |
| 9 |
* Skipped automatically if QEMU is not installed. */ |
| 10 |
|
| 11 |
#include <signal.h> |
| 12 |
#include <sys/eventfd.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
|
| 15 |
#include "sd-event.h" |
| 16 |
#include "sd-json.h" |
| 17 |
|
| 18 |
#include "fd-util.h" |
| 19 |
#include "pidref.h" |
| 20 |
#include "process-util.h" |
| 21 |
#include "qmp-client.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "tests.h" |
| 24 |
#include "time-util.h" |
| 25 |
#include "vmspawn-util.h" |
| 26 |
|
| 27 |
static int start_qemu(const char *qemu_binary, int fd, PidRef *ret) { |
| 28 |
_cleanup_free_ char *chardev_arg = NULL; |
| 29 |
int r; |
| 30 |
|
| 31 |
assert(qemu_binary); |
| 32 |
assert(fd >= 0); |
| 33 |
assert(ret); |
| 34 |
|
| 35 |
if (asprintf(&chardev_arg, "socket,id=qmp,fd=%d", fd) < 0) |
| 36 |
return -ENOMEM; |
| 37 |
|
| 38 |
r = pidref_safe_fork_full( |
| 39 |
"(qemu)", |
| 40 |
(const int[3]) { STDIN_FILENO, -EBADF, -EBADF }, |
| 41 |
&fd, 1, |
| 42 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_CLOEXEC_OFF, |
| 43 |
ret); |
| 44 |
if (r < 0) |
| 45 |
return r; |
| 46 |
if (r == 0) { |
| 47 |
/* Child */ |
| 48 |
execl(qemu_binary, qemu_binary, |
| 49 |
"-machine", "none", |
| 50 |
"-nographic", |
| 51 |
"-nodefaults", |
| 52 |
"-chardev", chardev_arg, |
| 53 |
"-mon", "chardev=qmp,mode=control", |
| 54 |
NULL); |
| 55 |
log_error_errno(errno, "Failed to exec %s: %m", qemu_binary); |
| 56 |
_exit(EXIT_FAILURE); |
| 57 |
} |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
/* Test helper: tracks an async QMP command result and signals completion. */ |
| 63 |
typedef struct { |
| 64 |
sd_json_variant *result; |
| 65 |
int error; |
| 66 |
bool done; |
| 67 |
} QmpTestResult; |
| 68 |
|
| 69 |
static int on_test_result( |
| 70 |
QmpClient *client, |
| 71 |
sd_json_variant *result, |
| 72 |
const char *error_desc, |
| 73 |
int error, |
| 74 |
void *userdata) { |
| 75 |
|
| 76 |
QmpTestResult *t = ASSERT_PTR(userdata); |
| 77 |
|
| 78 |
t->error = error; |
| 79 |
if (result) |
| 80 |
t->result = sd_json_variant_ref(result); |
| 81 |
t->done = true; |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
static void qmp_test_wait(sd_event *event, QmpTestResult *t) { |
| 86 |
assert(event); |
| 87 |
assert(t); |
| 88 |
|
| 89 |
usec_t deadline = usec_add(now(CLOCK_MONOTONIC), 5 * USEC_PER_MINUTE); |
| 90 |
|
| 91 |
while (!t->done) { |
| 92 |
usec_t n = now(CLOCK_MONOTONIC); |
| 93 |
ASSERT_LT(n, deadline); |
| 94 |
ASSERT_OK(sd_event_run(event, usec_sub_unsigned(deadline, n))); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void qmp_test_result_done(QmpTestResult *t) { |
| 99 |
assert(t); |
| 100 |
|
| 101 |
sd_json_variant_unref(t->result); |
| 102 |
*t = (QmpTestResult) {}; |
| 103 |
} |
| 104 |
|
| 105 |
TEST(qmp_client_qemu_handshake_and_schema) { |
| 106 |
_cleanup_free_ char *qemu = NULL; |
| 107 |
_cleanup_(qmp_client_unrefp) QmpClient *client = NULL; |
| 108 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 109 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 110 |
QmpTestResult t = {}; |
| 111 |
_cleanup_close_pair_ int qmp_fds[2] = EBADF_PAIR; |
| 112 |
int r; |
| 113 |
|
| 114 |
if (find_qemu_binary(&qemu) < 0) { |
| 115 |
log_tests_skipped("QEMU not found"); |
| 116 |
return; |
| 117 |
} |
| 118 |
log_info("Using QEMU: %s", qemu); |
| 119 |
|
| 120 |
ASSERT_OK(sd_event_new(&event)); |
| 121 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, qmp_fds)); |
| 122 |
|
| 123 |
ASSERT_OK(start_qemu(qemu, qmp_fds[1], &pidref)); |
| 124 |
qmp_fds[1] = safe_close(qmp_fds[1]); |
| 125 |
|
| 126 |
r = qmp_client_connect_fd(&client, qmp_fds[0]); |
| 127 |
if (r < 0) { |
| 128 |
log_tests_skipped_errno(r, "QMP connect failed (QEMU may not support -machine none)"); |
| 129 |
return; |
| 130 |
} |
| 131 |
TAKE_FD(qmp_fds[0]); |
| 132 |
|
| 133 |
ASSERT_OK(qmp_client_attach_event(client, event, SD_EVENT_PRIORITY_NORMAL)); |
| 134 |
|
| 135 |
/* query-qmp-schema returns ~200KB -- validates the buffered reader handles large multi-read() |
| 136 |
* responses correctly. The handshake completes transparently inside invoke(). */ |
| 137 |
r = qmp_client_invoke(client, /* ret_slot= */ NULL, "query-qmp-schema", NULL, on_test_result, &t); |
| 138 |
if (r < 0) { |
| 139 |
log_tests_skipped_errno(r, "QMP invoke failed (handshake or send)"); |
| 140 |
return; |
| 141 |
} |
| 142 |
qmp_test_wait(event, &t); |
| 143 |
ASSERT_EQ(t.error, 0); |
| 144 |
ASSERT_NOT_NULL(t.result); |
| 145 |
ASSERT_TRUE(sd_json_variant_is_array(t.result)); |
| 146 |
ASSERT_GT(sd_json_variant_elements(t.result), (size_t) 0); |
| 147 |
log_info("query-qmp-schema returned %zu entries", sd_json_variant_elements(t.result)); |
| 148 |
|
| 149 |
/* Smoke-test the schema walker against the real schema. node-name is on every BlockdevOptions* |
| 150 |
* object since blockdev-add was introduced. Don't assert discard-no-unref — CI may have QEMU < 8.1. */ |
| 151 |
ASSERT_TRUE(qmp_schema_has_member(t.result, "node-name")); |
| 152 |
ASSERT_FALSE(qmp_schema_has_member(t.result, "definitely-not-a-real-field")); |
| 153 |
|
| 154 |
qmp_test_result_done(&t); |
| 155 |
|
| 156 |
/* Clean shutdown */ |
| 157 |
ASSERT_OK(qmp_client_invoke(client, /* ret_slot= */ NULL, "quit", NULL, on_test_result, &t)); |
| 158 |
qmp_test_wait(event, &t); |
| 159 |
ASSERT_EQ(t.error, 0); |
| 160 |
qmp_test_result_done(&t); |
| 161 |
|
| 162 |
siginfo_t si = {}; |
| 163 |
ASSERT_OK(pidref_wait_for_terminate(&pidref, &si)); |
| 164 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 165 |
ASSERT_EQ(si.si_status, EXIT_SUCCESS); |
| 166 |
pidref_done(&pidref); |
| 167 |
} |
| 168 |
|
| 169 |
TEST(qmp_client_qemu_query_status) { |
| 170 |
_cleanup_free_ char *qemu = NULL; |
| 171 |
_cleanup_(qmp_client_unrefp) QmpClient *client = NULL; |
| 172 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 173 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 174 |
QmpTestResult t = {}; |
| 175 |
sd_json_variant *running, *status; |
| 176 |
_cleanup_close_pair_ int qmp_fds[2] = EBADF_PAIR; |
| 177 |
int r; |
| 178 |
|
| 179 |
if (find_qemu_binary(&qemu) < 0) { |
| 180 |
log_tests_skipped("QEMU not found"); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
ASSERT_OK(sd_event_new(&event)); |
| 185 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, qmp_fds)); |
| 186 |
|
| 187 |
ASSERT_OK(start_qemu(qemu, qmp_fds[1], &pidref)); |
| 188 |
qmp_fds[1] = safe_close(qmp_fds[1]); |
| 189 |
|
| 190 |
r = qmp_client_connect_fd(&client, qmp_fds[0]); |
| 191 |
if (r < 0) { |
| 192 |
log_tests_skipped_errno(r, "QMP connect failed"); |
| 193 |
return; |
| 194 |
} |
| 195 |
TAKE_FD(qmp_fds[0]); |
| 196 |
|
| 197 |
ASSERT_OK(qmp_client_attach_event(client, event, SD_EVENT_PRIORITY_NORMAL)); |
| 198 |
|
| 199 |
/* query-status validates response parsing against real QEMU output format. |
| 200 |
* The handshake completes transparently inside invoke(). */ |
| 201 |
r = qmp_client_invoke(client, /* ret_slot= */ NULL, "query-status", NULL, on_test_result, &t); |
| 202 |
if (r < 0) { |
| 203 |
log_tests_skipped_errno(r, "QMP invoke failed (handshake or send)"); |
| 204 |
return; |
| 205 |
} |
| 206 |
qmp_test_wait(event, &t); |
| 207 |
ASSERT_EQ(t.error, 0); |
| 208 |
ASSERT_NOT_NULL(t.result); |
| 209 |
|
| 210 |
status = ASSERT_NOT_NULL(sd_json_variant_by_key(t.result, "status")); |
| 211 |
ASSERT_TRUE(sd_json_variant_is_string(status)); |
| 212 |
|
| 213 |
running = ASSERT_NOT_NULL(sd_json_variant_by_key(t.result, "running")); |
| 214 |
ASSERT_TRUE(sd_json_variant_is_boolean(running)); |
| 215 |
|
| 216 |
log_info("QEMU status: %s, running: %s", |
| 217 |
sd_json_variant_string(status), |
| 218 |
true_false(sd_json_variant_boolean(running))); |
| 219 |
|
| 220 |
qmp_test_result_done(&t); |
| 221 |
|
| 222 |
/* Test stop + cont to exercise command sequencing and id correlation */ |
| 223 |
ASSERT_OK(qmp_client_invoke(client, /* ret_slot= */ NULL, "stop", NULL, on_test_result, &t)); |
| 224 |
qmp_test_wait(event, &t); |
| 225 |
ASSERT_EQ(t.error, 0); |
| 226 |
qmp_test_result_done(&t); |
| 227 |
|
| 228 |
/* Verify status changed */ |
| 229 |
ASSERT_OK(qmp_client_invoke(client, /* ret_slot= */ NULL, "query-status", NULL, on_test_result, &t)); |
| 230 |
qmp_test_wait(event, &t); |
| 231 |
ASSERT_EQ(t.error, 0); |
| 232 |
ASSERT_NOT_NULL(t.result); |
| 233 |
|
| 234 |
running = ASSERT_NOT_NULL(sd_json_variant_by_key(t.result, "running")); |
| 235 |
ASSERT_FALSE(sd_json_variant_boolean(running)); |
| 236 |
log_info("After stop: running=%s", true_false(sd_json_variant_boolean(running))); |
| 237 |
|
| 238 |
qmp_test_result_done(&t); |
| 239 |
|
| 240 |
ASSERT_OK(qmp_client_invoke(client, /* ret_slot= */ NULL, "cont", NULL, on_test_result, &t)); |
| 241 |
qmp_test_wait(event, &t); |
| 242 |
ASSERT_EQ(t.error, 0); |
| 243 |
qmp_test_result_done(&t); |
| 244 |
|
| 245 |
/* Clean shutdown */ |
| 246 |
ASSERT_OK(qmp_client_invoke(client, /* ret_slot= */ NULL, "quit", NULL, on_test_result, &t)); |
| 247 |
qmp_test_wait(event, &t); |
| 248 |
ASSERT_EQ(t.error, 0); |
| 249 |
qmp_test_result_done(&t); |
| 250 |
|
| 251 |
siginfo_t si = {}; |
| 252 |
ASSERT_OK(pidref_wait_for_terminate(&pidref, &si)); |
| 253 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 254 |
ASSERT_EQ(si.si_status, EXIT_SUCCESS); |
| 255 |
pidref_done(&pidref); |
| 256 |
} |
| 257 |
|
| 258 |
TEST(qmp_client_qemu_add_fd) { |
| 259 |
_cleanup_free_ char *qemu = NULL; |
| 260 |
_cleanup_(qmp_client_unrefp) QmpClient *client = NULL; |
| 261 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 262 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 263 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL; |
| 264 |
_cleanup_close_ int fd_to_pass = -EBADF; |
| 265 |
QmpTestResult t = {}; |
| 266 |
_cleanup_close_pair_ int qmp_fds[2] = EBADF_PAIR; |
| 267 |
int r; |
| 268 |
|
| 269 |
if (find_qemu_binary(&qemu) < 0) { |
| 270 |
log_tests_skipped("QEMU not found"); |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
ASSERT_OK(sd_event_new(&event)); |
| 275 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, qmp_fds)); |
| 276 |
|
| 277 |
ASSERT_OK(start_qemu(qemu, qmp_fds[1], &pidref)); |
| 278 |
qmp_fds[1] = safe_close(qmp_fds[1]); |
| 279 |
|
| 280 |
r = qmp_client_connect_fd(&client, qmp_fds[0]); |
| 281 |
if (r < 0) { |
| 282 |
log_tests_skipped_errno(r, "QMP connect failed"); |
| 283 |
return; |
| 284 |
} |
| 285 |
TAKE_FD(qmp_fds[0]); |
| 286 |
|
| 287 |
ASSERT_OK(qmp_client_attach_event(client, event, SD_EVENT_PRIORITY_NORMAL)); |
| 288 |
|
| 289 |
fd_to_pass = eventfd(0, EFD_CLOEXEC); |
| 290 |
ASSERT_OK_ERRNO(fd_to_pass); |
| 291 |
|
| 292 |
ASSERT_OK(sd_json_buildo(&args, SD_JSON_BUILD_PAIR_UNSIGNED("fdset-id", 0))); |
| 293 |
|
| 294 |
/* Pass an fd via SCM_RIGHTS on the very first invoke against a fresh client: |
| 295 |
* add-fd lands right after the eagerly-enqueued qmp_capabilities. QEMU processes cap |
| 296 |
* first (no fd needed), then add-fd, popping the fd from its FIFO receive queue. */ |
| 297 |
r = qmp_client_invoke(client, /* ret_slot= */ NULL, "add-fd", |
| 298 |
QMP_CLIENT_ARGS_FD(args, TAKE_FD(fd_to_pass)), |
| 299 |
on_test_result, &t); |
| 300 |
if (r < 0) { |
| 301 |
log_tests_skipped_errno(r, "QMP add-fd invoke failed"); |
| 302 |
return; |
| 303 |
} |
| 304 |
qmp_test_wait(event, &t); |
| 305 |
ASSERT_EQ(t.error, 0); |
| 306 |
ASSERT_NOT_NULL(t.result); |
| 307 |
|
| 308 |
sd_json_variant *fdset_id = ASSERT_NOT_NULL(sd_json_variant_by_key(t.result, "fdset-id")); |
| 309 |
sd_json_variant *fd_v = ASSERT_NOT_NULL(sd_json_variant_by_key(t.result, "fd")); |
| 310 |
ASSERT_EQ(sd_json_variant_unsigned(fdset_id), (uint64_t) 0); |
| 311 |
log_info("add-fd returned fdset-id=%" PRIu64 ", fd=%" PRIu64, |
| 312 |
sd_json_variant_unsigned(fdset_id), |
| 313 |
sd_json_variant_unsigned(fd_v)); |
| 314 |
|
| 315 |
qmp_test_result_done(&t); |
| 316 |
|
| 317 |
/* Clean shutdown */ |
| 318 |
ASSERT_OK(qmp_client_invoke(client, /* ret_slot= */ NULL, "quit", NULL, on_test_result, &t)); |
| 319 |
qmp_test_wait(event, &t); |
| 320 |
ASSERT_EQ(t.error, 0); |
| 321 |
qmp_test_result_done(&t); |
| 322 |
|
| 323 |
siginfo_t si = {}; |
| 324 |
ASSERT_OK(pidref_wait_for_terminate(&pidref, &si)); |
| 325 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 326 |
ASSERT_EQ(si.si_status, EXIT_SUCCESS); |
| 327 |
pidref_done(&pidref); |
| 328 |
} |
| 329 |
|
| 330 |
static int intro(void) { |
| 331 |
/* QEMU dies between our last write and read on the QMP socket — without this we'd |
| 332 |
* get killed by the SIGPIPE the kernel raises on write-after-EOF. */ |
| 333 |
ASSERT_TRUE(signal(SIGPIPE, SIG_IGN) != SIG_ERR); |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
DEFINE_TEST_MAIN_FULL(LOG_DEBUG, intro, NULL); |