util.h revision 019c7fba754f74909bdb8bbbbbbe529082928a95
/*-*- 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 <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* one_zero(bool b) {
return b ? "1" : "0";
}
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 char *startswith(const char *s, const char *prefix) {
size_t l;
return (char*) s + l;
return NULL;
}
static inline char *startswith_no_case(const char *s, const char *prefix) {
size_t l;
if (strncasecmp(s, prefix, l) == 0)
return (char*) s + l;
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);
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_value(const char *p, char **ret);
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);
int reset_signal_mask(void);
char *strstrip(char *s);
char *delete_chars(char *s, const char *bad);
char *truncate_nl(char *s);
char *cescape(const char *s);
typedef enum UnescapeFlags {
UNESCAPE_RELAX = 1,
int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
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);
void initialize_srand(void);
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; \
}
}
#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(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, "%i", 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 fd_is_temporary_fs(int fd);
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);
#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
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);
}
#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 http_etag_is_valid(const char *etag);
bool in_initrd(void);
void warn_melody(void);
int get_home_dir(char **ret);
static inline void freep(void *p) {
free(*(void**) 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_valid(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);
void init_gettext(void);
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
if (!de) { \
if (errno > 0) { \
on_error; \
} \
break; \
} 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;
}
static inline int negative_errno(void) {
/* This helper should be used to shut up gcc if you know 'errno' is
* negative. Instead of "return -errno;", use "return negative_errno();"
* It will suppress bogus gcc warnings in case it assumes 'errno' might
* be 0 and thus the caller's error-handling might not be triggered. */
return -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 unsigned log2i(int x) {
assert(x > 0);
}
static inline unsigned log2u(unsigned x) {
assert(x > 0);
}
static inline unsigned log2u_round_up(unsigned x) {
assert(x > 0);
if (x == 1)
return 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_; \
})
/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
({ \
void *_ptr_; \
})
({ \
void *_new_; \
})
#define strjoina(a, ...) \
({ \
const char *_appendees_[] = { a, __VA_ARGS__ }; \
int _len_ = 0; \
unsigned _i_; \
*_p_ = 0; \
_d_; \
})
({ \
const char *_r_; \
if (_pid_ == 0) { \
} else { \
} \
_r_; \
})
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);
int tempfn_xxxxxx(const char *p, char **ret);
int tempfn_random(const char *p, char **ret);
int tempfn_random_child(const char *p, char **ret);
bool is_localhost(const char *hostname);
int take_password_lock(const char *root);
int is_symlink(const char *path);
typedef enum UnquoteFlags {
UNQUOTE_RELAX = 1,
UNQUOTE_CUNESCAPE = 2,
} UnquoteFlags;
int free_and_strdup(char **p, const char *s);
int sethostname_idempotent(const char *s);
union inotify_event_buffer {
struct inotify_event ev;
};
ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
int same_fd(int a, int b);
int read_attr_path(const char *p, unsigned *ret);
typedef struct LockFile {
char *path;
int fd;
int operation;
} LockFile;
void release_lock_file(LockFile *f);
char *shell_maybe_quote(const char *s);