util.h revision 4b73a0c0612d26d49791f389e92f85d6444c36af
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt This file is part of systemd.
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt Copyright 2010 Lennart Poettering
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt systemd is free software; you can redistribute it and/or modify it
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt under the terms of the GNU Lesser General Public License as published by
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt the Free Software Foundation; either version 2.1 of the License, or
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt (at your option) any later version.
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt systemd is distributed in the hope that it will be useful, but
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt WITHOUT ANY WARRANTY; without even the implied warranty of
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt Lesser General Public License for more details.
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt You should have received a copy of the GNU Lesser General Public License
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt along with systemd; If not, see <http://www.gnu.org/licenses/>.
709d6710d047566c71f03e579a02c3d99fe15a3ePatrik Flykt uint8_t storage[offsetof(struct dirent, d_name) +
709d6710d047566c71f03e579a02c3d99fe15a3ePatrik Flykt ((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))];
709d6710d047566c71f03e579a02c3d99fe15a3ePatrik Flykt/* What is interpreted as whitespace? */
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktbool streq_ptr(const char *a, const char *b);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define new0(t, n) ((t*) calloc((n), sizeof(t)))
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktstatic inline const char* yes_no(bool b) {
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktstatic inline const char* strempty(const char *s) {
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt return s ? s : "";
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktstatic inline const char* strnull(const char *s) {
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt return s ? s : "(null)";
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktstatic inline const char *strna(const char *s) {
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt return s ? s : "n/a";
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktstatic inline bool isempty(const char *p) {
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt return !p || !p[0];
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *endswith(const char *s, const char *postfix);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *startswith(const char *s, const char *prefix);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *startswith_no_case(const char *s, const char *prefix);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktbool first_word(const char *s, const char *word);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktvoid close_many(const int fds[], unsigned n_fd);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint parse_boolean(const char *v);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint safe_atollu(const char *s, unsigned long long *ret_u);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint safe_atolli(const char *s, long long int *ret_i);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(unsigned long) == sizeof(unsigned));
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atoli(const char *s, long int *ret_u) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(long int) == sizeof(int));
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt return safe_atollu(s, (unsigned long long*) ret_u);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atoli(const char *s, long int *ret_u) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(long int) == sizeof(long long int));
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(uint32_t) == sizeof(unsigned));
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt return safe_atollu(s, (unsigned long long*) ret_u);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_cc(sizeof(int64_t) == sizeof(long long int));
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *split(const char *c, size_t *l, const char *separator, char **state);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *split_quoted(const char *c, size_t *l, char **state);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#define FOREACH_WORD_QUOTED(word, length, s, state) \
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint get_starttime_of_pid(pid_t pid, unsigned long long *st);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *strappend(const char *s, const char *suffix);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *strnappend(const char *s, const char *suffix, size_t length);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *replace_env(const char *format, char **env);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar **replace_env_argv(char **argv, char **env);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint readlink_malloc(const char *p, char **r);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint readlink_and_make_absolute(const char *p, char **r);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktint readlink_and_canonicalize(const char *p, char **r);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktchar *strstrip(char *s);
char *truncate_nl(char *s);
char hexchar(int x);
int unhexchar(char c);
char octchar(int x);
int unoctchar(char c);
char decchar(int x);
int undecchar(char c);
char *cescape(const char *s);
char *cunescape(const char *s);
char *bus_path_escape(const char *s);
char *bus_path_unescape(const char *s);
bool chars_intersect(const char *a, const char *b);
int make_null_stdio(void);
int make_console_stdio(void);
unsigned long long random_ull(void);
return NULL; \
type i; \
assert(s); \
#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
if (i < 0 || i > max) \
return -ERANGE; \
return log_oom(); \
return log_oom(); \
*str = s; \
type i; \
assert(s); \
return (type) u; \
int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
int release_terminal(void);
int close_pipe(int p[]);
bool hostname_is_set(void);
char* gethostname_malloc(void);
char* getlogname_malloc(void);
char* getusername_malloc(void);
int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap);
int status_welcome(void);
unsigned columns(void);
unsigned lines(void);
bool on_tty(void);
int running_in_chroot(void);
char *normalize_env_assignment(const char *s);
char *fstab_node_to_udev_node(const char *p);
bool plymouth_running(void);
bool hostname_is_valid(const char *s);
char* hostname_cleanup(char *s);
int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
bool is_main_thread(void);
int file_is_priv_sticky(const char *p);
int strdup_or_null(const char *a, char **b);
#define NULSTR_FOREACH(i, l) \
#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);
int sigchld_code_from_string(const char *s);
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);
int rlimit_from_string(const char *s);
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);
int signal_from_string(const char *s);
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);
static inline void freep(void *p) {
free(*(void**) p);
umask(*u);
sd_journal_close(*j);
return NULL;
return malloc(a * b);
return NULL;
return memdup(p, a * b);
bool filename_is_safe(const char *p);
bool path_is_safe(const char *p);
bool string_is_safe(const char *p);
bool string_has_cc(const char *p);
int (*compar) (const void *, const void *, void *),
void *arg);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
int on_ac_power(void);
if (ferror(f)) { \
on_error; \
if (!de) { \
if (errno > 0) { \
on_error; \
memset(s, c, n);
char *strextend(char **x, ...);
char *strrep(const char *s, unsigned n);
struct umask_struct {
bool quit;
for (__attribute__((cleanup(_reset_umask_))) struct umask_struct _saved_umask_ = { umask(mask), false }; \
static inline bool logind_running(void) {