[1] | 1 | /* $Id: init.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Init Ring-3.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_DEFAULT
|
---|
[20839] | 42 | #include <iprt/types.h> /* darwin: UINT32_C and others. */
|
---|
| 43 |
|
---|
[3672] | 44 | #ifdef RT_OS_WINDOWS
|
---|
[62592] | 45 | # include <iprt/win/windows.h>
|
---|
[1] | 46 | #else
|
---|
| 47 | # include <unistd.h>
|
---|
[18997] | 48 | # ifndef RT_OS_OS2
|
---|
| 49 | # include <pthread.h>
|
---|
[23919] | 50 | # include <signal.h>
|
---|
| 51 | # include <errno.h>
|
---|
| 52 | # define IPRT_USE_SIG_CHILD_DUMMY
|
---|
[18997] | 53 | # endif
|
---|
[1] | 54 | #endif
|
---|
[18997] | 55 | #ifdef RT_OS_OS2
|
---|
| 56 | # include <InnoTekLIBC/fork.h>
|
---|
[40304] | 57 | # define INCL_DOSMISC
|
---|
| 58 | # include <os2.h>
|
---|
[18997] | 59 | #endif
|
---|
[95806] | 60 | #ifndef IPRT_NO_CRT
|
---|
| 61 | # include <locale.h>
|
---|
| 62 | #endif
|
---|
[1] | 63 |
|
---|
[14831] | 64 | #include <iprt/initterm.h>
|
---|
[14832] | 65 | #include <iprt/asm.h>
|
---|
[1] | 66 | #include <iprt/assert.h>
|
---|
[95806] | 67 | #include <iprt/env.h>
|
---|
[76452] | 68 | #include <iprt/errcore.h>
|
---|
[1] | 69 | #include <iprt/log.h>
|
---|
[38636] | 70 | #include <iprt/mem.h>
|
---|
[14832] | 71 | #include <iprt/path.h>
|
---|
[1] | 72 | #include <iprt/time.h>
|
---|
| 73 | #include <iprt/string.h>
|
---|
| 74 | #include <iprt/param.h>
|
---|
[76409] | 75 | #ifdef RT_OS_WINDOWS
|
---|
[92625] | 76 | # include <iprt/getopt.h>
|
---|
[76409] | 77 | # include <iprt/utf16.h>
|
---|
| 78 | #endif
|
---|
[6677] | 79 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
[1] | 80 | # include <iprt/file.h>
|
---|
| 81 | # include <VBox/sup.h>
|
---|
| 82 | #endif
|
---|
[20821] | 83 | #include <stdlib.h>
|
---|
[18055] | 84 |
|
---|
[46593] | 85 | #include "init.h"
|
---|
[20821] | 86 | #include "internal/alignmentchecks.h"
|
---|
[96442] | 87 | #include "internal/initterm.h"
|
---|
[1] | 88 | #include "internal/path.h"
|
---|
| 89 | #include "internal/process.h"
|
---|
| 90 | #include "internal/thread.h"
|
---|
| 91 | #include "internal/time.h"
|
---|
| 92 |
|
---|
| 93 |
|
---|
[57358] | 94 | /*********************************************************************************************************************************
|
---|
| 95 | * Global Variables *
|
---|
| 96 | *********************************************************************************************************************************/
|
---|
[44486] | 97 | /** The IPRT init flags. */
|
---|
| 98 | static uint32_t g_fInitFlags;
|
---|
| 99 |
|
---|
[38636] | 100 | /** The argument count of the program. */
|
---|
[38639] | 101 | static int g_crtArgs = -1;
|
---|
[38636] | 102 | /** The arguments of the program (UTF-8). This is "leaked". */
|
---|
[38639] | 103 | static char ** g_papszrtArgs;
|
---|
[38636] | 104 | /** The original argument vector of the program. */
|
---|
[38639] | 105 | static char ** g_papszrtOrgArgs;
|
---|
[38636] | 106 |
|
---|
[20911] | 107 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
[20821] | 108 | /**
|
---|
| 109 | * Whether alignment checks are enabled.
|
---|
| 110 | * This is set if the environment variable IPRT_ALIGNMENT_CHECKS is 1.
|
---|
| 111 | */
|
---|
| 112 | RTDATADECL(bool) g_fRTAlignmentChecks = false;
|
---|
| 113 | #endif
|
---|
[1] | 114 |
|
---|
[20821] | 115 |
|
---|
[62944] | 116 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_HAIKU) \
|
---|
[46593] | 117 | || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS) /** @todo add host init hooks everywhere. */
|
---|
| 118 | /* Stubs */
|
---|
[62564] | 119 | DECLHIDDEN(int) rtR3InitNativeFirst(uint32_t fFlags) { RT_NOREF_PV(fFlags); return VINF_SUCCESS; }
|
---|
| 120 | DECLHIDDEN(int) rtR3InitNativeFinal(uint32_t fFlags) { RT_NOREF_PV(fFlags); return VINF_SUCCESS; }
|
---|
| 121 | DECLHIDDEN(void) rtR3InitNativeObtrusive(uint32_t fFlags) { RT_NOREF_PV(fFlags); }
|
---|
[46593] | 122 | #endif
|
---|
| 123 |
|
---|
| 124 |
|
---|
[21890] | 125 | /**
|
---|
| 126 | * atexit callback.
|
---|
| 127 | *
|
---|
| 128 | * This makes sure any loggers are flushed and will later also work the
|
---|
| 129 | * termination callback chain.
|
---|
| 130 | */
|
---|
[85121] | 131 | static void rtR3ExitCallback(void) RT_NOTHROW_DEF
|
---|
[21890] | 132 | {
|
---|
[36363] | 133 | ASMAtomicWriteBool(&g_frtAtExitCalled, true);
|
---|
| 134 |
|
---|
[96442] | 135 | if (g_crtR3Users > 0)
|
---|
[21890] | 136 | {
|
---|
| 137 | PRTLOGGER pLogger = RTLogGetDefaultInstance();
|
---|
| 138 | if (pLogger)
|
---|
| 139 | RTLogFlush(pLogger);
|
---|
[20821] | 140 |
|
---|
[55980] | 141 | pLogger = RTLogRelGetDefaultInstance();
|
---|
[21890] | 142 | if (pLogger)
|
---|
| 143 | RTLogFlush(pLogger);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[18997] | 148 | #ifndef RT_OS_WINDOWS
|
---|
| 149 | /**
|
---|
| 150 | * Fork callback, child context.
|
---|
| 151 | */
|
---|
| 152 | static void rtR3ForkChildCallback(void)
|
---|
| 153 | {
|
---|
| 154 | g_ProcessSelf = getpid();
|
---|
| 155 | }
|
---|
| 156 | #endif /* RT_OS_WINDOWS */
|
---|
[11836] | 157 |
|
---|
[18997] | 158 | #ifdef RT_OS_OS2
|
---|
[24287] | 159 | /** Fork completion callback for OS/2. Only called in the child. */
|
---|
| 160 | static void rtR3ForkOs2ChildCompletionCallback(void *pvArg, int rc, __LIBC_FORKCTX enmCtx)
|
---|
| 161 | {
|
---|
| 162 | Assert(enmCtx == __LIBC_FORK_CTX_CHILD); NOREF(enmCtx);
|
---|
| 163 | NOREF(pvArg);
|
---|
| 164 |
|
---|
| 165 | if (!rc)
|
---|
| 166 | rtR3ForkChildCallback();
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[18997] | 169 | /** Low-level fork callback for OS/2. */
|
---|
| 170 | int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
|
---|
| 171 | {
|
---|
[24287] | 172 | if (enmOperation == __LIBC_FORK_OP_EXEC_CHILD)
|
---|
| 173 | return pForkHandle->pfnCompletionCallback(pForkHandle, rtR3ForkOs2ChildCompletionCallback, NULL, __LIBC_FORK_CTX_CHILD);
|
---|
[18997] | 174 | return 0;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[40317] | 177 | # define static static volatile /** @todo _FORK_CHILD1 causes unresolved externals in optimized builds. Fix macro. */
|
---|
[18997] | 178 | _FORK_CHILD1(0, rtR3ForkOs2Child);
|
---|
[40317] | 179 | # undef static
|
---|
[18997] | 180 | #endif /* RT_OS_OS2 */
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 |
|
---|
[14831] | 184 | /**
|
---|
| 185 | * Internal worker which initializes or re-initializes the
|
---|
[11836] | 186 | * program path, name and directory globals.
|
---|
| 187 | *
|
---|
[14831] | 188 | * @returns IPRT status code.
|
---|
[11836] | 189 | * @param pszProgramPath The program path, NULL if not specified.
|
---|
| 190 | */
|
---|
| 191 | static int rtR3InitProgramPath(const char *pszProgramPath)
|
---|
| 192 | {
|
---|
| 193 | /*
|
---|
| 194 | * We're reserving 32 bytes here for file names as what not.
|
---|
| 195 | */
|
---|
| 196 | if (!pszProgramPath)
|
---|
| 197 | {
|
---|
| 198 | int rc = rtProcInitExePath(g_szrtProcExePath, sizeof(g_szrtProcExePath) - 32);
|
---|
| 199 | if (RT_FAILURE(rc))
|
---|
| 200 | return rc;
|
---|
| 201 | }
|
---|
| 202 | else
|
---|
| 203 | {
|
---|
| 204 | size_t cch = strlen(pszProgramPath);
|
---|
| 205 | Assert(cch > 1);
|
---|
| 206 | AssertMsgReturn(cch < sizeof(g_szrtProcExePath) - 32, ("%zu\n", cch), VERR_BUFFER_OVERFLOW);
|
---|
| 207 | memcpy(g_szrtProcExePath, pszProgramPath, cch + 1);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /*
|
---|
| 211 | * Parse the name.
|
---|
| 212 | */
|
---|
| 213 | ssize_t offName;
|
---|
[96442] | 214 | g_cchrtProcExePath = RTPathParseSimple(g_szrtProcExePath, &g_cchrtProcExeDir, &offName, NULL);
|
---|
[11836] | 215 | g_offrtProcName = offName;
|
---|
| 216 | return VINF_SUCCESS;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[23919] | 219 |
|
---|
[92625] | 220 | #ifdef RT_OS_WINDOWS
|
---|
[38636] | 221 | /**
|
---|
[92625] | 222 | * Checks the two argument vectors contains the same strings.
|
---|
| 223 | */
|
---|
| 224 | DECLINLINE(bool) rtR3InitArgvEquals(int cArgs, char **papszArgs1, char **papszArgs2)
|
---|
| 225 | {
|
---|
| 226 | if (papszArgs1 != papszArgs2)
|
---|
| 227 | while (cArgs-- > 0)
|
---|
| 228 | if (strcmp(papszArgs1[cArgs], papszArgs2[cArgs]) != 0)
|
---|
| 229 | return false;
|
---|
| 230 | return true;
|
---|
| 231 | }
|
---|
| 232 | #endif
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
[38636] | 236 | * Internal worker which initializes or re-initializes the
|
---|
| 237 | * program path, name and directory globals.
|
---|
| 238 | *
|
---|
| 239 | * @returns IPRT status code.
|
---|
| 240 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
| 241 | * @param cArgs Pointer to the argument count.
|
---|
| 242 | * @param ppapszArgs Pointer to the argument vector pointer. NULL
|
---|
| 243 | * allowed if @a cArgs is 0.
|
---|
| 244 | */
|
---|
| 245 | static int rtR3InitArgv(uint32_t fFlags, int cArgs, char ***ppapszArgs)
|
---|
| 246 | {
|
---|
| 247 | NOREF(fFlags);
|
---|
| 248 | if (cArgs)
|
---|
| 249 | {
|
---|
| 250 | AssertPtr(ppapszArgs);
|
---|
| 251 | AssertPtr(*ppapszArgs);
|
---|
| 252 | char **papszOrgArgs = *ppapszArgs;
|
---|
| 253 |
|
---|
| 254 | /*
|
---|
| 255 | * Normally we should only be asked to convert arguments once. If we
|
---|
| 256 | * are though, it should be the already convered arguments.
|
---|
| 257 | */
|
---|
| 258 | if (g_crtArgs != -1)
|
---|
| 259 | {
|
---|
| 260 | AssertReturn( g_crtArgs == cArgs
|
---|
| 261 | && g_papszrtArgs == papszOrgArgs,
|
---|
| 262 | VERR_WRONG_ORDER); /* only init once! */
|
---|
| 263 | return VINF_SUCCESS;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[95806] | 266 | #if !defined(IPRT_NO_CRT) || !defined(RT_OS_WINDOWS)
|
---|
[48681] | 267 | if (!(fFlags & RTR3INIT_FLAGS_UTF8_ARGV))
|
---|
| 268 | {
|
---|
| 269 | /*
|
---|
| 270 | * Convert the arguments.
|
---|
| 271 | */
|
---|
[92625] | 272 | char **papszArgs;
|
---|
[38636] | 273 |
|
---|
[95806] | 274 | # ifdef RT_OS_WINDOWS
|
---|
[48681] | 275 | /* HACK ALERT! Try convert from unicode versions if possible.
|
---|
[92625] | 276 | Unfortunately for us, __wargv is only initialized if we have a unicode
|
---|
| 277 | main function. So, use getoptarv.cpp code to do the conversions and
|
---|
| 278 | hope it gives us the same result. (CommandLineToArgvW was not in NT 3.1.) */
|
---|
| 279 | if ( cArgs == __argc
|
---|
| 280 | && rtR3InitArgvEquals(cArgs, papszOrgArgs, __argv))
|
---|
[38636] | 281 | {
|
---|
[92625] | 282 | char *pszCmdLine = NULL;
|
---|
| 283 | int rc = RTUtf16ToUtf8Tag(GetCommandLineW(), &pszCmdLine, "will-leak:rtR3InitArgv");
|
---|
| 284 | AssertRCReturn(rc, rc);
|
---|
| 285 |
|
---|
| 286 | int cArgsFromCmdLine = -1;
|
---|
| 287 | rc = RTGetOptArgvFromString(&papszArgs, &cArgsFromCmdLine, pszCmdLine,
|
---|
| 288 | RTGETOPTARGV_CNV_QUOTE_MS_CRT | RTGETOPTARGV_CNV_MODIFY_INPUT, NULL);
|
---|
| 289 | AssertMsgRCReturn(rc, ("pszCmdLine='%s' rc=%Rrc\n", pszCmdLine, rc), rc);
|
---|
| 290 | AssertMsg(cArgsFromCmdLine == cArgs,
|
---|
| 291 | ("cArgsFromCmdLine=%d cArgs=%d pszCmdLine='%s' rc=%Rrc\n", cArgsFromCmdLine, cArgs, pszCmdLine));
|
---|
[38636] | 292 | }
|
---|
[48681] | 293 | else
|
---|
[95806] | 294 | # endif
|
---|
[45350] | 295 | {
|
---|
[92625] | 296 | papszArgs = (char **)RTMemAllocZTag((cArgs + 1) * sizeof(char *), "will-leak:rtR3InitArgv");
|
---|
| 297 | if (!papszArgs)
|
---|
| 298 | return VERR_NO_MEMORY;
|
---|
| 299 |
|
---|
[48681] | 300 | for (int i = 0; i < cArgs; i++)
|
---|
[45350] | 301 | {
|
---|
[48681] | 302 | int rc = RTStrCurrentCPToUtf8(&papszArgs[i], papszOrgArgs[i]);
|
---|
| 303 | if (RT_FAILURE(rc))
|
---|
| 304 | {
|
---|
| 305 | while (i--)
|
---|
| 306 | RTStrFree(papszArgs[i]);
|
---|
| 307 | RTMemFree(papszArgs);
|
---|
| 308 | return rc;
|
---|
| 309 | }
|
---|
[45350] | 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[48681] | 313 | papszArgs[cArgs] = NULL;
|
---|
[38636] | 314 |
|
---|
[48681] | 315 | g_papszrtOrgArgs = papszOrgArgs;
|
---|
| 316 | g_papszrtArgs = papszArgs;
|
---|
| 317 | g_crtArgs = cArgs;
|
---|
[38636] | 318 |
|
---|
[48681] | 319 | *ppapszArgs = papszArgs;
|
---|
| 320 | }
|
---|
| 321 | else
|
---|
[95806] | 322 | #endif /* !IPRT_NO_CRT || !RT_OS_WINDOWS */
|
---|
[48681] | 323 | {
|
---|
| 324 | /*
|
---|
| 325 | * The arguments are already UTF-8, no conversion needed.
|
---|
| 326 | */
|
---|
| 327 | g_papszrtOrgArgs = papszOrgArgs;
|
---|
| 328 | g_papszrtArgs = papszOrgArgs;
|
---|
| 329 | g_crtArgs = cArgs;
|
---|
| 330 | }
|
---|
[38636] | 331 | }
|
---|
| 332 |
|
---|
| 333 | return VINF_SUCCESS;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 |
|
---|
[23919] | 337 | #ifdef IPRT_USE_SIG_CHILD_DUMMY
|
---|
[1] | 338 | /**
|
---|
[23919] | 339 | * Dummy SIGCHILD handler.
|
---|
| 340 | *
|
---|
| 341 | * Assigned on rtR3Init only when SIGCHILD handler is set SIGIGN or SIGDEF to
|
---|
| 342 | * ensure waitpid works properly for the terminated processes.
|
---|
| 343 | */
|
---|
| 344 | static void rtR3SigChildHandler(int iSignal)
|
---|
| 345 | {
|
---|
| 346 | NOREF(iSignal);
|
---|
| 347 | }
|
---|
| 348 | #endif /* IPRT_USE_SIG_CHILD_DUMMY */
|
---|
| 349 |
|
---|
| 350 |
|
---|
| 351 | /**
|
---|
[18997] | 352 | * rtR3Init worker.
|
---|
[1] | 353 | */
|
---|
[57926] | 354 | static int rtR3InitBody(uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
[1] | 355 | {
|
---|
[11597] | 356 | /*
|
---|
[46593] | 357 | * Early native initialization.
|
---|
| 358 | */
|
---|
| 359 | int rc = rtR3InitNativeFirst(fFlags);
|
---|
| 360 | AssertMsgRCReturn(rc, ("rtR3InitNativeFirst failed with %Rrc\n", rc), rc);
|
---|
| 361 |
|
---|
| 362 | /*
|
---|
| 363 | * Disable error popups.
|
---|
| 364 | */
|
---|
| 365 | #if defined(RT_OS_OS2) /** @todo move to private code. */
|
---|
| 366 | DosError(FERR_DISABLEHARDERR);
|
---|
| 367 | #endif
|
---|
| 368 |
|
---|
[95806] | 369 | #ifndef IPRT_NO_CRT
|
---|
[46593] | 370 | /*
|
---|
[28903] | 371 | * Init C runtime locale before we do anything that may end up converting
|
---|
| 372 | * paths or we'll end up using the "C" locale for path conversion.
|
---|
| 373 | */
|
---|
| 374 | setlocale(LC_CTYPE, "");
|
---|
[95806] | 375 | #endif
|
---|
[28903] | 376 |
|
---|
| 377 | /*
|
---|
[18997] | 378 | * The Process ID.
|
---|
[11597] | 379 | */
|
---|
[18997] | 380 | #ifdef _MSC_VER
|
---|
[95806] | 381 | g_ProcessSelf = GetCurrentProcessId(); /* since NT 3.1, not 3.51+ as listed on geoffchappell.com */
|
---|
[18997] | 382 | #else
|
---|
| 383 | g_ProcessSelf = getpid();
|
---|
[11794] | 384 | #endif
|
---|
[11597] | 385 |
|
---|
[39751] | 386 | /*
|
---|
[44486] | 387 | * Save the init flags.
|
---|
| 388 | */
|
---|
| 389 | g_fInitFlags |= fFlags;
|
---|
[44456] | 390 |
|
---|
[11597] | 391 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
[1] | 392 | # ifdef VBOX
|
---|
| 393 | /*
|
---|
| 394 | * This MUST be done as the very first thing, before any file is opened.
|
---|
| 395 | * The log is opened on demand, but the first log entries may be caused
|
---|
| 396 | * by rtThreadInit() below.
|
---|
| 397 | */
|
---|
| 398 | const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
|
---|
| 399 | if ( pszDisableHostCache != NULL
|
---|
[20821] | 400 | && *pszDisableHostCache
|
---|
[1] | 401 | && strcmp(pszDisableHostCache, "0") != 0)
|
---|
| 402 | {
|
---|
| 403 | RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
|
---|
| 404 | RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
|
---|
| 405 | }
|
---|
| 406 | # endif /* VBOX */
|
---|
[6677] | 407 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
[1] | 408 |
|
---|
| 409 | /*
|
---|
| 410 | * Thread Thread database and adopt the caller thread as 'main'.
|
---|
| 411 | * This must be done before everything else or else we'll call into threading
|
---|
| 412 | * without having initialized TLS entries and suchlike.
|
---|
| 413 | */
|
---|
[46593] | 414 | rc = rtThreadInit();
|
---|
[18997] | 415 | AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
|
---|
[1] | 416 |
|
---|
[60773] | 417 | /*
|
---|
| 418 | * The executable path before SUPLib (windows requirement).
|
---|
| 419 | */
|
---|
| 420 | rc = rtR3InitProgramPath(pszProgramPath);
|
---|
| 421 | AssertLogRelMsgRCReturn(rc, ("Failed to get executable directory path, rc=%Rrc!\n", rc), rc);
|
---|
| 422 |
|
---|
[6677] | 423 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
[60773] | 424 | /*
|
---|
| 425 | * Initialize SUPLib here so the GIP can get going as early as possible
|
---|
| 426 | * (improves accuracy for the first client).
|
---|
| 427 | */
|
---|
[92613] | 428 | if (fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB))
|
---|
[1] | 429 | {
|
---|
[92714] | 430 | if (!(fFlags & ((SUPR3INIT_F_UNRESTRICTED | SUPR3INIT_F_LIMITED) << RTR3INIT_FLAGS_SUPLIB_SHIFT)))
|
---|
| 431 | g_fInitFlags |= fFlags |= SUPR3INIT_F_UNRESTRICTED << RTR3INIT_FLAGS_SUPLIB_SHIFT;
|
---|
| 432 | rc = SUPR3InitEx(fFlags >> RTR3INIT_FLAGS_SUPLIB_SHIFT, NULL /*ppSession*/);
|
---|
[92613] | 433 | AssertMsgReturn(RT_SUCCESS(rc) || (fFlags & RTR3INIT_FLAGS_TRY_SUPLIB),
|
---|
| 434 | ("Failed to initialize the support library, rc=%Rrc!\n", rc), rc);
|
---|
[1] | 435 | }
|
---|
| 436 | #endif
|
---|
| 437 |
|
---|
| 438 | /*
|
---|
[60773] | 439 | * Convert arguments.
|
---|
[1] | 440 | */
|
---|
[57926] | 441 | rc = rtR3InitArgv(fFlags, cArgs, ppapszArgs);
|
---|
[38636] | 442 | AssertLogRelMsgRCReturn(rc, ("Failed to convert the arguments, rc=%Rrc!\n", rc), rc);
|
---|
| 443 |
|
---|
[11836] | 444 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
[1] | 445 | /*
|
---|
[92613] | 446 | * The threading is initialized, so we can safely sleep a bit if GIP
|
---|
| 447 | * needs some time to start updating itself. Currently limited to
|
---|
| 448 | * the first mapping of GIP (u32TransactionId <= 4), quite possible we
|
---|
| 449 | * could just ditch this now.
|
---|
[1] | 450 | */
|
---|
[92613] | 451 | /** @todo consider dropping this... */
|
---|
| 452 | PSUPGLOBALINFOPAGE pGip;
|
---|
| 453 | if ( (fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB))
|
---|
| 454 | && (pGip = g_pSUPGlobalInfoPage) != NULL
|
---|
| 455 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC)
|
---|
[11836] | 456 | {
|
---|
[92613] | 457 | PSUPGIPCPU pGipCpu = SUPGetGipCpuPtr(pGip);
|
---|
| 458 | if ( pGipCpu
|
---|
| 459 | && pGipCpu->u32TransactionId <= 4)
|
---|
| 460 | {
|
---|
| 461 | RTThreadSleep(pGip->u32UpdateIntervalNS / RT_NS_1MS + 2);
|
---|
| 462 | RTTimeNanoTS();
|
---|
| 463 | }
|
---|
[11836] | 464 | }
|
---|
[1] | 465 | #endif
|
---|
| 466 |
|
---|
| 467 | /*
|
---|
[72863] | 468 | * Init the program start timestamp TS.
|
---|
[14856] | 469 | * Do that here to be sure that the GIP time was properly updated the 1st time.
|
---|
| 470 | */
|
---|
| 471 | g_u64ProgramStartNanoTS = RTTimeNanoTS();
|
---|
| 472 |
|
---|
[18095] | 473 | /*
|
---|
[18997] | 474 | * The remainder cannot easily be undone, so it has to go last.
|
---|
[18055] | 475 | */
|
---|
[18997] | 476 |
|
---|
[21890] | 477 | /* Fork and exit callbacks. */
|
---|
[18997] | 478 | #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|
---|
| 479 | rc = pthread_atfork(NULL, NULL, rtR3ForkChildCallback);
|
---|
| 480 | AssertMsg(rc == 0, ("%d\n", rc));
|
---|
| 481 | #endif
|
---|
[21890] | 482 | atexit(rtR3ExitCallback);
|
---|
[18997] | 483 |
|
---|
[23919] | 484 | #ifdef IPRT_USE_SIG_CHILD_DUMMY
|
---|
| 485 | /*
|
---|
| 486 | * SIGCHLD must not be ignored (that's default), otherwise posix compliant waitpid
|
---|
| 487 | * implementations won't work right.
|
---|
| 488 | */
|
---|
| 489 | for (;;)
|
---|
| 490 | {
|
---|
| 491 | struct sigaction saOld;
|
---|
| 492 | rc = sigaction(SIGCHLD, 0, &saOld); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
| 493 | if ( rc != 0
|
---|
| 494 | || (saOld.sa_flags & SA_SIGINFO)
|
---|
| 495 | || ( saOld.sa_handler != SIG_IGN
|
---|
| 496 | && saOld.sa_handler != SIG_DFL)
|
---|
| 497 | )
|
---|
| 498 | break;
|
---|
| 499 |
|
---|
| 500 | /* Try install dummy handler. */
|
---|
| 501 | struct sigaction saNew = saOld;
|
---|
| 502 | saNew.sa_flags = SA_NOCLDSTOP | SA_RESTART;
|
---|
| 503 | saNew.sa_handler = rtR3SigChildHandler;
|
---|
| 504 | rc = sigemptyset(&saNew.sa_mask); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
| 505 | struct sigaction saOld2;
|
---|
| 506 | rc = sigaction(SIGCHLD, &saNew, &saOld2); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
| 507 | if ( rc != 0
|
---|
| 508 | || ( saOld2.sa_handler == saOld.sa_handler
|
---|
| 509 | && !(saOld2.sa_flags & SA_SIGINFO))
|
---|
| 510 | )
|
---|
| 511 | break;
|
---|
| 512 |
|
---|
| 513 | /* Race during dynamic load, restore and try again... */
|
---|
| 514 | sigaction(SIGCHLD, &saOld2, NULL);
|
---|
| 515 | RTThreadYield();
|
---|
| 516 | }
|
---|
| 517 | #endif /* IPRT_USE_SIG_CHILD_DUMMY */
|
---|
| 518 |
|
---|
[20911] | 519 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
[20821] | 520 | /*
|
---|
| 521 | * Enable alignment checks.
|
---|
| 522 | */
|
---|
[95806] | 523 | const char *pszAlignmentChecks = RTEnvGet("IPRT_ALIGNMENT_CHECKS"); /** @todo add RTEnvGetBool */
|
---|
[20821] | 524 | g_fRTAlignmentChecks = pszAlignmentChecks != NULL
|
---|
| 525 | && pszAlignmentChecks[0] == '1'
|
---|
| 526 | && pszAlignmentChecks[1] == '\0';
|
---|
| 527 | if (g_fRTAlignmentChecks)
|
---|
| 528 | IPRT_ALIGNMENT_CHECKS_ENABLE();
|
---|
| 529 | #endif
|
---|
| 530 |
|
---|
[46593] | 531 | /*
|
---|
| 532 | * Final native initialization.
|
---|
| 533 | */
|
---|
| 534 | rc = rtR3InitNativeFinal(fFlags);
|
---|
| 535 | AssertMsgRCReturn(rc, ("rtR3InitNativeFinal failed with %Rrc\n", rc), rc);
|
---|
| 536 |
|
---|
[18997] | 537 | return VINF_SUCCESS;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 |
|
---|
| 541 | /**
|
---|
| 542 | * Internal initialization worker.
|
---|
| 543 | *
|
---|
| 544 | * @returns IPRT status code.
|
---|
[38636] | 545 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
| 546 | * @param cArgs Pointer to the argument count.
|
---|
| 547 | * @param ppapszArgs Pointer to the argument vector pointer. NULL
|
---|
| 548 | * allowed if @a cArgs is 0.
|
---|
| 549 | * @param pszProgramPath The program path. Pass NULL if we're to figure it
|
---|
| 550 | * out ourselves.
|
---|
[18997] | 551 | */
|
---|
[57926] | 552 | static int rtR3Init(uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
[18997] | 553 | {
|
---|
| 554 | /* no entry log flow, because prefixes and thread may freak out. */
|
---|
[92613] | 555 | Assert(!(fFlags & ~RTR3INIT_FLAGS_VALID_MASK));
|
---|
[38636] | 556 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL) || cArgs == 0);
|
---|
[18997] | 557 |
|
---|
[14856] | 558 | /*
|
---|
[18997] | 559 | * Do reference counting, only initialize the first time around.
|
---|
| 560 | *
|
---|
[38636] | 561 | * We are ASSUMING that nobody will be able to race RTR3Init* calls when the
|
---|
[18997] | 562 | * first one, the real init, is running (second assertion).
|
---|
[1] | 563 | */
|
---|
[96442] | 564 | int32_t cUsers = ASMAtomicIncS32(&g_crtR3Users);
|
---|
[18997] | 565 | if (cUsers != 1)
|
---|
| 566 | {
|
---|
| 567 | AssertMsg(cUsers > 1, ("%d\n", cUsers));
|
---|
[96442] | 568 | Assert(!g_frtR3Initializing);
|
---|
[92613] | 569 |
|
---|
[18997] | 570 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
[92613] | 571 | /* Initialize the support library if requested. We've always ignored the
|
---|
| 572 | status code here for some reason, making the two flags same. */
|
---|
| 573 | if (fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB))
|
---|
[44486] | 574 | {
|
---|
[92714] | 575 | if (!(fFlags & ((SUPR3INIT_F_UNRESTRICTED | SUPR3INIT_F_LIMITED) << RTR3INIT_FLAGS_SUPLIB_SHIFT)))
|
---|
| 576 | fFlags |= SUPR3INIT_F_UNRESTRICTED << RTR3INIT_FLAGS_SUPLIB_SHIFT;
|
---|
| 577 | SUPR3InitEx(fFlags >> RTR3INIT_FLAGS_SUPLIB_SHIFT, NULL /*ppSession*/);
|
---|
[92613] | 578 | g_fInitFlags |= fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB | RTR3INIT_FLAGS_SUPLIB_MASK);
|
---|
[44486] | 579 | }
|
---|
[18997] | 580 | #endif
|
---|
[59404] | 581 | g_fInitFlags |= fFlags & RTR3INIT_FLAGS_UTF8_ARGV;
|
---|
[38636] | 582 |
|
---|
[46593] | 583 | if ( !(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE)
|
---|
| 584 | && (g_fInitFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
|
---|
| 585 | {
|
---|
| 586 | g_fInitFlags &= ~RTR3INIT_FLAGS_UNOBTRUSIVE;
|
---|
[59404] | 587 | g_fInitFlags |= fFlags & RTR3INIT_FLAGS_STANDALONE_APP;
|
---|
| 588 | rtR3InitNativeObtrusive(g_fInitFlags | fFlags);
|
---|
[46593] | 589 | rtThreadReInitObtrusive();
|
---|
| 590 | }
|
---|
[59404] | 591 | else
|
---|
| 592 | Assert(!(fFlags & RTR3INIT_FLAGS_STANDALONE_APP) || (g_fInitFlags & RTR3INIT_FLAGS_STANDALONE_APP));
|
---|
[46593] | 593 |
|
---|
| 594 | int rc = VINF_SUCCESS;
|
---|
| 595 | if (pszProgramPath)
|
---|
| 596 | rc = rtR3InitProgramPath(pszProgramPath);
|
---|
[38636] | 597 | if (RT_SUCCESS(rc))
|
---|
[57926] | 598 | rc = rtR3InitArgv(fFlags, cArgs, ppapszArgs);
|
---|
[38636] | 599 | return rc;
|
---|
[18997] | 600 | }
|
---|
[1] | 601 |
|
---|
[18997] | 602 | /*
|
---|
| 603 | * Do the initialization.
|
---|
| 604 | */
|
---|
[96442] | 605 | ASMAtomicWriteBool(&g_frtR3Initializing, true);
|
---|
[57926] | 606 | int rc = rtR3InitBody(fFlags, cArgs, ppapszArgs, pszProgramPath);
|
---|
[96442] | 607 | ASMAtomicWriteBool(&g_frtR3Initializing, false);
|
---|
[18997] | 608 | if (RT_FAILURE(rc))
|
---|
| 609 | {
|
---|
| 610 | /* failure */
|
---|
[96442] | 611 | ASMAtomicDecS32(&g_crtR3Users);
|
---|
[18997] | 612 | return rc;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | /* success */
|
---|
[38636] | 616 | LogFlow(("rtR3Init: returns VINF_SUCCESS\n"));
|
---|
[1] | 617 | return VINF_SUCCESS;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[11822] | 620 |
|
---|
[57926] | 621 | RTR3DECL(int) RTR3InitExe(int cArgs, char ***ppapszArgs, uint32_t fFlags)
|
---|
[11822] | 622 | {
|
---|
[38636] | 623 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
[57926] | 624 | return rtR3Init(fFlags, cArgs, ppapszArgs, NULL);
|
---|
[11822] | 625 | }
|
---|
| 626 |
|
---|
| 627 |
|
---|
[38636] | 628 | RTR3DECL(int) RTR3InitExeNoArguments(uint32_t fFlags)
|
---|
[11841] | 629 | {
|
---|
[38636] | 630 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
| 631 | return rtR3Init(fFlags, 0, NULL, NULL);
|
---|
[11841] | 632 | }
|
---|
| 633 |
|
---|
| 634 |
|
---|
[38636] | 635 | RTR3DECL(int) RTR3InitDll(uint32_t fFlags)
|
---|
[11822] | 636 | {
|
---|
[38636] | 637 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
| 638 | return rtR3Init(fFlags | RTR3INIT_FLAGS_DLL, 0, NULL, NULL);
|
---|
[11822] | 639 | }
|
---|
| 640 |
|
---|
| 641 |
|
---|
[57926] | 642 | RTR3DECL(int) RTR3InitEx(uint32_t iVersion, uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
[11822] | 643 | {
|
---|
[38636] | 644 | AssertReturn(iVersion == RTR3INIT_VER_CUR, VERR_NOT_SUPPORTED);
|
---|
[57926] | 645 | return rtR3Init(fFlags, cArgs, ppapszArgs, pszProgramPath);
|
---|
[11822] | 646 | }
|
---|
| 647 |
|
---|
[53525] | 648 |
|
---|
| 649 | RTR3DECL(bool) RTR3InitIsInitialized(void)
|
---|
| 650 | {
|
---|
[96442] | 651 | return g_crtR3Users >= 1 && !g_frtR3Initializing;
|
---|
[53525] | 652 | }
|
---|
| 653 |
|
---|
| 654 |
|
---|
[44456] | 655 | RTR3DECL(bool) RTR3InitIsUnobtrusive(void)
|
---|
| 656 | {
|
---|
[44486] | 657 | return RT_BOOL(g_fInitFlags & RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
[44456] | 658 | }
|
---|
[11822] | 659 |
|
---|
[53525] | 660 |
|
---|
[11822] | 661 | #if 0 /** @todo implement RTR3Term. */
|
---|
| 662 | RTR3DECL(void) RTR3Term(void)
|
---|
| 663 | {
|
---|
| 664 | }
|
---|
| 665 | #endif
|
---|
| 666 |
|
---|