]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
tree-wide: use __ prefixed gcc attributes (#10843)
[thirdparty/systemd.git] / src / basic / util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <alloca.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <inttypes.h>
8 #include <limits.h>
9 #include <locale.h>
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/inotify.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/statfs.h>
21 #include <sys/sysmacros.h>
22 #include <sys/types.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "format-util.h"
27 #include "macro.h"
28 #include "missing.h"
29 #include "time-util.h"
30
31 size_t page_size(void) _pure_;
32 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
33
34 static inline const char* yes_no(bool b) {
35 return b ? "yes" : "no";
36 }
37
38 static inline const char* true_false(bool b) {
39 return b ? "true" : "false";
40 }
41
42 static inline const char* one_zero(bool b) {
43 return b ? "1" : "0";
44 }
45
46 static inline const char* enable_disable(bool b) {
47 return b ? "enable" : "disable";
48 }
49
50 bool plymouth_running(void);
51
52 bool display_is_local(const char *display) _pure_;
53
54 #define NULSTR_FOREACH(i, l) \
55 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
56
57 #define NULSTR_FOREACH_PAIR(i, j, l) \
58 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
59
60 extern int saved_argc;
61 extern char **saved_argv;
62
63 bool kexec_loaded(void);
64
65 int prot_from_flags(int flags) _const_;
66
67 bool in_initrd(void);
68 void in_initrd_force(bool value);
69
70 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
71 __compar_d_fn_t compar, void *arg);
72
73 #define typesafe_bsearch_r(k, b, n, func, userdata) \
74 ({ \
75 const typeof(b[0]) *_k = k; \
76 int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \
77 xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \
78 })
79
80 /**
81 * Normal bsearch requires base to be nonnull. Here were require
82 * that only if nmemb > 0.
83 */
84 static inline void* bsearch_safe(const void *key, const void *base,
85 size_t nmemb, size_t size, __compar_fn_t compar) {
86 if (nmemb <= 0)
87 return NULL;
88
89 assert(base);
90 return bsearch(key, base, nmemb, size, compar);
91 }
92
93 #define typesafe_bsearch(k, b, n, func) \
94 ({ \
95 const typeof(b[0]) *_k = k; \
96 int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \
97 bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \
98 })
99
100 /**
101 * Normal qsort requires base to be nonnull. Here were require
102 * that only if nmemb > 0.
103 */
104 static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) {
105 if (nmemb <= 1)
106 return;
107
108 assert(base);
109 qsort(base, nmemb, size, compar);
110 }
111
112 /* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
113 * is the prototype for the comparison function */
114 #define typesafe_qsort(p, n, func) \
115 ({ \
116 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
117 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
118 })
119
120 static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
121 if (nmemb <= 1)
122 return;
123
124 assert(base);
125 qsort_r(base, nmemb, size, compar, userdata);
126 }
127
128 #define typesafe_qsort_r(p, n, func, userdata) \
129 ({ \
130 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
131 qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
132 })
133
134 /* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */
135 static inline void memcpy_safe(void *dst, const void *src, size_t n) {
136 if (n == 0)
137 return;
138 assert(src);
139 memcpy(dst, src, n);
140 }
141
142 /* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */
143 static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
144 if (n == 0)
145 return 0;
146 assert(s1);
147 assert(s2);
148 return memcmp(s1, s2, n);
149 }
150
151 int on_ac_power(void);
152
153 #define memzero(x,l) \
154 ({ \
155 size_t _l_ = (l); \
156 void *_x_ = (x); \
157 _l_ == 0 ? _x_ : memset(_x_, 0, _l_); \
158 })
159
160 #define zero(x) (memzero(&(x), sizeof(x)))
161
162 bool memeqzero(const void *data, size_t length);
163
164 #define eqzero(x) memeqzero(x, sizeof(x))
165
166 static inline void *mempset(void *s, int c, size_t n) {
167 memset(s, c, n);
168 return (uint8_t*)s + n;
169 }
170
171 static inline void _reset_errno_(int *saved_errno) {
172 errno = *saved_errno;
173 }
174
175 #define PROTECT_ERRNO \
176 _cleanup_(_reset_errno_) __attribute__((__unused__)) int _saved_errno_ = errno
177
178 static inline int negative_errno(void) {
179 /* This helper should be used to shut up gcc if you know 'errno' is
180 * negative. Instead of "return -errno;", use "return negative_errno();"
181 * It will suppress bogus gcc warnings in case it assumes 'errno' might
182 * be 0 and thus the caller's error-handling might not be triggered. */
183 assert_return(errno > 0, -EINVAL);
184 return -errno;
185 }
186
187 static inline unsigned u64log2(uint64_t n) {
188 #if __SIZEOF_LONG_LONG__ == 8
189 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
190 #else
191 #error "Wut?"
192 #endif
193 }
194
195 static inline unsigned u32ctz(uint32_t n) {
196 #if __SIZEOF_INT__ == 4
197 return __builtin_ctz(n);
198 #else
199 #error "Wut?"
200 #endif
201 }
202
203 static inline unsigned log2i(int x) {
204 assert(x > 0);
205
206 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
207 }
208
209 static inline unsigned log2u(unsigned x) {
210 assert(x > 0);
211
212 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
213 }
214
215 static inline unsigned log2u_round_up(unsigned x) {
216 assert(x > 0);
217
218 if (x == 1)
219 return 0;
220
221 return log2u(x - 1) + 1;
222 }
223
224 int container_get_leader(const char *machine, pid_t *pid);
225
226 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
227 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
228
229 uint64_t physical_memory(void);
230 uint64_t physical_memory_scale(uint64_t v, uint64_t max);
231
232 uint64_t system_tasks_max(void);
233 uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
234
235 int version(void);
236
237 int str_verscmp(const char *s1, const char *s2);
238
239 void disable_coredumps(void);