1 |
|
---|
2 | /* $Id: VBoxServiceControl.cpp 28119 2010-04-09 07:54:19Z 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/assert.h>
|
---|
28 | #include <iprt/getopt.h>
|
---|
29 | #include <iprt/mem.h>
|
---|
30 | #include <iprt/semaphore.h>
|
---|
31 | #include <iprt/thread.h>
|
---|
32 | #include <VBox/VBoxGuestLib.h>
|
---|
33 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
34 | #include "VBoxServiceInternal.h"
|
---|
35 | #include "VBoxServiceUtils.h"
|
---|
36 |
|
---|
37 | using namespace guestControl;
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Global Variables *
|
---|
41 | *******************************************************************************/
|
---|
42 | /** The control interval (millseconds). */
|
---|
43 | uint32_t g_ControlInterval = 0;
|
---|
44 | /** The semaphore we're blocking on. */
|
---|
45 | static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
46 | /** The guest property service client ID. */
|
---|
47 | static uint32_t g_GuestControlSvcClientID = 0;
|
---|
48 |
|
---|
49 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
50 | static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
|
---|
51 | {
|
---|
52 | return VINF_SUCCESS;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
57 | static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
58 | {
|
---|
59 | int rc = -1;
|
---|
60 | if (ppszShort)
|
---|
61 | /* no short options */;
|
---|
62 | else if (!strcmp(argv[*pi], "--control-interval"))
|
---|
63 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
64 | &g_ControlInterval, 1, UINT32_MAX - 1);
|
---|
65 | return rc;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
70 | static DECLCALLBACK(int) VBoxServiceControlInit(void)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * If not specified, find the right interval default.
|
---|
74 | * Then create the event sem to block on.
|
---|
75 | */
|
---|
76 | if (!g_ControlInterval)
|
---|
77 | g_ControlInterval = 1000;
|
---|
78 |
|
---|
79 | int rc = RTSemEventMultiCreate(&g_hControlEvent);
|
---|
80 | AssertRCReturn(rc, rc);
|
---|
81 |
|
---|
82 | rc = VbglR3GuestCtrlConnect(&g_GuestControlSvcClientID);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | VBoxServiceVerbose(3, "Control: Service Client ID: %#x\n", g_GuestControlSvcClientID);
|
---|
85 | else
|
---|
86 | {
|
---|
87 | VBoxServiceError("Control: Failed to connect to the guest control service! Error: %Rrc\n", rc);
|
---|
88 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
89 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | static int VBoxServiceControlHandleCmdExec(uint32_t u32ClientId, uint32_t uNumParms)
|
---|
97 | {
|
---|
98 | char szCmd[_1K];
|
---|
99 | uint32_t uFlags;
|
---|
100 | char szArgs[_1K];
|
---|
101 | uint32_t uNumArgs;
|
---|
102 | char szEnv[_64K];
|
---|
103 | uint32_t cbEnv = sizeof(szEnv);
|
---|
104 | uint32_t uNumEnvVars;
|
---|
105 | char szStdIn[_1K];
|
---|
106 | char szStdOut[_1K];
|
---|
107 | char szStdErr[_1K];
|
---|
108 | char szUser[128];
|
---|
109 | char szPassword[128];
|
---|
110 | uint32_t uTimeLimitMS;
|
---|
111 |
|
---|
112 | int rc = VbglR3GuestCtrlGetHostCmdExec(u32ClientId, uNumParms,
|
---|
113 | /* Command */
|
---|
114 | szCmd, sizeof(szCmd),
|
---|
115 | /* Flags */
|
---|
116 | &uFlags,
|
---|
117 | /* Arguments */
|
---|
118 | szArgs, sizeof(szArgs), &uNumArgs,
|
---|
119 | /* Environment */
|
---|
120 | szEnv, &cbEnv, &uNumEnvVars,
|
---|
121 | /* Pipes */
|
---|
122 | szStdIn, sizeof(szStdIn),
|
---|
123 | szStdOut, sizeof(szStdOut),
|
---|
124 | szStdErr, sizeof(szStdErr),
|
---|
125 | /* Credentials */
|
---|
126 | szUser, sizeof(szUser),
|
---|
127 | szPassword, sizeof(szPassword),
|
---|
128 | /* Timelimit */
|
---|
129 | &uTimeLimitMS);
|
---|
130 | if (RT_FAILURE(rc))
|
---|
131 | {
|
---|
132 | VBoxServiceError("Control: Failed to retrieve execution command! Error: %Rrc\n", rc);
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | rc = VBoxServiceControlExecProcess(szCmd, uFlags, szArgs, uNumArgs,
|
---|
137 | szEnv, cbEnv, uNumEnvVars,
|
---|
138 | szStdIn, szStdOut, szStdErr,
|
---|
139 | szUser, szPassword, uTimeLimitMS);
|
---|
140 | }
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
146 | DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
|
---|
147 | {
|
---|
148 | /*
|
---|
149 | * Tell the control thread that it can continue
|
---|
150 | * spawning services.
|
---|
151 | */
|
---|
152 | RTThreadUserSignal(RTThreadSelf());
|
---|
153 | Assert(g_GuestControlSvcClientID > 0);
|
---|
154 |
|
---|
155 | int rc = VINF_SUCCESS;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Execution loop.
|
---|
159 | *
|
---|
160 | * @todo
|
---|
161 | */
|
---|
162 | for (;;)
|
---|
163 | {
|
---|
164 | uint32_t uMsg;
|
---|
165 | uint32_t uNumParms;
|
---|
166 | rc = VbglR3GuestCtrlGetHostMsg(g_GuestControlSvcClientID, &uMsg, &uNumParms);
|
---|
167 | if (rc == VERR_TOO_MUCH_DATA)
|
---|
168 | {
|
---|
169 | VBoxServiceVerbose(3, "Control: Message requires %ld parameters, but only 2 supplied.\n", uNumParms);
|
---|
170 | rc = VINF_SUCCESS;
|
---|
171 | }
|
---|
172 | if (RT_SUCCESS(rc))
|
---|
173 | {
|
---|
174 | switch(uMsg)
|
---|
175 | {
|
---|
176 | case GETHOSTMSG_EXEC_CMD:
|
---|
177 | rc = VBoxServiceControlHandleCmdExec(g_GuestControlSvcClientID, uNumParms);
|
---|
178 | break;
|
---|
179 |
|
---|
180 | default:
|
---|
181 | VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%ld\n", uMsg);
|
---|
182 | /* Don't terminate here; just wait for the next message. */
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Block for a while.
|
---|
189 | *
|
---|
190 | * The event semaphore takes care of ignoring interruptions and it
|
---|
191 | * allows us to implement service wakeup later.
|
---|
192 | */
|
---|
193 | if (*pfShutdown)
|
---|
194 | break;
|
---|
195 | int rc2 = RTSemEventMultiWait(g_hControlEvent, g_ControlInterval);
|
---|
196 | if (*pfShutdown)
|
---|
197 | break;
|
---|
198 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
199 | {
|
---|
200 | VBoxServiceError("Control: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
201 | rc = rc2;
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
207 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
208 | return rc;
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
213 | static DECLCALLBACK(void) VBoxServiceControlStop(void)
|
---|
214 | {
|
---|
215 | /** @todo Later, figure what to do if we're in RTProcWait(). it's a very
|
---|
216 | * annoying call since doesn't support timeouts in the posix world. */
|
---|
217 | RTSemEventMultiSignal(g_hControlEvent);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
222 | static DECLCALLBACK(void) VBoxServiceControlTerm(void)
|
---|
223 | {
|
---|
224 | /* Nothing here yet. */
|
---|
225 | VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
|
---|
226 | g_GuestControlSvcClientID = 0;
|
---|
227 |
|
---|
228 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
229 | {
|
---|
230 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
231 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * The 'vminfo' service description.
|
---|
238 | */
|
---|
239 | VBOXSERVICE g_Control =
|
---|
240 | {
|
---|
241 | /* pszName. */
|
---|
242 | "control",
|
---|
243 | /* pszDescription. */
|
---|
244 | "Host-driven Guest Control",
|
---|
245 | /* pszUsage. */
|
---|
246 | "[--control-interval <ms>]"
|
---|
247 | ,
|
---|
248 | /* pszOptions. */
|
---|
249 | " --control-interval Specifies the interval at which to check for\n"
|
---|
250 | " new ocntrol commands. The default is 1000 ms.\n"
|
---|
251 | ,
|
---|
252 | /* methods */
|
---|
253 | VBoxServiceControlPreInit,
|
---|
254 | VBoxServiceControlOption,
|
---|
255 | VBoxServiceControlInit,
|
---|
256 | VBoxServiceControlWorker,
|
---|
257 | VBoxServiceControlStop,
|
---|
258 | VBoxServiceControlTerm
|
---|
259 | };
|
---|
260 |
|
---|