VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/init.cpp@ 95806

Last change on this file since 95806 was 95806, checked in by vboxsync, 3 years ago

IPRT/r3/init.cpp: Call GetCurrentProcessId directly instead of _getpid() on windows, use RTEnvGet() instead of getenv(), assume argv is UTF-8 in IPRT_NO_CRT mode, and skip setlocale in IPRT_NO_CRT mode. bugref:10261

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette