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