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