VirtualBox

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

Last change on this file since 39751 was 39751, checked in by vboxsync, 13 years ago

IPRT: disable critical error message boxes on init.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.2 KB
Line 
1/* $Id: init.cpp 39751 2012-01-11 16:07:37Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Oracle Corporation
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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DEFAULT
32#include <iprt/types.h> /* darwin: UINT32_C and others. */
33
34#ifdef RT_OS_WINDOWS
35# include <process.h>
36# include <Windows.h>
37#else
38# include <unistd.h>
39# ifndef RT_OS_OS2
40# include <pthread.h>
41# include <signal.h>
42# include <errno.h>
43# define IPRT_USE_SIG_CHILD_DUMMY
44# endif
45#endif
46#ifdef RT_OS_OS2
47# include <InnoTekLIBC/fork.h>
48#endif
49#include <locale.h>
50
51#include <iprt/initterm.h>
52#include <iprt/asm.h>
53#include <iprt/assert.h>
54#include <iprt/err.h>
55#include <iprt/log.h>
56#include <iprt/mem.h>
57#include <iprt/path.h>
58#include <iprt/time.h>
59#include <iprt/string.h>
60#include <iprt/param.h>
61#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
62# include <iprt/file.h>
63# include <VBox/sup.h>
64#endif
65#include <stdlib.h>
66
67#include "internal/alignmentchecks.h"
68#include "internal/path.h"
69#include "internal/process.h"
70#include "internal/thread.h"
71#include "internal/thread.h"
72#include "internal/time.h"
73
74
75/*******************************************************************************
76* Global Variables *
77*******************************************************************************/
78/** The number of calls to RTR3Init*. */
79static int32_t volatile g_cUsers = 0;
80/** Whether we're currently initializing the IPRT. */
81static bool volatile g_fInitializing = false;
82
83/** The process path.
84 * This is used by RTPathExecDir and RTProcGetExecutablePath and set by rtProcInitName. */
85DECLHIDDEN(char) g_szrtProcExePath[RTPATH_MAX];
86/** The length of g_szrtProcExePath. */
87DECLHIDDEN(size_t) g_cchrtProcExePath;
88/** The length of directory path component of g_szrtProcExePath. */
89DECLHIDDEN(size_t) g_cchrtProcDir;
90/** The offset of the process name into g_szrtProcExePath. */
91DECLHIDDEN(size_t) g_offrtProcName;
92
93/** The argument count of the program. */
94static int g_crtArgs = -1;
95/** The arguments of the program (UTF-8). This is "leaked". */
96static char ** g_papszrtArgs;
97/** The original argument vector of the program. */
98static char ** g_papszrtOrgArgs;
99
100/**
101 * Program start nanosecond TS.
102 */
103DECLHIDDEN(uint64_t) g_u64ProgramStartNanoTS;
104
105/**
106 * Program start microsecond TS.
107 */
108DECLHIDDEN(uint64_t) g_u64ProgramStartMicroTS;
109
110/**
111 * Program start millisecond TS.
112 */
113DECLHIDDEN(uint64_t) g_u64ProgramStartMilliTS;
114
115/**
116 * The process identifier of the running process.
117 */
118DECLHIDDEN(RTPROCESS) g_ProcessSelf = NIL_RTPROCESS;
119
120/**
121 * The current process priority.
122 */
123DECLHIDDEN(RTPROCPRIORITY) g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
124
125/**
126 * Set if the atexit callback has been called, i.e. indicating
127 * that the process is terminating.
128 */
129DECLHIDDEN(bool volatile) g_frtAtExitCalled = false;
130
131#ifdef IPRT_WITH_ALIGNMENT_CHECKS
132/**
133 * Whether alignment checks are enabled.
134 * This is set if the environment variable IPRT_ALIGNMENT_CHECKS is 1.
135 */
136RTDATADECL(bool) g_fRTAlignmentChecks = false;
137#endif
138
139
140/**
141 * atexit callback.
142 *
143 * This makes sure any loggers are flushed and will later also work the
144 * termination callback chain.
145 */
146static void rtR3ExitCallback(void)
147{
148 ASMAtomicWriteBool(&g_frtAtExitCalled, true);
149
150 if (g_cUsers > 0)
151 {
152 PRTLOGGER pLogger = RTLogGetDefaultInstance();
153 if (pLogger)
154 RTLogFlush(pLogger);
155
156 pLogger = RTLogRelDefaultInstance();
157 if (pLogger)
158 RTLogFlush(pLogger);
159 }
160}
161
162
163#ifndef RT_OS_WINDOWS
164/**
165 * Fork callback, child context.
166 */
167static void rtR3ForkChildCallback(void)
168{
169 g_ProcessSelf = getpid();
170}
171#endif /* RT_OS_WINDOWS */
172
173#ifdef RT_OS_OS2
174/** Fork completion callback for OS/2. Only called in the child. */
175static void rtR3ForkOs2ChildCompletionCallback(void *pvArg, int rc, __LIBC_FORKCTX enmCtx)
176{
177 Assert(enmCtx == __LIBC_FORK_CTX_CHILD); NOREF(enmCtx);
178 NOREF(pvArg);
179
180 if (!rc)
181 rtR3ForkChildCallback();
182}
183
184/** Low-level fork callback for OS/2. */
185int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
186{
187 if (enmOperation == __LIBC_FORK_OP_EXEC_CHILD)
188 return pForkHandle->pfnCompletionCallback(pForkHandle, rtR3ForkOs2ChildCompletionCallback, NULL, __LIBC_FORK_CTX_CHILD);
189 return 0;
190}
191
192_FORK_CHILD1(0, rtR3ForkOs2Child);
193#endif /* RT_OS_OS2 */
194
195
196
197/**
198 * Internal worker which initializes or re-initializes the
199 * program path, name and directory globals.
200 *
201 * @returns IPRT status code.
202 * @param pszProgramPath The program path, NULL if not specified.
203 */
204static int rtR3InitProgramPath(const char *pszProgramPath)
205{
206 /*
207 * We're reserving 32 bytes here for file names as what not.
208 */
209 if (!pszProgramPath)
210 {
211 int rc = rtProcInitExePath(g_szrtProcExePath, sizeof(g_szrtProcExePath) - 32);
212 if (RT_FAILURE(rc))
213 return rc;
214 }
215 else
216 {
217 size_t cch = strlen(pszProgramPath);
218 Assert(cch > 1);
219 AssertMsgReturn(cch < sizeof(g_szrtProcExePath) - 32, ("%zu\n", cch), VERR_BUFFER_OVERFLOW);
220 memcpy(g_szrtProcExePath, pszProgramPath, cch + 1);
221 }
222
223 /*
224 * Parse the name.
225 */
226 ssize_t offName;
227 g_cchrtProcExePath = RTPathParse(g_szrtProcExePath, &g_cchrtProcDir, &offName, NULL);
228 g_offrtProcName = offName;
229 return VINF_SUCCESS;
230}
231
232
233/**
234 * Internal worker which initializes or re-initializes the
235 * program path, name and directory globals.
236 *
237 * @returns IPRT status code.
238 * @param fFlags Flags, see RTR3INIT_XXX.
239 * @param cArgs Pointer to the argument count.
240 * @param ppapszArgs Pointer to the argument vector pointer. NULL
241 * allowed if @a cArgs is 0.
242 */
243static int rtR3InitArgv(uint32_t fFlags, int cArgs, char ***ppapszArgs)
244{
245 NOREF(fFlags);
246 if (cArgs)
247 {
248 AssertPtr(ppapszArgs);
249 AssertPtr(*ppapszArgs);
250 char **papszOrgArgs = *ppapszArgs;
251
252 /*
253 * Normally we should only be asked to convert arguments once. If we
254 * are though, it should be the already convered arguments.
255 */
256 if (g_crtArgs != -1)
257 {
258 AssertReturn( g_crtArgs == cArgs
259 && g_papszrtArgs == papszOrgArgs,
260 VERR_WRONG_ORDER); /* only init once! */
261 return VINF_SUCCESS;
262 }
263
264 /*
265 * Convert the arguments.
266 */
267 char **papszArgs = (char **)RTMemAllocZ((cArgs + 1) * sizeof(char *));
268 if (!papszArgs)
269 return VERR_NO_MEMORY;
270
271 for (int i = 0; i < cArgs; i++)
272 {
273 int rc = RTStrCurrentCPToUtf8(&papszArgs[i], papszOrgArgs[i]);
274 if (RT_FAILURE(rc))
275 {
276 while (i--)
277 RTStrFree(papszArgs[i]);
278 RTMemFree(papszArgs);
279 return rc;
280 }
281 }
282 papszArgs[cArgs] = NULL;
283
284 g_papszrtOrgArgs = papszOrgArgs;
285 g_papszrtArgs = papszArgs;
286 g_crtArgs = cArgs;
287
288 *ppapszArgs = papszArgs;
289 }
290
291 return VINF_SUCCESS;
292}
293
294
295#ifdef IPRT_USE_SIG_CHILD_DUMMY
296/**
297 * Dummy SIGCHILD handler.
298 *
299 * Assigned on rtR3Init only when SIGCHILD handler is set SIGIGN or SIGDEF to
300 * ensure waitpid works properly for the terminated processes.
301 */
302static void rtR3SigChildHandler(int iSignal)
303{
304 NOREF(iSignal);
305}
306#endif /* IPRT_USE_SIG_CHILD_DUMMY */
307
308
309/**
310 * rtR3Init worker.
311 */
312static int rtR3InitBody(uint32_t fFlags, int cArgs, char ***papszArgs, const char *pszProgramPath)
313{
314 /*
315 * Init C runtime locale before we do anything that may end up converting
316 * paths or we'll end up using the "C" locale for path conversion.
317 */
318 setlocale(LC_CTYPE, "");
319
320 /*
321 * The Process ID.
322 */
323#ifdef _MSC_VER
324 g_ProcessSelf = _getpid(); /* crappy ansi compiler */
325#else
326 g_ProcessSelf = getpid();
327#endif
328
329 /*
330 * Disable error popups.
331 */
332#ifdef RT_OS_WINDOWS
333 SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS);
334#elif defined(RT_OS_OS2)
335# error "FIXME"
336#endif
337
338#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
339# ifdef VBOX
340 /*
341 * This MUST be done as the very first thing, before any file is opened.
342 * The log is opened on demand, but the first log entries may be caused
343 * by rtThreadInit() below.
344 */
345 const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
346 if ( pszDisableHostCache != NULL
347 && *pszDisableHostCache
348 && strcmp(pszDisableHostCache, "0") != 0)
349 {
350 RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
351 RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
352 }
353# endif /* VBOX */
354#endif /* !IN_GUEST && !RT_NO_GIP */
355
356 /*
357 * Thread Thread database and adopt the caller thread as 'main'.
358 * This must be done before everything else or else we'll call into threading
359 * without having initialized TLS entries and suchlike.
360 */
361 int rc = rtThreadInit();
362 AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
363
364#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
365 if (fFlags & RTR3INIT_FLAGS_SUPLIB)
366 {
367 /*
368 * Init GIP first.
369 * (The more time for updates before real use, the better.)
370 */
371 rc = SUPR3Init(NULL);
372 AssertMsgRCReturn(rc, ("Failed to initializable the support library, rc=%Rrc!\n", rc), rc);
373 }
374#endif
375
376 /*
377 * The executable path, name and directory. Convert arguments.
378 */
379 rc = rtR3InitProgramPath(pszProgramPath);
380 AssertLogRelMsgRCReturn(rc, ("Failed to get executable directory path, rc=%Rrc!\n", rc), rc);
381
382 rc = rtR3InitArgv(fFlags, cArgs, papszArgs);
383 AssertLogRelMsgRCReturn(rc, ("Failed to convert the arguments, rc=%Rrc!\n", rc), rc);
384
385#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
386 /*
387 * The threading is initialized we can safely sleep a bit if GIP
388 * needs some time to update itself updating.
389 */
390 if ((fFlags & RTR3INIT_FLAGS_SUPLIB) && g_pSUPGlobalInfoPage)
391 {
392 RTThreadSleep(20);
393 RTTimeNanoTS();
394 }
395#endif
396
397 /*
398 * Init the program start TSes.
399 * Do that here to be sure that the GIP time was properly updated the 1st time.
400 */
401 g_u64ProgramStartNanoTS = RTTimeNanoTS();
402 g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
403 g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
404
405 /*
406 * The remainder cannot easily be undone, so it has to go last.
407 */
408
409 /* Fork and exit callbacks. */
410#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
411 rc = pthread_atfork(NULL, NULL, rtR3ForkChildCallback);
412 AssertMsg(rc == 0, ("%d\n", rc));
413#endif
414 atexit(rtR3ExitCallback);
415
416#ifdef IPRT_USE_SIG_CHILD_DUMMY
417 /*
418 * SIGCHLD must not be ignored (that's default), otherwise posix compliant waitpid
419 * implementations won't work right.
420 */
421 for (;;)
422 {
423 struct sigaction saOld;
424 rc = sigaction(SIGCHLD, 0, &saOld); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
425 if ( rc != 0
426 || (saOld.sa_flags & SA_SIGINFO)
427 || ( saOld.sa_handler != SIG_IGN
428 && saOld.sa_handler != SIG_DFL)
429 )
430 break;
431
432 /* Try install dummy handler. */
433 struct sigaction saNew = saOld;
434 saNew.sa_flags = SA_NOCLDSTOP | SA_RESTART;
435 saNew.sa_handler = rtR3SigChildHandler;
436 rc = sigemptyset(&saNew.sa_mask); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
437 struct sigaction saOld2;
438 rc = sigaction(SIGCHLD, &saNew, &saOld2); AssertMsg(rc == 0, ("%d/%d\n", rc, errno));
439 if ( rc != 0
440 || ( saOld2.sa_handler == saOld.sa_handler
441 && !(saOld2.sa_flags & SA_SIGINFO))
442 )
443 break;
444
445 /* Race during dynamic load, restore and try again... */
446 sigaction(SIGCHLD, &saOld2, NULL);
447 RTThreadYield();
448 }
449#endif /* IPRT_USE_SIG_CHILD_DUMMY */
450
451#ifdef IPRT_WITH_ALIGNMENT_CHECKS
452 /*
453 * Enable alignment checks.
454 */
455 const char *pszAlignmentChecks = getenv("IPRT_ALIGNMENT_CHECKS");
456 g_fRTAlignmentChecks = pszAlignmentChecks != NULL
457 && pszAlignmentChecks[0] == '1'
458 && pszAlignmentChecks[1] == '\0';
459 if (g_fRTAlignmentChecks)
460 IPRT_ALIGNMENT_CHECKS_ENABLE();
461#endif
462
463 return VINF_SUCCESS;
464}
465
466
467/**
468 * Internal initialization worker.
469 *
470 * @returns IPRT status code.
471 * @param fFlags Flags, see RTR3INIT_XXX.
472 * @param cArgs Pointer to the argument count.
473 * @param ppapszArgs Pointer to the argument vector pointer. NULL
474 * allowed if @a cArgs is 0.
475 * @param pszProgramPath The program path. Pass NULL if we're to figure it
476 * out ourselves.
477 */
478static int rtR3Init(uint32_t fFlags, int cArgs, char ***papszArgs, const char *pszProgramPath)
479{
480 /* no entry log flow, because prefixes and thread may freak out. */
481 Assert(!(fFlags & ~(RTR3INIT_FLAGS_DLL | RTR3INIT_FLAGS_SUPLIB)));
482 Assert(!(fFlags & RTR3INIT_FLAGS_DLL) || cArgs == 0);
483
484 /*
485 * Do reference counting, only initialize the first time around.
486 *
487 * We are ASSUMING that nobody will be able to race RTR3Init* calls when the
488 * first one, the real init, is running (second assertion).
489 */
490 int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
491 if (cUsers != 1)
492 {
493 AssertMsg(cUsers > 1, ("%d\n", cUsers));
494 Assert(!g_fInitializing);
495#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
496 if (fFlags & RTR3INIT_FLAGS_SUPLIB)
497 SUPR3Init(NULL);
498#endif
499 if (!pszProgramPath)
500 return VINF_SUCCESS;
501
502 int rc = rtR3InitProgramPath(pszProgramPath);
503 if (RT_SUCCESS(rc))
504 rc = rtR3InitArgv(fFlags, cArgs, papszArgs);
505 return rc;
506 }
507 ASMAtomicWriteBool(&g_fInitializing, true);
508
509 /*
510 * Do the initialization.
511 */
512 int rc = rtR3InitBody(fFlags, cArgs, papszArgs, pszProgramPath);
513 if (RT_FAILURE(rc))
514 {
515 /* failure */
516 ASMAtomicWriteBool(&g_fInitializing, false);
517 ASMAtomicDecS32(&g_cUsers);
518 return rc;
519 }
520
521 /* success */
522 LogFlow(("rtR3Init: returns VINF_SUCCESS\n"));
523 ASMAtomicWriteBool(&g_fInitializing, false);
524 return VINF_SUCCESS;
525}
526
527
528RTR3DECL(int) RTR3InitExe(int cArgs, char ***papszArgs, uint32_t fFlags)
529{
530 Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
531 return rtR3Init(fFlags, cArgs, papszArgs, NULL);
532}
533
534
535RTR3DECL(int) RTR3InitExeNoArguments(uint32_t fFlags)
536{
537 Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
538 return rtR3Init(fFlags, 0, NULL, NULL);
539}
540
541
542RTR3DECL(int) RTR3InitDll(uint32_t fFlags)
543{
544 Assert(!(fFlags & RTR3INIT_FLAGS_DLL));
545 return rtR3Init(fFlags | RTR3INIT_FLAGS_DLL, 0, NULL, NULL);
546}
547
548
549RTR3DECL(int) RTR3InitEx(uint32_t iVersion, uint32_t fFlags, int cArgs, char ***papszArgs, const char *pszProgramPath)
550{
551 AssertReturn(iVersion == RTR3INIT_VER_CUR, VERR_NOT_SUPPORTED);
552 return rtR3Init(fFlags, cArgs, papszArgs, pszProgramPath);
553}
554
555
556#if 0 /** @todo implement RTR3Term. */
557RTR3DECL(void) RTR3Term(void)
558{
559}
560#endif
561
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