From nobody Sat Apr 27 17:26:06 2024 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 1492080155331190.3484918239418; Thu, 13 Apr 2017 03:42:35 -0700 (PDT) Received: from localhost ([::1]:48473 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cycCv-0001OS-CF for importer@patchew.org; Thu, 13 Apr 2017 06:42:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53601) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cycBq-0000p3-Gb for qemu-devel@nongnu.org; Thu, 13 Apr 2017 06:41:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cycBo-00019D-VC for qemu-devel@nongnu.org; Thu, 13 Apr 2017 06:41:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50592) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cycBo-00018r-Ld for qemu-devel@nongnu.org; Thu, 13 Apr 2017 06:41:24 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 77CA680F75; Thu, 13 Apr 2017 10:41:23 +0000 (UTC) Received: from evilvm.brq.redhat.com (ovpn-117-228.ams2.redhat.com [10.36.117.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A08B17132; Thu, 13 Apr 2017 10:41:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 77CA680F75 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=vfeenstr@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 77CA680F75 From: Vinzenz 'evilissimo' Feenstra To: qemu-devel@nongnu.org Date: Thu, 13 Apr 2017 12:41:17 +0200 Message-Id: <20170413104117.4948-2-vfeenstr@redhat.com> In-Reply-To: <20170413104117.4948-1-vfeenstr@redhat.com> References: <20170413104117.4948-1-vfeenstr@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 13 Apr 2017 10:41:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 1/1] 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: marcandre.lureau@gmail.com, mdroth@linux.vnet.ibm.com, 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 --- 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(-) diff --git a/configure b/configure index be4d326..55e6654 100755 --- a/configure +++ b/configure @@ -742,7 +742,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..f8ee9dc 100644 --- a/include/glib-compat.h +++ b/include/glib-compat.h @@ -217,6 +217,11 @@ static inline void g_hash_table_add(GHashTable *hash_t= able, 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 =20 #ifndef g_assert_true diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 915df9e..4a59988 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,54 @@ 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 g_hash_table_new(g_str_hash, g_str_equal); + GuestUserList *head =3D NULL, *cur_item =3D NULL; + setutxent(); + for (;;) { + struct utmpx *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)) { + gpointer value =3D g_hash_table_lookup(cache, user_info->ut_us= er); + GuestUser *user =3D (GuestUser *)value; + double 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; + } + + GuestUserList *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 19d72b2..8b84a90 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,88 @@ void ga_command_state_init(GAState *s, GACommandStat= e *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) +#ifndef QGA_NANOSECONDS +# define QGA_NANOSECONDS 10000000 +#endif +#ifndef QGA_EPOCH_TIME_DIFF +# define QGA_EPOCH_TIME_DIFF 116444736000000000LL +#endif + + GHashTable *cache =3D g_hash_table_new(g_str_hash, g_str_equal); + GuestUserList *head =3D NULL, *cur_item =3D NULL; + + DWORD count =3D 0; + WTS_SESSION_INFOA *entries =3D NULL; + if (WTSEnumerateSessionsA(NULL, 0, 1, &entries, &count)) { + for (DWORD i =3D 0; i < count; ++i) { + DWORD buffer_size =3D 0; + GA_WTSINFOA *session_info =3D NULL; + if (WTSQuerySessionInformationA( + NULL, + entries[i].SessionId, + WTSSessionInfo, + (LPSTR *)&session_info, + &buffer_size + )) { + if (strlen(session_info->UserName) =3D=3D 0 || + g_hash_table_contains(cache, session_info->UserName)) { + WTSFreeMemory(session_info); + continue; + } + + GuestUserList *item =3D g_new0(GuestUserList, 1); + item->value =3D g_new0(GuestUser, 1); + + item->value->user =3D g_strdup(session_info->UserName); + item->value->domain =3D g_strdup(session_info->Domain); + item->value->has_domain =3D true; + + INT64 login =3D session_info->LogonTime.QuadPart; + login -=3D QGA_EPOCH_TIME_DIFF; + item->value->login_time =3D ((double)login) / QGA_NANOSECO= NDS; + + 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(session_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 a02dbf2..27d10be 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -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 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.9.3