From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YcrdU-0000d2-VZ for qemu-devel@nongnu.org; Tue, 31 Mar 2015 04:35:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YcrdQ-0004Fg-9x for qemu-devel@nongnu.org; Tue, 31 Mar 2015 04:35:00 -0400 Received: from mail-wg0-x229.google.com ([2a00:1450:400c:c00::229]:34546) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YcrdQ-0004FM-0w for qemu-devel@nongnu.org; Tue, 31 Mar 2015 04:34:56 -0400 Received: by wgbdm7 with SMTP id dm7so10734106wgb.1 for ; Tue, 31 Mar 2015 01:34:55 -0700 (PDT) From: itamar.tal4@gmail.com Date: Tue, 31 Mar 2015 11:34:36 +0300 Message-Id: <1427790877-14544-4-git-send-email-itamar@guardicore.com> In-Reply-To: <1427790877-14544-1-git-send-email-itamar@guardicore.com> References: <1427790877-14544-1-git-send-email-itamar@guardicore.com> Subject: [Qemu-devel] [PATCH 3/4] added qga system uptime and hostname commands List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: ori@guardicore.com, ariel@guardicore.com, mdroth@linux.vnet.ibm.com, pavel@guardicore.com, Itamar Tal From: Itamar Tal 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 #include #include +#include #include #include #include @@ -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