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