util.h revision 1a29929959fd8f59e19ce60c25d1a1f7d910fac0
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#pragma once
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/***
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen This file is part of systemd.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Copyright 2010 Lennart Poettering
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen systemd is free software; you can redistribute it and/or modify it
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen under the terms of the GNU Lesser General Public License as published by
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (at your option) any later version.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen systemd is distributed in the hope that it will be useful, but
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen Lesser General Public License for more details.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen You should have received a copy of the GNU Lesser General Public License
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen***/
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <alloca.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <fcntl.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <inttypes.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <time.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/time.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdarg.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdbool.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdlib.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdio.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <signal.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sched.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <limits.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/types.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/stat.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <dirent.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/resource.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stddef.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <unistd.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <locale.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <mntent.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/socket.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if SIZEOF_PID_T == 4
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define PID_FMT "%" PRIu32
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#elif SIZEOF_PID_T == 2
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define PID_FMT "%" PRIu16
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# error Unknown pid_t size
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if SIZEOF_UID_T == 4
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define UID_FMT "%" PRIu32
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#elif SIZEOF_UID_T == 2
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define UID_FMT "%" PRIu16
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# error Unknown uid_t size
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if SIZEOF_GID_T == 4
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define GID_FMT "%" PRIu32
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#elif SIZEOF_GID_T == 2
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define GID_FMT "%" PRIu16
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# error Unknown gid_t size
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if SIZEOF_TIME_T == 8
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define PRI_TIME PRIu64
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#elif SIZEOF_TIME_T == 4
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define PRI_TIME PRIu32
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# error Unknown time_t size
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if SIZEOF_RLIM_T == 8
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define RLIM_FMT "%" PRIu64
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#elif SIZEOF_RLIM_T == 4
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# define RLIM_FMT "%" PRIu32
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen# error Unknown rlim_t size
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "macro.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "missing.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "time-util.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* What is interpreted as whitespace? */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define WHITESPACE " \t\n\r"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define NEWLINE "\n\r"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define QUOTES "\"\'"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define COMMENTS "#;"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define GLOB_CHARS "*?["
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* What characters are special in the shell? */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* must be escaped outside and inside double-quotes */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SHELL_NEED_ESCAPE "\"\\`$"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* can be escaped or double-quoted */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FORMAT_BYTES_MAX 8
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_RED_ON "\x1B[31m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_GREEN_ON "\x1B[32m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersensize_t page_size(void) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define streq(a,b) (strcmp((a),(b)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool streq_ptr(const char *a, const char *b) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define new0(t, n) ((t*) calloc((n), sizeof(t)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define malloc0(n) (calloc((n), 1))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* yes_no(bool b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return b ? "yes" : "no";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* true_false(bool b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return b ? "true" : "false";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* strempty(const char *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s ? s : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char* strnull(const char *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s ? s : "(null)";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *strna(const char *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s ? s : "n/a";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline bool isempty(const char *p) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return !p || !p[0];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline char *startswith(const char *s, const char *prefix) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen l = strlen(prefix);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (strncmp(s, prefix, l) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (char*) s + l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline char *startswith_no_case(const char *s, const char *prefix) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen l = strlen(prefix);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (strncasecmp(s, prefix, l) == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (char*) s + l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *endswith(const char *s, const char *postfix) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *first_word(const char *s, const char *word) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint close_nointr(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_close(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid safe_close_pair(int p[]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid close_many(const int fds[], unsigned n_fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_size(const char *t, off_t base, off_t *size);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_boolean(const char *v) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_pid(const char *s, pid_t* ret_pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_uid(const char *s, uid_t* ret_uid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atou(const char *s, unsigned *ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atoi(const char *s, int *ret_i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atollu(const char *s, unsigned long long *ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atolli(const char *s, long long int *ret_i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atod(const char *s, double *ret_d);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atou8(const char *s, uint8_t *ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if LONG_MAX == INT_MAX
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(unsigned long) == sizeof(unsigned));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atou(s, (unsigned*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoli(const char *s, long int *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(long int) == sizeof(int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atoi(s, (int*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atollu(s, (unsigned long long*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoli(const char *s, long int *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(long int) == sizeof(long long int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atolli(s, (long long int*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(uint32_t) == sizeof(unsigned));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atou(s, (unsigned*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(int32_t) == sizeof(int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atoi(s, (int*) ret_i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atollu(s, (unsigned long long*) ret_u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_cc(sizeof(int64_t) == sizeof(long long int));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return safe_atolli(s, (long long int*) ret_i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atou16(const char *s, uint16_t *ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint safe_atoi16(const char *s, int16_t *ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char* split(const char **state, size_t *l, const char *separator, bool quoted);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_WORD(word, length, s, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _FOREACH_WORD(word, length, s, separator, false, state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_WORD_QUOTED(word, length, s, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_starttime_of_pid(pid_t pid, unsigned long long *st);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strappend(const char *s, const char *suffix);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strnappend(const char *s, const char *suffix, size_t length);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *replace_env(const char *format, char **env);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar **replace_env_argv(char **argv, char **env);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlinkat_malloc(int fd, const char *p, char **ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_malloc(const char *p, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_value(const char *p, char **ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_and_make_absolute(const char *p, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint readlink_and_canonicalize(const char *p, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint reset_all_signal_handlers(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint reset_signal_mask(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strstrip(char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *delete_chars(char *s, const char *bad);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *truncate_nl(char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *file_in_same_dir(const char *path, const char *filename);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rmdir_parents(const char *path, const char *stop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_state(pid_t pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_comm(pid_t pid, char **name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_exe(pid_t pid, char **name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_uid(pid_t pid, uid_t *uid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_gid(pid_t pid, gid_t *gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_capeff(pid_t pid, char **capeff);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_cwd(pid_t pid, char **cwd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_root(pid_t pid, char **root);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_process_environ(pid_t pid, char **environ);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar hexchar(int x) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unhexchar(char c) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar octchar(int x) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unoctchar(char c) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar decchar(int x) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint undecchar(char c) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cescape(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cunescape(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cunescape_length(const char *s, size_t length);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *xescape(const char *s, const char *bad);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *ascii_strlower(char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool dirent_is_file(const struct dirent *de) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool ignore_file(const char *filename) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool chars_intersect(const char *a, const char *b) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint make_stdio(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint make_null_stdio(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint make_console_stdio(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint dev_urandom(void *p, size_t n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid random_bytes(void *p, size_t n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid initialize_srand(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline uint64_t random_u64(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen uint64_t u;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen random_bytes(&u, sizeof(u));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return u;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline uint32_t random_u32(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen uint32_t u;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen random_bytes(&u, sizeof(u));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return u;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* For basic lookup tables with strictly enumerated entries */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen scope const char *name##_to_string(type i) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return NULL; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return name##_table[i]; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen scope type name##_from_string(const char *s) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen type i; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!s) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (type) -1; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (name##_table[i] && \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen streq(name##_table[i], s)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return i; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (type) -1; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* For string conversions where numbers are also acceptable */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int name##_to_string_alloc(type i, char **str) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *s; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int r; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i < 0 || i > max) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -ERANGE; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i < (type) ELEMENTSOF(name##_table)) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen s = strdup(name##_table[i]); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!s) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return log_oom(); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = asprintf(&s, "%u", i); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return log_oom(); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *str = s; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen type name##_from_string(const char *s) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen type i; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned u = 0; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(s); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (name##_table[i] && \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen streq(name##_table[i], s)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return i; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (safe_atou(s, &u) >= 0 && u <= max) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (type) u; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (type) -1; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_nonblock(int fd, bool nonblock);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_cloexec(int fd, bool cloexec);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint close_all_fds(const int except[], unsigned n_except);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool fstype_is_network(const char *fstype);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint chvt(int vt);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ask_string(char **ret, const char *text, ...) _printf_(2, 3);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint reset_terminal_fd(int fd, bool switch_to_text);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint reset_terminal(const char *name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint open_terminal(const char *name, int mode);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint release_terminal(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint flush_fd(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ignore_signals(int sig, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint default_signals(int sig, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sigaction_many(const struct sigaction *sa, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool is_device_path(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint dir_is_empty(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* dirname_malloc(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid rename_process(const char name[8]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid sigset_add_many(sigset_t *ss, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sigprocmask_many(int how, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool hostname_is_set(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* lookup_uid(uid_t uid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* gethostname_malloc(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* getlogname_malloc(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* getusername_malloc(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint getttyname_malloc(int fd, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint getttyname_harder(int fd, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_ctty_devnr(pid_t pid, dev_t *d);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_ctty(pid_t, dev_t *_devnr, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint pipe_eof(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersencpu_set_t* cpu_set_malloc(unsigned *ncpus);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_columns(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunsigned columns(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_lines(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunsigned lines(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid columns_lines_cache_reset(int _unused_ signum);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool on_tty(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_red(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_green(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_yellow(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_blue(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline const char *ansi_highlight_off(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint files_same(const char *filea, const char *fileb);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint running_in_chroot(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *ellipsize(const char *s, size_t length, unsigned percent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* bytes columns */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint touch(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *unquote(const char *s, const char *quotes);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *normalize_env_assignment(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint wait_for_terminate(pid_t pid, siginfo_t *status);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint wait_for_terminate_and_warn(const char *name, pid_t pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersennoreturn void freeze(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool null_or_empty(struct stat *st) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint null_or_empty_path(const char *fn);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint null_or_empty_fd(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom GundersenDIR *xopendirat(int dirfd, const char *name, int flags);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *fstab_node_to_udev_node(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *resolve_dev_console(char **active);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool tty_is_vc(const char *tty);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool tty_is_vc_resolve(const char *tty);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool tty_is_console(const char *tty) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint vtnr_from_tty(const char *tty);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *default_term_for_tty(const char *tty);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid execute_directory(const char *directory, DIR *_d, usec_t timeout, char *argv[]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint kill_and_sigcont(pid_t pid, int sig);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool nulstr_contains(const char*nulstr, const char *needle);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool plymouth_running(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool hostname_is_valid(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* hostname_cleanup(char *s, bool lowercase);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool machine_name_is_valid(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* strshorten(char *s, size_t l);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint terminal_vhangup_fd(int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint terminal_vhangup(const char *name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint vt_disallocate(const char *name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint symlink_atomic(const char *from, const char *to);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint mknod_atomic(const char *path, mode_t mode, dev_t dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint mkfifo_atomic(const char *path, mode_t mode);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fchmod_umask(int fd, mode_t mode);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool display_is_local(const char *display) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint socket_from_display(const char *display, char **path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_group_creds(const char **groupname, gid_t *gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint in_gid(gid_t gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint in_group(const char *name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* uid_to_name(uid_t uid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* gid_to_name(gid_t gid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint glob_exists(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint glob_extend(char ***strv, const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint dirent_ensure_type(DIR *d, struct dirent *de);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_files_in_directory(const char *path, char ***list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strjoin(const char *x, ...) _sentinel_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool is_main_thread(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline bool _pure_ in_charset(const char *s, const char* charset) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(charset);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return s[strspn(s, charset)] == '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint block_get_whole_disk(dev_t d, dev_t *ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define NULSTR_FOREACH(i, l) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define NULSTR_FOREACH_PAIR(i, j, l) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ioprio_class_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ioprio_class_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *sigchld_code_to_string(int i) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sigchld_code_from_string(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_facility_unshifted_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_facility_unshifted_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_level_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint log_level_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sched_policy_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sched_policy_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *rlimit_to_string(int i) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint rlimit_from_string(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ip_tos_to_string_alloc(int i, char **s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint ip_tos_from_string(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *signal_to_string(int i) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint signal_from_string(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint signal_from_string_try_harder(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenextern int saved_argc;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenextern char **saved_argv;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool kexec_loaded(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint prot_from_flags(int flags) _const_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *format_bytes(char *buf, size_t l, off_t t);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_wait_for_event(int fd, int event, usec_t timeout);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid* memdup(const void *p, size_t l) _alloc_(2);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint is_kernel_thread(pid_t pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_inc_sndbuf(int fd, size_t n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_inc_rcvbuf(int fd, size_t n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint setrlimit_closest(int resource, const struct rlimit *rlim);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersenint getenv_for_pid(pid_t pid, const char *field, char **_value);
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersen
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersenbool is_valid_documentation_url(const char *url) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool in_initrd(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid warn_melody(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_home_dir(char **ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_shell(char **_ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void freep(void *p) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(*(void**) p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen static inline void func##p(type *p) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (*p) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen func(*p); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void closep(int *fd) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen safe_close(*fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void umaskp(mode_t *u) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen umask(*u);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void close_pairp(int (*p)[2]) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen safe_close_pair(*p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersen#define _cleanup_free_ _cleanup_(freep)
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersen#define _cleanup_close_ _cleanup_(closep)
aa20f49a1c5816e6e7e97f2e2ba209be47f3c0a3Tom Gundersen#define _cleanup_umask_ _cleanup_(umaskp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_globfree_ _cleanup_(globfree)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_fclose_ _cleanup_(fclosep)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_pclose_ _cleanup_(pclosep)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_closedir_ _cleanup_(closedirp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_endmntent_ _cleanup_(endmntentp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define _cleanup_close_pair_ _cleanup_(close_pairp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return malloc(a * b);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return realloc(p, a * b);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return memdup(p, a * b);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool filename_is_safe(const char *p) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool path_is_safe(const char *p) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool string_is_safe(const char *p) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool string_has_cc(const char *p, const char *ok) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/**
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Check if a string contains any glob patterns.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen_pure_ static inline bool string_is_glob(const char *p) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return !!strpbrk(p, GLOB_CHARS);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int (*compar) (const void *, const void *, void *),
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen void *arg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool is_locale_utf8(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersentypedef enum DrawSpecialChar {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_TREE_VERTICAL,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_TREE_BRANCH,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_TREE_RIGHT,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_TREE_SPACE,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_TRIANGULAR_BULLET,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_BLACK_CIRCLE,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_ARROW,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen DRAW_DASH,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _DRAW_SPECIAL_CHAR_MAX
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen} DrawSpecialChar;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *draw_special_char(DrawSpecialChar ch);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strreplace(const char *text, const char *old_string, const char *new_string);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strip_tab_ansi(char **p, size_t *l);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint on_ac_power(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_LINE(line, f, on_error) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (;;) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!fgets(line, sizeof(line), f)) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ferror(f)) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen on_error; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define FOREACH_DIRENT(de, d, on_error) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!de) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (errno > 0) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen on_error; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (ignore_file((de)->d_name)) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void *mempset(void *s, int c, size_t n) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(s, c, n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (uint8_t*)s + n;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *hexmem(const void *p, size_t l);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid *unhexmem(const char *p, size_t l);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strextend(char **x, ...) _sentinel_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *strrep(const char *s, unsigned n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define GREEDY_REALLOC(array, allocated, need) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define GREEDY_REALLOC0(array, allocated, need) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void _reset_errno_(int *saved_errno) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen errno = *saved_errno;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int negative_errno(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* This helper should be used to shut up gcc if you know 'errno' is
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * negative. Instead of "return -errno;", use "return negative_errno();"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * It will suppress bogus gcc warnings in case it assumes 'errno' might
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * be 0 and thus the caller's error-handling might not be triggered. */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_return(errno > 0, -EINVAL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -errno;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstruct _umask_struct_ {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen mode_t mask;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen bool quit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void _reset_umask_(struct _umask_struct_ *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen umask(s->mask);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define RUN_WITH_UMASK(mask) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !_saved_umask_.quit ; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _saved_umask_.quit = true)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline unsigned u64log2(uint64_t n) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if __SIZEOF_LONG_LONG__ == 8
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#error "Wut?"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline unsigned u32ctz(uint32_t n) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#if __SIZEOF_INT__ == 4
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return __builtin_ctz(n);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#error "Wut?"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline int log2i(int x) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(x > 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline unsigned log2u(unsigned x) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(x > 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline unsigned log2u_round_up(unsigned x) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(x > 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (x == 1)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return log2u(x - 1) + 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline bool logind_running(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return access("/run/systemd/seats/", F_OK) >= 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DECIMAL_STR_WIDTH(x) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen typeof(x) _x_ = (x); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned ans = 1; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while (_x_ /= 10) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ans++; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ans; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen })
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unlink_noerrno(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define alloca0(n) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *_new_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t _len_ = n; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _new_ = alloca(_len_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (void *) memset(_new_, 0, _len_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen })
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define alloca_align(size, align) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen void *_ptr_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t _mask_ = (align) - 1; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _ptr_ = alloca((size) + _mask_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen })
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define alloca0_align(size, align) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen void *_new_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t _size_ = (size); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _new_ = alloca_align(_size_, (align)); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (void*)memset(_new_, 0, _size_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen })
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define strappenda(a, ...) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int _len = strlen(a); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned _i; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *_d_, *_p_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *_appendees_[] = { __VA_ARGS__ }; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _len += strlen(_appendees_[_i]); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _d_ = alloca(_len + 1); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _p_ = stpcpy(_d_, a); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _p_ = stpcpy(_p_, _appendees_[_i]); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _d_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen })
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define procfs_file_alloca(pid, field) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid_t _pid_ = (pid); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *_r_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_pid_ == 0) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _r_ = ("/proc/self/" field); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _r_; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen })
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstruct _locale_struct_ {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen locale_t saved_locale;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen locale_t new_locale;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen bool quit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void _reset_locale_(struct _locale_struct_ *s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen PROTECT_ERRNO;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (s->saved_locale != (locale_t) 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen uselocale(s->saved_locale);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (s->new_locale != (locale_t) 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen freelocale(s->new_locale);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define RUN_WITH_LOCALE(mask, loc) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ({ \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!_saved_locale_.quit) { \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen PROTECT_ERRNO; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _saved_locale_.new_locale = newlocale((mask), (loc), (locale_t) 0); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (_saved_locale_.new_locale != (locale_t) 0) \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _saved_locale_.saved_locale = uselocale(_saved_locale_.new_locale); \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !_saved_locale_.quit; }) ; \
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _saved_locale_.quit = true)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool id128_is_valid(const char *s) _pure_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint split_pair(const char *s, const char *sep, char **l, char **r);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint shall_restore_state(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/**
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Normal qsort requires base to be nonnull. Here were require
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * that only if nmemb > 0.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline void qsort_safe(void *base, size_t nmemb, size_t size,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int (*compar)(const void *, const void *)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (nmemb) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(base);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen qsort(base, nmemb, size, compar);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint proc_cmdline(char **ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint get_proc_cmdline_key(const char *parameter, char **value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint container_get_leader(const char *machine, pid_t *pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool pid_is_alive(pid_t pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool pid_is_unwaited(pid_t pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint getpeercred(int fd, struct ucred *ucred);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint getpeersec(int fd, char **ret);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint writev_safe(int fd, const struct iovec *w, int j);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint mkostemp_safe(char *pattern, int flags);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint open_tmpfile(const char *path, int flags);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fd_warn_permissions(const char *path, int fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunsigned long personality_from_string(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenconst char *personality_to_string(unsigned long);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenuint64_t physical_memory(void);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar* mount_test_option(const char *haystack, const char *needle);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenvoid hexdump(FILE *f, const void *p, size_t s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenunion file_handle_union {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct file_handle handle;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint update_reboot_param_file(const char *param);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint umount_recursive(const char *target, int flags);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint bind_remount_recursive(const char *prefix, bool ro);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint fflush_and_check(FILE *f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *tempfn_xxxxxx(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenchar *tempfn_random(const char *p);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenbool is_localhost(const char *hostname);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint take_password_lock(const char *root);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint is_symlink(const char *path);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint is_dir(const char *path, bool follow);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unquote_first_word(const char **p, char **ret, bool relax);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint unquote_many_words(const char **p, ...) _sentinel_;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint free_and_strdup(char **p, const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint sethostname_idempotent(const char *s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen