qga: Add 'guest-get-users' command
v4 cover qga: Add 'guest-get-users' command
From: Vinzenz Feenstra <[email protected]>
Changes since v3:
- Removed ifdef/endif around QGA_MICRO_SECOND_TO_SECOND
Changes since v2:
- Updated the documentation of login-time to be more precise what the value contains and what time is actually reported and in which format.
Changes since v1:
- fixed spelling issues in the commit message and schema
- added login-time field which specifies the time the users logged on
- applied changes suggested for glib hash table usage
- added new link time library dependency for wtsapi32
- setting now _WIN32_WINNT to 0x0600 if not defined globally, which is Windows Vista and/or Windows 2008 Server and higher
Vinzenz Feenstra (1):
qga: Add 'guest-get-users' command
configure | 2 +- include/glib-compat.h | 5 +++ qga/commands-posix.c | 52 ++++++++++++++++++++++++++++++ qga/commands-win32.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 24 ++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-)
Signature · 1 line
2.9.3
PATCH v4 1/1 qga: Add 'guest-get-users' command
From: Vinzenz Feenstra <[email protected]>
A command that will list all currently logged in users, and the time since when they are logged in.
Examples:
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490622289.903835,"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490351044.670552,"domain":"LADIDA", "user":"Administrator"}]}
--- configure | 2 +- include/glib-compat.h | 5 +++ qga/commands-posix.c | 52 ++++++++++++++++++++++++++++++ qga/commands-win32.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 24 ++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-)
configure +1 -1
@@ -742,7 +742,7 @@ if test "$mingw32" = "yes" ; then sysconfdir="\${prefix}" local_statedir= confsuffix="" - libs_qga="-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof -lwtsapi32 -liphlpapi -lnetapi32 $libs_qga" fi werror=""
include/glib-compat.h +5 -0
@@ -217,6 +217,11 @@ static inline void g_hash_table_add(GHashTable *hash_table, gpointer key) {g_hash_table_replace (hash_table, key, key); } + +static gboolean g_hash_table_contains (GHashTable *hash_table,gpointer key) +{ +return g_hash_table_lookup_extended (hash_table, key,NULL ,NULL ); +}#endif #ifndef g_assert_true
qga/commands-posix.c +52 -0
61 lines not on this page · raw message
qga/commands-win32.c +89 -0
104 lines not on this page · raw message
qga/qapi-schema.json +24 -0
@@ -1042,3 +1042,27 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# @login-time: Time of login of this user on the computer. If multiple +# instances of the user are logged in, the earliest login time is +# reported. The value is in fractional seconds since epoch time. +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', 'login-time': 'number', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active users on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] }
Signature · 1 line
2.9.3
Michael Roth
AppliedQuoting Vinzenz 'evilissimo' Feenstra (2017-04-13 05:41:17)
Quoted 7 lines
From: Vinzenz Feenstra <[email protected]>
A command that will list all currently logged in users, and the time since when they are logged in.
Examples:
Quoted 8 lines
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490622289.903835,"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490351044.670552,"domain":"LADIDA", "user":"Administrator"}]}
Signed-off-by Vinzenz Feenstra <[email protected]>
Thanks, applied to qga tree with some minor whitespace fix-ups:
https://github.com/mdroth/qemu/commits/qga
Quoted 250 lines
--- configure | 2 +- include/glib-compat.h | 5 +++ qga/commands-posix.c | 52 ++++++++++++++++++++++++++++++ qga/commands-win32.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 24 ++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-)configure +1
-1@@ -742,7 +742,7 @@ if test "$mingw32" = "yes" ; then sysconfdir="\${prefix}" local_statedir= confsuffix="" - libs_qga="-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof -lwtsapi32 -liphlpapi -lnetapi32 $libs_qga" fi werror=""include/glib-compat.h +5
-0@@ -217,6 +217,11 @@ static inline void g_hash_table_add(GHashTable *hash_table, gpointer key) {g_hash_table_replace (hash_table, key, key); } + +static gboolean g_hash_table_contains (GHashTable *hash_table,gpointer key) +{ +return g_hash_table_lookup_extended (hash_table, key,NULL ,NULL ); +}#endif #ifndef g_assert_trueqga/commands-posix.c +52
-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" @@ -2517,3 +2518,54 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup);#endif } + +#define QGA_MICRO_SECOND_TO_SECOND 1000000 + +static double ga_get_login_time (struct utmpx *user_info) +{ +double seconds = (double )user_info->ut_tv.tv_sec; +double useconds = (double )user_info->ut_tv.tv_usec; + useconds /=QGA_MICRO_SECOND_TO_SECOND ; +return seconds + useconds; +} + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +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)) { +gpointer value =g_hash_table_lookup (cache, user_info->ut_user); +GuestUser *user = (GuestUser *)value; +double login_time =ga_get_login_time (user_info); +/* We're ensuring the earliest login time to be sent */ +if (login_time < user->login_time) { + user->login_time = login_time; + } +continue ; + } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user =g_strdup (user_info->ut_user); + item->value->login_time =ga_get_login_time (user_info); + +g_hash_table_insert (cache, item->value->user, item->value); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +endutxent (); +g_hash_table_destroy (cache); +return head; +}qga/commands-win32.c +89
-0@@ -11,6 +11,9 @@* See the COPYING file in the top-level directory. */ +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0600 +#endif #include "qemu/osdep.h" #include <wtypes.h> #include <powrprof.h> @@ -25,6 +28,7 @@#include <initguid.h> #endif #include <lm.h> +#include <wtsapi32.h> #include "qga/guest-agent-core.h" #include "qga/vss-win32.h" @@ -1536,3 +1540,88 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup); } } + +/* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ +typedef struct _GA_WTSINFOA { +WTS_CONNECTSTATE_CLASS State; +DWORD SessionId; +DWORD IncomingBytes; +DWORD OutgoingBytes; +DWORD IncomingFrames; +DWORD OutgoingFrames; +DWORD IncomingCompressedBytes; +DWORD OutgoingCompressedBy; +CHAR WinStationName[WINSTATIONNAME_LENGTH ]; +CHAR Domain[DOMAIN_LENGTH ]; +CHAR UserName[USERNAME_LENGTH +1 ]; +LARGE_INTEGER ConnectTime; +LARGE_INTEGER DisconnectTime; +LARGE_INTEGER LastInputTime; +LARGE_INTEGER LogonTime; +LARGE_INTEGER CurrentTime; + +}GA_WTSINFOA ; + +GuestUserList *qmp_guest_get_users (Error **err) +{ +#if (_WIN32_WINNT >=0x0600 ) +#ifndef QGA_NANOSECONDS +# define QGA_NANOSECONDS 10000000 +#endif +#ifndef QGA_EPOCH_TIME_DIFF +# define QGA_EPOCH_TIME_DIFF 116444736000000000LL +#endif + +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +GuestUserList *head =NULL , *cur_item =NULL ; + +DWORD count =0 ; +WTS_SESSION_INFOA *entries =NULL ; +if (WTSEnumerateSessionsA (NULL ,0 ,1 , &entries, &count)) { +for (DWORD i =0 ; i < count; ++i) { +DWORD buffer_size =0 ; +GA_WTSINFOA *session_info =NULL ; +if (WTSQuerySessionInformationA ( +NULL , + entries[i].SessionId, + WTSSessionInfo, + (LPSTR *)&session_info, + &buffer_size + )) { +if (strlen (session_info->UserName) ==0 || +g_hash_table_contains (cache, session_info->UserName)) { +WTSFreeMemory (session_info); +continue ; + } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + + item->value->user =g_strdup (session_info->UserName); + item->value->domain =g_strdup (session_info->Domain); + item->value->has_domain = true; + +INT64 login = session_info->LogonTime.QuadPart; + login -=QGA_EPOCH_TIME_DIFF ; + item->value->login_time = ((double )login) /QGA_NANOSECONDS ; + +g_hash_table_add (cache, item->value->user); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +WTSFreeMemory (session_info); + } +WTSFreeMemory (entries); + } +g_hash_table_destroy (cache); +return head; +#else +error_setg (err,QERR_UNSUPPORTED ); +return NULL ; +#endif +}qga/qapi-schema.json +24
-0@@ -1042,3 +1042,27 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# @login-time: Time of login of this user on the computer. If multiple +# instances of the user are logged in, the earliest login time is +# reported. The value is in fractional seconds since epoch time. +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', 'login-time': 'number', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active users on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] }Signature · 1 line
2.9.3
Quoting Michael Roth (2017-04-13 15:30:06)
Quoted 15 lines
Quoting Vinzenz 'evilissimo' Feenstra (2017-04-13 05:41:17)
Quoted 14 lines
From: Vinzenz Feenstra <[email protected]>
A command that will list all currently logged in users, and the time since when they are logged in.
Examples:
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490622289.903835,"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490351044.670552,"domain":"LADIDA", "user":"Administrator"}]}
Quoted 5 lines
Quoted 1 line
Signed-off-by Vinzenz Feenstra <[email protected]>Thanks, applied to qga tree with some minor whitespace fix-ups:
Er, whoops, replied to the wrong patch sorry.
Quoted 253 lines
Quoted 251 lines
--- configure | 2 +- include/glib-compat.h | 5 +++ qga/commands-posix.c | 52 ++++++++++++++++++++++++++++++ qga/commands-win32.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 24 ++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-)configure +1
-1@@ -742,7 +742,7 @@ if test "$mingw32" = "yes" ; then sysconfdir="\${prefix}" local_statedir= confsuffix="" - libs_qga="-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof -lwtsapi32 -liphlpapi -lnetapi32 $libs_qga" fi werror=""include/glib-compat.h +5
-0@@ -217,6 +217,11 @@ static inline void g_hash_table_add(GHashTable *hash_table, gpointer key) {g_hash_table_replace (hash_table, key, key); } + +static gboolean g_hash_table_contains (GHashTable *hash_table,gpointer key) +{ +return g_hash_table_lookup_extended (hash_table, key,NULL ,NULL ); +}#endif #ifndef g_assert_trueqga/commands-posix.c +52
-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" @@ -2517,3 +2518,54 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup);#endif } + +#define QGA_MICRO_SECOND_TO_SECOND 1000000 + +static double ga_get_login_time (struct utmpx *user_info) +{ +double seconds = (double )user_info->ut_tv.tv_sec; +double useconds = (double )user_info->ut_tv.tv_usec; + useconds /=QGA_MICRO_SECOND_TO_SECOND ; +return seconds + useconds; +} + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +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)) { +gpointer value =g_hash_table_lookup (cache, user_info->ut_user); +GuestUser *user = (GuestUser *)value; +double login_time =ga_get_login_time (user_info); +/* We're ensuring the earliest login time to be sent */ +if (login_time < user->login_time) { + user->login_time = login_time; + } +continue ; + } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user =g_strdup (user_info->ut_user); + item->value->login_time =ga_get_login_time (user_info); + +g_hash_table_insert (cache, item->value->user, item->value); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +endutxent (); +g_hash_table_destroy (cache); +return head; +}qga/commands-win32.c +89
-0@@ -11,6 +11,9 @@* See the COPYING file in the top-level directory. */ +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0600 +#endif #include "qemu/osdep.h" #include <wtypes.h> #include <powrprof.h> @@ -25,6 +28,7 @@#include <initguid.h> #endif #include <lm.h> +#include <wtsapi32.h> #include "qga/guest-agent-core.h" #include "qga/vss-win32.h" @@ -1536,3 +1540,88 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup); } } + +/* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ +typedef struct _GA_WTSINFOA { +WTS_CONNECTSTATE_CLASS State; +DWORD SessionId; +DWORD IncomingBytes; +DWORD OutgoingBytes; +DWORD IncomingFrames; +DWORD OutgoingFrames; +DWORD IncomingCompressedBytes; +DWORD OutgoingCompressedBy; +CHAR WinStationName[WINSTATIONNAME_LENGTH ]; +CHAR Domain[DOMAIN_LENGTH ]; +CHAR UserName[USERNAME_LENGTH +1 ]; +LARGE_INTEGER ConnectTime; +LARGE_INTEGER DisconnectTime; +LARGE_INTEGER LastInputTime; +LARGE_INTEGER LogonTime; +LARGE_INTEGER CurrentTime; + +}GA_WTSINFOA ; + +GuestUserList *qmp_guest_get_users (Error **err) +{ +#if (_WIN32_WINNT >=0x0600 ) +#ifndef QGA_NANOSECONDS +# define QGA_NANOSECONDS 10000000 +#endif +#ifndef QGA_EPOCH_TIME_DIFF +# define QGA_EPOCH_TIME_DIFF 116444736000000000LL +#endif + +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +GuestUserList *head =NULL , *cur_item =NULL ; + +DWORD count =0 ; +WTS_SESSION_INFOA *entries =NULL ; +if (WTSEnumerateSessionsA (NULL ,0 ,1 , &entries, &count)) { +for (DWORD i =0 ; i < count; ++i) { +DWORD buffer_size =0 ; +GA_WTSINFOA *session_info =NULL ; +if (WTSQuerySessionInformationA ( +NULL , + entries[i].SessionId, + WTSSessionInfo, + (LPSTR *)&session_info, + &buffer_size + )) { +if (strlen (session_info->UserName) ==0 || +g_hash_table_contains (cache, session_info->UserName)) { +WTSFreeMemory (session_info); +continue ; + } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + + item->value->user =g_strdup (session_info->UserName); + item->value->domain =g_strdup (session_info->Domain); + item->value->has_domain = true; + +INT64 login = session_info->LogonTime.QuadPart; + login -=QGA_EPOCH_TIME_DIFF ; + item->value->login_time = ((double )login) /QGA_NANOSECONDS ; + +g_hash_table_add (cache, item->value->user); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +WTSFreeMemory (session_info); + } +WTSFreeMemory (entries); + } +g_hash_table_destroy (cache); +return head; +#else +error_setg (err,QERR_UNSUPPORTED ); +return NULL ; +#endif +}qga/qapi-schema.json +24
-0@@ -1042,3 +1042,27 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# @login-time: Time of login of this user on the computer. If multiple +# instances of the user are logged in, the earliest login time is +# reported. The value is in fractional seconds since epoch time. +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', 'login-time': 'number', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active users on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] }Signature · 1 line
2.9.3
Quoting Vinzenz 'evilissimo' Feenstra (2017-04-13 05:41:17)
Quoted 163 lines
From: Vinzenz Feenstra <[email protected]>
A command that will list all currently logged in users, and the time since when they are logged in.
Examples:
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490622289.903835,"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490351044.670552,"domain":"LADIDA", "user":"Administrator"}]}
Signed-off-by Vinzenz Feenstra <[email protected]>--- configure | 2 +- include/glib-compat.h | 5 +++ qga/commands-posix.c | 52 ++++++++++++++++++++++++++++++ qga/commands-win32.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 24 ++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-)configure +1
-1@@ -742,7 +742,7 @@ if test "$mingw32" = "yes" ; then sysconfdir="\${prefix}" local_statedir= confsuffix="" - libs_qga="-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof -lwtsapi32 -liphlpapi -lnetapi32 $libs_qga" fi werror=""include/glib-compat.h +5
-0@@ -217,6 +217,11 @@ static inline void g_hash_table_add(GHashTable *hash_table, gpointer key) {g_hash_table_replace (hash_table, key, key); } + +static gboolean g_hash_table_contains (GHashTable *hash_table,gpointer key) +{ +return g_hash_table_lookup_extended (hash_table, key,NULL ,NULL ); +}#endif #ifndef g_assert_trueqga/commands-posix.c +52
-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" @@ -2517,3 +2518,54 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup);#endif } + +#define QGA_MICRO_SECOND_TO_SECOND 1000000 + +static double ga_get_login_time (struct utmpx *user_info) +{ +double seconds = (double )user_info->ut_tv.tv_sec; +double useconds = (double )user_info->ut_tv.tv_usec; + useconds /=QGA_MICRO_SECOND_TO_SECOND ; +return seconds + useconds; +} + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +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)) { +gpointer value =g_hash_table_lookup (cache, user_info->ut_user); +GuestUser *user = (GuestUser *)value; +double login_time =ga_get_login_time (user_info); +/* We're ensuring the earliest login time to be sent */ +if (login_time < user->login_time) { + user->login_time = login_time; + } +continue ; + } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user =g_strdup (user_info->ut_user); + item->value->login_time =ga_get_login_time (user_info); + +g_hash_table_insert (cache, item->value->user, item->value); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +endutxent (); +g_hash_table_destroy (cache); +return head; +}qga/commands-win32.c +22
-0@@ -11,6 +11,9 @@* See the COPYING file in the top-level directory. */ +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0600 +#endif #include "qemu/osdep.h" #include <wtypes.h> #include <powrprof.h> @@ -25,6 +28,7 @@#include <initguid.h> #endif #include <lm.h> +#include <wtsapi32.h> #include "qga/guest-agent-core.h" #include "qga/vss-win32.h" @@ -1536,3 +1540,88 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup); } } + +/* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ +typedef struct _GA_WTSINFOA { + WTS_CONNECTSTATE_CLASS State; + DWORD SessionId; + DWORD IncomingBytes; + DWORD OutgoingBytes; + DWORD IncomingFrames; + DWORD OutgoingFrames; + DWORD IncomingCompressedBytes; + DWORD OutgoingCompressedBy; + CHAR WinStationName[WINSTATIONNAME_LENGTH]; + CHAR Domain[DOMAIN_LENGTH]; + CHAR UserName[USERNAME_LENGTH + 1]; + LARGE_INTEGER ConnectTime; + LARGE_INTEGER DisconnectTime; + LARGE_INTEGER LastInputTime; + LARGE_INTEGER LogonTime;
qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1570
Quoted 10 lines
qga/commands-win32.c +10
-0+LARGE_INTEGER CurrentTime; + +}GA_WTSINFOA ; + +GuestUserList *qmp_guest_get_users (Error **err) +{ +#if (_WIN32_WINNT >=0x0600 ) +#ifndef QGA_NANOSECONDS +# define QGA_NANOSECONDS 10000000 +#endif
In my previous comment I was referring to all instances of this #ifndef -> #define pattern. I think they are extraneous in this case as well.
qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1572
Quoted 2 lines
qga/commands-win32.c +2
-0+#ifndef QGA_EPOCH_TIME_DIFF +# define QGA_EPOCH_TIME_DIFF 116444736000000000LL
We actually have a W32_FT_OFFSET currently used in guest_get_time which you can probably use here as well.
qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1581
Quoted 9 lines
qga/commands-win32.c +9
-0+#endif + +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +GuestUserList *head =NULL , *cur_item =NULL ; + +DWORD count =0 ; +WTS_SESSION_INFOA *entries =NULL ; +if (WTSEnumerateSessionsA (NULL ,0 ,1 , &entries, &count)) { +for (DWORD i =0 ; i < count; ++i) {
The convention in QEMU seems to be to place all declarations at the start of the block. There's a note about this in CODING_STYLE under "5. Declarations".
qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1594
Quoted 13 lines
qga/commands-win32.c +13
-0+DWORD buffer_size =0 ; +GA_WTSINFOA *session_info =NULL ; +if (WTSQuerySessionInformationA ( +NULL , + entries[i].SessionId, + WTSSessionInfo, + (LPSTR *)&session_info, + &buffer_size + )) { +if (strlen (session_info->UserName) ==0 || +g_hash_table_contains (cache, session_info->UserName)) { +WTSFreeMemory (session_info); +continue ;
If it's already in the hash table, don't we want to update that entry with the earliest login time here, like we did with the posix version?
Quoted 68 lines
qga/commands-win32.c +33
-0+ } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + + item->value->user =g_strdup (session_info->UserName); + item->value->domain =g_strdup (session_info->Domain); + item->value->has_domain = true; + +INT64 login = session_info->LogonTime.QuadPart; + login -=QGA_EPOCH_TIME_DIFF ; + item->value->login_time = ((double )login) /QGA_NANOSECONDS ; + +g_hash_table_add (cache, item->value->user); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +WTSFreeMemory (session_info); + } +WTSFreeMemory (entries); + } +g_hash_table_destroy (cache); +return head; +#else +error_setg (err,QERR_UNSUPPORTED ); +return NULL ; +#endif +}qga/qapi-schema.json +24
-0@@ -1042,3 +1042,27 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# @login-time: Time of login of this user on the computer. If multiple +# instances of the user are logged in, the earliest login time is +# reported. The value is in fractional seconds since epoch time. +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', 'login-time': 'number', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active users on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] }Signature · 1 line
2.9.3
Quoted 182 lines · Michael Roth
Quoting Vinzenz 'evilissimo' Feenstra (2017-04-13 05:41:17)
Quoted 163 lines
From: Vinzenz Feenstra <[email protected]>
A command that will list all currently logged in users, and the time since when they are logged in.
Examples:
virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490622289.903835,"user":"root"}]}
virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"login-time":1490351044.670552,"domain":"LADIDA", "user":"Administrator"}]}
Signed-off-by Vinzenz Feenstra <[email protected]>---
configure | 2 +- include/glib-compat.h | 5 +++ qga/commands-posix.c | 52 ++++++++++++++++++++++++++++++ qga/commands-win32.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 24 ++++++++++++++ 5 files changed, 171 insertions(+), 1 deletion(-)configure +1
-1@@ -742,7 +742,7 @@ if test "$mingw32" = "yes" ; then sysconfdir="\${prefix}" local_statedir= confsuffix="" - libs_qga="-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof -lwtsapi32 -liphlpapi -lnetapi32 $libs_qga"fi
werror=""
include/glib-compat.h +5
-0@@ -217,6 +217,11 @@ static inline void g_hash_table_add(GHashTable *hash_table, gpointer key) {g_hash_table_replace (hash_table, key, key); } + +static gboolean g_hash_table_contains (GHashTable *hash_table,gpointer key) +{ +return g_hash_table_lookup_extended (hash_table, key,NULL ,NULL ); +}#endif #ifndef g_assert_trueqga/commands-posix.c +52
-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" @@ -2517,3 +2518,54 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup);#endif } + +#define QGA_MICRO_SECOND_TO_SECOND 1000000 + +static double ga_get_login_time (struct utmpx *user_info) +{ +double seconds = (double )user_info->ut_tv.tv_sec; +double useconds = (double )user_info->ut_tv.tv_usec; + useconds /=QGA_MICRO_SECOND_TO_SECOND ; +return seconds + useconds; +} + +GuestUserList *qmp_guest_get_users (Error **err) +{ +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +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)) { +gpointer value =g_hash_table_lookup (cache, user_info->ut_user); +GuestUser *user = (GuestUser *)value; +double login_time =ga_get_login_time (user_info); +/* We're ensuring the earliest login time to be sent */ +if (login_time < user->login_time) { + user->login_time = login_time; + } +continue ; + } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + item->value->user =g_strdup (user_info->ut_user); + item->value->login_time =ga_get_login_time (user_info); + +g_hash_table_insert (cache, item->value->user, item->value); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +endutxent (); +g_hash_table_destroy (cache); +return head; +}qga/commands-win32.c +22
-0@@ -11,6 +11,9 @@* See the COPYING file in the top-level directory. */ +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0600 +#endif #include "qemu/osdep.h" #include <wtypes.h> #include <powrprof.h> @@ -25,6 +28,7 @@#include <initguid.h> #endif #include <lm.h> +#include <wtsapi32.h> #include "qga/guest-agent-core.h" #include "qga/vss-win32.h" @@ -1536,3 +1540,88 @@ void ga_command_state_init(GAState *s, GACommandState *cs)ga_command_state_add (cs,NULL , guest_fsfreeze_cleanup); } } + +/* MINGW is missing two fields: IncomingFrames & OutgoingFrames */ +typedef struct _GA_WTSINFOA { + WTS_CONNECTSTATE_CLASS State; + DWORD SessionId; + DWORD IncomingBytes; + DWORD OutgoingBytes; + DWORD IncomingFrames; + DWORD OutgoingFrames; + DWORD IncomingCompressedBytes; + DWORD OutgoingCompressedBy; + CHAR WinStationName[WINSTATIONNAME_LENGTH]; + CHAR Domain[DOMAIN_LENGTH]; + CHAR UserName[USERNAME_LENGTH + 1]; + LARGE_INTEGER ConnectTime; + LARGE_INTEGER DisconnectTime; + LARGE_INTEGER LastInputTime; + LARGE_INTEGER LogonTime;qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1570
Quoted 10 lines
qga/commands-win32.c +10
-0+LARGE_INTEGER CurrentTime; + +}GA_WTSINFOA ; + +GuestUserList *qmp_guest_get_users (Error **err) +{ +#if (_WIN32_WINNT >=0x0600 ) +#ifndef QGA_NANOSECONDS +# define QGA_NANOSECONDS 10000000 +#endif In my previous comment I was referring to all instances of this #ifndef -> #define pattern. I think they are extraneous in this case as well.
Quoted 5 lines
qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1572
Quoted 2 lines
qga/commands-win32.c +2
-0+#ifndef QGA_EPOCH_TIME_DIFF +# define QGA_EPOCH_TIME_DIFF 116444736000000000LLWe actually have a
W32_FT_OFFSETcurrently used inguest_get_timewhich you can probably use here as well.
I missed that in the comment, fixed in v5.
Quoted 14 lines
qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1581
Quoted 10 lines
qga/commands-win32.c +9
-0+#endif + +GHashTable *cache =g_hash_table_new (g_str_hash, g_str_equal); +GuestUserList *head =NULL , *cur_item =NULL ; + +DWORD count =0 ; +WTS_SESSION_INFOA *entries =NULL ; +if (WTSEnumerateSessionsA (NULL ,0 ,1 , &entries, &count)) { +for (DWORD i =0 ; i < count; ++i) {The convention in QEMU seems to be to place all declarations at the start of the block. There's a note about this in
CODING_STYLEunder "5. Declarations”.
Changed the patch to comply with that in v5.
Quoted 15 lines
Quoted 4 lines
qga/commands-win32.c +3
-0+DWORD buffer_size =0 ; +GA_WTSINFOA *session_info =NULL ; + if (WTSQuerySessionInformationA(qga/commands-win32.c void ga_command_state_init(GAState *s, GACommandState *cs) · line 1594
Quoted 10 lines
qga/commands-win32.c +10
-0+ NULL, + entries[i].SessionId, + WTSSessionInfo, + (LPSTR *)&session_info, + &buffer_size + )) { +if (strlen (session_info->UserName) ==0 || +g_hash_table_contains (cache, session_info->UserName)) { +WTSFreeMemory (session_info); +continue ;
Quoted 2 lines
If it's already in the hash table, don't we want to update that entry with the earliest login time here, like we did with the posix version?
Right, at first I thought it wouldn’t be necessary but then I realized it might actually be so I have added it in v5.
Quoted 60 lines
Quoted 60 lines
qga/commands-win32.c +33
-0+ } + +GuestUserList *item =g_new0 (GuestUserList,1 ); + item->value =g_new0 (GuestUser,1 ); + + item->value->user =g_strdup (session_info->UserName); + item->value->domain =g_strdup (session_info->Domain); + item->value->has_domain = true; + +INT64 login = session_info->LogonTime.QuadPart; + login -=QGA_EPOCH_TIME_DIFF ; + item->value->login_time = ((double )login) /QGA_NANOSECONDS ; + +g_hash_table_add (cache, item->value->user); + +if (!cur_item) { + head = cur_item = item; + }else { + cur_item->next = item; + cur_item = item; + } + } +WTSFreeMemory (session_info); + } +WTSFreeMemory (entries); + } +g_hash_table_destroy (cache); +return head; +#else +error_setg (err,QERR_UNSUPPORTED ); +return NULL ; +#endif +}qga/qapi-schema.json +18
-0@@ -1042,3 +1042,27 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# @login-time: Time of login of this user on the computer. If multiple +# instances of the user are logged in, the earliest login time is +# reported. The value is in fractional seconds since epoch time. +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', 'login-time': 'number', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active users on the VM. +#
Quoted 8 lines
Quoted 8 lines
qga/qapi-schema.json +6
-0+# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] }Signature · 1 line
2.9.3
--
Vinzenz Feenstra
Senior Software Developer
Red Hat Czech