From nobody Thu Nov 6 12:14:10 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 154091103466271.8152764512655; Tue, 30 Oct 2018 07:50:34 -0700 (PDT) Received: from localhost ([::1]:53668 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHVLi-0003BD-1n for importer@patchew.org; Tue, 30 Oct 2018 10:50:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46483) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHVGk-00087n-MQ for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:45:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gHVGe-0000vn-Pu for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:45:22 -0400 Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]:39581) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gHVGc-0000tk-Qf for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:45:15 -0400 Received: by mail-qk1-x72f.google.com with SMTP id e4so7379190qkh.6 for ; Tue, 30 Oct 2018 07:45:14 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id d6-v6sm13992415qkb.71.2018.10.30.07.45.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 30 Oct 2018 07:45:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=+3EgmmikQq+I7mn9Hitw7dh66ejEiEkMl9ZvZ+ITy6g=; b=NNOGqhit/tliEgtN/Wkip/6ixvc2XGp/+s4Gfq+DZ1Kh8jGtwrlk3jhq6hgOarbkEq 5meH3gSk/d+m5YvpReUhv3HtlKJZbsRzTjyZzXvg2akaEYeGmjPa96IRdlF4kM5KmPJg EXWKXZWFSiwL/L69C2e8H/8Zv6IJKWl9BTj0/i4Qjy/LFZEwoWe1goSr+YXFXeZOtRvo U+VH8V3lfKV4KLadJjnH2tJTXCwT46AyU3X3nOsxRDWXHGJeP1HDr8d0yYZ9CloOb6xt 9VpxBxjvNPlBlZvmfiapNjuC5vnfylpoVgyqpNTgG6NU6nHfioxN8C6leeJ4Xsj3HkLf qBeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=+3EgmmikQq+I7mn9Hitw7dh66ejEiEkMl9ZvZ+ITy6g=; b=o4oIx6bDlJxfmhRMSOBByiZJvK78269hCP9OnCKFOvo///Li1Q11geJqzR5sH/bFGN EdOPHe3SVKNacdUDJ5oG24pK4EuUCx30hKwc8w6al3E/kP0pVl+JpuK8tphOoMajY3zR fv6P88SULm/HnjFqXscDXGcRHqFjkWXAJQ+x+KvFAR5VOy5DxEH8r0FpZnAff/AMarql giyNqq4lMGmw6LwWgrLoAoXrc2RnO+u5RiLOxlJVTI7kq7+l/h4fQ1vXbaEz/JKp5U2c s2m5RgUBBF7Nl1XN7ha5Gfl+z4mLQ6TM2iUo7ja31zxZSXO0L3dQ87iH0Odw5ohnvUg/ 1P4A== X-Gm-Message-State: AGRZ1gLq/m9f5BG3zdJQnZrCQcVw7r5tBgzqN4U9pLK/capHr7rXP9zy RMnglLfdCsbYjla/wcbQX6YhA55B X-Google-Smtp-Source: AJdET5dGCag6vTX8V7jyGZAeJ+X0khZUmARPFx6S1lk3B7jHINMr2jiyuQ727LlWFhwouebXl76Tsg== X-Received: by 2002:a37:27d2:: with SMTP id n201mr9718808qkn.18.1540910712828; Tue, 30 Oct 2018 07:45:12 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 30 Oct 2018 09:43:58 -0500 Message-Id: <20181030144358.23144-25-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181030144358.23144-1-mdroth@linux.vnet.ibm.com> References: <20181030144358.23144-1-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::72f Subject: [Qemu-devel] [PULL 24/24] qga-win: changing --retry-path option behavior X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bishara AbuHattoum , peter.maydell@linaro.org, Sameeh Jubran Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Bishara AbuHattoum Currently whenever the qemu-ga's service doesn't find the virtio-serial the run_agent() loops in a QGA_RETRY_INTERVAL (default 5 seconds) intervals and try to restart the qemu-ga which causes a synchronous loop. Changed to wait and listen for the serial events by registering for notifications a proper serial event handler that deals with events: DBT_DEVICEARRIVAL indicates that the device has been inserted and is available DBT_DEVICEREMOVECOMPLETE indicates that the devive has been removed Which allow us to determine when the channel path is available for the qemu-ga to restart. Signed-off-by: Bishara AbuHattoum Signed-off-by: Sameeh Jubran Signed-off-by: Michael Roth --- qga/main.c | 86 ++++++++++++++++++++++++++++++++++++++++++++- qga/service-win32.h | 4 +++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/qga/main.c b/qga/main.c index 506a314140..87a0711c14 100644 --- a/qga/main.c +++ b/qga/main.c @@ -34,6 +34,7 @@ #include "qemu/systemd.h" #include "qemu-version.h" #ifdef _WIN32 +#include #include "qga/service-win32.h" #include "qga/vss-win32.h" #endif @@ -83,6 +84,7 @@ struct GAState { bool logging_enabled; #ifdef _WIN32 GAService service; + HANDLE wakeup_event; #endif bool delimit_response; bool frozen; @@ -119,6 +121,7 @@ static const char *ga_freeze_whitelist[] =3D { #ifdef _WIN32 DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, LPVOID ctx); +DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data); VOID WINAPI service_main(DWORD argc, TCHAR *argv[]); #endif static int run_agent(GAState *s); @@ -677,6 +680,36 @@ static gboolean channel_init(GAState *s, const gchar *= method, const gchar *path, } =20 #ifdef _WIN32 +DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data) +{ + DWORD ret =3D NO_ERROR; + PDEV_BROADCAST_HDR broadcast_header =3D (PDEV_BROADCAST_HDR)data; + + if (broadcast_header->dbch_devicetype =3D=3D DBT_DEVTYP_DEVICEINTERFAC= E) { + switch (type) { + /* Device inserted */ + case DBT_DEVICEARRIVAL: + /* Start QEMU-ga's service */ + if (!SetEvent(ga_state->wakeup_event)) { + ret =3D GetLastError(); + } + break; + /* Device removed */ + case DBT_DEVICEQUERYREMOVE: + case DBT_DEVICEREMOVEPENDING: + case DBT_DEVICEREMOVECOMPLETE: + /* Stop QEMU-ga's service */ + if (!ResetEvent(ga_state->wakeup_event)) { + ret =3D GetLastError(); + } + break; + default: + ret =3D ERROR_CALL_NOT_IMPLEMENTED; + } + } + return ret; +} + DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, LPVOID ctx) { @@ -688,9 +721,13 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD ty= pe, LPVOID data, case SERVICE_CONTROL_STOP: case SERVICE_CONTROL_SHUTDOWN: quit_handler(SIGTERM); + SetEvent(ga_state->wakeup_event); service->status.dwCurrentState =3D SERVICE_STOP_PENDING; SetServiceStatus(service->status_handle, &service->status); break; + case SERVICE_CONTROL_DEVICEEVENT: + handle_serial_device_events(type, data); + break; =20 default: ret =3D ERROR_CALL_NOT_IMPLEMENTED; @@ -717,10 +754,24 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[]) service->status.dwServiceSpecificExitCode =3D NO_ERROR; service->status.dwCheckPoint =3D 0; service->status.dwWaitHint =3D 0; + DEV_BROADCAST_DEVICEINTERFACE notification_filter; + ZeroMemory(¬ification_filter, sizeof(notification_filter)); + notification_filter.dbcc_devicetype =3D DBT_DEVTYP_DEVICEINTERFACE; + notification_filter.dbcc_size =3D sizeof(DEV_BROADCAST_DEVICEINTERFACE= ); + notification_filter.dbcc_classguid =3D GUID_VIOSERIAL_PORT; + + service->device_notification_handle =3D + RegisterDeviceNotification(service->status_handle, + ¬ification_filter, DEVICE_NOTIFY_SERVICE_HANDLE); + if (!service->device_notification_handle) { + g_critical("Failed to register device notification handle!\n"); + return; + } SetServiceStatus(service->status_handle, &service->status); =20 run_agent(ga_state); =20 + UnregisterDeviceNotification(service->device_notification_handle); service->status.dwCurrentState =3D SERVICE_STOPPED; SetServiceStatus(service->status_handle, &service->status); } @@ -1328,12 +1379,24 @@ static GAState *initialize_agent(GAConfig *config, = int socket_activation) =20 s->config =3D config; s->socket_activation =3D socket_activation; + +#ifdef _WIN32 + s->wakeup_event =3D CreateEvent(NULL, TRUE, FALSE, TEXT("WakeUp")); + if (s->wakeup_event =3D=3D NULL) { + g_critical("CreateEvent failed"); + return NULL; + } +#endif + ga_state =3D s; return s; } =20 static void cleanup_agent(GAState *s) { +#ifdef _WIN32 + CloseHandle(s->wakeup_event); +#endif if (s->command_state) { ga_command_state_cleanup_all(s->command_state); ga_command_state_free(s->command_state); @@ -1365,6 +1428,27 @@ static int run_agent_once(GAState *s) return EXIT_SUCCESS; } =20 +static void wait_for_channel_availability(GAState *s) +{ + g_warning("waiting for channel path..."); +#ifndef _WIN32 + sleep(QGA_RETRY_INTERVAL); +#else + DWORD dwWaitResult; + + dwWaitResult =3D WaitForSingleObject(s->wakeup_event, INFINITE); + + switch (dwWaitResult) { + case WAIT_OBJECT_0: + break; + case WAIT_TIMEOUT: + break; + default: + g_critical("WaitForSingleObject failed"); + } +#endif +} + static int run_agent(GAState *s) { int ret =3D EXIT_SUCCESS; @@ -1375,7 +1459,7 @@ static int run_agent(GAState *s) ret =3D run_agent_once(s); if (s->config->retry_path && !s->force_exit) { g_warning("agent stopped unexpectedly, restarting..."); - sleep(QGA_RETRY_INTERVAL); + wait_for_channel_availability(s); } } while (s->config->retry_path && !s->force_exit); =20 diff --git a/qga/service-win32.h b/qga/service-win32.h index 89e99dfede..7b16d69b57 100644 --- a/qga/service-win32.h +++ b/qga/service-win32.h @@ -20,9 +20,13 @@ #define QGA_SERVICE_NAME "qemu-ga" #define QGA_SERVICE_DESCRIPTION "Enables integration with QEMU machine em= ulator and virtualizer." =20 +static const GUID GUID_VIOSERIAL_PORT =3D { 0x6fde7521, 0x1b65, 0x48ae, +{ 0xb6, 0x28, 0x80, 0xbe, 0x62, 0x1, 0x60, 0x26 } }; + typedef struct GAService { SERVICE_STATUS status; SERVICE_STATUS_HANDLE status_handle; + HDEVNOTIFY device_notification_handle; } GAService; =20 int ga_install_service(const char *path, const char *logfile, --=20 2.17.1