init.cpp revision 2fb6af7450c485b20eebec0e6963081826b1f32f
/* $Id$ */
/** @file
* IPRT - Init Ring-3.
*/
/*
* Copyright (C) 2006-2009 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_DEFAULT
#ifdef RT_OS_WINDOWS
# include <process.h>
# include <Windows.h>
#else
# include <unistd.h>
# ifndef RT_OS_OS2
# include <pthread.h>
# include <signal.h>
# include <errno.h>
# define IPRT_USE_SIG_CHILD_DUMMY
# endif
#endif
#ifdef RT_OS_OS2
# include <InnoTekLIBC/fork.h>
# define INCL_DOSMISC
# include <os2.h>
#endif
#include <locale.h>
#include <iprt/initterm.h>
#endif
#include <stdlib.h>
#include "internal/alignmentchecks.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The number of calls to RTR3Init*. */
/** Whether we're currently initializing the IPRT. */
static bool volatile g_fInitializing = false;
/** The process path.
* This is used by RTPathExecDir and RTProcGetExecutablePath and set by rtProcInitName. */
/** The length of g_szrtProcExePath. */
/** The length of directory path component of g_szrtProcExePath. */
/** The offset of the process name into g_szrtProcExePath. */
/** The argument count of the program. */
static int g_crtArgs = -1;
/** The arguments of the program (UTF-8). This is "leaked". */
static char ** g_papszrtArgs;
/** The original argument vector of the program. */
static char ** g_papszrtOrgArgs;
/** Are we running in unobtrusive mode? */
static bool g_fUnobtrusive = false;
/**
* Program start nanosecond TS.
*/
/**
* Program start microsecond TS.
*/
/**
* Program start millisecond TS.
*/
/**
* The process identifier of the running process.
*/
/**
* The current process priority.
*/
/**
* Set if the atexit callback has been called, i.e. indicating
* that the process is terminating.
*/
DECLHIDDEN(bool volatile) g_frtAtExitCalled = false;
#ifdef IPRT_WITH_ALIGNMENT_CHECKS
/**
* Whether alignment checks are enabled.
* This is set if the environment variable IPRT_ALIGNMENT_CHECKS is 1.
*/
RTDATADECL(bool) g_fRTAlignmentChecks = false;
#endif
/**
* atexit callback.
*
* This makes sure any loggers are flushed and will later also work the
* termination callback chain.
*/
static void rtR3ExitCallback(void)
{
ASMAtomicWriteBool(&g_frtAtExitCalled, true);
if (g_cUsers > 0)
{
if (pLogger)
if (pLogger)
}
}
#ifndef RT_OS_WINDOWS
/**
* Fork callback, child context.
*/
static void rtR3ForkChildCallback(void)
{
g_ProcessSelf = getpid();
}
#endif /* RT_OS_WINDOWS */
#ifdef RT_OS_OS2
/** Fork completion callback for OS/2. Only called in the child. */
{
if (!rc)
}
/** Low-level fork callback for OS/2. */
{
return pForkHandle->pfnCompletionCallback(pForkHandle, rtR3ForkOs2ChildCompletionCallback, NULL, __LIBC_FORK_CTX_CHILD);
return 0;
}
# define static static volatile /** @todo _FORK_CHILD1 causes unresolved externals in optimized builds. Fix macro. */
# undef static
#endif /* RT_OS_OS2 */
/**
* Internal worker which initializes or re-initializes the
* program path, name and directory globals.
*
* @returns IPRT status code.
* @param pszProgramPath The program path, NULL if not specified.
*/
static int rtR3InitProgramPath(const char *pszProgramPath)
{
/*
* We're reserving 32 bytes here for file names as what not.
*/
if (!pszProgramPath)
{
if (RT_FAILURE(rc))
return rc;
}
else
{
}
/*
* Parse the name.
*/
return VINF_SUCCESS;
}
/**
* Internal worker which initializes or re-initializes the
* program path, name and directory globals.
*
* @returns IPRT status code.
* @param fFlags Flags, see RTR3INIT_XXX.
* @param cArgs Pointer to the argument count.
* @param ppapszArgs Pointer to the argument vector pointer. NULL
* allowed if @a cArgs is 0.
*/
{
if (cArgs)
{
char **papszOrgArgs = *ppapszArgs;
/*
* Normally we should only be asked to convert arguments once. If we
* are though, it should be the already convered arguments.
*/
if (g_crtArgs != -1)
{
&& g_papszrtArgs == papszOrgArgs,
VERR_WRONG_ORDER); /* only init once! */
return VINF_SUCCESS;
}
/*
* Convert the arguments.
*/
if (!papszArgs)
return VERR_NO_MEMORY;
for (int i = 0; i < cArgs; i++)
{
if (RT_FAILURE(rc))
{
while (i--)
return rc;
}
}
*ppapszArgs = papszArgs;
}
return VINF_SUCCESS;
}
#ifdef IPRT_USE_SIG_CHILD_DUMMY
/**
* Dummy SIGCHILD handler.
*
* Assigned on rtR3Init only when SIGCHILD handler is set SIGIGN or SIGDEF to
* ensure waitpid works properly for the terminated processes.
*/
static void rtR3SigChildHandler(int iSignal)
{
}
#endif /* IPRT_USE_SIG_CHILD_DUMMY */
/**
* rtR3Init worker.
*/
{
/*
* Init C runtime locale before we do anything that may end up converting
* paths or we'll end up using the "C" locale for path conversion.
*/
/*
* The Process ID.
*/
#ifdef _MSC_VER
#else
g_ProcessSelf = getpid();
#endif
/*
* Disable error popups.
*/
#ifdef RT_OS_WINDOWS
#endif
# ifdef VBOX
/*
* This MUST be done as the very first thing, before any file is opened.
* The log is opened on demand, but the first log entries may be caused
* by rtThreadInit() below.
*/
if ( pszDisableHostCache != NULL
{
}
# endif /* VBOX */
#endif /* !IN_GUEST && !RT_NO_GIP */
/*
* Thread Thread database and adopt the caller thread as 'main'.
* This must be done before everything else or else we'll call into threading
* without having initialized TLS entries and suchlike.
*/
int rc = rtThreadInit();
if (fFlags & RTR3INIT_FLAGS_SUPLIB)
{
/*
* Init GIP first.
* (The more time for updates before real use, the better.)
*/
}
#endif
/*
* The executable path, name and directory. Convert arguments.
*/
/*
* The threading is initialized we can safely sleep a bit if GIP
* needs some time to update itself updating.
*/
{
RTThreadSleep(20);
RTTimeNanoTS();
}
#endif
/*
* Init the program start TSes.
* Do that here to be sure that the GIP time was properly updated the 1st time.
*/
/*
* The remainder cannot easily be undone, so it has to go last.
*/
/* Fork and exit callbacks. */
#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
#endif
#ifdef IPRT_USE_SIG_CHILD_DUMMY
/*
* SIGCHLD must not be ignored (that's default), otherwise posix compliant waitpid
* implementations won't work right.
*/
for (;;)
{
if ( rc != 0
)
break;
/* Try install dummy handler. */
if ( rc != 0
)
break;
/* Race during dynamic load, restore and try again... */
}
#endif /* IPRT_USE_SIG_CHILD_DUMMY */
#ifdef IPRT_WITH_ALIGNMENT_CHECKS
/*
* Enable alignment checks.
*/
&& pszAlignmentChecks[0] == '1'
if (g_fRTAlignmentChecks)
#endif
return VINF_SUCCESS;
}
/**
* Internal initialization worker.
*
* @returns IPRT status code.
* @param fFlags Flags, see RTR3INIT_XXX.
* @param cArgs Pointer to the argument count.
* @param ppapszArgs Pointer to the argument vector pointer. NULL
* allowed if @a cArgs is 0.
* @param pszProgramPath The program path. Pass NULL if we're to figure it
* out ourselves.
*/
{
/* no entry log flow, because prefixes and thread may freak out. */
/*
* Do reference counting, only initialize the first time around.
*
* We are ASSUMING that nobody will be able to race RTR3Init* calls when the
* first one, the real init, is running (second assertion).
*/
if (cUsers != 1)
{
if (fFlags & RTR3INIT_FLAGS_SUPLIB)
#endif
if (!pszProgramPath)
return VINF_SUCCESS;
if (RT_SUCCESS(rc))
return rc;
}
ASMAtomicWriteBool(&g_fInitializing, true);
/*
* Do the initialization.
*/
if (RT_FAILURE(rc))
{
/* failure */
ASMAtomicWriteBool(&g_fInitializing, false);
return rc;
}
/* success */
LogFlow(("rtR3Init: returns VINF_SUCCESS\n"));
ASMAtomicWriteBool(&g_fInitializing, false);
return VINF_SUCCESS;
}
{
}
{
}
{
}
RTR3DECL(int) RTR3InitEx(uint32_t iVersion, uint32_t fFlags, int cArgs, char ***papszArgs, const char *pszProgramPath)
{
}
RTR3DECL(bool) RTR3InitIsUnobtrusive(void)
{
return g_fUnobtrusive;
}
#if 0 /** @todo implement RTR3Term. */
{
}
#endif