]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
util-lib: split out printf() helpers to stdio-util.h
[thirdparty/systemd.git] / src / basic / util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <alloca.h>
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/inotify.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/statfs.h>
38 #include <sys/types.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "formats-util.h"
43 #include "macro.h"
44 #include "missing.h"
45 #include "time-util.h"
46
47 /* What is interpreted as whitespace? */
48 #define WHITESPACE " \t\n\r"
49 #define NEWLINE "\n\r"
50 #define QUOTES "\"\'"
51 #define COMMENTS "#;"
52 #define GLOB_CHARS "*?["
53
54 size_t page_size(void) _pure_;
55 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
56
57 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
58
59 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
60
61 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
62
63 #define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
64
65 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
66
67 #define malloc0(n) (calloc(1, (n)))
68
69 static inline void *mfree(void *memory) {
70 free(memory);
71 return NULL;
72 }
73
74 static inline const char* yes_no(bool b) {
75 return b ? "yes" : "no";
76 }
77
78 static inline const char* true_false(bool b) {
79 return b ? "true" : "false";
80 }
81
82 static inline const char* one_zero(bool b) {
83 return b ? "1" : "0";
84 }
85
86 bool fstype_is_network(const char *fstype);
87
88 noreturn void freeze(void);
89
90 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
91
92 bool plymouth_running(void);
93
94 bool display_is_local(const char *display) _pure_;
95 int socket_from_display(const char *display, char **path);
96
97 int glob_exists(const char *path);
98 int glob_extend(char ***strv, const char *path);
99
100 int block_get_whole_disk(dev_t d, dev_t *ret);
101
102 #define NULSTR_FOREACH(i, l) \
103 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
104
105 #define NULSTR_FOREACH_PAIR(i, j, l) \
106 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
107
108 int ioprio_class_to_string_alloc(int i, char **s);
109 int ioprio_class_from_string(const char *s);
110
111 const char *sigchld_code_to_string(int i) _const_;
112 int sigchld_code_from_string(const char *s) _pure_;
113
114 int sched_policy_to_string_alloc(int i, char **s);
115 int sched_policy_from_string(const char *s);
116
117 extern int saved_argc;
118 extern char **saved_argv;
119
120 bool kexec_loaded(void);
121
122 int prot_from_flags(int flags) _const_;
123
124 void* memdup(const void *p, size_t l) _alloc_(2);
125
126 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
127
128 bool in_initrd(void);
129
130 static inline void freep(void *p) {
131 free(*(void**) p);
132 }
133
134 #define _cleanup_free_ _cleanup_(freep)
135 #define _cleanup_globfree_ _cleanup_(globfree)
136
137 _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
138 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
139 return NULL;
140
141 return malloc(a * b);
142 }
143
144 _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
145 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
146 return NULL;
147
148 return realloc(p, a * b);
149 }
150
151 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
152 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
153 return NULL;
154
155 return memdup(p, a * b);
156 }
157
158 /**
159 * Check if a string contains any glob patterns.
160 */
161 _pure_ static inline bool string_is_glob(const char *p) {
162 return !!strpbrk(p, GLOB_CHARS);
163 }
164
165 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
166 int (*compar) (const void *, const void *, void *),
167 void *arg);
168
169 int on_ac_power(void);
170
171 static inline void *mempset(void *s, int c, size_t n) {
172 memset(s, c, n);
173 return (uint8_t*)s + n;
174 }
175
176 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
177 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
178 #define GREEDY_REALLOC(array, allocated, need) \
179 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
180
181 #define GREEDY_REALLOC0(array, allocated, need) \
182 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
183
184 static inline void _reset_errno_(int *saved_errno) {
185 errno = *saved_errno;
186 }
187
188 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
189
190 static inline int negative_errno(void) {
191 /* This helper should be used to shut up gcc if you know 'errno' is
192 * negative. Instead of "return -errno;", use "return negative_errno();"
193 * It will suppress bogus gcc warnings in case it assumes 'errno' might
194 * be 0 and thus the caller's error-handling might not be triggered. */
195 assert_return(errno > 0, -EINVAL);
196 return -errno;
197 }
198
199 static inline unsigned u64log2(uint64_t n) {
200 #if __SIZEOF_LONG_LONG__ == 8
201 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
202 #else
203 #error "Wut?"
204 #endif
205 }
206
207 static inline unsigned u32ctz(uint32_t n) {
208 #if __SIZEOF_INT__ == 4
209 return __builtin_ctz(n);
210 #else
211 #error "Wut?"
212 #endif
213 }
214
215 static inline unsigned log2i(int x) {
216 assert(x > 0);
217
218 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
219 }
220
221 static inline unsigned log2u(unsigned x) {
222 assert(x > 0);
223
224 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
225 }
226
227 static inline unsigned log2u_round_up(unsigned x) {
228 assert(x > 0);
229
230 if (x == 1)
231 return 0;
232
233 return log2u(x - 1) + 1;
234 }
235
236 #define alloca0(n) \
237 ({ \
238 char *_new_; \
239 size_t _len_ = n; \
240 _new_ = alloca(_len_); \
241 (void *) memset(_new_, 0, _len_); \
242 })
243
244 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
245 #define alloca_align(size, align) \
246 ({ \
247 void *_ptr_; \
248 size_t _mask_ = (align) - 1; \
249 _ptr_ = alloca((size) + _mask_); \
250 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
251 })
252
253 #define alloca0_align(size, align) \
254 ({ \
255 void *_new_; \
256 size_t _size_ = (size); \
257 _new_ = alloca_align(_size_, (align)); \
258 (void*)memset(_new_, 0, _size_); \
259 })
260
261 bool id128_is_valid(const char *s) _pure_;
262
263 /**
264 * Normal qsort requires base to be nonnull. Here were require
265 * that only if nmemb > 0.
266 */
267 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
268 if (nmemb <= 1)
269 return;
270
271 assert(base);
272 qsort(base, nmemb, size, compar);
273 }
274
275 int container_get_leader(const char *machine, pid_t *pid);
276
277 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
278 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
279
280 #ifndef PERSONALITY_INVALID
281 /* personality(7) documents that 0xffffffffUL is used for querying the
282 * current personality, hence let's use that here as error
283 * indicator. */
284 #define PERSONALITY_INVALID 0xffffffffLU
285 #endif
286
287 unsigned long personality_from_string(const char *p);
288 const char *personality_to_string(unsigned long);
289
290 uint64_t physical_memory(void);
291
292 union file_handle_union {
293 struct file_handle handle;
294 char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
295 };
296 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
297
298 int update_reboot_param_file(const char *param);
299
300 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
301
302 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
303 for ((e) = &buffer.ev; \
304 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
305 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
306
307 union inotify_event_buffer {
308 struct inotify_event ev;
309 uint8_t raw[INOTIFY_EVENT_MAX];
310 };
311
312 int version(void);
313
314 bool fdname_is_valid(const char *s);
315
316 bool oom_score_adjust_is_valid(int oa);