1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
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.
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.
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/>.
35 #include <sys/types.h>
36 #include <sys/socket.h>
43 #include <sys/inotify.h>
44 #include <sys/statfs.h>
48 #include "time-util.h"
49 #include "formats-util.h"
51 /* What is interpreted as whitespace? */
52 #define WHITESPACE " \t\n\r"
53 #define NEWLINE "\n\r"
56 #define GLOB_CHARS "*?["
58 /* What characters are special in the shell? */
59 /* must be escaped outside and inside double-quotes */
60 #define SHELL_NEED_ESCAPE "\"\\`$"
61 /* can be escaped or double-quoted */
62 #define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;"
64 #define FORMAT_BYTES_MAX 8
66 #define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
67 #define ANSI_RED_ON "\x1B[31m"
68 #define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
69 #define ANSI_GREEN_ON "\x1B[32m"
70 #define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
71 #define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
72 #define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
73 #define ANSI_HIGHLIGHT_OFF "\x1B[0m"
74 #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
76 size_t page_size(void) _pure_;
77 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
79 #define streq(a,b) (strcmp((a),(b)) == 0)
80 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
81 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
82 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
84 bool streq_ptr(const char *a, const char *b) _pure_;
86 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
88 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
90 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
92 #define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
94 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
96 #define malloc0(n) (calloc((n), 1))
98 static inline const char* yes_no(bool b) {
99 return b ? "yes" : "no";
102 static inline const char* true_false(bool b) {
103 return b ? "true" : "false";
106 static inline const char* one_zero(bool b) {
107 return b ? "1" : "0";
110 static inline const char* strempty(const char *s) {
114 static inline const char* strnull(const char *s) {
115 return s ? s : "(null)";
118 static inline const char *strna(const char *s) {
119 return s ? s : "n/a";
122 static inline bool isempty(const char *p) {
126 static inline char *startswith(const char *s, const char *prefix) {
130 if (strncmp(s, prefix, l) == 0)
131 return (char*) s + l;
136 static inline char *startswith_no_case(const char *s, const char *prefix) {
140 if (strncasecmp(s, prefix, l) == 0)
141 return (char*) s + l;
146 char *endswith(const char *s, const char *postfix) _pure_;
147 char *endswith_no_case(const char *s, const char *postfix) _pure_;
149 char *first_word(const char *s, const char *word) _pure_;
151 int close_nointr(int fd);
152 int safe_close(int fd);
153 void safe_close_pair(int p[]);
155 void close_many(const int fds[], unsigned n_fd);
157 int parse_size(const char *t, off_t base, off_t *size);
159 int parse_boolean(const char *v) _pure_;
160 int parse_pid(const char *s, pid_t* ret_pid);
161 int parse_uid(const char *s, uid_t* ret_uid);
162 #define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
164 int safe_atou(const char *s, unsigned *ret_u);
165 int safe_atoi(const char *s, int *ret_i);
167 int safe_atollu(const char *s, unsigned long long *ret_u);
168 int safe_atolli(const char *s, long long int *ret_i);
170 int safe_atod(const char *s, double *ret_d);
172 int safe_atou8(const char *s, uint8_t *ret);
174 #if LONG_MAX == INT_MAX
175 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
176 assert_cc(sizeof(unsigned long) == sizeof(unsigned));
177 return safe_atou(s, (unsigned*) ret_u);
179 static inline int safe_atoli(const char *s, long int *ret_u) {
180 assert_cc(sizeof(long int) == sizeof(int));
181 return safe_atoi(s, (int*) ret_u);
184 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
185 assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
186 return safe_atollu(s, (unsigned long long*) ret_u);
188 static inline int safe_atoli(const char *s, long int *ret_u) {
189 assert_cc(sizeof(long int) == sizeof(long long int));
190 return safe_atolli(s, (long long int*) ret_u);
194 static inline int safe_atou32(const char *s, uint32_t *ret_u) {
195 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
196 return safe_atou(s, (unsigned*) ret_u);
199 static inline int safe_atoi32(const char *s, int32_t *ret_i) {
200 assert_cc(sizeof(int32_t) == sizeof(int));
201 return safe_atoi(s, (int*) ret_i);
204 static inline int safe_atou64(const char *s, uint64_t *ret_u) {
205 assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
206 return safe_atollu(s, (unsigned long long*) ret_u);
209 static inline int safe_atoi64(const char *s, int64_t *ret_i) {
210 assert_cc(sizeof(int64_t) == sizeof(long long int));
211 return safe_atolli(s, (long long int*) ret_i);
214 int safe_atou16(const char *s, uint16_t *ret);
215 int safe_atoi16(const char *s, int16_t *ret);
217 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
219 #define FOREACH_WORD(word, length, s, state) \
220 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
222 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
223 _FOREACH_WORD(word, length, s, separator, false, state)
225 #define FOREACH_WORD_QUOTED(word, length, s, state) \
226 _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
228 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
229 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
231 char *strappend(const char *s, const char *suffix);
232 char *strnappend(const char *s, const char *suffix, size_t length);
234 char *replace_env(const char *format, char **env);
235 char **replace_env_argv(char **argv, char **env);
237 int readlinkat_malloc(int fd, const char *p, char **ret);
238 int readlink_malloc(const char *p, char **r);
239 int readlink_value(const char *p, char **ret);
240 int readlink_and_make_absolute(const char *p, char **r);
241 int readlink_and_canonicalize(const char *p, char **r);
243 int reset_all_signal_handlers(void);
244 int reset_signal_mask(void);
246 char *strstrip(char *s);
247 char *delete_chars(char *s, const char *bad);
248 char *truncate_nl(char *s);
250 char *file_in_same_dir(const char *path, const char *filename);
252 int rmdir_parents(const char *path, const char *stop);
254 char hexchar(int x) _const_;
255 int unhexchar(char c) _const_;
256 char octchar(int x) _const_;
257 int unoctchar(char c) _const_;
258 char decchar(int x) _const_;
259 int undecchar(char c) _const_;
261 char *cescape(const char *s);
262 size_t cescape_char(char c, char *buf);
264 typedef enum UnescapeFlags {
268 int cunescape(const char *s, UnescapeFlags flags, char **ret);
269 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret);
270 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
272 char *xescape(const char *s, const char *bad);
274 char *ascii_strlower(char *path);
276 bool dirent_is_file(const struct dirent *de) _pure_;
277 bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
279 bool hidden_file(const char *filename) _pure_;
281 bool chars_intersect(const char *a, const char *b) _pure_;
283 int make_stdio(int fd);
284 int make_null_stdio(void);
285 int make_console_stdio(void);
287 /* For basic lookup tables with strictly enumerated entries */
288 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
289 scope const char *name##_to_string(type i) { \
290 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
292 return name##_table[i]; \
295 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
297 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
298 scope inline type name##_from_string(const char *s) { \
299 return (type)string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
302 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
303 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
304 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
305 struct __useless_struct_to_allow_trailing_semicolon__
307 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
308 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
309 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
310 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
312 /* For string conversions where numbers are also acceptable */
313 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
314 int name##_to_string_alloc(type i, char **str) { \
317 if (i < 0 || i > max) \
319 if (i < (type) ELEMENTSOF(name##_table)) { \
320 s = strdup(name##_table[i]); \
324 r = asprintf(&s, "%i", i); \
331 type name##_from_string(const char *s) { \
335 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
336 if (name##_table[i] && \
337 streq(name##_table[i], s)) \
339 if (safe_atou(s, &u) >= 0 && u <= max) \
343 struct __useless_struct_to_allow_trailing_semicolon__
345 int fd_nonblock(int fd, bool nonblock);
346 int fd_cloexec(int fd, bool cloexec);
348 int close_all_fds(const int except[], unsigned n_except);
350 bool fstype_is_network(const char *fstype);
354 int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
355 int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
356 int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
358 int reset_terminal_fd(int fd, bool switch_to_text);
359 int reset_terminal(const char *name);
361 int open_terminal(const char *name, int mode);
362 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
363 int release_terminal(void);
365 int flush_fd(int fd);
367 int ignore_signals(int sig, ...);
368 int default_signals(int sig, ...);
369 int sigaction_many(const struct sigaction *sa, ...);
371 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
373 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
374 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
375 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
377 bool is_device_path(const char *path);
379 int dir_is_empty(const char *path);
380 char* dirname_malloc(const char *path);
382 void sigset_add_many(sigset_t *ss, ...);
383 int sigprocmask_many(int how, ...);
385 bool hostname_is_set(void);
387 char* lookup_uid(uid_t uid);
388 char* gethostname_malloc(void);
389 char* getlogname_malloc(void);
390 char* getusername_malloc(void);
392 int getttyname_malloc(int fd, char **r);
393 int getttyname_harder(int fd, char **r);
395 int get_ctty_devnr(pid_t pid, dev_t *d);
396 int get_ctty(pid_t, dev_t *_devnr, char **r);
398 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
399 int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
401 bool is_temporary_fs(const struct statfs *s) _pure_;
402 int fd_is_temporary_fs(int fd);
404 int pipe_eof(int fd);
406 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
408 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
409 int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
411 #define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
413 int fd_columns(int fd);
414 unsigned columns(void);
415 int fd_lines(int fd);
416 unsigned lines(void);
417 void columns_lines_cache_reset(int _unused_ signum);
421 static inline const char *ansi_highlight(void) {
422 return on_tty() ? ANSI_HIGHLIGHT_ON : "";
425 static inline const char *ansi_highlight_red(void) {
426 return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
429 static inline const char *ansi_highlight_green(void) {
430 return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
433 static inline const char *ansi_highlight_yellow(void) {
434 return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
437 static inline const char *ansi_highlight_blue(void) {
438 return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
441 static inline const char *ansi_highlight_off(void) {
442 return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
445 int files_same(const char *filea, const char *fileb);
447 int running_in_chroot(void);
449 char *ellipsize(const char *s, size_t length, unsigned percent);
451 char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent);
453 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
454 int touch(const char *path);
456 noreturn void freeze(void);
458 bool null_or_empty(struct stat *st) _pure_;
459 int null_or_empty_path(const char *fn);
460 int null_or_empty_fd(int fd);
462 DIR *xopendirat(int dirfd, const char *name, int flags);
464 char *fstab_node_to_udev_node(const char *p);
466 char *resolve_dev_console(char **active);
467 bool tty_is_vc(const char *tty);
468 bool tty_is_vc_resolve(const char *tty);
469 bool tty_is_console(const char *tty) _pure_;
470 int vtnr_from_tty(const char *tty);
471 const char *default_term_for_tty(const char *tty);
473 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
475 bool nulstr_contains(const char*nulstr, const char *needle);
477 bool plymouth_running(void);
479 bool hostname_is_valid(const char *s) _pure_;
480 char* hostname_cleanup(char *s, bool lowercase);
482 bool machine_name_is_valid(const char *s) _pure_;
484 char* strshorten(char *s, size_t l);
486 int terminal_vhangup_fd(int fd);
487 int terminal_vhangup(const char *name);
489 int vt_disallocate(const char *name);
491 int symlink_atomic(const char *from, const char *to);
492 int mknod_atomic(const char *path, mode_t mode, dev_t dev);
493 int mkfifo_atomic(const char *path, mode_t mode);
495 int fchmod_umask(int fd, mode_t mode);
497 bool display_is_local(const char *display) _pure_;
498 int socket_from_display(const char *display, char **path);
500 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
501 int get_group_creds(const char **groupname, gid_t *gid);
503 int in_gid(gid_t gid);
504 int in_group(const char *name);
506 char* uid_to_name(uid_t uid);
507 char* gid_to_name(gid_t gid);
509 int glob_exists(const char *path);
510 int glob_extend(char ***strv, const char *path);
512 int dirent_ensure_type(DIR *d, struct dirent *de);
514 int get_files_in_directory(const char *path, char ***list);
516 char *strjoin(const char *x, ...) _sentinel_;
518 bool is_main_thread(void);
520 static inline bool _pure_ in_charset(const char *s, const char* charset) {
523 return s[strspn(s, charset)] == '\0';
526 int block_get_whole_disk(dev_t d, dev_t *ret);
528 #define NULSTR_FOREACH(i, l) \
529 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
531 #define NULSTR_FOREACH_PAIR(i, j, l) \
532 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
534 int ioprio_class_to_string_alloc(int i, char **s);
535 int ioprio_class_from_string(const char *s);
537 const char *sigchld_code_to_string(int i) _const_;
538 int sigchld_code_from_string(const char *s) _pure_;
540 int log_facility_unshifted_to_string_alloc(int i, char **s);
541 int log_facility_unshifted_from_string(const char *s);
543 int log_level_to_string_alloc(int i, char **s);
544 int log_level_from_string(const char *s);
546 int sched_policy_to_string_alloc(int i, char **s);
547 int sched_policy_from_string(const char *s);
549 const char *rlimit_to_string(int i) _const_;
550 int rlimit_from_string(const char *s) _pure_;
552 int ip_tos_to_string_alloc(int i, char **s);
553 int ip_tos_from_string(const char *s);
555 const char *signal_to_string(int i) _const_;
556 int signal_from_string(const char *s) _pure_;
558 int signal_from_string_try_harder(const char *s);
560 extern int saved_argc;
561 extern char **saved_argv;
563 bool kexec_loaded(void);
565 int prot_from_flags(int flags) _const_;
567 char *format_bytes(char *buf, size_t l, off_t t);
569 int fd_wait_for_event(int fd, int event, usec_t timeout);
571 void* memdup(const void *p, size_t l) _alloc_(2);
573 int fd_inc_sndbuf(int fd, size_t n);
574 int fd_inc_rcvbuf(int fd, size_t n);
576 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
578 int setrlimit_closest(int resource, const struct rlimit *rlim);
580 bool http_url_is_valid(const char *url) _pure_;
581 bool documentation_url_is_valid(const char *url) _pure_;
583 bool http_etag_is_valid(const char *etag);
585 bool in_initrd(void);
587 void warn_melody(void);
589 int get_home_dir(char **ret);
590 int get_shell(char **_ret);
592 static inline void freep(void *p) {
596 static inline void closep(int *fd) {
600 static inline void umaskp(mode_t *u) {
604 static inline void close_pairp(int (*p)[2]) {
608 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
609 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
610 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
611 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
613 #define _cleanup_free_ _cleanup_(freep)
614 #define _cleanup_close_ _cleanup_(closep)
615 #define _cleanup_umask_ _cleanup_(umaskp)
616 #define _cleanup_globfree_ _cleanup_(globfree)
617 #define _cleanup_fclose_ _cleanup_(fclosep)
618 #define _cleanup_pclose_ _cleanup_(pclosep)
619 #define _cleanup_closedir_ _cleanup_(closedirp)
620 #define _cleanup_endmntent_ _cleanup_(endmntentp)
621 #define _cleanup_close_pair_ _cleanup_(close_pairp)
623 _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
624 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
627 return malloc(a * b);
630 _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
631 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
634 return realloc(p, a * b);
637 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
638 if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
641 return memdup(p, a * b);
644 bool filename_is_valid(const char *p) _pure_;
645 bool path_is_safe(const char *p) _pure_;
646 bool string_is_safe(const char *p) _pure_;
647 bool string_has_cc(const char *p, const char *ok) _pure_;
650 * Check if a string contains any glob patterns.
652 _pure_ static inline bool string_is_glob(const char *p) {
653 return !!strpbrk(p, GLOB_CHARS);
656 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
657 int (*compar) (const void *, const void *, void *),
660 #define _(String) gettext (String)
661 void init_gettext(void);
662 bool is_locale_utf8(void);
664 typedef enum DrawSpecialChar {
669 DRAW_TRIANGULAR_BULLET,
673 _DRAW_SPECIAL_CHAR_MAX
676 const char *draw_special_char(DrawSpecialChar ch);
678 char *strreplace(const char *text, const char *old_string, const char *new_string);
680 char *strip_tab_ansi(char **p, size_t *l);
682 int on_ac_power(void);
684 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
685 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
687 #define FOREACH_LINE(line, f, on_error) \
689 if (!fgets(line, sizeof(line), f)) { \
696 #define FOREACH_DIRENT(de, d, on_error) \
697 for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
703 } else if (hidden_file((de)->d_name)) \
707 #define FOREACH_DIRENT_ALL(de, d, on_error) \
708 for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
716 static inline void *mempset(void *s, int c, size_t n) {
718 return (uint8_t*)s + n;
721 char *hexmem(const void *p, size_t l);
722 void *unhexmem(const char *p, size_t l);
724 char *strextend(char **x, ...) _sentinel_;
725 char *strrep(const char *s, unsigned n);
727 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
728 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
729 #define GREEDY_REALLOC(array, allocated, need) \
730 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
732 #define GREEDY_REALLOC0(array, allocated, need) \
733 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
735 static inline void _reset_errno_(int *saved_errno) {
736 errno = *saved_errno;
739 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
741 static inline int negative_errno(void) {
742 /* This helper should be used to shut up gcc if you know 'errno' is
743 * negative. Instead of "return -errno;", use "return negative_errno();"
744 * It will suppress bogus gcc warnings in case it assumes 'errno' might
745 * be 0 and thus the caller's error-handling might not be triggered. */
746 assert_return(errno > 0, -EINVAL);
750 struct _umask_struct_ {
755 static inline void _reset_umask_(struct _umask_struct_ *s) {
759 #define RUN_WITH_UMASK(mask) \
760 for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
761 !_saved_umask_.quit ; \
762 _saved_umask_.quit = true)
764 static inline unsigned u64log2(uint64_t n) {
765 #if __SIZEOF_LONG_LONG__ == 8
766 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
772 static inline unsigned u32ctz(uint32_t n) {
773 #if __SIZEOF_INT__ == 4
774 return __builtin_ctz(n);
780 static inline unsigned log2i(int x) {
783 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
786 static inline unsigned log2u(unsigned x) {
789 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
792 static inline unsigned log2u_round_up(unsigned x) {
798 return log2u(x - 1) + 1;
801 static inline bool logind_running(void) {
802 return access("/run/systemd/seats/", F_OK) >= 0;
805 #define DECIMAL_STR_WIDTH(x) \
807 typeof(x) _x_ = (x); \
814 int unlink_noerrno(const char *path);
820 _new_ = alloca(_len_); \
821 (void *) memset(_new_, 0, _len_); \
824 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
825 #define alloca_align(size, align) \
828 size_t _mask_ = (align) - 1; \
829 _ptr_ = alloca((size) + _mask_); \
830 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
833 #define alloca0_align(size, align) \
836 size_t _size_ = (size); \
837 _new_ = alloca_align(_size_, (align)); \
838 (void*)memset(_new_, 0, _size_); \
841 #define strjoina(a, ...) \
843 const char *_appendees_[] = { a, __VA_ARGS__ }; \
847 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
848 _len_ += strlen(_appendees_[_i_]); \
849 _p_ = _d_ = alloca(_len_ + 1); \
850 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
851 _p_ = stpcpy(_p_, _appendees_[_i_]); \
856 bool id128_is_valid(const char *s) _pure_;
858 int split_pair(const char *s, const char *sep, char **l, char **r);
860 int shall_restore_state(void);
863 * Normal qsort requires base to be nonnull. Here were require
864 * that only if nmemb > 0.
866 static inline void qsort_safe(void *base, size_t nmemb, size_t size,
867 int (*compar)(const void *, const void *)) {
870 qsort(base, nmemb, size, compar);
874 int proc_cmdline(char **ret);
875 int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
876 int get_proc_cmdline_key(const char *parameter, char **value);
878 int container_get_leader(const char *machine, pid_t *pid);
880 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
881 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
883 int getpeercred(int fd, struct ucred *ucred);
884 int getpeersec(int fd, char **ret);
886 int writev_safe(int fd, const struct iovec *w, int j);
888 int mkostemp_safe(char *pattern, int flags);
889 int open_tmpfile(const char *path, int flags);
891 int fd_warn_permissions(const char *path, int fd);
893 unsigned long personality_from_string(const char *p);
894 const char *personality_to_string(unsigned long);
896 uint64_t physical_memory(void);
898 void hexdump(FILE *f, const void *p, size_t s);
900 union file_handle_union {
901 struct file_handle handle;
902 char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
904 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
906 int update_reboot_param_file(const char *param);
908 int umount_recursive(const char *target, int flags);
910 int bind_remount_recursive(const char *prefix, bool ro);
912 int fflush_and_check(FILE *f);
914 int tempfn_xxxxxx(const char *p, char **ret);
915 int tempfn_random(const char *p, char **ret);
916 int tempfn_random_child(const char *p, char **ret);
918 bool is_localhost(const char *hostname);
920 int take_password_lock(const char *root);
922 int is_symlink(const char *path);
923 int is_dir(const char *path, bool follow);
925 typedef enum UnquoteFlags {
927 UNQUOTE_CUNESCAPE = 2,
930 int unquote_first_word(const char **p, char **ret, UnquoteFlags flags);
931 int unquote_many_words(const char **p, UnquoteFlags flags, ...) _sentinel_;
933 int free_and_strdup(char **p, const char *s);
935 int sethostname_idempotent(const char *s);
937 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
939 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
940 for ((e) = &buffer.ev; \
941 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
942 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
944 union inotify_event_buffer {
945 struct inotify_event ev;
946 uint8_t raw[INOTIFY_EVENT_MAX];
949 #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
951 int ptsname_malloc(int fd, char **ret);
953 int openpt_in_namespace(pid_t pid, int flags);
955 ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
957 int fd_setcrtime(int fd, usec_t usec);
958 int fd_getcrtime(int fd, usec_t *usec);
959 int path_getcrtime(const char *p, usec_t *usec);
960 int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
962 int chattr_fd(int fd, unsigned value, unsigned mask);
963 int chattr_path(const char *p, unsigned value, unsigned mask);
965 int read_attr_fd(int fd, unsigned *ret);
966 int read_attr_path(const char *p, unsigned *ret);
968 typedef struct LockFile {
974 int make_lock_file(const char *p, int operation, LockFile *ret);
975 int make_lock_file_for(const char *p, int operation, LockFile *ret);
976 void release_lock_file(LockFile *f);
978 #define _cleanup_release_lock_file_ _cleanup_(release_lock_file)
980 #define LOCK_FILE_INIT { .fd = -1, .path = NULL }
982 #define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
984 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
986 void sigkill_wait(pid_t *pid);
987 #define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait)
989 int syslog_parse_priority(const char **p, int *priority, bool with_facility);
991 void cmsg_close_all(struct msghdr *mh);
993 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
995 char *shell_maybe_quote(const char *s);
997 int parse_mode(const char *s, mode_t *ret);