VirtualBox

source: vbox/trunk/src/VBox/Main/include/VirtualBoxSDSImpl.h

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: VirtualBoxSDSImpl.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBox Global COM Class definition
4 */
5
6/*
7 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef MAIN_INCLUDED_VirtualBoxSDSImpl_h
29#define MAIN_INCLUDED_VirtualBoxSDSImpl_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include "VirtualBoxBase.h"
35
36/* Enable the watcher code in debug builds. */
37#ifdef DEBUG
38# define WITH_WATCHER
39#endif
40
41
42class VBoxSDSPerUserData; /* See VirtualBoxSDSImpl.cpp. */
43struct VBoxSDSWatcher; /* See VirtualBoxSDSImpl.cpp. */
44
45/**
46 * The IVirtualBoxSDS implementation.
47 *
48 * This class helps different VBoxSVC processes make sure a user only have a
49 * single VirtualBox instance.
50 *
51 * @note This is a simple internal class living in a privileged process. So, we
52 * do not use the API wrappers as they add complexity. In particular,
53 * they add the auto caller logic, which is an excellent tool to create
54 * unkillable processes. If an API method during development or product
55 * for instance triggers an NT exception like STATUS_ACCESS_VIOLATION, the
56 * caller will be unwound without releasing the caller. When uninit is
57 * called during COM shutdown/whatever, the thread gets stuck waiting for
58 * the long gone caller and cannot be killed (Windows 10, build 16299),
59 * requiring a reboot to continue.
60 *
61 * @todo Would be very nice to get rid of the ATL cruft too here.
62 */
63class VirtualBoxSDS
64 : public IVirtualBoxSDS
65 , public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>
66 , public ATL::CComCoClass<VirtualBoxSDS, &CLSID_VirtualBoxSDS>
67{
68private:
69 typedef std::map<com::Utf8Str, VBoxSDSPerUserData *> UserDataMap_T;
70 /** Per user data map (key is SID string).
71 * This is an insert-only map! */
72 UserDataMap_T m_UserDataMap;
73 /** Number of registered+watched VBoxSVC processes. */
74 uint32_t m_cVBoxSvcProcesses;
75#ifdef WITH_WATCHER
76 /** Number of watcher threads. */
77 uint32_t m_cWatchers;
78 /** Pointer to an array of watcher pointers. */
79 VBoxSDSWatcher **m_papWatchers;
80 /** Lock protecting m_papWatchers and associated structures. */
81 RTCRITSECT m_WatcherCritSect;
82#endif
83 /** Lock protecting m_UserDataMap . */
84 RTCRITSECTRW m_MapCritSect;
85
86public:
87 DECLARE_CLASSFACTORY_SINGLETON(VirtualBoxSDS)
88 DECLARE_NOT_AGGREGATABLE(VirtualBoxSDS)
89 DECLARE_PROTECT_FINAL_CONSTRUCT()
90
91 BEGIN_COM_MAP(VirtualBoxSDS)
92 COM_INTERFACE_ENTRY(IVirtualBoxSDS)
93 END_COM_MAP()
94
95 DECLARE_COMMON_CLASS_METHODS(VirtualBoxSDS)
96
97 HRESULT FinalConstruct();
98 void FinalRelease();
99
100private:
101
102 /** @name IVirtualBoxSDS methods
103 * @{ */
104 STDMETHOD(RegisterVBoxSVC)(IVBoxSVCRegistration *aVBoxSVC, LONG aPid, IUnknown **aExistingVirtualBox);
105 STDMETHOD(DeregisterVBoxSVC)(IVBoxSVCRegistration *aVBoxSVC, LONG aPid);
106 STDMETHOD(LaunchVMProcess)(IN_BSTR aMachine, IN_BSTR aComment, IN_BSTR aFrontend,
107 ComSafeArrayIn(IN_BSTR, aEnvironmentChanges), IN_BSTR aCmdOptions,
108 ULONG aSessionId, ULONG *aPid);
109 /** @} */
110
111
112 /** @name Private methods
113 * @{ */
114 /**
115 * Gets the client user SID of the
116 */
117 static bool i_getClientUserSid(com::Utf8Str *a_pStrSid, com::Utf8Str *a_pStrUsername);
118
119 /**
120 * Returns whether a VBoxSDS feature is enabled or not.
121 *
122 * @returns \c true if enabled, \c false if not.
123 * @param a_pwszFeature Feature to check enabled status for.
124 */
125 static bool i_isFeatureEnabled(wchar_t const *a_pwszFeature);
126
127 /**
128 * Looks up the given user.
129 *
130 * @returns Pointer to the LOCKED per user data. NULL if not found.
131 * @param a_rStrUserSid The user SID.
132 */
133 VBoxSDSPerUserData *i_lookupPerUserData(com::Utf8Str const &a_rStrUserSid);
134
135 /**
136 * Looks up the given user, creating it if not found
137 *
138 * @returns Pointer to the LOCKED per user data. NULL on allocation error.
139 * @param a_rStrUserSid The user SID.
140 * @param a_rStrUsername The user name if available.
141 */
142 VBoxSDSPerUserData *i_lookupOrCreatePerUserData(com::Utf8Str const &a_rStrUserSid, com::Utf8Str const &a_rStrUsername);
143
144#ifdef WITH_WATCHER
145 static DECLCALLBACK(int) i_watcherThreadProc(RTTHREAD hSelf, void *pvUser);
146 bool i_watchIt(VBoxSDSPerUserData *pProcess, HANDLE hProcess, RTPROCESS pid);
147 void i_stopWatching(VBoxSDSPerUserData *pProcess, RTPROCESS pid);
148 void i_shutdownAllWatchers(void);
149
150 void i_decrementClientCount();
151 void i_incrementClientCount();
152#endif
153 /** @} */
154};
155
156#ifdef WITH_WATCHER
157void VBoxSDSNotifyClientCount(uint32_t cClients);
158#endif
159
160#endif /* !MAIN_INCLUDED_VirtualBoxSDSImpl_h */
161/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use