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