1 | /* $Id: init.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Init Ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_DEFAULT
|
---|
42 | #include <iprt/types.h> /* darwin: UINT32_C and others. */
|
---|
43 |
|
---|
44 | #ifdef RT_OS_WINDOWS
|
---|
45 | # include <iprt/win/windows.h>
|
---|
46 | #else
|
---|
47 | # include <unistd.h>
|
---|
48 | # ifndef RT_OS_OS2
|
---|
49 | # include <pthread.h>
|
---|
50 | # include <signal.h>
|
---|
51 | # include <errno.h>
|
---|
52 | # define IPRT_USE_SIG_CHILD_DUMMY
|
---|
53 | # endif
|
---|
54 | #endif
|
---|
55 | #ifdef RT_OS_OS2
|
---|
56 | # include <InnoTekLIBC/fork.h>
|
---|
57 | # define INCL_DOSMISC
|
---|
58 | # include <os2.h>
|
---|
59 | #endif
|
---|
60 | #ifndef IPRT_NO_CRT
|
---|
61 | # include <locale.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | #include <iprt/initterm.h>
|
---|
65 | #include <iprt/asm.h>
|
---|
66 | #include <iprt/assert.h>
|
---|
67 | #include <iprt/env.h>
|
---|
68 | #include <iprt/errcore.h>
|
---|
69 | #include <iprt/log.h>
|
---|
70 | #include <iprt/mem.h>
|
---|
71 | #include <iprt/path.h>
|
---|
72 | #include <iprt/time.h>
|
---|
73 | #include <iprt/string.h>
|
---|
74 | #include <iprt/param.h>
|
---|
75 | #ifdef RT_OS_WINDOWS
|
---|
76 | # include <iprt/getopt.h>
|
---|
77 | # include <iprt/utf16.h>
|
---|
78 | #endif
|
---|
79 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
80 | # include <iprt/file.h>
|
---|
81 | # include <VBox/sup.h>
|
---|
82 | #endif
|
---|
83 | #include <stdlib.h>
|
---|
84 |
|
---|
85 | #include "init.h"
|
---|
86 | #include "internal/alignmentchecks.h"
|
---|
87 | #include "internal/initterm.h"
|
---|
88 | #include "internal/path.h"
|
---|
89 | #include "internal/process.h"
|
---|
90 | #include "internal/thread.h"
|
---|
91 | #include "internal/time.h"
|
---|
92 |
|
---|
93 |
|
---|
94 | /*********************************************************************************************************************************
|
---|
95 | * Global Variables *
|
---|
96 | *********************************************************************************************************************************/
|
---|
97 | /** The IPRT init flags. */
|
---|
98 | static uint32_t g_fInitFlags;
|
---|
99 |
|
---|
100 | /** The argument count of the program. */
|
---|
101 | static int g_crtArgs = -1;
|
---|
102 | /** The arguments of the program (UTF-8). This is "leaked". */
|
---|
103 | static char ** g_papszrtArgs;
|
---|
104 | /** The original argument vector of the program. */
|
---|
105 | static char ** g_papszrtOrgArgs;
|
---|
106 |
|
---|
107 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
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
|
---|
114 |
|
---|
115 |
|
---|
116 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_HAIKU) \
|
---|
117 | || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS) /** @todo add host init hooks everywhere. */
|
---|
118 | /* Stubs */
|
---|
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); }
|
---|
122 | #endif
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * atexit callback.
|
---|
127 | *
|
---|
128 | * This makes sure any loggers are flushed and will later also work the
|
---|
129 | * termination callback chain.
|
---|
130 | */
|
---|
131 | static void rtR3ExitCallback(void) RT_NOTHROW_DEF
|
---|
132 | {
|
---|
133 | ASMAtomicWriteBool(&g_frtAtExitCalled, true);
|
---|
134 |
|
---|
135 | if (g_crtR3Users > 0)
|
---|
136 | {
|
---|
137 | PRTLOGGER pLogger = RTLogGetDefaultInstance();
|
---|
138 | if (pLogger)
|
---|
139 | RTLogFlush(pLogger);
|
---|
140 |
|
---|
141 | pLogger = RTLogRelGetDefaultInstance();
|
---|
142 | if (pLogger)
|
---|
143 | RTLogFlush(pLogger);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
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 */
|
---|
157 |
|
---|
158 | #ifdef RT_OS_OS2
|
---|
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 |
|
---|
169 | /** Low-level fork callback for OS/2. */
|
---|
170 | int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
|
---|
171 | {
|
---|
172 | if (enmOperation == __LIBC_FORK_OP_EXEC_CHILD)
|
---|
173 | return pForkHandle->pfnCompletionCallback(pForkHandle, rtR3ForkOs2ChildCompletionCallback, NULL, __LIBC_FORK_CTX_CHILD);
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | # define static static volatile /** @todo _FORK_CHILD1 causes unresolved externals in optimized builds. Fix macro. */
|
---|
178 | _FORK_CHILD1(0, rtR3ForkOs2Child);
|
---|
179 | # undef static
|
---|
180 | #endif /* RT_OS_OS2 */
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Internal worker which initializes or re-initializes the
|
---|
186 | * program path, name and directory globals.
|
---|
187 | *
|
---|
188 | * @returns IPRT status code.
|
---|
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;
|
---|
214 | g_cchrtProcExePath = RTPathParseSimple(g_szrtProcExePath, &g_cchrtProcExeDir, &offName, NULL);
|
---|
215 | g_offrtProcName = offName;
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | #ifdef RT_OS_WINDOWS
|
---|
221 | /**
|
---|
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 | /**
|
---|
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 |
|
---|
266 | #if !defined(IPRT_NO_CRT) || !defined(RT_OS_WINDOWS)
|
---|
267 | if (!(fFlags & RTR3INIT_FLAGS_UTF8_ARGV))
|
---|
268 | {
|
---|
269 | /*
|
---|
270 | * Convert the arguments.
|
---|
271 | */
|
---|
272 | char **papszArgs;
|
---|
273 |
|
---|
274 | # ifdef RT_OS_WINDOWS
|
---|
275 | /* HACK ALERT! Try convert from unicode versions if possible.
|
---|
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))
|
---|
281 | {
|
---|
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));
|
---|
292 | }
|
---|
293 | else
|
---|
294 | # endif
|
---|
295 | {
|
---|
296 | papszArgs = (char **)RTMemAllocZTag((cArgs + 1) * sizeof(char *), "will-leak:rtR3InitArgv");
|
---|
297 | if (!papszArgs)
|
---|
298 | return VERR_NO_MEMORY;
|
---|
299 |
|
---|
300 | for (int i = 0; i < cArgs; i++)
|
---|
301 | {
|
---|
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 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | papszArgs[cArgs] = NULL;
|
---|
314 |
|
---|
315 | g_papszrtOrgArgs = papszOrgArgs;
|
---|
316 | g_papszrtArgs = papszArgs;
|
---|
317 | g_crtArgs = cArgs;
|
---|
318 |
|
---|
319 | *ppapszArgs = papszArgs;
|
---|
320 | }
|
---|
321 | else
|
---|
322 | #endif /* !IPRT_NO_CRT || !RT_OS_WINDOWS */
|
---|
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 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | return VINF_SUCCESS;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | #ifdef IPRT_USE_SIG_CHILD_DUMMY
|
---|
338 | /**
|
---|
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 | /**
|
---|
352 | * rtR3Init worker.
|
---|
353 | */
|
---|
354 | static int rtR3InitBody(uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
355 | {
|
---|
356 | /*
|
---|
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 |
|
---|
369 | #ifndef IPRT_NO_CRT
|
---|
370 | /*
|
---|
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, "");
|
---|
375 | #endif
|
---|
376 |
|
---|
377 | /*
|
---|
378 | * The Process ID.
|
---|
379 | */
|
---|
380 | #ifdef _MSC_VER
|
---|
381 | g_ProcessSelf = GetCurrentProcessId(); /* since NT 3.1, not 3.51+ as listed on geoffchappell.com */
|
---|
382 | #else
|
---|
383 | g_ProcessSelf = getpid();
|
---|
384 | #endif
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Save the init flags.
|
---|
388 | */
|
---|
389 | g_fInitFlags |= fFlags;
|
---|
390 |
|
---|
391 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
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
|
---|
400 | && *pszDisableHostCache
|
---|
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 */
|
---|
407 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
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 | */
|
---|
414 | rc = rtThreadInit();
|
---|
415 | AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
|
---|
416 |
|
---|
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 |
|
---|
423 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
424 | /*
|
---|
425 | * Initialize SUPLib here so the GIP can get going as early as possible
|
---|
426 | * (improves accuracy for the first client).
|
---|
427 | */
|
---|
428 | if (fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB))
|
---|
429 | {
|
---|
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*/);
|
---|
433 | AssertMsgReturn(RT_SUCCESS(rc) || (fFlags & RTR3INIT_FLAGS_TRY_SUPLIB),
|
---|
434 | ("Failed to initialize the support library, rc=%Rrc!\n", rc), rc);
|
---|
435 | }
|
---|
436 | #endif
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * Convert arguments.
|
---|
440 | */
|
---|
441 | rc = rtR3InitArgv(fFlags, cArgs, ppapszArgs);
|
---|
442 | AssertLogRelMsgRCReturn(rc, ("Failed to convert the arguments, rc=%Rrc!\n", rc), rc);
|
---|
443 |
|
---|
444 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
445 | /*
|
---|
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.
|
---|
450 | */
|
---|
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)
|
---|
456 | {
|
---|
457 | PSUPGIPCPU pGipCpu = SUPGetGipCpuPtr(pGip);
|
---|
458 | if ( pGipCpu
|
---|
459 | && pGipCpu->u32TransactionId <= 4)
|
---|
460 | {
|
---|
461 | RTThreadSleep(pGip->u32UpdateIntervalNS / RT_NS_1MS + 2);
|
---|
462 | RTTimeNanoTS();
|
---|
463 | }
|
---|
464 | }
|
---|
465 | #endif
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Init the program start timestamp TS.
|
---|
469 | * Do that here to be sure that the GIP time was properly updated the 1st time.
|
---|
470 | */
|
---|
471 | g_u64ProgramStartNanoTS = RTTimeNanoTS();
|
---|
472 |
|
---|
473 | /*
|
---|
474 | * The remainder cannot easily be undone, so it has to go last.
|
---|
475 | */
|
---|
476 |
|
---|
477 | /* Fork and exit callbacks. */
|
---|
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
|
---|
482 | atexit(rtR3ExitCallback);
|
---|
483 |
|
---|
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 |
|
---|
519 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
520 | /*
|
---|
521 | * Enable alignment checks.
|
---|
522 | */
|
---|
523 | const char *pszAlignmentChecks = RTEnvGet("IPRT_ALIGNMENT_CHECKS"); /** @todo add RTEnvGetBool */
|
---|
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 |
|
---|
531 | /*
|
---|
532 | * Final native initialization.
|
---|
533 | */
|
---|
534 | rc = rtR3InitNativeFinal(fFlags);
|
---|
535 | AssertMsgRCReturn(rc, ("rtR3InitNativeFinal failed with %Rrc\n", rc), rc);
|
---|
536 |
|
---|
537 | return VINF_SUCCESS;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Internal initialization worker.
|
---|
543 | *
|
---|
544 | * @returns IPRT status code.
|
---|
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.
|
---|
551 | */
|
---|
552 | static int rtR3Init(uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
553 | {
|
---|
554 | /* no entry log flow, because prefixes and thread may freak out. */
|
---|
555 | Assert(!(fFlags & ~RTR3INIT_FLAGS_VALID_MASK));
|
---|
556 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL) || cArgs == 0);
|
---|
557 |
|
---|
558 | /*
|
---|
559 | * Do reference counting, only initialize the first time around.
|
---|
560 | *
|
---|
561 | * We are ASSUMING that nobody will be able to race RTR3Init* calls when the
|
---|
562 | * first one, the real init, is running (second assertion).
|
---|
563 | */
|
---|
564 | int32_t cUsers = ASMAtomicIncS32(&g_crtR3Users);
|
---|
565 | if (cUsers != 1)
|
---|
566 | {
|
---|
567 | AssertMsg(cUsers > 1, ("%d\n", cUsers));
|
---|
568 | Assert(!g_frtR3Initializing);
|
---|
569 |
|
---|
570 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
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))
|
---|
574 | {
|
---|
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*/);
|
---|
578 | g_fInitFlags |= fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB | RTR3INIT_FLAGS_SUPLIB_MASK);
|
---|
579 | }
|
---|
580 | #endif
|
---|
581 | g_fInitFlags |= fFlags & RTR3INIT_FLAGS_UTF8_ARGV;
|
---|
582 |
|
---|
583 | if ( !(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE)
|
---|
584 | && (g_fInitFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
|
---|
585 | {
|
---|
586 | g_fInitFlags &= ~RTR3INIT_FLAGS_UNOBTRUSIVE;
|
---|
587 | g_fInitFlags |= fFlags & RTR3INIT_FLAGS_STANDALONE_APP;
|
---|
588 | rtR3InitNativeObtrusive(g_fInitFlags | fFlags);
|
---|
589 | rtThreadReInitObtrusive();
|
---|
590 | }
|
---|
591 | else
|
---|
592 | Assert(!(fFlags & RTR3INIT_FLAGS_STANDALONE_APP) || (g_fInitFlags & RTR3INIT_FLAGS_STANDALONE_APP));
|
---|
593 |
|
---|
594 | int rc = VINF_SUCCESS;
|
---|
595 | if (pszProgramPath)
|
---|
596 | rc = rtR3InitProgramPath(pszProgramPath);
|
---|
597 | if (RT_SUCCESS(rc))
|
---|
598 | rc = rtR3InitArgv(fFlags, cArgs, ppapszArgs);
|
---|
599 | return rc;
|
---|
600 | }
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * Do the initialization.
|
---|
604 | */
|
---|
605 | ASMAtomicWriteBool(&g_frtR3Initializing, true);
|
---|
606 | int rc = rtR3InitBody(fFlags, cArgs, ppapszArgs, pszProgramPath);
|
---|
607 | ASMAtomicWriteBool(&g_frtR3Initializing, false);
|
---|
608 | if (RT_FAILURE(rc))
|
---|
609 | {
|
---|
610 | /* failure */
|
---|
611 | ASMAtomicDecS32(&g_crtR3Users);
|
---|
612 | return rc;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /* success */
|
---|
616 | LogFlow(("rtR3Init: returns VINF_SUCCESS\n"));
|
---|
617 | return VINF_SUCCESS;
|
---|
618 | }
|
---|
619 |
|
---|
620 |
|
---|
621 | RTR3DECL(int) RTR3InitExe(int cArgs, char ***ppapszArgs, uint32_t fFlags)
|
---|
622 | {
|
---|
623 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
624 | return rtR3Init(fFlags, cArgs, ppapszArgs, NULL);
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | RTR3DECL(int) RTR3InitExeNoArguments(uint32_t fFlags)
|
---|
629 | {
|
---|
630 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
631 | return rtR3Init(fFlags, 0, NULL, NULL);
|
---|
632 | }
|
---|
633 |
|
---|
634 |
|
---|
635 | RTR3DECL(int) RTR3InitDll(uint32_t fFlags)
|
---|
636 | {
|
---|
637 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
638 | return rtR3Init(fFlags | RTR3INIT_FLAGS_DLL, 0, NULL, NULL);
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 | RTR3DECL(int) RTR3InitEx(uint32_t iVersion, uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
643 | {
|
---|
644 | AssertReturn(iVersion == RTR3INIT_VER_CUR, VERR_NOT_SUPPORTED);
|
---|
645 | return rtR3Init(fFlags, cArgs, ppapszArgs, pszProgramPath);
|
---|
646 | }
|
---|
647 |
|
---|
648 |
|
---|
649 | RTR3DECL(bool) RTR3InitIsInitialized(void)
|
---|
650 | {
|
---|
651 | return g_crtR3Users >= 1 && !g_frtR3Initializing;
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | RTR3DECL(bool) RTR3InitIsUnobtrusive(void)
|
---|
656 | {
|
---|
657 | return RT_BOOL(g_fInitFlags & RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
658 | }
|
---|
659 |
|
---|
660 |
|
---|
661 | #if 0 /** @todo implement RTR3Term. */
|
---|
662 | RTR3DECL(void) RTR3Term(void)
|
---|
663 | {
|
---|
664 | }
|
---|
665 | #endif
|
---|
666 |
|
---|