1 | /* $Id: init.cpp 106787 2024-10-30 11:44:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Init Ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 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 <errno.h>
|
---|
51 | # define IPRT_USE_SIG_CHILD_DUMMY
|
---|
52 | # endif
|
---|
53 | # include <signal.h>
|
---|
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/system.h"
|
---|
91 | #include "internal/thread.h"
|
---|
92 | #include "internal/time.h"
|
---|
93 |
|
---|
94 |
|
---|
95 | /*********************************************************************************************************************************
|
---|
96 | * Global Variables *
|
---|
97 | *********************************************************************************************************************************/
|
---|
98 | /** The IPRT init flags. */
|
---|
99 | static uint32_t g_fInitFlags;
|
---|
100 |
|
---|
101 | /** The argument count of the program. */
|
---|
102 | static int g_crtArgs = -1;
|
---|
103 | /** The arguments of the program (UTF-8). This is "leaked". */
|
---|
104 | static char ** g_papszrtArgs;
|
---|
105 | /** The original argument vector of the program. */
|
---|
106 | static char ** g_papszrtOrgArgs;
|
---|
107 |
|
---|
108 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
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
|
---|
115 |
|
---|
116 |
|
---|
117 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_HAIKU) \
|
---|
118 | || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS) /** @todo add host init hooks everywhere. */
|
---|
119 | /* Stubs */
|
---|
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); }
|
---|
123 | #endif
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * atexit callback.
|
---|
128 | *
|
---|
129 | * This makes sure any loggers are flushed and will later also work the
|
---|
130 | * termination callback chain.
|
---|
131 | */
|
---|
132 | static void rtR3ExitCallback(void) RT_NOTHROW_DEF
|
---|
133 | {
|
---|
134 | ASMAtomicWriteBool(&g_frtAtExitCalled, true);
|
---|
135 |
|
---|
136 | if (g_crtR3Users > 0)
|
---|
137 | {
|
---|
138 | PRTLOGGER pLogger = RTLogGetDefaultInstance();
|
---|
139 | if (pLogger)
|
---|
140 | RTLogFlush(pLogger);
|
---|
141 |
|
---|
142 | pLogger = RTLogRelGetDefaultInstance();
|
---|
143 | if (pLogger)
|
---|
144 | RTLogFlush(pLogger);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
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 */
|
---|
158 |
|
---|
159 | #ifdef RT_OS_OS2
|
---|
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 |
|
---|
170 | /** Low-level fork callback for OS/2. */
|
---|
171 | int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
|
---|
172 | {
|
---|
173 | if (enmOperation == __LIBC_FORK_OP_EXEC_CHILD)
|
---|
174 | return pForkHandle->pfnCompletionCallback(pForkHandle, rtR3ForkOs2ChildCompletionCallback, NULL, __LIBC_FORK_CTX_CHILD);
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | # define static static volatile /** @todo _FORK_CHILD1 causes unresolved externals in optimized builds. Fix macro. */
|
---|
179 | _FORK_CHILD1(0, rtR3ForkOs2Child);
|
---|
180 | # undef static
|
---|
181 | #endif /* RT_OS_OS2 */
|
---|
182 |
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Internal worker which initializes or re-initializes the
|
---|
187 | * program path, name and directory globals.
|
---|
188 | *
|
---|
189 | * @returns IPRT status code.
|
---|
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;
|
---|
215 | g_cchrtProcExePath = RTPathParseSimple(g_szrtProcExePath, &g_cchrtProcExeDir, &offName, NULL);
|
---|
216 | g_offrtProcName = offName;
|
---|
217 | return VINF_SUCCESS;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | #ifdef RT_OS_WINDOWS
|
---|
222 | /**
|
---|
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 | /**
|
---|
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 |
|
---|
267 | #if !defined(IPRT_NO_CRT) || !defined(RT_OS_WINDOWS)
|
---|
268 | if (!(fFlags & RTR3INIT_FLAGS_UTF8_ARGV))
|
---|
269 | {
|
---|
270 | /*
|
---|
271 | * Convert the arguments.
|
---|
272 | */
|
---|
273 | char **papszArgs;
|
---|
274 |
|
---|
275 | # ifdef RT_OS_WINDOWS
|
---|
276 | /* HACK ALERT! Try convert from unicode versions if possible.
|
---|
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))
|
---|
282 | {
|
---|
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));
|
---|
293 | }
|
---|
294 | else
|
---|
295 | # endif
|
---|
296 | {
|
---|
297 | papszArgs = (char **)RTMemAllocZTag((cArgs + 1) * sizeof(char *), "will-leak:rtR3InitArgv");
|
---|
298 | if (!papszArgs)
|
---|
299 | return VERR_NO_MEMORY;
|
---|
300 |
|
---|
301 | for (int i = 0; i < cArgs; i++)
|
---|
302 | {
|
---|
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 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | papszArgs[cArgs] = NULL;
|
---|
315 |
|
---|
316 | g_papszrtOrgArgs = papszOrgArgs;
|
---|
317 | g_papszrtArgs = papszArgs;
|
---|
318 | g_crtArgs = cArgs;
|
---|
319 |
|
---|
320 | *ppapszArgs = papszArgs;
|
---|
321 | }
|
---|
322 | else
|
---|
323 | #endif /* !IPRT_NO_CRT || !RT_OS_WINDOWS */
|
---|
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 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | return VINF_SUCCESS;
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | #ifdef IPRT_USE_SIG_CHILD_DUMMY
|
---|
339 | /**
|
---|
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 | /**
|
---|
353 | * rtR3Init worker.
|
---|
354 | */
|
---|
355 | static int rtR3InitBody(uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
356 | {
|
---|
357 | /*
|
---|
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 |
|
---|
370 | #ifndef IPRT_NO_CRT
|
---|
371 | /*
|
---|
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, "");
|
---|
376 | #endif
|
---|
377 |
|
---|
378 | /*
|
---|
379 | * The Process ID.
|
---|
380 | */
|
---|
381 | #ifdef _MSC_VER
|
---|
382 | g_ProcessSelf = GetCurrentProcessId(); /* since NT 3.1, not 3.51+ as listed on geoffchappell.com */
|
---|
383 | #else
|
---|
384 | g_ProcessSelf = getpid();
|
---|
385 | #endif
|
---|
386 |
|
---|
387 | /*
|
---|
388 | * Save the init flags.
|
---|
389 | */
|
---|
390 | g_fInitFlags |= fFlags;
|
---|
391 |
|
---|
392 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
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
|
---|
401 | && *pszDisableHostCache
|
---|
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 */
|
---|
408 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
409 |
|
---|
410 | #if defined(RT_ARCH_ARM64) && defined(RT_OS_LINUX)
|
---|
411 | /*
|
---|
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 | /*
|
---|
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 | */
|
---|
423 | rc = rtThreadInit();
|
---|
424 | AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
|
---|
425 |
|
---|
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 |
|
---|
432 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
433 | /*
|
---|
434 | * Initialize SUPLib here so the GIP can get going as early as possible
|
---|
435 | * (improves accuracy for the first client).
|
---|
436 | */
|
---|
437 | if (fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB))
|
---|
438 | {
|
---|
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*/);
|
---|
442 | AssertMsgReturn(RT_SUCCESS(rc) || (fFlags & RTR3INIT_FLAGS_TRY_SUPLIB),
|
---|
443 | ("Failed to initialize the support library, rc=%Rrc!\n", rc), rc);
|
---|
444 | }
|
---|
445 | #endif
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Convert arguments.
|
---|
449 | */
|
---|
450 | rc = rtR3InitArgv(fFlags, cArgs, ppapszArgs);
|
---|
451 | AssertLogRelMsgRCReturn(rc, ("Failed to convert the arguments, rc=%Rrc!\n", rc), rc);
|
---|
452 |
|
---|
453 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
454 | /*
|
---|
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.
|
---|
459 | */
|
---|
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)
|
---|
465 | {
|
---|
466 | PSUPGIPCPU pGipCpu = SUPGetGipCpuPtr(pGip);
|
---|
467 | if ( pGipCpu
|
---|
468 | && pGipCpu->u32TransactionId <= 4)
|
---|
469 | {
|
---|
470 | RTThreadSleep(pGip->u32UpdateIntervalNS / RT_NS_1MS + 2);
|
---|
471 | RTTimeNanoTS();
|
---|
472 | }
|
---|
473 | }
|
---|
474 | #endif
|
---|
475 |
|
---|
476 | /*
|
---|
477 | * Init the program start timestamp TS.
|
---|
478 | * Do that here to be sure that the GIP time was properly updated the 1st time.
|
---|
479 | */
|
---|
480 | g_u64ProgramStartNanoTS = RTTimeNanoTS();
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * The remainder cannot easily be undone, so it has to go last.
|
---|
484 | */
|
---|
485 |
|
---|
486 | /* Fork and exit callbacks. */
|
---|
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
|
---|
491 | atexit(rtR3ExitCallback);
|
---|
492 |
|
---|
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 |
|
---|
528 | #ifndef RT_OS_WINDOWS
|
---|
529 | /*
|
---|
530 | * Ignore SIGPIPE if it is configured as SIG_DFL.
|
---|
531 | */
|
---|
532 | if (!(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
|
---|
533 | {
|
---|
534 | struct sigaction SigActOld;
|
---|
535 | rc = sigaction(SIGPIPE, 0, &SigActOld);
|
---|
536 | AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
537 | if ( rc == 0
|
---|
538 | && !(SigActOld.sa_flags & SA_SIGINFO)
|
---|
539 | && SigActOld.sa_handler == SIG_DFL)
|
---|
540 | {
|
---|
541 | struct sigaction SigAct;
|
---|
542 | memset(&SigAct, 0, sizeof(SigAct));
|
---|
543 | SigAct.sa_handler = SIG_IGN;
|
---|
544 | rc = sigaction(SIGPIPE, &SigAct, &SigActOld);
|
---|
545 | AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
546 | Assert(SigActOld.sa_handler == SIG_DFL || rc != 0);
|
---|
547 | }
|
---|
548 | }
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
552 | /*
|
---|
553 | * Enable alignment checks.
|
---|
554 | */
|
---|
555 | const char *pszAlignmentChecks = RTEnvGet("IPRT_ALIGNMENT_CHECKS"); /** @todo add RTEnvGetBool */
|
---|
556 | g_fRTAlignmentChecks = pszAlignmentChecks != NULL
|
---|
557 | && pszAlignmentChecks[0] == '1'
|
---|
558 | && pszAlignmentChecks[1] == '\0';
|
---|
559 | if (g_fRTAlignmentChecks)
|
---|
560 | IPRT_ALIGNMENT_CHECKS_ENABLE();
|
---|
561 | #endif
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * Final native initialization.
|
---|
565 | */
|
---|
566 | rc = rtR3InitNativeFinal(fFlags);
|
---|
567 | AssertMsgRCReturn(rc, ("rtR3InitNativeFinal failed with %Rrc\n", rc), rc);
|
---|
568 |
|
---|
569 | return VINF_SUCCESS;
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Internal initialization worker.
|
---|
575 | *
|
---|
576 | * @returns IPRT status code.
|
---|
577 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
578 | * @param cArgs Pointer to the argument count.
|
---|
579 | * @param ppapszArgs Pointer to the argument vector pointer. NULL
|
---|
580 | * allowed if @a cArgs is 0.
|
---|
581 | * @param pszProgramPath The program path. Pass NULL if we're to figure it
|
---|
582 | * out ourselves.
|
---|
583 | */
|
---|
584 | static int rtR3Init(uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
585 | {
|
---|
586 | /* no entry log flow, because prefixes and thread may freak out. */
|
---|
587 | Assert(!(fFlags & ~RTR3INIT_FLAGS_VALID_MASK));
|
---|
588 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL) || cArgs == 0);
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Do reference counting, only initialize the first time around.
|
---|
592 | *
|
---|
593 | * We are ASSUMING that nobody will be able to race RTR3Init* calls when the
|
---|
594 | * first one, the real init, is running (second assertion).
|
---|
595 | */
|
---|
596 | int32_t cUsers = ASMAtomicIncS32(&g_crtR3Users);
|
---|
597 | if (cUsers != 1)
|
---|
598 | {
|
---|
599 | AssertMsg(cUsers > 1, ("%d\n", cUsers));
|
---|
600 | Assert(!g_frtR3Initializing);
|
---|
601 |
|
---|
602 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
603 | /* Initialize the support library if requested. We've always ignored the
|
---|
604 | status code here for some reason, making the two flags same. */
|
---|
605 | if (fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB))
|
---|
606 | {
|
---|
607 | if (!(fFlags & ((SUPR3INIT_F_UNRESTRICTED | SUPR3INIT_F_LIMITED) << RTR3INIT_FLAGS_SUPLIB_SHIFT)))
|
---|
608 | fFlags |= SUPR3INIT_F_UNRESTRICTED << RTR3INIT_FLAGS_SUPLIB_SHIFT;
|
---|
609 | SUPR3InitEx(fFlags >> RTR3INIT_FLAGS_SUPLIB_SHIFT, NULL /*ppSession*/);
|
---|
610 | g_fInitFlags |= fFlags & (RTR3INIT_FLAGS_SUPLIB | RTR3INIT_FLAGS_TRY_SUPLIB | RTR3INIT_FLAGS_SUPLIB_MASK);
|
---|
611 | }
|
---|
612 | #endif
|
---|
613 | g_fInitFlags |= fFlags & RTR3INIT_FLAGS_UTF8_ARGV;
|
---|
614 |
|
---|
615 | if ( !(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE)
|
---|
616 | && (g_fInitFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
|
---|
617 | {
|
---|
618 | g_fInitFlags &= ~RTR3INIT_FLAGS_UNOBTRUSIVE;
|
---|
619 | g_fInitFlags |= fFlags & RTR3INIT_FLAGS_STANDALONE_APP;
|
---|
620 | rtR3InitNativeObtrusive(g_fInitFlags | fFlags);
|
---|
621 | rtThreadReInitObtrusive();
|
---|
622 | }
|
---|
623 | else
|
---|
624 | Assert(!(fFlags & RTR3INIT_FLAGS_STANDALONE_APP) || (g_fInitFlags & RTR3INIT_FLAGS_STANDALONE_APP));
|
---|
625 |
|
---|
626 | int rc = VINF_SUCCESS;
|
---|
627 | if (pszProgramPath)
|
---|
628 | rc = rtR3InitProgramPath(pszProgramPath);
|
---|
629 | if (RT_SUCCESS(rc))
|
---|
630 | rc = rtR3InitArgv(fFlags, cArgs, ppapszArgs);
|
---|
631 | return rc;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Do the initialization.
|
---|
636 | */
|
---|
637 | ASMAtomicWriteBool(&g_frtR3Initializing, true);
|
---|
638 | int rc = rtR3InitBody(fFlags, cArgs, ppapszArgs, pszProgramPath);
|
---|
639 | ASMAtomicWriteBool(&g_frtR3Initializing, false);
|
---|
640 | if (RT_FAILURE(rc))
|
---|
641 | {
|
---|
642 | /* failure */
|
---|
643 | ASMAtomicDecS32(&g_crtR3Users);
|
---|
644 | return rc;
|
---|
645 | }
|
---|
646 |
|
---|
647 | /* success */
|
---|
648 | LogFlow(("rtR3Init: returns VINF_SUCCESS\n"));
|
---|
649 | return VINF_SUCCESS;
|
---|
650 | }
|
---|
651 |
|
---|
652 |
|
---|
653 | RTR3DECL(int) RTR3InitExe(int cArgs, char ***ppapszArgs, uint32_t fFlags)
|
---|
654 | {
|
---|
655 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
656 | return rtR3Init(fFlags, cArgs, ppapszArgs, NULL);
|
---|
657 | }
|
---|
658 |
|
---|
659 |
|
---|
660 | RTR3DECL(int) RTR3InitExeNoArguments(uint32_t fFlags)
|
---|
661 | {
|
---|
662 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
663 | return rtR3Init(fFlags, 0, NULL, NULL);
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | RTR3DECL(int) RTR3InitDll(uint32_t fFlags)
|
---|
668 | {
|
---|
669 | Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
|
---|
670 | return rtR3Init(fFlags | RTR3INIT_FLAGS_DLL, 0, NULL, NULL);
|
---|
671 | }
|
---|
672 |
|
---|
673 |
|
---|
674 | RTR3DECL(int) RTR3InitEx(uint32_t iVersion, uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath)
|
---|
675 | {
|
---|
676 | AssertReturn(iVersion == RTR3INIT_VER_CUR, VERR_NOT_SUPPORTED);
|
---|
677 | return rtR3Init(fFlags, cArgs, ppapszArgs, pszProgramPath);
|
---|
678 | }
|
---|
679 |
|
---|
680 |
|
---|
681 | RTR3DECL(bool) RTR3InitIsInitialized(void)
|
---|
682 | {
|
---|
683 | return g_crtR3Users >= 1 && !g_frtR3Initializing;
|
---|
684 | }
|
---|
685 |
|
---|
686 |
|
---|
687 | RTR3DECL(bool) RTR3InitIsUnobtrusive(void)
|
---|
688 | {
|
---|
689 | return RT_BOOL(g_fInitFlags & RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 | #if 0 /** @todo implement RTR3Term. */
|
---|
694 | RTR3DECL(void) RTR3Term(void)
|
---|
695 | {
|
---|
696 | }
|
---|
697 | #endif
|
---|
698 |
|
---|