HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/vmspawn/vmspawn-qmp.c

87,329 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 0e34b05a54bb5d1403f7e64677a0c022f6739898

Lines 2001–2100 of 2,100

        qmp_client_set_userdata(bridge->qmp, bridge);
        qmp_client_bind_event(bridge->qmp, on_qmp_event, bridge);
 
        r = qmp_client_attach_event(bridge->qmp, event, SD_EVENT_PRIORITY_NORMAL);
        if (r < 0)
                return log_error_errno(r, "Failed to attach QMP client to event loop: %m");
 
        *ret = TAKE_PTR(bridge);
        return 0;
}
 
/* Canonical sync-on-async pump, matching varlink_call_internal(). The QMP client tracks
 * outstanding replies in its own slots set; drain until it's idle. */
static int vmspawn_qmp_bridge_drain(VmspawnQmpBridge *bridge) {
        int r;
 
        assert(bridge);
 
        sd_event *event = qmp_client_get_event(bridge->qmp);
 
        for (;;) {
                int code;
                /* Callbacks of fatal boot-time errors request the event loop to exit, but that isn't running
                 * yet; stop draining then. */
                if (sd_event_get_exit_code(event, &code) >= 0) {
                        if (code < 0)
                                return code;
                        return log_error_errno(SYNTHETIC_ERRNO(ECANCELED), "Exit requested during QMP device setup.");
                }
 
                if (qmp_client_is_idle(bridge->qmp))
                        return 0;
 
                r = qmp_client_process(bridge->qmp);
                if (r < 0)
                        return log_error_errno(r, "Failed to process QMP messages: %m");
                if (r > 0)
                        continue;
 
                r = qmp_client_wait(bridge->qmp, USEC_INFINITY);
                if (r < 0)
                        return log_error_errno(r, "Failed to wait for QMP messages: %m");
        }
}
 
int vmspawn_qmp_probe_features(VmspawnQmpBridge *bridge) {
        int r;
 
        assert(bridge);
 
        /* probe_io_uring() and probe_schema() both call qmp_client_invoke(), which internally
         * drives the handshake to RUNNING via qmp_client_ensure_running() on its first call. */
        r = probe_io_uring(bridge->qmp, bridge);
        if (r < 0)
                return log_error_errno(r, "Failed to issue io_uring probe: %m");
 
        r = probe_schema(bridge->qmp, bridge);
        if (r < 0)
                return log_error_errno(r, "Failed to issue schema probe: %m");
 
        r = vmspawn_qmp_bridge_drain(bridge);
        if (r < 0)
                return r;
 
        /* If fail_pending() drained the slots (transport dropped mid-probe), features can't be
         * trusted and we have no QMP channel for device setup anyway. */
        if (qmp_client_is_disconnected(bridge->qmp))
                return log_error_errno(SYNTHETIC_ERRNO(ECONNRESET),
                                       "QMP connection dropped during feature probing");
 
        return 0;
}
 
static int on_cont_complete(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        assert(client);
 
        VmspawnQmpBridge *bridge = ASSERT_PTR(userdata);
 
        if (error < 0) {
                log_error_errno(error, "Failed to resume QEMU execution: %s", strna(error_desc));
                return sd_event_exit(qmp_client_get_event(client), error);
        }
 
        /* VM is running — all boot-time device setup has completed. */
        bridge->setup_done = true;
        return 0;
}
 
int vmspawn_qmp_start(VmspawnQmpBridge *bridge) {
        assert(bridge);
 
        return qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "cont", /* args= */ NULL, on_cont_complete, bridge);
}