VirtualBox

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

最後變更 在這個檔案從40304是 40304,由 vboxsync 提交於 13 年 前

IPRT: fixed OS/2 'bitrot'.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette