]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
util-lib: introduce dirent-util.[ch] for directory entry calls
[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 int readlinkat_malloc(int fd, const char *p, char **ret);
87 int readlink_malloc(const char *p, char **r);
88 int readlink_value(const char *p, char **ret);
89 int readlink_and_make_absolute(const char *p, char **r);
90 int readlink_and_canonicalize(const char *p, char **r);
91
92 int rmdir_parents(const char *path, const char *stop);
93
94 /* For basic lookup tables with strictly enumerated entries */
95 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
96 scope const char *name##_to_string(type i) { \
97 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
98 return NULL; \
99 return name##_table[i]; \
100 }
101
102 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
103
104 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
105 scope type name##_from_string(const char *s) { \
106 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
107 }
108
109 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
110 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
111 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
112 struct __useless_struct_to_allow_trailing_semicolon__
113
114 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
115 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
116 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
117 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
118
119 /* For string conversions where numbers are also acceptable */
120 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
121 int name##_to_string_alloc(type i, char **str) { \
122 char *s; \
123 if (i < 0 || i > max) \
124 return -ERANGE; \
125 if (i < (type) ELEMENTSOF(name##_table)) { \
126 s = strdup(name##_table[i]); \
127 if (!s) \
128 return -ENOMEM; \
129 } else { \
130 if (asprintf(&s, "%i", i) < 0) \
131 return -ENOMEM; \
132 } \
133 *str = s; \
134 return 0; \
135 } \
136 type name##_from_string(const char *s) { \
137 type i; \
138 unsigned u = 0; \
139 if (!s) \
140 return (type) -1; \
141 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
142 if (streq_ptr(name##_table[i], s)) \
143 return i; \
144 if (safe_atou(s, &u) >= 0 && u <= max) \
145 return (type) u; \
146 return (type) -1; \
147 } \
148 struct __useless_struct_to_allow_trailing_semicolon__
149
150 bool fstype_is_network(const char *fstype);
151
152 int dir_is_empty(const char *path);
153
154 static inline int dir_is_populated(const char *path) {
155 int r;
156 r = dir_is_empty(path);
157 if (r < 0)
158 return r;
159 return !r;
160 }
161
162 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
163 int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
164
165 typedef long statfs_f_type_t;
166
167 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
168 int fd_check_fstype(int fd, statfs_f_type_t magic_value);
169 int path_check_fstype(const char *path, statfs_f_type_t magic_value);
170
171 bool is_temporary_fs(const struct statfs *s) _pure_;
172 int fd_is_temporary_fs(int fd);
173
174 #define xsprintf(buf, fmt, ...) \
175 assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
176 "xsprintf: " #buf "[] must be big enough")
177
178 int files_same(const char *filea, const char *fileb);
179
180 int running_in_chroot(void);
181
182 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
183 int touch(const char *path);
184
185 noreturn void freeze(void);
186
187 bool null_or_empty(struct stat *st) _pure_;
188 int null_or_empty_path(const char *fn);
189 int null_or_empty_fd(int fd);
190
191 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
192
193 bool plymouth_running(void);
194
195 int symlink_idempotent(const char *from, const char *to);
196
197 int symlink_atomic(const char *from, const char *to);
198 int mknod_atomic(const char *path, mode_t mode, dev_t dev);
199 int mkfifo_atomic(const char *path, mode_t mode);
200
201 int fchmod_umask(int fd, mode_t mode);
202
203 bool display_is_local(const char *display) _pure_;
204 int socket_from_display(const char *display, char **path);
205
206 int glob_exists(const char *path);
207 int glob_extend(char ***strv, const char *path);
208
209 int get_files_in_directory(const char *path, char ***list);
210
211 bool is_main_thread(void);
212
213 int block_get_whole_disk(dev_t d, dev_t *ret);
214
215 #define NULSTR_FOREACH(i, l) \
216 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
217
218 #define NULSTR_FOREACH_PAIR(i, j, l) \
219 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
220
221 int ioprio_class_to_string_alloc(int i, char **s);
222 int ioprio_class_from_string(const char *s);
223
224 const char *sigchld_code_to_string(int i) _const_;
225 int sigchld_code_from_string(const char *s) _pure_;
226
227 int log_facility_unshifted_to_string_alloc(int i, char **s);
228 int log_facility_unshifted_from_string(const char *s);
229 bool log_facility_unshifted_is_valid(int faciliy);
230
231 int log_level_to_string_alloc(int i, char **s);
232 int log_level_from_string(const char *s);
233 bool log_level_is_valid(int level);
234
235 int sched_policy_to_string_alloc(int i, char **s);
236 int sched_policy_from_string(const char *s);
237
238 extern int saved_argc;
239 extern char **saved_argv;
240
241 bool kexec_loaded(void);
242
243 int prot_from_flags(int flags) _const_;
244
245 void* memdup(const void *p, size_t l) _alloc_(2);
246
247 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
248
249 bool http_url_is_valid(const char *url) _pure_;
250 bool documentation_url_is_valid(const char *url) _pure_;
251
252 bool http_etag_is_valid(const char *etag);
253
254 bool in_initrd(void);
255
256 static inline void freep(void *p) {
257 free(*(void**) p);
258 }
259
260 static inline void umaskp(mode_t *u) {
261 umask(*u);
262 }
263
264 #define _cleanup_free_ _cleanup_(freep)
265 #define _cleanup_umask_ _cleanup_(umaskp)
266 #define _cleanup_globfree_ _cleanup_(globfree)
267
268 _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
269 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
270 return NULL;
271
272 return malloc(a * b);
273 }
274
275 _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
276 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
277 return NULL;
278
279 return realloc(p, a * b);
280 }
281
282 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
283 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
284 return NULL;
285
286 return memdup(p, a * b);
287 }
288
289 bool string_is_safe(const char *p) _pure_;
290
291 /**
292 * Check if a string contains any glob patterns.
293 */
294 _pure_ static inline bool string_is_glob(const char *p) {
295 return !!strpbrk(p, GLOB_CHARS);
296 }
297
298 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
299 int (*compar) (const void *, const void *, void *),
300 void *arg);
301
302 #define _(String) gettext (String)
303 #define N_(String) String
304 void init_gettext(void);
305 bool is_locale_utf8(void);
306
307 typedef enum DrawSpecialChar {
308 DRAW_TREE_VERTICAL,
309 DRAW_TREE_BRANCH,
310 DRAW_TREE_RIGHT,
311 DRAW_TREE_SPACE,
312 DRAW_TRIANGULAR_BULLET,
313 DRAW_BLACK_CIRCLE,
314 DRAW_ARROW,
315 DRAW_DASH,
316 _DRAW_SPECIAL_CHAR_MAX
317 } DrawSpecialChar;
318
319 const char *draw_special_char(DrawSpecialChar ch);
320
321 int on_ac_power(void);
322
323 static inline void *mempset(void *s, int c, size_t n) {
324 memset(s, c, n);
325 return (uint8_t*)s + n;
326 }
327
328 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
329 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
330 #define GREEDY_REALLOC(array, allocated, need) \
331 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
332
333 #define GREEDY_REALLOC0(array, allocated, need) \
334 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
335
336 static inline void _reset_errno_(int *saved_errno) {
337 errno = *saved_errno;
338 }
339
340 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
341
342 static inline int negative_errno(void) {
343 /* This helper should be used to shut up gcc if you know 'errno' is
344 * negative. Instead of "return -errno;", use "return negative_errno();"
345 * It will suppress bogus gcc warnings in case it assumes 'errno' might
346 * be 0 and thus the caller's error-handling might not be triggered. */
347 assert_return(errno > 0, -EINVAL);
348 return -errno;
349 }
350
351 struct _umask_struct_ {
352 mode_t mask;
353 bool quit;
354 };
355
356 static inline void _reset_umask_(struct _umask_struct_ *s) {
357 umask(s->mask);
358 };
359
360 #define RUN_WITH_UMASK(mask) \
361 for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
362 !_saved_umask_.quit ; \
363 _saved_umask_.quit = true)
364
365 static inline unsigned u64log2(uint64_t n) {
366 #if __SIZEOF_LONG_LONG__ == 8
367 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
368 #else
369 #error "Wut?"
370 #endif
371 }
372
373 static inline unsigned u32ctz(uint32_t n) {
374 #if __SIZEOF_INT__ == 4
375 return __builtin_ctz(n);
376 #else
377 #error "Wut?"
378 #endif
379 }
380
381 static inline unsigned log2i(int x) {
382 assert(x > 0);
383
384 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
385 }
386
387 static inline unsigned log2u(unsigned x) {
388 assert(x > 0);
389
390 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
391 }
392
393 static inline unsigned log2u_round_up(unsigned x) {
394 assert(x > 0);
395
396 if (x == 1)
397 return 0;
398
399 return log2u(x - 1) + 1;
400 }
401
402 #define DECIMAL_STR_WIDTH(x) \
403 ({ \
404 typeof(x) _x_ = (x); \
405 unsigned ans = 1; \
406 while (_x_ /= 10) \
407 ans++; \
408 ans; \
409 })
410
411 int unlink_noerrno(const char *path);
412
413 #define alloca0(n) \
414 ({ \
415 char *_new_; \
416 size_t _len_ = n; \
417 _new_ = alloca(_len_); \
418 (void *) memset(_new_, 0, _len_); \
419 })
420
421 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
422 #define alloca_align(size, align) \
423 ({ \
424 void *_ptr_; \
425 size_t _mask_ = (align) - 1; \
426 _ptr_ = alloca((size) + _mask_); \
427 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
428 })
429
430 #define alloca0_align(size, align) \
431 ({ \
432 void *_new_; \
433 size_t _size_ = (size); \
434 _new_ = alloca_align(_size_, (align)); \
435 (void*)memset(_new_, 0, _size_); \
436 })
437
438 bool id128_is_valid(const char *s) _pure_;
439
440 int shall_restore_state(void);
441
442 /**
443 * Normal qsort requires base to be nonnull. Here were require
444 * that only if nmemb > 0.
445 */
446 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
447 if (nmemb <= 1)
448 return;
449
450 assert(base);
451 qsort(base, nmemb, size, compar);
452 }
453
454 int proc_cmdline(char **ret);
455 int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
456 int get_proc_cmdline_key(const char *parameter, char **value);
457
458 int container_get_leader(const char *machine, pid_t *pid);
459
460 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
461 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
462
463 int fd_warn_permissions(const char *path, int fd);
464
465 #ifndef PERSONALITY_INVALID
466 /* personality(7) documents that 0xffffffffUL is used for querying the
467 * current personality, hence let's use that here as error
468 * indicator. */
469 #define PERSONALITY_INVALID 0xffffffffLU
470 #endif
471
472 unsigned long personality_from_string(const char *p);
473 const char *personality_to_string(unsigned long);
474
475 uint64_t physical_memory(void);
476
477 union file_handle_union {
478 struct file_handle handle;
479 char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
480 };
481 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
482
483 int update_reboot_param_file(const char *param);
484
485 int is_symlink(const char *path);
486 int is_dir(const char *path, bool follow);
487 int is_device_node(const char *path);
488
489 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
490
491 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
492 for ((e) = &buffer.ev; \
493 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
494 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
495
496 union inotify_event_buffer {
497 struct inotify_event ev;
498 uint8_t raw[INOTIFY_EVENT_MAX];
499 };
500
501 #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
502
503 ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
504
505 int fd_setcrtime(int fd, usec_t usec);
506 int fd_getcrtime(int fd, usec_t *usec);
507 int path_getcrtime(const char *p, usec_t *usec);
508 int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
509
510 int chattr_fd(int fd, unsigned value, unsigned mask);
511 int chattr_path(const char *p, unsigned value, unsigned mask);
512
513 int read_attr_fd(int fd, unsigned *ret);
514 int read_attr_path(const char *p, unsigned *ret);
515
516 int syslog_parse_priority(const char **p, int *priority, bool with_facility);
517
518 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
519
520 int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink);
521 int fgetxattr_malloc(int fd, const char *name, char **value);
522
523 int version(void);
524
525 bool fdname_is_valid(const char *s);
526
527 bool oom_score_adjust_is_valid(int oa);