Skip to contentSkip to conversation

qga support process list, netstat and file stat/delete

PATCH In review [email protected] 2 participants 2 messages Mar 25, 2015 – Mar 26, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 qga support process list, netstat and file stat/delete

From: Itamar Tal <[email protected]>

this patch add support for some more functionality in the qemu-guest-agent, both for windows and linux. Main added features are:
- interface listing in Windows
- Process list in Windows
- network connections enumeration in Windows
- file delete for both Windows and Posix
- file stat() for both Windows and Posix
- system uptime for both Windows and Posix

Itamar,
Guardicore
[email protected]

---
 qga/commands-posix.c    |  49 +++++
 qga/commands-win32.c    | 552 +++++++++++++++++++++++++++++++++++++++++++++++-
 qga/commands.c          |  25 +++
 qga/guest-agent-core.h  |   2 +
 qga/main.c              |   9 +
 qga/qapi-schema.json    | 172 +++++++++++++++
 qga/win32-definitions.h | 115 ++++++++++
 qga/win32-iptypes.h     | 411 +++++++++++++++++++++++++++++++++++
 8 files changed, 1332 insertions(+), 3 deletions(-)
 create mode 100644 qga/win32-definitions.h
 create mode 100644 qga/win32-iptypes.h
qga/commands-posix.c +49 -0
index ba8de62..15e39fc 100644
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <inttypes.h>
+#include <sys/sysinfo.h>
 #include "qga/guest-agent-core.h"
 #include "qga-qmp-commands.h"
 #include "qapi/qmp/qerror.h"
@@ -2456,3 +2457,51 @@ void ga_command_state_init(GAState *s, GACommandState *cs)
 #endif
     ga_command_state_add(cs, guest_file_init, NULL);
 }
+
+uint64_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;
+}
+
+GuestFileStat *qmp_guest_file_stat(const char *path, Error **errp)
+{
+	GuestFileStat * file_stat = g_malloc0(sizeof(GuestFileStat));
+	struct stat file_os_stat;
+
+	if (stat(path, &file_os_stat))
+	{
+		error_setg(errp, "Failed to get file stat() for file '%s'", path);
+		return NULL;
+	}
+
+	file_stat->mode = file_os_stat.st_mode;
+	file_stat->inode = file_os_stat.st_ino;
+	file_stat->dev = file_os_stat.st_dev;
+	file_stat->nlink = file_os_stat.st_nlink;
+	file_stat->uid = file_os_stat.st_uid;
+	file_stat->gid = file_os_stat.st_gid;
+	file_stat->size = file_os_stat.st_size;
+	file_stat->atime = file_os_stat.st_atime;
+	file_stat->mtime = file_os_stat.st_mtime;
+	file_stat->ctime = file_os_stat.st_ctime;
+
+	return file_stat;
+}
+
+GuestProcessInfoList *qmp_guest_get_process_list(Error **errp)
+{
+	error_set(errp, QERR_UNSUPPORTED);
+	return NULL;
+}
+
+GuestActiveConnectionList *qmp_guest_get_active_connections(Error **errp)
+{
+	error_set(errp, QERR_UNSUPPORTED);
+	return NULL;
+}
qga/commands-win32.c +549 -3

583 lines not on this page · raw message

qga/commands.c +25 -0
index 7834967..2fb364c 100644
@@ -70,3 +70,28 @@ struct GuestAgentInfo *qmp_guest_info(Error **errp)
     qmp_for_each_command(qmp_command_info, info);
     return info;
 }
+
+void qmp_guest_file_delete(const char *path, Error **errp)
+{
+	if (unlink(path))
+	{
+		error_setg(errp, "Error deleting file '%s'", path);
+	}
+}
+
+char *qmp_guest_get_hostname(Error **errp)
+{
+	char hostname[64];
+
+	if (gethostname(hostname, 64))
+	{
+#ifdef _WIN32
+		error_setg(errp, "Error getting hostname (%d)", WSAGetLastError());
+#else
+		error_setg(errp, "Error getting hostname");
+#endif
+		return NULL;
+	}
+
+	return g_strdup(hostname);
+}
qga/guest-agent-core.h +2 -0
index e92c6ab..89d4165 100644
@@ -40,4 +40,6 @@ 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
qga/main.c +9 -0
index 9939a2b..3992aac 100644
@@ -1163,6 +1163,15 @@ 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);
qga/qapi-schema.json +172 -0

175 lines not on this page · raw message

qga/win32-definitions.h new file +115 -0

115 lines not on this page · raw message

qga/win32-iptypes.h new file +411 -0

411 lines not on this page · raw message

Signature · 1 line
2.3.4
#1 lore raw
Quoted 1 line · On 03/25/2015 06:25 AM, [email protected] wrote:

From: Itamar Tal <[email protected]>

This patch says 3/3, but I see no 1/3 or 2/3 (let alone a 0/3 cover letter) in the mail archives.

Quoted 14 lines

this patch add support for some more functionality in the qemu-guest-agent, both for windows and linux. Main added features are:
- interface listing in Windows
- Process list in Windows
- network connections enumeration in Windows
- file delete for both Windows and Posix
- file stat() for both Windows and Posix
- system uptime for both Windows and Posix

Itamar,
Guardicore
[email protected]

Quoted 9 lines
---
 qga/commands-posix.c    |  49 +++++
 qga/commands-win32.c    | 552 +++++++++++++++++++++++++++++++++++++++++++++++-
 qga/commands.c          |  25 +++
 qga/guest-agent-core.h  |   2 +
 qga/main.c              |   9 +
 qga/qapi-schema.json    | 172 +++++++++++++++
 qga/win32-definitions.h | 115 ++++++++++
 qga/win32-iptypes.h     | 411 +++++++++++++++++++++++++++++++++++

That's awfully big. Please split it up into one or two closely-related qga commands per patch, rather than lots of commands all at once.

qga/qapi-schema.json line 900

Quoted 12 lines

+++ b/qga/qapi-schema.json

qga/qapi-schema.json +7 -0
@@ -891,3 +891,175 @@
 ##
 { 'command': 'guest-get-memory-block-info',
   'returns': 'GuestMemoryBlockInfo' }
+
+##
+# @GuestFileStat
+#
+# @st_mode: file access permissions mode
+# @st_ino: file inode id
+# @st_dev: file device node

Is this portable to be exposing? Windows in particular is notorious for not having decent ino/dev in its basic stat() call, and anything we do to fake it is likely to break someone's use case. I'd rather play it conservative and go with fewer fields that we know are portable than to try and expose all of struct stat, if we can't prove that the extra fields would be useful.

qga/qapi-schema.json line 904

Quoted 4 lines
qga/qapi-schema.json +4 -0
+# @st_nlink: number of links pointing to the file
+# @st_uid: file user id
+# @st_gid: file group id
+# @st_atime: file access time

Insufficient. Please report timestamps as a struct containing seconds since Epoch (1970) AND nanoseconds. Truncating timestamps to the nearest second by returning only an int is not friendly.

qga/qapi-schema.json line 906

Quoted 2 lines
qga/qapi-schema.json +2 -0
+# @st_mtime: file modification time
+# @st_ctime: file creation time

Absolutely not. ctime is NOT creation time, but last change time. Creation time is typically reported as Birthtime on decent Unix-y systems. Yes, many (but not all) filesystems track FOUR times per file, and even though POSIX only requires three timestamps, all of BSD, Linux, and Cygwin have their struct stat() report all four timestamps (where birthtime is an obvious all-0s for a filesystem that doesn't track it). (The fact that windows tracks BOTH birth time AND change time, but reports ctime as birthtime in its stat() call, doesn't help matters).

qga/qapi-schema.json line 907

Quoted 1 line
qga/qapi-schema.json +1 -0
+# @st_size: file size in bytes

The names documented here...[1]

Review line 909

Quoted 2 lines
(quoted diff) +2 -0
+#
+# Since: 2.3

You missed 2.3. The earliest this can go in is 2.4.

qga/qapi-schema.json line 913

Quoted 4 lines
qga/qapi-schema.json +4 -0
+##
+{ 'type': 'GuestFileStat',
+  'data': {'mode': 'int', 'inode': 'int', 'dev': 'int',
+  			'nlink': 'int', 'uid': 'int', 'gid': 'int',

TAB damage. Please make sure your patch passes ./scripts/checkpatch.pl before submitting.

qga/qapi-schema.json line 915

Quoted 2 lines
qga/qapi-schema.json +2 -0
+  			'size': 'uint64', 'atime': 'int', 'mtime': 'int',
+  			'ctime': 'int'

[1]...don't match the names actually used here.

qga/qapi-schema.json line 923

Quoted 8 lines
qga/qapi-schema.json +8 -0
+           }}
+
+##
+# @guest-file-stat:
+#
+# Get the stat() for a file in the guest's operating system
+#
+# Returns: hostname string.

Wrong copy-and-paste.

Review line 925

Quoted 2 lines
(quoted diff) +2 -0
+#
+# Since 2.3

2.4. (Won't mention it again)

qga/qapi-schema.json line 929

Quoted 4 lines
qga/qapi-schema.json +4 -0
+##
+{ 'command': 'guest-file-stat',
+  'data': { 'path': 'str' },
+  'returns': 'GuestFileStat' }

Are you planning on an fstat counterpart of an open handle? Are you planning on handling the symlink aspect of stat vs. lstat? Should you be providing more of an fstatat() interface where you can specify a name relative to an already-open directory handle?

qga/qapi-schema.json line 941

Quoted 12 lines
qga/qapi-schema.json +12 -0
+  
+##
+# @guest-file-delete:
+#
+# Delete a file in the guest's operating system
+#
+# Returns: 
+#
+# Since 2.3
+##
+{ 'command': 'guest-file-delete',
+  'data': { 'path': 'str' }}

Can this delete directories? If so, should it be named remove? Should it provide removeat() functionality of specifying a name relative to an already-open directory handle?

Quoted 9 lines
qga/qapi-schema.json +9 -0
+
+##
+# @guest-get-hostname:
+#
+# Get the hostname of the guest's operating system
+#
+# Returns: hostname string.
+#
+# Since 2.3

qga/qapi-schema.json line 960

Quoted 10 lines
qga/qapi-schema.json +10 -0
+##
+{ '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

Is this sufficient, or should you provide more resolution?

Quoted 10 lines
qga/qapi-schema.json +10 -0
+#
+# Since 2.3
+##
+{ 'command': 'guest-uptime',
+  'returns': 'uint64' }
+
+##
+# @GuestProcessInfo
+#
+# @process-id: the process unique id

qga/qapi-schema.json line 980

Quoted 10 lines
qga/qapi-schema.json +10 -0
+# @parent-id: the process parent unique id
+# @process-name: the name of the process
+# @image-path: full path of the process image
+# @session-id: the session id of the process
+#
+# Since: 2.3
+##
+{ 'type': 'GuestProcessInfo',
+  'data': {'process-id': 'int', 'parent-id': 'int', 'process-name': 'str',
+  			'image-path': 'str', 'session-id': 'int'}}

Do you really need this, or is the existing guest-file-open on /proc/... sufficient for getting at the same information without needing a new command?

qga/qapi-schema.json line 992

Quoted 12 lines
qga/qapi-schema.json +12 -0
+  
+##
+# @guest-get-process-list:
+#
+# Get the list of active processes on the guest operating system
+#
+# Returns: array of active processes
+#
+# Since 2.3
+##
+{ 'command': 'guest-get-process-list',
+  'returns': ['GuestProcessInfo'] }

Almost guaranteed to be stale (processes will probably come and go during the time that the guest agent is collecting data to return), and rather expensive to collect. Should it take an optional argument for filtering the results to a specific pid?

Quoted 63 lines
qga/qapi-schema.json +63 -0
+
+##
+# @GuestIpProtocol:
+#
+# An enumeration of supported IP protocols
+#
+# @tcp: TCP
+#
+# @udp: UDP
+#
+# Since: 2.3
+##
+{ 'enum': 'GuestIpProtocol',
+  'data': [ 'tcp', 'udp' ] }
+  
+##
+# @GuestTcpProtocolState:
+#
+# An enumeration of TCP connection state
+#
+# @closed: CLOSED
+# @listen: LISTEN
+# @syn-sent: SYN_SENT
+# @syn-rcvd: SYN_RCVD
+# @established: ESTABLISHED
+# @fin-wait1: FIN_WAIT1
+# @fin-wait2: FIN_WAIT2
+# @close-wait: CLOSE_WAIT
+# @closing: CLOSING
+# @last-ack: LAST_ACK
+# @time-wait: TIME_WAIT
+# @delete-tcb: DELETE_TCB
+#
+# Since: 2.3
+##
+{ 'enum': 'GuestTcpProtocolState',
+  'data': [ 'closed', 'listen', 'syn-sent', 'syn-rcvd', 'established',
+  			'fin-wait1', 'fin-wait2', 'close-wait', 'closing',
+  			'last-ack', 'time-wait', 'delete-tcb' ] }
+
+##
+# @GuestActiveConnection
+#
+# @if-family: 4 / 6
+# @protocol: TCP / UDP
+# @source-addr: the source IP address of the connection
+# @source-port: the source port of the connection
+# @dest-addr: the destination IP address of the connection
+# @dest-port: the destination port of the connection
+# @owner-process_id: the process unique id for the connection owner
+# @state: connection protocol state
+# @start-time: time where bind() was called for the connection
+#
+# Since: 2.3
+##
+{ 'type': 'GuestActiveConnection',
+  'data': {	'source-addr': 'str', 'source-port': 'int', 'dest-addr': 'str',
+  			'dest-port': 'int', 'owner-process_id': 'int', 'state': 'GuestTcpProtocolState',
+  			'if-family': 'GuestIpAddressType', 'protocol': 'GuestIpProtocol',
+  			'start-time': 'uint64'}}
+ 
+ ##
+# @guest-get-active-connections:

qga/qapi-schema.json line 1065

Quoted 10 lines
qga/qapi-schema.json +10 -0
+#
+# Get the list of active connections on the guest operating system
+#
+# Returns: array of active connections
+#
+# Since 2.3
+##
+{ 'command': 'guest-get-active-connections',
+  'returns': ['GuestActiveConnection'] }
+ 

I'm not sure if all of these commands make sense to add, but I _am_ sure that you are trying to add too much in one commit. Please break this up before resubmitting.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org