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