util.h revision 050f727728f0631ce2b9c5f9635054480ccea3f6
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster This file is part of systemd.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster Copyright 2010 Lennart Poettering
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster systemd is free software; you can redistribute it and/or modify it
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster under the terms of the GNU Lesser General Public License as published by
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster the Free Software Foundation; either version 2.1 of the License, or
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster (at your option) any later version.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster systemd is distributed in the hope that it will be useful, but
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster WITHOUT ANY WARRANTY; without even the implied warranty of
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster Lesser General Public License for more details.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster You should have received a copy of the GNU Lesser General Public License
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster along with systemd; If not, see <http://www.gnu.org/licenses/>.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* What is interpreted as whitespace? */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* What characters are special in the shell? */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* must be escaped outside and inside double-quotes */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* can be escaped or double-quoted */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;"
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterbool streq_ptr(const char *a, const char *b) _pure_;
static inline const char* yes_no(bool b) {
static inline const char* true_false(bool b) {
static inline const char* one_zero(bool b) {
static inline const char* strempty(const char *s) {
static inline const char* strnull(const char *s) {
static inline const char *strna(const char *s) {
static inline bool isempty(const char *p) {
size_t l;
return NULL;
size_t l;
return NULL;
void safe_close_pair(int p[]);
assert_cc(sizeof(unsigned long) == sizeof(unsigned));
assert_cc(sizeof(long int) == sizeof(int));
assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
assert_cc(sizeof(long int) == sizeof(long long int));
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);
int reset_signal_mask(void);
char *strstrip(char *s);
char *truncate_nl(char *s);
char *cescape(const char *s);
typedef enum UnescapeFlags {
int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
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)
if (i < 0 || i > max) \
return -ERANGE; \
return log_oom(); \
return log_oom(); \
*str = s; \
type i; \
assert(s); \
return (type) u; \
char* getlogname_malloc(void);
char* getusername_malloc(void);
#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
int running_in_chroot(void);
char *fstab_node_to_udev_node(const char *p);
bool plymouth_running(void);
int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
bool is_main_thread(void);
assert(s);
#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);
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);
int ip_tos_to_string_alloc(int i, char **s);
int ip_tos_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);
static inline void freep(void *p) {
free(*(void**) p);
umask(*u);
safe_close_pair(*p);
return NULL;
return malloc(a * b);
return NULL;
return realloc(p, a * b);
return NULL;
return memdup(p, a * b);
int (*compar) (const void *, const void *, void *),
void *arg);
void init_gettext(void);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
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);
if (ferror(f)) { \
on_error; \
if (!de) { \
if (errno > 0) { \
on_error; \
if (!de) { \
if (errno > 0) { \
on_error; \
memset(s, c, n);
return (uint8_t*)s + n;
char *strrep(const char *s, unsigned n);
static inline int negative_errno(void) {
return -errno;
struct _umask_struct_ {
bool quit;
return __builtin_ctz(n);
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);
static inline bool logind_running(void) {
#define DECIMAL_STR_WIDTH(x) \
ans++; \
ans; \
#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, ...) \
int _len_ = 0; \
unsigned _i_; \
*_p_ = 0; \
_d_; \
int shall_restore_state(void);
if (nmemb <= 0)
static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
if (needlelen <= 0)
return (void*) haystack;
return NULL;
#ifndef PERSONALITY_INVALID
unsigned long personality_from_string(const char *p);
const char *personality_to_string(unsigned long);
union file_handle_union {
typedef enum UnquoteFlags {
} UnquoteFlags;
int free_and_strdup(char **p, const char *s);
union inotify_event_buffer {
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);
char *shell_maybe_quote(const char *s);
int reset_uid_gid(void);