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