]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
pam_systemd: move socket_from_display() from util.[ch] to pam_systemd.c
[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 int (*compar) (const void *, const void *, void *),
72 void *arg);
73
74 /**
75 * Normal bsearch requires base to be nonnull. Here were require
76 * that only if nmemb > 0.
77 */
78 static inline void* bsearch_safe(const void *key, const void *base,
79 size_t nmemb, size_t size, comparison_fn_t compar) {
80 if (nmemb <= 0)
81 return NULL;
82
83 assert(base);
84 return bsearch(key, base, nmemb, size, compar);
85 }
86
87 /**
88 * Normal qsort requires base to be nonnull. Here were require
89 * that only if nmemb > 0.
90 */
91 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
92 if (nmemb <= 1)
93 return;
94
95 assert(base);
96 qsort(base, nmemb, size, compar);
97 }
98
99 /* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
100 * is the prototype for the comparison function */
101 #define typesafe_qsort(p, n, func) \
102 ({ \
103 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
104 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
105 })
106
107 static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void *userdata) {
108 if (nmemb <= 1)
109 return;
110
111 assert(base);
112 qsort_r(base, nmemb, size, compar, userdata);
113 }
114
115 /**
116 * Normal memcpy requires src to be nonnull. We do nothing if n is 0.
117 */
118 static inline void memcpy_safe(void *dst, const void *src, size_t n) {
119 if (n == 0)
120 return;
121 assert(src);
122 memcpy(dst, src, n);
123 }
124
125 int on_ac_power(void);
126
127 #define memzero(x,l) (memset((x), 0, (l)))
128 #define zero(x) (memzero(&(x), sizeof(x)))
129
130 static inline void *mempset(void *s, int c, size_t n) {
131 memset(s, c, n);
132 return (uint8_t*)s + n;
133 }
134
135 static inline void _reset_errno_(int *saved_errno) {
136 errno = *saved_errno;
137 }
138
139 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
140
141 static inline int negative_errno(void) {
142 /* This helper should be used to shut up gcc if you know 'errno' is
143 * negative. Instead of "return -errno;", use "return negative_errno();"
144 * It will suppress bogus gcc warnings in case it assumes 'errno' might
145 * be 0 and thus the caller's error-handling might not be triggered. */
146 assert_return(errno > 0, -EINVAL);
147 return -errno;
148 }
149
150 static inline unsigned u64log2(uint64_t n) {
151 #if __SIZEOF_LONG_LONG__ == 8
152 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
153 #else
154 #error "Wut?"
155 #endif
156 }
157
158 static inline unsigned u32ctz(uint32_t n) {
159 #if __SIZEOF_INT__ == 4
160 return __builtin_ctz(n);
161 #else
162 #error "Wut?"
163 #endif
164 }
165
166 static inline unsigned log2i(int x) {
167 assert(x > 0);
168
169 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
170 }
171
172 static inline unsigned log2u(unsigned x) {
173 assert(x > 0);
174
175 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
176 }
177
178 static inline unsigned log2u_round_up(unsigned x) {
179 assert(x > 0);
180
181 if (x == 1)
182 return 0;
183
184 return log2u(x - 1) + 1;
185 }
186
187 int container_get_leader(const char *machine, pid_t *pid);
188
189 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
190 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
191
192 uint64_t physical_memory(void);
193 uint64_t physical_memory_scale(uint64_t v, uint64_t max);
194
195 uint64_t system_tasks_max(void);
196 uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
197
198 int version(void);
199
200 int str_verscmp(const char *s1, const char *s2);
201
202 void disable_coredumps(void);