aboutsummaryrefslogtreecommitdiff
path: root/qga/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'qga/main.c')
-rw-r--r--qga/main.c76
1 files changed, 63 insertions, 13 deletions
diff --git a/qga/main.c b/qga/main.c
index 4a69523..6c02f3e 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -19,9 +19,9 @@
#include <sys/wait.h>
#endif
#include "qemu/help-texts.h"
-#include "qapi/qmp/json-parser.h"
-#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qjson.h"
+#include "qobject/json-parser.h"
+#include "qobject/qdict.h"
+#include "qobject/qjson.h"
#include "guest-agent-core.h"
#include "qga-qapi-init-commands.h"
#include "qapi/error.h"
@@ -33,6 +33,7 @@
#include "qemu-version.h"
#ifdef _WIN32
#include <dbt.h>
+#include <pdh.h>
#include "qga/service-win32.h"
#include "qga/vss-win32.h"
#endif
@@ -105,6 +106,9 @@ struct GAState {
GAService service;
HANDLE wakeup_event;
HANDLE event_log;
+ HANDLE load_avg_wait_handle;
+ HANDLE load_avg_event;
+ HQUERY load_avg_pdh_query;
#endif
bool delimit_response;
bool frozen;
@@ -582,6 +586,25 @@ const char *ga_fsfreeze_hook(GAState *s)
}
#endif
+#ifdef _WIN32
+void ga_set_load_avg_wait_handle(GAState *s, HANDLE wait_handle)
+{
+ s->load_avg_wait_handle = wait_handle;
+}
+void ga_set_load_avg_event(GAState *s, HANDLE event)
+{
+ s->load_avg_event = event;
+}
+void ga_set_load_avg_pdh_query(GAState *s, HQUERY query)
+{
+ s->load_avg_pdh_query = query;
+}
+HQUERY ga_get_load_avg_pdh_query(GAState *s)
+{
+ return s->load_avg_pdh_query;
+}
+#endif
+
static void become_daemon(const char *pidfile)
{
#ifndef _WIN32
@@ -1402,6 +1425,10 @@ static GAState *initialize_agent(GAConfig *config, int socket_activation)
g_debug("Guest agent version %s started", QEMU_FULL_VERSION);
#ifdef _WIN32
+ s->load_avg_wait_handle = INVALID_HANDLE_VALUE;
+ s->load_avg_event = INVALID_HANDLE_VALUE;
+ s->load_avg_pdh_query = NULL;
+
s->event_log = RegisterEventSource(NULL, "qemu-ga");
if (!s->event_log) {
g_autofree gchar *errmsg = g_win32_error_message(GetLastError());
@@ -1430,7 +1457,6 @@ static GAState *initialize_agent(GAConfig *config, int socket_activation)
if (config->daemonize) {
/* delay opening/locking of pidfile till filesystems are unfrozen */
s->deferred_options.pid_filepath = config->pid_filepath;
- become_daemon(NULL);
}
if (config->log_filepath) {
/* delay opening the log file till filesystems are unfrozen */
@@ -1438,9 +1464,6 @@ static GAState *initialize_agent(GAConfig *config, int socket_activation)
}
ga_disable_logging(s);
} else {
- if (config->daemonize) {
- become_daemon(config->pid_filepath);
- }
if (config->log_filepath) {
FILE *log_file = ga_open_logfile(config->log_filepath);
if (!log_file) {
@@ -1487,6 +1510,20 @@ static GAState *initialize_agent(GAConfig *config, int socket_activation)
ga_apply_command_filters(s);
+ if (!channel_init(s, s->config->method, s->config->channel_path,
+ s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) {
+ g_critical("failed to initialize guest agent channel");
+ return NULL;
+ }
+
+ if (config->daemonize) {
+ if (ga_is_frozen(s)) {
+ become_daemon(NULL);
+ } else {
+ become_daemon(config->pid_filepath);
+ }
+ }
+
ga_state = s;
return s;
}
@@ -1496,6 +1533,18 @@ static void cleanup_agent(GAState *s)
#ifdef _WIN32
CloseHandle(s->wakeup_event);
CloseHandle(s->event_log);
+
+ if (s->load_avg_wait_handle != INVALID_HANDLE_VALUE) {
+ UnregisterWait(s->load_avg_wait_handle);
+ }
+
+ if (s->load_avg_event != INVALID_HANDLE_VALUE) {
+ CloseHandle(s->load_avg_event);
+ }
+
+ if (s->load_avg_pdh_query) {
+ PdhCloseQuery(s->load_avg_pdh_query);
+ }
#endif
if (s->command_state) {
ga_command_state_cleanup_all(s->command_state);
@@ -1513,8 +1562,9 @@ static void cleanup_agent(GAState *s)
static int run_agent_once(GAState *s)
{
- if (!channel_init(s, s->config->method, s->config->channel_path,
- s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) {
+ if (!s->channel &&
+ channel_init(s, s->config->method, s->config->channel_path,
+ s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) {
g_critical("failed to initialize guest agent channel");
return EXIT_FAILURE;
}
@@ -1523,6 +1573,7 @@ static int run_agent_once(GAState *s)
if (s->channel) {
ga_channel_free(s->channel);
+ s->channel = NULL;
}
return EXIT_SUCCESS;
@@ -1579,7 +1630,7 @@ static void stop_agent(GAState *s, bool requested)
int main(int argc, char **argv)
{
- int ret = EXIT_SUCCESS;
+ int ret = EXIT_FAILURE;
GAState *s;
GAConfig *config = g_new0(GAConfig, 1);
int socket_activation;
@@ -1607,7 +1658,6 @@ int main(int argc, char **argv)
socket_activation = check_socket_activation();
if (socket_activation > 1) {
g_critical("qemu-ga only supports listening on one socket");
- ret = EXIT_FAILURE;
goto end;
}
if (socket_activation) {
@@ -1631,7 +1681,6 @@ int main(int argc, char **argv)
if (!config->method) {
g_critical("unsupported listen fd type");
- ret = EXIT_FAILURE;
goto end;
}
} else if (config->channel_path == NULL) {
@@ -1643,13 +1692,13 @@ int main(int argc, char **argv)
config->channel_path = g_strdup(QGA_SERIAL_PATH_DEFAULT);
} else {
g_critical("must specify a path for this channel");
- ret = EXIT_FAILURE;
goto end;
}
}
if (config->dumpconf) {
config_dump(config);
+ ret = EXIT_SUCCESS;
goto end;
}
@@ -1664,6 +1713,7 @@ int main(int argc, char **argv)
SERVICE_TABLE_ENTRY service_table[] = {
{ (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } };
StartServiceCtrlDispatcher(service_table);
+ ret = EXIT_SUCCESS;
} else {
ret = run_agent(s);
}