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