[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 3/4] added qga system uptime and hostname commands
From: |
itamar . tal4 |
Subject: |
[Qemu-devel] [PATCH 3/4] added qga system uptime and hostname commands |
Date: |
Tue, 31 Mar 2015 11:34:36 +0300 |
From: Itamar Tal <address@hidden>
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