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