1 | /* $Id: init.cpp 36549 2011-04-05 09:28:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Init Ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 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 | #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 | #endif
|
---|
48 | #include <locale.h>
|
---|
49 |
|
---|
50 | #include <iprt/initterm.h>
|
---|
51 | #include <iprt/asm.h>
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/err.h>
|
---|
54 | #include <iprt/log.h>
|
---|
55 | #include <iprt/path.h>
|
---|
56 | #include <iprt/time.h>
|
---|
57 | #include <iprt/string.h>
|
---|
58 | #include <iprt/param.h>
|
---|
59 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
60 | # include <iprt/file.h>
|
---|
61 | # include <VBox/sup.h>
|
---|
62 | #endif
|
---|
63 | #include <stdlib.h>
|
---|
64 |
|
---|
65 | #include "internal/alignmentchecks.h"
|
---|
66 | #include "internal/path.h"
|
---|
67 | #include "internal/process.h"
|
---|
68 | #include "internal/thread.h"
|
---|
69 | #include "internal/thread.h"
|
---|
70 | #include "internal/time.h"
|
---|
71 |
|
---|
72 |
|
---|
73 | /*******************************************************************************
|
---|
74 | * Global Variables *
|
---|
75 | *******************************************************************************/
|
---|
76 | /** The number of calls to RTR3Init. */
|
---|
77 | static int32_t volatile g_cUsers = 0;
|
---|
78 | /** Whether we're currently initializing the IPRT. */
|
---|
79 | static bool volatile g_fInitializing = false;
|
---|
80 |
|
---|
81 | /** The process path.
|
---|
82 | * This is used by RTPathExecDir and RTProcGetExecutablePath and set by rtProcInitName. */
|
---|
83 | DECLHIDDEN(char) g_szrtProcExePath[RTPATH_MAX];
|
---|
84 | /** The length of g_szrtProcExePath. */
|
---|
85 | DECLHIDDEN(size_t) g_cchrtProcExePath;
|
---|
86 | /** The length of directory path component of g_szrtProcExePath. */
|
---|
87 | DECLHIDDEN(size_t) g_cchrtProcDir;
|
---|
88 | /** The offset of the process name into g_szrtProcExePath. */
|
---|
89 | DECLHIDDEN(size_t) g_offrtProcName;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Program start nanosecond TS.
|
---|
93 | */
|
---|
94 | DECLHIDDEN(uint64_t) g_u64ProgramStartNanoTS;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Program start microsecond TS.
|
---|
98 | */
|
---|
99 | DECLHIDDEN(uint64_t) g_u64ProgramStartMicroTS;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Program start millisecond TS.
|
---|
103 | */
|
---|
104 | DECLHIDDEN(uint64_t) g_u64ProgramStartMilliTS;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * The process identifier of the running process.
|
---|
108 | */
|
---|
109 | DECLHIDDEN(RTPROCESS) g_ProcessSelf = NIL_RTPROCESS;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * The current process priority.
|
---|
113 | */
|
---|
114 | DECLHIDDEN(RTPROCPRIORITY) g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Set if the atexit callback has been called, i.e. indicating
|
---|
118 | * that the process is terminating.
|
---|
119 | */
|
---|
120 | DECLHIDDEN(bool volatile) g_frtAtExitCalled = false;
|
---|
121 |
|
---|
122 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
123 | /**
|
---|
124 | * Whether alignment checks are enabled.
|
---|
125 | * This is set if the environment variable IPRT_ALIGNMENT_CHECKS is 1.
|
---|
126 | */
|
---|
127 | RTDATADECL(bool) g_fRTAlignmentChecks = false;
|
---|
128 | #endif
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * atexit callback.
|
---|
133 | *
|
---|
134 | * This makes sure any loggers are flushed and will later also work the
|
---|
135 | * termination callback chain.
|
---|
136 | */
|
---|
137 | static void rtR3ExitCallback(void)
|
---|
138 | {
|
---|
139 | ASMAtomicWriteBool(&g_frtAtExitCalled, true);
|
---|
140 |
|
---|
141 | if (g_cUsers > 0)
|
---|
142 | {
|
---|
143 | PRTLOGGER pLogger = RTLogGetDefaultInstance();
|
---|
144 | if (pLogger)
|
---|
145 | RTLogFlush(pLogger);
|
---|
146 |
|
---|
147 | pLogger = RTLogRelDefaultInstance();
|
---|
148 | if (pLogger)
|
---|
149 | RTLogFlush(pLogger);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | #ifndef RT_OS_WINDOWS
|
---|
155 | /**
|
---|
156 | * Fork callback, child context.
|
---|
157 | */
|
---|
158 | static void rtR3ForkChildCallback(void)
|
---|
159 | {
|
---|
160 | g_ProcessSelf = getpid();
|
---|
161 | }
|
---|
162 | #endif /* RT_OS_WINDOWS */
|
---|
163 |
|
---|
164 | #ifdef RT_OS_OS2
|
---|
165 | /** Fork completion callback for OS/2. Only called in the child. */
|
---|
166 | static void rtR3ForkOs2ChildCompletionCallback(void *pvArg, int rc, __LIBC_FORKCTX enmCtx)
|
---|
167 | {
|
---|
168 | Assert(enmCtx == __LIBC_FORK_CTX_CHILD); NOREF(enmCtx);
|
---|
169 | NOREF(pvArg);
|
---|
170 |
|
---|
171 | if (!rc)
|
---|
172 | rtR3ForkChildCallback();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** Low-level fork callback for OS/2. */
|
---|
176 | int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
|
---|
177 | {
|
---|
178 | if (enmOperation == __LIBC_FORK_OP_EXEC_CHILD)
|
---|
179 | return pForkHandle->pfnCompletionCallback(pForkHandle, rtR3ForkOs2ChildCompletionCallback, NULL, __LIBC_FORK_CTX_CHILD);
|
---|
180 | return 0;
|
---|
181 | }
|
---|
182 |
|
---|
183 | _FORK_CHILD1(0, rtR3ForkOs2Child);
|
---|
184 | #endif /* RT_OS_OS2 */
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Internal worker which initializes or re-initializes the
|
---|
190 | * program path, name and directory globals.
|
---|
191 | *
|
---|
192 | * @returns IPRT status code.
|
---|
193 | * @param pszProgramPath The program path, NULL if not specified.
|
---|
194 | */
|
---|
195 | static int rtR3InitProgramPath(const char *pszProgramPath)
|
---|
196 | {
|
---|
197 | /*
|
---|
198 | * We're reserving 32 bytes here for file names as what not.
|
---|
199 | */
|
---|
200 | if (!pszProgramPath)
|
---|
201 | {
|
---|
202 | int rc = rtProcInitExePath(g_szrtProcExePath, sizeof(g_szrtProcExePath) - 32);
|
---|
203 | if (RT_FAILURE(rc))
|
---|
204 | return rc;
|
---|
205 | }
|
---|
206 | else
|
---|
207 | {
|
---|
208 | size_t cch = strlen(pszProgramPath);
|
---|
209 | Assert(cch > 1);
|
---|
210 | AssertMsgReturn(cch < sizeof(g_szrtProcExePath) - 32, ("%zu\n", cch), VERR_BUFFER_OVERFLOW);
|
---|
211 | memcpy(g_szrtProcExePath, pszProgramPath, cch + 1);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Parse the name.
|
---|
216 | */
|
---|
217 | ssize_t offName;
|
---|
218 | g_cchrtProcExePath = RTPathParse(g_szrtProcExePath, &g_cchrtProcDir, &offName, NULL);
|
---|
219 | g_offrtProcName = offName;
|
---|
220 | return VINF_SUCCESS;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | #ifdef IPRT_USE_SIG_CHILD_DUMMY
|
---|
225 | /**
|
---|
226 | * Dummy SIGCHILD handler.
|
---|
227 | *
|
---|
228 | * Assigned on rtR3Init only when SIGCHILD handler is set SIGIGN or SIGDEF to
|
---|
229 | * ensure waitpid works properly for the terminated processes.
|
---|
230 | */
|
---|
231 | static void rtR3SigChildHandler(int iSignal)
|
---|
232 | {
|
---|
233 | NOREF(iSignal);
|
---|
234 | }
|
---|
235 | #endif /* IPRT_USE_SIG_CHILD_DUMMY */
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * rtR3Init worker.
|
---|
240 | */
|
---|
241 | static int rtR3InitBody(bool fInitSUPLib, const char *pszProgramPath)
|
---|
242 | {
|
---|
243 | /*
|
---|
244 | * Init C runtime locale before we do anything that may end up converting
|
---|
245 | * paths or we'll end up using the "C" locale for path conversion.
|
---|
246 | */
|
---|
247 | setlocale(LC_CTYPE, "");
|
---|
248 |
|
---|
249 | /*
|
---|
250 | * The Process ID.
|
---|
251 | */
|
---|
252 | #ifdef _MSC_VER
|
---|
253 | g_ProcessSelf = _getpid(); /* crappy ansi compiler */
|
---|
254 | #else
|
---|
255 | g_ProcessSelf = getpid();
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
259 | # ifdef VBOX
|
---|
260 | /*
|
---|
261 | * This MUST be done as the very first thing, before any file is opened.
|
---|
262 | * The log is opened on demand, but the first log entries may be caused
|
---|
263 | * by rtThreadInit() below.
|
---|
264 | */
|
---|
265 | const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
|
---|
266 | if ( pszDisableHostCache != NULL
|
---|
267 | && *pszDisableHostCache
|
---|
268 | && strcmp(pszDisableHostCache, "0") != 0)
|
---|
269 | {
|
---|
270 | RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
|
---|
271 | RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
|
---|
272 | }
|
---|
273 | # endif /* VBOX */
|
---|
274 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * Thread Thread database and adopt the caller thread as 'main'.
|
---|
278 | * This must be done before everything else or else we'll call into threading
|
---|
279 | * without having initialized TLS entries and suchlike.
|
---|
280 | */
|
---|
281 | int rc = rtThreadInit();
|
---|
282 | AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
|
---|
283 |
|
---|
284 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
285 | if (fInitSUPLib)
|
---|
286 | {
|
---|
287 | /*
|
---|
288 | * Init GIP first.
|
---|
289 | * (The more time for updates before real use, the better.)
|
---|
290 | */
|
---|
291 | rc = SUPR3Init(NULL);
|
---|
292 | AssertMsgRCReturn(rc, ("Failed to initializable the support library, rc=%Rrc!\n", rc), rc);
|
---|
293 | }
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * The executable path, name and directory.
|
---|
298 | */
|
---|
299 | rc = rtR3InitProgramPath(pszProgramPath);
|
---|
300 | AssertLogRelMsgRCReturn(rc, ("Failed to get executable directory path, rc=%Rrc!\n", rc), rc);
|
---|
301 |
|
---|
302 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
303 | /*
|
---|
304 | * The threading is initialized we can safely sleep a bit if GIP
|
---|
305 | * needs some time to update itself updating.
|
---|
306 | */
|
---|
307 | if (fInitSUPLib && g_pSUPGlobalInfoPage)
|
---|
308 | {
|
---|
309 | RTThreadSleep(20);
|
---|
310 | RTTimeNanoTS();
|
---|
311 | }
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Init the program start TSes.
|
---|
316 | * Do that here to be sure that the GIP time was properly updated the 1st time.
|
---|
317 | */
|
---|
318 | g_u64ProgramStartNanoTS = RTTimeNanoTS();
|
---|
319 | g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
|
---|
320 | g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * The remainder cannot easily be undone, so it has to go last.
|
---|
324 | */
|
---|
325 |
|
---|
326 | /* Fork and exit callbacks. */
|
---|
327 | #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|
---|
328 | rc = pthread_atfork(NULL, NULL, rtR3ForkChildCallback);
|
---|
329 | AssertMsg(rc == 0, ("%d\n", rc));
|
---|
330 | #endif
|
---|
331 | atexit(rtR3ExitCallback);
|
---|
332 |
|
---|
333 | #ifdef IPRT_USE_SIG_CHILD_DUMMY
|
---|
334 | /*
|
---|
335 | * SIGCHLD must not be ignored (that's default), otherwise posix compliant waitpid
|
---|
336 | * implementations won't work right.
|
---|
337 | */
|
---|
338 | for (;;)
|
---|
339 | {
|
---|
340 | struct sigaction saOld;
|
---|
341 | rc = sigaction(SIGCHLD, 0, &saOld); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
342 | if ( rc != 0
|
---|
343 | || (saOld.sa_flags & SA_SIGINFO)
|
---|
344 | || ( saOld.sa_handler != SIG_IGN
|
---|
345 | && saOld.sa_handler != SIG_DFL)
|
---|
346 | )
|
---|
347 | break;
|
---|
348 |
|
---|
349 | /* Try install dummy handler. */
|
---|
350 | struct sigaction saNew = saOld;
|
---|
351 | saNew.sa_flags = SA_NOCLDSTOP | SA_RESTART;
|
---|
352 | saNew.sa_handler = rtR3SigChildHandler;
|
---|
353 | rc = sigemptyset(&saNew.sa_mask); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
354 | struct sigaction saOld2;
|
---|
355 | rc = sigaction(SIGCHLD, &saNew, &saOld2); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
|
---|
356 | if ( rc != 0
|
---|
357 | || ( saOld2.sa_handler == saOld.sa_handler
|
---|
358 | && !(saOld2.sa_flags & SA_SIGINFO))
|
---|
359 | )
|
---|
360 | break;
|
---|
361 |
|
---|
362 | /* Race during dynamic load, restore and try again... */
|
---|
363 | sigaction(SIGCHLD, &saOld2, NULL);
|
---|
364 | RTThreadYield();
|
---|
365 | }
|
---|
366 | #endif /* IPRT_USE_SIG_CHILD_DUMMY */
|
---|
367 |
|
---|
368 | #ifdef IPRT_WITH_ALIGNMENT_CHECKS
|
---|
369 | /*
|
---|
370 | * Enable alignment checks.
|
---|
371 | */
|
---|
372 | const char *pszAlignmentChecks = getenv("IPRT_ALIGNMENT_CHECKS");
|
---|
373 | g_fRTAlignmentChecks = pszAlignmentChecks != NULL
|
---|
374 | && pszAlignmentChecks[0] == '1'
|
---|
375 | && pszAlignmentChecks[1] == '\0';
|
---|
376 | if (g_fRTAlignmentChecks)
|
---|
377 | IPRT_ALIGNMENT_CHECKS_ENABLE();
|
---|
378 | #endif
|
---|
379 |
|
---|
380 | return VINF_SUCCESS;
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Internal initialization worker.
|
---|
386 | *
|
---|
387 | * @returns IPRT status code.
|
---|
388 | * @param fInitSUPLib Whether to call SUPR3Init.
|
---|
389 | * @param pszProgramPath The program path, NULL if not specified.
|
---|
390 | */
|
---|
391 | static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
|
---|
392 | {
|
---|
393 | /* no entry log flow, because prefixes and thread may freak out. */
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Do reference counting, only initialize the first time around.
|
---|
397 | *
|
---|
398 | * We are ASSUMING that nobody will be able to race RTR3Init calls when the
|
---|
399 | * first one, the real init, is running (second assertion).
|
---|
400 | */
|
---|
401 | int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
|
---|
402 | if (cUsers != 1)
|
---|
403 | {
|
---|
404 | AssertMsg(cUsers > 1, ("%d\n", cUsers));
|
---|
405 | Assert(!g_fInitializing);
|
---|
406 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
407 | if (fInitSUPLib)
|
---|
408 | SUPR3Init(NULL);
|
---|
409 | #endif
|
---|
410 | if (!pszProgramPath)
|
---|
411 | return VINF_SUCCESS;
|
---|
412 | return rtR3InitProgramPath(pszProgramPath);
|
---|
413 | }
|
---|
414 | ASMAtomicWriteBool(&g_fInitializing, true);
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Do the initialization.
|
---|
418 | */
|
---|
419 | int rc = rtR3InitBody(fInitSUPLib, pszProgramPath);
|
---|
420 | if (RT_FAILURE(rc))
|
---|
421 | {
|
---|
422 | /* failure */
|
---|
423 | ASMAtomicWriteBool(&g_fInitializing, false);
|
---|
424 | ASMAtomicDecS32(&g_cUsers);
|
---|
425 | return rc;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /* success */
|
---|
429 | LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
|
---|
430 | ASMAtomicWriteBool(&g_fInitializing, false);
|
---|
431 | return VINF_SUCCESS;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | RTR3DECL(int) RTR3Init(void)
|
---|
436 | {
|
---|
437 | return rtR3Init(false /* fInitSUPLib */, NULL);
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|
441 | RTR3DECL(int) RTR3InitEx(uint32_t iVersion, const char *pszProgramPath, bool fInitSUPLib)
|
---|
442 | {
|
---|
443 | AssertReturn(iVersion == 0, VERR_NOT_SUPPORTED);
|
---|
444 | return rtR3Init(fInitSUPLib, pszProgramPath);
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | RTR3DECL(int) RTR3InitWithProgramPath(const char *pszProgramPath)
|
---|
449 | {
|
---|
450 | return rtR3Init(false /* fInitSUPLib */, pszProgramPath);
|
---|
451 | }
|
---|
452 |
|
---|
453 |
|
---|
454 | RTR3DECL(int) RTR3InitAndSUPLib(void)
|
---|
455 | {
|
---|
456 | return rtR3Init(true /* fInitSUPLib */, NULL /* pszProgramPath */);
|
---|
457 | }
|
---|
458 |
|
---|
459 |
|
---|
460 | RTR3DECL(int) RTR3InitAndSUPLibWithProgramPath(const char *pszProgramPath)
|
---|
461 | {
|
---|
462 | return rtR3Init(true /* fInitSUPLib */, pszProgramPath);
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 | #if 0 /** @todo implement RTR3Term. */
|
---|
467 | RTR3DECL(void) RTR3Term(void)
|
---|
468 | {
|
---|
469 | }
|
---|
470 | #endif
|
---|
471 |
|
---|