Skip to contentSkip to conversation

qemu-ga: add support for Windows

PATCH New Michael Roth 1 participant 9 messages Feb 14, 2012 Kbuild

lore
Share & tools
lore
#3 lore raw

PATCH v3 3/8 qemu-ga: separate out common commands from posix-specific ones

In reply to Michael Roth

Many of the current RPC implementations are very much POSIX-specific and require complete re-writes for Windows. There are however a small set of core guest agent commands that are common to both, and other commands such as guest-file-* which *may* be portable. So we introduce commands.c for the latter, and will rename guest-agent-commands.c to commands-posix.c in a future commit. Windows implementations will go in commands-win32.c, eventually.

---
 Makefile.objs              |    2 +-
 qga/commands.c             |   73 ++++++++++++++++++++++++++++++++++++++++++++
 qga/guest-agent-commands.c |   59 +-----------------------------------
 qga/guest-agent-core.h     |    1 +
 4 files changed, 76 insertions(+), 59 deletions(-)
 create mode 100644 qga/commands.c
Makefile.objs +1 -1
index a9c4d51..0ec5874 100644
@@ -422,7 +422,7 @@ common-obj-y += qmp.o hmp.o
 ######################################################################
 # guest agent
 
-qga-nested-y = guest-agent-commands.o guest-agent-command-state.o
+qga-nested-y = commands.o guest-agent-commands.o guest-agent-command-state.o
 qga-nested-y += channel-posix.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
qga/commands.c new file +73 -0

73 lines not on this page · raw message

qga/guest-agent-commands.c +1 -58

69 lines not on this page · raw message

qga/guest-agent-core.h +1 -0
index 6148d10..b5dfa5b 100644
@@ -29,3 +29,4 @@ GACommandState *ga_command_state_new(void);
 bool ga_logging_enabled(GAState *s);
 void ga_disable_logging(GAState *s);
 void ga_enable_logging(GAState *s);
+void slog(const gchar *fmt, ...);
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v3 1/8 qemu-ga: Add schema documentation for types

In reply to Michael Roth

Document guest agent schema types in similar fashion as qmp schema types.

---
 qapi-schema-guest.json |  118 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 97 insertions(+), 21 deletions(-)
qapi-schema-guest.json +97 -21
index 5f8a18d..706925d 100644
@@ -37,17 +37,42 @@
 { 'command': 'guest-ping' }
 
 ##
-# @guest-info:
+# @GuestAgentCommandInfo:
 #
-# Get some information about the guest agent.
+# Information about guest agent commands.
 #
-# Since: 0.15.0
+# @name: name of the command
+#
+# @enabled: whether command is currently enabled by guest admin
+#
+# Since 1.1.0
 ##
 { 'type': 'GuestAgentCommandInfo',
   'data': { 'name': 'str', 'enabled': 'bool' } }
+
+##
+# @GuestAgentInfo
+#
+# Information about guest agent.
+#
+# @version: guest agent version
+#
+# @supported_commands: Information about guest agent commands
+#
+# Since 0.15.0
+##
 { 'type': 'GuestAgentInfo',
   'data': { 'version': 'str',
             'supported_commands': ['GuestAgentCommandInfo'] } }
+##
+# @guest-info:
+#
+# Get some information about the guest agent.
+#
+# Returns: @GuestAgentInfo
+#
+# Since: 0.15.0
+##
 { 'command': 'guest-info',
   'returns': 'GuestAgentInfo' }
 
@@ -98,6 +123,23 @@
   'data': { 'handle': 'int' } }
 
 ##
+# @GuestFileRead
+#
+# Result of guest agent file-read operation
+#
+# @count: number of bytes read (note: count is *before*
+#         base64-encoding is applied)
+#
+# @buf-b64: base64-encoded bytes read
+#
+# @eof: whether EOF was encountered during read operation.
+#
+# Since: 0.15.0
+##
+{ 'type': 'GuestFileRead',
+  'data': { 'count': 'int', 'buf-b64': 'str', 'eof': 'bool' } }
+
+##
 # @guest-file-read:
 #
 # Read from an open file in the guest. Data will be base64-encoded
@@ -106,19 +148,30 @@
 #
 # @count: #optional maximum number of bytes to read (default is 4KB)
 #
-# Returns: GuestFileRead on success. Note: count is number of bytes read
-#          *before* base64 encoding bytes read.
+# Returns: @GuestFileRead on success.
 #
 # Since: 0.15.0
 ##
-{ 'type': 'GuestFileRead',
-  'data': { 'count': 'int', 'buf-b64': 'str', 'eof': 'bool' } }
-
 { 'command': 'guest-file-read',
   'data':    { 'handle': 'int', '*count': 'int' },
   'returns': 'GuestFileRead' }
 
 ##
+# @GuestFileWrite
+#
+# Result of guest agent file-write operation
+#
+# @count: number of bytes written (note: count is actual bytes
+#         written, after base64-decoding of provided buffer)
+#
+# @eof: whether EOF was encountered during write operation.
+#
+# Since: 0.15.0
+##
+{ 'type': 'GuestFileWrite',
+  'data': { 'count': 'int', 'eof': 'bool' } }
+
+##
 # @guest-file-write:
 #
 # Write to an open file in the guest.
@@ -130,17 +183,29 @@
 # @count: #optional bytes to write (actual bytes, after base64-decode),
 #         default is all content in buf-b64 buffer after base64 decoding
 #
-# Returns: GuestFileWrite on success. Note: count is the number of bytes
-#          base64-decoded bytes written
+# Returns: @GuestFileWrite on success.
 #
 # Since: 0.15.0
 ##
-{ 'type': 'GuestFileWrite',
-  'data': { 'count': 'int', 'eof': 'bool' } }
 { 'command': 'guest-file-write',
   'data':    { 'handle': 'int', 'buf-b64': 'str', '*count': 'int' },
   'returns': 'GuestFileWrite' }
 
+
+##
+# @GuestFileSeek
+#
+# Result of guest agent file-seek operation
+#
+# @position: current file position
+#
+# @eof: whether EOF was encountered during file seek
+#
+# Since: 0.15.0
+##
+{ 'type': 'GuestFileSeek',
+  'data': { 'position': 'int', 'eof': 'bool' } }
+
 ##
 # @guest-file-seek:
 #
@@ -154,13 +219,10 @@
 #
 # @whence: SEEK_SET, SEEK_CUR, or SEEK_END, as with fseek()
 #
-# Returns: GuestFileSeek on success.
+# Returns: @GuestFileSeek on success.
 #
 # Since: 0.15.0
 ##
-{ 'type': 'GuestFileSeek',
-  'data': { 'position': 'int', 'eof': 'bool' } }
-
 { 'command': 'guest-file-seek',
   'data':    { 'handle': 'int', 'offset': 'int', 'whence': 'int' },
   'returns': 'GuestFileSeek' }
@@ -180,18 +242,32 @@
   'data': { 'handle': 'int' } }
 
 ##
-# @guest-fsfreeze-status:
+# @GuestFsFreezeStatus
 #
-# Get guest fsfreeze state. error state indicates failure to thaw 1 or more
-# previously frozen filesystems, or failure to open a previously cached
-# filesytem (filesystem unmounted/directory changes, etc).
+# An enumation of filesystem freeze states
 #
-# Returns: GuestFsfreezeStatus ("thawed", "frozen", etc., as defined below)
+# @thawed: filesystems thawed/unfrozen
+#
+# @frozen: all non-network guest filesystems frozen
+#
+# @error: failure to thaw 1 or more
+#         previously frozen filesystems, or failure to open a previously
+#         cached filesytem (filesystem unmounted/directory changes, etc).
 #
 # Since: 0.15.0
 ##
 { 'enum': 'GuestFsfreezeStatus',
   'data': [ 'thawed', 'frozen', 'error' ] }
+
+##
+# @guest-fsfreeze-status:
+#
+# Get guest fsfreeze state. error state indicates
+#
+# Returns: GuestFsfreezeStatus ("thawed", "frozen", etc., as defined below)
+#
+# Since: 0.15.0
+##
 { 'command': 'guest-fsfreeze-status',
   'returns': 'GuestFsfreezeStatus' }
 
Signature · 1 line
1.7.4.1
#0 lore raw

v3 cover qemu-ga: add support for Windows

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v3

CHANGES SINCE V2:

- Fixed long-standing bug in getopt usage that triggered on win32 (Matsuda)
- Treat ERROR_OPERATION_ABORTED on read from unconnected/empty channel as eof (Matsuda)

CHANGES SINCE V1:

- Dropped guest-set-support-level patch dependency
- Rebased on master and re-tested
- Spelling/grammar fixes in commits/comments

OVERVIEW:

These patches add support for Windows to the QEMU guest agent. With these patches the following guest agent commands are supported on Windows:

guest-ping
guest-info
guest-sync
guest-shutdown

The guest-file* commands can essentially be enabled for Windows as-is, but since mingw does not honor the O_NONBLOCK flag, they'll need to be reworked if we're to retain the current non-blocking behavior.

The rest of the commands are currently stubbed out for Windows (qemu-ga will return an "unsupported" error), but it should be easy to implement these going forward with basic Windows support/infrastructure in place.

The build was tested using Fedora15 with a MinGW cross-build target via:

configure --enable-guest-agent --cross-prefix=i686-pc-mingw32- make qemu-ga.exe

The executable was tested using Windows XP SP3, and partially tested using Windows Server 2008 and Windows 7 (no I/O for the latter 2, having issues with virtio-win drivers). GLib 2.28+ for Windows is required. You can install qemu-ga as a load-on-boot service by running:

./qemu-ga --service install

And start/stop manually via:

net start qemu-ga
net stop qemu-ga

Many thanks to Gal Hammer for contributing the service integration and shutdown code.

 Makefile                   |    2 +-
 Makefile.objs              |    8 +-
 configure                  |    2 +-
 qapi-schema-guest.json     |  118 ++++++++--
 qemu-ga.c                  |  423 ++++++++++++++------------------
 qga/channel-posix.c        |  246 +++++++++++++++++++
 qga/channel-win32.c        |  340 +++++++++++++++++++++++++
 qga/channel.h              |   33 +++
 qga/commands-posix.c       |  528 +++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c       |  130 ++++++++++
 qga/commands.c             |   73 ++++++
 qga/guest-agent-commands.c |  585 --------------------------------------------
 qga/guest-agent-core.h     |    3 +-
 qga/service-win32.c        |  114 +++++++++
 qga/service-win32.h        |   30 +++
 15 files changed, 1790 insertions(+), 845 deletions(-)
#4 lore raw

PATCH v3 4/8 qemu-ga: rename guest-agent-commands.c -> commands-posix.c

---
 Makefile.objs              |    2 +-
 qga/commands-posix.c       |  528 ++++++++++++++++++++++++++++++++++++++++++++
 qga/guest-agent-commands.c |  528 --------------------------------------------
 3 files changed, 529 insertions(+), 529 deletions(-)
 create mode 100644 qga/commands-posix.c
 delete mode 100644 qga/guest-agent-commands.c
Makefile.objs +1 -1
index 0ec5874..a6297d2 100644
@@ -422,7 +422,7 @@ common-obj-y += qmp.o hmp.o
 ######################################################################
 # guest agent
 
-qga-nested-y = commands.o guest-agent-commands.o guest-agent-command-state.o
+qga-nested-y = commands.o commands-posix.o guest-agent-command-state.o
 qga-nested-y += channel-posix.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
qga/commands-posix.c new file +528 -0

528 lines not on this page · raw message

qga/guest-agent-commands.c deleted +0 -528

528 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#7 lore raw

PATCH v3 7/8 qemu-ga: add Windows service integration

In reply to Michael Roth

This allows qemu-ga to function as a Windows service:

- to install the service (will auto-start on boot):

     qemu-ga --service install
 - to start the service:
     net start qemu-ga
 - to stop the service:
     net stop qemu-ga
 - to uninstall service:
     qemu-ga --service uninstall

Original patch by Gal Hammer <[email protected]>

---
 Makefile.objs       |    2 +-
 qemu-ga.c           |  113 ++++++++++++++++++++++++++++++++++++++++++++++----
 qga/service-win32.c |  114 +++++++++++++++++++++++++++++++++++++++++++++++++++
 qga/service-win32.h |   30 +++++++++++++
 4 files changed, 249 insertions(+), 10 deletions(-)
 create mode 100644 qga/service-win32.c
 create mode 100644 qga/service-win32.h
Makefile.objs +1 -1
index 5e8fed9..c8d7563 100644
@@ -424,7 +424,7 @@ common-obj-y += qmp.o hmp.o
 
 qga-nested-y = commands.o guest-agent-command-state.o
 qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
-qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o
+qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o service-win32.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o module.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
qemu-ga.c +104 -9

175 lines not on this page · raw message

qga/service-win32.c new file +114 -0

114 lines not on this page · raw message

qga/service-win32.h new file +30 -0
new file mode 100644
index 0000000..99dfc53
@@ -0,0 +1,30 @@
+/*
+ * QEMU Guest Agent helpers for win32 service management
+ *
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Gal Hammer        <[email protected]>
+ *  Michael Roth      <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QGA_SERVICE_H
+#define QGA_SERVICE_H
+
+#include <windows.h>
+
+#define QGA_SERVICE_DISPLAY_NAME "QEMU Guest Agent"
+#define QGA_SERVICE_NAME         "qemu-ga"
+#define QGA_SERVICE_DESCRIPTION  "Enables integration with QEMU machine emulator and virtualizer."
+
+typedef struct GAService {
+    SERVICE_STATUS status;
+    SERVICE_STATUS_HANDLE status_handle;
+} GAService;
+
+int ga_install_service(const char *path, const char *logfile);
+int ga_uninstall_service(void);
+
+#endif
Signature · 1 line
1.7.4.1
#6 lore raw

PATCH v3 6/8 qemu-ga: add initial win32 support

In reply to Michael Roth

This adds a win32 channel implementation that makes qemu-ga functional on Windows using virtio-serial (unix-listen/isa-serial not currently implemented). Unlike with the posix implementation, we do not use GIOChannel for the following reasons:

- glib calls stat() on an fd to check whether S_IFCHR is set, which is the case for virtio-serial on win32. Because of that, a one-time check to determine whether the channel is readable is done by making a call to PeekConsoleInput(), which reports the underlying handle is not a valid console handle, and thus we can never read from the channel.

- if one goes as far as to "trick" glib into thinking it is a normal file descripter, the buffering is done in such a way that data written to the output stream will subsequently result in that same data being read back as if it were input, causing an error loop. furthermore, a forced flush of the channel only moves the data into a secondary buffer managed by glib, so there's no way to prevent output from getting read back as input.

The implementation here ties into the glib main loop by implementing a custom GSource that continually submits asynchronous/overlapped I/O to fill an GAChannel-managed read buffer, and tells glib to poll the corresponding event handle for a completion whenever there is no data/RPC in the read buffer to notify the main application about.

---
 Makefile.objs       |    2 +-
 qemu-ga.c           |    4 +
 qga/channel-win32.c |  340 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 345 insertions(+), 1 deletions(-)
 create mode 100644 qga/channel-win32.c
Makefile.objs +1 -1
index 5f05afd..5e8fed9 100644
@@ -424,7 +424,7 @@ common-obj-y += qmp.o hmp.o
 
 qga-nested-y = commands.o guest-agent-command-state.o
 qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
-qga-nested-$(CONFIG_WIN32) += commands-win32.o
+qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o module.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
qemu-ga.c +4 -0
index 93ebc3e..8e517b5 100644
@@ -30,7 +30,11 @@
 #include "qapi/qmp-core.h"
 #include "qga/channel.h"
 
+#ifndef _WIN32
 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
+#else
+#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
+#endif
 #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
 
 struct GAState {
qga/channel-win32.c new file +340 -0

340 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#8 lore raw

PATCH v3 8/8 qemu-ga: add win32 guest-shutdown command

In reply to Michael Roth

Implement guest-shutdown RPC for Windows. Functionally this should be equivalent to the posix implementation.

Original patch by Gal Hammer <[email protected]>

---
 qga/commands-win32.c |   41 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 40 insertions(+), 1 deletions(-)
qga/commands-win32.c +40 -1
index d96f1ad..4aa0f0d 100644
@@ -15,9 +15,48 @@
 #include "qga-qmp-commands.h"
 #include "qerror.h"
 
+#ifndef SHTDN_REASON_FLAG_PLANNED
+#define SHTDN_REASON_FLAG_PLANNED 0x80000000
+#endif
+
 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
 {
-    error_set(err, QERR_UNSUPPORTED);
+    HANDLE token;
+    TOKEN_PRIVILEGES priv;
+    UINT shutdown_flag = EWX_FORCE;
+
+    slog("guest-shutdown called, mode: %s", mode);
+
+    if (!has_mode || strcmp(mode, "powerdown") == 0) {
+        shutdown_flag |= EWX_POWEROFF;
+    } else if (strcmp(mode, "halt") == 0) {
+        shutdown_flag |= EWX_SHUTDOWN;
+    } else if (strcmp(mode, "reboot") == 0) {
+        shutdown_flag |= EWX_REBOOT;
+    } else {
+        error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
+                  "halt|powerdown|reboot");
+        return;
+    }
+
+    /* Request a shutdown privilege, but try to shut down the system
+       anyway. */
+    if (OpenProcessToken(GetCurrentProcess(),
+        TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token))
+    {
+        LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
+            &priv.Privileges[0].Luid);
+
+        priv.PrivilegeCount = 1;
+        priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+
+        AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0);
+    }
+
+    if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) {
+        slog("guest-shutdown failed: %d", GetLastError());
+        error_set(err, QERR_UNDEFINED_ERROR);
+    }
 }
 
 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
Signature · 1 line
1.7.4.1
#5 lore raw

PATCH v3 5/8 qemu-ga: fixes for win32 build of qemu-ga

In reply to Michael Roth

Various stubs and #ifdefs to compile for Windows using mingw cross-build. Still has 1 linker error due to a dependency on the forthcoming win32 versions of the GAChannel/transport class.

---
 Makefile             |    2 +-
 Makefile.objs        |    9 +++--
 configure            |    2 +-
 qemu-ga.c            |   16 +++++++++
 qga/commands-win32.c |   91 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 114 insertions(+), 6 deletions(-)
 create mode 100644 qga/commands-win32.c
Makefile +1 -1
index e66e885..2d9ebdc 100644
@@ -201,7 +201,7 @@ QGALIB_GEN=$(addprefix $(qapi-dir)/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-c
 $(QGALIB_OBJ): $(QGALIB_GEN) $(GENERATED_HEADERS)
 $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) $(GENERATED_HEADERS)
 
-qemu-ga$(EXESUF): qemu-ga.o $(qga-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qobject-obj-y) $(version-obj-y) $(QGALIB_OBJ)
+qemu-ga$(EXESUF): qemu-ga.o $(qga-obj-y) $(tools-obj-y) $(qapi-obj-y) $(qobject-obj-y) $(version-obj-y) $(QGALIB_OBJ)
 
 QEMULIBS=libhw32 libhw64 libuser libdis libdis-user
 
Makefile.objs +5 -4
index a6297d2..5f05afd 100644
@@ -422,12 +422,13 @@ common-obj-y += qmp.o hmp.o
 ######################################################################
 # guest agent
 
-qga-nested-y = commands.o commands-posix.o guest-agent-command-state.o
-qga-nested-y += channel-posix.o
+qga-nested-y = commands.o guest-agent-command-state.o
+qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
+qga-nested-$(CONFIG_WIN32) += commands-win32.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
-qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
+qga-obj-y += qemu-ga.o module.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
-qga-obj-$(CONFIG_POSIX) += oslib-posix.o
+qga-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-sockets.o qemu-option.o
 
 vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
 
configure +1 -1
index 763db24..4b0ce71 100755
@@ -509,7 +509,7 @@ if test "$mingw32" = "yes" ; then
   bindir="\${prefix}"
   sysconfdir="\${prefix}"
   confsuffix=""
-  guest_agent="no"
+  libs_qga="-lws2_32 -lwinmm $lib_qga"
 fi
 
 werror=""
qemu-ga.c +16 -0
index 2e8af02..93ebc3e 100644
@@ -15,7 +15,9 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <getopt.h>
+#ifndef _WIN32
 #include <syslog.h>
+#endif
 #include "json-streamer.h"
 #include "json-parser.h"
 #include "qint.h"
@@ -44,6 +46,7 @@ struct GAState {
 
 static struct GAState *ga_state;
 
+#ifndef _WIN32
 static void quit_handler(int sig)
 {
     g_debug("received signal num %d, quitting", sig);
@@ -73,6 +76,7 @@ static gboolean register_signal_handlers(void)
     }
     return true;
 }
+#endif
 
 static void usage(const char *cmd)
 {
@@ -87,7 +91,9 @@ static void usage(const char *cmd)
 "  -f, --pidfile     specify pidfile (default is %s)\n"
 "  -v, --verbose     log extra debugging information\n"
 "  -V, --version     print version information and exit\n"
+#ifndef _WIN32
 "  -d, --daemonize   become a daemon\n"
+#endif
 "  -b, --blacklist   comma-separated list of RPCs to disable (no spaces, \"?\""
 "                    to list available RPCs)\n"
 "  -h, --help        display this help and exit\n"
@@ -143,9 +149,13 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
     }
 
     level &= G_LOG_LEVEL_MASK;
+#ifndef _WIN32
     if (domain && strcmp(domain, "syslog") == 0) {
         syslog(LOG_INFO, "%s: %s", level_str, msg);
     } else if (level & s->log_level) {
+#else
+    if (level & s->log_level) {
+#endif
         g_get_current_time(&time);
         fprintf(s->log_file,
                 "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg);
@@ -153,6 +163,7 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
     }
 }
 
+#ifndef _WIN32
 static void become_daemon(const char *pidfile)
 {
     pid_t pid, sid;
@@ -203,6 +214,7 @@ fail:
     g_critical("failed to daemonize");
     exit(EXIT_FAILURE);
 }
+#endif
 
 static int send_response(GAState *s, QObject *payload)
 {
@@ -466,10 +478,12 @@ int main(int argc, char **argv)
         }
     }
 
+#ifndef _WIN32
     if (daemonize) {
         g_debug("starting daemon");
         become_daemon(pidfile);
     }
+#endif
 
     s = g_malloc0(sizeof(GAState));
     s->log_file = log_file;
@@ -482,10 +496,12 @@ int main(int argc, char **argv)
     ga_command_state_init_all(s->command_state);
     json_message_parser_init(&s->parser, process_event);
     ga_state = s;
+#ifndef _WIN32
     if (!register_signal_handlers()) {
         g_critical("failed to register signal handlers");
         goto out_bad;
     }
+#endif
 
     s->main_loop = g_main_loop_new(NULL, false);
     if (!channel_init(ga_state, method, path)) {
qga/commands-win32.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#2 lore raw

PATCH v3 2/8 qemu-ga: move channel/transport functionality into wrapper class

In reply to Michael Roth

This is mostly in preparation for the win32 port, which won't use GIO channels for reasons that will be made clearer later. Here the GAChannel class is just a loose wrapper around GIOChannel calls/callbacks, but we also roll in the logic/configuration for various channel types and managing unix socket connections, which makes the abstraction much more complete and further aids in the win32 port since isa-serial/unix-listen will not be supported initially.

There's also a bit of refactoring in the main logic to consolidate the exit paths so we can do common cleanup for things like pid files, which weren't always cleaned up previously.

---
 Makefile.objs          |    1 +
 qemu-ga.c              |  306 ++++++++++++------------------------------------
 qga/channel-posix.c    |  246 ++++++++++++++++++++++++++++++++++++++
 qga/channel.h          |   33 +++++
 qga/guest-agent-core.h |    2 +-
 5 files changed, 355 insertions(+), 233 deletions(-)
 create mode 100644 qga/channel-posix.c
 create mode 100644 qga/channel.h
Makefile.objs +1 -0
index 391e524..a9c4d51 100644
@@ -423,6 +423,7 @@ common-obj-y += qmp.o hmp.o
 # guest agent
 
 qga-nested-y = guest-agent-commands.o guest-agent-command-state.o
+qga-nested-y += channel-posix.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
qemu-ga.c +74 -232

444 lines not on this page · raw message

qga/channel-posix.c new file +246 -0

246 lines not on this page · raw message

qga/channel.h new file +33 -0
new file mode 100644
index 0000000..3704ea9
@@ -0,0 +1,33 @@
+/*
+ * QEMU Guest Agent channel declarations
+ *
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Michael Roth      <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QGA_CHANNEL_H
+#define QGA_CHANNEL_H
+
+#include <glib.h>
+
+typedef struct GAChannel GAChannel;
+
+typedef enum {
+    GA_CHANNEL_VIRTIO_SERIAL,
+    GA_CHANNEL_ISA_SERIAL,
+    GA_CHANNEL_UNIX_LISTEN,
+} GAChannelMethod;
+
+typedef gboolean (*GAChannelCallback)(GIOCondition condition, gpointer opaque);
+
+GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
+                          GAChannelCallback cb, gpointer opaque);
+void ga_channel_free(GAChannel *c);
+GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count);
+GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size);
+
+#endif
qga/guest-agent-core.h +1 -1
index e42b91d..6148d10 100644
@@ -14,7 +14,7 @@
 #include "qemu-common.h"
 
 #define QGA_VERSION "1.0"
-#define QGA_READ_COUNT_DEFAULT 4 << 10
+#define QGA_READ_COUNT_DEFAULT 4096
 
 typedef struct GAState GAState;
 typedef struct GACommandState GACommandState;
Signature · 1 line
1.7.4.1