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