1 | /* $Id: server.cpp 16966 2009-02-20 09:14:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * XPCOM server process (VBoxSVC) start point.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /* Make sure all the stdint.h macros are included - must come first! */
|
---|
23 | #ifndef __STDC_LIMIT_MACROS
|
---|
24 | # define __STDC_LIMIT_MACROS
|
---|
25 | #endif
|
---|
26 | #ifndef __STDC_CONSTANT_MACROS
|
---|
27 | # define __STDC_CONSTANT_MACROS
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include <ipcIService.h>
|
---|
31 | #include <ipcCID.h>
|
---|
32 |
|
---|
33 | #include <nsIComponentRegistrar.h>
|
---|
34 |
|
---|
35 | #if defined(XPCOM_GLUE)
|
---|
36 | #include <nsXPCOMGlue.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include <nsEventQueueUtils.h>
|
---|
40 | #include <nsGenericFactory.h>
|
---|
41 |
|
---|
42 | #include "xpcom/server.h"
|
---|
43 |
|
---|
44 | #include "Logging.h"
|
---|
45 |
|
---|
46 | #include <VBox/param.h>
|
---|
47 | #include <VBox/version.h>
|
---|
48 |
|
---|
49 | #include <iprt/initterm.h>
|
---|
50 | #include <iprt/path.h>
|
---|
51 | #include <iprt/critsect.h>
|
---|
52 | #include <iprt/timer.h>
|
---|
53 |
|
---|
54 | #include <stdio.h>
|
---|
55 |
|
---|
56 | // for the signal handler
|
---|
57 | #include <signal.h>
|
---|
58 | #include <stdlib.h>
|
---|
59 | #include <unistd.h>
|
---|
60 | #include <errno.h>
|
---|
61 | #include <getopt.h>
|
---|
62 |
|
---|
63 | #ifndef RT_OS_OS2
|
---|
64 | # include <sys/resource.h>
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | // for the backtrace signal handler
|
---|
68 | #if defined(DEBUG) && defined(RT_OS_LINUX)
|
---|
69 | # define USE_BACKTRACE
|
---|
70 | #endif
|
---|
71 | #if defined(USE_BACKTRACE)
|
---|
72 | # include <execinfo.h>
|
---|
73 | // get REG_EIP/RIP from ucontext.h
|
---|
74 | # ifndef __USE_GNU
|
---|
75 | # define __USE_GNU
|
---|
76 | # endif
|
---|
77 | # include <ucontext.h>
|
---|
78 | # ifdef RT_ARCH_AMD64
|
---|
79 | # define REG_PC REG_RIP
|
---|
80 | # else
|
---|
81 | # define REG_PC REG_EIP
|
---|
82 | # endif
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | /////////////////////////////////////////////////////////////////////////////
|
---|
86 | // VirtualBox component instantiation
|
---|
87 | /////////////////////////////////////////////////////////////////////////////
|
---|
88 |
|
---|
89 | #include <nsIGenericFactory.h>
|
---|
90 |
|
---|
91 | #include <VirtualBox_XPCOM.h>
|
---|
92 | #include <VirtualBoxImpl.h>
|
---|
93 | #include <MachineImpl.h>
|
---|
94 | #include <ApplianceImpl.h>
|
---|
95 | #include <SnapshotImpl.h>
|
---|
96 | #include <MediumImpl.h>
|
---|
97 | #include <HardDiskImpl.h>
|
---|
98 | #include <HardDiskFormatImpl.h>
|
---|
99 | #include <ProgressImpl.h>
|
---|
100 | #include <DVDDriveImpl.h>
|
---|
101 | #include <FloppyDriveImpl.h>
|
---|
102 | #include <VRDPServerImpl.h>
|
---|
103 | #include <SharedFolderImpl.h>
|
---|
104 | #include <HostImpl.h>
|
---|
105 | #include <HostDVDDriveImpl.h>
|
---|
106 | #include <HostFloppyDriveImpl.h>
|
---|
107 | #include <HostNetworkInterfaceImpl.h>
|
---|
108 | #include <GuestOSTypeImpl.h>
|
---|
109 | #include <NetworkAdapterImpl.h>
|
---|
110 | #include <SerialPortImpl.h>
|
---|
111 | #include <ParallelPortImpl.h>
|
---|
112 | #include <USBControllerImpl.h>
|
---|
113 | #ifdef VBOX_WITH_USB
|
---|
114 | # include <HostUSBDeviceImpl.h>
|
---|
115 | # include <USBDeviceImpl.h>
|
---|
116 | #endif
|
---|
117 | #include <SATAControllerImpl.h>
|
---|
118 | #include <AudioAdapterImpl.h>
|
---|
119 | #include <SystemPropertiesImpl.h>
|
---|
120 | #include <Collection.h>
|
---|
121 |
|
---|
122 | /* implement nsISupports parts of our objects with support for nsIClassInfo */
|
---|
123 |
|
---|
124 | NS_DECL_CLASSINFO(VirtualBox)
|
---|
125 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VirtualBox, IVirtualBox)
|
---|
126 |
|
---|
127 | NS_DECL_CLASSINFO(Machine)
|
---|
128 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Machine, IMachine)
|
---|
129 |
|
---|
130 | NS_DECL_CLASSINFO(Appliance)
|
---|
131 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Appliance, IAppliance)
|
---|
132 |
|
---|
133 | NS_DECL_CLASSINFO(VirtualSystemDescription)
|
---|
134 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VirtualSystemDescription, IVirtualSystemDescription)
|
---|
135 |
|
---|
136 | NS_DECL_CLASSINFO(SessionMachine)
|
---|
137 | NS_IMPL_THREADSAFE_ISUPPORTS2_CI(SessionMachine, IMachine, IInternalMachineControl)
|
---|
138 |
|
---|
139 | NS_DECL_CLASSINFO(SnapshotMachine)
|
---|
140 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SnapshotMachine, IMachine)
|
---|
141 |
|
---|
142 | NS_DECL_CLASSINFO(Snapshot)
|
---|
143 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Snapshot, ISnapshot)
|
---|
144 |
|
---|
145 | NS_DECL_CLASSINFO(DVDImage)
|
---|
146 | NS_IMPL_THREADSAFE_ISUPPORTS2_AMBIGUOUS_CI(DVDImage,
|
---|
147 | IMedium, ImageMediumBase,
|
---|
148 | IDVDImage, DVDImage)
|
---|
149 | NS_DECL_CLASSINFO(FloppyImage)
|
---|
150 | NS_IMPL_THREADSAFE_ISUPPORTS2_AMBIGUOUS_CI(FloppyImage,
|
---|
151 | IMedium, ImageMediumBase,
|
---|
152 | IFloppyImage, FloppyImage)
|
---|
153 |
|
---|
154 | NS_DECL_CLASSINFO(HardDisk)
|
---|
155 | NS_IMPL_THREADSAFE_ISUPPORTS2_AMBIGUOUS_CI(HardDisk,
|
---|
156 | IMedium, MediumBase,
|
---|
157 | IHardDisk, HardDisk)
|
---|
158 |
|
---|
159 | NS_DECL_CLASSINFO(HardDiskFormat)
|
---|
160 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(HardDiskFormat, IHardDiskFormat)
|
---|
161 |
|
---|
162 | NS_DECL_CLASSINFO(HardDiskAttachment)
|
---|
163 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(HardDiskAttachment, IHardDiskAttachment)
|
---|
164 |
|
---|
165 | NS_DECL_CLASSINFO(Progress)
|
---|
166 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Progress, IProgress)
|
---|
167 |
|
---|
168 | NS_DECL_CLASSINFO(CombinedProgress)
|
---|
169 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(CombinedProgress, IProgress)
|
---|
170 |
|
---|
171 | NS_DECL_CLASSINFO(DVDDrive)
|
---|
172 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(DVDDrive, IDVDDrive)
|
---|
173 |
|
---|
174 | NS_DECL_CLASSINFO(FloppyDrive)
|
---|
175 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(FloppyDrive, IFloppyDrive)
|
---|
176 |
|
---|
177 | NS_DECL_CLASSINFO(SharedFolder)
|
---|
178 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SharedFolder, ISharedFolder)
|
---|
179 |
|
---|
180 | #ifdef VBOX_WITH_VRDP
|
---|
181 | NS_DECL_CLASSINFO(VRDPServer)
|
---|
182 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDPServer, IVRDPServer)
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | NS_DECL_CLASSINFO(Host)
|
---|
186 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Host, IHost)
|
---|
187 |
|
---|
188 | NS_DECL_CLASSINFO(HostDVDDrive)
|
---|
189 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostDVDDrive, IHostDVDDrive)
|
---|
190 |
|
---|
191 | NS_DECL_CLASSINFO(HostFloppyDrive)
|
---|
192 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostFloppyDrive, IHostFloppyDrive)
|
---|
193 |
|
---|
194 | NS_DECL_CLASSINFO(HostNetworkInterface)
|
---|
195 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostNetworkInterface, IHostNetworkInterface)
|
---|
196 |
|
---|
197 | NS_DECL_CLASSINFO(GuestOSType)
|
---|
198 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(GuestOSType, IGuestOSType)
|
---|
199 |
|
---|
200 | NS_DECL_CLASSINFO(NetworkAdapter)
|
---|
201 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(NetworkAdapter, INetworkAdapter)
|
---|
202 |
|
---|
203 | NS_DECL_CLASSINFO(SerialPort)
|
---|
204 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SerialPort, ISerialPort)
|
---|
205 |
|
---|
206 | NS_DECL_CLASSINFO(ParallelPort)
|
---|
207 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(ParallelPort, IParallelPort)
|
---|
208 |
|
---|
209 | NS_DECL_CLASSINFO(USBController)
|
---|
210 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(USBController, IUSBController)
|
---|
211 |
|
---|
212 | NS_DECL_CLASSINFO(SATAController)
|
---|
213 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SATAController, ISATAController)
|
---|
214 |
|
---|
215 | #ifdef VBOX_WITH_USB
|
---|
216 | NS_DECL_CLASSINFO(USBDeviceFilter)
|
---|
217 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(USBDeviceFilter, IUSBDeviceFilter)
|
---|
218 |
|
---|
219 | NS_DECL_CLASSINFO(HostUSBDevice)
|
---|
220 | NS_IMPL_THREADSAFE_ISUPPORTS2_CI(HostUSBDevice, IUSBDevice, IHostUSBDevice)
|
---|
221 |
|
---|
222 | NS_DECL_CLASSINFO(HostUSBDeviceFilter)
|
---|
223 | NS_IMPL_THREADSAFE_ISUPPORTS2_CI(HostUSBDeviceFilter, IUSBDeviceFilter, IHostUSBDeviceFilter)
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | NS_DECL_CLASSINFO(AudioAdapter)
|
---|
227 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(AudioAdapter, IAudioAdapter)
|
---|
228 |
|
---|
229 | NS_DECL_CLASSINFO(SystemProperties)
|
---|
230 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SystemProperties, ISystemProperties)
|
---|
231 |
|
---|
232 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
233 | NS_DECL_CLASSINFO(PerformanceCollector)
|
---|
234 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(PerformanceCollector, IPerformanceCollector)
|
---|
235 | NS_DECL_CLASSINFO(PerformanceMetric)
|
---|
236 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(PerformanceMetric, IPerformanceMetric)
|
---|
237 | #endif /* VBOX_WITH_RESOURCE_USAGE_API */
|
---|
238 |
|
---|
239 | NS_DECL_CLASSINFO(BIOSSettings)
|
---|
240 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(BIOSSettings, IBIOSSettings)
|
---|
241 |
|
---|
242 | /* collections and enumerators */
|
---|
243 |
|
---|
244 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(Snapshot)
|
---|
245 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(GuestOSType)
|
---|
246 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(HostDVDDrive)
|
---|
247 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(HostFloppyDrive)
|
---|
248 | #ifdef VBOX_WITH_USB
|
---|
249 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(HostUSBDevice)
|
---|
250 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(HostUSBDeviceFilter)
|
---|
251 | COM_IMPL_READONLY_ENUM_AND_COLLECTION(USBDeviceFilter)
|
---|
252 | #endif
|
---|
253 |
|
---|
254 | #ifdef VBOX_WITH_USB
|
---|
255 | COM_IMPL_READONLY_ENUM_AND_COLLECTION_AS(IfaceUSBDevice, IUSBDevice)
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | ////////////////////////////////////////////////////////////////////////////////
|
---|
259 |
|
---|
260 | enum
|
---|
261 | {
|
---|
262 | /* Delay before shutting down the VirtualBox server after the last
|
---|
263 | * VirtualBox instance is released, in ms */
|
---|
264 | VBoxSVC_ShutdownDelay = 5000,
|
---|
265 | };
|
---|
266 |
|
---|
267 | static bool gAutoShutdown = false;
|
---|
268 |
|
---|
269 | static nsIEventQueue* gEventQ = nsnull;
|
---|
270 | static PRBool volatile gKeepRunning = PR_TRUE;
|
---|
271 |
|
---|
272 | /////////////////////////////////////////////////////////////////////////////
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Simple but smart PLEvent wrapper.
|
---|
276 | *
|
---|
277 | * @note Instances must be always created with <tt>operator new</tt>!
|
---|
278 | */
|
---|
279 | class MyEvent
|
---|
280 | {
|
---|
281 | public:
|
---|
282 |
|
---|
283 | MyEvent()
|
---|
284 | {
|
---|
285 | mEv.that = NULL;
|
---|
286 | };
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Posts this event to the given message queue. This method may only be
|
---|
290 | * called once. @note On success, the event will be deleted automatically
|
---|
291 | * after it is delivered and handled. On failure, the event will delete
|
---|
292 | * itself before this method returns! The caller must not delete it in
|
---|
293 | * either case.
|
---|
294 | */
|
---|
295 | nsresult postTo (nsIEventQueue *aEventQ)
|
---|
296 | {
|
---|
297 | AssertReturn (mEv.that == NULL, NS_ERROR_FAILURE);
|
---|
298 | AssertReturn (aEventQ, NS_ERROR_FAILURE);
|
---|
299 | nsresult rv = aEventQ->InitEvent (&mEv.e, NULL,
|
---|
300 | eventHandler, eventDestructor);
|
---|
301 | if (NS_SUCCEEDED (rv))
|
---|
302 | {
|
---|
303 | mEv.that = this;
|
---|
304 | rv = aEventQ->PostEvent (&mEv.e);
|
---|
305 | if (NS_SUCCEEDED (rv))
|
---|
306 | return rv;
|
---|
307 | }
|
---|
308 | delete this;
|
---|
309 | return rv;
|
---|
310 | }
|
---|
311 |
|
---|
312 | virtual void *handler() = 0;
|
---|
313 |
|
---|
314 | private:
|
---|
315 |
|
---|
316 | struct Ev
|
---|
317 | {
|
---|
318 | PLEvent e;
|
---|
319 | MyEvent *that;
|
---|
320 | } mEv;
|
---|
321 |
|
---|
322 | static void *PR_CALLBACK eventHandler (PLEvent *self)
|
---|
323 | {
|
---|
324 | return reinterpret_cast <Ev *> (self)->that->handler();
|
---|
325 | }
|
---|
326 |
|
---|
327 | static void PR_CALLBACK eventDestructor (PLEvent *self)
|
---|
328 | {
|
---|
329 | delete reinterpret_cast <Ev *> (self)->that;
|
---|
330 | }
|
---|
331 | };
|
---|
332 |
|
---|
333 | ////////////////////////////////////////////////////////////////////////////////
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * VirtualBox class factory that destroys the created instance right after
|
---|
337 | * the last reference to it is released by the client, and recreates it again
|
---|
338 | * when necessary (so VirtualBox acts like a singleton object).
|
---|
339 | */
|
---|
340 | class VirtualBoxClassFactory : public VirtualBox
|
---|
341 | {
|
---|
342 | public:
|
---|
343 |
|
---|
344 | virtual ~VirtualBoxClassFactory()
|
---|
345 | {
|
---|
346 | LogFlowFunc (("Deleting VirtualBox...\n"));
|
---|
347 |
|
---|
348 | FinalRelease();
|
---|
349 | sInstance = NULL;
|
---|
350 |
|
---|
351 | LogFlowFunc (("VirtualBox object deleted.\n"));
|
---|
352 | printf ("Informational: VirtualBox object deleted.\n");
|
---|
353 | }
|
---|
354 |
|
---|
355 | NS_IMETHOD_(nsrefcnt) Release()
|
---|
356 | {
|
---|
357 | /* we overload Release() to guarantee the VirtualBox destructor is
|
---|
358 | * always called on the main thread */
|
---|
359 |
|
---|
360 | nsrefcnt count = VirtualBox::Release();
|
---|
361 |
|
---|
362 | if (count == 1)
|
---|
363 | {
|
---|
364 | /* the last reference held by clients is being released
|
---|
365 | * (see GetInstance()) */
|
---|
366 |
|
---|
367 | PRBool onMainThread = PR_TRUE;
|
---|
368 | if (gEventQ)
|
---|
369 | gEventQ->IsOnCurrentThread (&onMainThread);
|
---|
370 |
|
---|
371 | PRBool timerStarted = PR_FALSE;
|
---|
372 |
|
---|
373 | /* sTimer is null if this call originates from FactoryDestructor()*/
|
---|
374 | if (sTimer != NULL)
|
---|
375 | {
|
---|
376 | LogFlowFunc (("Last VirtualBox instance was released.\n"));
|
---|
377 | LogFlowFunc (("Scheduling server shutdown in %d ms...\n",
|
---|
378 | VBoxSVC_ShutdownDelay));
|
---|
379 |
|
---|
380 | /* make sure the previous timer (if any) is stopped;
|
---|
381 | * otherwise RTTimerStart() will definitely fail. */
|
---|
382 | RTTimerLRStop (sTimer);
|
---|
383 |
|
---|
384 | int vrc = RTTimerLRStart (sTimer, uint64_t (VBoxSVC_ShutdownDelay) * 1000000);
|
---|
385 | AssertRC (vrc);
|
---|
386 | timerStarted = SUCCEEDED (vrc);
|
---|
387 | }
|
---|
388 | else
|
---|
389 | {
|
---|
390 | LogFlowFunc (("Last VirtualBox instance was released "
|
---|
391 | "on XPCOM shutdown.\n"));
|
---|
392 | Assert (onMainThread);
|
---|
393 | }
|
---|
394 |
|
---|
395 | if (!timerStarted)
|
---|
396 | {
|
---|
397 | if (!onMainThread)
|
---|
398 | {
|
---|
399 | /* Failed to start the timer, post the shutdown event
|
---|
400 | * manually if not on the main thread alreay. */
|
---|
401 | ShutdownTimer (NULL, NULL, 0);
|
---|
402 | }
|
---|
403 | else
|
---|
404 | {
|
---|
405 | /* Here we come if:
|
---|
406 | *
|
---|
407 | * a) gEventQ is 0 which means either FactoryDestructor() is called
|
---|
408 | * or the IPC/DCONNECT shutdown sequence is initiated by the
|
---|
409 | * XPCOM shutdown routine (NS_ShutdownXPCOM()), which always
|
---|
410 | * happens on the main thread.
|
---|
411 | *
|
---|
412 | * b) gEventQ has reported we're on the main thread. This means
|
---|
413 | * that DestructEventHandler() has been called, but another
|
---|
414 | * client was faster and requested VirtualBox again.
|
---|
415 | *
|
---|
416 | * In either case, there is nothing to do.
|
---|
417 | *
|
---|
418 | * Note: case b) is actually no more valid since we don't
|
---|
419 | * call Release() from DestructEventHandler() in this case
|
---|
420 | * any more. Thus, we assert below.
|
---|
421 | */
|
---|
422 |
|
---|
423 | Assert (gEventQ == NULL);
|
---|
424 | }
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | return count;
|
---|
429 | }
|
---|
430 |
|
---|
431 | class MaybeQuitEvent : public MyEvent
|
---|
432 | {
|
---|
433 | /* called on the main thread */
|
---|
434 | void *handler()
|
---|
435 | {
|
---|
436 | LogFlowFunc (("\n"));
|
---|
437 |
|
---|
438 | Assert (RTCritSectIsInitialized (&sLock));
|
---|
439 |
|
---|
440 | /* stop accepting GetInstance() requests on other threads during
|
---|
441 | * possible destruction */
|
---|
442 | RTCritSectEnter (&sLock);
|
---|
443 |
|
---|
444 | nsrefcnt count = 0;
|
---|
445 |
|
---|
446 | /* sInstance is NULL here if it was deleted immediately after
|
---|
447 | * creation due to initialization error. See GetInstance(). */
|
---|
448 | if (sInstance != NULL)
|
---|
449 | {
|
---|
450 | /* Release the guard reference added in GetInstance() */
|
---|
451 | count = sInstance->Release();
|
---|
452 | }
|
---|
453 |
|
---|
454 | if (count == 0)
|
---|
455 | {
|
---|
456 | if (gAutoShutdown)
|
---|
457 | {
|
---|
458 | Assert (sInstance == NULL);
|
---|
459 | LogFlowFunc (("Terminating the server process...\n"));
|
---|
460 | /* make it leave the event loop */
|
---|
461 | gKeepRunning = PR_FALSE;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | else
|
---|
465 | {
|
---|
466 | /* This condition is quite rare: a new client happened to
|
---|
467 | * connect after this event has been posted to the main queue
|
---|
468 | * but before it started to process it. */
|
---|
469 | LogFlowFunc (("Destruction is canceled (refcnt=%d).\n", count));
|
---|
470 | }
|
---|
471 |
|
---|
472 | RTCritSectLeave (&sLock);
|
---|
473 |
|
---|
474 | return NULL;
|
---|
475 | }
|
---|
476 | };
|
---|
477 |
|
---|
478 | static void ShutdownTimer (RTTIMERLR hTimerLR, void *pvUser, uint64_t /*iTick*/)
|
---|
479 | {
|
---|
480 | NOREF (hTimerLR);
|
---|
481 | NOREF (pvUser);
|
---|
482 |
|
---|
483 | /* A "too late" event is theoretically possible if somebody
|
---|
484 | * manually ended the server after a destruction has been scheduled
|
---|
485 | * and this method was so lucky that it got a chance to run before
|
---|
486 | * the timer was killed. */
|
---|
487 | AssertReturnVoid (gEventQ);
|
---|
488 |
|
---|
489 | /* post a quit event to the main queue */
|
---|
490 | MaybeQuitEvent *ev = new MaybeQuitEvent();
|
---|
491 | nsresult rv = ev->postTo (gEventQ);
|
---|
492 | NOREF (rv);
|
---|
493 |
|
---|
494 | /* A failure above means we've been already stopped (for example
|
---|
495 | * by Ctrl-C). FactoryDestructor() (NS_ShutdownXPCOM())
|
---|
496 | * will do the job. Nothing to do. */
|
---|
497 | }
|
---|
498 |
|
---|
499 | static NS_IMETHODIMP FactoryConstructor()
|
---|
500 | {
|
---|
501 | LogFlowFunc (("\n"));
|
---|
502 |
|
---|
503 | /* create a critsect to protect object construction */
|
---|
504 | if (RT_FAILURE (RTCritSectInit (&sLock)))
|
---|
505 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
506 |
|
---|
507 | int vrc = RTTimerLRCreateEx (&sTimer, 0, 0, ShutdownTimer, NULL);
|
---|
508 | if (RT_FAILURE (vrc))
|
---|
509 | {
|
---|
510 | LogFlowFunc (("Failed to create a timer! (vrc=%Rrc)\n", vrc));
|
---|
511 | return NS_ERROR_FAILURE;
|
---|
512 | }
|
---|
513 |
|
---|
514 | return NS_OK;
|
---|
515 | }
|
---|
516 |
|
---|
517 | static NS_IMETHODIMP FactoryDestructor()
|
---|
518 | {
|
---|
519 | LogFlowFunc (("\n"));
|
---|
520 |
|
---|
521 | RTTimerLRDestroy (sTimer);
|
---|
522 | sTimer = NULL;
|
---|
523 |
|
---|
524 | RTCritSectDelete (&sLock);
|
---|
525 |
|
---|
526 | if (sInstance != NULL)
|
---|
527 | {
|
---|
528 | /* Either posting a destruction event falied for some reason (most
|
---|
529 | * likely, the quit event has been received before the last release),
|
---|
530 | * or the client has terminated abnormally w/o releasing its
|
---|
531 | * VirtualBox instance (so NS_ShutdownXPCOM() is doing a cleanup).
|
---|
532 | * Release the guard reference we added in GetInstance(). */
|
---|
533 | sInstance->Release();
|
---|
534 | }
|
---|
535 |
|
---|
536 | return NS_OK;
|
---|
537 | }
|
---|
538 |
|
---|
539 | static nsresult GetInstance (VirtualBox **inst)
|
---|
540 | {
|
---|
541 | LogFlowFunc (("Getting VirtualBox object...\n"));
|
---|
542 |
|
---|
543 | RTCritSectEnter (&sLock);
|
---|
544 |
|
---|
545 | if (!gKeepRunning)
|
---|
546 | {
|
---|
547 | LogFlowFunc (("Process termination requested first. Refusing.\n"));
|
---|
548 |
|
---|
549 | RTCritSectLeave (&sLock);
|
---|
550 |
|
---|
551 | /* this rv is what CreateInstance() on the client side returns
|
---|
552 | * when the server process stops accepting events. Do the same
|
---|
553 | * here. The client wrapper should attempt to start a new process in
|
---|
554 | * response to a failure from us. */
|
---|
555 | return NS_ERROR_ABORT;
|
---|
556 | }
|
---|
557 |
|
---|
558 | nsresult rv = NS_OK;
|
---|
559 |
|
---|
560 | if (sInstance == NULL)
|
---|
561 | {
|
---|
562 | LogFlowFunc (("Creating new VirtualBox object...\n"));
|
---|
563 | sInstance = new VirtualBoxClassFactory();
|
---|
564 | if (sInstance != NULL)
|
---|
565 | {
|
---|
566 | /* make an extra AddRef to take the full control
|
---|
567 | * on the VirtualBox destruction (see FinalRelease()) */
|
---|
568 | sInstance->AddRef();
|
---|
569 |
|
---|
570 | sInstance->AddRef(); /* protect FinalConstruct() */
|
---|
571 | rv = sInstance->FinalConstruct();
|
---|
572 | printf ("Informational: VirtualBox object created (rc=%08X).\n", rv);
|
---|
573 | if (NS_FAILED (rv))
|
---|
574 | {
|
---|
575 | /* On failure diring VirtualBox initialization, delete it
|
---|
576 | * immediately on the current thread by releasing all
|
---|
577 | * references in order to properly schedule the server
|
---|
578 | * shutdown. Since the object is fully deleted here, there
|
---|
579 | * is a chance to fix the error and request a new
|
---|
580 | * instantiation before the server terminates. However,
|
---|
581 | * the main reason to maintain the shoutdown delay on
|
---|
582 | * failure is to let the front-end completely fetch error
|
---|
583 | * info from a server-side IVirtualBoxErrorInfo object. */
|
---|
584 | sInstance->Release();
|
---|
585 | sInstance->Release();
|
---|
586 | Assert (sInstance == NULL);
|
---|
587 | }
|
---|
588 | else
|
---|
589 | {
|
---|
590 | /* On success, make sure the previous timer is stopped to
|
---|
591 | * cancel a scheduled server termination (if any). */
|
---|
592 | RTTimerLRStop (sTimer);
|
---|
593 | }
|
---|
594 | }
|
---|
595 | else
|
---|
596 | {
|
---|
597 | rv = NS_ERROR_OUT_OF_MEMORY;
|
---|
598 | }
|
---|
599 | }
|
---|
600 | else
|
---|
601 | {
|
---|
602 | LogFlowFunc (("Using existing VirtualBox object...\n"));
|
---|
603 | nsrefcnt count = sInstance->AddRef();
|
---|
604 | Assert (count > 1);
|
---|
605 |
|
---|
606 | if (count == 2)
|
---|
607 | {
|
---|
608 | LogFlowFunc (("Another client has requested a reference to VirtualBox, "
|
---|
609 | "canceling detruction...\n"));
|
---|
610 |
|
---|
611 | /* make sure the previous timer is stopped */
|
---|
612 | RTTimerLRStop (sTimer);
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | *inst = sInstance;
|
---|
617 |
|
---|
618 | RTCritSectLeave (&sLock);
|
---|
619 |
|
---|
620 | return rv;
|
---|
621 | }
|
---|
622 |
|
---|
623 | private:
|
---|
624 |
|
---|
625 | /* Don't be confused that sInstance is of the *ClassFactory type. This is
|
---|
626 | * actually a singleton instance (*ClassFactory inherits the singleton
|
---|
627 | * class; we combined them just for "simplicity" and used "static" for
|
---|
628 | * factory methods. *ClassFactory here is necessary for a couple of extra
|
---|
629 | * methods. */
|
---|
630 |
|
---|
631 | static VirtualBoxClassFactory *sInstance;
|
---|
632 | static RTCRITSECT sLock;
|
---|
633 |
|
---|
634 | static RTTIMERLR sTimer;
|
---|
635 | };
|
---|
636 |
|
---|
637 | VirtualBoxClassFactory *VirtualBoxClassFactory::sInstance = NULL;
|
---|
638 | RTCRITSECT VirtualBoxClassFactory::sLock = {0};
|
---|
639 |
|
---|
640 | RTTIMERLR VirtualBoxClassFactory::sTimer = NIL_RTTIMERLR;
|
---|
641 |
|
---|
642 | NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR_WITH_RC
|
---|
643 | (VirtualBox, VirtualBoxClassFactory::GetInstance)
|
---|
644 |
|
---|
645 | ////////////////////////////////////////////////////////////////////////////////
|
---|
646 |
|
---|
647 | typedef NSFactoryDestructorProcPtr NSFactoryConsructorProcPtr;
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Enhanced module component information structure.
|
---|
651 | *
|
---|
652 | * nsModuleComponentInfo lacks the factory construction callback, here we add
|
---|
653 | * it. This callback is called by NS_NewGenericFactoryEx() after a
|
---|
654 | * nsGenericFactory instance is successfully created.
|
---|
655 | */
|
---|
656 | struct nsModuleComponentInfoEx : nsModuleComponentInfo
|
---|
657 | {
|
---|
658 | nsModuleComponentInfoEx () {}
|
---|
659 | nsModuleComponentInfoEx (int) {}
|
---|
660 |
|
---|
661 | nsModuleComponentInfoEx (
|
---|
662 | const char* aDescription,
|
---|
663 | const nsCID& aCID,
|
---|
664 | const char* aContractID,
|
---|
665 | NSConstructorProcPtr aConstructor,
|
---|
666 | NSRegisterSelfProcPtr aRegisterSelfProc,
|
---|
667 | NSUnregisterSelfProcPtr aUnregisterSelfProc,
|
---|
668 | NSFactoryDestructorProcPtr aFactoryDestructor,
|
---|
669 | NSGetInterfacesProcPtr aGetInterfacesProc,
|
---|
670 | NSGetLanguageHelperProcPtr aGetLanguageHelperProc,
|
---|
671 | nsIClassInfo ** aClassInfoGlobal,
|
---|
672 | PRUint32 aFlags,
|
---|
673 | NSFactoryConsructorProcPtr aFactoryConstructor)
|
---|
674 | {
|
---|
675 | mDescription = aDescription;
|
---|
676 | mCID = aCID;
|
---|
677 | mContractID = aContractID;
|
---|
678 | mConstructor = aConstructor;
|
---|
679 | mRegisterSelfProc = aRegisterSelfProc;
|
---|
680 | mUnregisterSelfProc = aUnregisterSelfProc;
|
---|
681 | mFactoryDestructor = aFactoryDestructor;
|
---|
682 | mGetInterfacesProc = aGetInterfacesProc;
|
---|
683 | mGetLanguageHelperProc = aGetLanguageHelperProc;
|
---|
684 | mClassInfoGlobal = aClassInfoGlobal;
|
---|
685 | mFlags = aFlags;
|
---|
686 | mFactoryConstructor = aFactoryConstructor;
|
---|
687 | }
|
---|
688 |
|
---|
689 | /** (optional) Factory Construction Callback */
|
---|
690 | NSFactoryConsructorProcPtr mFactoryConstructor;
|
---|
691 | };
|
---|
692 |
|
---|
693 | ////////////////////////////////////////////////////////////////////////////////
|
---|
694 |
|
---|
695 | static const nsModuleComponentInfoEx components[] =
|
---|
696 | {
|
---|
697 | nsModuleComponentInfoEx (
|
---|
698 | "VirtualBox component",
|
---|
699 | (nsCID) NS_VIRTUALBOX_CID,
|
---|
700 | NS_VIRTUALBOX_CONTRACTID,
|
---|
701 | VirtualBoxConstructor, // constructor funcion
|
---|
702 | NULL, // registration function
|
---|
703 | NULL, // deregistration function
|
---|
704 | VirtualBoxClassFactory::FactoryDestructor, // factory destructor function
|
---|
705 | NS_CI_INTERFACE_GETTER_NAME(VirtualBox),
|
---|
706 | NULL, // language helper
|
---|
707 | &NS_CLASSINFO_NAME(VirtualBox),
|
---|
708 | 0, // flags
|
---|
709 | VirtualBoxClassFactory::FactoryConstructor // factory constructor function
|
---|
710 | )
|
---|
711 | };
|
---|
712 |
|
---|
713 | /////////////////////////////////////////////////////////////////////////////
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Extends NS_NewGenericFactory() by immediately calling
|
---|
717 | * nsModuleComponentInfoEx::mFactoryConstructor before returning to the
|
---|
718 | * caller.
|
---|
719 | */
|
---|
720 | nsresult
|
---|
721 | NS_NewGenericFactoryEx (nsIGenericFactory **result,
|
---|
722 | const nsModuleComponentInfoEx *info)
|
---|
723 | {
|
---|
724 | AssertReturn (result, NS_ERROR_INVALID_POINTER);
|
---|
725 |
|
---|
726 | nsresult rv = NS_NewGenericFactory (result, info);
|
---|
727 | if (NS_SUCCEEDED (rv) && info && info->mFactoryConstructor)
|
---|
728 | {
|
---|
729 | rv = info->mFactoryConstructor();
|
---|
730 | if (NS_FAILED (rv))
|
---|
731 | NS_RELEASE (*result);
|
---|
732 | }
|
---|
733 |
|
---|
734 | return rv;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /////////////////////////////////////////////////////////////////////////////
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * Hhelper function to register self components upon start-up
|
---|
741 | * of the out-of-proc server.
|
---|
742 | */
|
---|
743 | static nsresult
|
---|
744 | RegisterSelfComponents (nsIComponentRegistrar *registrar,
|
---|
745 | const nsModuleComponentInfoEx *components,
|
---|
746 | PRUint32 count)
|
---|
747 | {
|
---|
748 | nsresult rc = NS_OK;
|
---|
749 | const nsModuleComponentInfoEx *info = components;
|
---|
750 | for (PRUint32 i = 0; i < count && NS_SUCCEEDED (rc); i++, info++)
|
---|
751 | {
|
---|
752 | /* skip components w/o a constructor */
|
---|
753 | if (!info->mConstructor) continue;
|
---|
754 | /* create a new generic factory for a component and register it */
|
---|
755 | nsIGenericFactory *factory;
|
---|
756 | rc = NS_NewGenericFactoryEx (&factory, info);
|
---|
757 | if (NS_SUCCEEDED (rc))
|
---|
758 | {
|
---|
759 | rc = registrar->RegisterFactory (info->mCID,
|
---|
760 | info->mDescription,
|
---|
761 | info->mContractID,
|
---|
762 | factory);
|
---|
763 | factory->Release();
|
---|
764 | }
|
---|
765 | }
|
---|
766 | return rc;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /////////////////////////////////////////////////////////////////////////////
|
---|
770 |
|
---|
771 | static ipcIService *gIpcServ = nsnull;
|
---|
772 | static char *pszPidFile = NULL;
|
---|
773 |
|
---|
774 | class ForceQuitEvent : public MyEvent
|
---|
775 | {
|
---|
776 | void *handler()
|
---|
777 | {
|
---|
778 | LogFlowFunc (("\n"));
|
---|
779 |
|
---|
780 | gKeepRunning = PR_FALSE;
|
---|
781 |
|
---|
782 | if (pszPidFile)
|
---|
783 | RTFileDelete(pszPidFile);
|
---|
784 |
|
---|
785 | return NULL;
|
---|
786 | }
|
---|
787 | };
|
---|
788 |
|
---|
789 | static void signal_handler (int sig)
|
---|
790 | {
|
---|
791 | if (gEventQ && gKeepRunning)
|
---|
792 | {
|
---|
793 | /* post a quit event to the queue */
|
---|
794 | ForceQuitEvent *ev = new ForceQuitEvent();
|
---|
795 | ev->postTo (gEventQ);
|
---|
796 | }
|
---|
797 | }
|
---|
798 |
|
---|
799 | #if defined(USE_BACKTRACE)
|
---|
800 | /**
|
---|
801 | * the signal handler that prints out a backtrace of the call stack.
|
---|
802 | * the code is taken from http://www.linuxjournal.com/article/6391.
|
---|
803 | */
|
---|
804 | static void bt_sighandler (int sig, siginfo_t *info, void *secret)
|
---|
805 | {
|
---|
806 |
|
---|
807 | void *trace[16];
|
---|
808 | char **messages = (char **)NULL;
|
---|
809 | int i, trace_size = 0;
|
---|
810 | ucontext_t *uc = (ucontext_t *)secret;
|
---|
811 |
|
---|
812 | // Do something useful with siginfo_t
|
---|
813 | if (sig == SIGSEGV)
|
---|
814 | Log (("Got signal %d, faulty address is %p, from %p\n",
|
---|
815 | sig, info->si_addr, uc->uc_mcontext.gregs[REG_PC]));
|
---|
816 | else
|
---|
817 | Log (("Got signal %d\n", sig));
|
---|
818 |
|
---|
819 | trace_size = backtrace (trace, 16);
|
---|
820 | // overwrite sigaction with caller's address
|
---|
821 | trace[1] = (void *) uc->uc_mcontext.gregs [REG_PC];
|
---|
822 |
|
---|
823 | messages = backtrace_symbols (trace, trace_size);
|
---|
824 | // skip first stack frame (points here)
|
---|
825 | Log (("[bt] Execution path:\n"));
|
---|
826 | for (i = 1; i < trace_size; ++i)
|
---|
827 | Log (("[bt] %s\n", messages[i]));
|
---|
828 |
|
---|
829 | exit (0);
|
---|
830 | }
|
---|
831 | #endif
|
---|
832 |
|
---|
833 | int main (int argc, char **argv)
|
---|
834 | {
|
---|
835 | const struct option options[] =
|
---|
836 | {
|
---|
837 | { "automate", no_argument, NULL, 'a' },
|
---|
838 | #ifdef RT_OS_DARWIN
|
---|
839 | { "auto-shutdown", no_argument, NULL, 'A' },
|
---|
840 | #endif
|
---|
841 | { "daemonize", no_argument, NULL, 'd' },
|
---|
842 | { "pidfile", required_argument, NULL, 'p' },
|
---|
843 | #ifdef RT_OS_DARWIN
|
---|
844 | { "pipe", required_argument, NULL, 'P' },
|
---|
845 | #endif
|
---|
846 | { NULL, 0, NULL, 0 }
|
---|
847 | };
|
---|
848 | int c;
|
---|
849 |
|
---|
850 | bool fDaemonize = false;
|
---|
851 | #ifndef RT_OS_OS2
|
---|
852 | static int daemon_pipe_fds[2] = {-1, -1};
|
---|
853 | #endif
|
---|
854 |
|
---|
855 | for (;;)
|
---|
856 | {
|
---|
857 | c = getopt_long(argc, argv, "", options, NULL);
|
---|
858 | if (c == -1)
|
---|
859 | break;
|
---|
860 | switch (c)
|
---|
861 | {
|
---|
862 | case 'a':
|
---|
863 | {
|
---|
864 | /* --automate mode means we are started by XPCOM on
|
---|
865 | * demand. Daemonize ourselves and activate
|
---|
866 | * auto-shutdown. */
|
---|
867 | gAutoShutdown = true;
|
---|
868 | fDaemonize = true;
|
---|
869 | break;
|
---|
870 | }
|
---|
871 |
|
---|
872 | #ifdef RT_OS_DARWIN
|
---|
873 | /* Used together with '-P', see below. Internal use only. */
|
---|
874 | case 'A':
|
---|
875 | {
|
---|
876 | gAutoShutdown = true;
|
---|
877 | break;
|
---|
878 | }
|
---|
879 | #endif
|
---|
880 |
|
---|
881 | case 'd':
|
---|
882 | {
|
---|
883 | fDaemonize = true;
|
---|
884 | break;
|
---|
885 | }
|
---|
886 |
|
---|
887 | case 'p':
|
---|
888 | {
|
---|
889 | pszPidFile = optarg;
|
---|
890 | break;
|
---|
891 | }
|
---|
892 |
|
---|
893 | #ifdef RT_OS_DARWIN
|
---|
894 | /* we need to exec on darwin, this is just an internal
|
---|
895 | * hack for passing the pipe fd along to the final child. */
|
---|
896 | case 'P':
|
---|
897 | {
|
---|
898 | daemon_pipe_fds[1] = atoi(optarg);
|
---|
899 | break;
|
---|
900 | }
|
---|
901 | #endif
|
---|
902 |
|
---|
903 | default:
|
---|
904 | {
|
---|
905 | /* exit on invalid options */
|
---|
906 | return 1;
|
---|
907 | }
|
---|
908 | }
|
---|
909 | }
|
---|
910 |
|
---|
911 | static RTFILE pidFile = NIL_RTFILE;
|
---|
912 |
|
---|
913 | #ifdef RT_OS_OS2
|
---|
914 |
|
---|
915 | /* nothing to do here, the process is supposed to be already
|
---|
916 | * started daemonized when it is necessary */
|
---|
917 | NOREF(fDaemonize);
|
---|
918 |
|
---|
919 | #else // ifdef RT_OS_OS2
|
---|
920 |
|
---|
921 | if (fDaemonize)
|
---|
922 | {
|
---|
923 | /* create a pipe for communication between child and parent */
|
---|
924 | if (pipe(daemon_pipe_fds) < 0)
|
---|
925 | {
|
---|
926 | printf("ERROR: pipe() failed (errno = %d)\n", errno);
|
---|
927 | return 1;
|
---|
928 | }
|
---|
929 |
|
---|
930 | pid_t childpid = fork();
|
---|
931 | if (childpid == -1)
|
---|
932 | {
|
---|
933 | printf("ERROR: fork() failed (errno = %d)\n", errno);
|
---|
934 | return 1;
|
---|
935 | }
|
---|
936 |
|
---|
937 | if (childpid != 0)
|
---|
938 | {
|
---|
939 | /* we're the parent process */
|
---|
940 | bool fSuccess = false;
|
---|
941 |
|
---|
942 | /* close the writing end of the pipe */
|
---|
943 | close(daemon_pipe_fds[1]);
|
---|
944 |
|
---|
945 | /* try to read a message from the pipe */
|
---|
946 | char msg[10] = {0}; /* initialize so it's NULL terminated */
|
---|
947 | if (read(daemon_pipe_fds[0], msg, sizeof(msg)) > 0)
|
---|
948 | {
|
---|
949 | if (strcmp(msg, "READY") == 0)
|
---|
950 | fSuccess = true;
|
---|
951 | else
|
---|
952 | printf ("ERROR: Unknown message from child "
|
---|
953 | "process (%s)\n", msg);
|
---|
954 | }
|
---|
955 | else
|
---|
956 | printf ("ERROR: 0 bytes read from child process\n");
|
---|
957 |
|
---|
958 | /* close the reading end of the pipe as well and exit */
|
---|
959 | close(daemon_pipe_fds[0]);
|
---|
960 | return fSuccess ? 0 : 1;
|
---|
961 | }
|
---|
962 | /* we're the child process */
|
---|
963 |
|
---|
964 | /* Create a new SID for the child process */
|
---|
965 | pid_t sid = setsid();
|
---|
966 | if (sid < 0)
|
---|
967 | {
|
---|
968 | printf("ERROR: setsid() failed (errno = %d)\n", errno);
|
---|
969 | return 1;
|
---|
970 | }
|
---|
971 |
|
---|
972 | /* Need to do another for to get rid of the session leader status.
|
---|
973 | * Otherwise any accidentally opened tty will automatically become a
|
---|
974 | * controlling tty for the daemon process. */
|
---|
975 | childpid = fork();
|
---|
976 | if (childpid == -1)
|
---|
977 | {
|
---|
978 | printf("ERROR: second fork() failed (errno = %d)\n", errno);
|
---|
979 | return 1;
|
---|
980 | }
|
---|
981 |
|
---|
982 | if (childpid != 0)
|
---|
983 | {
|
---|
984 | /* we're the parent process, just a dummy so terminate now */
|
---|
985 | exit(0);
|
---|
986 | }
|
---|
987 |
|
---|
988 | /* Redirect standard i/o streams to /dev/null */
|
---|
989 | if (daemon_pipe_fds[0] > 2)
|
---|
990 | {
|
---|
991 | freopen ("/dev/null", "r", stdin);
|
---|
992 | freopen ("/dev/null", "w", stdout);
|
---|
993 | freopen ("/dev/null", "w", stderr);
|
---|
994 | }
|
---|
995 |
|
---|
996 | /* close the reading end of the pipe */
|
---|
997 | close(daemon_pipe_fds[0]);
|
---|
998 |
|
---|
999 | # ifdef RT_OS_DARWIN
|
---|
1000 | /*
|
---|
1001 | * On leopard we're no longer allowed to use some of the core API's
|
---|
1002 | * after forking - this will cause us to hit an int3.
|
---|
1003 | * So, we'll have to execv VBoxSVC once again and hand it the pipe
|
---|
1004 | * and all other relevant options.
|
---|
1005 | */
|
---|
1006 | const char *apszArgs[7];
|
---|
1007 | unsigned i = 0;
|
---|
1008 | apszArgs[i++] = argv[0];
|
---|
1009 | apszArgs[i++] = "--pipe";
|
---|
1010 | char szPipeArg[32];
|
---|
1011 | RTStrPrintf (szPipeArg, sizeof (szPipeArg), "%d", daemon_pipe_fds[1]);
|
---|
1012 | apszArgs[i++] = szPipeArg;
|
---|
1013 | if (pszPidFile)
|
---|
1014 | {
|
---|
1015 | apszArgs[i++] = "--pidfile";
|
---|
1016 | apszArgs[i++] = pszPidFile;
|
---|
1017 | }
|
---|
1018 | if (gAutoShutdown)
|
---|
1019 | apszArgs[i++] = "--auto-shutdown";
|
---|
1020 | apszArgs[i++] = NULL; Assert(i <= RT_ELEMENTS(apszArgs));
|
---|
1021 | execv (apszArgs[0], (char * const *)apszArgs);
|
---|
1022 | exit (0);
|
---|
1023 | # endif
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | #endif // ifdef RT_OS_OS2
|
---|
1027 |
|
---|
1028 | #if defined(USE_BACKTRACE)
|
---|
1029 | {
|
---|
1030 | /* install our signal handler to backtrace the call stack */
|
---|
1031 | struct sigaction sa;
|
---|
1032 | sa.sa_sigaction = bt_sighandler;
|
---|
1033 | sigemptyset (&sa.sa_mask);
|
---|
1034 | sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
---|
1035 | sigaction (SIGSEGV, &sa, NULL);
|
---|
1036 | sigaction (SIGBUS, &sa, NULL);
|
---|
1037 | sigaction (SIGUSR1, &sa, NULL);
|
---|
1038 | }
|
---|
1039 | #endif
|
---|
1040 |
|
---|
1041 | /*
|
---|
1042 | * Initialize the VBox runtime without loading
|
---|
1043 | * the support driver
|
---|
1044 | */
|
---|
1045 | RTR3Init();
|
---|
1046 |
|
---|
1047 | nsresult rc;
|
---|
1048 |
|
---|
1049 | do
|
---|
1050 | {
|
---|
1051 | rc = com::Initialize();
|
---|
1052 | if (NS_FAILED (rc))
|
---|
1053 | {
|
---|
1054 | printf ("ERROR: Failed to initialize XPCOM! (rc=%08X)\n", rc);
|
---|
1055 | break;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | nsCOMPtr <nsIComponentRegistrar> registrar;
|
---|
1059 | rc = NS_GetComponentRegistrar (getter_AddRefs (registrar));
|
---|
1060 | if (NS_FAILED (rc))
|
---|
1061 | {
|
---|
1062 | printf ("ERROR: Failed to get component registrar! (rc=%08X)\n", rc);
|
---|
1063 | break;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | registrar->AutoRegister (nsnull);
|
---|
1067 | rc = RegisterSelfComponents (registrar, components,
|
---|
1068 | NS_ARRAY_LENGTH (components));
|
---|
1069 | if (NS_FAILED (rc))
|
---|
1070 | {
|
---|
1071 | printf ("ERROR: Failed to register server components! (rc=%08X)\n", rc);
|
---|
1072 | break;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /* get the main thread's event queue (afaik, the dconnect service always
|
---|
1076 | * gets created upon XPCOM startup, so it will use the main (this)
|
---|
1077 | * thread's event queue to receive IPC events) */
|
---|
1078 | rc = NS_GetMainEventQ (&gEventQ);
|
---|
1079 | if (NS_FAILED (rc))
|
---|
1080 | {
|
---|
1081 | printf ("ERROR: Failed to get the main event queue! (rc=%08X)\n", rc);
|
---|
1082 | break;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | nsCOMPtr<ipcIService> ipcServ (do_GetService(IPC_SERVICE_CONTRACTID, &rc));
|
---|
1086 | if (NS_FAILED (rc))
|
---|
1087 | {
|
---|
1088 | printf ("ERROR: Failed to get IPC service! (rc=%08X)\n", rc);
|
---|
1089 | break;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | NS_ADDREF (gIpcServ = ipcServ);
|
---|
1093 |
|
---|
1094 | LogFlowFunc (("Will use \"%s\" as server name.\n", VBOXSVC_IPC_NAME));
|
---|
1095 |
|
---|
1096 | rc = gIpcServ->AddName (VBOXSVC_IPC_NAME);
|
---|
1097 | if (NS_FAILED (rc))
|
---|
1098 | {
|
---|
1099 | LogFlowFunc (("Failed to register the server name (rc=%08X)!\n"
|
---|
1100 | "Is another server already running?\n", rc));
|
---|
1101 |
|
---|
1102 | printf ("ERROR: Failed to register the server name \"%s\" (rc=%08X)!\n"
|
---|
1103 | "Is another server already running?\n",
|
---|
1104 | VBOXSVC_IPC_NAME, rc);
|
---|
1105 | NS_RELEASE (gIpcServ);
|
---|
1106 | break;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | {
|
---|
1110 | /* setup signal handling to convert some signals to a quit event */
|
---|
1111 | struct sigaction sa;
|
---|
1112 | sa.sa_handler = signal_handler;
|
---|
1113 | sigemptyset (&sa.sa_mask);
|
---|
1114 | sa.sa_flags = 0;
|
---|
1115 | sigaction (SIGINT, &sa, NULL);
|
---|
1116 | sigaction (SIGQUIT, &sa, NULL);
|
---|
1117 | sigaction (SIGTERM, &sa, NULL);
|
---|
1118 | sigaction (SIGTRAP, &sa, NULL);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | {
|
---|
1122 | char szBuf[80];
|
---|
1123 | int iSize;
|
---|
1124 |
|
---|
1125 | iSize = snprintf (szBuf, sizeof(szBuf),
|
---|
1126 | "Sun xVM VirtualBox XPCOM Server Version "
|
---|
1127 | VBOX_VERSION_STRING);
|
---|
1128 | for (int i=iSize; i>0; i--)
|
---|
1129 | putchar('*');
|
---|
1130 | printf ("\n%s\n", szBuf);
|
---|
1131 | printf ("(C) 2008-2009 Sun Microsystems, Inc.\n"
|
---|
1132 | "All rights reserved.\n");
|
---|
1133 | #ifdef DEBUG
|
---|
1134 | printf ("Debug version.\n");
|
---|
1135 | #endif
|
---|
1136 | #if 0
|
---|
1137 | /* in my opinion two lines enclosing the text look better */
|
---|
1138 | for (int i=iSize; i>0; i--)
|
---|
1139 | putchar('*');
|
---|
1140 | putchar('\n');
|
---|
1141 | #endif
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | #ifndef RT_OS_OS2
|
---|
1145 | if (daemon_pipe_fds[1] >= 0)
|
---|
1146 | {
|
---|
1147 | printf ("\nStarting event loop....\n[send TERM signal to quit]\n");
|
---|
1148 | /* now we're ready, signal the parent process */
|
---|
1149 | write(daemon_pipe_fds[1], "READY", strlen("READY"));
|
---|
1150 | }
|
---|
1151 | else
|
---|
1152 | #endif
|
---|
1153 | {
|
---|
1154 | printf ("\nStarting event loop....\n[press Ctrl-C to quit]\n");
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | if (pszPidFile)
|
---|
1158 | {
|
---|
1159 | char szBuf[32];
|
---|
1160 | const char *lf = "\n";
|
---|
1161 | RTFileOpen(&pidFile, pszPidFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE);
|
---|
1162 | RTStrFormatNumber(szBuf, getpid(), 10, 0, 0, 0);
|
---|
1163 | RTFileWrite(pidFile, szBuf, strlen(szBuf), NULL);
|
---|
1164 | RTFileWrite(pidFile, lf, strlen(lf), NULL);
|
---|
1165 | RTFileClose(pidFile);
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | #ifndef RT_OS_OS2
|
---|
1169 | // Increase the file table size to 10240 or as high as possible.
|
---|
1170 | struct rlimit lim;
|
---|
1171 | if (getrlimit(RLIMIT_NOFILE, &lim) == 0)
|
---|
1172 | {
|
---|
1173 | if ( lim.rlim_cur < 10240
|
---|
1174 | && lim.rlim_cur < lim.rlim_max)
|
---|
1175 | {
|
---|
1176 | lim.rlim_cur = RT_MIN(lim.rlim_max, 10240);
|
---|
1177 | if (setrlimit(RLIMIT_NOFILE, &lim) == -1)
|
---|
1178 | printf("WARNING: failed to increase file descriptor limit. (%d)\n", errno);
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 | else
|
---|
1182 | printf("WARNING: failed to obtain per-process file-descriptor limit (%d).\n", errno);
|
---|
1183 | #endif
|
---|
1184 |
|
---|
1185 | PLEvent *ev;
|
---|
1186 | while (gKeepRunning)
|
---|
1187 | {
|
---|
1188 | gEventQ->WaitForEvent (&ev);
|
---|
1189 | gEventQ->HandleEvent (ev);
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /* stop accepting new events. Clients that happen to resolve our
|
---|
1193 | * name and issue a CreateInstance() request after this point will
|
---|
1194 | * get NS_ERROR_ABORT once we hande the remaining messages. As a
|
---|
1195 | * result, they should try to start a new server process. */
|
---|
1196 | gEventQ->StopAcceptingEvents();
|
---|
1197 |
|
---|
1198 | /* unregister ourselves. After this point, clients will start a new
|
---|
1199 | * process because they won't be able to resolve the server name.*/
|
---|
1200 | gIpcServ->RemoveName (VBOXSVC_IPC_NAME);
|
---|
1201 |
|
---|
1202 | /* process any remaining events. These events may include
|
---|
1203 | * CreateInstance() requests received right before we called
|
---|
1204 | * StopAcceptingEvents() above. We will detect this case below,
|
---|
1205 | * restore gKeepRunning and continue to serve. */
|
---|
1206 | gEventQ->ProcessPendingEvents();
|
---|
1207 |
|
---|
1208 | printf ("Terminated event loop.\n");
|
---|
1209 | }
|
---|
1210 | while (0); // this scopes the nsCOMPtrs
|
---|
1211 |
|
---|
1212 | NS_IF_RELEASE (gIpcServ);
|
---|
1213 | NS_IF_RELEASE (gEventQ);
|
---|
1214 |
|
---|
1215 | /* no nsCOMPtrs are allowed to be alive when you call com::Shutdown(). */
|
---|
1216 |
|
---|
1217 | LogFlowFunc (("Calling com::Shutdown()...\n"));
|
---|
1218 | rc = com::Shutdown();
|
---|
1219 | LogFlowFunc (("Finished com::Shutdown() (rc=%08X)\n", rc));
|
---|
1220 |
|
---|
1221 | if (NS_FAILED (rc))
|
---|
1222 | printf ("ERROR: Failed to shutdown XPCOM! (rc=%08X)\n", rc);
|
---|
1223 |
|
---|
1224 | printf ("XPCOM server has shutdown.\n");
|
---|
1225 |
|
---|
1226 | if (pszPidFile)
|
---|
1227 | {
|
---|
1228 | RTFileDelete(pszPidFile);
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | #ifndef RT_OS_OS2
|
---|
1232 | if (daemon_pipe_fds[1] >= 0)
|
---|
1233 | {
|
---|
1234 | /* close writing end of the pipe as well */
|
---|
1235 | close(daemon_pipe_fds[1]);
|
---|
1236 | }
|
---|
1237 | #endif
|
---|
1238 |
|
---|
1239 | return 0;
|
---|
1240 | }
|
---|