1 | /* $Id: server.cpp 46969 2013-07-04 06:35:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * XPCOM server process (VBoxSVC) start point.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2004-2012 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 | #include <ipcIService.h>
|
---|
19 | #include <ipcCID.h>
|
---|
20 |
|
---|
21 | #include <nsIComponentRegistrar.h>
|
---|
22 |
|
---|
23 | #ifdef XPCOM_GLUE
|
---|
24 | # include <nsXPCOMGlue.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <nsEventQueueUtils.h>
|
---|
28 | #include <nsGenericFactory.h>
|
---|
29 |
|
---|
30 | #include "prio.h"
|
---|
31 | #include "prproces.h"
|
---|
32 |
|
---|
33 | #include "server.h"
|
---|
34 |
|
---|
35 | #include "Logging.h"
|
---|
36 |
|
---|
37 | #include <VBox/param.h>
|
---|
38 |
|
---|
39 | #include <iprt/buildconfig.h>
|
---|
40 | #include <iprt/initterm.h>
|
---|
41 | #include <iprt/critsect.h>
|
---|
42 | #include <iprt/getopt.h>
|
---|
43 | #include <iprt/message.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/stream.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/timer.h>
|
---|
48 | #include <iprt/env.h>
|
---|
49 |
|
---|
50 | #include <signal.h> // for the signal handler
|
---|
51 | #include <stdlib.h>
|
---|
52 | #include <unistd.h>
|
---|
53 | #include <errno.h>
|
---|
54 | #include <fcntl.h>
|
---|
55 | #include <sys/stat.h>
|
---|
56 | #include <sys/resource.h>
|
---|
57 |
|
---|
58 | /////////////////////////////////////////////////////////////////////////////
|
---|
59 | // VirtualBox component instantiation
|
---|
60 | /////////////////////////////////////////////////////////////////////////////
|
---|
61 |
|
---|
62 | #include <nsIGenericFactory.h>
|
---|
63 | #include <VirtualBox_XPCOM.h>
|
---|
64 |
|
---|
65 | #include "ApplianceImpl.h"
|
---|
66 | #include "AudioAdapterImpl.h"
|
---|
67 | #include "BandwidthControlImpl.h"
|
---|
68 | #include "BandwidthGroupImpl.h"
|
---|
69 | #include "DHCPServerRunner.h"
|
---|
70 | #include "DHCPServerImpl.h"
|
---|
71 | #include "GuestOSTypeImpl.h"
|
---|
72 | #include "HostImpl.h"
|
---|
73 | #include "HostNetworkInterfaceImpl.h"
|
---|
74 | #include "MachineImpl.h"
|
---|
75 | #include "MediumFormatImpl.h"
|
---|
76 | #include "MediumImpl.h"
|
---|
77 | #include "NATEngineImpl.h"
|
---|
78 | #include "NetworkAdapterImpl.h"
|
---|
79 | #include "ParallelPortImpl.h"
|
---|
80 | #include "ProgressProxyImpl.h"
|
---|
81 | #include "SerialPortImpl.h"
|
---|
82 | #include "SharedFolderImpl.h"
|
---|
83 | #include "SnapshotImpl.h"
|
---|
84 | #include "StorageControllerImpl.h"
|
---|
85 | #include "SystemPropertiesImpl.h"
|
---|
86 | #include "USBControllerImpl.h"
|
---|
87 | #include "VFSExplorerImpl.h"
|
---|
88 | #include "VirtualBoxImpl.h"
|
---|
89 | #include "VRDEServerImpl.h"
|
---|
90 | #ifdef VBOX_WITH_USB
|
---|
91 | # include "HostUSBDeviceImpl.h"
|
---|
92 | # include "USBDeviceFilterImpl.h"
|
---|
93 | # include "USBDeviceImpl.h"
|
---|
94 | #endif
|
---|
95 | #ifdef VBOX_WITH_EXTPACK
|
---|
96 | # include "ExtPackManagerImpl.h"
|
---|
97 | #endif
|
---|
98 | # include "NATNetworkImpl.h"
|
---|
99 |
|
---|
100 |
|
---|
101 | /* implement nsISupports parts of our objects with support for nsIClassInfo */
|
---|
102 |
|
---|
103 | NS_DECL_CLASSINFO(VirtualBox)
|
---|
104 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VirtualBox, IVirtualBox)
|
---|
105 |
|
---|
106 | NS_DECL_CLASSINFO(Machine)
|
---|
107 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Machine, IMachine)
|
---|
108 |
|
---|
109 | NS_DECL_CLASSINFO(VFSExplorer)
|
---|
110 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VFSExplorer, IVFSExplorer)
|
---|
111 |
|
---|
112 | NS_DECL_CLASSINFO(Appliance)
|
---|
113 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Appliance, IAppliance)
|
---|
114 |
|
---|
115 | NS_DECL_CLASSINFO(VirtualSystemDescription)
|
---|
116 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VirtualSystemDescription, IVirtualSystemDescription)
|
---|
117 |
|
---|
118 | NS_DECL_CLASSINFO(SessionMachine)
|
---|
119 | NS_IMPL_THREADSAFE_ISUPPORTS2_CI(SessionMachine, IMachine, IInternalMachineControl)
|
---|
120 |
|
---|
121 | NS_DECL_CLASSINFO(SnapshotMachine)
|
---|
122 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SnapshotMachine, IMachine)
|
---|
123 |
|
---|
124 | NS_DECL_CLASSINFO(Snapshot)
|
---|
125 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Snapshot, ISnapshot)
|
---|
126 |
|
---|
127 | NS_DECL_CLASSINFO(Medium)
|
---|
128 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Medium, IMedium)
|
---|
129 |
|
---|
130 | NS_DECL_CLASSINFO(MediumAttachment)
|
---|
131 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(MediumAttachment, IMediumAttachment)
|
---|
132 |
|
---|
133 | NS_DECL_CLASSINFO(Progress)
|
---|
134 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Progress, IProgress)
|
---|
135 |
|
---|
136 | NS_DECL_CLASSINFO(ProgressProxy)
|
---|
137 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(ProgressProxy, IProgress)
|
---|
138 |
|
---|
139 | NS_DECL_CLASSINFO(SharedFolder)
|
---|
140 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SharedFolder, ISharedFolder)
|
---|
141 |
|
---|
142 | NS_DECL_CLASSINFO(VRDEServer)
|
---|
143 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDEServer, IVRDEServer)
|
---|
144 |
|
---|
145 | NS_DECL_CLASSINFO(Host)
|
---|
146 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Host, IHost)
|
---|
147 |
|
---|
148 | NS_DECL_CLASSINFO(HostNetworkInterface)
|
---|
149 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostNetworkInterface, IHostNetworkInterface)
|
---|
150 |
|
---|
151 | NS_DECL_CLASSINFO(DHCPServer)
|
---|
152 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(DHCPServer, IDHCPServer)
|
---|
153 |
|
---|
154 | NS_DECL_CLASSINFO(NATNetwork)
|
---|
155 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(NATNetwork, INATNetwork)
|
---|
156 |
|
---|
157 | NS_DECL_CLASSINFO(GuestOSType)
|
---|
158 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(GuestOSType, IGuestOSType)
|
---|
159 |
|
---|
160 | NS_DECL_CLASSINFO(NetworkAdapter)
|
---|
161 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(NetworkAdapter, INetworkAdapter)
|
---|
162 |
|
---|
163 | NS_DECL_CLASSINFO(NATEngine)
|
---|
164 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(NATEngine, INATEngine)
|
---|
165 |
|
---|
166 |
|
---|
167 | NS_DECL_CLASSINFO(SerialPort)
|
---|
168 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SerialPort, ISerialPort)
|
---|
169 |
|
---|
170 | NS_DECL_CLASSINFO(ParallelPort)
|
---|
171 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(ParallelPort, IParallelPort)
|
---|
172 |
|
---|
173 | NS_DECL_CLASSINFO(USBController)
|
---|
174 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(USBController, IUSBController)
|
---|
175 |
|
---|
176 | NS_DECL_CLASSINFO(StorageController)
|
---|
177 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(StorageController, IStorageController)
|
---|
178 |
|
---|
179 | #ifdef VBOX_WITH_USB
|
---|
180 | NS_DECL_CLASSINFO(USBDeviceFilter)
|
---|
181 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(USBDeviceFilter, IUSBDeviceFilter)
|
---|
182 |
|
---|
183 | NS_DECL_CLASSINFO(HostUSBDevice)
|
---|
184 | NS_IMPL_THREADSAFE_ISUPPORTS2_CI(HostUSBDevice, IUSBDevice, IHostUSBDevice)
|
---|
185 |
|
---|
186 | NS_DECL_CLASSINFO(HostUSBDeviceFilter)
|
---|
187 | NS_IMPL_THREADSAFE_ISUPPORTS2_CI(HostUSBDeviceFilter, IUSBDeviceFilter, IHostUSBDeviceFilter)
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | NS_DECL_CLASSINFO(AudioAdapter)
|
---|
191 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(AudioAdapter, IAudioAdapter)
|
---|
192 |
|
---|
193 | NS_DECL_CLASSINFO(SystemProperties)
|
---|
194 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SystemProperties, ISystemProperties)
|
---|
195 |
|
---|
196 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
197 | NS_DECL_CLASSINFO(PerformanceCollector)
|
---|
198 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(PerformanceCollector, IPerformanceCollector)
|
---|
199 | NS_DECL_CLASSINFO(PerformanceMetric)
|
---|
200 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(PerformanceMetric, IPerformanceMetric)
|
---|
201 | #endif /* VBOX_WITH_RESOURCE_USAGE_API */
|
---|
202 |
|
---|
203 | NS_DECL_CLASSINFO(BIOSSettings)
|
---|
204 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(BIOSSettings, IBIOSSettings)
|
---|
205 |
|
---|
206 | #ifdef VBOX_WITH_EXTPACK
|
---|
207 | NS_DECL_CLASSINFO(ExtPackFile)
|
---|
208 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(ExtPackFile, IExtPackFile)
|
---|
209 |
|
---|
210 | NS_DECL_CLASSINFO(ExtPack)
|
---|
211 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(ExtPack, IExtPack)
|
---|
212 |
|
---|
213 | NS_DECL_CLASSINFO(ExtPackManager)
|
---|
214 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(ExtPackManager, IExtPackManager)
|
---|
215 | #endif
|
---|
216 |
|
---|
217 | NS_DECL_CLASSINFO(BandwidthGroup)
|
---|
218 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(BandwidthGroup, IBandwidthGroup)
|
---|
219 |
|
---|
220 | NS_DECL_CLASSINFO(BandwidthControl)
|
---|
221 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(BandwidthControl, IBandwidthControl)
|
---|
222 |
|
---|
223 | ////////////////////////////////////////////////////////////////////////////////
|
---|
224 |
|
---|
225 | static bool gAutoShutdown = false;
|
---|
226 | /** Delay before shutting down the VirtualBox server after the last
|
---|
227 | * VirtualBox instance is released, in ms */
|
---|
228 | static uint32_t gShutdownDelayMs = 5000;
|
---|
229 |
|
---|
230 | static nsIEventQueue *gEventQ = nsnull;
|
---|
231 | static PRBool volatile gKeepRunning = PR_TRUE;
|
---|
232 | static PRBool volatile gAllowSigUsrQuit = PR_TRUE;
|
---|
233 |
|
---|
234 | /////////////////////////////////////////////////////////////////////////////
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Simple but smart PLEvent wrapper.
|
---|
238 | *
|
---|
239 | * @note Instances must be always created with <tt>operator new</tt>!
|
---|
240 | */
|
---|
241 | class MyEvent
|
---|
242 | {
|
---|
243 | public:
|
---|
244 |
|
---|
245 | MyEvent()
|
---|
246 | {
|
---|
247 | mEv.that = NULL;
|
---|
248 | };
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Posts this event to the given message queue. This method may only be
|
---|
252 | * called once. @note On success, the event will be deleted automatically
|
---|
253 | * after it is delivered and handled. On failure, the event will delete
|
---|
254 | * itself before this method returns! The caller must not delete it in
|
---|
255 | * either case.
|
---|
256 | */
|
---|
257 | nsresult postTo(nsIEventQueue *aEventQ)
|
---|
258 | {
|
---|
259 | AssertReturn(mEv.that == NULL, NS_ERROR_FAILURE);
|
---|
260 | AssertReturn(aEventQ, NS_ERROR_FAILURE);
|
---|
261 | nsresult rv = aEventQ->InitEvent(&mEv.e, NULL,
|
---|
262 | eventHandler, eventDestructor);
|
---|
263 | if (NS_SUCCEEDED(rv))
|
---|
264 | {
|
---|
265 | mEv.that = this;
|
---|
266 | rv = aEventQ->PostEvent(&mEv.e);
|
---|
267 | if (NS_SUCCEEDED(rv))
|
---|
268 | return rv;
|
---|
269 | }
|
---|
270 | delete this;
|
---|
271 | return rv;
|
---|
272 | }
|
---|
273 |
|
---|
274 | virtual void *handler() = 0;
|
---|
275 |
|
---|
276 | private:
|
---|
277 |
|
---|
278 | struct Ev
|
---|
279 | {
|
---|
280 | PLEvent e;
|
---|
281 | MyEvent *that;
|
---|
282 | } mEv;
|
---|
283 |
|
---|
284 | static void *PR_CALLBACK eventHandler(PLEvent *self)
|
---|
285 | {
|
---|
286 | return reinterpret_cast<Ev *>(self)->that->handler();
|
---|
287 | }
|
---|
288 |
|
---|
289 | static void PR_CALLBACK eventDestructor(PLEvent *self)
|
---|
290 | {
|
---|
291 | delete reinterpret_cast<Ev *>(self)->that;
|
---|
292 | }
|
---|
293 | };
|
---|
294 |
|
---|
295 | ////////////////////////////////////////////////////////////////////////////////
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * VirtualBox class factory that destroys the created instance right after
|
---|
299 | * the last reference to it is released by the client, and recreates it again
|
---|
300 | * when necessary (so VirtualBox acts like a singleton object).
|
---|
301 | */
|
---|
302 | class VirtualBoxClassFactory : public VirtualBox
|
---|
303 | {
|
---|
304 | public:
|
---|
305 |
|
---|
306 | virtual ~VirtualBoxClassFactory()
|
---|
307 | {
|
---|
308 | LogFlowFunc(("Deleting VirtualBox...\n"));
|
---|
309 |
|
---|
310 | FinalRelease();
|
---|
311 | sInstance = NULL;
|
---|
312 |
|
---|
313 | LogFlowFunc(("VirtualBox object deleted.\n"));
|
---|
314 | RTPrintf("Informational: VirtualBox object deleted.\n");
|
---|
315 | }
|
---|
316 |
|
---|
317 | NS_IMETHOD_(nsrefcnt) Release()
|
---|
318 | {
|
---|
319 | /* we overload Release() to guarantee the VirtualBox destructor is
|
---|
320 | * always called on the main thread */
|
---|
321 |
|
---|
322 | nsrefcnt count = VirtualBox::Release();
|
---|
323 |
|
---|
324 | if (count == 1)
|
---|
325 | {
|
---|
326 | /* the last reference held by clients is being released
|
---|
327 | * (see GetInstance()) */
|
---|
328 |
|
---|
329 | PRBool onMainThread = PR_TRUE;
|
---|
330 | if (gEventQ)
|
---|
331 | gEventQ->IsOnCurrentThread(&onMainThread);
|
---|
332 |
|
---|
333 | PRBool timerStarted = PR_FALSE;
|
---|
334 |
|
---|
335 | /* sTimer is null if this call originates from FactoryDestructor()*/
|
---|
336 | if (sTimer != NULL)
|
---|
337 | {
|
---|
338 | LogFlowFunc(("Last VirtualBox instance was released.\n"));
|
---|
339 | LogFlowFunc(("Scheduling server shutdown in %u ms...\n",
|
---|
340 | gShutdownDelayMs));
|
---|
341 |
|
---|
342 | /* make sure the previous timer (if any) is stopped;
|
---|
343 | * otherwise RTTimerStart() will definitely fail. */
|
---|
344 | RTTimerLRStop(sTimer);
|
---|
345 |
|
---|
346 | int vrc = RTTimerLRStart(sTimer, gShutdownDelayMs * RT_NS_1MS_64);
|
---|
347 | AssertRC(vrc);
|
---|
348 | timerStarted = SUCCEEDED(vrc);
|
---|
349 | }
|
---|
350 | else
|
---|
351 | {
|
---|
352 | LogFlowFunc(("Last VirtualBox instance was released "
|
---|
353 | "on XPCOM shutdown.\n"));
|
---|
354 | Assert(onMainThread);
|
---|
355 | }
|
---|
356 |
|
---|
357 | gAllowSigUsrQuit = PR_TRUE;
|
---|
358 |
|
---|
359 | if (!timerStarted)
|
---|
360 | {
|
---|
361 | if (!onMainThread)
|
---|
362 | {
|
---|
363 | /* Failed to start the timer, post the shutdown event
|
---|
364 | * manually if not on the main thread already. */
|
---|
365 | ShutdownTimer(NULL, NULL, 0);
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | /* Here we come if:
|
---|
370 | *
|
---|
371 | * a) gEventQ is 0 which means either FactoryDestructor() is called
|
---|
372 | * or the IPC/DCONNECT shutdown sequence is initiated by the
|
---|
373 | * XPCOM shutdown routine (NS_ShutdownXPCOM()), which always
|
---|
374 | * happens on the main thread.
|
---|
375 | *
|
---|
376 | * b) gEventQ has reported we're on the main thread. This means
|
---|
377 | * that DestructEventHandler() has been called, but another
|
---|
378 | * client was faster and requested VirtualBox again.
|
---|
379 | *
|
---|
380 | * In either case, there is nothing to do.
|
---|
381 | *
|
---|
382 | * Note: case b) is actually no more valid since we don't
|
---|
383 | * call Release() from DestructEventHandler() in this case
|
---|
384 | * any more. Thus, we assert below.
|
---|
385 | */
|
---|
386 |
|
---|
387 | Assert(gEventQ == NULL);
|
---|
388 | }
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | return count;
|
---|
393 | }
|
---|
394 |
|
---|
395 | class MaybeQuitEvent : public MyEvent
|
---|
396 | {
|
---|
397 | /* called on the main thread */
|
---|
398 | void *handler()
|
---|
399 | {
|
---|
400 | LogFlowFuncEnter();
|
---|
401 |
|
---|
402 | Assert(RTCritSectIsInitialized(&sLock));
|
---|
403 |
|
---|
404 | /* stop accepting GetInstance() requests on other threads during
|
---|
405 | * possible destruction */
|
---|
406 | RTCritSectEnter(&sLock);
|
---|
407 |
|
---|
408 | nsrefcnt count = 0;
|
---|
409 |
|
---|
410 | /* sInstance is NULL here if it was deleted immediately after
|
---|
411 | * creation due to initialization error. See GetInstance(). */
|
---|
412 | if (sInstance != NULL)
|
---|
413 | {
|
---|
414 | /* Release the guard reference added in GetInstance() */
|
---|
415 | count = sInstance->Release();
|
---|
416 | }
|
---|
417 |
|
---|
418 | if (count == 0)
|
---|
419 | {
|
---|
420 | if (gAutoShutdown)
|
---|
421 | {
|
---|
422 | Assert(sInstance == NULL);
|
---|
423 | LogFlowFunc(("Terminating the server process...\n"));
|
---|
424 | /* make it leave the event loop */
|
---|
425 | gKeepRunning = PR_FALSE;
|
---|
426 | }
|
---|
427 | else
|
---|
428 | LogFlowFunc(("No automatic shutdown.\n"));
|
---|
429 | }
|
---|
430 | else
|
---|
431 | {
|
---|
432 | /* This condition is quite rare: a new client happened to
|
---|
433 | * connect after this event has been posted to the main queue
|
---|
434 | * but before it started to process it. */
|
---|
435 | LogFlowFunc(("Destruction is canceled (refcnt=%d).\n", count));
|
---|
436 | }
|
---|
437 |
|
---|
438 | RTCritSectLeave(&sLock);
|
---|
439 |
|
---|
440 | LogFlowFuncLeave();
|
---|
441 | return NULL;
|
---|
442 | }
|
---|
443 | };
|
---|
444 |
|
---|
445 | static void ShutdownTimer(RTTIMERLR hTimerLR, void *pvUser, uint64_t /*iTick*/)
|
---|
446 | {
|
---|
447 | NOREF(hTimerLR);
|
---|
448 | NOREF(pvUser);
|
---|
449 |
|
---|
450 | /* A "too late" event is theoretically possible if somebody
|
---|
451 | * manually ended the server after a destruction has been scheduled
|
---|
452 | * and this method was so lucky that it got a chance to run before
|
---|
453 | * the timer was killed. */
|
---|
454 | AssertReturnVoid(gEventQ);
|
---|
455 |
|
---|
456 | /* post a quit event to the main queue */
|
---|
457 | MaybeQuitEvent *ev = new MaybeQuitEvent();
|
---|
458 | nsresult rv = ev->postTo(gEventQ);
|
---|
459 | NOREF(rv);
|
---|
460 |
|
---|
461 | /* A failure above means we've been already stopped (for example
|
---|
462 | * by Ctrl-C). FactoryDestructor() (NS_ShutdownXPCOM())
|
---|
463 | * will do the job. Nothing to do. */
|
---|
464 | }
|
---|
465 |
|
---|
466 | static NS_IMETHODIMP FactoryConstructor()
|
---|
467 | {
|
---|
468 | LogFlowFunc(("\n"));
|
---|
469 |
|
---|
470 | /* create a critsect to protect object construction */
|
---|
471 | if (RT_FAILURE(RTCritSectInit(&sLock)))
|
---|
472 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
473 |
|
---|
474 | int vrc = RTTimerLRCreateEx(&sTimer, 0, 0, ShutdownTimer, NULL);
|
---|
475 | if (RT_FAILURE(vrc))
|
---|
476 | {
|
---|
477 | LogFlowFunc(("Failed to create a timer! (vrc=%Rrc)\n", vrc));
|
---|
478 | return NS_ERROR_FAILURE;
|
---|
479 | }
|
---|
480 |
|
---|
481 | return NS_OK;
|
---|
482 | }
|
---|
483 |
|
---|
484 | static NS_IMETHODIMP FactoryDestructor()
|
---|
485 | {
|
---|
486 | LogFlowFunc(("\n"));
|
---|
487 |
|
---|
488 | RTTimerLRDestroy(sTimer);
|
---|
489 | sTimer = NULL;
|
---|
490 |
|
---|
491 | RTCritSectDelete(&sLock);
|
---|
492 |
|
---|
493 | if (sInstance != NULL)
|
---|
494 | {
|
---|
495 | /* Either posting a destruction event failed for some reason (most
|
---|
496 | * likely, the quit event has been received before the last release),
|
---|
497 | * or the client has terminated abnormally w/o releasing its
|
---|
498 | * VirtualBox instance (so NS_ShutdownXPCOM() is doing a cleanup).
|
---|
499 | * Release the guard reference we added in GetInstance(). */
|
---|
500 | sInstance->Release();
|
---|
501 | }
|
---|
502 |
|
---|
503 | return NS_OK;
|
---|
504 | }
|
---|
505 |
|
---|
506 | static nsresult GetInstance(VirtualBox **inst)
|
---|
507 | {
|
---|
508 | LogFlowFunc(("Getting VirtualBox object...\n"));
|
---|
509 |
|
---|
510 | RTCritSectEnter(&sLock);
|
---|
511 |
|
---|
512 | if (!gKeepRunning)
|
---|
513 | {
|
---|
514 | LogFlowFunc(("Process termination requested first. Refusing.\n"));
|
---|
515 |
|
---|
516 | RTCritSectLeave(&sLock);
|
---|
517 |
|
---|
518 | /* this rv is what CreateInstance() on the client side returns
|
---|
519 | * when the server process stops accepting events. Do the same
|
---|
520 | * here. The client wrapper should attempt to start a new process in
|
---|
521 | * response to a failure from us. */
|
---|
522 | return NS_ERROR_ABORT;
|
---|
523 | }
|
---|
524 |
|
---|
525 | nsresult rv = NS_OK;
|
---|
526 |
|
---|
527 | if (sInstance == NULL)
|
---|
528 | {
|
---|
529 | LogFlowFunc (("Creating new VirtualBox object...\n"));
|
---|
530 | sInstance = new VirtualBoxClassFactory();
|
---|
531 | if (sInstance != NULL)
|
---|
532 | {
|
---|
533 | /* make an extra AddRef to take the full control
|
---|
534 | * on the VirtualBox destruction (see FinalRelease()) */
|
---|
535 | sInstance->AddRef();
|
---|
536 |
|
---|
537 | sInstance->AddRef(); /* protect FinalConstruct() */
|
---|
538 | rv = sInstance->FinalConstruct();
|
---|
539 | RTPrintf("Informational: VirtualBox object created (rc=%Rhrc).\n", rv);
|
---|
540 | if (NS_FAILED(rv))
|
---|
541 | {
|
---|
542 | /* On failure diring VirtualBox initialization, delete it
|
---|
543 | * immediately on the current thread by releasing all
|
---|
544 | * references in order to properly schedule the server
|
---|
545 | * shutdown. Since the object is fully deleted here, there
|
---|
546 | * is a chance to fix the error and request a new
|
---|
547 | * instantiation before the server terminates. However,
|
---|
548 | * the main reason to maintain the shutdown delay on
|
---|
549 | * failure is to let the front-end completely fetch error
|
---|
550 | * info from a server-side IVirtualBoxErrorInfo object. */
|
---|
551 | sInstance->Release();
|
---|
552 | sInstance->Release();
|
---|
553 | Assert(sInstance == NULL);
|
---|
554 | }
|
---|
555 | else
|
---|
556 | {
|
---|
557 | /* On success, make sure the previous timer is stopped to
|
---|
558 | * cancel a scheduled server termination (if any). */
|
---|
559 | gAllowSigUsrQuit = PR_FALSE;
|
---|
560 | RTTimerLRStop(sTimer);
|
---|
561 | }
|
---|
562 | }
|
---|
563 | else
|
---|
564 | {
|
---|
565 | rv = NS_ERROR_OUT_OF_MEMORY;
|
---|
566 | }
|
---|
567 | }
|
---|
568 | else
|
---|
569 | {
|
---|
570 | LogFlowFunc(("Using existing VirtualBox object...\n"));
|
---|
571 | nsrefcnt count = sInstance->AddRef();
|
---|
572 | Assert(count > 1);
|
---|
573 |
|
---|
574 | if (count == 2)
|
---|
575 | {
|
---|
576 | LogFlowFunc(("Another client has requested a reference to VirtualBox, canceling destruction...\n"));
|
---|
577 |
|
---|
578 | /* make sure the previous timer is stopped */
|
---|
579 | gAllowSigUsrQuit = PR_FALSE;
|
---|
580 | RTTimerLRStop(sTimer);
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | *inst = sInstance;
|
---|
585 |
|
---|
586 | RTCritSectLeave(&sLock);
|
---|
587 |
|
---|
588 | return rv;
|
---|
589 | }
|
---|
590 |
|
---|
591 | private:
|
---|
592 |
|
---|
593 | /* Don't be confused that sInstance is of the *ClassFactory type. This is
|
---|
594 | * actually a singleton instance (*ClassFactory inherits the singleton
|
---|
595 | * class; we combined them just for "simplicity" and used "static" for
|
---|
596 | * factory methods. *ClassFactory here is necessary for a couple of extra
|
---|
597 | * methods. */
|
---|
598 |
|
---|
599 | static VirtualBoxClassFactory *sInstance;
|
---|
600 | static RTCRITSECT sLock;
|
---|
601 |
|
---|
602 | static RTTIMERLR sTimer;
|
---|
603 | };
|
---|
604 |
|
---|
605 | VirtualBoxClassFactory *VirtualBoxClassFactory::sInstance = NULL;
|
---|
606 | RTCRITSECT VirtualBoxClassFactory::sLock;
|
---|
607 |
|
---|
608 | RTTIMERLR VirtualBoxClassFactory::sTimer = NIL_RTTIMERLR;
|
---|
609 |
|
---|
610 | NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR_WITH_RC(VirtualBox, VirtualBoxClassFactory::GetInstance)
|
---|
611 |
|
---|
612 | ////////////////////////////////////////////////////////////////////////////////
|
---|
613 |
|
---|
614 | typedef NSFactoryDestructorProcPtr NSFactoryConstructorProcPtr;
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Enhanced module component information structure.
|
---|
618 | *
|
---|
619 | * nsModuleComponentInfo lacks the factory construction callback, here we add
|
---|
620 | * it. This callback is called straight after a nsGenericFactory instance is
|
---|
621 | * successfully created in RegisterSelfComponents.
|
---|
622 | */
|
---|
623 | struct nsModuleComponentInfoPlusFactoryConstructor
|
---|
624 | {
|
---|
625 | /** standard module component information */
|
---|
626 | const nsModuleComponentInfo *mpModuleComponentInfo;
|
---|
627 | /** (optional) Factory Construction Callback */
|
---|
628 | NSFactoryConstructorProcPtr mFactoryConstructor;
|
---|
629 | };
|
---|
630 |
|
---|
631 | /////////////////////////////////////////////////////////////////////////////
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * Helper function to register self components upon start-up
|
---|
635 | * of the out-of-proc server.
|
---|
636 | */
|
---|
637 | static nsresult
|
---|
638 | RegisterSelfComponents(nsIComponentRegistrar *registrar,
|
---|
639 | const nsModuleComponentInfoPlusFactoryConstructor *aComponents,
|
---|
640 | PRUint32 count)
|
---|
641 | {
|
---|
642 | nsresult rc = NS_OK;
|
---|
643 | const nsModuleComponentInfoPlusFactoryConstructor *info = aComponents;
|
---|
644 | for (PRUint32 i = 0; i < count && NS_SUCCEEDED(rc); i++, info++)
|
---|
645 | {
|
---|
646 | /* skip components w/o a constructor */
|
---|
647 | if (!info->mpModuleComponentInfo->mConstructor)
|
---|
648 | continue;
|
---|
649 | /* create a new generic factory for a component and register it */
|
---|
650 | nsIGenericFactory *factory;
|
---|
651 | rc = NS_NewGenericFactory(&factory, info->mpModuleComponentInfo);
|
---|
652 | if (NS_SUCCEEDED(rc) && info->mFactoryConstructor)
|
---|
653 | {
|
---|
654 | rc = info->mFactoryConstructor();
|
---|
655 | if (NS_FAILED(rc))
|
---|
656 | NS_RELEASE(factory);
|
---|
657 | }
|
---|
658 | if (NS_SUCCEEDED(rc))
|
---|
659 | {
|
---|
660 | rc = registrar->RegisterFactory(info->mpModuleComponentInfo->mCID,
|
---|
661 | info->mpModuleComponentInfo->mDescription,
|
---|
662 | info->mpModuleComponentInfo->mContractID,
|
---|
663 | factory);
|
---|
664 | NS_RELEASE(factory);
|
---|
665 | }
|
---|
666 | }
|
---|
667 | return rc;
|
---|
668 | }
|
---|
669 |
|
---|
670 | /////////////////////////////////////////////////////////////////////////////
|
---|
671 |
|
---|
672 | static ipcIService *gIpcServ = nsnull;
|
---|
673 | static const char *g_pszPidFile = NULL;
|
---|
674 |
|
---|
675 | class ForceQuitEvent : public MyEvent
|
---|
676 | {
|
---|
677 | void *handler()
|
---|
678 | {
|
---|
679 | LogFlowFunc(("\n"));
|
---|
680 |
|
---|
681 | gKeepRunning = PR_FALSE;
|
---|
682 |
|
---|
683 | if (g_pszPidFile)
|
---|
684 | RTFileDelete(g_pszPidFile);
|
---|
685 |
|
---|
686 | return NULL;
|
---|
687 | }
|
---|
688 | };
|
---|
689 |
|
---|
690 | static void signal_handler(int sig)
|
---|
691 | {
|
---|
692 | if (gEventQ && gKeepRunning)
|
---|
693 | {
|
---|
694 | if (sig == SIGUSR1)
|
---|
695 | {
|
---|
696 | if (gAllowSigUsrQuit)
|
---|
697 | {
|
---|
698 | VirtualBoxClassFactory::MaybeQuitEvent *ev = new VirtualBoxClassFactory::MaybeQuitEvent();
|
---|
699 | ev->postTo(gEventQ);
|
---|
700 | }
|
---|
701 | /* else do nothing */
|
---|
702 | }
|
---|
703 | else
|
---|
704 | {
|
---|
705 | /* post a force quit event to the queue */
|
---|
706 | ForceQuitEvent *ev = new ForceQuitEvent();
|
---|
707 | ev->postTo(gEventQ);
|
---|
708 | }
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | static nsresult vboxsvcSpawnDaemonByReExec(const char *pszPath, bool fAutoShutdown, const char *pszPidFile)
|
---|
713 | {
|
---|
714 | PRFileDesc *readable = nsnull, *writable = nsnull;
|
---|
715 | PRProcessAttr *attr = nsnull;
|
---|
716 | nsresult rv = NS_ERROR_FAILURE;
|
---|
717 | PRFileDesc *devNull;
|
---|
718 | unsigned args_index = 0;
|
---|
719 | // The ugly casts are necessary because the PR_CreateProcessDetached has
|
---|
720 | // a const array of writable strings as a parameter. It won't write. */
|
---|
721 | char * args[1 + 1 + 2 + 1];
|
---|
722 | args[args_index++] = (char *)pszPath;
|
---|
723 | if (fAutoShutdown)
|
---|
724 | args[args_index++] = (char *)"--auto-shutdown";
|
---|
725 | if (pszPidFile)
|
---|
726 | {
|
---|
727 | args[args_index++] = (char *)"--pidfile";
|
---|
728 | args[args_index++] = (char *)pszPidFile;
|
---|
729 | }
|
---|
730 | args[args_index++] = 0;
|
---|
731 |
|
---|
732 | // Use a pipe to determine when the daemon process is in the position
|
---|
733 | // to actually process requests. The daemon will write "READY" to the pipe.
|
---|
734 | if (PR_CreatePipe(&readable, &writable) != PR_SUCCESS)
|
---|
735 | goto end;
|
---|
736 | PR_SetFDInheritable(writable, PR_TRUE);
|
---|
737 |
|
---|
738 | attr = PR_NewProcessAttr();
|
---|
739 | if (!attr)
|
---|
740 | goto end;
|
---|
741 |
|
---|
742 | if (PR_ProcessAttrSetInheritableFD(attr, writable, VBOXSVC_STARTUP_PIPE_NAME) != PR_SUCCESS)
|
---|
743 | goto end;
|
---|
744 |
|
---|
745 | devNull = PR_Open("/dev/null", PR_RDWR, 0);
|
---|
746 | if (!devNull)
|
---|
747 | goto end;
|
---|
748 |
|
---|
749 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, devNull);
|
---|
750 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, devNull);
|
---|
751 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardError, devNull);
|
---|
752 |
|
---|
753 | if (PR_CreateProcessDetached(pszPath, (char * const *)args, nsnull, attr) != PR_SUCCESS)
|
---|
754 | goto end;
|
---|
755 |
|
---|
756 | // Close /dev/null
|
---|
757 | PR_Close(devNull);
|
---|
758 | // Close the child end of the pipe to make it the only owner of the
|
---|
759 | // file descriptor, so that unexpected closing can be detected.
|
---|
760 | PR_Close(writable);
|
---|
761 | writable = nsnull;
|
---|
762 |
|
---|
763 | char msg[10];
|
---|
764 | memset(msg, '\0', sizeof(msg));
|
---|
765 | if ( PR_Read(readable, msg, sizeof(msg)-1) != 5
|
---|
766 | || strcmp(msg, "READY"))
|
---|
767 | goto end;
|
---|
768 |
|
---|
769 | rv = NS_OK;
|
---|
770 |
|
---|
771 | end:
|
---|
772 | if (readable)
|
---|
773 | PR_Close(readable);
|
---|
774 | if (writable)
|
---|
775 | PR_Close(writable);
|
---|
776 | if (attr)
|
---|
777 | PR_DestroyProcessAttr(attr);
|
---|
778 | return rv;
|
---|
779 | }
|
---|
780 |
|
---|
781 | int main(int argc, char **argv)
|
---|
782 | {
|
---|
783 | /*
|
---|
784 | * Initialize the VBox runtime without loading
|
---|
785 | * the support driver
|
---|
786 | */
|
---|
787 | int vrc = RTR3InitExe(argc, &argv, 0);
|
---|
788 | if (RT_FAILURE(vrc))
|
---|
789 | return RTMsgInitFailure(vrc);
|
---|
790 |
|
---|
791 | static const RTGETOPTDEF s_aOptions[] =
|
---|
792 | {
|
---|
793 | { "--automate", 'a', RTGETOPT_REQ_NOTHING },
|
---|
794 | { "--auto-shutdown", 'A', RTGETOPT_REQ_NOTHING },
|
---|
795 | { "--daemonize", 'd', RTGETOPT_REQ_NOTHING },
|
---|
796 | { "--shutdown-delay", 'D', RTGETOPT_REQ_UINT32 },
|
---|
797 | { "--pidfile", 'p', RTGETOPT_REQ_STRING },
|
---|
798 | { "--logfile", 'F', RTGETOPT_REQ_STRING },
|
---|
799 | { "--logrotate", 'R', RTGETOPT_REQ_UINT32 },
|
---|
800 | { "--logsize", 'S', RTGETOPT_REQ_UINT64 },
|
---|
801 | { "--loginterval", 'I', RTGETOPT_REQ_UINT32 }
|
---|
802 | };
|
---|
803 |
|
---|
804 | const char *pszLogFile = NULL;
|
---|
805 | uint32_t cHistory = 10; // enable log rotation, 10 files
|
---|
806 | uint32_t uHistoryFileTime = RT_SEC_1DAY; // max 1 day per file
|
---|
807 | uint64_t uHistoryFileSize = 100 * _1M; // max 100MB per file
|
---|
808 | bool fDaemonize = false;
|
---|
809 | PRFileDesc *daemon_pipe_wr = nsnull;
|
---|
810 |
|
---|
811 | RTGETOPTSTATE GetOptState;
|
---|
812 | vrc = RTGetOptInit(&GetOptState, argc, argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), 1, 0 /*fFlags*/);
|
---|
813 | AssertRC(vrc);
|
---|
814 |
|
---|
815 | RTGETOPTUNION ValueUnion;
|
---|
816 | while ((vrc = RTGetOpt(&GetOptState, &ValueUnion)))
|
---|
817 | {
|
---|
818 | switch (vrc)
|
---|
819 | {
|
---|
820 | case 'a':
|
---|
821 | /* --automate mode means we are started by XPCOM on
|
---|
822 | * demand. Daemonize ourselves and activate
|
---|
823 | * auto-shutdown. */
|
---|
824 | gAutoShutdown = true;
|
---|
825 | fDaemonize = true;
|
---|
826 | break;
|
---|
827 |
|
---|
828 | case 'A':
|
---|
829 | /* --auto-shutdown mode means we're already daemonized. */
|
---|
830 | gAutoShutdown = true;
|
---|
831 | break;
|
---|
832 |
|
---|
833 | case 'd':
|
---|
834 | fDaemonize = true;
|
---|
835 | break;
|
---|
836 |
|
---|
837 | case 'D':
|
---|
838 | gShutdownDelayMs = ValueUnion.u32;
|
---|
839 | break;
|
---|
840 |
|
---|
841 | case 'p':
|
---|
842 | g_pszPidFile = ValueUnion.psz;
|
---|
843 | break;
|
---|
844 |
|
---|
845 | case 'F':
|
---|
846 | pszLogFile = ValueUnion.psz;
|
---|
847 | break;
|
---|
848 |
|
---|
849 | case 'R':
|
---|
850 | cHistory = ValueUnion.u32;
|
---|
851 | break;
|
---|
852 |
|
---|
853 | case 'S':
|
---|
854 | uHistoryFileSize = ValueUnion.u64;
|
---|
855 | break;
|
---|
856 |
|
---|
857 | case 'I':
|
---|
858 | uHistoryFileTime = ValueUnion.u32;
|
---|
859 | break;
|
---|
860 |
|
---|
861 | case 'h':
|
---|
862 | RTPrintf("no help\n");
|
---|
863 | return 1;
|
---|
864 |
|
---|
865 | case 'V':
|
---|
866 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
867 | return 0;
|
---|
868 |
|
---|
869 | default:
|
---|
870 | return RTGetOptPrintError(vrc, &ValueUnion);
|
---|
871 | }
|
---|
872 | }
|
---|
873 |
|
---|
874 | if (fDaemonize)
|
---|
875 | {
|
---|
876 | vboxsvcSpawnDaemonByReExec(argv[0], gAutoShutdown, g_pszPidFile);
|
---|
877 | exit(126);
|
---|
878 | }
|
---|
879 |
|
---|
880 | nsresult rc;
|
---|
881 |
|
---|
882 | /** @todo Merge this code with svcmain.cpp (use Logging.cpp?). */
|
---|
883 | char szLogFile[RTPATH_MAX];
|
---|
884 | if (!pszLogFile)
|
---|
885 | {
|
---|
886 | vrc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile));
|
---|
887 | if (RT_SUCCESS(vrc))
|
---|
888 | vrc = RTPathAppend(szLogFile, sizeof(szLogFile), "VBoxSVC.log");
|
---|
889 | }
|
---|
890 | else
|
---|
891 | {
|
---|
892 | if (!RTStrPrintf(szLogFile, sizeof(szLogFile), "%s", pszLogFile))
|
---|
893 | vrc = VERR_NO_MEMORY;
|
---|
894 | }
|
---|
895 | if (RT_FAILURE(vrc))
|
---|
896 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to create logging file name, rc=%Rrc", vrc);
|
---|
897 |
|
---|
898 | char szError[RTPATH_MAX + 128];
|
---|
899 | vrc = com::VBoxLogRelCreate("XPCOM Server", szLogFile,
|
---|
900 | RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
|
---|
901 | "all", "VBOXSVC_RELEASE_LOG",
|
---|
902 | RTLOGDEST_FILE, UINT32_MAX /* cMaxEntriesPerGroup */,
|
---|
903 | cHistory, uHistoryFileTime, uHistoryFileSize,
|
---|
904 | szError, sizeof(szError));
|
---|
905 | if (RT_FAILURE(vrc))
|
---|
906 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, vrc);
|
---|
907 |
|
---|
908 | daemon_pipe_wr = PR_GetInheritedFD(VBOXSVC_STARTUP_PIPE_NAME);
|
---|
909 | RTEnvUnset("NSPR_INHERIT_FDS");
|
---|
910 |
|
---|
911 | const nsModuleComponentInfo VirtualBoxInfo = {
|
---|
912 | "VirtualBox component",
|
---|
913 | NS_VIRTUALBOX_CID,
|
---|
914 | NS_VIRTUALBOX_CONTRACTID,
|
---|
915 | VirtualBoxConstructor, // constructor function
|
---|
916 | NULL, // registration function
|
---|
917 | NULL, // deregistration function
|
---|
918 | VirtualBoxClassFactory::FactoryDestructor, // factory destructor function
|
---|
919 | NS_CI_INTERFACE_GETTER_NAME(VirtualBox),
|
---|
920 | NULL, // language helper
|
---|
921 | &NS_CLASSINFO_NAME(VirtualBox),
|
---|
922 | 0 // flags
|
---|
923 | };
|
---|
924 |
|
---|
925 | const nsModuleComponentInfoPlusFactoryConstructor components[] = {
|
---|
926 | {
|
---|
927 | &VirtualBoxInfo,
|
---|
928 | VirtualBoxClassFactory::FactoryConstructor // factory constructor function
|
---|
929 | }
|
---|
930 | };
|
---|
931 |
|
---|
932 | do
|
---|
933 | {
|
---|
934 | rc = com::Initialize();
|
---|
935 | if (NS_FAILED(rc))
|
---|
936 | {
|
---|
937 | RTMsgError("Failed to initialize XPCOM! (rc=%Rhrc)\n", rc);
|
---|
938 | break;
|
---|
939 | }
|
---|
940 |
|
---|
941 | nsCOMPtr <nsIComponentRegistrar> registrar;
|
---|
942 | rc = NS_GetComponentRegistrar(getter_AddRefs(registrar));
|
---|
943 | if (NS_FAILED(rc))
|
---|
944 | {
|
---|
945 | RTMsgError("Failed to get component registrar! (rc=%Rhrc)", rc);
|
---|
946 | break;
|
---|
947 | }
|
---|
948 |
|
---|
949 | registrar->AutoRegister(nsnull);
|
---|
950 | rc = RegisterSelfComponents(registrar, components,
|
---|
951 | NS_ARRAY_LENGTH(components));
|
---|
952 | if (NS_FAILED(rc))
|
---|
953 | {
|
---|
954 | RTMsgError("Failed to register server components! (rc=%Rhrc)", rc);
|
---|
955 | break;
|
---|
956 | }
|
---|
957 |
|
---|
958 | /* get the main thread's event queue (afaik, the dconnect service always
|
---|
959 | * gets created upon XPCOM startup, so it will use the main (this)
|
---|
960 | * thread's event queue to receive IPC events) */
|
---|
961 | rc = NS_GetMainEventQ(&gEventQ);
|
---|
962 | if (NS_FAILED(rc))
|
---|
963 | {
|
---|
964 | RTMsgError("Failed to get the main event queue! (rc=%Rhrc)", rc);
|
---|
965 | break;
|
---|
966 | }
|
---|
967 |
|
---|
968 | nsCOMPtr<ipcIService> ipcServ (do_GetService(IPC_SERVICE_CONTRACTID, &rc));
|
---|
969 | if (NS_FAILED(rc))
|
---|
970 | {
|
---|
971 | RTMsgError("Failed to get IPC service! (rc=%Rhrc)", rc);
|
---|
972 | break;
|
---|
973 | }
|
---|
974 |
|
---|
975 | NS_ADDREF(gIpcServ = ipcServ);
|
---|
976 |
|
---|
977 | LogFlowFunc(("Will use \"%s\" as server name.\n", VBOXSVC_IPC_NAME));
|
---|
978 |
|
---|
979 | rc = gIpcServ->AddName(VBOXSVC_IPC_NAME);
|
---|
980 | if (NS_FAILED(rc))
|
---|
981 | {
|
---|
982 | LogFlowFunc(("Failed to register the server name (rc=%Rhrc (%08X))!\n"
|
---|
983 | "Is another server already running?\n", rc, rc));
|
---|
984 |
|
---|
985 | RTMsgError("Failed to register the server name \"%s\" (rc=%Rhrc)!\n"
|
---|
986 | "Is another server already running?\n",
|
---|
987 | VBOXSVC_IPC_NAME, rc);
|
---|
988 | NS_RELEASE(gIpcServ);
|
---|
989 | break;
|
---|
990 | }
|
---|
991 |
|
---|
992 | {
|
---|
993 | /* setup signal handling to convert some signals to a quit event */
|
---|
994 | struct sigaction sa;
|
---|
995 | sa.sa_handler = signal_handler;
|
---|
996 | sigemptyset(&sa.sa_mask);
|
---|
997 | sa.sa_flags = 0;
|
---|
998 | sigaction(SIGINT, &sa, NULL);
|
---|
999 | sigaction(SIGQUIT, &sa, NULL);
|
---|
1000 | sigaction(SIGTERM, &sa, NULL);
|
---|
1001 | sigaction(SIGTRAP, &sa, NULL);
|
---|
1002 | sigaction(SIGUSR1, &sa, NULL);
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | {
|
---|
1006 | char szBuf[80];
|
---|
1007 | int iSize;
|
---|
1008 |
|
---|
1009 | iSize = RTStrPrintf(szBuf, sizeof(szBuf),
|
---|
1010 | VBOX_PRODUCT" XPCOM Server Version "
|
---|
1011 | VBOX_VERSION_STRING);
|
---|
1012 | for (int i = iSize; i > 0; i--)
|
---|
1013 | putchar('*');
|
---|
1014 | RTPrintf("\n%s\n", szBuf);
|
---|
1015 | RTPrintf("(C) 2004-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
1016 | "All rights reserved.\n");
|
---|
1017 | #ifdef DEBUG
|
---|
1018 | RTPrintf("Debug version.\n");
|
---|
1019 | #endif
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | if (daemon_pipe_wr != nsnull)
|
---|
1023 | {
|
---|
1024 | RTPrintf("\nStarting event loop....\n[send TERM signal to quit]\n");
|
---|
1025 | /* now we're ready, signal the parent process */
|
---|
1026 | PR_Write(daemon_pipe_wr, "READY", strlen("READY"));
|
---|
1027 | /* close writing end of the pipe, its job is done */
|
---|
1028 | PR_Close(daemon_pipe_wr);
|
---|
1029 | }
|
---|
1030 | else
|
---|
1031 | RTPrintf("\nStarting event loop....\n[press Ctrl-C to quit]\n");
|
---|
1032 |
|
---|
1033 | if (g_pszPidFile)
|
---|
1034 | {
|
---|
1035 | RTFILE hPidFile = NIL_RTFILE;
|
---|
1036 | vrc = RTFileOpen(&hPidFile, g_pszPidFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
|
---|
1037 | if (RT_SUCCESS(vrc))
|
---|
1038 | {
|
---|
1039 | char szBuf[32];
|
---|
1040 | const char *lf = "\n";
|
---|
1041 | RTStrFormatNumber(szBuf, getpid(), 10, 0, 0, 0);
|
---|
1042 | RTFileWrite(hPidFile, szBuf, strlen(szBuf), NULL);
|
---|
1043 | RTFileWrite(hPidFile, lf, strlen(lf), NULL);
|
---|
1044 | RTFileClose(hPidFile);
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | // Increase the file table size to 10240 or as high as possible.
|
---|
1049 | struct rlimit lim;
|
---|
1050 | if (getrlimit(RLIMIT_NOFILE, &lim) == 0)
|
---|
1051 | {
|
---|
1052 | if ( lim.rlim_cur < 10240
|
---|
1053 | && lim.rlim_cur < lim.rlim_max)
|
---|
1054 | {
|
---|
1055 | lim.rlim_cur = RT_MIN(lim.rlim_max, 10240);
|
---|
1056 | if (setrlimit(RLIMIT_NOFILE, &lim) == -1)
|
---|
1057 | RTPrintf("WARNING: failed to increase file descriptor limit. (%d)\n", errno);
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | else
|
---|
1061 | RTPrintf("WARNING: failed to obtain per-process file-descriptor limit (%d).\n", errno);
|
---|
1062 |
|
---|
1063 | PLEvent *ev;
|
---|
1064 | while (gKeepRunning)
|
---|
1065 | {
|
---|
1066 | gEventQ->WaitForEvent(&ev);
|
---|
1067 | gEventQ->HandleEvent(ev);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | /* stop accepting new events. Clients that happen to resolve our
|
---|
1071 | * name and issue a CreateInstance() request after this point will
|
---|
1072 | * get NS_ERROR_ABORT once we handle the remaining messages. As a
|
---|
1073 | * result, they should try to start a new server process. */
|
---|
1074 | gEventQ->StopAcceptingEvents();
|
---|
1075 |
|
---|
1076 | /* unregister ourselves. After this point, clients will start a new
|
---|
1077 | * process because they won't be able to resolve the server name.*/
|
---|
1078 | gIpcServ->RemoveName(VBOXSVC_IPC_NAME);
|
---|
1079 |
|
---|
1080 | /* process any remaining events. These events may include
|
---|
1081 | * CreateInstance() requests received right before we called
|
---|
1082 | * StopAcceptingEvents() above. We will detect this case below,
|
---|
1083 | * restore gKeepRunning and continue to serve. */
|
---|
1084 | gEventQ->ProcessPendingEvents();
|
---|
1085 |
|
---|
1086 | RTPrintf("Terminated event loop.\n");
|
---|
1087 | }
|
---|
1088 | while (0); // this scopes the nsCOMPtrs
|
---|
1089 |
|
---|
1090 | NS_IF_RELEASE(gIpcServ);
|
---|
1091 | NS_IF_RELEASE(gEventQ);
|
---|
1092 |
|
---|
1093 | /* no nsCOMPtrs are allowed to be alive when you call com::Shutdown(). */
|
---|
1094 |
|
---|
1095 | LogFlowFunc(("Calling com::Shutdown()...\n"));
|
---|
1096 | rc = com::Shutdown();
|
---|
1097 | LogFlowFunc(("Finished com::Shutdown() (rc=%Rhrc)\n", rc));
|
---|
1098 |
|
---|
1099 | if (NS_FAILED(rc))
|
---|
1100 | RTMsgError("Failed to shutdown XPCOM! (rc=%Rhrc)", rc);
|
---|
1101 |
|
---|
1102 | RTPrintf("XPCOM server has shutdown.\n");
|
---|
1103 |
|
---|
1104 | if (g_pszPidFile)
|
---|
1105 | RTFileDelete(g_pszPidFile);
|
---|
1106 |
|
---|
1107 | return 0;
|
---|
1108 | }
|
---|