]> git.proxmox.com Git - mirror_lxcfs.git/blob - src/macro.h
tree-wide: set _GNU_SOURCE in meson.build
[mirror_lxcfs.git] / src / macro.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXCFS_MACRO_H
4 #define __LXCFS_MACRO_H
5
6 #include "config.h"
7
8 #include <stdio.h>
9
10 #define BATCH_SIZE 50
11
12 /* filesystem magic values */
13 #ifndef CGROUP_SUPER_MAGIC
14 #define CGROUP_SUPER_MAGIC 0x27e0eb
15 #endif
16
17 #ifndef CGROUP2_SUPER_MAGIC
18 #define CGROUP2_SUPER_MAGIC 0x63677270
19 #endif
20
21 #define lxcfs_debug_stream(stream, format, ...) \
22 do { \
23 fprintf(stream, "%s: %d: %s: " format "\n", __FILE__, \
24 __LINE__, __func__, ##__VA_ARGS__); \
25 } while (false)
26
27 #define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__)
28
29 #ifdef DEBUG
30 #define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__)
31 #else
32 #define lxcfs_debug(format, ...) \
33 do { \
34 } while (false)
35 #endif /* DEBUG */
36
37 #ifdef VERBOSE
38 #define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__);
39 #else
40 #define lxcfs_v(format, ...)
41 #endif /* VERBOSE */
42
43 #define lxcfs_info(format, ...) \
44 do { \
45 fprintf(stderr, format "\n", ##__VA_ARGS__); \
46 } while (false)
47
48 #define log_error_errno(__ret__, __errno__, format, ...) \
49 ({ \
50 errno = __errno__; \
51 lxcfs_error(format, ##__VA_ARGS__); \
52 __ret__; \
53 })
54
55 #define log_error(__ret__, format, ...) \
56 ({ \
57 lxcfs_error(format, ##__VA_ARGS__); \
58 __ret__; \
59 })
60
61 #define STRLITERALLEN(x) (sizeof(""x"") - 1)
62
63 /* Calculate the number of chars needed to represent a given integer as a C
64 * string. Include room for '-' to indicate negative numbers and the \0 byte.
65 * This is based on systemd.
66 */
67 #define INTTYPE_TO_STRLEN(type) \
68 (2 + (sizeof(type) <= 1 \
69 ? 3 \
70 : sizeof(type) <= 2 \
71 ? 5 \
72 : sizeof(type) <= 4 \
73 ? 10 \
74 : sizeof(type) <= 8 \
75 ? 20 \
76 : sizeof(int[-2 * (sizeof(type) > 8)])))
77
78 #define move_ptr(ptr) \
79 ({ \
80 __typeof__(ptr) __internal_ptr__ = (ptr); \
81 (ptr) = NULL; \
82 __internal_ptr__; \
83 })
84
85 #define move_fd(fd) \
86 ({ \
87 int __internal_fd__ = (fd); \
88 (fd) = -EBADF; \
89 __internal_fd__; \
90 })
91
92 #define ret_errno(__errno__) \
93 ({ \
94 errno = __errno__; \
95 -__errno__; \
96 })
97
98 #define ret_set_errno(__ret__, __errno__) \
99 ({ \
100 errno = __errno__; \
101 __ret__; \
102 })
103
104 #define lxc_iterate_parts(__iterator, __splitme, __separators) \
105 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
106 (__iterator = __it); \
107 __iterator = __it = strtok_r(NULL, __separators, &__p))
108
109 #define log_exit(format, ...) \
110 ({ \
111 fprintf(stderr, format, ##__VA_ARGS__); \
112 _exit(EXIT_FAILURE); \
113 })
114
115 #ifdef DEBUG
116 #define log_debug(__ret__, format, ...) \
117 ({ \
118 lxcfs_debug_stream(stderr, format, ##__VA_ARGS__); \
119 __ret__; \
120 })
121 #else
122 #define log_debug(__ret__, format, ...) ({ __ret__; })
123 #endif
124
125 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
126 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
127 #define PTR_TO_UINT64(p) ((uint64_t)((intptr_t)(p)))
128 #define INTTYPE_TO_PTR(u) ((void *)((intptr_t)(u)))
129
130 #define __visible __attribute__((visibility("default")))
131
132 #define __lxcfs_fuse_ops
133
134 #ifndef __returns_twice
135 #define __returns_twice __attribute__((returns_twice))
136 #endif
137
138 #define STRINGIFY(a) __STRINGIFY(a)
139 #define __STRINGIFY(a) #a
140
141 #endif /* __LXCFS_MACRO_H */