#include "qemu/systemd.h"
#include "qemu-version.h"
#ifdef _WIN32
+#include <dbt.h>
#include "qga/service-win32.h"
#include "qga/vss-win32.h"
#endif
bool logging_enabled;
#ifdef _WIN32
GAService service;
+ HANDLE wakeup_event;
#endif
bool delimit_response;
bool frozen;
#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);
}
#ifdef _WIN32
+DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data)
+{
+ DWORD ret = NO_ERROR;
+ PDEV_BROADCAST_HDR broadcast_header = (PDEV_BROADCAST_HDR)data;
+
+ if (broadcast_header->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
+ switch (type) {
+ /* Device inserted */
+ case DBT_DEVICEARRIVAL:
+ /* Start QEMU-ga's service */
+ if (!SetEvent(ga_state->wakeup_event)) {
+ ret = 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 = GetLastError();
+ }
+ break;
+ default:
+ ret = ERROR_CALL_NOT_IMPLEMENTED;
+ }
+ }
+ return ret;
+}
+
DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
LPVOID ctx)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
quit_handler(SIGTERM);
+ SetEvent(ga_state->wakeup_event);
service->status.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(service->status_handle, &service->status);
break;
+ case SERVICE_CONTROL_DEVICEEVENT:
+ handle_serial_device_events(type, data);
+ break;
default:
ret = ERROR_CALL_NOT_IMPLEMENTED;
service->status.dwServiceSpecificExitCode = NO_ERROR;
service->status.dwCheckPoint = 0;
service->status.dwWaitHint = 0;
+ DEV_BROADCAST_DEVICEINTERFACE notification_filter;
+ ZeroMemory(¬ification_filter, sizeof(notification_filter));
+ notification_filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
+ notification_filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
+ notification_filter.dbcc_classguid = GUID_VIOSERIAL_PORT;
+
+ service->device_notification_handle =
+ 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);
run_agent(ga_state);
+ UnregisterDeviceNotification(service->device_notification_handle);
service->status.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(service->status_handle, &service->status);
}
s->config = config;
s->socket_activation = socket_activation;
+
+#ifdef _WIN32
+ s->wakeup_event = CreateEvent(NULL, TRUE, FALSE, TEXT("WakeUp"));
+ if (s->wakeup_event == NULL) {
+ g_critical("CreateEvent failed");
+ return NULL;
+ }
+#endif
+
ga_state = s;
return s;
}
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);
return EXIT_SUCCESS;
}
+static void wait_for_channel_availability(GAState *s)
+{
+ g_warning("waiting for channel path...");
+#ifndef _WIN32
+ sleep(QGA_RETRY_INTERVAL);
+#else
+ DWORD dwWaitResult;
+
+ dwWaitResult = 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 = EXIT_SUCCESS;
ret = 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);