server.cpp revision 3fa054c0038fc8f6731129def4437296b773918f
6ca0e6973c8176100f4a426444823ae5e777e28fsascha/** @file
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz *
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz * XPCOM server process start point
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz */
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz/*
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz * Copyright (C) 2006-2007 innotek GmbH
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz *
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * This file is part of VirtualBox Open Source Edition (OSE), as
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * available from http://www.virtualbox.org. This file is free software;
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * you can redistribute it and/or modify it under the terms of the GNU
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * General Public License as published by the Free Software Foundation,
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * distribution. VirtualBox OSE is distributed in the hope that it will
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * be useful, but WITHOUT ANY WARRANTY of any kind.
6ca0e6973c8176100f4a426444823ae5e777e28fsascha *
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * If you received this file as part of a commercial VirtualBox
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * distribution, then only the terms of your commercial VirtualBox
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * license agreement apply instead of the previous paragraph.
6ca0e6973c8176100f4a426444823ae5e777e28fsascha */
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <ipcIService.h>
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsascha#include <ipcCID.h>
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsascha
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsascha#include <nsIComponentRegistrar.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <nsXPCOMGlue.h>
e50587e6b2ff25bdcbe29410be5bf8f4792016eegregames#include <nsEventQueueUtils.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
0d9c389e9c63d02edb068917d49af2fa8012af91marc// for nsMyFactory
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <nsIGenericFactory.h>
dadf206a4cf1eccf5c13b64bd9678610dd763868rbb#include <nsIClassInfo.h>
dadf206a4cf1eccf5c13b64bd9678610dd763868rbb
a773226ea345018b49d45cf2e076ff2452eead5arbb#include "linux/server.h"
f9e54e16ac4952a85bb00b8043c1d1e0f495eefbgregames
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include "Logging.h"
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
ec9b5c4854a4055d00f5459dbbd879d517f1531dmartin#include <iprt/runtime.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <iprt/path.h>
5181b311fbd45b65a34b3fddb3ace5fab85f36e4wrowe#include <iprt/critsect.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <iprt/timer.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
4cbb3bcf66a6788196bcc7244e4e36705acfbb57rbb#include <VBox/param.h>
a91a466a0d812a522d492c3c182163f73591ca20aaron#include <VBox/version.h>
a91a466a0d812a522d492c3c182163f73591ca20aaron
5f69f46ccdf50afd8f0a23f134746e6066185e89rbb#include <VBox/com/com.h>
12b0070d002520b81a273ae892b9ea08e810b123rbb
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <stdio.h>
886cd69ebf69e990dbc365be87ff8ea7cd681904rbb
6ca0e6973c8176100f4a426444823ae5e777e28fsascha// for the signal handler
305a68a1a5db3cc264077d89b3caa6f923257150fielding#include <signal.h>
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha#include <stdlib.h>
105475009f541187ba7a14a367547d9404c578befielding#include <unistd.h>
105475009f541187ba7a14a367547d9404c578befielding#include <errno.h>
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha#include <getopt.h>
a7d224ab99ed0a9b2b7fee9db6ac231fb7e02b0bfuankg
a7d224ab99ed0a9b2b7fee9db6ac231fb7e02b0bfuankg// for the backtrace signal handler
f07a80771bc5d30b1e0cfcb7256c1a302da77675rbb#if defined(DEBUG) && defined(__LINUX__)
6ca0e6973c8176100f4a426444823ae5e777e28fsascha# define USE_BACKTRACE
80e9324586de2fa55e2da4df409e6a85885ea659fielding#endif
9022fa73d4ac101553e7c36a1457ead52a73333borlikowski#if defined(USE_BACKTRACE)
9022fa73d4ac101553e7c36a1457ead52a73333borlikowski# include <execinfo.h>
38d37e90b45f2a735e7aecf702c76159e0b9f58ftrawick// get REG_EIP/RIP from ucontext.h
105475009f541187ba7a14a367547d9404c578befielding# ifndef __USE_GNU
6ca0e6973c8176100f4a426444823ae5e777e28fsascha# define __USE_GNU
105475009f541187ba7a14a367547d9404c578befielding# endif
105475009f541187ba7a14a367547d9404c578befielding# include <ucontext.h>
105475009f541187ba7a14a367547d9404c578befielding# ifdef __AMD64__
105475009f541187ba7a14a367547d9404c578befielding# define REG_PC REG_RIP
105475009f541187ba7a14a367547d9404c578befielding# else
105475009f541187ba7a14a367547d9404c578befielding# define REG_PC REG_EIP
105475009f541187ba7a14a367547d9404c578befielding# endif
105475009f541187ba7a14a367547d9404c578befielding#endif
105475009f541187ba7a14a367547d9404c578befielding
105475009f541187ba7a14a367547d9404c578befielding/////////////////////////////////////////////////////////////////////////////
105475009f541187ba7a14a367547d9404c578befielding// VirtualBox component instantiation
105475009f541187ba7a14a367547d9404c578befielding/////////////////////////////////////////////////////////////////////////////
67a658b0de7252b685412e0d424a0a18ab484f9ftrawick
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <nsIGenericFactory.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
9db19053d8158d96abd6934678c012e0d59d59edjerenkrantz#include <VirtualBox_XPCOM.h>
6ca0e6973c8176100f4a426444823ae5e777e28fsascha#include <VirtualBoxImpl.h>
cb5d3f2f217d457dada4883addb1dc9f3f17bb85fielding#include <MachineImpl.h>
6a1c919219ae7e3382fb22f1c4cf89f11b4becffcoar#include <SnapshotImpl.h>
24066abc54d26593eaad3ebbbaad2fd6b6693700fuankg#include <HardDiskImpl.h>
ce8d7a0627c0f246376cccd980463f8250a6a97ebjh#include <ProgressImpl.h>
ce8d7a0627c0f246376cccd980463f8250a6a97ebjh#include <DVDDriveImpl.h>
20bca6c9617ad7d0268203c21039424e99acdd91bjh#include <FloppyDriveImpl.h>
ce8d7a0627c0f246376cccd980463f8250a6a97ebjh#include <VRDPServerImpl.h>
ce8d7a0627c0f246376cccd980463f8250a6a97ebjh#include <DVDImageImpl.h>
11c8f1b409a02814693ec75cbe7f69f3ef994fbajerenkrantz#include <FloppyImageImpl.h>
cdb58be93a9cbeaba1ebc759b48aa8ed519675a9bjh#include <SharedFolderImpl.h>
7dfed2b71c9c4223996cbd7c5c0c85c7c8fef2a4rbb#include <HostImpl.h>
0a2212da6843659e65c378c5201e6612be625731sf#include <HostDVDDriveImpl.h>
fb333f30ae01a1f14b2afaa8a92c99192abf883erjung#include <HostFloppyDriveImpl.h>
5d5073be0470140d32914de8e74d869ee4145bffjerenkrantz#include <HostUSBDeviceImpl.h>
6552de06c3d34e6ecfad4c93a6604531b52caf63wsanchez#include <GuestOSTypeImpl.h>
4b09621ec91a53c7b48114c37a84f0018cadcb34jerenkrantz#include <NetworkAdapterImpl.h>
e9ab76f3d59c1be02501c291554715e8155c7c81jorton#include <SerialPortImpl.h>
5d5073be0470140d32914de8e74d869ee4145bffjerenkrantz#include <USBControllerImpl.h>
6552de06c3d34e6ecfad4c93a6604531b52caf63wsanchez#include <USBDeviceImpl.h>
4b09621ec91a53c7b48114c37a84f0018cadcb34jerenkrantz#include <AudioAdapterImpl.h>
e9ab76f3d59c1be02501c291554715e8155c7c81jorton#include <SystemPropertiesImpl.h>
93e568f5fc0607ca5063f5fe1b6cef985379de24jorton#include <Collection.h>
93e568f5fc0607ca5063f5fe1b6cef985379de24jorton
6ca0e6973c8176100f4a426444823ae5e777e28fsascha// implement nsISupports parts of our objects with support for nsIClassInfo
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_DECL_CLASSINFO(VirtualBox)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(VirtualBox, IVirtualBox)
26e9ee946723dbc91b9e5eee0d903975bc19e84fjortonNS_DECL_CLASSINFO(Machine)
a736bac16e58d5e96945f35ee3c43a2cd2f5d37ejerenkrantzNS_IMPL_THREADSAFE_ISUPPORTS1_CI(Machine, IMachine)
88ecd979f1112454432371f55a1420240fae3743trawickNS_DECL_CLASSINFO(SessionMachine)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS2_CI(SessionMachine, IMachine, IInternalMachineControl)
88ecd979f1112454432371f55a1420240fae3743trawickNS_DECL_CLASSINFO(SnapshotMachine)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(SnapshotMachine, IMachine)
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsaschaNS_DECL_CLASSINFO(Snapshot)
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(Snapshot, ISnapshot)
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsaschaNS_DECL_CLASSINFO(HardDisk)
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(HardDisk, IHardDisk)
3a47c2ef905c3b6483279637fb8054ca67c5b6ddsaschaNS_DECL_CLASSINFO(HVirtualDiskImage)
96e6cafca226a8a2a64a7bbdc634b5b2679c9e0csaschaNS_IMPL_THREADSAFE_ISUPPORTS2_CI(HVirtualDiskImage, IHardDisk, IVirtualDiskImage)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_DECL_CLASSINFO(HISCSIHardDisk)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_IMPL_THREADSAFE_ISUPPORTS2_CI(HISCSIHardDisk, IHardDisk, IISCSIHardDisk)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_DECL_CLASSINFO(HVMDKImage)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_IMPL_THREADSAFE_ISUPPORTS2_CI(HVMDKImage, IHardDisk, IVMDKImage)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_DECL_CLASSINFO(HardDiskAttachment)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(HardDiskAttachment, IHardDiskAttachment)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_DECL_CLASSINFO(Progress)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(Progress, IProgress)
22ad84685642948ad15cc1e881ecbc4ac8cbf98cmanojNS_DECL_CLASSINFO(CombinedProgress)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(CombinedProgress, IProgress)
b90c64e517df3fe1c97806830f84a30362187f0afieldingNS_DECL_CLASSINFO(DVDDrive)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(DVDDrive, IDVDDrive)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_DECL_CLASSINFO(FloppyDrive)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(FloppyDrive, IFloppyDrive)
b90c64e517df3fe1c97806830f84a30362187f0afieldingNS_DECL_CLASSINFO(SharedFolder)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(SharedFolder, ISharedFolder)
e6b0215ca481b00d658a0adfea378c36a67aa088trawick#ifdef VBOX_VRDP
e6b0215ca481b00d658a0adfea378c36a67aa088trawickNS_DECL_CLASSINFO(VRDPServer)
e6b0215ca481b00d658a0adfea378c36a67aa088trawickNS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDPServer, IVRDPServer)
e6b0215ca481b00d658a0adfea378c36a67aa088trawick#endif
e6b0215ca481b00d658a0adfea378c36a67aa088trawickNS_DECL_CLASSINFO(DVDImage)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(DVDImage, IDVDImage)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_DECL_CLASSINFO(FloppyImage)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(FloppyImage, IFloppyImage)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_DECL_CLASSINFO(Host)
ec7f09808149aa3a0f9895c29025766d4961a044dougmNS_IMPL_THREADSAFE_ISUPPORTS1_CI(Host, IHost)
b9e4a86f22b3e8666186b9fe08e9241cdaf50d26manojNS_DECL_CLASSINFO(HostDVDDrive)
b9e4a86f22b3e8666186b9fe08e9241cdaf50d26manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostDVDDrive, IHostDVDDrive)
d6baa4df51dd4759e4a7d26d3c864d7ef20e08bcmanojNS_DECL_CLASSINFO(HostFloppyDrive)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostFloppyDrive, IHostFloppyDrive)
aa811cad2eb3fc01d17a5c8eba274b915ab8cd20manojNS_DECL_CLASSINFO(GuestOSType)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_IMPL_THREADSAFE_ISUPPORTS1_CI(GuestOSType, IGuestOSType)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_DECL_CLASSINFO(NetworkAdapter)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_IMPL_THREADSAFE_ISUPPORTS1_CI(NetworkAdapter, INetworkAdapter)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_DECL_CLASSINFO(SerialPort)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_IMPL_THREADSAFE_ISUPPORTS1_CI(SerialPort, ISerialPort)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_DECL_CLASSINFO(USBController)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(USBController, IUSBController)
825ab8e480abebc9528fa3b580df41bc9ec4fbderbbNS_DECL_CLASSINFO(USBDeviceFilter)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(USBDeviceFilter, IUSBDeviceFilter)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_DECL_CLASSINFO(HostUSBDevice)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS2_CI(HostUSBDevice, IUSBDevice, IHostUSBDevice)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_DECL_CLASSINFO(HostUSBDeviceFilter)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_IMPL_THREADSAFE_ISUPPORTS1_CI(HostUSBDeviceFilter, IHostUSBDeviceFilter)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandNS_DECL_CLASSINFO(AudioAdapter)
0af3a77b888861a190b1701079a96469f323e661rbbNS_IMPL_THREADSAFE_ISUPPORTS1_CI(AudioAdapter, IAudioAdapter)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_DECL_CLASSINFO(SystemProperties)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(SystemProperties, ISystemProperties)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_DECL_CLASSINFO(BIOSSettings)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaNS_IMPL_THREADSAFE_ISUPPORTS1_CI(BIOSSettings, IBIOSSettings)
7b4eb92d597c1c5cb679d021b84f06f1e425f41bdgaudet
6ca0e6973c8176100f4a426444823ae5e777e28fsascha// collections and enumerators
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaCOM_IMPL_READONLY_ENUM_AND_COLLECTION(Machine)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandCOM_IMPL_READONLY_ENUM_AND_COLLECTION(Snapshot)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandCOM_IMPL_READONLY_ENUM_AND_COLLECTION(HardDiskAttachment)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandCOM_IMPL_READONLY_ENUM_AND_COLLECTION(GuestOSType)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandCOM_IMPL_READONLY_ENUM_AND_COLLECTION(USBDeviceFilter)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrandCOM_IMPL_READONLY_ENUM_AND_COLLECTION(HostDVDDrive)
825ab8e480abebc9528fa3b580df41bc9ec4fbderbbCOM_IMPL_READONLY_ENUM_AND_COLLECTION(HostFloppyDrive)
825ab8e480abebc9528fa3b580df41bc9ec4fbderbbCOM_IMPL_READONLY_ENUM_AND_COLLECTION(HostUSBDevice)
cb5d3f2f217d457dada4883addb1dc9f3f17bb85fieldingCOM_IMPL_READONLY_ENUM_AND_COLLECTION(HostUSBDeviceFilter)
cb5d3f2f217d457dada4883addb1dc9f3f17bb85fieldingCOM_IMPL_READONLY_ENUM_AND_COLLECTION(HardDisk)
825ab8e480abebc9528fa3b580df41bc9ec4fbderbbCOM_IMPL_READONLY_ENUM_AND_COLLECTION(DVDImage)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaCOM_IMPL_READONLY_ENUM_AND_COLLECTION(FloppyImage)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaCOM_IMPL_READONLY_ENUM_AND_COLLECTION(SharedFolder)
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaCOM_IMPL_READONLY_ENUM_AND_COLLECTION_AS(Progress, IProgress)
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaCOM_IMPL_READONLY_ENUM_AND_COLLECTION_AS(IfaceUSBDevice, IUSBDevice)
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
6ca0e6973c8176100f4a426444823ae5e777e28fsascha////////////////////////////////////////////////////////////////////////////////
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
6ca0e6973c8176100f4a426444823ae5e777e28fsaschaenum
6ca0e6973c8176100f4a426444823ae5e777e28fsascha{
6ca0e6973c8176100f4a426444823ae5e777e28fsascha /* Delay before shutting down the VirtualBox server after the last
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * VirtualBox instance is released, in ms */
6ca0e6973c8176100f4a426444823ae5e777e28fsascha VBoxSVC_ShutdownDelay = 5000,
5eb7c0254ac26350a5a471c23514152446d50bccjorton};
8013f5d3fff948bf54b8df266e5fab613629b0c1jorton
6ca0e6973c8176100f4a426444823ae5e777e28fsaschastatic bool gAutoShutdown = false;
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
6ca0e6973c8176100f4a426444823ae5e777e28fsaschastatic nsIEventQueue* gEventQ = nsnull;
6ca0e6973c8176100f4a426444823ae5e777e28fsaschastatic PRBool volatile gKeepRunning = PR_TRUE;
164141857c02667118df04b14fe536d1756f0ef4jorton
6ca0e6973c8176100f4a426444823ae5e777e28fsascha/////////////////////////////////////////////////////////////////////////////
d52ef6a778f70aae633080afeb3b8668005a07c7jerenkrantz
d52ef6a778f70aae633080afeb3b8668005a07c7jerenkrantz/**
d52ef6a778f70aae633080afeb3b8668005a07c7jerenkrantz * Simple but smart PLEvent wrapper.
6ca0e6973c8176100f4a426444823ae5e777e28fsascha *
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * @note Instances must be always created with <tt>operator new</tt>!
6ca0e6973c8176100f4a426444823ae5e777e28fsascha */
832935ea1f5cc1b21d72674929eef16ed80437d6martinclass MyEvent
6ca0e6973c8176100f4a426444823ae5e777e28fsascha{
6ca0e6973c8176100f4a426444823ae5e777e28fsaschapublic:
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha MyEvent()
6ca0e6973c8176100f4a426444823ae5e777e28fsascha {
6ca0e6973c8176100f4a426444823ae5e777e28fsascha mEv.that = NULL;
6ca0e6973c8176100f4a426444823ae5e777e28fsascha };
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf /**
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf * Posts this event to the given message queue. This method may only be
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf * called once. @note On success, the event will be deleted automatically
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf * after it is delivered and handled. On failure, the event will delete
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf * itself before this method returns! The caller must not delete it in
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf * either case.
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf */
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf nsresult postTo (nsIEventQueue *aEventQ)
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf {
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf AssertReturn (mEv.that == NULL, NS_ERROR_FAILURE);
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf AssertReturn (aEventQ, NS_ERROR_FAILURE);
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf nsresult rv = aEventQ->InitEvent (&mEv.e, NULL,
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf eventHandler, eventDestructor);
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf if (NS_SUCCEEDED (rv))
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf {
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf mEv.that = this;
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf rv = aEventQ->PostEvent (&mEv.e);
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf if (NS_SUCCEEDED (rv))
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf return rv;
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf }
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf delete this;
d0211374a981173d4537e3f919c708b5f64040fcgstein return rv;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick }
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick virtual void *handler() = 0;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawickprivate:
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick struct Ev
e5281d93b355c2c57b57e70091acc90afb81b3bdtrawick {
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick PLEvent e;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick MyEvent *that;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick } mEv;
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick static void *PR_CALLBACK eventHandler (PLEvent *self)
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick {
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick return reinterpret_cast <Ev *> (self)->that->handler();
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick }
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick static void PR_CALLBACK eventDestructor (PLEvent *self)
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick {
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick delete reinterpret_cast <Ev *> (self)->that;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick }
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick};
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick////////////////////////////////////////////////////////////////////////////////
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick/**
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick * VirtualBox class factory that destroys the created instance right after
cbe7e2406bb76d2182ef7065f0a4064a8ad65631trawick * the last reference to it is released by the client, and recreates it again
cbe7e2406bb76d2182ef7065f0a4064a8ad65631trawick * when necessary (so VirtualBox acts like a singleton object).
cbe7e2406bb76d2182ef7065f0a4064a8ad65631trawick */
0af58edfee6112cc3399e0e693340e525b96ab1ctrawickclass VirtualBoxClassFactory : public VirtualBox
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick{
0af58edfee6112cc3399e0e693340e525b96ab1ctrawickpublic:
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick virtual ~VirtualBoxClassFactory()
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf {
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick LogFlowFunc (("Deleting VirtualBox...\n"));
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick FinalRelease();
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick sInstance = 0;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick LogFlowFunc (("VirtualBox object deleted.\n"));
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick printf ("Informational: VirtualBox object deleted.\n");
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick }
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick NS_IMETHOD_(nsrefcnt) Release()
319fab7d7f9b50ff1e6403b44a51c09f9a594cdbsf {
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick /* we overload Release() to guarantee the VirtualBox destructor is
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * always called on the main thread */
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick nsrefcnt count = VirtualBox::Release();
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick if (count == 1)
a299403a6661753a93f4e4f476946c0de4e11311trawick {
a299403a6661753a93f4e4f476946c0de4e11311trawick /* the last reference held by clients is being released
a299403a6661753a93f4e4f476946c0de4e11311trawick * (see GetInstance()) */
dc12feb05fcd9f7afbe6c5d830898f028370cafbsf
a299403a6661753a93f4e4f476946c0de4e11311trawick PRBool onMainThread = PR_TRUE;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick if (gEventQ)
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick gEventQ->IsOnCurrentThread (&onMainThread);
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick PRBool timerStarted = PR_FALSE;
0af58edfee6112cc3399e0e693340e525b96ab1ctrawick
6ca0e6973c8176100f4a426444823ae5e777e28fsascha /* sTimes is null if this call originates from
39a5e6b885a8705613e73c368e063547ec2bf813sf * FactoryDestructor() */
d0211374a981173d4537e3f919c708b5f64040fcgstein if (sTimer != NULL)
d0211374a981173d4537e3f919c708b5f64040fcgstein {
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb LogFlowFunc (("Last VirtualBox instance was released.\n"));
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb LogFlowFunc (("Scheduling server shutdown in %d ms...\n",
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb VBoxSVC_ShutdownDelay));
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb /* make sure the previous timer (if any) is stopped;
0368b03c62a6bd2af3b5c2077fdb2257032aebf6sf * otherwise RTTimerStart() will definitely fail. */
0368b03c62a6bd2af3b5c2077fdb2257032aebf6sf RTTimerStop (sTimer);
0368b03c62a6bd2af3b5c2077fdb2257032aebf6sf
d0211374a981173d4537e3f919c708b5f64040fcgstein int vrc = RTTimerStart (sTimer, uint64_t (VBoxSVC_ShutdownDelay) * 1000000);
d0211374a981173d4537e3f919c708b5f64040fcgstein AssertRC (vrc);
d0211374a981173d4537e3f919c708b5f64040fcgstein timerStarted = SUCCEEDED (vrc);
212c401808586d1fecd0e639ec1697229674a151sf }
212c401808586d1fecd0e639ec1697229674a151sf else
d0211374a981173d4537e3f919c708b5f64040fcgstein {
39a5e6b885a8705613e73c368e063547ec2bf813sf LogFlowFunc (("Last VirtualBox instance was released "
39a5e6b885a8705613e73c368e063547ec2bf813sf "on XPCOM shutdown.\n"));
39a5e6b885a8705613e73c368e063547ec2bf813sf Assert (onMainThread);
39a5e6b885a8705613e73c368e063547ec2bf813sf }
39a5e6b885a8705613e73c368e063547ec2bf813sf
39a5e6b885a8705613e73c368e063547ec2bf813sf if (!timerStarted)
39a5e6b885a8705613e73c368e063547ec2bf813sf {
39a5e6b885a8705613e73c368e063547ec2bf813sf if (!onMainThread)
39a5e6b885a8705613e73c368e063547ec2bf813sf {
6ca0e6973c8176100f4a426444823ae5e777e28fsascha /* Failed to start the timer, post the shutdown event
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * manually if not on the main thread alreay. */
e379fc6d7f79163700290d92ce75deb4f3005301jerenkrantz ShutdownTimer (NULL, NULL);
54e36aed6866e09e1a572dc84996e93cdb487b7bsf }
7a9190dfde3a06f271dc6c4f4830c1a5440efef8fanf else
d0211374a981173d4537e3f919c708b5f64040fcgstein {
739cc2257378b5b9092669d142dbf5d41b6f75b7trawick /* Here we come if:
212c401808586d1fecd0e639ec1697229674a151sf *
e68a106d8ca66ed58b44bca438b13838ddd4f2b8sf * a) gEventQ is 0 which means either FactoryDestructor() is called
e68a106d8ca66ed58b44bca438b13838ddd4f2b8sf * or the IPC/DCONNECT shutdown sequence is initiated by the
0a2212da6843659e65c378c5201e6612be625731sf * XPCOM shutdown routine (NS_ShutdownXPCOM()), which always
e68a106d8ca66ed58b44bca438b13838ddd4f2b8sf * happens on the main thread.
e68a106d8ca66ed58b44bca438b13838ddd4f2b8sf *
739cc2257378b5b9092669d142dbf5d41b6f75b7trawick * b) gEventQ has reported we're on the main thread. This means
739cc2257378b5b9092669d142dbf5d41b6f75b7trawick * that DestructEventHandler() has been called, but another
e68a106d8ca66ed58b44bca438b13838ddd4f2b8sf * client was faster and requested VirtualBox again.
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb *
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung * In either case, there is nothing to do.
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb *
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb * Note: case b) is actually no more valid since we don't
2c294c31addd5c957bafe6e78c4a30d423ad6e80rbb * call Release() from DestructEventHandler() in this case
212c401808586d1fecd0e639ec1697229674a151sf * any more. Thus, we assert below.
212c401808586d1fecd0e639ec1697229674a151sf */
212c401808586d1fecd0e639ec1697229674a151sf
68de907078051f290050b089c0570d8167b3500dgstein Assert (gEventQ == NULL);
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung }
68de907078051f290050b089c0570d8167b3500dgstein }
d0211374a981173d4537e3f919c708b5f64040fcgstein }
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0211374a981173d4537e3f919c708b5f64040fcgstein return count;
212c401808586d1fecd0e639ec1697229674a151sf }
212c401808586d1fecd0e639ec1697229674a151sf
68de907078051f290050b089c0570d8167b3500dgstein class MaybeQuitEvent : public MyEvent
212c401808586d1fecd0e639ec1697229674a151sf {
d0211374a981173d4537e3f919c708b5f64040fcgstein /* called on the main thread */
68de907078051f290050b089c0570d8167b3500dgstein void *handler()
d0211374a981173d4537e3f919c708b5f64040fcgstein {
54e36aed6866e09e1a572dc84996e93cdb487b7bsf LogFlowFunc (("\n"));
54e36aed6866e09e1a572dc84996e93cdb487b7bsf
212c401808586d1fecd0e639ec1697229674a151sf Assert (RTCritSectIsInitialized (&sLock));
212c401808586d1fecd0e639ec1697229674a151sf
d0211374a981173d4537e3f919c708b5f64040fcgstein /* stop accepting GetInstance() requests on other threads during
855aa233fcb7b51d738fca5c36585646ae1960c3jerenkrantz * possible destruction */
855aa233fcb7b51d738fca5c36585646ae1960c3jerenkrantz RTCritSectEnter (&sLock);
855aa233fcb7b51d738fca5c36585646ae1960c3jerenkrantz
f0845d140b11c340e910322f5fb4469bf5c98092sf nsrefcnt count = 0;
f0845d140b11c340e910322f5fb4469bf5c98092sf
f0845d140b11c340e910322f5fb4469bf5c98092sf /* sInstance is NULL here if it was deleted immediately after
f0845d140b11c340e910322f5fb4469bf5c98092sf * creation due to initialization error. See GetInstance(). */
f0845d140b11c340e910322f5fb4469bf5c98092sf if (sInstance != NULL)
f0845d140b11c340e910322f5fb4469bf5c98092sf {
f0845d140b11c340e910322f5fb4469bf5c98092sf /* Release the guard reference added in GetInstance() */
f0845d140b11c340e910322f5fb4469bf5c98092sf count = sInstance->Release();
f0845d140b11c340e910322f5fb4469bf5c98092sf }
f0845d140b11c340e910322f5fb4469bf5c98092sf
f0845d140b11c340e910322f5fb4469bf5c98092sf if (count == 0)
f0845d140b11c340e910322f5fb4469bf5c98092sf {
f0845d140b11c340e910322f5fb4469bf5c98092sf if (gAutoShutdown)
f0845d140b11c340e910322f5fb4469bf5c98092sf {
f0845d140b11c340e910322f5fb4469bf5c98092sf Assert (sInstance == NULL);
f0845d140b11c340e910322f5fb4469bf5c98092sf LogFlowFunc (("Terminating the server process...\n"));
f0845d140b11c340e910322f5fb4469bf5c98092sf /* make it leave the event loop */
f0845d140b11c340e910322f5fb4469bf5c98092sf gKeepRunning = PR_FALSE;
f0845d140b11c340e910322f5fb4469bf5c98092sf }
855aa233fcb7b51d738fca5c36585646ae1960c3jerenkrantz }
d0211374a981173d4537e3f919c708b5f64040fcgstein else
6ca0e6973c8176100f4a426444823ae5e777e28fsascha {
6ca0e6973c8176100f4a426444823ae5e777e28fsascha /* This condition is quite rare: a new client will have to
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung * connect after this event has been posted to the main queue
6ca0e6973c8176100f4a426444823ae5e777e28fsascha * but before it started to process it. */
df349d5fc2ae7d106b058fe779c7ea4d19ee0588trawick LogFlowFunc (("Destruction is canceled (refcnt=%d).\n", count));
c803b0d84b48f151504b87ec3f196cb433149592rbb }
c803b0d84b48f151504b87ec3f196cb433149592rbb
6ca0e6973c8176100f4a426444823ae5e777e28fsascha RTCritSectLeave (&sLock);
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung return NULL;
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung }
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung };
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
521fc51bdadd21ae8cc2f4a8b354c5fe424a037csf static void ShutdownTimer (PRTTIMER pTimer, void *pvUser)
dc12feb05fcd9f7afbe6c5d830898f028370cafbsf {
0a2212da6843659e65c378c5201e6612be625731sf NOREF (pTimer);
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung NOREF (pvUser);
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
164141857c02667118df04b14fe536d1756f0ef4jorton /* A "too late" event is theoretically possible if somebody
164141857c02667118df04b14fe536d1756f0ef4jorton * manually ended the server after a destruction has been scheduled
164141857c02667118df04b14fe536d1756f0ef4jorton * and this method was so lucky that it got a chance to run before
164141857c02667118df04b14fe536d1756f0ef4jorton * the timer was killed. */
6ca0e6973c8176100f4a426444823ae5e777e28fsascha AssertReturnVoid (gEventQ);
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
a91a466a0d812a522d492c3c182163f73591ca20aaron /* post a quit event to the main queue */
6ca0e6973c8176100f4a426444823ae5e777e28fsascha MaybeQuitEvent *ev = new MaybeQuitEvent();
6ca0e6973c8176100f4a426444823ae5e777e28fsascha nsresult rv = ev->postTo (gEventQ);
6ca0e6973c8176100f4a426444823ae5e777e28fsascha NOREF (rv);
6ca0e6973c8176100f4a426444823ae5e777e28fsascha
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung /* A failure above means we've been already stopped (for example
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung * by Ctrl-C). FactoryDestructor() (NS_ShutdownXPCOM())
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung * will do the job. Nothing to do. */
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung }
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung static NS_IMETHODIMP FactoryConstructor()
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung {
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung LogFlowFunc (("\n"));
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung /* create a critsect to protect object construction */
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung if (VBOX_FAILURE (RTCritSectInit (&sLock)))
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung return NS_ERROR_OUT_OF_MEMORY;
d0211374a981173d4537e3f919c708b5f64040fcgstein
d0211374a981173d4537e3f919c708b5f64040fcgstein int vrc = RTTimerCreateEx (&sTimer, 0, 0, ShutdownTimer, NULL);
686555019e71b355e835166dfefbec33f7fb6f90rjung if (VBOX_FAILURE (vrc))
c71458c4b81237e414c1c774a80da14fe22c52b4jorton {
c71458c4b81237e414c1c774a80da14fe22c52b4jorton LogFlowFunc (("Failed to create a timer! (vrc=%Vrc)\n", vrc));
c71458c4b81237e414c1c774a80da14fe22c52b4jorton return NS_ERROR_FAILURE;
c71458c4b81237e414c1c774a80da14fe22c52b4jorton }
c71458c4b81237e414c1c774a80da14fe22c52b4jorton
212c401808586d1fecd0e639ec1697229674a151sf return NS_OK;
212c401808586d1fecd0e639ec1697229674a151sf }
c71458c4b81237e414c1c774a80da14fe22c52b4jorton
c71458c4b81237e414c1c774a80da14fe22c52b4jorton static NS_IMETHODIMP FactoryDestructor()
c71458c4b81237e414c1c774a80da14fe22c52b4jorton {
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung LogFlowFunc (("\n"));
c71458c4b81237e414c1c774a80da14fe22c52b4jorton
c71458c4b81237e414c1c774a80da14fe22c52b4jorton RTTimerDestroy (sTimer);
c71458c4b81237e414c1c774a80da14fe22c52b4jorton sTimer = NULL;
d0211374a981173d4537e3f919c708b5f64040fcgstein
d0211374a981173d4537e3f919c708b5f64040fcgstein RTCritSectDelete (&sLock);
d0211374a981173d4537e3f919c708b5f64040fcgstein
686555019e71b355e835166dfefbec33f7fb6f90rjung if (sInstance)
d0211374a981173d4537e3f919c708b5f64040fcgstein {
212c401808586d1fecd0e639ec1697229674a151sf /* Either posting a destruction event falied for some reason (most
212c401808586d1fecd0e639ec1697229674a151sf * likely, the quit event has been received before the last release),
d0211374a981173d4537e3f919c708b5f64040fcgstein * or the client has terminated abnormally w/o releasing its
d0211374a981173d4537e3f919c708b5f64040fcgstein * VirtualBox instance (so NS_ShutdownXPCOM() is doing a cleanup).
d0211374a981173d4537e3f919c708b5f64040fcgstein * Release the guard reference we added in GetInstance(). */
98fde0d0b2576b57adedb21b7cb2fbbc765df7e1trawick sInstance->Release();
d0211374a981173d4537e3f919c708b5f64040fcgstein }
d0211374a981173d4537e3f919c708b5f64040fcgstein
d0211374a981173d4537e3f919c708b5f64040fcgstein return NS_OK;
d0211374a981173d4537e3f919c708b5f64040fcgstein }
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung static nsresult GetInstance (VirtualBox **inst)
686555019e71b355e835166dfefbec33f7fb6f90rjung {
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung LogFlowFunc (("Getting VirtualBox object...\n"));
686555019e71b355e835166dfefbec33f7fb6f90rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung RTCritSectEnter (&sLock);
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung int rv = NS_OK;
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung if (sInstance == 0)
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung {
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung LogFlowFunc (("Creating new VirtualBox object...\n"));
d0bec559bdc255b6bfc91251d24c5ea9a10e1b75rjung sInstance = new VirtualBoxClassFactory();
0f47ee3ae8ede113e11f21bf34f3b11161d3e61drasmus if (sInstance)
78e8f0ef042921ab37fdf14ba9babfdd03c8c5dbrbb {
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha /* make an extra AddRef to take the full control
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha * on the VirtualBox destruction (see FinalRelease()) */
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha sInstance->AddRef();
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha sInstance->AddRef(); /* protect FinalConstruct() */
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha rv = sInstance->FinalConstruct();
7bfe076b680e6e591add6cdc754ce9a6910682a2sascha printf ("Informational: VirtualBox object created (rc=%08X).\n", rv);
11ce1f263fdd8812189b85930874943f9fa81bd5fielding if (NS_FAILED (rv))
11ce1f263fdd8812189b85930874943f9fa81bd5fielding {
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand /* On failure diring VirtualBox initialization, delete it
11ce1f263fdd8812189b85930874943f9fa81bd5fielding * immediately on the current thread by releasing all
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand * references in order to properly schedule the server
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand * shutdown. Since the object is fully deleted here, there
11ce1f263fdd8812189b85930874943f9fa81bd5fielding * is a chance to fix the error and request a new
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand * instantiation before the server terminates. However,
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand * the main reason to maintain the shoutdown delay on
8c2ac56b3654234237ca3a943407361c45a3a4d1sf * failure is to let the front-end completely fetch error
292707b9868335763d6f2bb74a263134eeeb8cadkbrand * info from a server-side IVirtualBoxErrorInfo object. */
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand sInstance->Release();
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand sInstance->Release();
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand Assert (sInstance == 0);
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand }
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand else
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand {
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand /* On success, make sure the previous timer is stopped to
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand * cancel a scheduled server termination (if any). */
8c2ac56b3654234237ca3a943407361c45a3a4d1sf RTTimerStop (sTimer);
8c2ac56b3654234237ca3a943407361c45a3a4d1sf }
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand }
8c2ac56b3654234237ca3a943407361c45a3a4d1sf else
8c2ac56b3654234237ca3a943407361c45a3a4d1sf {
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand rv = NS_ERROR_OUT_OF_MEMORY;
8c2ac56b3654234237ca3a943407361c45a3a4d1sf }
8c2ac56b3654234237ca3a943407361c45a3a4d1sf }
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand else
11ce1f263fdd8812189b85930874943f9fa81bd5fielding {
92a2439559cf1161742650ed9c50c6483bd029cemadhum LogFlowFunc (("Using existing VirtualBox object...\n"));
8c2ac56b3654234237ca3a943407361c45a3a4d1sf nsrefcnt count = sInstance->AddRef();
8c2ac56b3654234237ca3a943407361c45a3a4d1sf Assert (count > 1);
8c2ac56b3654234237ca3a943407361c45a3a4d1sf
8c2ac56b3654234237ca3a943407361c45a3a4d1sf if (count == 2)
f16fd56d941828bc839b519f33af547fd72cde51jerenkrantz {
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand LogFlowFunc (("Another client has requested a reference to VirtualBox, "
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand "canceling detruction...\n"));
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand /* make sure the previous timer is stopped */
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand RTTimerStop (sTimer);
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand }
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand }
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand *inst = sInstance;
8c2ac56b3654234237ca3a943407361c45a3a4d1sf
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand RTCritSectLeave (&sLock);
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand return rv;
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand }
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrandprivate:
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand /* Don't be confused that sInstance is of the *ClassFactory type. This is
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand * actually a singleton instance (*ClassFactory inherits the singleton
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand * class; we combined them just for "simplicity" and used "static" for
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand * factory methods. *ClassFactory here is necessary for a couple of extra
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand * methods. */
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand
a4b590ec2d838dde45afa73723862a348e77d933kbrand static VirtualBoxClassFactory *sInstance;
a4b590ec2d838dde45afa73723862a348e77d933kbrand static RTCRITSECT sLock;
c0771b8f1380258edacd34451cf7aef1daa8b0cfkbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand static PRTTIMER sTimer;
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand};
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrandVirtualBoxClassFactory *VirtualBoxClassFactory::sInstance = 0;
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrandRTCRITSECT VirtualBoxClassFactory::sLock = {0};
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrandPRTTIMER VirtualBoxClassFactory::sTimer = NULL;
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrandNS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR_WITH_RC
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand (VirtualBox, VirtualBoxClassFactory::GetInstance)
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand////////////////////////////////////////////////////////////////////////////////
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrandtypedef NSFactoryDestructorProcPtr NSFactoryConsructorProcPtr;
cda405bd389d0b9a161ba98f4a10caeebd24d7dfkbrand
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand/**
8c2ac56b3654234237ca3a943407361c45a3a4d1sf * Enhanced module component information structure.
183fa93e80381d4a172fde145fc796b3f6906f4cwrowe * nsModuleComponentInfo lacks the factory construction callback,
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand * here we add it. This callback is called by NS_NewMyFactory() after
ac0c8366f49659958162b4bfb3d625a77343eda3kbrand * a nsMyFactory instance is successfully created.
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand */
da482ec6da90f2609367d840933bedd9e90e11b6wrowestruct nsMyModuleComponentInfo : nsModuleComponentInfo
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand{
da482ec6da90f2609367d840933bedd9e90e11b6wrowe nsMyModuleComponentInfo () {}
ac0c8366f49659958162b4bfb3d625a77343eda3kbrand nsMyModuleComponentInfo (int) {}
292707b9868335763d6f2bb74a263134eeeb8cadkbrand
92a2439559cf1161742650ed9c50c6483bd029cemadhum nsMyModuleComponentInfo (
292707b9868335763d6f2bb74a263134eeeb8cadkbrand const char* aDescription,
292707b9868335763d6f2bb74a263134eeeb8cadkbrand const nsCID& aCID,
292707b9868335763d6f2bb74a263134eeeb8cadkbrand const char* aContractID,
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand NSConstructorProcPtr aConstructor,
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand NSRegisterSelfProcPtr aRegisterSelfProc,
a4b590ec2d838dde45afa73723862a348e77d933kbrand NSUnregisterSelfProcPtr aUnregisterSelfProc,
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand NSFactoryDestructorProcPtr aFactoryDestructor,
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand NSGetInterfacesProcPtr aGetInterfacesProc,
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand NSGetLanguageHelperProcPtr aGetLanguageHelperProc,
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand nsIClassInfo ** aClassInfoGlobal,
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand PRUint32 aFlags,
183fa93e80381d4a172fde145fc796b3f6906f4cwrowe NSFactoryConsructorProcPtr aFactoryConstructor)
8c2ac56b3654234237ca3a943407361c45a3a4d1sf {
8c2ac56b3654234237ca3a943407361c45a3a4d1sf mDescription = aDescription;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand mCID = aCID;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand mContractID = aContractID;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand mConstructor = aConstructor;
8c2ac56b3654234237ca3a943407361c45a3a4d1sf mRegisterSelfProc = aRegisterSelfProc;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand mUnregisterSelfProc = aUnregisterSelfProc;
8c2ac56b3654234237ca3a943407361c45a3a4d1sf mFactoryDestructor = aFactoryDestructor;
292707b9868335763d6f2bb74a263134eeeb8cadkbrand mGetInterfacesProc = aGetInterfacesProc;
292707b9868335763d6f2bb74a263134eeeb8cadkbrand mGetLanguageHelperProc = aGetLanguageHelperProc;
8c2ac56b3654234237ca3a943407361c45a3a4d1sf mClassInfoGlobal = aClassInfoGlobal;
11ce1f263fdd8812189b85930874943f9fa81bd5fielding mFlags = aFlags;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand mFactoryConstructor = aFactoryConstructor;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand }
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand /** (optional) Factory Construction Callback */
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand NSFactoryConsructorProcPtr mFactoryConstructor;
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand};
4281cf6a722c99ae21394dc2000bd48efcebdb3akbrand
92a2439559cf1161742650ed9c50c6483bd029cemadhum////////////////////////////////////////////////////////////////////////////////
11ce1f263fdd8812189b85930874943f9fa81bd5fielding
11ce1f263fdd8812189b85930874943f9fa81bd5fieldingstatic const nsMyModuleComponentInfo components[] =
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf{
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf nsMyModuleComponentInfo (
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf "VirtualBox component",
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf (nsCID) NS_VIRTUALBOX_CID,
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf NS_VIRTUALBOX_CONTRACTID,
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf VirtualBoxConstructor, // constructor funcion
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf NULL, // registration function
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf NULL, // deregistration function
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf VirtualBoxClassFactory::FactoryDestructor, // factory destructor function
212c401808586d1fecd0e639ec1697229674a151sf NS_CI_INTERFACE_GETTER_NAME(VirtualBox),
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf NULL, // language helper
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand &NS_CLASSINFO_NAME(VirtualBox),
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf 0, // flags
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf VirtualBoxClassFactory::FactoryConstructor // factory constructor function
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf )
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf};
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf/////////////////////////////////////////////////////////////////////////////
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf/**
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * Generic component factory.
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf *
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * The code below is stolen from nsGenericFactory.h / nsGenericFactory.cpp,
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * because we get a segmentation fault for some unknown reason when VBoxSVC
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * starts up (somewhere during the initialization of the libipcdc.so module)
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * when we just reference XPCOM's NS_NewGenericFactory() from here (i.e. even
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * before actually calling it) and run VBoxSVC using the debug XPCOM
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * libraries.
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf *
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf * Actually, I know why, but I find it too stupid even to discuss.
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf */
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sfclass nsMyFactory : public nsIGenericFactory, public nsIClassInfo {
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sfpublic:
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf NS_DEFINE_STATIC_CID_ACCESSOR(NS_GENERICFACTORY_CID);
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf nsMyFactory(const nsModuleComponentInfo *info = NULL);
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
708fec3f1a67942d7ee00b8c57fb0aa4fb40dde2kbrand NS_DECL_ISUPPORTS
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf NS_DECL_NSICLASSINFO
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf /* nsIGenericFactory methods */
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton NS_IMETHOD SetComponentInfo(const nsModuleComponentInfo *info);
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton NS_IMETHOD GetComponentInfo(const nsModuleComponentInfo **infop);
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton NS_IMETHOD CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult);
d13139765fae8fcde58a1c767716d2663a3ebfd9jorton
d13139765fae8fcde58a1c767716d2663a3ebfd9jorton NS_IMETHOD LockFactory(PRBool aLock);
d13139765fae8fcde58a1c767716d2663a3ebfd9jorton
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton static NS_METHOD Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr);
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jortonprivate:
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton ~nsMyFactory();
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton const nsModuleComponentInfo *mInfo;
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton};
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jortonnsMyFactory::nsMyFactory(const nsModuleComponentInfo *info)
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton : mInfo(info)
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton{
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton if (mInfo && mInfo->mClassInfoGlobal)
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton *mInfo->mClassInfoGlobal = NS_STATIC_CAST(nsIClassInfo *, this);
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton}
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jortonnsMyFactory::~nsMyFactory()
265e9c84e4ee23f41ac25622f8b8b3f481bbf281jorton{
047cb8df15a408e0c0c6125c4a351b35aa3c6e82sf if (mInfo) {
9d432e23f6025b78322cbee43e8e89262a108e5eaaron if (mInfo->mFactoryDestructor)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron mInfo->mFactoryDestructor();
9d432e23f6025b78322cbee43e8e89262a108e5eaaron if (mInfo->mClassInfoGlobal)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron *mInfo->mClassInfoGlobal = 0;
9d432e23f6025b78322cbee43e8e89262a108e5eaaron }
9d432e23f6025b78322cbee43e8e89262a108e5eaaron}
9d432e23f6025b78322cbee43e8e89262a108e5eaaron
9d432e23f6025b78322cbee43e8e89262a108e5eaaronNS_IMPL_THREADSAFE_ISUPPORTS3(nsMyFactory,
9d432e23f6025b78322cbee43e8e89262a108e5eaaron nsIGenericFactory,
9d432e23f6025b78322cbee43e8e89262a108e5eaaron nsIFactory,
9d432e23f6025b78322cbee43e8e89262a108e5eaaron nsIClassInfo)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron
9d432e23f6025b78322cbee43e8e89262a108e5eaaronNS_IMETHODIMP nsMyFactory::CreateInstance(nsISupports *aOuter,
9d432e23f6025b78322cbee43e8e89262a108e5eaaron REFNSIID aIID, void **aResult)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron{
9d432e23f6025b78322cbee43e8e89262a108e5eaaron if (mInfo->mConstructor)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron return mInfo->mConstructor(aOuter, aIID, aResult);
e213c8a802baea59a07c8a6d089f94a678f70b96aaron
9d432e23f6025b78322cbee43e8e89262a108e5eaaron return NS_ERROR_FACTORY_NOT_REGISTERED;
9d432e23f6025b78322cbee43e8e89262a108e5eaaron}
9d432e23f6025b78322cbee43e8e89262a108e5eaaron
9d432e23f6025b78322cbee43e8e89262a108e5eaaronNS_IMETHODIMP nsMyFactory::LockFactory(PRBool aLock)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron{
9d432e23f6025b78322cbee43e8e89262a108e5eaaron // XXX do we care if (mInfo->mFlags & THREADSAFE)?
9d432e23f6025b78322cbee43e8e89262a108e5eaaron return NS_OK;
9d432e23f6025b78322cbee43e8e89262a108e5eaaron}
9d432e23f6025b78322cbee43e8e89262a108e5eaaron
9d432e23f6025b78322cbee43e8e89262a108e5eaaronNS_IMETHODIMP nsMyFactory::GetInterfaces(PRUint32 *countp,
9d432e23f6025b78322cbee43e8e89262a108e5eaaron nsIID* **array)
9d432e23f6025b78322cbee43e8e89262a108e5eaaron{
9d432e23f6025b78322cbee43e8e89262a108e5eaaron if (!mInfo->mGetInterfacesProc) {
9d432e23f6025b78322cbee43e8e89262a108e5eaaron *countp = 0;
9d432e23f6025b78322cbee43e8e89262a108e5eaaron *array = nsnull;
9d432e23f6025b78322cbee43e8e89262a108e5eaaron return NS_OK;
9d432e23f6025b78322cbee43e8e89262a108e5eaaron }
cb6310f4ea253e5706c71958b0943f142cbda667jorton return mInfo->mGetInterfacesProc(countp, array);
cb6310f4ea253e5706c71958b0943f142cbda667jorton}
cb6310f4ea253e5706c71958b0943f142cbda667jorton
cb6310f4ea253e5706c71958b0943f142cbda667jortonNS_IMETHODIMP nsMyFactory::GetHelperForLanguage(PRUint32 language,
cb6310f4ea253e5706c71958b0943f142cbda667jorton nsISupports **helper)
cb6310f4ea253e5706c71958b0943f142cbda667jorton{
cb6310f4ea253e5706c71958b0943f142cbda667jorton if (mInfo->mGetLanguageHelperProc)
cb6310f4ea253e5706c71958b0943f142cbda667jorton return mInfo->mGetLanguageHelperProc(language, helper);
cb6310f4ea253e5706c71958b0943f142cbda667jorton *helper = nsnull;
cb6310f4ea253e5706c71958b0943f142cbda667jorton return NS_OK;
cb6310f4ea253e5706c71958b0943f142cbda667jorton}
cb6310f4ea253e5706c71958b0943f142cbda667jorton
cb6310f4ea253e5706c71958b0943f142cbda667jortonNS_IMETHODIMP nsMyFactory::GetContractID(char **aContractID)
cb6310f4ea253e5706c71958b0943f142cbda667jorton{
cb6310f4ea253e5706c71958b0943f142cbda667jorton if (mInfo->mContractID) {
cb6310f4ea253e5706c71958b0943f142cbda667jorton *aContractID = (char *)nsMemory::Alloc(strlen(mInfo->mContractID) + 1);
cb6310f4ea253e5706c71958b0943f142cbda667jorton if (!*aContractID)
cb6310f4ea253e5706c71958b0943f142cbda667jorton return NS_ERROR_OUT_OF_MEMORY;
cb6310f4ea253e5706c71958b0943f142cbda667jorton strcpy(*aContractID, mInfo->mContractID);
cb6310f4ea253e5706c71958b0943f142cbda667jorton } else {
cb6310f4ea253e5706c71958b0943f142cbda667jorton *aContractID = nsnull;
cb6310f4ea253e5706c71958b0943f142cbda667jorton }
cb6310f4ea253e5706c71958b0943f142cbda667jorton return NS_OK;
cb6310f4ea253e5706c71958b0943f142cbda667jorton}
cb6310f4ea253e5706c71958b0943f142cbda667jorton
cb6310f4ea253e5706c71958b0943f142cbda667jortonNS_IMETHODIMP nsMyFactory::GetClassDescription(char * *aClassDescription)
cb6310f4ea253e5706c71958b0943f142cbda667jorton{
cb6310f4ea253e5706c71958b0943f142cbda667jorton if (mInfo->mDescription) {
cb6310f4ea253e5706c71958b0943f142cbda667jorton *aClassDescription = (char *)
cb6310f4ea253e5706c71958b0943f142cbda667jorton nsMemory::Alloc(strlen(mInfo->mDescription) + 1);
cb6310f4ea253e5706c71958b0943f142cbda667jorton if (!*aClassDescription)
cb6310f4ea253e5706c71958b0943f142cbda667jorton return NS_ERROR_OUT_OF_MEMORY;
cb6310f4ea253e5706c71958b0943f142cbda667jorton strcpy(*aClassDescription, mInfo->mDescription);
cb6310f4ea253e5706c71958b0943f142cbda667jorton } else {
cb6310f4ea253e5706c71958b0943f142cbda667jorton *aClassDescription = nsnull;
cb6310f4ea253e5706c71958b0943f142cbda667jorton }
cb6310f4ea253e5706c71958b0943f142cbda667jorton return NS_OK;
cb6310f4ea253e5706c71958b0943f142cbda667jorton}
cb6310f4ea253e5706c71958b0943f142cbda667jorton
cb6310f4ea253e5706c71958b0943f142cbda667jortonNS_IMETHODIMP nsMyFactory::GetClassID(nsCID * *aClassID)
cb6310f4ea253e5706c71958b0943f142cbda667jorton{
e36cfe8caf074cce72f1e4136894608665b7fbd8colm *aClassID =
e36cfe8caf074cce72f1e4136894608665b7fbd8colm NS_REINTERPRET_CAST(nsCID*,
e36cfe8caf074cce72f1e4136894608665b7fbd8colm nsMemory::Clone(&mInfo->mCID, sizeof mInfo->mCID));
e36cfe8caf074cce72f1e4136894608665b7fbd8colm if (! *aClassID)
e36cfe8caf074cce72f1e4136894608665b7fbd8colm return NS_ERROR_OUT_OF_MEMORY;
e36cfe8caf074cce72f1e4136894608665b7fbd8colm return NS_OK;
e36cfe8caf074cce72f1e4136894608665b7fbd8colm}
e36cfe8caf074cce72f1e4136894608665b7fbd8colm
e36cfe8caf074cce72f1e4136894608665b7fbd8colmNS_IMETHODIMP nsMyFactory::GetClassIDNoAlloc(nsCID *aClassID)
0b083c7f392e96303fc8aee44571631964778b7cjim{
e36cfe8caf074cce72f1e4136894608665b7fbd8colm *aClassID = mInfo->mCID;
e36cfe8caf074cce72f1e4136894608665b7fbd8colm return NS_OK;
e36cfe8caf074cce72f1e4136894608665b7fbd8colm}
e36cfe8caf074cce72f1e4136894608665b7fbd8colm
0b083c7f392e96303fc8aee44571631964778b7cjimNS_IMETHODIMP nsMyFactory::GetImplementationLanguage(PRUint32 *langp)
7f887fe6513b42d0a2e0ef8ca94aa51f59cfaba7fuankg{
e36cfe8caf074cce72f1e4136894608665b7fbd8colm *langp = nsIProgrammingLanguage::CPLUSPLUS;
0b083c7f392e96303fc8aee44571631964778b7cjim return NS_OK;
e36cfe8caf074cce72f1e4136894608665b7fbd8colm}
e36cfe8caf074cce72f1e4136894608665b7fbd8colm
e36cfe8caf074cce72f1e4136894608665b7fbd8colmNS_IMETHODIMP nsMyFactory::GetFlags(PRUint32 *flagsp)
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf{
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf *flagsp = mInfo->mFlags;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf return NS_OK;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf}
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf// nsIGenericFactory: component-info accessors
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sfNS_IMETHODIMP nsMyFactory::SetComponentInfo(const nsModuleComponentInfo *info)
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf{
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf if (mInfo && mInfo->mClassInfoGlobal)
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf *mInfo->mClassInfoGlobal = 0;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf mInfo = info;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf if (mInfo && mInfo->mClassInfoGlobal)
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf *mInfo->mClassInfoGlobal = NS_STATIC_CAST(nsIClassInfo *, this);
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf return NS_OK;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf}
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sfNS_IMETHODIMP nsMyFactory::GetComponentInfo(const nsModuleComponentInfo **infop)
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf{
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf *infop = mInfo;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf return NS_OK;
65f6e321663b3fd0f93d8b47b4df05f189de6cf1sf}
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sfNS_METHOD nsMyFactory::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf{
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf // sorry, aggregation not spoken here.
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf nsresult res = NS_ERROR_NO_AGGREGATION;
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf if (outer == NULL) {
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf nsMyFactory* factory = new nsMyFactory;
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf if (factory != NULL) {
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf res = factory->QueryInterface(aIID, aInstancePtr);
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf if (res != NS_OK)
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf delete factory;
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf } else {
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf res = NS_ERROR_OUT_OF_MEMORY;
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf }
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf }
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf return res;
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf}
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf/**
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf * Instantiates a new factory and calls
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf * nsMyModuleComponentInfo::mFactoryConstructor.
b8f1ab8691c3c3aad22102f6926dde31b7e45db9sf */
nsresult
NS_NewMyFactory(nsIGenericFactory* *result,
const nsMyModuleComponentInfo *info)
{
nsresult rv;
nsMyFactory* fact;
rv = nsMyFactory::Create(NULL, NS_GET_IID(nsIGenericFactory), (void**)&fact);
if (NS_FAILED(rv)) return rv;
rv = fact->SetComponentInfo(info);
if (NS_FAILED(rv)) goto error;
if (info && info->mFactoryConstructor) {
rv = info->mFactoryConstructor();
if (NS_FAILED(rv)) goto error;
}
*result = fact;
return rv;
error:
NS_RELEASE(fact);
return rv;
}
/////////////////////////////////////////////////////////////////////////////
/**
* Hhelper function to register self components upon start-up
* of the out-of-proc server.
*/
static nsresult
RegisterSelfComponents (nsIComponentRegistrar *registrar,
const nsMyModuleComponentInfo *components,
PRUint32 count)
{
nsresult rc = NS_OK;
const nsMyModuleComponentInfo *info = components;
for (PRUint32 i = 0; i < count && NS_SUCCEEDED (rc); i++, info++)
{
/* skip components w/o a constructor */
if (!info->mConstructor) continue;
/* create a new generic factory for a component and register it */
nsIGenericFactory *factory;
rc = NS_NewGenericFactory (&factory, info);
rc = NS_NewMyFactory (&factory, info);
if (NS_SUCCEEDED (rc))
{
rc = registrar->RegisterFactory (info->mCID,
info->mDescription,
info->mContractID,
factory);
factory->Release();
}
}
return rc;
}
/////////////////////////////////////////////////////////////////////////////
static ipcIService *gIpcServ = nsnull;
static char *pszPidFile = NULL;
class ForceQuitEvent : public MyEvent
{
void *handler()
{
LogFlowFunc (("\n"));
gKeepRunning = PR_FALSE;
if (pszPidFile)
RTFileDelete(pszPidFile);
return NULL;
}
};
static void signal_handler (int sig)
{
if (gEventQ && gKeepRunning)
{
/* post a quit event to the queue */
ForceQuitEvent *ev = new ForceQuitEvent();
ev->postTo (gEventQ);
}
}
#if defined(USE_BACKTRACE)
/**
* the signal handler that prints out a backtrace of the call stack.
* the code is taken from http://www.linuxjournal.com/article/6391.
*/
static void bt_sighandler (int sig, siginfo_t *info, void *secret)
{
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
ucontext_t *uc = (ucontext_t *)secret;
// Do something useful with siginfo_t
if (sig == SIGSEGV)
Log (("Got signal %d, faulty address is %p, from %p\n",
sig, info->si_addr, uc->uc_mcontext.gregs[REG_PC]));
else
Log (("Got signal %d\n", sig));
trace_size = backtrace (trace, 16);
// overwrite sigaction with caller's address
trace[1] = (void *) uc->uc_mcontext.gregs [REG_PC];
messages = backtrace_symbols (trace, trace_size);
// skip first stack frame (points here)
Log (("[bt] Execution path:\n"));
for (i = 1; i < trace_size; ++i)
Log (("[bt] %s\n", messages[i]));
exit (0);
}
#endif
int main (int argc, char **argv)
{
const struct option options[] =
{
{ "automate", no_argument, NULL, 'a' },
{ "daemonize", no_argument, NULL, 'd' },
{ "pidfile", required_argument, NULL, 'p' },
{ NULL, 0, NULL, 0 }
};
int c;
bool fDaemonize = false;
for (;;)
{
c = getopt_long(argc, argv, "", options, NULL);
if (c == -1)
break;
switch (c)
{
case 'a':
{
/* --automate mode means we are started by XPCOM on
* demand. Daemonize ourselves and activate
* auto-shutdown. */
gAutoShutdown = true;
fDaemonize = true;
break;
}
case 'd':
{
fDaemonize = true;
break;
}
case 'p':
{
pszPidFile = optarg;
break;
}
default:
{
/* exit on invalid options */
return 1;
}
}
}
static RTFILE pidFile = NIL_RTFILE;
#ifdef __OS2__
/* nothing to do here, the process is supposed to be already
* started daemonized when it is necessary */
NOREF(fDaemonize);
#else // ifdef __OS2__
static int daemon_pipe_fds[2];
if (fDaemonize)
{
/* create a pipe for communication between child and parent */
if (pipe(daemon_pipe_fds) < 0)
{
printf("ERROR: pipe() failed (errno = %d)\n", errno);
return 1;
}
pid_t childpid = fork();
if (childpid == -1)
{
printf("ERROR: fork() failed (errno = %d)\n", errno);
return 1;
}
if (childpid != 0)
{
/* we're the parent process */
bool fSuccess = false;
/* close the writing end of the pipe */
close(daemon_pipe_fds[1]);
/* try to read a message from the pipe */
char msg[10] = {0}; /* initialize so it's NULL terminated */
if (read(daemon_pipe_fds[0], msg, sizeof(msg)) > 0)
{
if (strcmp(msg, "READY") == 0)
fSuccess = true;
else
printf ("ERROR: Unknown message from child "
"process (%s)\n", msg);
}
else
printf ("ERROR: 0 bytes read from child process\n");
/* close the reading end of the pipe as well and exit */
close(daemon_pipe_fds[0]);
return fSuccess ? 0 : 1;
}
/* we're the child process */
/* Create a new SID for the child process */
pid_t sid = setsid();
if (sid < 0)
{
printf("ERROR: setsid() failed (errno = %d)\n", errno);
return 1;
}
/* Redirect standard i/o streams to /dev/null */
freopen ("/dev/null", "r", stdin);
freopen ("/dev/null", "w", stdout);
freopen ("/dev/null", "w", stderr);
/* close the reading end of the pipe */
close(daemon_pipe_fds[0]);
}
#endif // ifdef __OS2__
#if defined(USE_BACKTRACE)
{
/* install our signal handler to backtrace the call stack */
struct sigaction sa;
sa.sa_sigaction = bt_sighandler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction (SIGSEGV, &sa, NULL);
sigaction (SIGBUS, &sa, NULL);
sigaction (SIGUSR1, &sa, NULL);
}
#endif
/*
* Initialize the VBox runtime without loading
* the support driver
*/
RTR3Init(false);
nsresult rc;
do
{
rc = com::Initialize();
if (NS_FAILED (rc))
{
printf ("ERROR: Failed to initialize XPCOM! (rc=%08X)\n", rc);
break;
}
nsCOMPtr <nsIComponentRegistrar> registrar;
rc = NS_GetComponentRegistrar (getter_AddRefs (registrar));
if (NS_FAILED (rc))
{
printf ("ERROR: Failed to get component registrar! (rc=%08X)\n", rc);
break;
}
registrar->AutoRegister (nsnull);
rc = RegisterSelfComponents (registrar, components,
NS_ARRAY_LENGTH (components));
if (NS_FAILED (rc))
{
printf ("ERROR: Failed to register server components! (rc=%08X)\n", rc);
break;
}
/* get the main thread's event queue (afaik, the dconnect service always
* gets created upon XPCOM startup, so it will use the main (this)
* thread's event queue to receive IPC events) */
rc = NS_GetMainEventQ (&gEventQ);
if (NS_FAILED (rc))
{
printf ("ERROR: Failed to get the main event queue! (rc=%08X)\n", rc);
break;
}
nsCOMPtr<ipcIService> ipcServ (do_GetService(IPC_SERVICE_CONTRACTID, &rc));
if (NS_FAILED (rc))
{
printf ("ERROR: Failed to get IPC service! (rc=%08X)\n", rc);
break;
}
NS_ADDREF (gIpcServ = ipcServ);
LogFlowFunc (("Will use \"%s\" as server name.\n", VBOXSVC_IPC_NAME));
rc = gIpcServ->AddName (VBOXSVC_IPC_NAME);
if (NS_FAILED (rc))
{
printf ("ERROR: Failed to register VirtualBoxServer! (rc=%08X)\n", rc);
NS_RELEASE (gIpcServ);
break;
}
{
/* setup signal handling to convert some signals to a quit event */
struct sigaction sa;
sa.sa_handler = signal_handler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sigaction (SIGINT, &sa, NULL);
sigaction (SIGQUIT, &sa, NULL);
sigaction (SIGTERM, &sa, NULL);
sigaction (SIGTRAP, &sa, NULL);
}
{
char szBuf[80];
int iSize;
iSize = snprintf (szBuf, sizeof(szBuf),
"innotek VirtualBox XPCOM Server Version "
VBOX_VERSION_STRING);
for (int i=iSize; i>0; i--)
putchar('*');
printf ("\n%s\n", szBuf);
printf ("(C) 2004-2007 innotek GmbH\n");
printf ("All rights reserved.\n");
#ifdef DEBUG
printf ("Debug version.\n");
#endif
#if 0
/* in my opinion two lines enclosing the text look better */
for (int i=iSize; i>0; i--)
putchar('*');
putchar('\n');
#endif
}
if (fDaemonize)
{
printf ("\nStarting event loop....\n[send TERM signal to quit]\n");
#ifndef __OS2__
/* now we're ready, signal the parent process */
write(daemon_pipe_fds[1], "READY", strlen("READY"));
#endif
}
else
{
printf ("\nStarting event loop....\n[press Ctrl-C to quit]\n");
}
if (pszPidFile)
{
char szBuf[32];
const char *lf = "\n";
RTFileOpen(&pidFile, pszPidFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE);
RTStrFormatNumber(szBuf, getpid(), 10, 0, 0, 0);
RTFileWrite(pidFile, szBuf, strlen(szBuf), NULL);
RTFileWrite(pidFile, lf, strlen(lf), NULL);
RTFileClose(pidFile);
}
PLEvent *ev;
while (gKeepRunning)
{
gEventQ->WaitForEvent (&ev);
gEventQ->HandleEvent (ev);
}
gIpcServ->RemoveName (VBOXSVC_IPC_NAME);
/* stop accepting new events */
gEventQ->StopAcceptingEvents();
/* process any remaining events */
gEventQ->ProcessPendingEvents();
printf ("Terminated event loop.\n");
}
while (0); // this scopes the nsCOMPtrs
NS_IF_RELEASE (gIpcServ);
NS_IF_RELEASE (gEventQ);
/* no nsCOMPtrs are allowed to be alive when you call com::Shutdown(). */
LogFlowFunc (("Calling com::Shutdown()...\n"));
rc = com::Shutdown();
LogFlowFunc (("Finished com::Shutdown() (rc=%08X)\n", rc));
if (NS_FAILED (rc))
printf ("ERROR: Failed to shutdown XPCOM! (rc=%08X)\n", rc);
printf ("XPCOM server has shutdown.\n");
if (pszPidFile)
{
RTFileDelete(pszPidFile);
}
if (fDaemonize)
{
#ifndef __OS2__
/* close writing end of the pipe as well */
close(daemon_pipe_fds[1]);
#endif
}
return 0;
}