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