qga: Add 'guest-get-users' command
PATCH 1/1 qga: Add 'guest-get-users' command
From: Vinzenz Feenstra <[email protected]>
A command that will list all currenctly logged in users having running processes.
Examples:
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"domain":"LADIDA","user":"Administrator"}]}
--- qga/commands-posix.c | 35 +++++++++++++++++++++++ qga/commands-win32.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 21 ++++++++++++++ 3 files changed, 136 insertions(+)
qga/commands-posix.c +35 -0
@@ -15,6 +15,7 @@#include <sys/ioctl.h> #include <sys/wait.h> #include <dirent.h> +#include <utmpx.h> #include "qga/guest-agent-core.h" #include "qga-qmp-commands.h" #include "qapi/qmp/qerror.h"
@@ -2515,3 +2516,37 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup);#endif } + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new_full (g_str_hash, g_str_equal, +NULL ,NULL ); +GuestUserList *head =NULL , *cur_item =NULL ; +setutxent (); +for (;;) { +struct utmpx *user_info =getutxent (); +if (user_info ==NULL ) { +break ; + }else if (user_info->ut_type !=USER_PROCESS ) { +continue ; + }else if (g_hash_table_contains (cache, user_info->ut_user)) { +continue ; + } + +g_hash_table_insert (cache, user_info->ut_user,NULL ); + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user =g_strdup (user_info->ut_user); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +endutxent (); +g_hash_table_unref (cache); +return head; +}
qga/commands-win32.c +80 -0
83 lines not on this page · raw message
qga/qapi-schema.json +21 -0
@@ -1042,3 +1042,24 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active user accounts on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] }
Signature · 1 line
2.9.3
Hi
Quoted 7 lines · Vinzenz 'evilissimo' Feenstra
From: Vinzenz Feenstra <[email protected]>
A command that will list all currenctly logged in users having running processes.
Examples:
Quoted 9 lines
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"domain":"LADIDA","user":"Administrator"}]}
Signed-off-by Vinzenz Feenstra <[email protected]>---
Is there RFE bugs for these qga patches? If they are public, it would be worthwhile to add a link to your various qga patches.
Quoted 20 lines
qga/commands-posix.c | 35 +++++++++++++++++++++++ qga/commands-win32.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 21 ++++++++++++++ 3 files changed, 136 insertions(+)qga/commands-posix.c +1
-0@@ -15,6 +15,7 @@#include <sys/ioctl.h> #include <sys/wait.h> #include <dirent.h> +#include <utmpx.h> #include "qga/guest-agent-core.h" #include "qga-qmp-commands.h" #include "qapi/qmp/qerror.h"
qga/commands-posix.c void ga_command_state_init(GAState *s, · line 2523
Quoted 9 lines
qga/commands-posix.c +5
-0@@ -2515,3 +2516,37 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs ,NULL ,guest_fsfreeze_cleanup );#endif } + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new_full (g_str_hash, g_str_equal, +NULL ,NULL );
if there is no need for new_full() just use new() :)
qga/commands-posix.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 2532
Quoted 9 lines
qga/commands-posix.c +9
-0+GuestUserList *head =NULL , *cur_item =NULL ; +setutxent (); +for (;;) { +struct utmpx *user_info =getutxent (); +if (user_info ==NULL ) { +break ; + }else if (user_info->ut_type !=USER_PROCESS ) { +continue ; + }else if (g_hash_table_contains (cache, user_info->ut_user)) {
g_hash_table_contains requires glib 2.32, you should add a fallback code to glib-compat.
+ continue;
qga/commands-posix.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 2537
Quoted 5 lines
qga/commands-posix.c +4
-0+ } + +g_hash_table_insert (cache, user_info->ut_user,NULL ); +
That doesn't look safe to me, user_info->ut_user content may change after iterations. I think you should strdup the value instead.
Since you use the hashtable as a set, you could use g_hash_table_add.
qga/commands-posix.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 2541
Quoted 5 lines
qga/commands-posix.c +4
-0+GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user =g_strdup (user_info->ut_user); +
Or simply use the value of item->value->user, so you don't need to do extra dup/free.
+ if (!cur_item) {
Quoted 16 lines
qga/commands-posix.c +10
-0+ head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +endutxent (); +g_hash_table_unref (cache); +return head; +}qga/commands-win32.c +0
-0
qga/commands-win32.c void ga_command_state_init(GAState *s, · line 1543
Quoted 9 lines
qga/commands-win32.c +5
-0@@ -1536,3 +1536,83 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs ,NULL ,guest_fsfreeze_cleanup ); } } + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new_full (g_str_hash, g_str_equal, +NULL ,NULL );
same here
Quoted 82 lines
qga/commands-win32.c +23
-0+GuestUserList *head =NULL , *cur_item =NULL ; + +LPWKSTA_USER_INFO_1 buffer =NULL , iter_buffer =0 ; +DWORD level =1 ;/* Level 1 has more information */ +DWORD prefered_max_length =MAX_PREFERRED_LENGTH ; +DWORD entries_read =0 ; +DWORD total_entries =0 ; +DWORD resume_handle =0 ; +LPWSTR server_name =NULL ; +NET_API_STATUS result =ERROR_MORE_DATA ; + +while (result ==ERROR_MORE_DATA ) { + result =NetWkstaUserEnum ( + server_name, + level, + (LPBYTE *)&buffer, + prefered_max_length, + &entries_read, + &total_entries, + &resume_handle); + +if (result !=ERROR_MORE_DATA && result !=ERROR_SUCCESS ) { + error_setg_win32(err, result, "Failed to enumerate activeusers");
qga/commands-win32.c +52
-0+ goto error; + } + + iter_buffer = buffer; +DWORD i =0 ; +for (i =0 ; i < entries_read; ++i, ++iter_buffer) { +gchar *name =g_utf16_to_utf8 ( + iter_buffer->wkui1_username, +-1 ,NULL ,NULL ,NULL + ); + +if (name ==NULL ) { +continue ; + } + +if (g_hash_table_contains (cache, name)) { +g_free (name); +continue ; + } + +gchar *domain =g_utf16_to_utf8 ( + iter_buffer->wkui1_logon_domain, +-1 ,NULL ,NULL ,NULL + ); + +g_hash_table_insert (cache, name,NULL ); +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user = name; + item->value->domain = domain; + item->value->has_domain = true; + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } + } +NetApiBufferFree (buffer); +g_hash_table_unref (cache); +return head; + +error: +if (buffer !=NULL ) { +NetApiBufferFree (buffer); + } +g_hash_table_unref (cache); +qapi_free_GuestUserList (head); +return NULL ; +}qga/qapi-schema.json +0
-0
qga/qapi-schema.json line 1049
Quoted 8 lines
qga/qapi-schema.json +5
-0@@ -1042,3 +1042,24 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only)
Or use a flat union? (like suggested for get-osinfo)
qga/qapi-schema.json line 1054
Quoted 6 lines
qga/qapi-schema.json +5
-0+# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', '*domain': 'str' } }
+
Quoted 14 lines
qga/qapi-schema.json +10
-1+## +# @guest-get-users: +# Retrieves a list of currently active user accounts on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] } --2.9.3
Quoted 1 line
--
Marc-André Lureau
Quoted 8 lines · Marc-André Lureau
Hi
Quoted 3 lines · Vinzenz 'evilissimo' Feenstra
From: Vinzenz Feenstra <[email protected]>
A command that will list all currenctly logged in users having running
s/currenctly/currently/
Quoted 1 line
Quoted 1 line
processes.
Do you also want to include how long the user has been logged in, if available? (a 'num' for floating-point seconds may be the best unit for that).
Quoted 5 lines
Quoted 5 lines
+++
b/qga/qapi-schema.jsonqga/qapi-schema.json +0
-0@@ -1042,3 +1042,24 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' }
Quoted 8 lines
qga/qapi-schema.json line 1049
Quoted 6 lines
qga/qapi-schema.json +5
-0+ +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only)Or use a flat union? (like suggested for get-osinfo)
Here, I don't see much benefit to a flat union. The field is present only when the guest can supply it, and there is no obvious discriminator to mention whether the field must be present. I think the structure is just fine as proposed, unless you want to also add a duration parameter for how long the user has been logged in.
Quoted 3 lines
qga/qapi-schema.json line 1058
Quoted 3 lines
qga/qapi-schema.json +3
-0+## +# @guest-get-users: +# Retrieves a list of currently active user accounts on the VM.
Accounts that exist even if no one is logged in at the moment, or accounts that currently have someone logged in?
Quoted 14 lines
Quoted 11 lines
qga/qapi-schema.json +7
-1+# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] } --2.9.3
Quoted 1 line
--
Marc-André Lureau
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org