Skip to contentSkip to conversation

qga-win: changing --retry-path option behavior

PATCH Bishara AbuHattoum 1 participant 3 messages Oct 7, 2018 7 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CAJ+F1C+cUb-h0jSYem899WbQQiNCm97=VpmOaXao5Q+b6pUEHQ@mail.gmail.com

#3 lore raw

Your advice was taken and implemented, sending changes in the next version.

Quoted 253 lines · Marc-André Lureau

Hi

Quoted 7 lines · 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

Quoted 7 lines

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.

looks good overall,

qga/main.c static gboolean channel_init(GAState *s, const

Quoted 35 lines
---
 qga/main.c          | 101 +++++++++++++++++++++++++++++++++++++++++++-
 qga/service-win32.h |   4 ++
 2 files changed, 104 insertions(+), 1 deletion(-)
qga/main.c +4 -0
index 6a152eb3a7..215dcb36f1 100644
@@ -34,6 +34,7 @@
 #include "qemu/systemd.h"
 #include "qemu-version.h"
 #ifdef _WIN32
+#include <dbt.h>
 #include "qga/service-win32.h"
 #include "qga/vss-win32.h"
 #endif
@@ -101,6 +102,8 @@ struct GAState {
     bool logging_enabled;
 #ifdef _WIN32
     GAService service;
+    HANDLE channel_available_event;
+    HANDLE stop_event;
 #endif
     bool delimit_response;
     bool frozen;
@@ -137,6 +140,7 @@ static const char *ga_freeze_whitelist[] = {
 #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);
@@ -729,6 +733,36 @@ static gboolean channel_init(GAState *s, const

gchar *method, const gchar *path,

qga/main.c static gboolean channel_init(GAState *s, const · line 741

Quoted 9 lines
qga/main.c +6 -0
 }
 
 #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) {

qga/main.c DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD

Quoted 28 lines
qga/main.c +24 -0
+        switch (type) {
+            /* Device inserted */
+        case DBT_DEVICEARRIVAL:
+            /* Start QEMU-ga's service */
+            if (!SetEvent(ga_state->channel_available_event)) {
+                ret = GetLastError();
+            }
+            break;
+            /* Device removed */
+        case DBT_DEVICEQUERYREMOVE:
+        case DBT_DEVICEREMOVEPENDING:
+        case DBT_DEVICEREMOVECOMPLETE:
+            /* Stop QEMU-ga's service */
+            if (!ResetEvent(ga_state->channel_available_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)
 {
@@ -740,9 +774,13 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD

type, LPVOID data,

Quoted 14 lines
qga/main.c +4 -0
         case SERVICE_CONTROL_STOP:
         case SERVICE_CONTROL_SHUTDOWN:
             quit_handler(SIGTERM);
+            SetEvent(ga_state->stop_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;

qga/main.c VOID WINAPI service_main(DWORD argc, TCHAR *argv[]) · line 813

Quoted 7 lines
qga/main.c +4 -0
@@ -769,10 +807,25 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
     service->status.dwServiceSpecificExitCode = NO_ERROR;
     service->status.dwCheckPoint = 0;
     service->status.dwWaitHint = 0;
+    DEV_BROADCAST_DEVICEINTERFACE notification_filter;
+    ZeroMemory(&notification_filter, sizeof(notification_filter));
+    notification_filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
+    notification_filter.dbcc_size =

sizeof(DEV_BROADCAST_DEVICEINTERFACE);

qga/main.c static GAState *initialize_agent(GAConfig

Quoted 19 lines
qga/main.c +11 -0
+    notification_filter.dbcc_classguid = GUID_VIOSERIAL_PORT;
+
+    service->device_notification_handle =
+        RegisterDeviceNotification(service->status_handle,
+            &notification_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);
+    ResetEvent(ga_state->stop_event);
 
     run_agent(ga_state);
 
+    UnregisterDeviceNotification(service->device_notification_handle);
     service->status.dwCurrentState = SERVICE_STOPPED;
     SetServiceStatus(service->status_handle, &service->status);
 }
@@ -1360,12 +1413,32 @@ static GAState *initialize_agent(GAConfig

*config, int socket_activation)

qga/main.c static GAState *initialize_agent(GAConfig · line 1418

Quoted 7 lines
qga/main.c +4 -0
     s->config = config;
     s->socket_activation = socket_activation;
+
+#ifdef _WIN32
+    s->channel_available_event = CreateEvent(NULL, TRUE, FALSE,
+

TEXT("ChannelAvailableEvent"));

Quoted 30 lines
qga/main.c +17 -0
+    if (s->channel_available_event == NULL) {
+        g_critical("CreateEvent failed");
+        return NULL;
+    }
+    s->stop_event = CreateEvent(NULL, TRUE, FALSE,
+                                TEXT("StopEvent"));
+    if (s->stop_event == NULL) {
+        g_critical("CreateEvent failed");
+        return NULL;
+    }
+#endif
+
     ga_state = s;
     return s;
 }
 
 static void cleanup_agent(GAState *s)
 {
+#ifdef _WIN32
+    CloseHandle(s->channel_available_event);
+    CloseHandle(s->stop_event);
+#endif
     if (s->command_state) {
         ga_command_state_cleanup_all(s->command_state);
         ga_command_state_free(s->command_state);
@@ -1397,6 +1470,32 @@ static int run_agent_once(GAState *s)
     return EXIT_SUCCESS;
 }
 
+static void wait_for_channel_availability(GAState *s)

qga/main.c static int run_agent_once(GAState *s) · line 1483

Quoted 10 lines
qga/main.c +10 -0
+{
+    g_warning("waiting for channel path...");
+#ifndef _WIN32
+    sleep(QGA_RETRY_INTERVAL);
+#else
+    DWORD dwWaitResult;
+
+    HANDLE lpHandles[2] = { s->channel_available_event, s->stop_event };
+
+    dwWaitResult = WaitForMultipleObjects(2, lpHandles, false,

INFINITE);

qga/main.c static int run_agent_once(GAState *s) · line 1490

Quoted 7 lines
qga/main.c +7 -0
+
+    switch (dwWaitResult) {
+    case WAIT_OBJECT_0:
+        break;
+    case (WAIT_OBJECT_0 + 1):
+        g_warning("service stopped");
+        break;

Given that both events do basically the same thing, you could use one called generically "wakeup".

Alternatively, you could wait on the main_loop (manual iteration), and call g_main_context_wakeup() to avoid the extra events altogether (I think).

Quoted 25 lines
qga/main.c +9 -1
+    case WAIT_TIMEOUT:
+        break;
+    default:
+        g_critical("WaitForMultipleObjects failed");
+    }
+#endif
+}
+
 static int run_agent(GAState *s)
 {
     int ret = EXIT_SUCCESS;
@@ -1407,7 +1506,7 @@ static int run_agent(GAState *s)
         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);
 
qga/service-win32.h +0 -0
index 89e99dfede..7b16d69b57 100644

qga/service-win32.h line 21

Quoted 2 lines
qga/service-win32.h +0 -0
@@ -20,9 +20,13 @@
 #define QGA_SERVICE_NAME         "qemu-ga"
 #define QGA_SERVICE_DESCRIPTION  "Enables integration with QEMU machine

emulator and virtualizer."

Quoted 12 lines
qga/service-win32.h +4 -1
+static const GUID GUID_VIOSERIAL_PORT = { 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;
 
 int ga_install_service(const char *path, const char *logfile,
--
Quoted 3 lines

2.17.0

--
Marc-André Lureau

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CAJ+F1C+AROSNmnX6OWJ+dF5LozLQ8Afz6YB+Gq9y+epKmjB+Wg@mail.gmail.com

#6 lore raw

Re: [Qemu-devel] [PATCH 3/7] qga: move w32 service handling out of run_agent()

As I already clarified in the cover letter, I will fix the typo in the commit message and drop the message ID from all the patches, changes will be in the next version.

Quoted 95 lines · Marc-André Lureau

Hi

Quoted 10 lines · Bishara AbuHattoum

From: Michael Roth <[email protected]>

Eventually we want a w32 service to be able to restart the qga main loop from within service_main(). To allow for this we move service handling out of run_agent() such that service_main() calls run_agent() instead of the reverse.

Message-Id: <[email protected]>

Same remark about message-id

qga/main.c DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD

Quoted 9 lines
---
 qga/main.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
qga/main.c +0 -0
index 7381053a0f..d9888bff5f 100644
@@ -136,6 +136,7 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD

type, LPVOID data,

Quoted 17 lines
qga/main.c +2 -1
                                   LPVOID ctx);
 VOID WINAPI service_main(DWORD argc, TCHAR *argv[]);
 #endif
+static int run_agent(GAState *s);
 
 static void
 init_dfl_pathnames(void)
@@ -763,7 +764,7 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
     service->status.dwWaitHint = 0;
     SetServiceStatus(service->status_handle, &service->status);
 
-    g_main_loop_run(ga_state->main_loop);
+    run_agent(ga_state);
 
     service->status.dwCurrentState = SERVICE_STOPPED;
     SetServiceStatus(service->status_handle, &service->status);

qga/main.c static int run_agent(GAState *s) · line 1380

Quoted 10 lines
qga/main.c +1 -5
@@ -1372,17 +1373,8 @@ static int run_agent(GAState *s)
         g_critical("failed to initialize guest agent channel");
         return EXIT_FAILURE;
     }
-#ifndef _WIN32
+
     g_main_loop_run(ga_state->main_loop);
-#else
-    if (config->daemonize) {
-        SERVICE_TABLE_ENTRY service_table[] = {
-            { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL }

};

Quoted 9 lines
qga/main.c +0 -5
-        StartServiceCtrlDispatcher(service_table);
-    } else {
-        g_main_loop_run(ga_state->main_loop);
-    }
-#endif
 
     return EXIT_SUCCESS;
 }

qga/main.c int main(int argc, char **argv) · line 1467

Quoted 8 lines
qga/main.c +5 -0
@@ -1468,7 +1460,18 @@ int main(int argc, char **argv)
         g_critical("error initializing guest agent");
         goto end;
     }
+
+#ifdef _WIN32
+    if (config->daemonize) {
+        SERVICE_TABLE_ENTRY service_table[] = {
+            { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL }

};

qga/main.c int main(int argc, char **argv) · line 1473

Quoted 6 lines
qga/main.c +5 -0
+        StartServiceCtrlDispatcher(service_table);
+    } else {
+        ret = run_agent(s);
+    }
+#endif
     ret = run_agent(s);

run_agent() is called twice.

Quoted 8 lines
qga/main.c +1 -1
+
     cleanup_agent(s);
 
 end:
--

2.17.0

--
Marc-André Lureau

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CAJ+F1CJ6n41Sr2Tm4Fd+HQ0QBBCvrE1Jv4mZHXyo6OzJnRXULg@mail.gmail.com

#9 lore raw

Re: [Qemu-devel] [PATCH 1/7] qga: group agent init/cleanup init separate routines

As I already clarified in the cover letter, I will fix the typo in the commit message and drop the message ID from all the patches, changes will be in the next version.

Quoted 205 lines · Marc-André Lureau

Hi

Quoted 5 lines · Bishara AbuHattoum

From: Michael Roth <[email protected]>

This patch better separates the init/cleanup routines out into separate functions to make make the start-up procedure a bit easier

"make make"

Quoted 6 lines

to follow. This will be useful when we eventually break out the actual start/stop of the agent's main loop into separates routines that can be called multiple times after the init phase.

Message-Id: <[email protected]>

I don't know if it make sense to retain the original Message-Id.

it is not as good as it used to wrt to cleaning up on error, but that may be acceptable..

qga/main.c static bool check_is_frozen(GAState *s) · line 1248

Quoted 13 lines
---
 qga/main.c | 82 +++++++++++++++++++++++++++++++++---------------------
 1 file changed, 50 insertions(+), 32 deletions(-)
qga/main.c +0 -1
index 6d70242d05..7b7b837a14 100644
@@ -1245,9 +1245,21 @@ static bool check_is_frozen(GAState *s)
     return false;
 }
 
-static int run_agent(GAState *s, GAConfig *config, int

socket_activation)

qga/main.c static bool check_is_frozen(GAState *s) · line 1259

Quoted 13 lines
qga/main.c +11 -1
+static GAState *initialize_agent(GAConfig *config)
 {
-    ga_state = s;
+    GAState *s = g_new0(GAState, 1);
+
+    g_assert(ga_state == NULL);
+
+    s->log_level = config->log_level;
+    s->log_file = stderr;
+#ifdef CONFIG_FSFREEZE
+    s->fsfreeze_hook = config->fsfreeze_hook;
+#endif
+    s->pstate_filepath = g_strdup_printf("%s/qga.state",

config->state_dir);

qga/main.c static bool check_is_frozen(GAState *s) · line 1260

Quoted 1 line
qga/main.c +1 -0
+    s->state_filepath_isfrozen =

g_strdup_printf("%s/qga.state.isfrozen",

qga/main.c static int run_agent(GAState *s, GAConfig *config,

Quoted 6 lines
qga/main.c +2 -0
+                                                 config->state_dir);
+    s->frozen = check_is_frozen(s);
 
     g_log_set_default_handler(ga_log, s);
     g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
@@ -1263,7 +1275,7 @@ static int run_agent(GAState *s, GAConfig *config,

int socket_activation)

qga/main.c static int run_agent(GAState *s, GAConfig *config, · line 1276

Quoted 2 lines
qga/main.c +0 -0
     if (g_mkdir_with_parents(config->state_dir, S_IRWXU) == -1) {
         g_critical("unable to create (an ancestor of) the state

directory"

qga/main.c static int run_agent(GAState *s, GAConfig *config,

Quoted 7 lines
qga/main.c +1 -1
                    " '%s': %s", config->state_dir, strerror(errno));
-        return EXIT_FAILURE;
+        return NULL;
     }
 #endif
 
@@ -1288,7 +1300,7 @@ static int run_agent(GAState *s, GAConfig *config,

int socket_activation)

qga/main.c static int run_agent(GAState *s, GAConfig *config,

Quoted 9 lines
qga/main.c +1 -1
             if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
-                return EXIT_FAILURE;
+                return NULL;
             }
             s->log_file = log_file;
         }
@@ -1299,7 +1311,7 @@ static int run_agent(GAState *s, GAConfig *config,

int socket_activation)

qga/main.c static int run_agent(GAState *s, GAConfig

Quoted 9 lines
qga/main.c +1 -1
                                s->pstate_filepath,
                                ga_is_frozen(s))) {
         g_critical("failed to load persistent state");
-        return EXIT_FAILURE;
+        return NULL;
     }
 
     config->blacklist = ga_command_blacklist_init(config->blacklist);
@@ -1320,12 +1332,37 @@ static int run_agent(GAState *s, GAConfig

*config, int socket_activation)

Quoted 24 lines
qga/main.c +15 -1
 #ifndef _WIN32
     if (!register_signal_handlers()) {
         g_critical("failed to register signal handlers");
-        return EXIT_FAILURE;
+        return NULL;
     }
 #endif
 
     s->main_loop = g_main_loop_new(NULL, false);
 
+    ga_state = s;
+    return s;
+}
+
+static void cleanup_agent(GAState *s)
+{
+    if (s->command_state) {
+        ga_command_state_cleanup_all(s->command_state);
+        ga_command_state_free(s->command_state);
+        json_message_parser_destroy(&s->parser);
+    }
+    if (s->channel) {
+        ga_channel_free(s->channel);
+    }

qga/main.c static int run_agent(GAState *s, GAConfig · line 1364

Quoted 10 lines
qga/main.c +10 -0
+    g_free(s->pstate_filepath);
+    g_free(s->state_filepath_isfrozen);
+    if (s->main_loop) {
+        g_main_loop_unref(s->main_loop);
+    }
+    g_free(s);
+    ga_state = NULL;
+}
+
+static int run_agent(GAState *s, GAConfig *config, int

socket_activation)

qga/main.c static int run_agent(GAState *s, GAConfig · line 1367

Quoted 3 lines
qga/main.c +1 -0
+{
     if (!channel_init(ga_state, config->method, config->channel_path,
                       socket_activation ? FIRST_SOCKET_ACTIVATION_FD :

-1)) {

qga/main.c static int run_agent(GAState *s, GAConfig *config,

Quoted 2 lines
qga/main.c +0 -0
         g_critical("failed to initialize guest agent channel");
@@ -1349,7 +1386,7 @@ static int run_agent(GAState *s, GAConfig *config,

int socket_activation)

Quoted 9 lines
qga/main.c +1 -1
 int main(int argc, char **argv)
 {
     int ret = EXIT_SUCCESS;
-    GAState *s = g_new0(GAState, 1);
+    GAState *s;
     GAConfig *config = g_new0(GAConfig, 1);
     int socket_activation;
 

qga/main.c int main(int argc, char **argv) · line 1425

Quoted 9 lines
qga/main.c +0 -6
@@ -1417,44 +1454,25 @@ int main(int argc, char **argv)
         }
     }
 
-    s->log_level = config->log_level;
-    s->log_file = stderr;
-#ifdef CONFIG_FSFREEZE
-    s->fsfreeze_hook = config->fsfreeze_hook;
-#endif
-    s->pstate_filepath = g_strdup_printf("%s/qga.state",

config->state_dir);

qga/main.c int main(int argc, char **argv) · line 1426

Quoted 1 line
qga/main.c +0 -1
-    s->state_filepath_isfrozen =

g_strdup_printf("%s/qga.state.isfrozen",

Quoted 41 lines
qga/main.c +6 -19
-                                                 config->state_dir);
-    s->frozen = check_is_frozen(s);
-
     if (config->dumpconf) {
         config_dump(config);
         goto end;
     }
 
+    s = initialize_agent(config);
+    if (!s) {
+        g_critical("error initializing guest agent");
+        goto end;
+    }
     ret = run_agent(s, config, socket_activation);
+    cleanup_agent(s);
 
 end:
-    if (s->command_state) {
-        ga_command_state_cleanup_all(s->command_state);
-        ga_command_state_free(s->command_state);
-        json_message_parser_destroy(&s->parser);
-    }
-    if (s->channel) {
-        ga_channel_free(s->channel);
-    }
-    g_free(s->pstate_filepath);
-    g_free(s->state_filepath_isfrozen);
-
     if (config->daemonize) {
         unlink(config->pid_filepath);
     }
 
     config_free(config);
-    if (s->main_loop) {
-        g_main_loop_unref(s->main_loop);
-    }
-    g_free(s);
 
     return ret;
 }
--
Quoted 3 lines

2.17.0

--
Marc-André Lureau