2 * internal.h: internal definitions just used by code from the library
4 * Copyright (C) 2006-2014 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
32 # undef NDEBUG /* Don't let a prior NDEBUG definition cause trouble. */
34 # define sa_assert(expr) assert (expr)
36 # define sa_assert(expr) /* empty */
39 /* The library itself is allowed to use deprecated functions /
40 * variables, so effectively undefine the deprecated attribute
41 * which would otherwise be defined in libvirt.h.
44 #define VIR_DEPRECATED /*empty*/
46 /* The library itself needs to know enum sizes. */
47 #define VIR_ENUM_SENTINELS
50 # define DEFAULT_TEXT_DOMAIN PACKAGE
52 # define _(str) dgettext(PACKAGE, str)
53 #else /* HAVE_LIBINTL_H */
55 #endif /* HAVE_LIBINTL_H */
58 #include "libvirt/libvirt.h"
59 #include "libvirt/libvirt-lxc.h"
60 #include "libvirt/libvirt-qemu.h"
61 #include "libvirt/libvirt-admin.h"
62 #include "libvirt/virterror.h"
64 #include "c-strcase.h"
65 #include "ignore-value.h"
66 #include "count-leading-zeros.h"
68 /* String equality tests, suggested by Jim Meyering. */
69 #define STREQ(a, b) (strcmp(a, b) == 0)
70 #define STRCASEEQ(a, b) (c_strcasecmp(a, b) == 0)
71 #define STRNEQ(a, b) (strcmp(a, b) != 0)
72 #define STRCASENEQ(a, b) (c_strcasecmp(a, b) != 0)
73 #define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
74 #define STRCASEEQLEN(a, b, n) (c_strncasecmp(a, b, n) == 0)
75 #define STRNEQLEN(a, b, n) (strncmp(a, b, n) != 0)
76 #define STRCASENEQLEN(a, b, n) (c_strncasecmp(a, b, n) != 0)
77 #define STRPREFIX(a, b) (strncmp(a, b, strlen(b)) == 0)
78 #define STRCASEPREFIX(a, b) (c_strncasecmp(a, b, strlen(b)) == 0)
79 #define STRSKIP(a, b) (STRPREFIX(a, b) ? (a) + strlen(b) : NULL)
81 #define STREQ_NULLABLE(a, b) \
82 ((a) ? (b) && STREQ((a), (b)) : !(b))
83 #define STRNEQ_NULLABLE(a, b) \
84 ((a) ? !(b) || STRNEQ((a), (b)) : !!(b))
86 #define NUL_TERMINATE(buf) do { (buf)[sizeof(buf)-1] = '\0'; } while (0)
87 #define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array)))
92 * Macro to flag consciously unused parameters to functions
94 #ifndef ATTRIBUTE_UNUSED
95 # define ATTRIBUTE_UNUSED __attribute__((__unused__))
101 * Macro to indicate that a function won't return to the caller
103 #ifndef ATTRIBUTE_NORETURN
104 # define ATTRIBUTE_NORETURN __attribute__((__noreturn__))
108 * ATTRIBUTE_SENTINEL:
110 * Macro to check for NULL-terminated varargs lists
112 #ifndef ATTRIBUTE_SENTINEL
113 # define ATTRIBUTE_SENTINEL __attribute__((__sentinel__))
117 * ATTRIBUTE_NOINLINE:
119 * Force compiler not to inline a method. Should be used if
120 * the method need to be overridable by test mocks.
122 #ifndef ATTRIBUTE_NOINLINE
123 # define ATTRIBUTE_NOINLINE __attribute__((__noinline__))
127 * ATTRIBUTE_FMT_PRINTF
129 * Macro used to check printf like functions, if compiling
132 * We use gnulib which guarantees we always have GNU style
133 * printf format specifiers even on broken Win32 platforms
134 * hence we have to force 'gnu_printf' for new GCC
136 #ifndef ATTRIBUTE_FMT_PRINTF
138 # define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \
139 __attribute__((__format__ (__gnu_printf__, fmtpos, argpos)))
141 # define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \
142 __attribute__((__format__ (__printf__, fmtpos, argpos)))
146 #ifndef ATTRIBUTE_RETURN_CHECK
147 # define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
153 * force a structure to be packed, i.e. not following architecture and
154 * compiler best alignments for its sub components. It's needed for example
155 * for the network filetering code when defining the content of raw
157 * Others compiler than gcc may use something different e.g. #pragma pack(1)
159 #ifndef ATTRIBUTE_PACKED
160 # define ATTRIBUTE_PACKED __attribute__((packed))
163 /* gcc's handling of attribute nonnull is less than stellar - it does
164 * NOT improve diagnostics, and merely allows gcc to optimize away
165 * null code checks even when the caller manages to pass null in spite
166 * of the attribute, leading to weird crashes. Coverity, on the other
167 * hand, knows how to do better static analysis based on knowing
168 * whether a parameter is nonnull. Make this attribute conditional
169 * based on whether we are compiling for real or for analysis, while
170 * still requiring correct gcc syntax when it is turned off. See also
171 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17308 */
172 #ifndef ATTRIBUTE_NONNULL
174 # define ATTRIBUTE_NONNULL(m) __attribute__((__nonnull__(m)))
176 # define ATTRIBUTE_NONNULL(m) __attribute__(())
180 #ifndef ATTRIBUTE_FALLTHROUGH
181 # if __GNUC_PREREQ (7, 0)
182 # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
184 # define ATTRIBUTE_FALLTHROUGH do {} while(0)
188 #if WORKING_PRAGMA_PUSH
189 # define VIR_WARNINGS_NO_CAST_ALIGN \
190 _Pragma ("GCC diagnostic push") \
191 _Pragma ("GCC diagnostic ignored \"-Wcast-align\"")
193 # define VIR_WARNINGS_NO_DEPRECATED \
194 _Pragma ("GCC diagnostic push") \
195 _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
197 # if HAVE_SUGGEST_ATTRIBUTE_FORMAT
198 # define VIR_WARNINGS_NO_PRINTF \
199 _Pragma ("GCC diagnostic push") \
200 _Pragma ("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
202 # define VIR_WARNINGS_NO_PRINTF \
203 _Pragma ("GCC diagnostic push")
206 /* Workaround bogus GCC 6.0 for logical 'or' equal expression warnings.
208 # if BROKEN_GCC_WLOGICALOP_EQUAL_EXPR
209 # define VIR_WARNINGS_NO_WLOGICALOP_EQUAL_EXPR \
210 _Pragma ("GCC diagnostic push") \
211 _Pragma ("GCC diagnostic ignored \"-Wlogical-op\"")
213 # define VIR_WARNINGS_NO_WLOGICALOP_EQUAL_EXPR \
214 _Pragma ("GCC diagnostic push")
217 # define VIR_WARNINGS_RESET \
218 _Pragma ("GCC diagnostic pop")
220 # define VIR_WARNINGS_NO_CAST_ALIGN
221 # define VIR_WARNINGS_NO_DEPRECATED
222 # define VIR_WARNINGS_NO_PRINTF
223 # define VIR_WARNINGS_NO_WLOGICALOP_EQUAL_EXPR
224 # define VIR_WARNINGS_RESET
227 /* Workaround bogus GCC < 4.6 that produces false -Wlogical-op warnings for
228 * strchr(). Those old GCCs don't support push/pop. */
229 #if BROKEN_GCC_WLOGICALOP_STRCHR
230 # define VIR_WARNINGS_NO_WLOGICALOP_STRCHR \
231 _Pragma ("GCC diagnostic ignored \"-Wlogical-op\"")
233 # define VIR_WARNINGS_NO_WLOGICALOP_STRCHR
238 * Use this when passing possibly-NULL strings to printf-a-likes.
240 #define NULLSTR(s) ((s) ? (s) : "<null>")
243 * Turn a NULL string into an empty string
245 #define NULLSTR_EMPTY(s) ((s) ? (s) : "")
248 * Turn a NULL string into a star
250 #define NULLSTR_STAR(s) ((s) ? (s) : "*")
253 * Turn a NULL string into a minus sign
255 #define NULLSTR_MINUS(s) ((s) ? (s) : "-")
260 * In place exchange of two values
272 * Steals pointer passed as second argument into the first argument. Second
273 * argument must not have side effects.
275 #define VIR_STEAL_PTR(a, b) \
283 * @ret: pointer to return
285 * Returns value of @ret while clearing @ret. This ensures that pointers
286 * freed by using VIR_AUTOPTR can be easily passed back to the caller without
287 * any temporary variable. @ptr is evaluated more than once.
289 #define VIR_RETURN_PTR(ptr) \
291 typeof(ptr) virTemporaryReturnPointer = (ptr); \
293 return virTemporaryReturnPointer; \
298 * @supported: an OR'ed set of supported flags
299 * @retval: return value in case unsupported flags were passed
301 * To avoid memory leaks this macro has to be used before any non-trivial
302 * code which could possibly allocate some memory.
304 * Returns nothing. Exits the caller function if unsupported flags were
307 #define virCheckFlags(supported, retval) \
309 unsigned long __unsuppflags = flags & ~(supported); \
310 if (__unsuppflags) { \
311 virReportInvalidArg(flags, \
312 _("unsupported flags (0x%lx) in function %s"), \
313 __unsuppflags, __FUNCTION__); \
320 * @supported: an OR'ed set of supported flags
321 * @label: label to jump to on error
323 * To avoid memory leaks this macro has to be used before any non-trivial
324 * code which could possibly allocate some memory.
326 * Returns nothing. Jumps to a label if unsupported flags were
329 #define virCheckFlagsGoto(supported, label) \
331 unsigned long __unsuppflags = flags & ~(supported); \
332 if (__unsuppflags) { \
333 virReportInvalidArg(flags, \
334 _("unsupported flags (0x%lx) in function %s"), \
335 __unsuppflags, __FUNCTION__); \
340 /* Macros to help dealing with mutually exclusive flags. */
343 * VIR_EXCLUSIVE_FLAGS_RET:
345 * @FLAG1: First flag to be checked.
346 * @FLAG2: Second flag to be checked.
347 * @RET: Return value.
349 * Reject mutually exclusive API flags. The checked flags are compared
350 * with flags variable.
352 * This helper does an early return and therefore it has to be called
353 * before anything that would require cleanup.
355 #define VIR_EXCLUSIVE_FLAGS_RET(FLAG1, FLAG2, RET) \
357 if ((flags & FLAG1) && (flags & FLAG2)) { \
358 virReportInvalidArg(ctl, \
359 _("Flags '%s' and '%s' are mutually " \
367 * VIR_EXCLUSIVE_FLAGS_GOTO:
369 * @FLAG1: First flag to be checked.
370 * @FLAG2: Second flag to be checked.
371 * @LABEL: Label to jump to.
373 * Reject mutually exclusive API flags. The checked flags are compared
374 * with flags variable.
376 * Returns nothing. Jumps to a label if unsupported flags were
379 #define VIR_EXCLUSIVE_FLAGS_GOTO(FLAG1, FLAG2, LABEL) \
381 if ((flags & FLAG1) && (flags & FLAG2)) { \
382 virReportInvalidArg(ctl, \
383 _("Flags '%s' and '%s' are mutually " \
390 /* Macros to help dealing with flag requirements. */
393 * VIR_REQUIRE_FLAG_RET:
395 * @FLAG1: First flag to be checked.
396 * @FLAG2: Second flag that is required by first flag.
397 * @RET: Return value.
399 * Check whether required flag is set. The checked flags are compared
400 * with flags variable.
402 * This helper does an early return and therefore it has to be called
403 * before anything that would require cleanup.
405 #define VIR_REQUIRE_FLAG_RET(FLAG1, FLAG2, RET) \
407 if ((flags & FLAG1) && !(flags & FLAG2)) { \
408 virReportInvalidArg(ctl, \
409 _("Flag '%s' is required by flag '%s'"), \
416 * VIR_REQUIRE_FLAG_GOTO:
418 * @FLAG1: First flag to be checked.
419 * @FLAG2: Second flag that is required by first flag.
420 * @LABEL: Label to jump to.
422 * Check whether required flag is set. The checked flags are compared
423 * with flags variable.
425 * Returns nothing. Jumps to a label if required flag is not set.
427 #define VIR_REQUIRE_FLAG_GOTO(FLAG1, FLAG2, LABEL) \
429 if ((flags & FLAG1) && !(flags & FLAG2)) { \
430 virReportInvalidArg(ctl, \
431 _("Flag '%s' is required by flag '%s'"), \
437 #define virCheckNonNullArgReturn(argname, retval) \
439 if (argname == NULL) { \
440 virReportInvalidNonNullArg(argname); \
444 #define virCheckNullArgGoto(argname, label) \
446 if (argname != NULL) { \
447 virReportInvalidNullArg(argname); \
451 #define virCheckNonNullArgGoto(argname, label) \
453 if (argname == NULL) { \
454 virReportInvalidNonNullArg(argname); \
458 #define virCheckNonEmptyStringArgGoto(argname, label) \
460 if (argname == NULL) { \
461 virReportInvalidNonNullArg(argname); \
464 if (*argname == '\0') { \
465 virReportInvalidEmptyStringArg(argname); \
469 #define virCheckPositiveArgGoto(argname, label) \
471 if (argname <= 0) { \
472 virReportInvalidPositiveArg(argname); \
476 #define virCheckPositiveArgReturn(argname, retval) \
478 if (argname <= 0) { \
479 virReportInvalidPositiveArg(argname); \
483 #define virCheckNonZeroArgGoto(argname, label) \
485 if (argname == 0) { \
486 virReportInvalidNonZeroArg(argname); \
490 #define virCheckZeroArgGoto(argname, label) \
492 if (argname != 0) { \
493 virReportInvalidNonZeroArg(argname); \
497 #define virCheckNonNegativeArgGoto(argname, label) \
500 virReportInvalidNonNegativeArg(argname); \
504 #define virCheckReadOnlyGoto(flags, label) \
506 if ((flags) & VIR_CONNECT_RO) { \
507 virReportRestrictedError(_("read only access prevents %s"), \
515 /* divide value by size, rounding up */
516 #define VIR_DIV_UP(value, size) (((value) + (size) - 1) / (size))
518 /* round up value to the closest multiple of size */
519 #define VIR_ROUND_UP(value, size) (VIR_DIV_UP(value, size) * (size))
521 /* Round up to the next closest power of 2. It will return rounded number or 0
522 * for 0 or number more than 2^31 (for 32bit unsigned int). */
523 #define VIR_ROUND_UP_POWER_OF_TWO(value) \
524 ((value) > 0 && (value) <= 1U << (sizeof(unsigned int) * 8 - 1) ? \
525 1U << (sizeof(unsigned int) * 8 - count_leading_zeros((value) - 1)) : 0)
528 /* Specific error values for use in forwarding programs such as
529 * virt-login-shell; these values match what GNU env does. */
531 EXIT_CANCELED
= 125, /* Failed before attempting exec */
532 EXIT_CANNOT_INVOKE
= 126, /* Exists but couldn't exec */
533 EXIT_ENOENT
= 127, /* Could not find program to exec */