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