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