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