]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
02fc31e69e380bea2eb5028add1485a7e697a6f8
[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 "time-util.h"
29
30 static inline const char* yes_no(bool b) {
31 return b ? "yes" : "no";
32 }
33
34 static inline const char* true_false(bool b) {
35 return b ? "true" : "false";
36 }
37
38 static inline const char* one_zero(bool b) {
39 return b ? "1" : "0";
40 }
41
42 static inline const char* enable_disable(bool b) {
43 return b ? "enable" : "disable";
44 }
45
46 bool plymouth_running(void);
47
48 bool display_is_local(const char *display) _pure_;
49
50 #define NULSTR_FOREACH(i, l) \
51 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
52
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))
55
56 extern int saved_argc;
57 extern char **saved_argv;
58
59 bool kexec_loaded(void);
60
61 int prot_from_flags(int flags) _const_;
62
63 bool in_initrd(void);
64 void in_initrd_force(bool value);
65
66 int on_ac_power(void);
67
68 static inline void _reset_errno_(int *saved_errno) {
69 if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
70 return;
71
72 errno = *saved_errno;
73 }
74
75 #define PROTECT_ERRNO \
76 _cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
77
78 #define UNPROTECT_ERRNO \
79 do { \
80 errno = _saved_errno_; \
81 _saved_errno_ = -1; \
82 } while (false)
83
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);
90 return -errno;
91 }
92
93 static inline unsigned u64log2(uint64_t n) {
94 #if __SIZEOF_LONG_LONG__ == 8
95 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
96 #else
97 #error "Wut?"
98 #endif
99 }
100
101 static inline unsigned u32ctz(uint32_t n) {
102 #if __SIZEOF_INT__ == 4
103 return n != 0 ? __builtin_ctz(n) : 32;
104 #else
105 #error "Wut?"
106 #endif
107 }
108
109 static inline unsigned log2i(int x) {
110 assert(x > 0);
111
112 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
113 }
114
115 static inline unsigned log2u(unsigned x) {
116 assert(x > 0);
117
118 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
119 }
120
121 static inline unsigned log2u_round_up(unsigned x) {
122 assert(x > 0);
123
124 if (x == 1)
125 return 0;
126
127 return log2u(x - 1) + 1;
128 }
129
130 int container_get_leader(const char *machine, pid_t *pid);
131
132 int version(void);
133
134 int str_verscmp(const char *s1, const char *s2);
135
136 void disable_coredumps(void);