From nobody Sun Feb 8 23:05:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1493272169815605.8643307108673; Wed, 26 Apr 2017 22:49:29 -0700 (PDT) Received: from localhost ([::1]:58853 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3cIy-0002bv-Dr for importer@patchew.org; Thu, 27 Apr 2017 01:49:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60647) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3cHB-0001HD-7S for qemu-devel@nongnu.org; Thu, 27 Apr 2017 01:47:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d3cH7-0003mh-N9 for qemu-devel@nongnu.org; Thu, 27 Apr 2017 01:47:37 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:51226 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d3cH7-0003mD-GK for qemu-devel@nongnu.org; Thu, 27 Apr 2017 01:47:33 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v3R5hhbf187328 for ; Thu, 27 Apr 2017 01:47:32 -0400 Received: from e35.co.us.ibm.com (e35.co.us.ibm.com [32.97.110.153]) by mx0b-001b2d01.pphosted.com with ESMTP id 2a333hetvb-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 27 Apr 2017 01:47:32 -0400 Received: from localhost by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 26 Apr 2017 23:47:31 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (9.17.130.18) by e35.co.us.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 26 Apr 2017 23:47:30 -0600 Received: from b03ledav003.gho.boulder.ibm.com (b03ledav003.gho.boulder.ibm.com [9.17.130.234]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v3R5lTNS10813696; Wed, 26 Apr 2017 22:47:29 -0700 Received: from b03ledav003.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C8D126A03B; Wed, 26 Apr 2017 23:47:29 -0600 (MDT) Received: from localhost (unknown [9.80.102.60]) by b03ledav003.gho.boulder.ibm.com (Postfix) with ESMTP id 6145D6A03F; Wed, 26 Apr 2017 23:47:29 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Thu, 27 Apr 2017 00:46:35 -0500 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1493271996-23607-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1493271996-23607-1-git-send-email-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17042705-0012-0000-0000-00001420A44B X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006981; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000208; SDB=6.00853062; UDB=6.00421735; IPR=6.00631898; BA=6.00005313; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00015201; XFM=3.00000013; UTC=2017-04-27 05:47:31 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17042705-0013-0000-0000-00004D54B156 Message-Id: <1493271996-23607-8-git-send-email-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-04-27_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1704270094 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PULL v2 7/8] qga: Add 'guest-get-users' command X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Vinzenz Feenstra Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Vinzenz Feenstra 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 * make g_hash_table_contains compat func inline to avoid unused warnings Signed-off-by: Michael Roth --- configure | 2 +- include/glib-compat.h | 6 +++ qga/commands-posix.c | 60 +++++++++++++++++++++++++++++ qga/commands-win32.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ qga/qapi-schema.json | 25 ++++++++++++ 5 files changed, 195 insertions(+), 1 deletion(-) diff --git a/configure b/configure index c35acf1..80d17f3 100755 --- a/configure +++ b/configure @@ -743,7 +743,7 @@ if test "$mingw32" =3D "yes" ; then sysconfdir=3D"\${prefix}" local_statedir=3D confsuffix=3D"" - libs_qga=3D"-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" + libs_qga=3D"-lws2_32 -lwinmm -lpowrprof -lwtsapi32 -liphlpapi -lnetapi32= $libs_qga" fi =20 werror=3D"" diff --git a/include/glib-compat.h b/include/glib-compat.h index 863c8cf..fcffcd3 100644 --- a/include/glib-compat.h +++ b/include/glib-compat.h @@ -217,6 +217,12 @@ static inline void g_hash_table_add(GHashTable *hash_t= able, gpointer key) { g_hash_table_replace(hash_table, key, key); } + +static inline gboolean g_hash_table_contains(GHashTable *hash_table, + gpointer key) +{ + return g_hash_table_lookup_extended(hash_table, key, NULL, NULL); +} #endif =20 #ifndef g_assert_true diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 915df9e..ba06be4 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "qga/guest-agent-core.h" #include "qga-qmp-commands.h" #include "qapi/qmp/qerror.h" @@ -2517,3 +2518,62 @@ void ga_command_state_init(GAState *s, GACommandStat= e *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 =3D (double)user_info->ut_tv.tv_sec; + double useconds =3D (double)user_info->ut_tv.tv_usec; + useconds /=3D QGA_MICRO_SECOND_TO_SECOND; + return seconds + useconds; +} + +GuestUserList *qmp_guest_get_users(Error **err) +{ + GHashTable *cache =3D NULL; + GuestUserList *head =3D NULL, *cur_item =3D NULL; + struct utmpx *user_info =3D NULL; + gpointer value =3D NULL; + GuestUser *user =3D NULL; + GuestUserList *item =3D NULL; + double login_time =3D 0; + + cache =3D g_hash_table_new(g_str_hash, g_str_equal); + setutxent(); + + for (;;) { + user_info =3D getutxent(); + if (user_info =3D=3D NULL) { + break; + } else if (user_info->ut_type !=3D USER_PROCESS) { + continue; + } else if (g_hash_table_contains(cache, user_info->ut_user)) { + value =3D g_hash_table_lookup(cache, user_info->ut_user); + user =3D (GuestUser *)value; + login_time =3D 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 =3D login_time; + } + continue; + } + + item =3D g_new0(GuestUserList, 1); + item->value =3D g_new0(GuestUser, 1); + item->value->user =3D g_strdup(user_info->ut_user); + item->value->login_time =3D ga_get_login_time(user_info); + + g_hash_table_insert(cache, item->value->user, item->value); + + if (!cur_item) { + head =3D cur_item =3D item; + } else { + cur_item->next =3D item; + cur_item =3D item; + } + } + endutxent(); + g_hash_table_destroy(cache); + return head; +} diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 9fec1fb..439d229 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -11,6 +11,9 @@ * See the COPYING file in the top-level directory. */ =20 +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0600 +#endif #include "qemu/osdep.h" #include #include @@ -25,6 +28,7 @@ #include #endif #include +#include =20 #include "qga/guest-agent-core.h" #include "qga/vss-win32.h" @@ -1536,3 +1540,102 @@ void ga_command_state_init(GAState *s, GACommandSta= te *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 >=3D 0x0600) +#define QGA_NANOSECONDS 10000000 + + GHashTable *cache =3D NULL; + GuestUserList *head =3D NULL, *cur_item =3D NULL; + + DWORD buffer_size =3D 0, count =3D 0, i =3D 0; + GA_WTSINFOA *info =3D NULL; + WTS_SESSION_INFOA *entries =3D NULL; + GuestUserList *item =3D NULL; + GuestUser *user =3D NULL; + gpointer value =3D NULL; + INT64 login =3D 0; + double login_time =3D 0; + + cache =3D g_hash_table_new(g_str_hash, g_str_equal); + + if (WTSEnumerateSessionsA(NULL, 0, 1, &entries, &count)) { + for (i =3D 0; i < count; ++i) { + buffer_size =3D 0; + info =3D NULL; + if (WTSQuerySessionInformationA( + NULL, + entries[i].SessionId, + WTSSessionInfo, + (LPSTR *)&info, + &buffer_size + )) { + + if (strlen(info->UserName) =3D=3D 0) { + WTSFreeMemory(info); + continue; + } + + login =3D info->LogonTime.QuadPart; + login -=3D W32_FT_OFFSET; + login_time =3D ((double)login) / QGA_NANOSECONDS; + + if (g_hash_table_contains(cache, info->UserName)) { + value =3D g_hash_table_lookup(cache, info->UserName); + user =3D (GuestUser *)value; + if (user->login_time > login_time) { + user->login_time =3D login_time; + } + } else { + item =3D g_new0(GuestUserList, 1); + item->value =3D g_new0(GuestUser, 1); + + item->value->user =3D g_strdup(info->UserName); + item->value->domain =3D g_strdup(info->Domain); + item->value->has_domain =3D true; + + item->value->login_time =3D login_time; + + g_hash_table_add(cache, item->value->user); + + if (!cur_item) { + head =3D cur_item =3D item; + } else { + cur_item->next =3D item; + cur_item =3D item; + } + } + } + WTSFreeMemory(info); + } + WTSFreeMemory(entries); + } + g_hash_table_destroy(cache); + return head; +#else + error_setg(err, QERR_UNSUPPORTED); + return NULL; +#endif +} diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index 0f4cba5..f25467a 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1076,3 +1076,28 @@ ## { 'command': 'guest-get-host-name', 'returns': 'GuestHostName' } + + +## +# @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 tim= e is +# reported. The value is in fractional seconds since epoch ti= me. +# +# 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'] } --=20 2.7.4