VirtualBox

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

最後變更 在這個檔案從18055是 18055,由 vboxsync 提交於 16 年 前

Runtime: call setlocale from RTR3Init, not as a global initialiser

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 8.7 KB
 
1/* $Id: init.cpp 18055 2009-03-18 12:01:51Z 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.alldomusa.eu.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/initterm.h>
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/err.h>
47#include <iprt/log.h>
48#include <iprt/path.h>
49#include <iprt/time.h>
50#include <iprt/string.h>
51#include <iprt/param.h>
52#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
53# include <iprt/file.h>
54# include <VBox/sup.h>
55# include <stdlib.h>
56#endif
57
58#include <locale.h>
59
60#include "internal/path.h"
61#include "internal/process.h"
62#include "internal/thread.h"
63#include "internal/thread.h"
64#include "internal/time.h"
65
66
67/*******************************************************************************
68* Global Variables *
69*******************************************************************************/
70/** The number of calls to RTR3Init. */
71static int32_t volatile g_cUsers = 0;
72/** Whether we're currently initializing the IPRT. */
73static bool volatile g_fInitializing = false;
74
75/** The process path.
76 * This is used by RTPathProgram and RTProcGetExecutableName and set by rtProcInitName. */
77char g_szrtProcExePath[RTPATH_MAX];
78/** The length of g_szrtProcExePath. */
79size_t g_cchrtProcExePath;
80/** The length of directory path component of g_szrtProcExePath. */
81size_t g_cchrtProcDir;
82/** The offset of the process name into g_szrtProcExePath. */
83size_t g_offrtProcName;
84
85/**
86 * Program start nanosecond TS.
87 */
88uint64_t g_u64ProgramStartNanoTS;
89
90/**
91 * Program start microsecond TS.
92 */
93uint64_t g_u64ProgramStartMicroTS;
94
95/**
96 * Program start millisecond TS.
97 */
98uint64_t g_u64ProgramStartMilliTS;
99
100/**
101 * The process identifier of the running process.
102 */
103RTPROCESS g_ProcessSelf = NIL_RTPROCESS;
104
105/**
106 * The current process priority.
107 */
108RTPROCPRIORITY g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
109
110
111
112/**
113 * Internal worker which initializes or re-initializes the
114 * program path, name and directory globals.
115 *
116 * @returns IPRT status code.
117 * @param pszProgramPath The program path, NULL if not specified.
118 */
119static int rtR3InitProgramPath(const char *pszProgramPath)
120{
121 /*
122 * We're reserving 32 bytes here for file names as what not.
123 */
124 if (!pszProgramPath)
125 {
126 int rc = rtProcInitExePath(g_szrtProcExePath, sizeof(g_szrtProcExePath) - 32);
127 if (RT_FAILURE(rc))
128 return rc;
129 }
130 else
131 {
132 size_t cch = strlen(pszProgramPath);
133 Assert(cch > 1);
134 AssertMsgReturn(cch < sizeof(g_szrtProcExePath) - 32, ("%zu\n", cch), VERR_BUFFER_OVERFLOW);
135 memcpy(g_szrtProcExePath, pszProgramPath, cch + 1);
136 }
137
138 /*
139 * Parse the name.
140 */
141 ssize_t offName;
142 g_cchrtProcExePath = RTPathParse(g_szrtProcExePath, &g_cchrtProcDir, &offName, NULL);
143 g_offrtProcName = offName;
144 return VINF_SUCCESS;
145}
146
147
148/**
149 * Internal initialization worker.
150 *
151 * @returns IPRT status code.
152 * @param fInitSUPLib Whether to call SUPR3Init.
153 * @param pszProgramPath The program path, NULL if not specified.
154 */
155static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
156{
157 int rc = VINF_SUCCESS;
158 /* no entry log flow, because prefixes and thread may freak out. */
159
160 /*
161 * Do reference counting, only initialize the first time around.
162 *
163 * We are ASSUMING that nobody will be able to race RTR3Init calls when the
164 * first one, the real init, is running (second assertion).
165 */
166 int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
167 if (cUsers != 1)
168 {
169 AssertMsg(cUsers > 1, ("%d\n", cUsers));
170 Assert(!g_fInitializing);
171#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
172 if (fInitSUPLib)
173 SUPR3Init(NULL);
174#endif
175 if (pszProgramPath)
176 rc = rtR3InitProgramPath(pszProgramPath);
177 return rc;
178 }
179 ASMAtomicWriteBool(&g_fInitializing, true);
180
181#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
182# ifdef VBOX
183 /*
184 * This MUST be done as the very first thing, before any file is opened.
185 * The log is opened on demand, but the first log entries may be caused
186 * by rtThreadInit() below.
187 */
188 const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
189 if ( pszDisableHostCache != NULL
190 && strlen(pszDisableHostCache) > 0
191 && strcmp(pszDisableHostCache, "0") != 0)
192 {
193 RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
194 RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
195 }
196# endif /* VBOX */
197#endif /* !IN_GUEST && !RT_NO_GIP */
198
199 /*
200 * Thread Thread database and adopt the caller thread as 'main'.
201 * This must be done before everything else or else we'll call into threading
202 * without having initialized TLS entries and suchlike.
203 */
204 rc = rtThreadInit();
205 if (RT_FAILURE(rc))
206 {
207 AssertMsgFailed(("Failed to initialize threads, rc=%Rrc!\n", rc));
208 ASMAtomicWriteBool(&g_fInitializing, false);
209 ASMAtomicDecS32(&g_cUsers);
210 return rc;
211 }
212
213#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
214 if (fInitSUPLib)
215 {
216 /*
217 * Init GIP first.
218 * (The more time for updates before real use, the better.)
219 */
220 SUPR3Init(NULL);
221 }
222#endif
223
224 /*
225 * The Process ID.
226 */
227#ifdef _MSC_VER
228 g_ProcessSelf = _getpid(); /* crappy ansi compiler */
229#else
230 g_ProcessSelf = getpid();
231#endif
232
233 /*
234 * The executable path, name and directory.
235 */
236 rc = rtR3InitProgramPath(pszProgramPath);
237 if (RT_FAILURE(rc))
238 {
239 AssertLogRelMsgFailed(("Failed to get executable directory path, rc=%Rrc!\n", rc));
240 ASMAtomicWriteBool(&g_fInitializing, false);
241 ASMAtomicDecS32(&g_cUsers);
242 return rc;
243 }
244
245#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
246 /*
247 * The threading is initialized we can safely sleep a bit if GIP
248 * needs some time to update itself updating.
249 */
250 if (fInitSUPLib && g_pSUPGlobalInfoPage)
251 {
252 RTThreadSleep(20);
253 RTTimeNanoTS();
254 }
255#endif
256
257 /*
258 * Init the program start TSes.
259 * Do that here to be sure that the GIP time was properly updated the 1st time.
260 */
261 g_u64ProgramStartNanoTS = RTTimeNanoTS();
262 g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
263 g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
264
265 /**
266 * Init C runtime locale
267 */
268 char *pcDummy = setlocale(LC_CTYPE, "");
269
270 /*
271 * More stuff to come?
272 */
273
274 LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
275 ASMAtomicWriteBool(&g_fInitializing, false);
276 return VINF_SUCCESS;
277}
278
279
280RTR3DECL(int) RTR3Init(void)
281{
282 return rtR3Init(false /* fInitSUPLib */, NULL);
283}
284
285
286RTR3DECL(int) RTR3InitEx(uint32_t iVersion, const char *pszProgramPath, bool fInitSUPLib)
287{
288 AssertReturn(iVersion == 0, VERR_NOT_SUPPORTED);
289 return rtR3Init(fInitSUPLib, pszProgramPath);
290}
291
292
293RTR3DECL(int) RTR3InitWithProgramPath(const char *pszProgramPath)
294{
295 return rtR3Init(false /* fInitSUPLib */, pszProgramPath);
296}
297
298
299RTR3DECL(int) RTR3InitAndSUPLib(void)
300{
301 return rtR3Init(true /* fInitSUPLib */, NULL /* pszProgramPath */);
302}
303
304
305RTR3DECL(int) RTR3InitAndSUPLibWithProgramPath(const char *pszProgramPath)
306{
307 return rtR3Init(true /* fInitSUPLib */, pszProgramPath);
308}
309
310
311#if 0 /** @todo implement RTR3Term. */
312RTR3DECL(void) RTR3Term(void)
313{
314}
315#endif
316
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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