]>
Commit | Line | Data |
---|---|---|
db0463bf | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1f5596dd | 2 | |
17e0e368 CB |
3 | #ifndef __LXCFS_MACRO_H |
4 | #define __LXCFS_MACRO_H | |
5 | ||
d511b24d | 6 | #include "config.h" |
1f5596dd | 7 | |
a3c8d33c | 8 | #include <stdbool.h> |
5fbea8a6 | 9 | #include <stdio.h> |
a3c8d33c | 10 | #include <linux/types.h> |
5fbea8a6 CB |
11 | |
12 | #define BATCH_SIZE 50 | |
13 | ||
14 | /* filesystem magic values */ | |
15 | #ifndef CGROUP_SUPER_MAGIC | |
16 | #define CGROUP_SUPER_MAGIC 0x27e0eb | |
17 | #endif | |
18 | ||
19 | #ifndef CGROUP2_SUPER_MAGIC | |
20 | #define CGROUP2_SUPER_MAGIC 0x63677270 | |
21 | #endif | |
22 | ||
081d7647 CB |
23 | #define lxcfs_debug_stream(stream, format, ...) \ |
24 | do { \ | |
25 | fprintf(stream, "%s: %d: %s: " format "\n", __FILE__, \ | |
26 | __LINE__, __func__, ##__VA_ARGS__); \ | |
17e0e368 CB |
27 | } while (false) |
28 | ||
5faa9af7 | 29 | #define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__) |
17e0e368 CB |
30 | |
31 | #ifdef DEBUG | |
5faa9af7 | 32 | #define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__) |
17e0e368 | 33 | #else |
081d7647 CB |
34 | #define lxcfs_debug(format, ...) \ |
35 | do { \ | |
36 | } while (false) | |
17e0e368 CB |
37 | #endif /* DEBUG */ |
38 | ||
83dad6a5 | 39 | #ifdef VERBOSE |
5faa9af7 | 40 | #define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__); |
83dad6a5 SG |
41 | #else |
42 | #define lxcfs_v(format, ...) | |
43 | #endif /* VERBOSE */ | |
44 | ||
cc42d0c7 CB |
45 | #define lxcfs_info(format, ...) \ |
46 | do { \ | |
47 | fprintf(stderr, format "\n", ##__VA_ARGS__); \ | |
48 | } while (false) | |
49 | ||
5fbea8a6 CB |
50 | #define log_error_errno(__ret__, __errno__, format, ...) \ |
51 | ({ \ | |
52 | errno = __errno__; \ | |
53 | lxcfs_error(format, ##__VA_ARGS__); \ | |
54 | __ret__; \ | |
55 | }) | |
56 | ||
971aa868 CB |
57 | #define log_error(__ret__, format, ...) \ |
58 | ({ \ | |
59 | lxcfs_error(format, ##__VA_ARGS__); \ | |
60 | __ret__; \ | |
61 | }) | |
62 | ||
5fbea8a6 CB |
63 | #define STRLITERALLEN(x) (sizeof(""x"") - 1) |
64 | ||
65 | /* Calculate the number of chars needed to represent a given integer as a C | |
66 | * string. Include room for '-' to indicate negative numbers and the \0 byte. | |
67 | * This is based on systemd. | |
68 | */ | |
69 | #define INTTYPE_TO_STRLEN(type) \ | |
70 | (2 + (sizeof(type) <= 1 \ | |
71 | ? 3 \ | |
72 | : sizeof(type) <= 2 \ | |
73 | ? 5 \ | |
74 | : sizeof(type) <= 4 \ | |
75 | ? 10 \ | |
76 | : sizeof(type) <= 8 \ | |
77 | ? 20 \ | |
78 | : sizeof(int[-2 * (sizeof(type) > 8)]))) | |
79 | ||
1ebba49a AM |
80 | #define strnprintf(buf, buf_size, ...) \ |
81 | ({ \ | |
82 | int __ret_strnprintf; \ | |
83 | __ret_strnprintf = snprintf(buf, buf_size, ##__VA_ARGS__); \ | |
84 | if (__ret_strnprintf < 0 || (size_t)__ret_strnprintf >= (size_t)buf_size) \ | |
85 | __ret_strnprintf = ret_errno(EIO); \ | |
86 | __ret_strnprintf; \ | |
87 | }) | |
88 | ||
05b7a16d CB |
89 | #define move_ptr(ptr) \ |
90 | ({ \ | |
0d64d474 | 91 | __typeof__(ptr) __internal_ptr__ = (ptr); \ |
05b7a16d CB |
92 | (ptr) = NULL; \ |
93 | __internal_ptr__; \ | |
94 | }) | |
95 | ||
96 | #define move_fd(fd) \ | |
97 | ({ \ | |
98 | int __internal_fd__ = (fd); \ | |
99 | (fd) = -EBADF; \ | |
100 | __internal_fd__; \ | |
101 | }) | |
102 | ||
5fbea8a6 CB |
103 | #define ret_errno(__errno__) \ |
104 | ({ \ | |
105 | errno = __errno__; \ | |
106 | -__errno__; \ | |
107 | }) | |
108 | ||
109 | #define ret_set_errno(__ret__, __errno__) \ | |
110 | ({ \ | |
111 | errno = __errno__; \ | |
112 | __ret__; \ | |
113 | }) | |
114 | ||
115 | #define lxc_iterate_parts(__iterator, __splitme, __separators) \ | |
116 | for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \ | |
117 | (__iterator = __it); \ | |
118 | __iterator = __it = strtok_r(NULL, __separators, &__p)) | |
119 | ||
2243c5a9 CB |
120 | #define log_exit(format, ...) \ |
121 | ({ \ | |
122 | fprintf(stderr, format, ##__VA_ARGS__); \ | |
4ec5c9da | 123 | _exit(EXIT_FAILURE); \ |
2243c5a9 CB |
124 | }) |
125 | ||
b456d40d | 126 | #ifdef DEBUG |
6ba365d9 CB |
127 | #define log_debug(__ret__, format, ...) \ |
128 | ({ \ | |
129 | lxcfs_debug_stream(stderr, format, ##__VA_ARGS__); \ | |
130 | __ret__; \ | |
b456d40d CB |
131 | }) |
132 | #else | |
133 | #define log_debug(__ret__, format, ...) ({ __ret__; }) | |
134 | #endif | |
135 | ||
f1744de4 CB |
136 | #define PTR_TO_INT(p) ((int)((intptr_t)(p))) |
137 | #define INT_TO_PTR(u) ((void *)((intptr_t)(u))) | |
fc743888 CB |
138 | #define PTR_TO_UINT64(p) ((uint64_t)((uintptr_t)(p))) |
139 | #define INTTYPE_TO_PTR(u) ((void *)((uintptr_t)(u))) | |
99b183fb | 140 | |
b0a2dc08 CB |
141 | #define __visible __attribute__((visibility("default"))) |
142 | ||
2d7bcab7 CB |
143 | #define __lxcfs_fuse_ops |
144 | ||
35acc247 CB |
145 | #ifndef __returns_twice |
146 | #define __returns_twice __attribute__((returns_twice)) | |
147 | #endif | |
148 | ||
10deeaaf | 149 | #define STRINGIFY(a) __STRINGIFY(a) |
150 | #define __STRINGIFY(a) #a | |
151 | ||
a3c8d33c CB |
152 | /* Taken over modified from the kernel sources. */ |
153 | #define NBITS 32 /* bits in uint32_t */ | |
154 | #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d)) | |
155 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS) | |
156 | ||
157 | static inline void set_bit(__u32 bit, __u32 *bitarr) | |
158 | { | |
159 | bitarr[bit / NBITS] |= ((__u32)1 << (bit % NBITS)); | |
160 | } | |
161 | ||
162 | static inline void clear_bit(__u32 bit, __u32 *bitarr) | |
163 | { | |
164 | bitarr[bit / NBITS] &= ~((__u32)1 << (bit % NBITS)); | |
165 | } | |
166 | ||
167 | static inline bool is_set(__u32 bit, __u32 *bitarr) | |
168 | { | |
169 | return (bitarr[bit / NBITS] & ((__u32)1 << (bit % NBITS))) != 0; | |
170 | } | |
171 | ||
17e0e368 | 172 | #endif /* __LXCFS_MACRO_H */ |