util.h revision 5261ba901845c084de5a8fd06500ed09bfb0bd80
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <alloca.h>
#include <fcntl.h>
#include <inttypes.h>
#include <time.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sched.h>
#include <limits.h>
#include <dirent.h>
#include <sys/resource.h>
#include <stddef.h>
#include <unistd.h>
#include <locale.h>
#include <mntent.h>
#if SIZEOF_PID_T == 4
#else
#endif
#if SIZEOF_UID_T == 4
#else
#endif
#if SIZEOF_GID_T == 4
#else
#endif
#if SIZEOF_TIME_T == 8
#else
#endif
#if SIZEOF_RLIM_T == 8
#else
#endif
#include "macro.h"
#include "missing.h"
#include "time-util.h"
/* What is interpreted as whitespace? */
#define WHITESPACE " \t\n\r"
#define NEWLINE "\n\r"
#define QUOTES "\"\'"
#define COMMENTS "#;"
#define GLOB_CHARS "*?["
/* What characters are special in the shell? */
/* must be escaped outside and inside double-quotes */
#define SHELL_NEED_ESCAPE "\"\\`$"
/* can be escaped or double-quoted */
#define FORMAT_BYTES_MAX 8
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
#define ANSI_RED_ON "\x1B[31m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
#define ANSI_GREEN_ON "\x1B[32m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
static inline const char* yes_no(bool b) {
return b ? "yes" : "no";
}
static inline const char* true_false(bool b) {
return b ? "true" : "false";
}
static inline const char* strempty(const char *s) {
return s ? s : "";
}
static inline const char* strnull(const char *s) {
return s ? s : "(null)";
}
static inline const char *strna(const char *s) {
return s ? s : "n/a";
}
static inline bool isempty(const char *p) {
return !p || !p[0];
}
static inline const char *startswith(const char *s, const char *prefix) {
return NULL;
}
static inline const char *startswith_no_case(const char *s, const char *prefix) {
return NULL;
}
int close_nointr(int fd);
int safe_close(int fd);
void safe_close_pair(int p[]);
int parse_boolean(const char *v) _pure_;
int safe_atollu(const char *s, unsigned long long *ret_u);
int safe_atolli(const char *s, long long int *ret_i);
#if __WORDSIZE == 32
static inline int safe_atolu(const char *s, unsigned long *ret_u) {
assert_cc(sizeof(unsigned long) == sizeof(unsigned));
}
static inline int safe_atoli(const char *s, long int *ret_u) {
assert_cc(sizeof(long int) == sizeof(int));
}
#else
static inline int safe_atolu(const char *s, unsigned long *ret_u) {
assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
return safe_atollu(s, (unsigned long long*) ret_u);
}
static inline int safe_atoli(const char *s, long int *ret_u) {
assert_cc(sizeof(long int) == sizeof(long long int));
return safe_atolli(s, (long long int*) ret_u);
}
#endif
}
}
return safe_atollu(s, (unsigned long long*) ret_u);
}
return safe_atolli(s, (long long int*) ret_i);
}
for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
int readlink_malloc(const char *p, char **r);
int readlink_and_make_absolute(const char *p, char **r);
int readlink_and_canonicalize(const char *p, char **r);
int reset_all_signal_handlers(void);
char *strstrip(char *s);
char *delete_chars(char *s, const char *bad);
char *truncate_nl(char *s);
char *cescape(const char *s);
char *cunescape(const char *s);
char *ascii_strlower(char *path);
bool chars_intersect(const char *a, const char *b) _pure_;
int make_stdio(int fd);
int make_null_stdio(void);
int make_console_stdio(void);
int dev_urandom(void *p, size_t n);
void random_bytes(void *p, size_t n);
static inline uint64_t random_u64(void) {
uint64_t u;
random_bytes(&u, sizeof(u));
return u;
}
static inline uint32_t random_u32(void) {
uint32_t u;
random_bytes(&u, sizeof(u));
return u;
}
/* For basic lookup tables with strictly enumerated entries */
return NULL; \
} \
type i; \
if (!s) \
return (type) -1; \
return i; \
return (type) -1; \
} \
#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
/* For string conversions where numbers are also acceptable */
char *s; \
int r; \
if (i < 0 || i > max) \
return -ERANGE; \
if (!s) \
return log_oom(); \
} else { \
r = asprintf(&s, "%u", i); \
if (r < 0) \
return log_oom(); \
} \
*str = s; \
return 0; \
} \
type i; \
unsigned u = 0; \
assert(s); \
return i; \
return (type) u; \
return (type) -1; \
} \
bool fstype_is_network(const char *fstype);
int reset_terminal(const char *name);
int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
int release_terminal(void);
int ignore_signals(int sig, ...);
int default_signals(int sig, ...);
bool is_device_path(const char *path);
int dir_is_empty(const char *path);
char* dirname_malloc(const char *path);
int sigprocmask_many(int how, ...);
bool hostname_is_set(void);
char* gethostname_malloc(void);
char* getlogname_malloc(void);
char* getusername_malloc(void);
int getttyname_malloc(int fd, char **r);
int getttyname_harder(int fd, char **r);
int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
int fd_columns(int fd);
unsigned columns(void);
unsigned lines(void);
bool on_tty(void);
static inline const char *ansi_highlight(void) {
}
static inline const char *ansi_highlight_red(void) {
}
static inline const char *ansi_highlight_green(void) {
}
static inline const char *ansi_highlight_yellow(void) {
}
static inline const char *ansi_highlight_blue(void) {
}
static inline const char *ansi_highlight_off(void) {
}
int running_in_chroot(void);
/* bytes columns */
char *normalize_env_assignment(const char *s);
int null_or_empty_path(const char *fn);
int null_or_empty_fd(int fd);
char *fstab_node_to_udev_node(const char *p);
char *resolve_dev_console(char **active);
bool tty_is_vc_resolve(const char *tty);
int vtnr_from_tty(const char *tty);
const char *default_term_for_tty(const char *tty);
bool plymouth_running(void);
bool hostname_is_valid(const char *s) _pure_;
char* hostname_cleanup(char *s, bool lowercase);
bool machine_name_is_valid(const char *s) _pure_;
char* strshorten(char *s, size_t l);
int terminal_vhangup_fd(int fd);
int terminal_vhangup(const char *name);
int vt_disallocate(const char *name);
int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
int glob_exists(const char *path);
char *strjoin(const char *x, ...) _sentinel_;
bool is_main_thread(void);
assert(s);
}
int file_is_priv_sticky(const char *p);
int strdup_or_null(const char *a, char **b);
#define NULSTR_FOREACH(i, l) \
for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
#define NULSTR_FOREACH_PAIR(i, j, l) \
for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
int ioprio_class_to_string_alloc(int i, char **s);
int ioprio_class_from_string(const char *s);
const char *sigchld_code_to_string(int i) _const_;
int sigchld_code_from_string(const char *s) _pure_;
int log_facility_unshifted_to_string_alloc(int i, char **s);
int log_facility_unshifted_from_string(const char *s);
int log_level_to_string_alloc(int i, char **s);
int log_level_from_string(const char *s);
int sched_policy_to_string_alloc(int i, char **s);
int sched_policy_from_string(const char *s);
const char *rlimit_to_string(int i) _const_;
int rlimit_from_string(const char *s) _pure_;
int ip_tos_to_string_alloc(int i, char **s);
int ip_tos_from_string(const char *s);
const char *signal_to_string(int i) _const_;
int signal_from_string(const char *s) _pure_;
int signal_from_string_try_harder(const char *s);
extern int saved_argc;
extern char **saved_argv;
bool kexec_loaded(void);
bool in_initrd(void);
void warn_melody(void);
int get_home_dir(char **ret);
static inline void freep(void *p) {
free(*(void**) p);
}
if (*p) \
func(*p); \
} \
safe_close(*fd);
}
umask(*u);
}
static inline void close_pairp(int (*p)[2]) {
safe_close_pair(*p);
}
return NULL;
return malloc(a * b);
}
return NULL;
return realloc(p, a * b);
}
return NULL;
return memdup(p, a * b);
}
bool filename_is_safe(const char *p) _pure_;
bool path_is_safe(const char *p) _pure_;
bool string_is_safe(const char *p) _pure_;
/**
* Check if a string contains any glob patterns.
*/
_pure_ static inline bool string_is_glob(const char *p) {
return !!strpbrk(p, GLOB_CHARS);
}
int (*compar) (const void *, const void *, void *),
void *arg);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
char *strip_tab_ansi(char **p, size_t *l);
int on_ac_power(void);
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
for (;;) \
if (ferror(f)) { \
on_error; \
} \
break; \
} else
if (!de) { \
if (errno > 0) { \
on_error; \
} \
break; \
continue; \
else
memset(s, c, n);
return (uint8_t*)s + n;
}
char *strextend(char **x, ...) _sentinel_;
char *strrep(const char *s, unsigned n);
static inline void _reset_errno_(int *saved_errno) {
errno = *saved_errno;
}
struct _umask_struct_ {
bool quit;
};
static inline void _reset_umask_(struct _umask_struct_ *s) {
};
#define RUN_WITH_UMASK(mask) \
!_saved_umask_.quit ; \
_saved_umask_.quit = true)
#if __SIZEOF_LONG_LONG__ == 8
#else
#error "Wut?"
#endif
}
#if __SIZEOF_INT__ == 4
return __builtin_ctz(n);
#else
#error "Wut?"
#endif
}
static inline int log2i(int x) {
assert(x > 0);
}
static inline bool logind_running(void) {
}
#define DECIMAL_STR_WIDTH(x) \
({ \
unsigned ans = 1; \
while (_x_ /= 10) \
ans++; \
ans; \
})
int unlink_noerrno(const char *path);
#define alloca0(n) \
({ \
char *_new_; \
})
#define strappenda(a, ...) \
({ \
unsigned _i; \
const char *_appendees_[] = { __VA_ARGS__ }; \
_d_; \
})
({ \
const char *_r_; \
if (_pid_ == 0) { \
} else { \
} \
_r_; \
})
struct _locale_struct_ {
bool quit;
};
static inline void _reset_locale_(struct _locale_struct_ *s) {
if (s->saved_locale != (locale_t) 0)
uselocale(s->saved_locale);
if (s->new_locale != (locale_t) 0)
freelocale(s->new_locale);
}
for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
({ \
if (!_saved_locale_.quit) { \
} \
!_saved_locale_.quit; }) ; \
_saved_locale_.quit = true)
bool id128_is_valid(const char *s) _pure_;
int split_pair(const char *s, const char *sep, char **l, char **r);
int shall_restore_state(void);
/**
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
int (*compar)(const void *, const void *)) {
if (nmemb) {
}
}
int proc_cmdline(char **ret);
unsigned long personality_from_string(const char *p);
const char *personality_to_string(unsigned long);
uint64_t physical_memory(void);
union file_handle_union {
struct file_handle handle;
};
int update_reboot_param_file(const char *param);
int fflush_and_check(FILE *f);
char *tempfn_xxxxxx(const char *p);
char *tempfn_random(const char *p);
bool is_localhost(const char *hostname);
int take_password_lock(const char *root);
int is_symlink(const char *path);