]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
util-lib: move string table stuff into its own string-table.[ch]
[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 #define xsprintf(buf, fmt, ...) \
89 assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
90 "xsprintf: " #buf "[] must be big enough")
91
92 int running_in_chroot(void);
93
94 noreturn void freeze(void);
95
96 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
97
98 bool plymouth_running(void);
99
100 bool display_is_local(const char *display) _pure_;
101 int socket_from_display(const char *display, char **path);
102
103 int glob_exists(const char *path);
104 int glob_extend(char ***strv, const char *path);
105
106 bool is_main_thread(void);
107
108 int block_get_whole_disk(dev_t d, dev_t *ret);
109
110 #define NULSTR_FOREACH(i, l) \
111 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
112
113 #define NULSTR_FOREACH_PAIR(i, j, l) \
114 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
115
116 int ioprio_class_to_string_alloc(int i, char **s);
117 int ioprio_class_from_string(const char *s);
118
119 const char *sigchld_code_to_string(int i) _const_;
120 int sigchld_code_from_string(const char *s) _pure_;
121
122 int log_facility_unshifted_to_string_alloc(int i, char **s);
123 int log_facility_unshifted_from_string(const char *s);
124 bool log_facility_unshifted_is_valid(int faciliy);
125
126 int log_level_to_string_alloc(int i, char **s);
127 int log_level_from_string(const char *s);
128 bool log_level_is_valid(int level);
129
130 int sched_policy_to_string_alloc(int i, char **s);
131 int sched_policy_from_string(const char *s);
132
133 extern int saved_argc;
134 extern char **saved_argv;
135
136 bool kexec_loaded(void);
137
138 int prot_from_flags(int flags) _const_;
139
140 void* memdup(const void *p, size_t l) _alloc_(2);
141
142 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
143
144 bool http_url_is_valid(const char *url) _pure_;
145 bool documentation_url_is_valid(const char *url) _pure_;
146
147 bool http_etag_is_valid(const char *etag);
148
149 bool in_initrd(void);
150
151 static inline void freep(void *p) {
152 free(*(void**) p);
153 }
154
155 static inline void umaskp(mode_t *u) {
156 umask(*u);
157 }
158
159 #define _cleanup_free_ _cleanup_(freep)
160 #define _cleanup_umask_ _cleanup_(umaskp)
161 #define _cleanup_globfree_ _cleanup_(globfree)
162
163 _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
164 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
165 return NULL;
166
167 return malloc(a * b);
168 }
169
170 _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
171 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
172 return NULL;
173
174 return realloc(p, a * b);
175 }
176
177 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
178 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
179 return NULL;
180
181 return memdup(p, a * b);
182 }
183
184 /**
185 * Check if a string contains any glob patterns.
186 */
187 _pure_ static inline bool string_is_glob(const char *p) {
188 return !!strpbrk(p, GLOB_CHARS);
189 }
190
191 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
192 int (*compar) (const void *, const void *, void *),
193 void *arg);
194
195 #define _(String) gettext (String)
196 #define N_(String) String
197 void init_gettext(void);
198 bool is_locale_utf8(void);
199
200 typedef enum DrawSpecialChar {
201 DRAW_TREE_VERTICAL,
202 DRAW_TREE_BRANCH,
203 DRAW_TREE_RIGHT,
204 DRAW_TREE_SPACE,
205 DRAW_TRIANGULAR_BULLET,
206 DRAW_BLACK_CIRCLE,
207 DRAW_ARROW,
208 DRAW_DASH,
209 _DRAW_SPECIAL_CHAR_MAX
210 } DrawSpecialChar;
211
212 const char *draw_special_char(DrawSpecialChar ch);
213
214 int on_ac_power(void);
215
216 static inline void *mempset(void *s, int c, size_t n) {
217 memset(s, c, n);
218 return (uint8_t*)s + n;
219 }
220
221 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
222 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
223 #define GREEDY_REALLOC(array, allocated, need) \
224 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
225
226 #define GREEDY_REALLOC0(array, allocated, need) \
227 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
228
229 static inline void _reset_errno_(int *saved_errno) {
230 errno = *saved_errno;
231 }
232
233 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
234
235 static inline int negative_errno(void) {
236 /* This helper should be used to shut up gcc if you know 'errno' is
237 * negative. Instead of "return -errno;", use "return negative_errno();"
238 * It will suppress bogus gcc warnings in case it assumes 'errno' might
239 * be 0 and thus the caller's error-handling might not be triggered. */
240 assert_return(errno > 0, -EINVAL);
241 return -errno;
242 }
243
244 struct _umask_struct_ {
245 mode_t mask;
246 bool quit;
247 };
248
249 static inline void _reset_umask_(struct _umask_struct_ *s) {
250 umask(s->mask);
251 };
252
253 #define RUN_WITH_UMASK(mask) \
254 for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
255 !_saved_umask_.quit ; \
256 _saved_umask_.quit = true)
257
258 static inline unsigned u64log2(uint64_t n) {
259 #if __SIZEOF_LONG_LONG__ == 8
260 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
261 #else
262 #error "Wut?"
263 #endif
264 }
265
266 static inline unsigned u32ctz(uint32_t n) {
267 #if __SIZEOF_INT__ == 4
268 return __builtin_ctz(n);
269 #else
270 #error "Wut?"
271 #endif
272 }
273
274 static inline unsigned log2i(int x) {
275 assert(x > 0);
276
277 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
278 }
279
280 static inline unsigned log2u(unsigned x) {
281 assert(x > 0);
282
283 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
284 }
285
286 static inline unsigned log2u_round_up(unsigned x) {
287 assert(x > 0);
288
289 if (x == 1)
290 return 0;
291
292 return log2u(x - 1) + 1;
293 }
294
295 #define DECIMAL_STR_WIDTH(x) \
296 ({ \
297 typeof(x) _x_ = (x); \
298 unsigned ans = 1; \
299 while (_x_ /= 10) \
300 ans++; \
301 ans; \
302 })
303
304 #define alloca0(n) \
305 ({ \
306 char *_new_; \
307 size_t _len_ = n; \
308 _new_ = alloca(_len_); \
309 (void *) memset(_new_, 0, _len_); \
310 })
311
312 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
313 #define alloca_align(size, align) \
314 ({ \
315 void *_ptr_; \
316 size_t _mask_ = (align) - 1; \
317 _ptr_ = alloca((size) + _mask_); \
318 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
319 })
320
321 #define alloca0_align(size, align) \
322 ({ \
323 void *_new_; \
324 size_t _size_ = (size); \
325 _new_ = alloca_align(_size_, (align)); \
326 (void*)memset(_new_, 0, _size_); \
327 })
328
329 bool id128_is_valid(const char *s) _pure_;
330
331 int shall_restore_state(void);
332
333 /**
334 * Normal qsort requires base to be nonnull. Here were require
335 * that only if nmemb > 0.
336 */
337 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
338 if (nmemb <= 1)
339 return;
340
341 assert(base);
342 qsort(base, nmemb, size, compar);
343 }
344
345 int proc_cmdline(char **ret);
346 int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
347 int get_proc_cmdline_key(const char *parameter, char **value);
348
349 int container_get_leader(const char *machine, pid_t *pid);
350
351 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
352 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
353
354 #ifndef PERSONALITY_INVALID
355 /* personality(7) documents that 0xffffffffUL is used for querying the
356 * current personality, hence let's use that here as error
357 * indicator. */
358 #define PERSONALITY_INVALID 0xffffffffLU
359 #endif
360
361 unsigned long personality_from_string(const char *p);
362 const char *personality_to_string(unsigned long);
363
364 uint64_t physical_memory(void);
365
366 union file_handle_union {
367 struct file_handle handle;
368 char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
369 };
370 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
371
372 int update_reboot_param_file(const char *param);
373
374 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
375
376 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
377 for ((e) = &buffer.ev; \
378 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
379 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
380
381 union inotify_event_buffer {
382 struct inotify_event ev;
383 uint8_t raw[INOTIFY_EVENT_MAX];
384 };
385
386 int syslog_parse_priority(const char **p, int *priority, bool with_facility);
387
388 int version(void);
389
390 bool fdname_is_valid(const char *s);
391
392 bool oom_score_adjust_is_valid(int oa);