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