1 | /** @file
|
---|
2 | *
|
---|
3 | * XPCOM server process helper module implementation functions
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | #ifdef RT_OS_OS2
|
---|
19 | # include <prproces.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include <nsMemory.h>
|
---|
23 | #include <nsString.h>
|
---|
24 | #include <nsCOMPtr.h>
|
---|
25 | #include <nsIFile.h>
|
---|
26 | #include <nsIGenericFactory.h>
|
---|
27 | #include <nsIServiceManagerUtils.h>
|
---|
28 | #include <nsICategoryManager.h>
|
---|
29 | #include <nsDirectoryServiceDefs.h>
|
---|
30 |
|
---|
31 | #include <ipcIService.h>
|
---|
32 | #include <ipcIDConnectService.h>
|
---|
33 | #include <ipcCID.h>
|
---|
34 | #include <ipcdclient.h>
|
---|
35 |
|
---|
36 | #include "prio.h"
|
---|
37 | #include "prproces.h"
|
---|
38 |
|
---|
39 | // official XPCOM headers don't define it yet
|
---|
40 | #define IPC_DCONNECTSERVICE_CONTRACTID \
|
---|
41 | "@mozilla.org/ipc/dconnect-service;1"
|
---|
42 |
|
---|
43 | // generated file
|
---|
44 | #include <VirtualBox_XPCOM.h>
|
---|
45 |
|
---|
46 | #include "server.h"
|
---|
47 | #include "Logging.h"
|
---|
48 |
|
---|
49 | #include <VBox/err.h>
|
---|
50 |
|
---|
51 | #include <iprt/assert.h>
|
---|
52 | #include <iprt/param.h>
|
---|
53 | #include <iprt/path.h>
|
---|
54 | #include <iprt/process.h>
|
---|
55 | #include <iprt/env.h>
|
---|
56 | #include <iprt/string.h>
|
---|
57 | #include <iprt/thread.h>
|
---|
58 |
|
---|
59 | #if defined(RT_OS_SOLARIS)
|
---|
60 | # include <sys/systeminfo.h>
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | /// @todo move this to RT headers (and use them in MachineImpl.cpp as well)
|
---|
64 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
65 | #define HOSTSUFF_EXE ".exe"
|
---|
66 | #else /* !RT_OS_WINDOWS */
|
---|
67 | #define HOSTSUFF_EXE ""
|
---|
68 | #endif /* !RT_OS_WINDOWS */
|
---|
69 |
|
---|
70 |
|
---|
71 | /** Name of the server executable. */
|
---|
72 | const char VBoxSVC_exe[] = RTPATH_SLASH_STR "VBoxSVC" HOSTSUFF_EXE;
|
---|
73 |
|
---|
74 | enum
|
---|
75 | {
|
---|
76 | /** Amount of time to wait for the server to establish a connection, ms */
|
---|
77 | VBoxSVC_Timeout = 30000,
|
---|
78 | /** How often to perform a connection check, ms */
|
---|
79 | VBoxSVC_WaitSlice = 100
|
---|
80 | };
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Full path to the VBoxSVC executable.
|
---|
84 | */
|
---|
85 | static char VBoxSVCPath[RTPATH_MAX];
|
---|
86 | static bool IsVBoxSVCPathSet = false;
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * The following macros define the method necessary to provide a list of
|
---|
90 | * interfaces implemented by the VirtualBox component. Note that this must be
|
---|
91 | * in sync with macros used for VirtualBox in server.cpp for the same purpose.
|
---|
92 | */
|
---|
93 |
|
---|
94 | NS_DECL_CLASSINFO(VirtualBoxWrap)
|
---|
95 | NS_IMPL_CI_INTERFACE_GETTER1(VirtualBoxWrap, IVirtualBox)
|
---|
96 |
|
---|
97 | static nsresult vboxsvcSpawnDaemon(void)
|
---|
98 | {
|
---|
99 | PRFileDesc *readable = nsnull, *writable = nsnull;
|
---|
100 | PRProcessAttr *attr = nsnull;
|
---|
101 | nsresult rv = NS_ERROR_FAILURE;
|
---|
102 | PRFileDesc *devNull;
|
---|
103 | // The ugly casts are necessary because the PR_CreateProcessDetached has
|
---|
104 | // a const array of writable strings as a parameter. It won't write. */
|
---|
105 | char * const args[] = { (char *)VBoxSVCPath, (char *)"--auto-shutdown", 0 };
|
---|
106 |
|
---|
107 | // Use a pipe to determine when the daemon process is in the position
|
---|
108 | // to actually process requests. The daemon will write "READY" to the pipe.
|
---|
109 | if (PR_CreatePipe(&readable, &writable) != PR_SUCCESS)
|
---|
110 | goto end;
|
---|
111 | PR_SetFDInheritable(writable, PR_TRUE);
|
---|
112 |
|
---|
113 | attr = PR_NewProcessAttr();
|
---|
114 | if (!attr)
|
---|
115 | goto end;
|
---|
116 |
|
---|
117 | if (PR_ProcessAttrSetInheritableFD(attr, writable, VBOXSVC_STARTUP_PIPE_NAME) != PR_SUCCESS)
|
---|
118 | goto end;
|
---|
119 |
|
---|
120 | devNull = PR_Open("/dev/null", PR_RDWR, 0);
|
---|
121 | if (!devNull)
|
---|
122 | goto end;
|
---|
123 |
|
---|
124 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, devNull);
|
---|
125 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, devNull);
|
---|
126 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardError, devNull);
|
---|
127 |
|
---|
128 | if (PR_CreateProcessDetached(VBoxSVCPath, args, nsnull, attr) != PR_SUCCESS)
|
---|
129 | goto end;
|
---|
130 |
|
---|
131 | // Close /dev/null
|
---|
132 | PR_Close(devNull);
|
---|
133 | // Close the child end of the pipe to make it the only owner of the
|
---|
134 | // file descriptor, so that unexpected closing can be detected.
|
---|
135 | PR_Close(writable);
|
---|
136 | writable = nsnull;
|
---|
137 |
|
---|
138 | char msg[10];
|
---|
139 | RT_ZERO(msg);
|
---|
140 | if ( PR_Read(readable, msg, sizeof(msg)-1) != 5
|
---|
141 | || strcmp(msg, "READY"))
|
---|
142 | {
|
---|
143 | /* If several clients start VBoxSVC simultaneously only one can
|
---|
144 | * succeed. So treat this as success as well. */
|
---|
145 | rv = NS_OK;
|
---|
146 | goto end;
|
---|
147 | }
|
---|
148 |
|
---|
149 | rv = NS_OK;
|
---|
150 |
|
---|
151 | end:
|
---|
152 | if (readable)
|
---|
153 | PR_Close(readable);
|
---|
154 | if (writable)
|
---|
155 | PR_Close(writable);
|
---|
156 | if (attr)
|
---|
157 | PR_DestroyProcessAttr(attr);
|
---|
158 | return rv;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * VirtualBox component constructor.
|
---|
164 | *
|
---|
165 | * This constructor is responsible for starting the VirtualBox server
|
---|
166 | * process, connecting to it, and redirecting the constructor request to the
|
---|
167 | * VirtualBox component defined on the server.
|
---|
168 | */
|
---|
169 | static NS_IMETHODIMP
|
---|
170 | VirtualBoxConstructor(nsISupports *aOuter, REFNSIID aIID,
|
---|
171 | void **aResult)
|
---|
172 | {
|
---|
173 | LogFlowFuncEnter();
|
---|
174 |
|
---|
175 | nsresult rc = NS_OK;
|
---|
176 | int vrc = VINF_SUCCESS;
|
---|
177 |
|
---|
178 | do
|
---|
179 | {
|
---|
180 | *aResult = NULL;
|
---|
181 | if (NULL != aOuter)
|
---|
182 | {
|
---|
183 | rc = NS_ERROR_NO_AGGREGATION;
|
---|
184 | break;
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (!IsVBoxSVCPathSet)
|
---|
188 | {
|
---|
189 | /* Get the directory containing XPCOM components -- the VBoxSVC
|
---|
190 | * executable is expected in the parent directory. */
|
---|
191 | nsCOMPtr<nsIProperties> dirServ = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rc);
|
---|
192 | if (NS_SUCCEEDED(rc))
|
---|
193 | {
|
---|
194 | nsCOMPtr<nsIFile> componentDir;
|
---|
195 | rc = dirServ->Get(NS_XPCOM_COMPONENT_DIR,
|
---|
196 | NS_GET_IID(nsIFile), getter_AddRefs(componentDir));
|
---|
197 |
|
---|
198 | if (NS_SUCCEEDED(rc))
|
---|
199 | {
|
---|
200 | nsCAutoString path;
|
---|
201 | componentDir->GetNativePath(path);
|
---|
202 |
|
---|
203 | LogFlowFunc(("component directory = \"%s\"\n", path.get()));
|
---|
204 | AssertBreakStmt(path.Length() + strlen(VBoxSVC_exe) < RTPATH_MAX,
|
---|
205 | rc = NS_ERROR_FAILURE);
|
---|
206 |
|
---|
207 | #if defined(RT_OS_SOLARIS) && defined(VBOX_WITH_HARDENING)
|
---|
208 | char achKernArch[128];
|
---|
209 | int cbKernArch = sysinfo(SI_ARCHITECTURE_K, achKernArch, sizeof(achKernArch));
|
---|
210 | if (cbKernArch > 0)
|
---|
211 | {
|
---|
212 | sprintf(VBoxSVCPath, "/opt/VirtualBox/%s%s", achKernArch, VBoxSVC_exe);
|
---|
213 | IsVBoxSVCPathSet = true;
|
---|
214 | }
|
---|
215 | else
|
---|
216 | rc = NS_ERROR_UNEXPECTED;
|
---|
217 | #else
|
---|
218 | strcpy(VBoxSVCPath, path.get());
|
---|
219 | RTPathStripFilename(VBoxSVCPath);
|
---|
220 | strcat(VBoxSVCPath, VBoxSVC_exe);
|
---|
221 |
|
---|
222 | IsVBoxSVCPathSet = true;
|
---|
223 | #endif
|
---|
224 | }
|
---|
225 | }
|
---|
226 | if (NS_FAILED(rc))
|
---|
227 | break;
|
---|
228 | }
|
---|
229 |
|
---|
230 | nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
|
---|
231 | if (NS_FAILED(rc))
|
---|
232 | break;
|
---|
233 |
|
---|
234 | /* connect to the VBoxSVC server process */
|
---|
235 |
|
---|
236 | bool startedOnce = false;
|
---|
237 | unsigned timeLeft = VBoxSVC_Timeout;
|
---|
238 |
|
---|
239 | do
|
---|
240 | {
|
---|
241 | LogFlowFunc(("Resolving server name \"%s\"...\n", VBOXSVC_IPC_NAME));
|
---|
242 |
|
---|
243 | PRUint32 serverID = 0;
|
---|
244 | rc = ipcServ->ResolveClientName(VBOXSVC_IPC_NAME, &serverID);
|
---|
245 | if (NS_FAILED(rc))
|
---|
246 | {
|
---|
247 | LogFlowFunc(("Starting server \"%s\"...\n", VBoxSVCPath));
|
---|
248 |
|
---|
249 | startedOnce = true;
|
---|
250 |
|
---|
251 | rc = vboxsvcSpawnDaemon();
|
---|
252 | if (NS_FAILED(rc))
|
---|
253 | break;
|
---|
254 |
|
---|
255 | /* wait for the server process to establish a connection */
|
---|
256 | do
|
---|
257 | {
|
---|
258 | RTThreadSleep(VBoxSVC_WaitSlice);
|
---|
259 | rc = ipcServ->ResolveClientName(VBOXSVC_IPC_NAME, &serverID);
|
---|
260 | if (NS_SUCCEEDED(rc))
|
---|
261 | break;
|
---|
262 | if (timeLeft <= VBoxSVC_WaitSlice)
|
---|
263 | {
|
---|
264 | timeLeft = 0;
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | timeLeft -= VBoxSVC_WaitSlice;
|
---|
268 | }
|
---|
269 | while (1);
|
---|
270 |
|
---|
271 | if (!timeLeft)
|
---|
272 | {
|
---|
273 | rc = IPC_ERROR_WOULD_BLOCK;
|
---|
274 | break;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | LogFlowFunc(("Connecting to server (ID=%d)...\n", serverID));
|
---|
279 |
|
---|
280 | nsCOMPtr<ipcIDConnectService> dconServ =
|
---|
281 | do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
|
---|
282 | if (NS_FAILED(rc))
|
---|
283 | break;
|
---|
284 |
|
---|
285 | rc = dconServ->CreateInstance(serverID,
|
---|
286 | CLSID_VirtualBox,
|
---|
287 | aIID, aResult);
|
---|
288 | if (NS_SUCCEEDED(rc))
|
---|
289 | break;
|
---|
290 |
|
---|
291 | LogFlowFunc(("Failed to connect (rc=%Rhrc (%#08x))\n", rc, rc));
|
---|
292 |
|
---|
293 | /* It's possible that the server gets shut down after we
|
---|
294 | * successfully resolve the server name but before it
|
---|
295 | * receives our CreateInstance() request. So, check for the
|
---|
296 | * name again, and restart the cycle if it fails. */
|
---|
297 | if (!startedOnce)
|
---|
298 | {
|
---|
299 | nsresult rc2 =
|
---|
300 | ipcServ->ResolveClientName(VBOXSVC_IPC_NAME, &serverID);
|
---|
301 | if (NS_SUCCEEDED(rc2))
|
---|
302 | break;
|
---|
303 |
|
---|
304 | LogFlowFunc(("Server seems to have terminated before receiving our request. Will try again.\n"));
|
---|
305 | }
|
---|
306 | else
|
---|
307 | break;
|
---|
308 | }
|
---|
309 | while (1);
|
---|
310 | }
|
---|
311 | while (0);
|
---|
312 |
|
---|
313 | LogFlowFunc(("rc=%Rhrc (%#08x), vrc=%Rrc\n", rc, rc, vrc));
|
---|
314 | LogFlowFuncLeave();
|
---|
315 |
|
---|
316 | return rc;
|
---|
317 | }
|
---|
318 |
|
---|
319 | #if 0
|
---|
320 | /// @todo not really necessary for the moment
|
---|
321 | /**
|
---|
322 | *
|
---|
323 | * @param aCompMgr
|
---|
324 | * @param aPath
|
---|
325 | * @param aLoaderStr
|
---|
326 | * @param aType
|
---|
327 | * @param aInfo
|
---|
328 | *
|
---|
329 | * @return
|
---|
330 | */
|
---|
331 | static NS_IMETHODIMP
|
---|
332 | VirtualBoxRegistration(nsIComponentManager *aCompMgr,
|
---|
333 | nsIFile *aPath,
|
---|
334 | const char *aLoaderStr,
|
---|
335 | const char *aType,
|
---|
336 | const nsModuleComponentInfo *aInfo)
|
---|
337 | {
|
---|
338 | nsCAutoString modulePath;
|
---|
339 | aPath->GetNativePath(modulePath);
|
---|
340 | nsCAutoString moduleTarget;
|
---|
341 | aPath->GetNativeTarget(moduleTarget);
|
---|
342 |
|
---|
343 | LogFlowFunc(("aPath=%s, aTarget=%s, aLoaderStr=%s, aType=%s\n",
|
---|
344 | modulePath.get(), moduleTarget.get(), aLoaderStr, aType));
|
---|
345 |
|
---|
346 | nsresult rc = NS_OK;
|
---|
347 |
|
---|
348 | return rc;
|
---|
349 | }
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Component definition table.
|
---|
354 | * Lists all components defined in this module.
|
---|
355 | */
|
---|
356 | static const nsModuleComponentInfo components[] =
|
---|
357 | {
|
---|
358 | {
|
---|
359 | "VirtualBox component", // description
|
---|
360 | NS_VIRTUALBOX_CID, NS_VIRTUALBOX_CONTRACTID, // CID/ContractID
|
---|
361 | VirtualBoxConstructor, // constructor function
|
---|
362 | NULL, /* VirtualBoxRegistration, */ // registration function
|
---|
363 | NULL, // deregistration function
|
---|
364 | NULL, // destructor function
|
---|
365 | /// @todo
|
---|
366 | NS_CI_INTERFACE_GETTER_NAME(VirtualBoxWrap), // interfaces function
|
---|
367 | NULL, // language helper
|
---|
368 | /// @todo
|
---|
369 | &NS_CLASSINFO_NAME(VirtualBoxWrap) // global class info & flags
|
---|
370 | }
|
---|
371 | };
|
---|
372 |
|
---|
373 | NS_IMPL_NSGETMODULE(VirtualBox_Server_Module, components)
|
---|