]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
17 #include <sys/inotify.h>
18 #include <sys/socket.h>
20 #include <sys/statfs.h>
21 #include <sys/sysmacros.h>
22 #include <sys/types.h>
26 #include "format-util.h"
29 #include "time-util.h"
31 size_t page_size(void) _pure_
;
32 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
34 static inline const char* yes_no(bool b
) {
35 return b
? "yes" : "no";
38 static inline const char* true_false(bool b
) {
39 return b
? "true" : "false";
42 static inline const char* one_zero(bool b
) {
46 static inline const char* enable_disable(bool b
) {
47 return b
? "enable" : "disable";
50 bool plymouth_running(void);
52 bool display_is_local(const char *display
) _pure_
;
53 int socket_from_display(const char *display
, char **path
);
55 #define NULSTR_FOREACH(i, l) \
56 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
58 #define NULSTR_FOREACH_PAIR(i, j, l) \
59 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
61 extern int saved_argc
;
62 extern char **saved_argv
;
64 bool kexec_loaded(void);
66 int prot_from_flags(int flags
) _const_
;
69 void in_initrd_force(bool value
);
71 void *xbsearch_r(const void *key
, const void *base
, size_t nmemb
, size_t size
,
72 int (*compar
) (const void *, const void *, void *),
76 * Normal bsearch requires base to be nonnull. Here were require
77 * that only if nmemb > 0.
79 static inline void* bsearch_safe(const void *key
, const void *base
,
80 size_t nmemb
, size_t size
, comparison_fn_t compar
) {
85 return bsearch(key
, base
, nmemb
, size
, compar
);
89 * Normal qsort requires base to be nonnull. Here were require
90 * that only if nmemb > 0.
92 static inline void qsort_safe(void *base
, size_t nmemb
, size_t size
, comparison_fn_t compar
) {
97 qsort(base
, nmemb
, size
, compar
);
100 /* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
101 * is the prototype for the comparison function */
102 #define typesafe_qsort(p, n, func) \
104 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
105 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
108 static inline void qsort_r_safe(void *base
, size_t nmemb
, size_t size
, int (*compar
)(const void*, const void*, void*), void *userdata
) {
113 qsort_r(base
, nmemb
, size
, compar
, userdata
);
117 * Normal memcpy requires src to be nonnull. We do nothing if n is 0.
119 static inline void memcpy_safe(void *dst
, const void *src
, size_t n
) {
126 int on_ac_power(void);
128 #define memzero(x,l) (memset((x), 0, (l)))
129 #define zero(x) (memzero(&(x), sizeof(x)))
131 static inline void *mempset(void *s
, int c
, size_t n
) {
133 return (uint8_t*)s
+ n
;
136 static inline void _reset_errno_(int *saved_errno
) {
137 errno
= *saved_errno
;
140 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
142 static inline int negative_errno(void) {
143 /* This helper should be used to shut up gcc if you know 'errno' is
144 * negative. Instead of "return -errno;", use "return negative_errno();"
145 * It will suppress bogus gcc warnings in case it assumes 'errno' might
146 * be 0 and thus the caller's error-handling might not be triggered. */
147 assert_return(errno
> 0, -EINVAL
);
151 static inline unsigned u64log2(uint64_t n
) {
152 #if __SIZEOF_LONG_LONG__ == 8
153 return (n
> 1) ? (unsigned) __builtin_clzll(n
) ^ 63U : 0;
159 static inline unsigned u32ctz(uint32_t n
) {
160 #if __SIZEOF_INT__ == 4
161 return __builtin_ctz(n
);
167 static inline unsigned log2i(int x
) {
170 return __SIZEOF_INT__
* 8 - __builtin_clz(x
) - 1;
173 static inline unsigned log2u(unsigned x
) {
176 return sizeof(unsigned) * 8 - __builtin_clz(x
) - 1;
179 static inline unsigned log2u_round_up(unsigned x
) {
185 return log2u(x
- 1) + 1;
188 int container_get_leader(const char *machine
, pid_t
*pid
);
190 int namespace_open(pid_t pid
, int *pidns_fd
, int *mntns_fd
, int *netns_fd
, int *userns_fd
, int *root_fd
);
191 int namespace_enter(int pidns_fd
, int mntns_fd
, int netns_fd
, int userns_fd
, int root_fd
);
193 uint64_t physical_memory(void);
194 uint64_t physical_memory_scale(uint64_t v
, uint64_t max
);
196 uint64_t system_tasks_max(void);
197 uint64_t system_tasks_max_scale(uint64_t v
, uint64_t max
);
201 int str_verscmp(const char *s1
, const char *s2
);
203 void disable_coredumps(void);