VirtualBox

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

Last change on this file since 28863 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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