m4: Fix check for yajl.pc
[libvirt/ericb.git] / src / internal.h
blobf718895460ea6e0cd097a7c89342f3ae6a9d12a2
1 /*
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/>.
21 #pragma once
23 #include <errno.h>
24 #include <limits.h>
25 #include <verify.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
31 #if STATIC_ANALYSIS
32 # undef NDEBUG /* Don't let a prior NDEBUG definition cause trouble. */
33 # include <assert.h>
34 # define sa_assert(expr) assert (expr)
35 #else
36 # define sa_assert(expr) /* empty */
37 #endif
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.
43 #undef VIR_DEPRECATED
44 #define VIR_DEPRECATED /*empty*/
46 /* The library itself needs to know enum sizes. */
47 #define VIR_ENUM_SENTINELS
49 #ifdef HAVE_LIBINTL_H
50 # define DEFAULT_TEXT_DOMAIN PACKAGE
51 # include <libintl.h>
52 # define _(str) dgettext(PACKAGE, str)
53 #else /* HAVE_LIBINTL_H */
54 # define _(str) str
55 #endif /* HAVE_LIBINTL_H */
56 #define N_(str) str
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)))
89 /**
90 * ATTRIBUTE_UNUSED:
92 * Macro to flag consciously unused parameters to functions
94 #ifndef ATTRIBUTE_UNUSED
95 # define ATTRIBUTE_UNUSED __attribute__((__unused__))
96 #endif
98 /**
99 * ATTRIBUTE_NORETURN:
101 * Macro to indicate that a function won't return to the caller
103 #ifndef ATTRIBUTE_NORETURN
104 # define ATTRIBUTE_NORETURN __attribute__((__noreturn__))
105 #endif
108 * ATTRIBUTE_SENTINEL:
110 * Macro to check for NULL-terminated varargs lists
112 #ifndef ATTRIBUTE_SENTINEL
113 # define ATTRIBUTE_SENTINEL __attribute__((__sentinel__))
114 #endif
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__))
124 #endif
127 * ATTRIBUTE_FMT_PRINTF
129 * Macro used to check printf like functions, if compiling
130 * with gcc.
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
137 # ifndef __clang__
138 # define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \
139 __attribute__((__format__ (__gnu_printf__, fmtpos, argpos)))
140 # else
141 # define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \
142 __attribute__((__format__ (__printf__, fmtpos, argpos)))
143 # endif
144 #endif
146 #ifndef ATTRIBUTE_RETURN_CHECK
147 # define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
148 #endif
151 * ATTRIBUTE_PACKED
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
156 * ethernet packets.
157 * Others compiler than gcc may use something different e.g. #pragma pack(1)
159 #ifndef ATTRIBUTE_PACKED
160 # define ATTRIBUTE_PACKED __attribute__((packed))
161 #endif
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
173 # if STATIC_ANALYSIS
174 # define ATTRIBUTE_NONNULL(m) __attribute__((__nonnull__(m)))
175 # else
176 # define ATTRIBUTE_NONNULL(m) __attribute__(())
177 # endif
178 #endif
180 #ifndef ATTRIBUTE_FALLTHROUGH
181 # if __GNUC_PREREQ (7, 0)
182 # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
183 # else
184 # define ATTRIBUTE_FALLTHROUGH do {} while(0)
185 # endif
186 #endif
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\"")
201 # else
202 # define VIR_WARNINGS_NO_PRINTF \
203 _Pragma ("GCC diagnostic push")
204 # endif
206 /* Workaround bogus GCC 6.0 for logical 'or' equal expression warnings.
207 * (GCC bz 69602) */
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\"")
212 # else
213 # define VIR_WARNINGS_NO_WLOGICALOP_EQUAL_EXPR \
214 _Pragma ("GCC diagnostic push")
215 # endif
217 # define VIR_WARNINGS_RESET \
218 _Pragma ("GCC diagnostic pop")
219 #else
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
225 #endif
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\"")
232 #else
233 # define VIR_WARNINGS_NO_WLOGICALOP_STRCHR
234 #endif
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) : "-")
258 * SWAP:
260 * In place exchange of two values
262 #define SWAP(a, b) \
263 do { \
264 (a) = (a) ^ (b); \
265 (b) = (a) ^ (b); \
266 (a) = (a) ^ (b); \
267 } while (0)
270 * VIR_STEAL_PTR:
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) \
276 do { \
277 (a) = (b); \
278 (b) = NULL; \
279 } while (0)
282 * VIR_RETURN_PTR:
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) \
290 do { \
291 typeof(ptr) virTemporaryReturnPointer = (ptr); \
292 (ptr) = NULL; \
293 return virTemporaryReturnPointer; \
294 } while (0)
297 * virCheckFlags:
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
305 * passed to it.
307 #define virCheckFlags(supported, retval) \
308 do { \
309 unsigned long __unsuppflags = flags & ~(supported); \
310 if (__unsuppflags) { \
311 virReportInvalidArg(flags, \
312 _("unsupported flags (0x%lx) in function %s"), \
313 __unsuppflags, __FUNCTION__); \
314 return retval; \
316 } while (0)
319 * virCheckFlagsGoto:
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
327 * passed to it.
329 #define virCheckFlagsGoto(supported, label) \
330 do { \
331 unsigned long __unsuppflags = flags & ~(supported); \
332 if (__unsuppflags) { \
333 virReportInvalidArg(flags, \
334 _("unsupported flags (0x%lx) in function %s"), \
335 __unsuppflags, __FUNCTION__); \
336 goto label; \
338 } while (0)
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) \
356 do { \
357 if ((flags & FLAG1) && (flags & FLAG2)) { \
358 virReportInvalidArg(ctl, \
359 _("Flags '%s' and '%s' are mutually " \
360 "exclusive"), \
361 #FLAG1, #FLAG2); \
362 return RET; \
364 } while (0)
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
377 * passed to it.
379 #define VIR_EXCLUSIVE_FLAGS_GOTO(FLAG1, FLAG2, LABEL) \
380 do { \
381 if ((flags & FLAG1) && (flags & FLAG2)) { \
382 virReportInvalidArg(ctl, \
383 _("Flags '%s' and '%s' are mutually " \
384 "exclusive"), \
385 #FLAG1, #FLAG2); \
386 goto LABEL; \
388 } while (0)
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) \
406 do { \
407 if ((flags & FLAG1) && !(flags & FLAG2)) { \
408 virReportInvalidArg(ctl, \
409 _("Flag '%s' is required by flag '%s'"), \
410 #FLAG2, #FLAG1); \
411 return RET; \
413 } while (0)
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) \
428 do { \
429 if ((flags & FLAG1) && !(flags & FLAG2)) { \
430 virReportInvalidArg(ctl, \
431 _("Flag '%s' is required by flag '%s'"), \
432 #FLAG2, #FLAG1); \
433 goto LABEL; \
435 } while (0)
437 #define virCheckNonNullArgReturn(argname, retval) \
438 do { \
439 if (argname == NULL) { \
440 virReportInvalidNonNullArg(argname); \
441 return retval; \
443 } while (0)
444 #define virCheckNullArgGoto(argname, label) \
445 do { \
446 if (argname != NULL) { \
447 virReportInvalidNullArg(argname); \
448 goto label; \
450 } while (0)
451 #define virCheckNonNullArgGoto(argname, label) \
452 do { \
453 if (argname == NULL) { \
454 virReportInvalidNonNullArg(argname); \
455 goto label; \
457 } while (0)
458 #define virCheckNonEmptyStringArgGoto(argname, label) \
459 do { \
460 if (argname == NULL) { \
461 virReportInvalidNonNullArg(argname); \
462 goto label; \
464 if (*argname == '\0') { \
465 virReportInvalidEmptyStringArg(argname); \
466 goto label; \
468 } while (0)
469 #define virCheckPositiveArgGoto(argname, label) \
470 do { \
471 if (argname <= 0) { \
472 virReportInvalidPositiveArg(argname); \
473 goto label; \
475 } while (0)
476 #define virCheckPositiveArgReturn(argname, retval) \
477 do { \
478 if (argname <= 0) { \
479 virReportInvalidPositiveArg(argname); \
480 return retval; \
482 } while (0)
483 #define virCheckNonZeroArgGoto(argname, label) \
484 do { \
485 if (argname == 0) { \
486 virReportInvalidNonZeroArg(argname); \
487 goto label; \
489 } while (0)
490 #define virCheckZeroArgGoto(argname, label) \
491 do { \
492 if (argname != 0) { \
493 virReportInvalidNonZeroArg(argname); \
494 goto label; \
496 } while (0)
497 #define virCheckNonNegativeArgGoto(argname, label) \
498 do { \
499 if (argname < 0) { \
500 virReportInvalidNonNegativeArg(argname); \
501 goto label; \
503 } while (0)
504 #define virCheckReadOnlyGoto(flags, label) \
505 do { \
506 if ((flags) & VIR_CONNECT_RO) { \
507 virReportRestrictedError(_("read only access prevents %s"), \
508 __FUNCTION__); \
509 goto label; \
511 } while (0)
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. */
530 enum {
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 */
536 #ifndef ENODATA
537 # define ENODATA EIO
538 #endif