chiark / gitweb /
cgroup2: use new fstype for unified hierarchy
[elogind.git] / src / basic / util.h
1 #pragma once
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <alloca.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <stdarg.h>
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/inotify.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/statfs.h>
39 #include <sys/types.h>
40 #include <time.h>
41 #include <unistd.h>
42
43 #include "formats-util.h"
44 #include "macro.h"
45 #include "missing.h"
46 #include "time-util.h"
47
48 size_t page_size(void) _pure_;
49 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
50
51 static inline const char* yes_no(bool b) {
52         return b ? "yes" : "no";
53 }
54
55 static inline const char* true_false(bool b) {
56         return b ? "true" : "false";
57 }
58
59 static inline const char* one_zero(bool b) {
60         return b ? "1" : "0";
61 }
62
63 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
64
65 #if 0 /// UNNEEDED by elogind
66 bool plymouth_running(void);
67 #endif // 0
68
69 bool display_is_local(const char *display) _pure_;
70 int socket_from_display(const char *display, char **path);
71
72 #if 0 /// UNNEEDED by elogind
73 int block_get_whole_disk(dev_t d, dev_t *ret);
74 #endif // 0
75
76 #define NULSTR_FOREACH(i, l)                                    \
77         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
78
79 #define NULSTR_FOREACH_PAIR(i, j, l)                             \
80         for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
81
82 extern int saved_argc;
83 extern char **saved_argv;
84
85 #if 0 /// UNNEEDED by elogind
86 bool kexec_loaded(void);
87
88 int prot_from_flags(int flags) _const_;
89 #endif // 0
90
91 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
92
93 bool in_initrd(void);
94
95 #if 0 /// UNNEEDED by elogind
96 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
97                  int (*compar) (const void *, const void *, void *),
98                  void *arg);
99 #endif // 0
100
101 /**
102  * Normal qsort requires base to be nonnull. Here were require
103  * that only if nmemb > 0.
104  */
105 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
106         if (nmemb <= 1)
107                 return;
108
109         assert(base);
110         qsort(base, nmemb, size, compar);
111 }
112
113 #if 0 /// UNNEEDED by elogind
114 int on_ac_power(void);
115 #endif // 0
116
117 #define memzero(x,l) (memset((x), 0, (l)))
118 #define zero(x) (memzero(&(x), sizeof(x)))
119
120 static inline void *mempset(void *s, int c, size_t n) {
121         memset(s, c, n);
122         return (uint8_t*)s + n;
123 }
124
125 static inline void _reset_errno_(int *saved_errno) {
126         errno = *saved_errno;
127 }
128
129 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
130
131 #if 0 /// UNNEEDED by elogind
132 static inline int negative_errno(void) {
133         /* This helper should be used to shut up gcc if you know 'errno' is
134          * negative. Instead of "return -errno;", use "return negative_errno();"
135          * It will suppress bogus gcc warnings in case it assumes 'errno' might
136          * be 0 and thus the caller's error-handling might not be triggered. */
137         assert_return(errno > 0, -EINVAL);
138         return -errno;
139 }
140 #endif // 0
141
142 static inline unsigned u64log2(uint64_t n) {
143 #if __SIZEOF_LONG_LONG__ == 8
144         return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
145 #else
146 #error "Wut?"
147 #endif
148 }
149
150 static inline unsigned u32ctz(uint32_t n) {
151 #if __SIZEOF_INT__ == 4
152         return __builtin_ctz(n);
153 #else
154 #error "Wut?"
155 #endif
156 }
157
158 static inline unsigned log2i(int x) {
159         assert(x > 0);
160
161         return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
162 }
163
164 static inline unsigned log2u(unsigned x) {
165         assert(x > 0);
166
167         return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
168 }
169
170 static inline unsigned log2u_round_up(unsigned x) {
171         assert(x > 0);
172
173         if (x == 1)
174                 return 0;
175
176         return log2u(x - 1) + 1;
177 }
178
179 #if 0 /// UNNEEDED by elogind
180 bool id128_is_valid(const char *s) _pure_;
181 #endif // 0
182
183 int container_get_leader(const char *machine, pid_t *pid);
184
185 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
186 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
187
188 uint64_t physical_memory(void);
189
190 #if 0 /// UNNEEDED by elogind
191 int update_reboot_param_file(const char *param);
192 #endif // 0
193
194 int version(void);