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