util.h revision 4d0d3d41d21f34f28ee67b8b1952af88ada7abaf
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann This file is part of systemd.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann Copyright 2010 Lennart Poettering
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann systemd is free software; you can redistribute it and/or modify it
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann under the terms of the GNU Lesser General Public License as published by
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann the Free Software Foundation; either version 2.1 of the License, or
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (at your option) any later version.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann systemd is distributed in the hope that it will be useful, but
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann WITHOUT ANY WARRANTY; without even the implied warranty of
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann Lesser General Public License for more details.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann You should have received a copy of the GNU Lesser General Public License
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann along with systemd; If not, see <http://www.gnu.org/licenses/>.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* What is interpreted as whitespace? */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define new0(t, n) ((t*) calloc((n), sizeof(t)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline const char* yes_no(bool b) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline const char* true_false(bool b) {
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmannstatic inline const char* one_zero(bool b) {
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmannint parse_size(const char *t, uint64_t base, uint64_t *size);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atou(const char *s, unsigned *ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atollu(const char *s, unsigned long long *ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atolli(const char *s, long long int *ret_i);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(unsigned long) == sizeof(unsigned));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atoli(const char *s, long int *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(long int) == sizeof(int));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atollu(s, (unsigned long long*) ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atoli(const char *s, long int *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(long int) == sizeof(long long int));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atolli(s, (long long int*) ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(uint32_t) == sizeof(unsigned));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann return safe_atollu(s, (unsigned long long*) ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(int64_t) == sizeof(long long int));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atolli(s, (long long int*) ret_i);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atou16(const char *s, uint16_t *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint readlinkat_malloc(int fd, const char *p, char **ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint readlink_malloc(const char *p, char **r);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint readlink_value(const char *p, char **ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint readlink_and_make_absolute(const char *p, char **r);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint readlink_and_canonicalize(const char *p, char **r);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *file_in_same_dir(const char *path, const char *filename);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint rmdir_parents(const char *path, const char *stop);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool dirent_is_file(const struct dirent *de) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool hidden_file(const char *filename) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* For basic lookup tables with strictly enumerated entries */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann scope const char *name##_to_string(type i) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann scope type name##_from_string(const char *s) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann struct __useless_struct_to_allow_trailing_semicolon__
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* For string conversions where numbers are also acceptable */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann int name##_to_string_alloc(type i, char **str) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (i < 0 || i > max) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann unsigned u = 0; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return (type) u; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann struct __useless_struct_to_allow_trailing_semicolon__
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int dir_is_populated(const char *path) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_check_fstype(int fd, statfs_f_type_t magic_value);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint path_check_fstype(const char *path, statfs_f_type_t magic_value);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool is_temporary_fs(const struct statfs *s) _pure_;
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint files_same(const char *filea, const char *fileb);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David HerrmannDIR *xopendirat(int dirfd, const char *name, int flags);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannchar *fstab_node_to_udev_node(const char *p);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint symlink_idempotent(const char *from, const char *to);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint symlink_atomic(const char *from, const char *to);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint mknod_atomic(const char *path, mode_t mode, dev_t dev);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint mkfifo_atomic(const char *path, mode_t mode);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool display_is_local(const char *display) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint socket_from_display(const char *display, char **path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint glob_extend(char ***strv, const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint dirent_ensure_type(DIR *d, struct dirent *de);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint get_files_in_directory(const char *path, char ***list);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint block_get_whole_disk(dev_t d, dev_t *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann#define NULSTR_FOREACH_PAIR(i, j, l) \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint ioprio_class_to_string_alloc(int i, char **s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint ioprio_class_from_string(const char *s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *sigchld_code_to_string(int i) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint sigchld_code_from_string(const char *s) _pure_;
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint log_facility_unshifted_to_string_alloc(int i, char **s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint log_facility_unshifted_from_string(const char *s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool log_facility_unshifted_is_valid(int faciliy);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint log_level_to_string_alloc(int i, char **s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint log_level_from_string(const char *s);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint sched_policy_to_string_alloc(int i, char **s);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint sched_policy_from_string(const char *s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *format_bytes(char *buf, size_t l, uint64_t t);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid* memdup(const void *p, size_t l) _alloc_(2);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint setrlimit_closest(int resource, const struct rlimit *rlim);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool http_url_is_valid(const char *url) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool documentation_url_is_valid(const char *url) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void freep(void *p) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann free(*(void**) p);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid HerrmannDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _cleanup_globfree_ _cleanup_(globfree)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _cleanup_endmntent_ _cleanup_(endmntentp)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return malloc(a * b);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return realloc(p, a * b);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return memdup(p, a * b);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * Check if a string contains any glob patterns.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann_pure_ static inline bool string_is_glob(const char *p) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann int (*compar) (const void *, const void *, void *),
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *draw_special_char(DrawSpecialChar ch);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmannstatic inline void *mempset(void *s, int c, size_t n) {
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann return (uint8_t*)s + n;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unhexmem(const char *p, size_t l, void **mem, size_t *len);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *base32hexmem(const void *p, size_t l, bool padding);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *len);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unbase64mem(const char *p, size_t l, void **mem, size_t *len);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define GREEDY_REALLOC(array, allocated, need) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define GREEDY_REALLOC0(array, allocated, need) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void _reset_errno_(int *saved_errno) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmannstatic inline int negative_errno(void) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann /* This helper should be used to shut up gcc if you know 'errno' is
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * negative. Instead of "return -errno;", use "return negative_errno();"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * It will suppress bogus gcc warnings in case it assumes 'errno' might
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * be 0 and thus the caller's error-handling might not be triggered. */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void _reset_umask_(struct _umask_struct_ *s) {
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline unsigned log2i(int x) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmannstatic inline unsigned log2u(unsigned x) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline unsigned log2u_round_up(unsigned x) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * Normal qsort requires base to be nonnull. Here were require
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * that only if nmemb > 0.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint get_proc_cmdline_key(const char *parameter, char **value);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint container_get_leader(const char *machine, pid_t *pid);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint open_tmpfile(const char *path, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_warn_permissions(const char *path, int fd);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* personality(7) documents that 0xffffffffUL is used for querying the
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * current personality, hence let's use that here as error
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * indicator. */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannunsigned long personality_from_string(const char *p);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *personality_to_string(unsigned long);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid hexdump(FILE *f, const void *p, size_t s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint update_reboot_param_file(const char *param);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint umount_recursive(const char *target, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint bind_remount_recursive(const char *prefix, bool ro);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint tempfn_xxxxxx(const char *p, const char *extra, char **ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint tempfn_random(const char *p, const char *extra, char **ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint tempfn_random_child(const char *p, const char *extra, char **ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint path_getcrtime(const char *p, usec_t *usec);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint chattr_fd(int fd, unsigned value, unsigned mask);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint chattr_path(const char *p, unsigned value, unsigned mask);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint read_attr_path(const char *p, unsigned *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint syslog_parse_priority(const char **p, int *priority, bool with_facility);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fgetxattr_malloc(int fd, const char *name, char **value);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool fdname_is_valid(const char *s);