[1] | 1 | /** @file
|
---|
[8245] | 2 | * IPRT - Runtime Init/Term.
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[76553] | 6 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
[1] | 7 | *
|
---|
| 8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
[5999] | 11 | * General Public License (GPL) as published by the Free Software
|
---|
| 12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 15 | *
|
---|
| 16 | * The contents of this file may alternatively be used under the terms
|
---|
| 17 | * of the Common Development and Distribution License Version 1.0
|
---|
| 18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
| 19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
| 20 | * CDDL are applicable instead of those of the GPL.
|
---|
| 21 | *
|
---|
| 22 | * You may elect to license modified versions of this file under the
|
---|
| 23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[1] | 24 | */
|
---|
| 25 |
|
---|
[76557] | 26 | #ifndef IPRT_INCLUDED_initterm_h
|
---|
| 27 | #define IPRT_INCLUDED_initterm_h
|
---|
[76507] | 28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 29 | # pragma once
|
---|
| 30 | #endif
|
---|
[1] | 31 |
|
---|
| 32 | #include <iprt/cdefs.h>
|
---|
| 33 | #include <iprt/types.h>
|
---|
| 34 |
|
---|
[20374] | 35 | RT_C_DECLS_BEGIN
|
---|
[1] | 36 |
|
---|
[36508] | 37 | /** @defgroup grp_rt IPRT C/C++ APIs
|
---|
[1] | 38 | * @{
|
---|
| 39 | */
|
---|
| 40 |
|
---|
[57942] | 41 | /** @defgroup grp_rt_initterm RTInit/RTTerm - Initialization and Termination
|
---|
| 42 | *
|
---|
| 43 | * APIs for initializing and terminating the IPRT, optionally it can also
|
---|
| 44 | * convert input arguments to UTF-8 (in ring-3).
|
---|
| 45 | *
|
---|
| 46 | * @sa RTOnce, RTOnceEx.
|
---|
| 47 | *
|
---|
[1] | 48 | * @{
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | #ifdef IN_RING3
|
---|
[38636] | 52 | /** @name RTR3Init flags (RTR3INIT_XXX).
|
---|
| 53 | * @{ */
|
---|
| 54 | /** Try initialize SUPLib. */
|
---|
| 55 | #define RTR3INIT_FLAGS_SUPLIB RT_BIT(0)
|
---|
| 56 | /** Initializing IPRT from a DLL. */
|
---|
| 57 | #define RTR3INIT_FLAGS_DLL RT_BIT(1)
|
---|
[44456] | 58 | /** We are sharing a process space, so we need to behave. */
|
---|
| 59 | #define RTR3INIT_FLAGS_UNOBTRUSIVE RT_BIT(2)
|
---|
[48681] | 60 | /** The caller ensures that the argument bector is UTF-8. */
|
---|
| 61 | #define RTR3INIT_FLAGS_UTF8_ARGV RT_BIT(3)
|
---|
[59404] | 62 | /** Indicates that this is a standalone application without any additional
|
---|
| 63 | * shared libraries in the application directory. Mainly windows loader mess. */
|
---|
| 64 | #define RTR3INIT_FLAGS_STANDALONE_APP RT_BIT(4)
|
---|
[38636] | 65 | /** @} */
|
---|
| 66 |
|
---|
| 67 | /** @name RTR3InitEx version
|
---|
| 68 | * @{ */
|
---|
| 69 | /** Version 1. */
|
---|
| 70 | #define RTR3INIT_VER_1 UINT32_C(1)
|
---|
| 71 | /** The current version. */
|
---|
| 72 | #define RTR3INIT_VER_CUR RTR3INIT_VER_1
|
---|
| 73 | /** @} */
|
---|
| 74 |
|
---|
[1] | 75 | /**
|
---|
[11822] | 76 | * Initializes the runtime library.
|
---|
[1] | 77 | *
|
---|
| 78 | * @returns iprt status code.
|
---|
[38636] | 79 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
[11822] | 80 | */
|
---|
[38636] | 81 | RTR3DECL(int) RTR3InitExeNoArguments(uint32_t fFlags);
|
---|
[11822] | 82 |
|
---|
| 83 | /**
|
---|
[38636] | 84 | * Initializes the runtime library.
|
---|
[1] | 85 | *
|
---|
[38636] | 86 | * @returns iprt status code.
|
---|
| 87 | * @param cArgs Pointer to the argument count.
|
---|
| 88 | * @param ppapszArgs Pointer to the argument vector pointer.
|
---|
| 89 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
[1] | 90 | */
|
---|
[57926] | 91 | RTR3DECL(int) RTR3InitExe(int cArgs, char ***ppapszArgs, uint32_t fFlags);
|
---|
[1] | 92 |
|
---|
| 93 | /**
|
---|
[38636] | 94 | * Initializes the runtime library.
|
---|
[11822] | 95 | *
|
---|
[38636] | 96 | * @returns iprt status code.
|
---|
| 97 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
[11822] | 98 | */
|
---|
[38636] | 99 | RTR3DECL(int) RTR3InitDll(uint32_t fFlags);
|
---|
[11822] | 100 |
|
---|
| 101 | /**
|
---|
[11841] | 102 | * Initializes the runtime library and possibly also SUPLib too.
|
---|
[13813] | 103 | *
|
---|
[11841] | 104 | * Avoid this interface, it's not considered stable.
|
---|
[13813] | 105 | *
|
---|
[11841] | 106 | * @returns IPRT status code.
|
---|
| 107 | * @param iVersion The interface version. Must be 0 atm.
|
---|
[38636] | 108 | * @param fFlags Flags, see RTR3INIT_XXX.
|
---|
| 109 | * @param cArgs Pointer to the argument count.
|
---|
| 110 | * @param ppapszArgs Pointer to the argument vector pointer. NULL
|
---|
| 111 | * allowed if @a cArgs is 0.
|
---|
| 112 | * @param pszProgramPath The program path. Pass NULL if we're to figure it
|
---|
| 113 | * out ourselves.
|
---|
[11841] | 114 | */
|
---|
[57926] | 115 | RTR3DECL(int) RTR3InitEx(uint32_t iVersion, uint32_t fFlags, int cArgs, char ***ppapszArgs, const char *pszProgramPath);
|
---|
[11841] | 116 |
|
---|
[11822] | 117 | /**
|
---|
[1] | 118 | * Terminates the runtime library.
|
---|
| 119 | */
|
---|
| 120 | RTR3DECL(void) RTR3Term(void);
|
---|
| 121 |
|
---|
[44456] | 122 | /**
|
---|
[53525] | 123 | * Is IPRT succesfully initialized?
|
---|
| 124 | *
|
---|
| 125 | * @returns true/false.
|
---|
| 126 | */
|
---|
| 127 | RTR3DECL(bool) RTR3InitIsInitialized(void);
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
[44456] | 130 | * Are we running in unobtrusive mode?
|
---|
[46583] | 131 | * @returns true/false.
|
---|
[44456] | 132 | */
|
---|
| 133 | RTR3DECL(bool) RTR3InitIsUnobtrusive(void);
|
---|
[11822] | 134 | #endif /* IN_RING3 */
|
---|
[1] | 135 |
|
---|
[11822] | 136 |
|
---|
[1] | 137 | #ifdef IN_RING0
|
---|
| 138 | /**
|
---|
[33540] | 139 | * Initializes the ring-0 driver runtime library.
|
---|
[1] | 140 | *
|
---|
| 141 | * @returns iprt status code.
|
---|
| 142 | * @param fReserved Flags reserved for the future.
|
---|
| 143 | */
|
---|
| 144 | RTR0DECL(int) RTR0Init(unsigned fReserved);
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Terminates the ring-0 driver runtime library.
|
---|
| 148 | */
|
---|
| 149 | RTR0DECL(void) RTR0Term(void);
|
---|
[24179] | 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Forcibily terminates the ring-0 driver runtime library.
|
---|
| 153 | *
|
---|
| 154 | * This should be used when statically linking the IPRT. Module using dynamic
|
---|
| 155 | * linking shall use RTR0Term. If you're not sure, use RTR0Term!
|
---|
| 156 | */
|
---|
| 157 | RTR0DECL(void) RTR0TermForced(void);
|
---|
[1] | 158 | #endif
|
---|
| 159 |
|
---|
[13832] | 160 | #ifdef IN_RC
|
---|
[3123] | 161 | /**
|
---|
[13813] | 162 | * Initializes the raw-mode context runtime library.
|
---|
[3123] | 163 | *
|
---|
| 164 | * @returns iprt status code.
|
---|
| 165 | *
|
---|
| 166 | * @param u64ProgramStartNanoTS The startup timestamp.
|
---|
| 167 | */
|
---|
[26226] | 168 | RTRCDECL(int) RTRCInit(uint64_t u64ProgramStartNanoTS);
|
---|
[3123] | 169 |
|
---|
| 170 | /**
|
---|
[13813] | 171 | * Terminates the raw-mode context runtime library.
|
---|
[3123] | 172 | */
|
---|
[26226] | 173 | RTRCDECL(void) RTRCTerm(void);
|
---|
[3123] | 174 | #endif
|
---|
| 175 |
|
---|
| 176 |
|
---|
[19897] | 177 | /**
|
---|
| 178 | * Termination reason.
|
---|
| 179 | */
|
---|
| 180 | typedef enum RTTERMREASON
|
---|
| 181 | {
|
---|
| 182 | /** Normal exit. iStatus contains the exit code. */
|
---|
| 183 | RTTERMREASON_EXIT = 1,
|
---|
| 184 | /** Any abnormal exit. iStatus is 0 and has no meaning. */
|
---|
| 185 | RTTERMREASON_ABEND,
|
---|
| 186 | /** Killed by a signal. The iStatus contains the signal number. */
|
---|
| 187 | RTTERMREASON_SIGNAL,
|
---|
| 188 | /** The IPRT module is being unloaded. iStatus is 0 and has no meaning. */
|
---|
| 189 | RTTERMREASON_UNLOAD
|
---|
| 190 | } RTTERMREASON;
|
---|
| 191 |
|
---|
| 192 | /** Whether lazy clean up is Okay or not.
|
---|
| 193 | * When the process is exiting, it is a waste of time to for instance free heap
|
---|
| 194 | * memory or close open files. OTOH, when the runtime is unloaded from the
|
---|
| 195 | * process, it is important to release absolutely all resources to prevent
|
---|
| 196 | * resource leaks. */
|
---|
| 197 | #define RTTERMREASON_IS_LAZY_CLEANUP_OK(enmReason) ((enmReason) != RTTERMREASON_UNLOAD)
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * IPRT termination callback function.
|
---|
| 202 | *
|
---|
| 203 | * @param enmReason The cause of the termination.
|
---|
| 204 | * @param iStatus The meaning of this depends on enmReason.
|
---|
| 205 | * @param pvUser User argument passed to RTTermRegisterCallback.
|
---|
| 206 | */
|
---|
| 207 | typedef DECLCALLBACK(void) FNRTTERMCALLBACK(RTTERMREASON enmReason, int32_t iStatus, void *pvUser);
|
---|
| 208 | /** Pointer to an IPRT termination callback function. */
|
---|
| 209 | typedef FNRTTERMCALLBACK *PFNRTTERMCALLBACK;
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * Registers a termination callback.
|
---|
| 214 | *
|
---|
| 215 | * This is intended for performing clean up during IPRT termination. Frequently
|
---|
| 216 | * paired with lazy initialization thru RTOnce.
|
---|
| 217 | *
|
---|
| 218 | * The callbacks are called in LIFO order.
|
---|
| 219 | *
|
---|
| 220 | * @returns IPRT status code.
|
---|
| 221 | *
|
---|
| 222 | * @param pfnCallback The callback function.
|
---|
| 223 | * @param pvUser The user argument for the callback.
|
---|
| 224 | *
|
---|
| 225 | * @remarks May need to acquire a fast mutex or critical section, so use with
|
---|
| 226 | * some care in ring-0 context.
|
---|
| 227 | *
|
---|
| 228 | * @remarks Be very careful using this from code that may be unloaded before
|
---|
| 229 | * IPRT terminates. Unlike some atexit and on_exit implementations,
|
---|
| 230 | * IPRT will not automatically unregister callbacks when a module gets
|
---|
| 231 | * unloaded.
|
---|
| 232 | */
|
---|
| 233 | RTDECL(int) RTTermRegisterCallback(PFNRTTERMCALLBACK pfnCallback, void *pvUser);
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
| 236 | * Deregister a termination callback.
|
---|
| 237 | *
|
---|
| 238 | * @returns VINF_SUCCESS if found, VERR_NOT_FOUND if the callback/pvUser pair
|
---|
| 239 | * wasn't found.
|
---|
| 240 | *
|
---|
| 241 | * @param pfnCallback The callback function.
|
---|
| 242 | * @param pvUser The user argument for the callback.
|
---|
| 243 | */
|
---|
| 244 | RTDECL(int) RTTermDeregisterCallback(PFNRTTERMCALLBACK pfnCallback, void *pvUser);
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * Runs the termination callback queue.
|
---|
| 248 | *
|
---|
| 249 | * Normally called by an internal IPRT termination function, but may also be
|
---|
| 250 | * called by external code immediately prior to terminating IPRT if it is in a
|
---|
| 251 | * better position to state the termination reason and/or status.
|
---|
| 252 | *
|
---|
| 253 | * @param enmReason The reason why it's called.
|
---|
| 254 | * @param iStatus The associated exit status or signal number.
|
---|
| 255 | */
|
---|
| 256 | RTDECL(void) RTTermRunCallbacks(RTTERMREASON enmReason, int32_t iStatus);
|
---|
| 257 |
|
---|
[1] | 258 | /** @} */
|
---|
| 259 |
|
---|
| 260 | /** @} */
|
---|
| 261 |
|
---|
[20374] | 262 | RT_C_DECLS_END
|
---|
[1] | 263 |
|
---|
| 264 |
|
---|
[76585] | 265 | #endif /* !IPRT_INCLUDED_initterm_h */
|
---|
[1] | 266 |
|
---|