VirtualBox

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

Last change on this file since 23973 was 23919, checked in by vboxsync, 15 years ago

re-apply r53640, r53642

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.5 KB
Line 
1/* $Id: init.cpp 23919 2009-10-20 17:48:40Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3.
4 */
5
6/*
7 * Copyright (C) 2006-2009 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* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_DEFAULT
36#include <iprt/types.h> /* darwin: UINT32_C and others. */
37
38#ifdef RT_OS_WINDOWS
39# include <process.h>
40#else
41# include <unistd.h>
42# ifndef RT_OS_OS2
43# include <pthread.h>
44# include <signal.h>
45# include <errno.h>
46# define IPRT_USE_SIG_CHILD_DUMMY
47# endif
48#endif
49#ifdef RT_OS_OS2
50# include <InnoTekLIBC/fork.h>
51#endif
52#include <locale.h>
53
54#include <iprt/initterm.h>
55#include <iprt/asm.h>
56#include <iprt/assert.h>
57#include <iprt/err.h>
58#include <iprt/log.h>
59#include <iprt/path.h>
60#include <iprt/time.h>
61#include <iprt/string.h>
62#include <iprt/param.h>
63#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
64# include <iprt/file.h>
65# include <VBox/sup.h>
66#endif
67#include <stdlib.h>
68
69#include "internal/alignmentchecks.h"
70#include "internal/path.h"
71#include "internal/process.h"
72#include "internal/thread.h"
73#include "internal/thread.h"
74#include "internal/time.h"
75
76
77/*******************************************************************************
78* Global Variables *
79*******************************************************************************/
80/** The number of calls to RTR3Init. */
81static int32_t volatile g_cUsers = 0;
82/** Whether we're currently initializing the IPRT. */
83static bool volatile g_fInitializing = false;
84
85/** The process path.
86 * This is used by RTPathExecDir and RTProcGetExecutableName and set by rtProcInitName. */
87char g_szrtProcExePath[RTPATH_MAX];
88/** The length of g_szrtProcExePath. */
89size_t g_cchrtProcExePath;
90/** The length of directory path component of g_szrtProcExePath. */
91size_t g_cchrtProcDir;
92/** The offset of the process name into g_szrtProcExePath. */
93size_t g_offrtProcName;
94
95/**
96 * Program start nanosecond TS.
97 */
98uint64_t g_u64ProgramStartNanoTS;
99
100/**
101 * Program start microsecond TS.
102 */
103uint64_t g_u64ProgramStartMicroTS;
104
105/**
106 * Program start millisecond TS.
107 */
108uint64_t g_u64ProgramStartMilliTS;
109
110/**
111 * The process identifier of the running process.
112 */
113RTPROCESS g_ProcessSelf = NIL_RTPROCESS;
114
115/**
116 * The current process priority.
117 */
118RTPROCPRIORITY g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
119
120#ifdef IPRT_WITH_ALIGNMENT_CHECKS
121/**
122 * Whether alignment checks are enabled.
123 * This is set if the environment variable IPRT_ALIGNMENT_CHECKS is 1.
124 */
125RTDATADECL(bool) g_fRTAlignmentChecks = false;
126#endif
127
128
129/**
130 * atexit callback.
131 *
132 * This makes sure any loggers are flushed and will later also work the
133 * termination callback chain.
134 */
135static void rtR3ExitCallback(void)
136{
137 if (g_cUsers > 0)
138 {
139 PRTLOGGER pLogger = RTLogGetDefaultInstance();
140 if (pLogger)
141 RTLogFlush(pLogger);
142
143 pLogger = RTLogRelDefaultInstance();
144 if (pLogger)
145 RTLogFlush(pLogger);
146 }
147}
148
149
150#ifndef RT_OS_WINDOWS
151/**
152 * Fork callback, child context.
153 */
154static void rtR3ForkChildCallback(void)
155{
156 g_ProcessSelf = getpid();
157}
158#endif /* RT_OS_WINDOWS */
159
160#ifdef RT_OS_OS2
161/** Low-level fork callback for OS/2. */
162int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
163{
164 if (enmOperation == __LIBC_FORK_STAGE_COMPLETION_CHILD)
165 rtR3ForkChildCallback();
166 return 0;
167}
168
169_FORK_CHILD1(0, rtR3ForkOs2Child);
170#endif /* RT_OS_OS2 */
171
172
173
174/**
175 * Internal worker which initializes or re-initializes the
176 * program path, name and directory globals.
177 *
178 * @returns IPRT status code.
179 * @param pszProgramPath The program path, NULL if not specified.
180 */
181static int rtR3InitProgramPath(const char *pszProgramPath)
182{
183 /*
184 * We're reserving 32 bytes here for file names as what not.
185 */
186 if (!pszProgramPath)
187 {
188 int rc = rtProcInitExePath(g_szrtProcExePath, sizeof(g_szrtProcExePath) - 32);
189 if (RT_FAILURE(rc))
190 return rc;
191 }
192 else
193 {
194 size_t cch = strlen(pszProgramPath);
195 Assert(cch > 1);
196 AssertMsgReturn(cch < sizeof(g_szrtProcExePath) - 32, ("%zu\n", cch), VERR_BUFFER_OVERFLOW);
197 memcpy(g_szrtProcExePath, pszProgramPath, cch + 1);
198 }
199
200 /*
201 * Parse the name.
202 */
203 ssize_t offName;
204 g_cchrtProcExePath = RTPathParse(g_szrtProcExePath, &g_cchrtProcDir, &offName, NULL);
205 g_offrtProcName = offName;
206 return VINF_SUCCESS;
207}
208
209
210#ifdef IPRT_USE_SIG_CHILD_DUMMY
211/**
212 * Dummy SIGCHILD handler.
213 *
214 * Assigned on rtR3Init only when SIGCHILD handler is set SIGIGN or SIGDEF to
215 * ensure waitpid works properly for the terminated processes.
216 */
217static void rtR3SigChildHandler(int iSignal)
218{
219 NOREF(iSignal);
220}
221#endif /* IPRT_USE_SIG_CHILD_DUMMY */
222
223
224/**
225 * rtR3Init worker.
226 */
227static int rtR3InitBody(bool fInitSUPLib, const char *pszProgramPath)
228{
229 /*
230 * The Process ID.
231 */
232#ifdef _MSC_VER
233 g_ProcessSelf = _getpid(); /* crappy ansi compiler */
234#else
235 g_ProcessSelf = getpid();
236#endif
237
238#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
239# ifdef VBOX
240 /*
241 * This MUST be done as the very first thing, before any file is opened.
242 * The log is opened on demand, but the first log entries may be caused
243 * by rtThreadInit() below.
244 */
245 const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
246 if ( pszDisableHostCache != NULL
247 && *pszDisableHostCache
248 && strcmp(pszDisableHostCache, "0") != 0)
249 {
250 RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
251 RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
252 }
253# endif /* VBOX */
254#endif /* !IN_GUEST && !RT_NO_GIP */
255
256 /*
257 * Thread Thread database and adopt the caller thread as 'main'.
258 * This must be done before everything else or else we'll call into threading
259 * without having initialized TLS entries and suchlike.
260 */
261 int rc = rtThreadInit();
262 AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
263
264#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
265 if (fInitSUPLib)
266 {
267 /*
268 * Init GIP first.
269 * (The more time for updates before real use, the better.)
270 */
271 rc = SUPR3Init(NULL);
272 AssertMsgRCReturn(rc, ("Failed to initializeble the support library, rc=%Rrc!\n", rc), rc);
273 }
274#endif
275
276 /*
277 * The executable path, name and directory.
278 */
279 rc = rtR3InitProgramPath(pszProgramPath);
280 AssertLogRelMsgRCReturn(rc, ("Failed to get executable directory path, rc=%Rrc!\n", rc), rc);
281
282#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
283 /*
284 * The threading is initialized we can safely sleep a bit if GIP
285 * needs some time to update itself updating.
286 */
287 if (fInitSUPLib && g_pSUPGlobalInfoPage)
288 {
289 RTThreadSleep(20);
290 RTTimeNanoTS();
291 }
292#endif
293
294 /*
295 * Init the program start TSes.
296 * Do that here to be sure that the GIP time was properly updated the 1st time.
297 */
298 g_u64ProgramStartNanoTS = RTTimeNanoTS();
299 g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
300 g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
301
302 /*
303 * The remainder cannot easily be undone, so it has to go last.
304 */
305
306 /* Init C runtime locale. */
307 setlocale(LC_CTYPE, "");
308
309 /* Fork and exit callbacks. */
310#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
311 rc = pthread_atfork(NULL, NULL, rtR3ForkChildCallback);
312 AssertMsg(rc == 0, ("%d\n", rc));
313#endif
314 atexit(rtR3ExitCallback);
315
316#ifdef IPRT_USE_SIG_CHILD_DUMMY
317 /*
318 * SIGCHLD must not be ignored (that's default), otherwise posix compliant waitpid
319 * implementations won't work right.
320 */
321 for (;;)
322 {
323 struct sigaction saOld;
324 rc = sigaction(SIGCHLD, 0, &saOld); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
325 if ( rc != 0
326 || (saOld.sa_flags & SA_SIGINFO)
327 || ( saOld.sa_handler != SIG_IGN
328 && saOld.sa_handler != SIG_DFL)
329 )
330 break;
331
332 /* Try install dummy handler. */
333 struct sigaction saNew = saOld;
334 saNew.sa_flags = SA_NOCLDSTOP | SA_RESTART;
335 saNew.sa_handler = rtR3SigChildHandler;
336 rc = sigemptyset(&saNew.sa_mask); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
337 struct sigaction saOld2;
338 rc = sigaction(SIGCHLD, &saNew, &saOld2); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
339 if ( rc != 0
340 || ( saOld2.sa_handler == saOld.sa_handler
341 && !(saOld2.sa_flags & SA_SIGINFO))
342 )
343 break;
344
345 /* Race during dynamic load, restore and try again... */
346 sigaction(SIGCHLD, &saOld2, NULL);
347 RTThreadYield();
348 }
349#endif /* IPRT_USE_SIG_CHILD_DUMMY */
350
351#ifdef IPRT_WITH_ALIGNMENT_CHECKS
352 /*
353 * Enable alignment checks.
354 */
355 const char *pszAlignmentChecks = getenv("IPRT_ALIGNMENT_CHECKS");
356 g_fRTAlignmentChecks = pszAlignmentChecks != NULL
357 && pszAlignmentChecks[0] == '1'
358 && pszAlignmentChecks[1] == '\0';
359 if (g_fRTAlignmentChecks)
360 IPRT_ALIGNMENT_CHECKS_ENABLE();
361#endif
362
363 return VINF_SUCCESS;
364}
365
366
367/**
368 * Internal initialization worker.
369 *
370 * @returns IPRT status code.
371 * @param fInitSUPLib Whether to call SUPR3Init.
372 * @param pszProgramPath The program path, NULL if not specified.
373 */
374static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
375{
376 /* no entry log flow, because prefixes and thread may freak out. */
377
378 /*
379 * Do reference counting, only initialize the first time around.
380 *
381 * We are ASSUMING that nobody will be able to race RTR3Init calls when the
382 * first one, the real init, is running (second assertion).
383 */
384 int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
385 if (cUsers != 1)
386 {
387 AssertMsg(cUsers > 1, ("%d\n", cUsers));
388 Assert(!g_fInitializing);
389#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
390 if (fInitSUPLib)
391 SUPR3Init(NULL);
392#endif
393 if (!pszProgramPath)
394 return VINF_SUCCESS;
395 return rtR3InitProgramPath(pszProgramPath);
396 }
397 ASMAtomicWriteBool(&g_fInitializing, true);
398
399 /*
400 * Do the initialization.
401 */
402 int rc = rtR3InitBody(fInitSUPLib, pszProgramPath);
403 if (RT_FAILURE(rc))
404 {
405 /* failure */
406 ASMAtomicWriteBool(&g_fInitializing, false);
407 ASMAtomicDecS32(&g_cUsers);
408 return rc;
409 }
410
411 /* success */
412 LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
413 ASMAtomicWriteBool(&g_fInitializing, false);
414 return VINF_SUCCESS;
415}
416
417
418RTR3DECL(int) RTR3Init(void)
419{
420 return rtR3Init(false /* fInitSUPLib */, NULL);
421}
422
423
424RTR3DECL(int) RTR3InitEx(uint32_t iVersion, const char *pszProgramPath, bool fInitSUPLib)
425{
426 AssertReturn(iVersion == 0, VERR_NOT_SUPPORTED);
427 return rtR3Init(fInitSUPLib, pszProgramPath);
428}
429
430
431RTR3DECL(int) RTR3InitWithProgramPath(const char *pszProgramPath)
432{
433 return rtR3Init(false /* fInitSUPLib */, pszProgramPath);
434}
435
436
437RTR3DECL(int) RTR3InitAndSUPLib(void)
438{
439 return rtR3Init(true /* fInitSUPLib */, NULL /* pszProgramPath */);
440}
441
442
443RTR3DECL(int) RTR3InitAndSUPLibWithProgramPath(const char *pszProgramPath)
444{
445 return rtR3Init(true /* fInitSUPLib */, pszProgramPath);
446}
447
448
449#if 0 /** @todo implement RTR3Term. */
450RTR3DECL(void) RTR3Term(void)
451{
452}
453#endif
454
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