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