1 | /* $Id: VirtualBoxSDSImpl.h 76083 2018-12-09 19:26:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Global COM Class definition
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ____H_VIRTUALBOXSDSIMPL
|
---|
19 | #define ____H_VIRTUALBOXSDSIMPL
|
---|
20 |
|
---|
21 | #include "VirtualBoxBase.h"
|
---|
22 |
|
---|
23 | /* Enable the watcher code in debug builds. */
|
---|
24 | #ifdef DEBUG
|
---|
25 | # define WITH_WATCHER
|
---|
26 | #endif
|
---|
27 |
|
---|
28 |
|
---|
29 | class VBoxSDSPerUserData; /* See VirtualBoxSDSImpl.cpp. */
|
---|
30 | struct VBoxSDSWatcher; /* See VirtualBoxSDSImpl.cpp. */
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * The IVirtualBoxSDS implementation.
|
---|
34 | *
|
---|
35 | * This class helps different VBoxSVC processes make sure a user only have a
|
---|
36 | * single VirtualBox instance.
|
---|
37 | *
|
---|
38 | * @note This is a simple internal class living in a privileged process. So, we
|
---|
39 | * do not use the API wrappers as they add complexity. In particular,
|
---|
40 | * they add the auto caller logic, which is an excellent tool to create
|
---|
41 | * unkillable processes. If an API method during development or product
|
---|
42 | * for instance triggers an NT exception like STATUS_ACCESS_VIOLATION, the
|
---|
43 | * caller will be unwound without releasing the caller. When uninit is
|
---|
44 | * called during COM shutdown/whatever, the thread gets stuck waiting for
|
---|
45 | * the long gone caller and cannot be killed (Windows 10, build 16299),
|
---|
46 | * requiring a reboot to continue.
|
---|
47 | *
|
---|
48 | * @todo Would be very nice to get rid of the ATL cruft too here.
|
---|
49 | */
|
---|
50 | class VirtualBoxSDS
|
---|
51 | : public IVirtualBoxSDS
|
---|
52 | , public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>
|
---|
53 | , public ATL::CComCoClass<VirtualBoxSDS, &CLSID_VirtualBoxSDS>
|
---|
54 | {
|
---|
55 | private:
|
---|
56 | typedef std::map<com::Utf8Str, VBoxSDSPerUserData *> UserDataMap_T;
|
---|
57 | /** Per user data map (key is SID string).
|
---|
58 | * This is an insert-only map! */
|
---|
59 | UserDataMap_T m_UserDataMap;
|
---|
60 | /** Number of registered+watched VBoxSVC processes. */
|
---|
61 | uint32_t m_cVBoxSvcProcesses;
|
---|
62 | #ifdef WITH_WATCHER
|
---|
63 | /** Number of watcher threads. */
|
---|
64 | uint32_t m_cWatchers;
|
---|
65 | /** Pointer to an array of watcher pointers. */
|
---|
66 | VBoxSDSWatcher **m_papWatchers;
|
---|
67 | /** Lock protecting m_papWatchers and associated structures. */
|
---|
68 | RTCRITSECT m_WatcherCritSect;
|
---|
69 | #endif
|
---|
70 | /** Lock protecting m_UserDataMap . */
|
---|
71 | RTCRITSECTRW m_MapCritSect;
|
---|
72 |
|
---|
73 | public:
|
---|
74 | DECLARE_CLASSFACTORY_SINGLETON(VirtualBoxSDS)
|
---|
75 | DECLARE_NOT_AGGREGATABLE(VirtualBoxSDS)
|
---|
76 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
77 |
|
---|
78 | BEGIN_COM_MAP(VirtualBoxSDS)
|
---|
79 | COM_INTERFACE_ENTRY(IVirtualBoxSDS)
|
---|
80 | END_COM_MAP()
|
---|
81 |
|
---|
82 | DECLARE_EMPTY_CTOR_DTOR(VirtualBoxSDS)
|
---|
83 |
|
---|
84 | HRESULT FinalConstruct();
|
---|
85 | void FinalRelease();
|
---|
86 |
|
---|
87 | private:
|
---|
88 |
|
---|
89 | /** @name IVirtualBoxSDS methods
|
---|
90 | * @{ */
|
---|
91 | STDMETHOD(RegisterVBoxSVC)(IVBoxSVCRegistration *aVBoxSVC, LONG aPid, IUnknown **aExistingVirtualBox);
|
---|
92 | STDMETHOD(DeregisterVBoxSVC)(IVBoxSVCRegistration *aVBoxSVC, LONG aPid);
|
---|
93 | /** @} */
|
---|
94 |
|
---|
95 |
|
---|
96 | /** @name Private methods
|
---|
97 | * @{ */
|
---|
98 | /**
|
---|
99 | * Gets the client user SID of the
|
---|
100 | */
|
---|
101 | static bool i_getClientUserSid(com::Utf8Str *a_pStrSid, com::Utf8Str *a_pStrUsername);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Looks up the given user.
|
---|
105 | *
|
---|
106 | * @returns Pointer to the LOCKED per user data. NULL if not found.
|
---|
107 | * @param a_rStrUserSid The user SID.
|
---|
108 | */
|
---|
109 | VBoxSDSPerUserData *i_lookupPerUserData(com::Utf8Str const &a_rStrUserSid);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Looks up the given user, creating it if not found
|
---|
113 | *
|
---|
114 | * @returns Pointer to the LOCKED per user data. NULL on allocation error.
|
---|
115 | * @param a_rStrUserSid The user SID.
|
---|
116 | * @param a_rStrUsername The user name if available.
|
---|
117 | */
|
---|
118 | VBoxSDSPerUserData *i_lookupOrCreatePerUserData(com::Utf8Str const &a_rStrUserSid, com::Utf8Str const &a_rStrUsername);
|
---|
119 |
|
---|
120 | #ifdef WITH_WATCHER
|
---|
121 | static DECLCALLBACK(int) i_watcherThreadProc(RTTHREAD hSelf, void *pvUser);
|
---|
122 | bool i_watchIt(VBoxSDSPerUserData *pProcess, HANDLE hProcess, RTPROCESS pid);
|
---|
123 | void i_stopWatching(VBoxSDSPerUserData *pProcess, RTPROCESS pid);
|
---|
124 | void i_shutdownAllWatchers(void);
|
---|
125 |
|
---|
126 | void i_decrementClientCount();
|
---|
127 | void i_incrementClientCount();
|
---|
128 | #endif
|
---|
129 | /** @} */
|
---|
130 | };
|
---|
131 |
|
---|
132 | #ifdef WITH_WATCHER
|
---|
133 | void VBoxSDSNotifyClientCount(uint32_t cClients);
|
---|
134 | #endif
|
---|
135 |
|
---|
136 | #endif // !____H_VIRTUALBOXSDSIMPL
|
---|
137 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|