VirtualBox

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

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

darwin build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.4 KB
Line 
1/* $Id: init.cpp 20839 2009-06-23 14:20:17Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_DEFAULT
36#include <iprt/types.h> /* darwin: UINT32_C and others. */
37
38#ifdef RT_OS_WINDOWS
39# include <process.h>
40#else
41# include <unistd.h>
42# ifndef RT_OS_OS2
43# include <pthread.h>
44# endif
45#endif
46#ifdef RT_OS_OS2
47# include <InnoTekLIBC/fork.h>
48#endif
49#include <locale.h>
50
51#include <iprt/initterm.h>
52#include <iprt/asm.h>
53#include <iprt/assert.h>
54#include <iprt/err.h>
55#include <iprt/log.h>
56#include <iprt/path.h>
57#include <iprt/time.h>
58#include <iprt/string.h>
59#include <iprt/param.h>
60#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
61# include <iprt/file.h>
62# include <VBox/sup.h>
63#endif
64#include <stdlib.h>
65
66#include "internal/alignmentchecks.h"
67#include "internal/path.h"
68#include "internal/process.h"
69#include "internal/thread.h"
70#include "internal/thread.h"
71#include "internal/time.h"
72
73
74/*******************************************************************************
75* Global Variables *
76*******************************************************************************/
77/** The number of calls to RTR3Init. */
78static int32_t volatile g_cUsers = 0;
79/** Whether we're currently initializing the IPRT. */
80static bool volatile g_fInitializing = false;
81
82/** The process path.
83 * This is used by RTPathExecDir and RTProcGetExecutableName and set by rtProcInitName. */
84char g_szrtProcExePath[RTPATH_MAX];
85/** The length of g_szrtProcExePath. */
86size_t g_cchrtProcExePath;
87/** The length of directory path component of g_szrtProcExePath. */
88size_t g_cchrtProcDir;
89/** The offset of the process name into g_szrtProcExePath. */
90size_t g_offrtProcName;
91
92/**
93 * Program start nanosecond TS.
94 */
95uint64_t g_u64ProgramStartNanoTS;
96
97/**
98 * Program start microsecond TS.
99 */
100uint64_t g_u64ProgramStartMicroTS;
101
102/**
103 * Program start millisecond TS.
104 */
105uint64_t g_u64ProgramStartMilliTS;
106
107/**
108 * The process identifier of the running process.
109 */
110RTPROCESS g_ProcessSelf = NIL_RTPROCESS;
111
112/**
113 * The current process priority.
114 */
115RTPROCPRIORITY g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
116
117#ifdef DEBUG
118/**
119 * Whether alignment checks are enabled.
120 * This is set if the environment variable IPRT_ALIGNMENT_CHECKS is 1.
121 */
122RTDATADECL(bool) g_fRTAlignmentChecks = false;
123#endif
124
125
126
127#ifndef RT_OS_WINDOWS
128/**
129 * Fork callback, child context.
130 */
131static void rtR3ForkChildCallback(void)
132{
133 g_ProcessSelf = getpid();
134}
135#endif /* RT_OS_WINDOWS */
136
137#ifdef RT_OS_OS2
138/** Low-level fork callback for OS/2. */
139int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
140{
141 if (enmOperation == __LIBC_FORK_STAGE_COMPLETION_CHILD)
142 rtR3ForkChildCallback();
143 return 0;
144}
145
146_FORK_CHILD1(0, rtR3ForkOs2Child);
147#endif /* RT_OS_OS2 */
148
149
150
151/**
152 * Internal worker which initializes or re-initializes the
153 * program path, name and directory globals.
154 *
155 * @returns IPRT status code.
156 * @param pszProgramPath The program path, NULL if not specified.
157 */
158static int rtR3InitProgramPath(const char *pszProgramPath)
159{
160 /*
161 * We're reserving 32 bytes here for file names as what not.
162 */
163 if (!pszProgramPath)
164 {
165 int rc = rtProcInitExePath(g_szrtProcExePath, sizeof(g_szrtProcExePath) - 32);
166 if (RT_FAILURE(rc))
167 return rc;
168 }
169 else
170 {
171 size_t cch = strlen(pszProgramPath);
172 Assert(cch > 1);
173 AssertMsgReturn(cch < sizeof(g_szrtProcExePath) - 32, ("%zu\n", cch), VERR_BUFFER_OVERFLOW);
174 memcpy(g_szrtProcExePath, pszProgramPath, cch + 1);
175 }
176
177 /*
178 * Parse the name.
179 */
180 ssize_t offName;
181 g_cchrtProcExePath = RTPathParse(g_szrtProcExePath, &g_cchrtProcDir, &offName, NULL);
182 g_offrtProcName = offName;
183 return VINF_SUCCESS;
184}
185
186/**
187 * rtR3Init worker.
188 */
189static int rtR3InitBody(bool fInitSUPLib, const char *pszProgramPath)
190{
191 /*
192 * The Process ID.
193 */
194#ifdef _MSC_VER
195 g_ProcessSelf = _getpid(); /* crappy ansi compiler */
196#else
197 g_ProcessSelf = getpid();
198#endif
199
200#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
201# ifdef VBOX
202 /*
203 * This MUST be done as the very first thing, before any file is opened.
204 * The log is opened on demand, but the first log entries may be caused
205 * by rtThreadInit() below.
206 */
207 const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
208 if ( pszDisableHostCache != NULL
209 && *pszDisableHostCache
210 && strcmp(pszDisableHostCache, "0") != 0)
211 {
212 RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
213 RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
214 }
215# endif /* VBOX */
216#endif /* !IN_GUEST && !RT_NO_GIP */
217
218 /*
219 * Thread Thread database and adopt the caller thread as 'main'.
220 * This must be done before everything else or else we'll call into threading
221 * without having initialized TLS entries and suchlike.
222 */
223 int rc = rtThreadInit();
224 AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
225
226#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
227 if (fInitSUPLib)
228 {
229 /*
230 * Init GIP first.
231 * (The more time for updates before real use, the better.)
232 */
233 rc = SUPR3Init(NULL);
234 AssertMsgRCReturn(rc, ("Failed to initializeble the support library, rc=%Rrc!\n", rc), rc);
235 }
236#endif
237
238 /*
239 * The executable path, name and directory.
240 */
241 rc = rtR3InitProgramPath(pszProgramPath);
242 AssertLogRelMsgRCReturn(rc, ("Failed to get executable directory path, rc=%Rrc!\n", rc), rc);
243
244#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
245 /*
246 * The threading is initialized we can safely sleep a bit if GIP
247 * needs some time to update itself updating.
248 */
249 if (fInitSUPLib && g_pSUPGlobalInfoPage)
250 {
251 RTThreadSleep(20);
252 RTTimeNanoTS();
253 }
254#endif
255
256 /*
257 * Init the program start TSes.
258 * Do that here to be sure that the GIP time was properly updated the 1st time.
259 */
260 g_u64ProgramStartNanoTS = RTTimeNanoTS();
261 g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
262 g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
263
264 /*
265 * The remainder cannot easily be undone, so it has to go last.
266 */
267
268 /* Init C runtime locale. */
269 setlocale(LC_CTYPE, "");
270
271 /* Fork callbacks. */
272#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
273 rc = pthread_atfork(NULL, NULL, rtR3ForkChildCallback);
274 AssertMsg(rc == 0, ("%d\n", rc));
275#endif
276
277#if defined(DEBUG)
278 /*
279 * Enable alignment checks.
280 */
281 const char *pszAlignmentChecks = getenv("IPRT_ALIGNMENT_CHECKS");
282 g_fRTAlignmentChecks = pszAlignmentChecks != NULL
283 && pszAlignmentChecks[0] == '1'
284 && pszAlignmentChecks[1] == '\0';
285 if (g_fRTAlignmentChecks)
286 IPRT_ALIGNMENT_CHECKS_ENABLE();
287#endif
288
289 return VINF_SUCCESS;
290}
291
292
293/**
294 * Internal initialization worker.
295 *
296 * @returns IPRT status code.
297 * @param fInitSUPLib Whether to call SUPR3Init.
298 * @param pszProgramPath The program path, NULL if not specified.
299 */
300static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
301{
302 /* no entry log flow, because prefixes and thread may freak out. */
303
304 /*
305 * Do reference counting, only initialize the first time around.
306 *
307 * We are ASSUMING that nobody will be able to race RTR3Init calls when the
308 * first one, the real init, is running (second assertion).
309 */
310 int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
311 if (cUsers != 1)
312 {
313 AssertMsg(cUsers > 1, ("%d\n", cUsers));
314 Assert(!g_fInitializing);
315#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
316 if (fInitSUPLib)
317 SUPR3Init(NULL);
318#endif
319 if (!pszProgramPath)
320 return VINF_SUCCESS;
321 return rtR3InitProgramPath(pszProgramPath);
322 }
323 ASMAtomicWriteBool(&g_fInitializing, true);
324
325 /*
326 * Do the initialization.
327 */
328 int rc = rtR3InitBody(fInitSUPLib, pszProgramPath);
329 if (RT_FAILURE(rc))
330 {
331 /* failure */
332 ASMAtomicWriteBool(&g_fInitializing, false);
333 ASMAtomicDecS32(&g_cUsers);
334 return rc;
335 }
336
337 /* success */
338 LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
339 ASMAtomicWriteBool(&g_fInitializing, false);
340 return VINF_SUCCESS;
341}
342
343
344RTR3DECL(int) RTR3Init(void)
345{
346 return rtR3Init(false /* fInitSUPLib */, NULL);
347}
348
349
350RTR3DECL(int) RTR3InitEx(uint32_t iVersion, const char *pszProgramPath, bool fInitSUPLib)
351{
352 AssertReturn(iVersion == 0, VERR_NOT_SUPPORTED);
353 return rtR3Init(fInitSUPLib, pszProgramPath);
354}
355
356
357RTR3DECL(int) RTR3InitWithProgramPath(const char *pszProgramPath)
358{
359 return rtR3Init(false /* fInitSUPLib */, pszProgramPath);
360}
361
362
363RTR3DECL(int) RTR3InitAndSUPLib(void)
364{
365 return rtR3Init(true /* fInitSUPLib */, NULL /* pszProgramPath */);
366}
367
368
369RTR3DECL(int) RTR3InitAndSUPLibWithProgramPath(const char *pszProgramPath)
370{
371 return rtR3Init(true /* fInitSUPLib */, pszProgramPath);
372}
373
374
375#if 0 /** @todo implement RTR3Term. */
376RTR3DECL(void) RTR3Term(void)
377{
378}
379#endif
380
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