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