util.h revision 1a29929959fd8f59e19ce60c25d1a1f7d910fac0
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen This file is part of systemd.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Copyright 2010 Lennart Poettering
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen systemd is free software; you can redistribute it and/or modify it
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen under the terms of the GNU Lesser General Public License as published by
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (at your option) any later version.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen systemd is distributed in the hope that it will be useful, but
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Lesser General Public License for more details.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen You should have received a copy of the GNU Lesser General Public License
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* What is interpreted as whitespace? */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* What characters are special in the shell? */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* must be escaped outside and inside double-quotes */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* can be escaped or double-quoted */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool streq_ptr(const char *a, const char *b) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define new0(t, n) ((t*) calloc((n), sizeof(t)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* yes_no(bool b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* true_false(bool b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* strempty(const char *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s ? s : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* strnull(const char *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s ? s : "(null)";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *strna(const char *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s ? s : "n/a";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline bool isempty(const char *p) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return !p || !p[0];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline char *startswith(const char *s, const char *prefix) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (char*) s + l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline char *startswith_no_case(const char *s, const char *prefix) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (char*) s + l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *endswith(const char *s, const char *postfix) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *first_word(const char *s, const char *word) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid close_many(const int fds[], unsigned n_fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_size(const char *t, off_t base, off_t *size);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atollu(const char *s, unsigned long long *ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atolli(const char *s, long long int *ret_i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(unsigned long) == sizeof(unsigned));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoli(const char *s, long int *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(long int) == sizeof(int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atollu(s, (unsigned long long*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoli(const char *s, long int *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(long int) == sizeof(long long int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atolli(s, (long long int*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(uint32_t) == sizeof(unsigned));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atollu(s, (unsigned long long*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(int64_t) == sizeof(long long int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atolli(s, (long long int*) ret_i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char* split(const char **state, size_t *l, const char *separator, bool quoted);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _FOREACH_WORD(word, length, s, separator, false, state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_WORD_QUOTED(word, length, s, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_starttime_of_pid(pid_t pid, unsigned long long *st);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strappend(const char *s, const char *suffix);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strnappend(const char *s, const char *suffix, size_t length);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *replace_env(const char *format, char **env);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar **replace_env_argv(char **argv, char **env);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlinkat_malloc(int fd, const char *p, char **ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_malloc(const char *p, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_and_make_absolute(const char *p, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_and_canonicalize(const char *p, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strstrip(char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *truncate_nl(char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *file_in_same_dir(const char *path, const char *filename);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rmdir_parents(const char *path, const char *stop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_capeff(pid_t pid, char **capeff);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_environ(pid_t pid, char **environ);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cescape(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cunescape(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cunescape_length(const char *s, size_t length);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool dirent_is_file(const struct dirent *de) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool chars_intersect(const char *a, const char *b) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen random_bytes(&u, sizeof(u));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen random_bytes(&u, sizeof(u));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* For basic lookup tables with strictly enumerated entries */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen scope const char *name##_to_string(type i) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen scope type name##_from_string(const char *s) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* For string conversions where numbers are also acceptable */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int name##_to_string_alloc(type i, char **str) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i < 0 || i > max) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned u = 0; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (type) u; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint close_all_fds(const int except[], unsigned n_except);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ask_string(char **ret, const char *text, ...) _printf_(2, 3);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint reset_terminal_fd(int fd, bool switch_to_text);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sigaction_many(const struct sigaction *sa, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunsigned columns(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunsigned lines(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid columns_lines_cache_reset(int _unused_ signum);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_red(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_green(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_yellow(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_blue(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_off(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint files_same(const char *filea, const char *fileb);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *ellipsize(const char *s, size_t length, unsigned percent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* bytes columns */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *unquote(const char *s, const char *quotes);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *normalize_env_assignment(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint wait_for_terminate(pid_t pid, siginfo_t *status);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint wait_for_terminate_and_warn(const char *name, pid_t pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom GundersenDIR *xopendirat(int dirfd, const char *name, int flags);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *fstab_node_to_udev_node(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *default_term_for_tty(const char *tty);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid execute_directory(const char *directory, DIR *_d, usec_t timeout, char *argv[]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool nulstr_contains(const char*nulstr, const char *needle);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* hostname_cleanup(char *s, bool lowercase);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool machine_name_is_valid(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint symlink_atomic(const char *from, const char *to);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint mknod_atomic(const char *path, mode_t mode, dev_t dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint mkfifo_atomic(const char *path, mode_t mode);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool display_is_local(const char *display) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint socket_from_display(const char *display, char **path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_group_creds(const char **groupname, gid_t *gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint glob_extend(char ***strv, const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint dirent_ensure_type(DIR *d, struct dirent *de);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_files_in_directory(const char *path, char ***list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline bool _pure_ in_charset(const char *s, const char* charset) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define NULSTR_FOREACH_PAIR(i, j, l) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ioprio_class_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ioprio_class_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *sigchld_code_to_string(int i) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sigchld_code_from_string(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_facility_unshifted_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_facility_unshifted_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_level_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_level_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sched_policy_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sched_policy_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ip_tos_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ip_tos_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint signal_from_string_try_harder(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *format_bytes(char *buf, size_t l, off_t t);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_wait_for_event(int fd, int event, usec_t timeout);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid* memdup(const void *p, size_t l) _alloc_(2);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint setrlimit_closest(int resource, const struct rlimit *rlim);
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersenint getenv_for_pid(pid_t pid, const char *field, char **_value);
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersenbool is_valid_documentation_url(const char *url) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void freep(void *p) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(*(void**) p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_closedir_ _cleanup_(closedirp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_endmntent_ _cleanup_(endmntentp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_close_pair_ _cleanup_(close_pairp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return malloc(a * b);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return realloc(p, a * b);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return memdup(p, a * b);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool string_has_cc(const char *p, const char *ok) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Check if a string contains any glob patterns.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_pure_ static inline bool string_is_glob(const char *p) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int (*compar) (const void *, const void *, void *),
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *draw_special_char(DrawSpecialChar ch);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strreplace(const char *text, const char *old_string, const char *new_string);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void *mempset(void *s, int c, size_t n) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (uint8_t*)s + n;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strrep(const char *s, unsigned n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define GREEDY_REALLOC(array, allocated, need) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define GREEDY_REALLOC0(array, allocated, need) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void _reset_errno_(int *saved_errno) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int negative_errno(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* This helper should be used to shut up gcc if you know 'errno' is
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * negative. Instead of "return -errno;", use "return negative_errno();"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * It will suppress bogus gcc warnings in case it assumes 'errno' might
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * be 0 and thus the caller's error-handling might not be triggered. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void _reset_umask_(struct _umask_struct_ *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int log2i(int x) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline unsigned log2u(unsigned x) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline unsigned log2u_round_up(unsigned x) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline bool logind_running(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return access("/run/systemd/seats/", F_OK) >= 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strappenda(a, ...) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *_appendees_[] = { __VA_ARGS__ }; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *_r_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_pid_ == 0) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void _reset_locale_(struct _locale_struct_ *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _saved_locale_.new_locale = newlocale((mask), (loc), (locale_t) 0); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_saved_locale_.new_locale != (locale_t) 0) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _saved_locale_.saved_locale = uselocale(_saved_locale_.new_locale); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint split_pair(const char *s, const char *sep, char **l, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Normal qsort requires base to be nonnull. Here were require
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * that only if nmemb > 0.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void qsort_safe(void *base, size_t nmemb, size_t size,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int (*compar)(const void *, const void *)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_proc_cmdline_key(const char *parameter, char **value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint container_get_leader(const char *machine, pid_t *pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint writev_safe(int fd, const struct iovec *w, int j);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_warn_permissions(const char *path, int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunsigned long personality_from_string(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *personality_to_string(unsigned long);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* mount_test_option(const char *haystack, const char *needle);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid hexdump(FILE *f, const void *p, size_t s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint update_reboot_param_file(const char *param);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint umount_recursive(const char *target, int flags);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint bind_remount_recursive(const char *prefix, bool ro);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *tempfn_xxxxxx(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *tempfn_random(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unquote_first_word(const char **p, char **ret, bool relax);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unquote_many_words(const char **p, ...) _sentinel_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint free_and_strdup(char **p, const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sethostname_idempotent(const char *s);