VirtualBox

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

Last change on this file since 11794 was 11794, checked in by vboxsync, 16 years ago

SUP: SUPInit(ppSession=NULL, cbReserved=0) -> SUPR3Init(ppSession)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/* $Id: init.cpp 11794 2008-08-29 09:13:37Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#define LOG_GROUP RTLOGGROUP_DEFAULT
37#ifdef RT_OS_WINDOWS
38# include <process.h>
39#else
40# include <unistd.h>
41#endif
42
43#include <iprt/runtime.h>
44#include <iprt/path.h>
45#include <iprt/assert.h>
46#include <iprt/log.h>
47#include <iprt/time.h>
48#include <iprt/err.h>
49#include <iprt/string.h>
50#include <iprt/param.h>
51#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
52# include <iprt/file.h>
53# include <VBox/sup.h>
54# include <stdlib.h>
55#endif
56#include "internal/path.h"
57#include "internal/process.h"
58#include "internal/thread.h"
59#include "internal/thread.h"
60#include "internal/time.h"
61
62
63/*******************************************************************************
64* Global Variables *
65*******************************************************************************/
66/** The number of calls to RTR3Init. */
67static int32_t volatile g_cUsers = 0;
68/** Whether we're currently initializing the IPRT. */
69static bool volatile g_fInitializing = false;
70
71/** Program path.
72 * The size is hardcoded, so we'll have to check for overflow when setting it
73 * since some hosts might support longer paths.
74 * @internal
75 */
76char g_szrtProgramPath[RTPATH_MAX];
77
78/**
79 * Program start nanosecond TS.
80 */
81uint64_t g_u64ProgramStartNanoTS;
82
83/**
84 * Program start microsecond TS.
85 */
86uint64_t g_u64ProgramStartMicroTS;
87
88/**
89 * Program start millisecond TS.
90 */
91uint64_t g_u64ProgramStartMilliTS;
92
93/**
94 * The process identifier of the running process.
95 */
96RTPROCESS g_ProcessSelf = NIL_RTPROCESS;
97
98/**
99 * The current process priority.
100 */
101RTPROCPRIORITY g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
102
103
104/**
105 * Initalizes the runtime library.
106 *
107 * @returns iprt status code.
108 *
109 * @param fInitSUPLib Set if SUPR3Init() shall be called during init (default).
110 * Clear if not to call it.
111 * @param cbReserve Ignored.
112 */
113RTR3DECL(int) RTR3Init(bool fInitSUPLib, size_t cbReserve)
114{
115 /* no entry log flow, because prefixes and thread may freak out. */
116
117 /*
118 * Do reference counting, only initialize the first time around.
119 *
120 * We are ASSUMING that nobody will be able to race RTR3Init calls when the
121 * first one, the real init, is running (second assertion).
122 */
123 int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
124 if (cUsers != 1)
125 {
126 AssertMsg(cUsers > 1, ("%d\n", cUsers));
127 Assert(!g_fInitializing);
128#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
129 if (fInitSUPLib)
130 SUPR3Init(NULL);
131#endif
132 }
133 ASMAtomicWriteBool(&g_fInitializing, true);
134
135#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
136# ifdef VBOX
137 /*
138 * This MUST be done as the very first thing, before any file is opened.
139 * The log is opened on demand, but the first log entries may be caused
140 * by rtThreadInit() below.
141 */
142 const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
143 if ( pszDisableHostCache != NULL
144 && strlen(pszDisableHostCache) > 0
145 && strcmp(pszDisableHostCache, "0") != 0)
146 {
147 RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
148 RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
149 }
150# endif /* VBOX */
151#endif /* !IN_GUEST && !RT_NO_GIP */
152
153 /*
154 * Thread Thread database and adopt the caller thread as 'main'.
155 * This must be done before everything else or else we'll call into threading
156 * without having initialized TLS entries and suchlike.
157 */
158 int rc = rtThreadInit();
159 if (RT_FAILURE(rc))
160 {
161 AssertMsgFailed(("Failed to get executable directory path, rc=%d!\n", rc));
162 ASMAtomicWriteBool(&g_fInitializing, false);
163 ASMAtomicDecS32(&g_cUsers);
164 return rc;
165 }
166
167#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
168 if (fInitSUPLib)
169 {
170 /*
171 * Init GIP first.
172 * (The more time for updates before real use, the better.)
173 */
174 SUPR3Init(NULL);
175 }
176#endif
177
178 /*
179 * Init the program start TSes.
180 */
181 g_u64ProgramStartNanoTS = RTTimeNanoTS();
182 g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
183 g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
184
185#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
186 /*
187 * The threading is initialized we can safely sleep a bit if GIP
188 * needs some time to update itself updating.
189 */
190 if (fInitSUPLib && g_pSUPGlobalInfoPage)
191 {
192 RTThreadSleep(20);
193 RTTimeNanoTS();
194 }
195#endif
196
197 /*
198 * Get the executable path.
199 *
200 * We're also checking the depth here since we'll be
201 * appending filenames to the executable path. Currently
202 * we assume 16 bytes are what we need.
203 */
204 char szPath[RTPATH_MAX - 16];
205 rc = RTPathProgram(szPath, sizeof(szPath));
206 if (RT_FAILURE(rc))
207 {
208 AssertMsgFailed(("Failed to get executable directory path, rc=%d!\n", rc));
209 ASMAtomicWriteBool(&g_fInitializing, false);
210 ASMAtomicDecS32(&g_cUsers);
211 return rc;
212 }
213
214 /*
215 * The Process ID.
216 */
217#ifdef _MSC_VER
218 g_ProcessSelf = _getpid(); /* crappy ansi compiler */
219#else
220 g_ProcessSelf = getpid();
221#endif
222
223 /*
224 * More stuff to come.
225 */
226
227 LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
228 ASMAtomicWriteBool(&g_fInitializing, false);
229 return VINF_SUCCESS;
230}
231
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