1 | /* $Id: server_module.cpp 63378 2016-08-12 18:29:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * XPCOM server process helper module implementation functions
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
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 |
|
---|
19 | #ifdef RT_OS_OS2
|
---|
20 | # include <prproces.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #include <nsMemory.h>
|
---|
24 | #include <nsString.h>
|
---|
25 | #include <nsCOMPtr.h>
|
---|
26 | #include <nsIFile.h>
|
---|
27 | #include <nsIGenericFactory.h>
|
---|
28 | #include <nsIServiceManagerUtils.h>
|
---|
29 | #include <nsICategoryManager.h>
|
---|
30 | #include <nsDirectoryServiceDefs.h>
|
---|
31 |
|
---|
32 | #include <ipcIService.h>
|
---|
33 | #include <ipcIDConnectService.h>
|
---|
34 | #include <ipcCID.h>
|
---|
35 | #include <ipcdclient.h>
|
---|
36 |
|
---|
37 | #include "prio.h"
|
---|
38 | #include "prproces.h"
|
---|
39 |
|
---|
40 | // official XPCOM headers don't define it yet
|
---|
41 | #define IPC_DCONNECTSERVICE_CONTRACTID \
|
---|
42 | "@mozilla.org/ipc/dconnect-service;1"
|
---|
43 |
|
---|
44 | // generated file
|
---|
45 | #include <VirtualBox_XPCOM.h>
|
---|
46 |
|
---|
47 | #include "server.h"
|
---|
48 | #include "Logging.h"
|
---|
49 |
|
---|
50 | #include <VBox/err.h>
|
---|
51 |
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/param.h>
|
---|
54 | #include <iprt/path.h>
|
---|
55 | #include <iprt/process.h>
|
---|
56 | #include <iprt/env.h>
|
---|
57 | #include <iprt/string.h>
|
---|
58 | #include <iprt/thread.h>
|
---|
59 |
|
---|
60 | #if defined(RT_OS_SOLARIS)
|
---|
61 | # include <sys/systeminfo.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | /// @todo move this to RT headers (and use them in MachineImpl.cpp as well)
|
---|
65 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
66 | #define HOSTSUFF_EXE ".exe"
|
---|
67 | #else /* !RT_OS_WINDOWS */
|
---|
68 | #define HOSTSUFF_EXE ""
|
---|
69 | #endif /* !RT_OS_WINDOWS */
|
---|
70 |
|
---|
71 |
|
---|
72 | /** Name of the server executable. */
|
---|
73 | const char VBoxSVC_exe[] = RTPATH_SLASH_STR "VBoxSVC" HOSTSUFF_EXE;
|
---|
74 |
|
---|
75 | enum
|
---|
76 | {
|
---|
77 | /** Amount of time to wait for the server to establish a connection, ms */
|
---|
78 | VBoxSVC_Timeout = 30000,
|
---|
79 | /** How often to perform a connection check, ms */
|
---|
80 | VBoxSVC_WaitSlice = 100
|
---|
81 | };
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Full path to the VBoxSVC executable.
|
---|
85 | */
|
---|
86 | static char VBoxSVCPath[RTPATH_MAX];
|
---|
87 | static bool IsVBoxSVCPathSet = false;
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * The following macros define the method necessary to provide a list of
|
---|
91 | * interfaces implemented by the VirtualBox component. Note that this must be
|
---|
92 | * in sync with macros used for VirtualBox in server.cpp for the same purpose.
|
---|
93 | */
|
---|
94 |
|
---|
95 | NS_DECL_CLASSINFO(VirtualBoxWrap)
|
---|
96 | NS_IMPL_CI_INTERFACE_GETTER1(VirtualBoxWrap, IVirtualBox)
|
---|
97 |
|
---|
98 | static nsresult vboxsvcSpawnDaemon(void)
|
---|
99 | {
|
---|
100 | PRFileDesc *readable = nsnull, *writable = nsnull;
|
---|
101 | PRProcessAttr *attr = nsnull;
|
---|
102 | nsresult rv = NS_ERROR_FAILURE;
|
---|
103 | PRFileDesc *devNull;
|
---|
104 | // The ugly casts are necessary because the PR_CreateProcessDetached has
|
---|
105 | // a const array of writable strings as a parameter. It won't write. */
|
---|
106 | char * const args[] = { (char *)VBoxSVCPath, (char *)"--auto-shutdown", 0 };
|
---|
107 |
|
---|
108 | // Use a pipe to determine when the daemon process is in the position
|
---|
109 | // to actually process requests. The daemon will write "READY" to the pipe.
|
---|
110 | if (PR_CreatePipe(&readable, &writable) != PR_SUCCESS)
|
---|
111 | goto end;
|
---|
112 | PR_SetFDInheritable(writable, PR_TRUE);
|
---|
113 |
|
---|
114 | attr = PR_NewProcessAttr();
|
---|
115 | if (!attr)
|
---|
116 | goto end;
|
---|
117 |
|
---|
118 | if (PR_ProcessAttrSetInheritableFD(attr, writable, VBOXSVC_STARTUP_PIPE_NAME) != PR_SUCCESS)
|
---|
119 | goto end;
|
---|
120 |
|
---|
121 | devNull = PR_Open("/dev/null", PR_RDWR, 0);
|
---|
122 | if (!devNull)
|
---|
123 | goto end;
|
---|
124 |
|
---|
125 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, devNull);
|
---|
126 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, devNull);
|
---|
127 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardError, devNull);
|
---|
128 |
|
---|
129 | if (PR_CreateProcessDetached(VBoxSVCPath, args, nsnull, attr) != PR_SUCCESS)
|
---|
130 | goto end;
|
---|
131 |
|
---|
132 | // Close /dev/null
|
---|
133 | PR_Close(devNull);
|
---|
134 | // Close the child end of the pipe to make it the only owner of the
|
---|
135 | // file descriptor, so that unexpected closing can be detected.
|
---|
136 | PR_Close(writable);
|
---|
137 | writable = nsnull;
|
---|
138 |
|
---|
139 | char msg[10];
|
---|
140 | RT_ZERO(msg);
|
---|
141 | if ( PR_Read(readable, msg, sizeof(msg)-1) != 5
|
---|
142 | || strcmp(msg, "READY"))
|
---|
143 | {
|
---|
144 | /* If several clients start VBoxSVC simultaneously only one can
|
---|
145 | * succeed. So treat this as success as well. */
|
---|
146 | rv = NS_OK;
|
---|
147 | goto end;
|
---|
148 | }
|
---|
149 |
|
---|
150 | rv = NS_OK;
|
---|
151 |
|
---|
152 | end:
|
---|
153 | if (readable)
|
---|
154 | PR_Close(readable);
|
---|
155 | if (writable)
|
---|
156 | PR_Close(writable);
|
---|
157 | if (attr)
|
---|
158 | PR_DestroyProcessAttr(attr);
|
---|
159 | return rv;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * VirtualBox component constructor.
|
---|
165 | *
|
---|
166 | * This constructor is responsible for starting the VirtualBox server
|
---|
167 | * process, connecting to it, and redirecting the constructor request to the
|
---|
168 | * VirtualBox component defined on the server.
|
---|
169 | */
|
---|
170 | static NS_IMETHODIMP
|
---|
171 | VirtualBoxConstructor(nsISupports *aOuter, REFNSIID aIID,
|
---|
172 | void **aResult)
|
---|
173 | {
|
---|
174 | LogFlowFuncEnter();
|
---|
175 |
|
---|
176 | nsresult rc = NS_OK;
|
---|
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)\n", rc, rc));
|
---|
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)
|
---|