util.h revision 4d0d3d41d21f34f28ee67b8b1952af88ada7abaf
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#pragma once
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/***
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann This file is part of systemd.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann Copyright 2010 Lennart Poettering
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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
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
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***/
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <alloca.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <dirent.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <fcntl.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <inttypes.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <limits.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <locale.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <mntent.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <stdarg.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <stdbool.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <stddef.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <stdio.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <stdlib.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <sys/inotify.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <sys/socket.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <sys/stat.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <sys/statfs.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <sys/types.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <time.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include <unistd.h>
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include "formats-util.h"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include "macro.h"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include "missing.h"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#include "time-util.h"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* What is interpreted as whitespace? */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define WHITESPACE " \t\n\r"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define NEWLINE "\n\r"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define QUOTES "\"\'"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define COMMENTS "#;"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define GLOB_CHARS "*?["
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FORMAT_BYTES_MAX 8
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannsize_t page_size(void) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define new0(t, n) ((t*) calloc((n), sizeof(t)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define malloc0(n) (calloc(1, (n)))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void *mfree(void *memory) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann free(memory);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return NULL;
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline const char* yes_no(bool b) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return b ? "yes" : "no";
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline const char* true_false(bool b) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return b ? "true" : "false";
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmann
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmannstatic inline const char* one_zero(bool b) {
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmann return b ? "1" : "0";
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmann
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmannint parse_size(const char *t, uint64_t base, uint64_t *size);
e23f4bb525991c5908be0d0e7f8374c964d9996cDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint parse_boolean(const char *v) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint parse_pid(const char *s, pid_t* ret_pid);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atou(const char *s, unsigned *ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atoi(const char *s, int *ret_i);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atod(const char *s, double *ret_d);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atou8(const char *s, uint8_t *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#if LONG_MAX == INT_MAX
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(unsigned long) == sizeof(unsigned));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atou(s, (unsigned*) ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atoli(const char *s, long int *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(long int) == sizeof(int));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atoi(s, (int*) ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#else
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 Herrmann}
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 Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#endif
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(uint32_t) == sizeof(unsigned));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atou(s, (unsigned*) ret_u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert_cc(sizeof(int32_t) == sizeof(int));
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return safe_atoi(s, (int*) ret_i);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atou16(const char *s, uint16_t *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint safe_atoi16(const char *s, int16_t *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *file_in_same_dir(const char *path, const char *filename);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint rmdir_parents(const char *path, const char *stop);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar hexchar(int x) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unhexchar(char c) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar octchar(int x) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unoctchar(char c) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar decchar(int x) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint undecchar(char c) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar base32hexchar(int x) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unbase32hexchar(char c) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar base64char(int x) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unbase64char(char c) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool hidden_file(const char *filename) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann return NULL; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return name##_table[i]; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann }
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 }
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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
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
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 char *s; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (i < 0 || i > max) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return -ERANGE; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (i < (type) ELEMENTSOF(name##_table)) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann s = strdup(name##_table[i]); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (!s) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return -ENOMEM; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } else { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (asprintf(&s, "%i", i) < 0) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return -ENOMEM; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann *str = s; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return 0; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann type name##_from_string(const char *s) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann type i; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann unsigned u = 0; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (!s) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return (type) -1; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (streq_ptr(name##_table[i], s)) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return i; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (safe_atou(s, &u) >= 0 && u <= max) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return (type) u; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return (type) -1; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann struct __useless_struct_to_allow_trailing_semicolon__
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool fstype_is_network(const char *fstype);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool is_device_path(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint dir_is_empty(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar* dirname_malloc(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline int dir_is_populated(const char *path) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann int r;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann r = dir_is_empty(path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (r < 0)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return r;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return !r;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmanntypedef long statfs_f_type_t;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool is_temporary_fs(const struct statfs *s) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_is_temporary_fs(int fd);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define xsprintf(buf, fmt, ...) \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann "xsprintf: " #buf "[] must be big enough")
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint files_same(const char *filea, const char *fileb);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint running_in_chroot(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint touch(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannnoreturn void freeze(void);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannbool null_or_empty(struct stat *st) _pure_;
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint null_or_empty_path(const char *fn);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint null_or_empty_fd(int fd);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David HerrmannDIR *xopendirat(int dirfd, const char *name, int flags);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannchar *fstab_node_to_udev_node(const char *p);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool plymouth_running(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint symlink_idempotent(const char *from, const char *to);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fchmod_umask(int fd, mode_t mode);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool display_is_local(const char *display) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint socket_from_display(const char *display, char **path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint glob_exists(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint glob_extend(char ***strv, const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint dirent_ensure_type(DIR *d, struct dirent *de);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint get_files_in_directory(const char *path, char ***list);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool is_main_thread(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint block_get_whole_disk(dev_t d, dev_t *ret);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define NULSTR_FOREACH(i, l) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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))
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint ioprio_class_to_string_alloc(int i, char **s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint ioprio_class_from_string(const char *s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *sigchld_code_to_string(int i) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint sigchld_code_from_string(const char *s) _pure_;
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
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);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint log_level_to_string_alloc(int i, char **s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint log_level_from_string(const char *s);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannbool log_level_is_valid(int level);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint sched_policy_to_string_alloc(int i, char **s);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint sched_policy_from_string(const char *s);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *rlimit_to_string(int i) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint rlimit_from_string(const char *s) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannextern int saved_argc;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannextern char **saved_argv;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool kexec_loaded(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint prot_from_flags(int flags) _const_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *format_bytes(char *buf, size_t l, uint64_t t);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid* memdup(const void *p, size_t l) _alloc_(2);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint setrlimit_closest(int resource, const struct rlimit *rlim);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool http_url_is_valid(const char *url) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool documentation_url_is_valid(const char *url) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool http_etag_is_valid(const char *etag);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool in_initrd(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void freep(void *p) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann free(*(void**) p);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void umaskp(mode_t *u) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann umask(*u);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid HerrmannDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _cleanup_free_ _cleanup_(freep)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _cleanup_umask_ _cleanup_(umaskp)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _cleanup_globfree_ _cleanup_(globfree)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _cleanup_endmntent_ _cleanup_(endmntentp)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 NULL;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return malloc(a * b);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 NULL;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return realloc(p, a * b);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 NULL;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return memdup(p, a * b);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool filename_is_valid(const char *p) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool path_is_safe(const char *p) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool string_is_safe(const char *p) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/**
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * Check if a string contains any glob patterns.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann_pure_ static inline bool string_is_glob(const char *p) {
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann return !!strpbrk(p, GLOB_CHARS);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann void *arg);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define _(String) gettext (String)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define N_(String) String
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid init_gettext(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool is_locale_utf8(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmanntypedef enum DrawSpecialChar {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_TREE_VERTICAL,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_TREE_BRANCH,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_TREE_RIGHT,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_TREE_SPACE,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_TRIANGULAR_BULLET,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_BLACK_CIRCLE,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_ARROW,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann DRAW_DASH,
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann _DRAW_SPECIAL_CHAR_MAX
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann} DrawSpecialChar;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *draw_special_char(DrawSpecialChar ch);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint on_ac_power(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FOREACH_LINE(line, f, on_error) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (;;) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (!fgets(line, sizeof(line), f)) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (ferror(f)) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann on_error; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann break; \
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann } else
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FOREACH_DIRENT(de, d, on_error) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (!de) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (errno > 0) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann on_error; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann break; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } else if (hidden_file((de)->d_name)) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann continue; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann else
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann#define FOREACH_DIRENT_ALL(de, d, on_error) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (!de) { \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (errno > 0) { \
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann on_error; \
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann } \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann break; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann } else
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmannstatic inline void *mempset(void *s, int c, size_t n) {
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann memset(s, c, n);
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann return (uint8_t*)s + n;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *hexmem(const void *p, size_t l);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unhexmem(const char *p, size_t l, void **mem, size_t *len);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannchar *base64mem(const void *p, size_t l);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint unbase64mem(const char *p, size_t l, void **mem, size_t *len);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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]))
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define GREEDY_REALLOC0(array, allocated, need) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void _reset_errno_(int *saved_errno) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann errno = *saved_errno;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann assert_return(errno > 0, -EINVAL);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return -errno;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstruct _umask_struct_ {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann mode_t mask;
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann bool quit;
05bae4a60c32e29797597979cee2f3684eb3bc1eDavid Herrmann};
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void _reset_umask_(struct _umask_struct_ *s) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann umask(s->mask);
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann};
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann#define RUN_WITH_UMASK(mask) \
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann !_saved_umask_.quit ; \
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann _saved_umask_.quit = true)
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmannstatic inline unsigned u64log2(uint64_t n) {
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann#if __SIZEOF_LONG_LONG__ == 8
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann#else
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann#error "Wut?"
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann#endif
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann}
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmannstatic inline unsigned u32ctz(uint32_t n) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#if __SIZEOF_INT__ == 4
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return __builtin_ctz(n);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#else
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#error "Wut?"
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#endif
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline unsigned log2i(int x) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert(x > 0);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmann
7447362c530e3f7128f16a35d1e43da4251144ccDavid Herrmannstatic inline unsigned log2u(unsigned x) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert(x > 0);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline unsigned log2u_round_up(unsigned x) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert(x > 0);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (x == 1)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return 0;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return log2u(x - 1) + 1;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann#define DECIMAL_STR_WIDTH(x) \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann ({ \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann typeof(x) _x_ = (x); \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann unsigned ans = 1; \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann while (_x_ /= 10) \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann ans++; \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann ans; \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann })
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmannint unlink_noerrno(const char *path);
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann#define alloca0(n) \
c4bc1a8434f2a34840ea6f63064fa998ecfae738David Herrmann ({ \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann char *_new_; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann size_t _len_ = n; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann _new_ = alloca(_len_); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (void *) memset(_new_, 0, _len_); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann })
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define alloca_align(size, align) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann ({ \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann void *_ptr_; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann size_t _mask_ = (align) - 1; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann _ptr_ = alloca((size) + _mask_); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann })
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define alloca0_align(size, align) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann ({ \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann void *_new_; \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann size_t _size_ = (size); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann _new_ = alloca_align(_size_, (align)); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann (void*)memset(_new_, 0, _size_); \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann })
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool id128_is_valid(const char *s) _pure_;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint shall_restore_state(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann/**
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * Normal qsort requires base to be nonnull. Here were require
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann * that only if nmemb > 0.
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann */
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannstatic inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann if (nmemb <= 1)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann return;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann assert(base);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann qsort(base, nmemb, size, compar);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann}
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint proc_cmdline(char **ret);
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint container_get_leader(const char *machine, pid_t *pid);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint mkostemp_safe(char *pattern, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint open_tmpfile(const char *path, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_warn_permissions(const char *path, int fd);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#ifndef PERSONALITY_INVALID
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 Herrmann#define PERSONALITY_INVALID 0xffffffffLU
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#endif
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannunsigned long personality_from_string(const char *p);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannconst char *personality_to_string(unsigned long);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannuint64_t physical_memory(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid hexdump(FILE *f, const void *p, size_t s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannunion file_handle_union {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann struct file_handle handle;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann};
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint update_reboot_param_file(const char *param);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint umount_recursive(const char *target, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint bind_remount_recursive(const char *prefix, bool ro);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fflush_and_check(FILE *f);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint take_password_lock(const char *root);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint is_symlink(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint is_dir(const char *path, bool follow);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint is_device_node(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann for ((e) = &buffer.ev; \
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
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannunion inotify_event_buffer {
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann struct inotify_event ev;
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann uint8_t raw[INOTIFY_EVENT_MAX];
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann};
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_setcrtime(int fd, usec_t usec);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint fd_getcrtime(int fd, usec_t *usec);
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint chattr_fd(int fd, unsigned value, unsigned mask);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint chattr_path(const char *p, unsigned value, unsigned mask);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint read_attr_fd(int fd, unsigned *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint read_attr_path(const char *p, unsigned *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint syslog_parse_priority(const char **p, int *priority, bool with_facility);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint parse_mode(const char *s, mode_t *ret);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint mount_move_root(const char *path);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
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 Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannvoid nop_signal_handler(int sig);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannint version(void);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool fdname_is_valid(const char *s);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmannbool oom_score_adjust_is_valid(int oa);
c0395aeb903cde25bd9e81fba3334f63335fe0efDavid Herrmann