1 |
|
---|
2 | /* $Id: VBoxServiceControl.cpp 28446 2010-04-19 08:45:03Z vboxsync $ */
|
---|
3 | /** @file
|
---|
4 | * VBoxServiceControl - Host-driven Guest Control.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #include <iprt/asm.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/getopt.h>
|
---|
30 | #include <iprt/mem.h>
|
---|
31 | #include <iprt/semaphore.h>
|
---|
32 | #include <iprt/thread.h>
|
---|
33 | #include <VBox/VBoxGuestLib.h>
|
---|
34 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
35 | #include "VBoxServiceInternal.h"
|
---|
36 | #include "VBoxServiceUtils.h"
|
---|
37 |
|
---|
38 | using namespace guestControl;
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *******************************************************************************/
|
---|
43 | /** The control interval (millseconds). */
|
---|
44 | uint32_t g_ControlInterval = 0;
|
---|
45 | /** The semaphore we're blocking on. */
|
---|
46 | static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
47 | /** The guest property service client ID. */
|
---|
48 | static uint32_t g_GuestControlSvcClientID = 0;
|
---|
49 | /** List of spawned processes */
|
---|
50 | RTLISTNODE g_GuestControlExecThreads;
|
---|
51 |
|
---|
52 |
|
---|
53 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
54 | static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
|
---|
55 | {
|
---|
56 | return VINF_SUCCESS;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
61 | static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
62 | {
|
---|
63 | int rc = -1;
|
---|
64 | if (ppszShort)
|
---|
65 | /* no short options */;
|
---|
66 | else if (!strcmp(argv[*pi], "--control-interval"))
|
---|
67 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
68 | &g_ControlInterval, 1, UINT32_MAX - 1);
|
---|
69 | return rc;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
74 | static DECLCALLBACK(int) VBoxServiceControlInit(void)
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * If not specified, find the right interval default.
|
---|
78 | * Then create the event sem to block on.
|
---|
79 | */
|
---|
80 | if (!g_ControlInterval)
|
---|
81 | g_ControlInterval = 1000;
|
---|
82 |
|
---|
83 | int rc = RTSemEventMultiCreate(&g_hControlEvent);
|
---|
84 | AssertRCReturn(rc, rc);
|
---|
85 |
|
---|
86 | rc = VbglR3GuestCtrlConnect(&g_GuestControlSvcClientID);
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | VBoxServiceVerbose(3, "Control: Service Client ID: %#x\n", g_GuestControlSvcClientID);
|
---|
89 | else
|
---|
90 | {
|
---|
91 | VBoxServiceError("Control: Failed to connect to the guest control service! Error: %Rrc\n", rc);
|
---|
92 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
93 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Init thread list. */
|
---|
97 | RTListInit(&g_GuestControlExecThreads);
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | static int VBoxServiceControlHandleCmdExec(uint32_t u32ClientId, uint32_t uNumParms)
|
---|
103 | {
|
---|
104 | uint32_t uContextID;
|
---|
105 | char szCmd[_1K];
|
---|
106 | uint32_t uFlags;
|
---|
107 | char szArgs[_1K];
|
---|
108 | uint32_t uNumArgs;
|
---|
109 | char szEnv[_64K];
|
---|
110 | uint32_t cbEnv = sizeof(szEnv);
|
---|
111 | uint32_t uNumEnvVars;
|
---|
112 | char szStdIn[_1K];
|
---|
113 | char szStdOut[_1K];
|
---|
114 | char szStdErr[_1K];
|
---|
115 | char szUser[128];
|
---|
116 | char szPassword[128];
|
---|
117 | uint32_t uTimeLimitMS;
|
---|
118 |
|
---|
119 | if (uNumParms != 14)
|
---|
120 | return VERR_INVALID_PARAMETER;
|
---|
121 |
|
---|
122 | int rc = VbglR3GuestCtrlExecGetHostCmd(u32ClientId,
|
---|
123 | uNumParms,
|
---|
124 | &uContextID,
|
---|
125 | /* Command */
|
---|
126 | szCmd, sizeof(szCmd),
|
---|
127 | /* Flags */
|
---|
128 | &uFlags,
|
---|
129 | /* Arguments */
|
---|
130 | szArgs, sizeof(szArgs), &uNumArgs,
|
---|
131 | /* Environment */
|
---|
132 | szEnv, &cbEnv, &uNumEnvVars,
|
---|
133 | /* Pipes */
|
---|
134 | szStdIn, sizeof(szStdIn),
|
---|
135 | szStdOut, sizeof(szStdOut),
|
---|
136 | szStdErr, sizeof(szStdErr),
|
---|
137 | /* Credentials */
|
---|
138 | szUser, sizeof(szUser),
|
---|
139 | szPassword, sizeof(szPassword),
|
---|
140 | /* Timelimit */
|
---|
141 | &uTimeLimitMS);
|
---|
142 | if (RT_FAILURE(rc))
|
---|
143 | {
|
---|
144 | VBoxServiceError("Control: Failed to retrieve execution command! Error: %Rrc\n", rc);
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | rc = VBoxServiceControlExecProcess(uContextID, szCmd, uFlags, szArgs, uNumArgs,
|
---|
149 | szEnv, cbEnv, uNumEnvVars,
|
---|
150 | szStdIn, szStdOut, szStdErr,
|
---|
151 | szUser, szPassword, uTimeLimitMS);
|
---|
152 | }
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
158 | DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
|
---|
159 | {
|
---|
160 | /*
|
---|
161 | * Tell the control thread that it can continue
|
---|
162 | * spawning services.
|
---|
163 | */
|
---|
164 | RTThreadUserSignal(RTThreadSelf());
|
---|
165 | Assert(g_GuestControlSvcClientID > 0);
|
---|
166 |
|
---|
167 | int rc = VINF_SUCCESS;
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Execution loop.
|
---|
171 | *
|
---|
172 | * @todo
|
---|
173 | */
|
---|
174 | for (;;)
|
---|
175 | {
|
---|
176 | uint32_t uMsg;
|
---|
177 | uint32_t uNumParms;
|
---|
178 | VBoxServiceVerbose(3, "Control: Waiting for host msg ...\n");
|
---|
179 | rc = VbglR3GuestCtrlGetHostMsg(g_GuestControlSvcClientID, &uMsg, &uNumParms, 1000 /* 1s timeout */);
|
---|
180 | if (rc == VERR_TOO_MUCH_DATA)
|
---|
181 | {
|
---|
182 | VBoxServiceVerbose(3, "Control: Message requires %ld parameters, but only 2 supplied -- retrying request ...\n", uNumParms);
|
---|
183 | rc = VINF_SUCCESS;
|
---|
184 | }
|
---|
185 | else if (rc == VERR_TIMEOUT)
|
---|
186 | {
|
---|
187 | VBoxServiceVerbose(3, "Control: Wait timed out, waiting for next round ...\n");
|
---|
188 | RTThreadSleep(100);
|
---|
189 | }
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | switch(uMsg)
|
---|
193 | {
|
---|
194 | case GETHOSTMSG_EXEC_CMD:
|
---|
195 | rc = VBoxServiceControlHandleCmdExec(g_GuestControlSvcClientID, uNumParms);
|
---|
196 | break;
|
---|
197 |
|
---|
198 | default:
|
---|
199 | VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%ld\n", uMsg);
|
---|
200 | /* Don't terminate here; just wait for the next message. */
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Block for a while.
|
---|
207 | *
|
---|
208 | * The event semaphore takes care of ignoring interruptions and it
|
---|
209 | * allows us to implement service wakeup later.
|
---|
210 | */
|
---|
211 | if (*pfShutdown)
|
---|
212 | break;
|
---|
213 | int rc2 = RTSemEventMultiWait(g_hControlEvent, g_ControlInterval);
|
---|
214 | if (*pfShutdown)
|
---|
215 | break;
|
---|
216 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
217 | {
|
---|
218 | VBoxServiceError("Control: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
219 | rc = rc2;
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
225 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
231 | static DECLCALLBACK(void) VBoxServiceControlStop(void)
|
---|
232 | {
|
---|
233 | /** @todo Later, figure what to do if we're in RTProcWait(). it's a very
|
---|
234 | * annoying call since doesn't support timeouts in the posix world. */
|
---|
235 | RTSemEventMultiSignal(g_hControlEvent);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
240 | static DECLCALLBACK(void) VBoxServiceControlTerm(void)
|
---|
241 | {
|
---|
242 | /* Signal all threads that we want to shutdown. */
|
---|
243 | PVBOXSERVICECTRLTHREAD pNode;
|
---|
244 | RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
|
---|
245 | ASMAtomicXchgBool(&pNode->fShutdown, true);
|
---|
246 |
|
---|
247 | /* Wait for threads to shutdown. */
|
---|
248 | RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
|
---|
249 | {
|
---|
250 | if (pNode->Thread != NIL_RTTHREAD)
|
---|
251 | {
|
---|
252 | int rc2 = RTThreadWait(pNode->Thread, 30 * 1000 /* Wait 30 seconds max. */, NULL);
|
---|
253 | if (RT_FAILURE(rc2))
|
---|
254 | VBoxServiceError("Control: Thread (PID: %u) failed to stop; rc2=%Rrc\n", pNode->uPID, rc2);
|
---|
255 | }
|
---|
256 | VBoxServiceControlExecDestroyThreadData(pNode);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /* Finally destroy thread list. */
|
---|
260 | pNode = RTListNodeGetFirst(&g_GuestControlExecThreads, VBOXSERVICECTRLTHREAD, Node);
|
---|
261 | while (pNode)
|
---|
262 | {
|
---|
263 | PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICECTRLTHREAD, Node);
|
---|
264 |
|
---|
265 | RTListNodeRemove(&pNode->Node);
|
---|
266 | RTMemFree(pNode);
|
---|
267 |
|
---|
268 | if (pNext && RTListNodeIsLast(&g_GuestControlExecThreads, &pNext->Node))
|
---|
269 | break;
|
---|
270 |
|
---|
271 | pNode = pNext;
|
---|
272 | }
|
---|
273 |
|
---|
274 | VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
|
---|
275 | g_GuestControlSvcClientID = 0;
|
---|
276 |
|
---|
277 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
278 | {
|
---|
279 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
280 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * The 'vminfo' service description.
|
---|
287 | */
|
---|
288 | VBOXSERVICE g_Control =
|
---|
289 | {
|
---|
290 | /* pszName. */
|
---|
291 | "control",
|
---|
292 | /* pszDescription. */
|
---|
293 | "Host-driven Guest Control",
|
---|
294 | /* pszUsage. */
|
---|
295 | "[--control-interval <ms>]"
|
---|
296 | ,
|
---|
297 | /* pszOptions. */
|
---|
298 | " --control-interval Specifies the interval at which to check for\n"
|
---|
299 | " new ocntrol commands. The default is 1000 ms.\n"
|
---|
300 | ,
|
---|
301 | /* methods */
|
---|
302 | VBoxServiceControlPreInit,
|
---|
303 | VBoxServiceControlOption,
|
---|
304 | VBoxServiceControlInit,
|
---|
305 | VBoxServiceControlWorker,
|
---|
306 | VBoxServiceControlStop,
|
---|
307 | VBoxServiceControlTerm
|
---|
308 | };
|
---|
309 |
|
---|