1 | /** @file
|
---|
2 | * libslirp: glib replacement header
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef INCLUDED_glib_h
|
---|
37 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
38 | # pragma once
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #ifndef LOG_GROUP
|
---|
42 | # define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
43 | #endif
|
---|
44 | #include <VBox/log.h>
|
---|
45 |
|
---|
46 | #include <iprt/asm.h> /* for RT_BE2H_XXX and RT_H2BE_XXX */
|
---|
47 | #include <iprt/assert.h>
|
---|
48 | #include <iprt/getopt.h>
|
---|
49 | #include <iprt/mem.h>
|
---|
50 | #include <iprt/err.h>
|
---|
51 | #include <iprt/stdarg.h>
|
---|
52 | #include <iprt/string.h>
|
---|
53 | #include <iprt/rand.h>
|
---|
54 | #include <iprt/handletable.h>
|
---|
55 |
|
---|
56 | #if !defined(RT_OS_WINDOWS)
|
---|
57 | # include <signal.h>
|
---|
58 | # define G_OS_UNIX
|
---|
59 | #else
|
---|
60 | # define G_OS_WIN32
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #define G_LITTLE_ENDIAN 1234
|
---|
64 | #define G_BIG_ENDIAN 4321
|
---|
65 | #define GLIB_SIZEOF_VOID_P 8
|
---|
66 |
|
---|
67 | #ifndef RT_BIG_ENDIAN
|
---|
68 | # define G_BYTE_ORDER G_LITTLE_ENDIAN
|
---|
69 | #else
|
---|
70 | # define G_BYTE_ORDER G_BIG_ENDIAN
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | /** @name Typedefs
|
---|
74 | * @{ */
|
---|
75 | typedef char gchar;
|
---|
76 | typedef bool gboolean;
|
---|
77 | typedef int gint;
|
---|
78 | typedef int GPid;
|
---|
79 | typedef void *gpointer;
|
---|
80 | typedef int GRand;
|
---|
81 | typedef void *GSpawnChildSetupFunc;
|
---|
82 | typedef int GSpawnFlags;
|
---|
83 | typedef char **GStrv;
|
---|
84 | /** @} */
|
---|
85 |
|
---|
86 | /** @name Constants
|
---|
87 | * @{ */
|
---|
88 | #define STR_LEN_MAX 4096
|
---|
89 | #define G_SPAWN_SEARCH_PATH 4
|
---|
90 | #undef FALSE /* windows.h */
|
---|
91 | #define FALSE false
|
---|
92 | #undef TRUE /* windows.h */
|
---|
93 | #define TRUE true
|
---|
94 | /* #} */
|
---|
95 |
|
---|
96 | /** @name Data Structures
|
---|
97 | * @{ */
|
---|
98 |
|
---|
99 | typedef struct GError
|
---|
100 | {
|
---|
101 | uint32_t domain;
|
---|
102 | int code;
|
---|
103 | char *message;
|
---|
104 | } GError;
|
---|
105 |
|
---|
106 | typedef struct GString
|
---|
107 | {
|
---|
108 | char *str;
|
---|
109 | size_t len;
|
---|
110 | size_t cbAlloc;
|
---|
111 | } GString;
|
---|
112 |
|
---|
113 | /** @} */
|
---|
114 |
|
---|
115 | #define g_assert(x) Assert(x)
|
---|
116 | #define g_assert_not_reached() AssertFatalFailed()
|
---|
117 |
|
---|
118 | DECLINLINE(void) g_critical(const char *pszFmt, ...)
|
---|
119 | {
|
---|
120 | va_list args;
|
---|
121 | va_start(args, pszFmt);
|
---|
122 | RTLogRelPrintf("%N", pszFmt, &args);
|
---|
123 | va_end(args);
|
---|
124 | #ifdef LOG_ENABLED
|
---|
125 | va_start(args, pszFmt);
|
---|
126 | Log(("%N", pszFmt, &args));
|
---|
127 | va_end(args);
|
---|
128 | #endif
|
---|
129 | }
|
---|
130 |
|
---|
131 | #define g_error(...) LogRel((__VA_ARGS__))
|
---|
132 | #define g_warning(...) LogRelWarn((__VA_ARGS__))
|
---|
133 |
|
---|
134 | #define g_warn_if_fail(x) do { \
|
---|
135 | if (RT_LIKELY(!x)) { /* likely */ } \
|
---|
136 | else g_warning("WARNING\n"); \
|
---|
137 | } while (0)
|
---|
138 |
|
---|
139 | #define g_warn_if_reached() g_warning("WARNING: function %s line %s", __PRETTY_FUNCTION__, __LINE__)
|
---|
140 |
|
---|
141 | #define g_return_if_fail(a_Expr) do { if (RT_LIKELY((a_Expr))) {/* likely */} else return; } while (0)
|
---|
142 |
|
---|
143 | #define g_return_val_if_fail(a_Expr, a_rc) do { if (RT_LIKELY((a_Expr))) {/* likely */} else return (a_rc); } while (0)
|
---|
144 |
|
---|
145 | #define G_STATIC_ASSERT(x) AssertCompile(x)
|
---|
146 |
|
---|
147 |
|
---|
148 | /** @name Memory Allocation
|
---|
149 | * @{ */
|
---|
150 |
|
---|
151 | #define g_free(x) RTMemFree(x)
|
---|
152 | #define g_malloc(x) RTMemAlloc(x)
|
---|
153 | #define g_new(x, y) RTMemAlloc(sizeof(x) * (y))
|
---|
154 | #define g_new0(x, y) RTMemAllocZ(sizeof(x) * (y))
|
---|
155 | #define g_malloc0(x) RTMemAllocZ(x)
|
---|
156 | #define g_realloc(x, y) RTMemRealloc(x, y)
|
---|
157 |
|
---|
158 | DECLINLINE(GError *) g_error_new(uint32_t uDomain, int iErr, const char *pszMsg)
|
---|
159 | {
|
---|
160 | size_t cbMsg = pszMsg ? strlen(pszMsg) + 1 : 0;
|
---|
161 | GError *pErr = (GError *)g_malloc(sizeof(*pErr) + cbMsg);
|
---|
162 | if (pErr)
|
---|
163 | {
|
---|
164 | pErr->domain = uDomain;
|
---|
165 | pErr->code = iErr;
|
---|
166 | pErr->message = pszMsg ? (char *)memcpy(pErr + 1, pszMsg, cbMsg) : NULL;
|
---|
167 | }
|
---|
168 | return pErr;
|
---|
169 | }
|
---|
170 |
|
---|
171 | DECLINLINE(void) g_error_free(GError *pErr)
|
---|
172 | {
|
---|
173 | g_free(pErr);
|
---|
174 | }
|
---|
175 |
|
---|
176 | DECLINLINE(char *) g_strdup(const char *pcszStr)
|
---|
177 | {
|
---|
178 | if (pcszStr != NULL)
|
---|
179 | return RTStrDup(pcszStr);
|
---|
180 | return NULL;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** @} */
|
---|
184 |
|
---|
185 | /** @name String Functions
|
---|
186 | * @{ */
|
---|
187 | #ifdef __GNUC__
|
---|
188 | # define G_GNUC_PRINTF(x, y) __attribute__((format (printf, x, y)))
|
---|
189 | #else
|
---|
190 | # define G_GNUC_PRINTF(x, y)
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | DECLINLINE(int) g_vsnprintf(char *pszDst, size_t cbDst, const char *pszFmt, va_list va)
|
---|
194 | {
|
---|
195 | ssize_t cchRet = RTStrPrintf2V(pszDst, cbDst, pszFmt, va);
|
---|
196 | return (int)(cchRet >= 0 ? cchRet : -cchRet);
|
---|
197 | }
|
---|
198 |
|
---|
199 | DECLINLINE(int) g_snprintf(char *pszDst, size_t cbDst, const char *pszFmt, ...)
|
---|
200 | {
|
---|
201 | va_list va;
|
---|
202 | va_start(va, pszFmt);
|
---|
203 | int cchRet = g_vsnprintf(pszDst, cbDst, pszFmt, va);
|
---|
204 | va_end(va);
|
---|
205 | return cchRet;
|
---|
206 | }
|
---|
207 |
|
---|
208 | DECLINLINE(GString *) g_string_new(const char *pszInitial)
|
---|
209 | {
|
---|
210 | GString *pGStr = (GString *)RTMemAlloc(sizeof(GString));
|
---|
211 | if (pGStr)
|
---|
212 | {
|
---|
213 | size_t const cchSrc = pszInitial ? strlen(pszInitial) : 0;
|
---|
214 | size_t const cbAlloc = RT_ALIGN_Z(cchSrc + 1, 64);
|
---|
215 | pGStr->str = RTStrAlloc(cbAlloc);
|
---|
216 | AssertReturnStmt(pGStr->str, RTMemFree(pGStr), NULL);
|
---|
217 |
|
---|
218 | if (pszInitial)
|
---|
219 | memcpy(pGStr->str, pszInitial, cchSrc);
|
---|
220 | pGStr->str[cchSrc] = '\0';
|
---|
221 | pGStr->len = cchSrc;
|
---|
222 | pGStr->cbAlloc = cbAlloc;
|
---|
223 | }
|
---|
224 |
|
---|
225 | return pGStr;
|
---|
226 | }
|
---|
227 |
|
---|
228 | DECLINLINE(char *) g_string_free(GString *pGStr, bool fFreeCString)
|
---|
229 | {
|
---|
230 | char *pszCString = pGStr->str;
|
---|
231 | pGStr->str = NULL;
|
---|
232 | pGStr->len = 0;
|
---|
233 | pGStr->cbAlloc = 0;
|
---|
234 | RTMemFree(pGStr);
|
---|
235 | if (fFreeCString)
|
---|
236 | {
|
---|
237 | RTStrFree(pszCString);
|
---|
238 | pszCString = NULL;
|
---|
239 | }
|
---|
240 | return pszCString;
|
---|
241 | }
|
---|
242 |
|
---|
243 | DECLINLINE(GString *) g_string_append_len(GString *pGStr, const char *pszSrc, size_t cchSrc)
|
---|
244 | {
|
---|
245 | size_t cchDst = pGStr->len;
|
---|
246 | if (cchDst + cchSrc >= pGStr->cbAlloc)
|
---|
247 | {
|
---|
248 | size_t const cbAlloc = RT_ALIGN_Z(cchDst + cchSrc + 16, 64);
|
---|
249 | int rc = RTStrRealloc(&pGStr->str, cbAlloc);
|
---|
250 | AssertRCReturn(RT_SUCCESS(rc), NULL);
|
---|
251 | pGStr->cbAlloc = cbAlloc;
|
---|
252 | }
|
---|
253 | memcpy(&pGStr->str[cchDst], pszSrc, cchSrc);
|
---|
254 | cchDst += cchSrc;
|
---|
255 | pGStr->str[cchDst] = '\0';
|
---|
256 | pGStr->len = cchDst;
|
---|
257 | return pGStr;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | DECLINLINE(GString *) g_string_append_printf(GString *pGStr, const char *pszFmt, ...)
|
---|
262 | {
|
---|
263 | va_list args;
|
---|
264 | va_start(args, pszFmt);
|
---|
265 | char *pszTmp = NULL;
|
---|
266 | ssize_t cchRet = RTStrAPrintfV(&pszTmp, pszFmt, args);
|
---|
267 | va_end(args);
|
---|
268 | AssertReturn(cchRet >= 0, NULL);
|
---|
269 |
|
---|
270 | pGStr = g_string_append_len(pGStr, pszTmp, (size_t)cchRet);
|
---|
271 | RTStrFree(pszTmp);
|
---|
272 | return pGStr;
|
---|
273 | }
|
---|
274 |
|
---|
275 | DECLINLINE(char *) g_strstr_len(const char *pszHaystack, ssize_t cchHaystack, const char *pszNeedle)
|
---|
276 | {
|
---|
277 | if (cchHaystack != -1)
|
---|
278 | {
|
---|
279 | size_t const cchNeedle = strlen(pszNeedle);
|
---|
280 | if (cchHaystack >= (ssize_t)cchNeedle)
|
---|
281 | {
|
---|
282 | char const ch0Needle = *pszNeedle;
|
---|
283 | cchHaystack -= cchNeedle - 1;
|
---|
284 | while (cchHaystack > 0)
|
---|
285 | {
|
---|
286 | const char *pszHit = (const char *)memchr(pszHaystack, ch0Needle, cchHaystack);
|
---|
287 | if (!pszHit)
|
---|
288 | return NULL;
|
---|
289 | if (memcmp(pszHit, pszNeedle, cchNeedle) == 0)
|
---|
290 | return (char *)pszHit;
|
---|
291 | cchHaystack -= (ssize_t)(&pszHit[1] - pszHaystack);
|
---|
292 | pszHaystack = &pszHit[1];
|
---|
293 | }
|
---|
294 | }
|
---|
295 | return NULL;
|
---|
296 | }
|
---|
297 | return RTStrStr(pszHaystack, pszNeedle);
|
---|
298 | }
|
---|
299 |
|
---|
300 | DECLINLINE(bool) g_str_has_prefix(const char *pcszStr, const char *pcszPrefix)
|
---|
301 | {
|
---|
302 | return RTStrStartsWith(pcszStr, pcszPrefix);
|
---|
303 | }
|
---|
304 |
|
---|
305 | DECLINLINE(void) g_strfreev(char **papszStrings)
|
---|
306 | {
|
---|
307 | if (papszStrings)
|
---|
308 | {
|
---|
309 | size_t i = 0;
|
---|
310 | char *pszEntry;
|
---|
311 | while ((pszEntry = papszStrings[i]) != NULL)
|
---|
312 | {
|
---|
313 | RTStrFree(pszEntry);
|
---|
314 | papszStrings[i] = NULL;
|
---|
315 | i++;
|
---|
316 | }
|
---|
317 | RTMemFree(papszStrings);
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | DECLINLINE(size_t) g_strlcpy(char *pszDst, const char *pszSrc, size_t cchSrc)
|
---|
322 | {
|
---|
323 | return RTStrCopy(pszDst, cchSrc, pszSrc);
|
---|
324 | }
|
---|
325 |
|
---|
326 | DECLINLINE(unsigned) g_strv_length(char **papszStrings)
|
---|
327 | {
|
---|
328 | unsigned cStrings = 0;
|
---|
329 | AssertPtr(papszStrings);
|
---|
330 | if (papszStrings)
|
---|
331 | while (*papszStrings++ != NULL)
|
---|
332 | cStrings++;
|
---|
333 | return cStrings;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /** This is only used once to report a g_vsnprintf error for which errno (our x)
|
---|
337 | * isn't even set. */
|
---|
338 | #define g_strerror(x) ("whatever")
|
---|
339 |
|
---|
340 | /** @} */
|
---|
341 |
|
---|
342 | /** @name Misc. Functions
|
---|
343 | * @{ */
|
---|
344 | #define GLIB_CHECK_VERSION(x, y, z) true
|
---|
345 |
|
---|
346 | #define GINT16_FROM_BE(x) RT_BE2H_S16(x)
|
---|
347 | #define GINT16_TO_BE(x) RT_H2BE_S16(x)
|
---|
348 | #define GINT32_FROM_BE(x) RT_BE2H_S32(x)
|
---|
349 | #define GINT32_TO_BE(x) RT_H2BE_S32(x)
|
---|
350 |
|
---|
351 | #define GUINT16_FROM_BE(x) RT_BE2H_U16(x)
|
---|
352 | #define GUINT16_TO_BE(x) RT_H2BE_U16(x)
|
---|
353 | #define GUINT32_FROM_BE(x) RT_BE2H_U32(x)
|
---|
354 | #define GUINT32_TO_BE(x) RT_H2BE_U32(x)
|
---|
355 |
|
---|
356 | #define MAX(x, y) RT_MAX(x, y)
|
---|
357 | #define MIN(x, y) RT_MIN(x, y)
|
---|
358 | #define G_N_ELEMENTS(x) RT_ELEMENTS(x)
|
---|
359 | #define G_LIKELY(x) RT_LIKELY(x)
|
---|
360 | #define G_UNLIKELY(x) RT_UNLIKELY(x)
|
---|
361 |
|
---|
362 | #define g_rand_int_range(a_hRand, a_uFirst, a_uEnd) RTRandS32Ex(a_uFirst, (a_uEnd) - 1)
|
---|
363 | #define g_rand_new() NULL
|
---|
364 | #define g_rand_free(a_hRand) ((void)0)
|
---|
365 |
|
---|
366 | DECLINLINE(bool) g_shell_parse_argv(const char *pszCmdLine, int *pcArgs, char ***ppapszArgs, GError **ppErr)
|
---|
367 | {
|
---|
368 | char **papszTmpArgs = NULL;
|
---|
369 | int cTmpArgs = 0;
|
---|
370 | int rc = RTGetOptArgvFromString(&papszTmpArgs, &cTmpArgs, pszCmdLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL /*pszIfs*/);
|
---|
371 | if (RT_SUCCESS(rc))
|
---|
372 | {
|
---|
373 | /* Must create a regular string array compatible with g_strfreev. */
|
---|
374 | char **papszArgs = (char **)RTMemAllocZ(sizeof(char *) * (cTmpArgs + 1));
|
---|
375 | if (papszArgs)
|
---|
376 | {
|
---|
377 | int iArg = 0;
|
---|
378 | while (iArg < cTmpArgs)
|
---|
379 | {
|
---|
380 | papszArgs[iArg] = RTStrDup(papszTmpArgs[iArg]);
|
---|
381 | AssertBreak(papszArgs[iArg]);
|
---|
382 | iArg++;
|
---|
383 | }
|
---|
384 | if (iArg == cTmpArgs)
|
---|
385 | {
|
---|
386 | papszArgs[iArg] = NULL;
|
---|
387 |
|
---|
388 | *ppapszArgs = papszArgs;
|
---|
389 | *pcArgs = iArg;
|
---|
390 | if (ppErr)
|
---|
391 | *ppErr = NULL;
|
---|
392 | RTGetOptArgvFree(papszTmpArgs);
|
---|
393 | return true;
|
---|
394 | }
|
---|
395 |
|
---|
396 | while (iArg-- > 0)
|
---|
397 | RTStrFree(papszArgs[iArg]);
|
---|
398 | RTMemFree(papszArgs);
|
---|
399 | }
|
---|
400 | RTGetOptArgvFree(papszTmpArgs);
|
---|
401 | if (ppErr)
|
---|
402 | *ppErr = g_error_new(0, VERR_NO_STR_MEMORY, "VERR_NO_STR_MEMORY");
|
---|
403 | }
|
---|
404 | else if (ppErr)
|
---|
405 | *ppErr = g_error_new(0, rc, "RTGetOptArgvFromString failed");
|
---|
406 | return false;
|
---|
407 | }
|
---|
408 |
|
---|
409 | DECLINLINE(bool) g_spawn_async_with_fds(const char *working_directory, char **argv,
|
---|
410 | char **envp, int flags,
|
---|
411 | GSpawnChildSetupFunc child_setup,
|
---|
412 | void * user_data, GPid *child_pid, int stdin_fd,
|
---|
413 | int stdout_fd, int stderr_fd, GError **error)
|
---|
414 | {
|
---|
415 | /** @todo r=bird: This is best replaced directly in the src/misc.c file as
|
---|
416 | * there we know exactly what stdin_fd, stdout_fd and stderr_fd are
|
---|
417 | * can can do a better mapping to RTProcCreateEx. */
|
---|
418 | AssertFailed();
|
---|
419 | return false;
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /** @} */
|
---|
424 |
|
---|
425 | #endif
|
---|