qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: itamar.tal4@gmail.com
To: qemu-devel@nongnu.org
Cc: ori@guardicore.com, ariel@guardicore.com,
	mdroth@linux.vnet.ibm.com, pavel@guardicore.com,
	Itamar Tal <itamar@guardicore.com>
Subject: [Qemu-devel] [PATCH 3/4] added qga system uptime and hostname commands
Date: Tue, 31 Mar 2015 11:34:36 +0300	[thread overview]
Message-ID: <1427790877-14544-4-git-send-email-itamar@guardicore.com> (raw)
In-Reply-To: <1427790877-14544-1-git-send-email-itamar@guardicore.com>

From: Itamar Tal <itamar@guardicore.com>

This patch add more missing commands to the qga (both Windows and Linux):
- retrieving system uptime in seconds
- retrieving system hostname

---
 qga/commands-posix.c   | 13 +++++++++++++
 qga/commands-win32.c   | 22 ++++++++++++++++++++++
 qga/commands.c         | 18 ++++++++++++++++++
 qga/guest-agent-core.h |  2 ++
 qga/main.c             |  5 +++++
 qga/qapi-schema.json   | 25 +++++++++++++++++++++++++
 6 files changed, 85 insertions(+)

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 028d533..38b639c 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -17,6 +17,7 @@
 #include <glib.h>
 #include <sys/types.h>
 #include <sys/ioctl.h>
+#include <sys/sysinfo.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <errno.h>
@@ -2479,3 +2480,15 @@ GuestFileAttributes *qmp_guest_file_attributes(const char *path, Error **errp)
 
     return file_stat;
 }
+
+uint32_t qmp_guest_uptime(Error **errp)
+{
+    struct sysinfo sys_info;
+    if (sysinfo(&sys_info)) {
+        error_setg(errp, "Failed reading system info");
+    }
+
+    return sys_info.uptime;
+}
+
+
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index aeb49c5..6987f4e 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -385,6 +385,21 @@ static void guest_file_init(void)
     QTAILQ_INIT(&guest_file_state.filehandles);
 }
 
+int ga_win_commands_init(void)
+{
+    WSADATA wsa_data = {0};
+
+    if (WSAStartup(MAKEWORD(2, 2), &wsa_data)) {
+        g_critical("failed to initialize WSA engine");
+        goto error;
+    }
+
+    return 1;
+
+error:
+    return 0;
+}
+
 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
 {
     error_set(errp, QERR_UNSUPPORTED);
@@ -881,3 +896,10 @@ GuestFileAttributes *qmp_guest_file_attributes(const char *path, Error **errp)
     return result;
 }
 
+uint32_t qmp_guest_uptime(Error **errp)
+{
+    uint32_t uptime_milli = GetTickCount();
+    return uptime_milli / 1000;
+}
+
+
diff --git a/qga/commands.c b/qga/commands.c
index 64404d6..3acc95c 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -121,3 +121,21 @@ void qmp_guest_file_remove(const char *path, Error **errp)
         errno = 0;
     }
 }
+
+char *qmp_guest_get_hostname(Error **errp)
+{
+    char hostname[64];
+
+    if (gethostname(hostname, 64)) {
+#ifdef _WIN32
+        error_setg(errp, "Error getting hostname (error %d)",
+                   (int)WSAGetLastError());
+#else
+        error_setg(errp, "Error getting hostname (error %d)", errno);
+        errno = 0;
+#endif
+        return NULL;
+    }
+
+    return g_strdup(hostname);
+}
diff --git a/qga/guest-agent-core.h b/qga/guest-agent-core.h
index b496c47..49cbb19 100644
--- a/qga/guest-agent-core.h
+++ b/qga/guest-agent-core.h
@@ -44,6 +44,8 @@ int64_t ga_get_fd_handle(GAState *s, Error **errp);
 
 #ifndef _WIN32
 void reopen_fd_to_null(int fd);
+#else
+int ga_win_commands_init(void);
 #endif
 
 #endif /* _GUEST_AGENT_CORE_H_ */
diff --git a/qga/main.c b/qga/main.c
index 9939a2b..8365349 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -1163,6 +1163,11 @@ int main(int argc, char **argv)
         g_critical("failed to register signal handlers");
         goto out_bad;
     }
+#else
+    if (!ga_win_commands_init()) {
+        g_critical("failed initializing commands module");
+        goto out_bad;
+    }
 #endif
 
     s->main_loop = g_main_loop_new(NULL, false);
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index cc670b2..61f8a5a 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -946,3 +946,28 @@
 ##
 { 'command': 'guest-file-remove',
   'data': { 'path': 'str' }}
+
+##
+# @guest-get-hostname:
+#
+# Get the hostname of the guest's operating system
+#
+# Returns: hostname string.
+#
+# Since 2.4
+##
+{ 'command': 'guest-get-hostname',
+  'returns': 'str' }
+
+##
+# @guest-uptime:
+#
+# Get the time in seconds since the guest machine operating system was started
+#
+# Returns: uptime in seconds
+#
+# Since 2.4
+##
+{ 'command': 'guest-uptime',
+  'returns': 'uint32' }
+
-- 
2.3.4

  parent reply	other threads:[~2015-03-31  8:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-31  8:34 [Qemu-devel] [PATCH 0/4] qga added functionality itamar.tal4
2015-03-31  8:34 ` [Qemu-devel] [PATCH 1/4] added qga hash file command (win/linux) itamar.tal4
2015-03-31  8:34 ` [Qemu-devel] [PATCH 2/4] added missing file commands (attributes, deletion) itamar.tal4
2015-03-31  8:34 ` itamar.tal4 [this message]
2015-03-31  8:34 ` [Qemu-devel] [PATCH 4/4] added qga processes and connections enumeration commands (win/linux) itamar.tal4

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1427790877-14544-4-git-send-email-itamar@guardicore.com \
    --to=itamar.tal4@gmail.com \
    --cc=ariel@guardicore.com \
    --cc=itamar@guardicore.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=ori@guardicore.com \
    --cc=pavel@guardicore.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).