1 | /* $Id: ClientWatcher.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox API client session watcher
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-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_ClientWatcher_h
|
---|
29 | #define MAIN_INCLUDED_ClientWatcher_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 |
|
---|
35 | #include <list>
|
---|
36 | #include <VBox/com/ptr.h>
|
---|
37 | #include <VBox/com/AutoLock.h>
|
---|
38 |
|
---|
39 | #include "VirtualBoxImpl.h"
|
---|
40 |
|
---|
41 | #if defined(RT_OS_WINDOWS)
|
---|
42 | # define CWUPDATEREQARG NULL
|
---|
43 | # define CWUPDATEREQTYPE HANDLE
|
---|
44 | # define CW_MAX_CLIENTS _16K /**< Max number of clients we can watch (windows). */
|
---|
45 | # ifndef DEBUG /* The debug version triggers worker thread code much much earlier. */
|
---|
46 | # define CW_MAX_CLIENTS_PER_THREAD 63 /**< Max clients per watcher thread (windows). */
|
---|
47 | # else
|
---|
48 | # define CW_MAX_CLIENTS_PER_THREAD 3 /**< Max clients per watcher thread (windows). */
|
---|
49 | # endif
|
---|
50 | # define CW_MAX_HANDLES_PER_THREAD (CW_MAX_CLIENTS_PER_THREAD + 1) /**< Max handles per thread. */
|
---|
51 |
|
---|
52 | #elif defined(RT_OS_OS2)
|
---|
53 | # define CWUPDATEREQARG NIL_RTSEMEVENT
|
---|
54 | # define CWUPDATEREQTYPE RTSEMEVENT
|
---|
55 |
|
---|
56 | #elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER) || defined(VBOX_WITH_GENERIC_SESSION_WATCHER)
|
---|
57 | # define CWUPDATEREQARG NIL_RTSEMEVENT
|
---|
58 | # define CWUPDATEREQTYPE RTSEMEVENT
|
---|
59 |
|
---|
60 | #else
|
---|
61 | # error "Port me!"
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Class which checks for API clients which have crashed/exited, and takes
|
---|
66 | * the necessary cleanup actions. Singleton.
|
---|
67 | */
|
---|
68 | class VirtualBox::ClientWatcher
|
---|
69 | {
|
---|
70 | public:
|
---|
71 | /**
|
---|
72 | * Constructor which creates a usable instance
|
---|
73 | *
|
---|
74 | * @param pVirtualBox Reference to VirtualBox object
|
---|
75 | */
|
---|
76 | ClientWatcher(const ComObjPtr<VirtualBox> &pVirtualBox);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Default destructor. Cleans everything up.
|
---|
80 | */
|
---|
81 | ~ClientWatcher();
|
---|
82 |
|
---|
83 | bool isReady();
|
---|
84 |
|
---|
85 | void update();
|
---|
86 | void addProcess(RTPROCESS pid);
|
---|
87 |
|
---|
88 | private:
|
---|
89 | /**
|
---|
90 | * Default constructor. Don't use, will not create a sensible instance.
|
---|
91 | */
|
---|
92 | ClientWatcher();
|
---|
93 |
|
---|
94 | static DECLCALLBACK(int) worker(RTTHREAD hThreadSelf, void *pvUser);
|
---|
95 | uint32_t reapProcesses(void);
|
---|
96 |
|
---|
97 | VirtualBox *mVirtualBox;
|
---|
98 | RTTHREAD mThread;
|
---|
99 | CWUPDATEREQTYPE mUpdateReq;
|
---|
100 | util::RWLockHandle mLock;
|
---|
101 |
|
---|
102 | typedef std::list<RTPROCESS> ProcessList;
|
---|
103 | ProcessList mProcesses;
|
---|
104 |
|
---|
105 | #if defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER) || defined(VBOX_WITH_GENERIC_SESSION_WATCHER)
|
---|
106 | uint8_t mUpdateAdaptCtr;
|
---|
107 | #endif
|
---|
108 | #ifdef RT_OS_WINDOWS
|
---|
109 | /** Indicate a real update request is pending.
|
---|
110 | * To avoid race conditions this must be set before mUpdateReq is signalled and
|
---|
111 | * read after resetting mUpdateReq. */
|
---|
112 | volatile bool mfUpdateReq;
|
---|
113 | /** Set when the worker threads are supposed to shut down. */
|
---|
114 | volatile bool mfTerminate;
|
---|
115 | /** Number of active subworkers.
|
---|
116 | * When decremented to 0, subworker zero is signalled. */
|
---|
117 | uint32_t volatile mcActiveSubworkers;
|
---|
118 | /** Number of valid handles in mahWaitHandles. */
|
---|
119 | uint32_t mcWaitHandles;
|
---|
120 | /** The wait interval (usually INFINITE). */
|
---|
121 | uint32_t mcMsWait;
|
---|
122 | /** Per subworker data. Subworker 0 is the main worker and does not have a
|
---|
123 | * pReq pointer since. */
|
---|
124 | struct PerSubworker
|
---|
125 | {
|
---|
126 | /** The wait result. */
|
---|
127 | DWORD dwWait;
|
---|
128 | /** The subworker index. */
|
---|
129 | uint32_t iSubworker;
|
---|
130 | /** The subworker thread handle. */
|
---|
131 | RTTHREAD hThread;
|
---|
132 | /** Self pointer (for worker thread). */
|
---|
133 | VirtualBox::ClientWatcher *pSelf;
|
---|
134 | } maSubworkers[(CW_MAX_CLIENTS + CW_MAX_CLIENTS_PER_THREAD - 1) / CW_MAX_CLIENTS_PER_THREAD];
|
---|
135 | /** Wait handle array. The mUpdateReq manual reset event handle is inserted
|
---|
136 | * every 64 entries, first entry being 0. */
|
---|
137 | HANDLE mahWaitHandles[CW_MAX_CLIENTS + (CW_MAX_CLIENTS + CW_MAX_CLIENTS_PER_THREAD - 1) / CW_MAX_CLIENTS_PER_THREAD];
|
---|
138 |
|
---|
139 | void subworkerWait(VirtualBox::ClientWatcher::PerSubworker *pSubworker, uint32_t cMsWait);
|
---|
140 | static DECLCALLBACK(int) subworkerThread(RTTHREAD hThreadSelf, void *pvUser);
|
---|
141 | void winResetHandleArray(uint32_t cProcHandles);
|
---|
142 | #endif
|
---|
143 | };
|
---|
144 |
|
---|
145 | #endif /* !MAIN_INCLUDED_ClientWatcher_h */
|
---|
146 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|