]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
02fc31e69e380bea2eb5028add1485a7e697a6f8
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"
28 #include "time-util.h"
30 static inline const char* yes_no(bool b
) {
31 return b
? "yes" : "no";
34 static inline const char* true_false(bool b
) {
35 return b
? "true" : "false";
38 static inline const char* one_zero(bool b
) {
42 static inline const char* enable_disable(bool b
) {
43 return b
? "enable" : "disable";
46 bool plymouth_running(void);
48 bool display_is_local(const char *display
) _pure_
;
50 #define NULSTR_FOREACH(i, l) \
51 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
53 #define NULSTR_FOREACH_PAIR(i, j, l) \
54 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
56 extern int saved_argc
;
57 extern char **saved_argv
;
59 bool kexec_loaded(void);
61 int prot_from_flags(int flags
) _const_
;
64 void in_initrd_force(bool value
);
66 int on_ac_power(void);
68 static inline void _reset_errno_(int *saved_errno
) {
69 if (*saved_errno
< 0) /* Invalidated by UNPROTECT_ERRNO? */
75 #define PROTECT_ERRNO \
76 _cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
78 #define UNPROTECT_ERRNO \
80 errno = _saved_errno_; \
84 static inline int negative_errno(void) {
85 /* This helper should be used to shut up gcc if you know 'errno' is
86 * negative. Instead of "return -errno;", use "return negative_errno();"
87 * It will suppress bogus gcc warnings in case it assumes 'errno' might
88 * be 0 and thus the caller's error-handling might not be triggered. */
89 assert_return(errno
> 0, -EINVAL
);
93 static inline unsigned u64log2(uint64_t n
) {
94 #if __SIZEOF_LONG_LONG__ == 8
95 return (n
> 1) ? (unsigned) __builtin_clzll(n
) ^ 63U : 0;
101 static inline unsigned u32ctz(uint32_t n
) {
102 #if __SIZEOF_INT__ == 4
103 return n
!= 0 ? __builtin_ctz(n
) : 32;
109 static inline unsigned log2i(int x
) {
112 return __SIZEOF_INT__
* 8 - __builtin_clz(x
) - 1;
115 static inline unsigned log2u(unsigned x
) {
118 return sizeof(unsigned) * 8 - __builtin_clz(x
) - 1;
121 static inline unsigned log2u_round_up(unsigned x
) {
127 return log2u(x
- 1) + 1;
130 int container_get_leader(const char *machine
, pid_t
*pid
);
134 int str_verscmp(const char *s1
, const char *s2
);
136 void disable_coredumps(void);