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