util.h revision f4f15635ec05293ffcc83a5b39f624bbabbd8fd0
/*-*- 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 <limits.h>
#include <locale.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "formats-util.h"
#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 "*?["
#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
return NULL;
}
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";
}
/* 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; \
if (i < 0 || i > max) \
return -ERANGE; \
if (!s) \
return -ENOMEM; \
} else { \
if (asprintf(&s, "%i", i) < 0) \
return -ENOMEM; \
} \
*str = s; \
return 0; \
} \
type i; \
unsigned u = 0; \
if (!s) \
return (type) -1; \
return i; \
return (type) u; \
return (type) -1; \
} \
bool fstype_is_network(const char *fstype);
int dir_is_empty(const char *path);
static inline int dir_is_populated(const char *path) {
int r;
r = dir_is_empty(path);
if (r < 0)
return r;
return !r;
}
typedef long statfs_f_type_t;
int fd_is_temporary_fs(int fd);
int running_in_chroot(void);
int null_or_empty_path(const char *fn);
int null_or_empty_fd(int fd);
bool plymouth_running(void);
int glob_exists(const char *path);
bool is_main_thread(void);
#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);
bool log_facility_unshifted_is_valid(int faciliy);
int log_level_to_string_alloc(int i, char **s);
int log_level_from_string(const char *s);
bool log_level_is_valid(int level);
int sched_policy_to_string_alloc(int i, char **s);
int sched_policy_from_string(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);
static inline void freep(void *p) {
free(*(void**) p);
}
umask(*u);
}
return NULL;
return malloc(a * b);
}
return NULL;
return realloc(p, a * b);
}
return NULL;
return memdup(p, a * b);
}
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 {
int on_ac_power(void);
memset(s, c, n);
return (uint8_t*)s + 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;
}
#define DECIMAL_STR_WIDTH(x) \
({ \
unsigned ans = 1; \
while (_x_ /= 10) \
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_; \
})
bool id128_is_valid(const char *s) _pure_;
int shall_restore_state(void);
/**
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
if (nmemb <= 1)
return;
}
int proc_cmdline(char **ret);
int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
#ifndef PERSONALITY_INVALID
/* personality(7) documents that 0xffffffffUL is used for querying the
* current personality, hence let's use that here as error
* indicator. */
#define PERSONALITY_INVALID 0xffffffffLU
#endif
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 is_symlink(const char *path);
int is_device_node(const char *path);
union inotify_event_buffer {
struct inotify_event ev;
};
int version(void);
bool fdname_is_valid(const char *s);
bool oom_score_adjust_is_valid(int oa);