VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.h@ 98412

Last change on this file since 98412 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/* $Id: VBoxServiceControl.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxServiceControl.h - Internal guest control definitions.
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 GA_INCLUDED_SRC_common_VBoxService_VBoxServiceControl_h
29#define GA_INCLUDED_SRC_common_VBoxService_VBoxServiceControl_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <iprt/critsect.h>
35#include <iprt/list.h>
36#include <iprt/req.h>
37
38#include <VBox/VBoxGuestLib.h>
39#include <VBox/GuestHost/GuestControl.h>
40#include <VBox/HostServices/GuestControlSvc.h>
41
42
43/**
44 * Pipe IDs for handling the guest process poll set.
45 */
46typedef enum VBOXSERVICECTRLPIPEID
47{
48 VBOXSERVICECTRLPIPEID_UNKNOWN = 0,
49 VBOXSERVICECTRLPIPEID_STDIN = 10,
50 VBOXSERVICECTRLPIPEID_STDIN_WRITABLE = 11,
51 /** Pipe for reading from guest process' stdout. */
52 VBOXSERVICECTRLPIPEID_STDOUT = 40,
53 /** Pipe for reading from guest process' stderr. */
54 VBOXSERVICECTRLPIPEID_STDERR = 50,
55 /** Notification pipe for waking up the guest process
56 * control thread. */
57 VBOXSERVICECTRLPIPEID_IPC_NOTIFY = 100
58} VBOXSERVICECTRLPIPEID;
59
60/**
61 * Structure for one (opened) guest file.
62 */
63typedef struct VBOXSERVICECTRLFILE
64{
65 /** Pointer to list archor of following
66 * list node.
67 * @todo Would be nice to have a RTListGetAnchor(). */
68 PRTLISTANCHOR pAnchor;
69 /** Node to global guest control file list. */
70 /** @todo Use a map later? */
71 RTLISTNODE Node;
72 /** The file name. */
73 char *pszName;
74 /** The file handle on the guest. */
75 RTFILE hFile;
76 /** File handle to identify this file. */
77 uint32_t uHandle;
78 /** Context ID. */
79 uint32_t uContextID;
80 /** RTFILE_O_XXX flags. */
81 uint64_t fOpen;
82} VBOXSERVICECTRLFILE;
83/** Pointer to thread data. */
84typedef VBOXSERVICECTRLFILE *PVBOXSERVICECTRLFILE;
85
86/**
87 * Structure for a guest session thread to
88 * observe/control the forked session instance from
89 * the VBoxService main executable.
90 */
91typedef struct VBOXSERVICECTRLSESSIONTHREAD
92{
93 /** Node to global guest control session list. */
94 /** @todo Use a map later? */
95 RTLISTNODE Node;
96 /** The sessions's startup info. */
97 PVBGLR3GUESTCTRLSESSIONSTARTUPINFO
98 pStartupInfo;
99 /** Critical section for thread-safe use. */
100 RTCRITSECT CritSect;
101 /** The worker thread. */
102 RTTHREAD Thread;
103 /** Process handle for forked child. */
104 RTPROCESS hProcess;
105 /** Shutdown indicator; will be set when the thread
106 * needs (or is asked) to shutdown. */
107 bool volatile fShutdown;
108 /** Indicator set by the service thread exiting. */
109 bool volatile fStopped;
110 /** Whether the thread was started or not. */
111 bool fStarted;
112#if 0 /* Pipe IPC not used yet. */
113 /** Pollset containing all the pipes. */
114 RTPOLLSET hPollSet;
115 RTPIPE hStdInW;
116 RTPIPE hStdOutR;
117 RTPIPE hStdErrR;
118 struct StdPipe
119 {
120 RTHANDLE hChild;
121 PRTHANDLE phChild;
122 } StdIn,
123 StdOut,
124 StdErr;
125 /** The notification pipe associated with this guest session.
126 * This is NIL_RTPIPE for output pipes. */
127 RTPIPE hNotificationPipeW;
128 /** The other end of hNotificationPipeW. */
129 RTPIPE hNotificationPipeR;
130#endif
131 /** Pipe for handing the secret key to the session process. */
132 RTPIPE hKeyPipe;
133 /** Secret key. */
134 uint8_t abKey[_4K];
135} VBOXSERVICECTRLSESSIONTHREAD;
136/** Pointer to thread data. */
137typedef VBOXSERVICECTRLSESSIONTHREAD *PVBOXSERVICECTRLSESSIONTHREAD;
138
139/** Defines the prefix being used for telling our service executable that we're going
140 * to spawn a new (Guest Control) user session. */
141#define VBOXSERVICECTRLSESSION_GETOPT_PREFIX "guestsession"
142
143/** Flag indicating that this session has been spawned from
144 * the main executable. */
145#define VBOXSERVICECTRLSESSION_FLAG_SPAWN RT_BIT(0)
146/** Flag indicating that this session is anonymous, that is,
147 * it will run start guest processes with the same credentials
148 * as the main executable. */
149#define VBOXSERVICECTRLSESSION_FLAG_ANONYMOUS RT_BIT(1)
150/** Flag indicating that started guest processes will dump their
151 * stdout output to a separate file on disk. For debugging. */
152#define VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT RT_BIT(2)
153/** Flag indicating that started guest processes will dump their
154 * stderr output to a separate file on disk. For debugging. */
155#define VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR RT_BIT(3)
156
157/**
158 * Structure for maintaining a guest session. This also
159 * contains all started threads (e.g. for guest processes).
160 *
161 * This structure can act in two different ways:
162 * - For legacy guest control handling (protocol version < 2)
163 * this acts as a per-guest process structure containing all
164 * the information needed to get a guest process up and running.
165 * - For newer guest control protocols (>= 2) this structure is
166 * part of the forked session child, maintaining all guest
167 * control objects under it.
168 */
169typedef struct VBOXSERVICECTRLSESSION
170{
171 /* The session's startup information. */
172 VBGLR3GUESTCTRLSESSIONSTARTUPINFO
173 StartupInfo;
174 /** List of active guest process threads
175 * (VBOXSERVICECTRLPROCESS). */
176 RTLISTANCHOR lstProcesses;
177 /** Number of guest processes in the process list. */
178 uint32_t cProcesses;
179 /** List of guest control files (VBOXSERVICECTRLFILE). */
180 RTLISTANCHOR lstFiles;
181 /** Number of guest files in the file list. */
182 uint32_t cFiles;
183 /** The session's critical section. */
184 RTCRITSECT CritSect;
185 /** Internal session flags, not related
186 * to StartupInfo stuff.
187 * @sa VBOXSERVICECTRLSESSION_FLAG_* flags. */
188 uint32_t fFlags;
189 /** How many processes do we allow keeping around at a time? */
190 uint32_t uProcsMaxKept;
191} VBOXSERVICECTRLSESSION;
192/** Pointer to guest session. */
193typedef VBOXSERVICECTRLSESSION *PVBOXSERVICECTRLSESSION;
194
195/**
196 * Structure for holding data for one (started) guest process.
197 */
198typedef struct VBOXSERVICECTRLPROCESS
199{
200 /** Node. */
201 RTLISTNODE Node;
202 /** Process handle. */
203 RTPROCESS hProcess;
204 /** Number of references using this struct. */
205 uint32_t cRefs;
206 /** The worker thread. */
207 RTTHREAD Thread;
208 /** The session this guest process
209 * is bound to. */
210 PVBOXSERVICECTRLSESSION pSession;
211 /** Shutdown indicator; will be set when the thread
212 * needs (or is asked) to shutdown. */
213 bool volatile fShutdown;
214 /** Whether the guest process thread was stopped or not. */
215 bool volatile fStopped;
216 /** Whether the guest process thread was started or not. */
217 bool fStarted;
218 /** Context ID. */
219 uint32_t uContextID;
220 /** Critical section for thread-safe use. */
221 RTCRITSECT CritSect;
222 /** Process startup information. */
223 PVBGLR3GUESTCTRLPROCSTARTUPINFO
224 pStartupInfo;
225 /** The process' PID assigned by the guest OS. */
226 uint32_t uPID;
227 /** The process' request queue to handle requests
228 * from the outside, e.g. the session. */
229 RTREQQUEUE hReqQueue;
230 /** Our pollset, used for accessing the process'
231 * std* pipes + the notification pipe. */
232 RTPOLLSET hPollSet;
233 /** StdIn pipe for addressing writes to the
234 * guest process' stdin.*/
235 RTPIPE hPipeStdInW;
236 /** StdOut pipe for addressing reads from
237 * guest process' stdout.*/
238 RTPIPE hPipeStdOutR;
239 /** StdOut pipe for addressing reads from
240 * guest process' stderr.*/
241 RTPIPE hPipeStdErrR;
242
243 /** The write end of the notification pipe that is used to poke the thread
244 * monitoring the process.
245 * This is NIL_RTPIPE for output pipes. */
246 RTPIPE hNotificationPipeW;
247 /** The other end of hNotificationPipeW, read by vgsvcGstCtrlProcessProcLoop(). */
248 RTPIPE hNotificationPipeR;
249} VBOXSERVICECTRLPROCESS;
250/** Pointer to thread data. */
251typedef VBOXSERVICECTRLPROCESS *PVBOXSERVICECTRLPROCESS;
252
253RT_C_DECLS_BEGIN
254
255extern RTLISTANCHOR g_lstControlSessionThreads;
256extern VBOXSERVICECTRLSESSION g_Session;
257extern uint32_t g_idControlSvcClient;
258extern uint64_t g_fControlHostFeatures0;
259extern bool g_fControlSupportsOptimizations;
260
261
262/** @name Guest session thread handling.
263 * @{ */
264extern int VGSvcGstCtrlSessionThreadCreate(PRTLISTANCHOR pList, const PVBGLR3GUESTCTRLSESSIONSTARTUPINFO pSessionStartupInfo, PVBOXSERVICECTRLSESSIONTHREAD *ppSessionThread);
265extern int VGSvcGstCtrlSessionThreadDestroy(PVBOXSERVICECTRLSESSIONTHREAD pSession, uint32_t uFlags);
266extern int VGSvcGstCtrlSessionThreadDestroyAll(PRTLISTANCHOR pList, uint32_t uFlags);
267extern int VGSvcGstCtrlSessionThreadTerminate(PVBOXSERVICECTRLSESSIONTHREAD pSession);
268extern RTEXITCODE VGSvcGstCtrlSessionSpawnInit(int argc, char **argv);
269/** @} */
270/** @name Per-session functions.
271 * @{ */
272extern PVBOXSERVICECTRLPROCESS VGSvcGstCtrlSessionRetainProcess(PVBOXSERVICECTRLSESSION pSession, uint32_t uPID);
273extern int VGSvcGstCtrlSessionClose(PVBOXSERVICECTRLSESSION pSession);
274extern int VGSvcGstCtrlSessionDestroy(PVBOXSERVICECTRLSESSION pSession);
275extern int VGSvcGstCtrlSessionInit(PVBOXSERVICECTRLSESSION pSession, uint32_t uFlags);
276extern int VGSvcGstCtrlSessionHandler(PVBOXSERVICECTRLSESSION pSession, uint32_t uMsg, PVBGLR3GUESTCTRLCMDCTX pHostCtx, void *pvScratchBuf, size_t cbScratchBuf, volatile bool *pfShutdown);
277extern int VGSvcGstCtrlSessionProcessAdd(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess);
278extern int VGSvcGstCtrlSessionProcessRemove(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess);
279extern int VGSvcGstCtrlSessionProcessStartAllowed(const PVBOXSERVICECTRLSESSION pSession, bool *pfAllowed);
280extern int VGSvcGstCtrlSessionReapProcesses(PVBOXSERVICECTRLSESSION pSession);
281/** @} */
282/** @name Per-guest process functions.
283 * @{ */
284extern int VGSvcGstCtrlProcessFree(PVBOXSERVICECTRLPROCESS pProcess);
285extern int VGSvcGstCtrlProcessHandleInput(PVBOXSERVICECTRLPROCESS pProcess, PVBGLR3GUESTCTRLCMDCTX pHostCtx, bool fPendingClose, void *pvBuf, uint32_t cbBuf);
286extern int VGSvcGstCtrlProcessHandleOutput(PVBOXSERVICECTRLPROCESS pProcess, PVBGLR3GUESTCTRLCMDCTX pHostCtx, uint32_t uHandle, uint32_t cbToRead, uint32_t uFlags);
287extern int VGSvcGstCtrlProcessHandleTerm(PVBOXSERVICECTRLPROCESS pProcess);
288extern void VGSvcGstCtrlProcessRelease(PVBOXSERVICECTRLPROCESS pProcess);
289extern int VGSvcGstCtrlProcessStart(const PVBOXSERVICECTRLSESSION pSession, const PVBGLR3GUESTCTRLPROCSTARTUPINFO pStartupInfo, uint32_t uContext);
290extern int VGSvcGstCtrlProcessStop(PVBOXSERVICECTRLPROCESS pProcess);
291extern int VGSvcGstCtrlProcessWait(const PVBOXSERVICECTRLPROCESS pProcess, RTMSINTERVAL msTimeout, int *pRc);
292/** @} */
293
294RT_C_DECLS_END
295
296#endif /* !GA_INCLUDED_SRC_common_VBoxService_VBoxServiceControl_h */
297
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette