util.h revision fdb9161cd3e1a64eb9a653a6bf69596670d6e942
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#pragma once
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/***
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering This file is part of systemd.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Copyright 2010 Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is free software; you can redistribute it and/or modify it
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering under the terms of the GNU Lesser General Public License as published by
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (at your option) any later version.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is distributed in the hope that it will be useful, but
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Lesser General Public License for more details.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering You should have received a copy of the GNU Lesser General Public License
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering***/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <alloca.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <inttypes.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <time.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <sys/time.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdarg.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdbool.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdlib.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdio.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <signal.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <sched.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <limits.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <sys/types.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <sys/stat.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <dirent.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <sys/resource.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stddef.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <unistd.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <locale.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <mntent.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <sys/socket.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#if SIZEOF_PID_T == 4
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering# define PID_FMT "%" PRIu32
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#elif SIZEOF_PID_T == 2
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering# define PID_FMT "%" PRIu16
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#else
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering# error Unknown pid_t size
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#endif
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering#if SIZEOF_UID_T == 4
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering# define UID_FMT "%" PRIu32
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering#elif SIZEOF_UID_T == 2
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering# define UID_FMT "%" PRIu16
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#else
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering# error Unknown uid_t size
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#endif
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering#include "macro.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "time-util.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/* What is interpreted as whitespace? */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define WHITESPACE " \t\n\r"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define NEWLINE "\n\r"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define QUOTES "\"\'"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define COMMENTS "#;"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define GLOB_CHARS "*?["
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define FORMAT_BYTES_MAX 8
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_RED_ON "\x1B[31m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_GREEN_ON "\x1B[32m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringsize_t page_size(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define streq(a,b) (strcmp((a),(b)) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool streq_ptr(const char *a, const char *b) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define new0(t, n) ((t*) calloc((n), sizeof(t)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define malloc0(n) (calloc((n), 1))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char* yes_no(bool b) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return b ? "yes" : "no";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char* true_false(bool b) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return b ? "true" : "false";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char* strempty(const char *s) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return s ? s : "";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char* strnull(const char *s) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return s ? s : "(null)";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *strna(const char *s) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return s ? s : "n/a";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline bool isempty(const char *p) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return !p || !p[0];
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringstatic inline const char *startswith(const char *s, const char *prefix) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (strncmp(s, prefix, strlen(prefix)) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return s + strlen(prefix);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *startswith_no_case(const char *s, const char *prefix) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (strncasecmp(s, prefix, strlen(prefix)) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return s + strlen(prefix);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *endswith(const char *s, const char *postfix) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool first_word(const char *s, const char *word) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint close_nointr(int fd);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid close_nointr_nofail(int fd);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid close_many(const int fds[], unsigned n_fd);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint parse_boolean(const char *v) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint parse_bytes(const char *t, off_t *bytes);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint parse_pid(const char *s, pid_t* ret_pid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint parse_uid(const char *s, uid_t* ret_uid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint safe_atou(const char *s, unsigned *ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint safe_atoi(const char *s, int *ret_i);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint safe_atollu(const char *s, unsigned long long *ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint safe_atolli(const char *s, long long int *ret_i);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint safe_atod(const char *s, double *ret_d);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#if __WORDSIZE == 32
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(unsigned long) == sizeof(unsigned));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atou(s, (unsigned*) ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atoli(const char *s, long int *ret_u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(long int) == sizeof(int));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atoi(s, (int*) ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#else
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atollu(s, (unsigned long long*) ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atoli(const char *s, long int *ret_u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(long int) == sizeof(long long int));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atolli(s, (long long int*) ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#endif
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(uint32_t) == sizeof(unsigned));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atou(s, (unsigned*) ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(int32_t) == sizeof(int));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atoi(s, (int*) ret_i);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atollu(s, (unsigned long long*) ret_u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(int64_t) == sizeof(long long int));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return safe_atolli(s, (long long int*) ret_i);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *split(const char *c, size_t *l, const char *separator, bool quoted, char **state);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define FOREACH_WORD(word, length, s, state) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _FOREACH_WORD(word, length, s, separator, false, state)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define FOREACH_WORD_QUOTED(word, length, s, state) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define FOREACH_WORD_SEPARATOR_QUOTED(word, length, s, separator, state) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _FOREACH_WORD(word, length, s, separator, true, state)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), (separator), (quoted), &(state)); (word); (word) = split((s), &(length), (separator), (quoted), &(state)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_starttime_of_pid(pid_t pid, unsigned long long *st);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strappend(const char *s, const char *suffix);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strnappend(const char *s, const char *suffix, size_t length);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *replace_env(const char *format, char **env);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **replace_env_argv(char **argv, char **env);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint readlink_malloc(const char *p, char **r);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint readlink_and_make_absolute(const char *p, char **r);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint readlink_and_canonicalize(const char *p, char **r);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint reset_all_signal_handlers(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strstrip(char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *delete_chars(char *s, const char *bad);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *truncate_nl(char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *file_in_same_dir(const char *path, const char *filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint rmdir_parents(const char *path, const char *stop);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_process_comm(pid_t pid, char **name);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_process_exe(pid_t pid, char **name);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_process_uid(pid_t pid, uid_t *uid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_process_gid(pid_t pid, gid_t *gid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_process_capeff(pid_t pid, char **capeff);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar hexchar(int x) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint unhexchar(char c) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar octchar(int x) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint unoctchar(char c) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar decchar(int x) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint undecchar(char c) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *cescape(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *cunescape(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *cunescape_length(const char *s, size_t length);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *xescape(const char *s, const char *bad);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *ascii_strlower(char *path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool dirent_is_file(const struct dirent *de) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool ignore_file(const char *filename) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool chars_intersect(const char *a, const char *b) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint make_stdio(int fd);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekint make_null_stdio(void);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekint make_console_stdio(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint dev_urandom(void *p, size_t n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid random_bytes(void *p, size_t n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline uint64_t random_u64(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint64_t u;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering random_bytes(&u, sizeof(u));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return u;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline uint32_t random_u32(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint32_t u;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering random_bytes(&u, sizeof(u));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return u;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/* For basic lookup tables with strictly enumerated entries */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering scope const char *name##_to_string(type i) { \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return NULL; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return name##_table[i]; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering scope type name##_from_string(const char *s) { \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering type i; \
7850b3b83791ba0e2377ba40383c5abc258b839dKay Sievers if (!s) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return (type) -1; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (name##_table[i] && \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering streq(name##_table[i], s)) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return i; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return (type) -1; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/* For string conversions where numbers are also acceptable */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek int name##_to_string_alloc(type i, char **str) { \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek char *s; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (i < 0 || i > max) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -ERANGE; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (i < (type) ELEMENTSOF(name##_table)) { \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering s = strdup(name##_table[i]); \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!s) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return log_oom(); \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } else { \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = asprintf(&s, "%u", i); \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return log_oom(); \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *str = s; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering type name##_from_string(const char *s) { \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering type i; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned u = 0; \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek assert(s); \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek if (name##_table[i] && \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek streq(name##_table[i], s)) \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek return i; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (safe_atou(s, &u) >= 0 && u <= max) \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek return (type) u; \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return (type) -1; \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek } \
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek struct __useless_struct_to_allow_trailing_semicolon__
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_nonblock(int fd, bool nonblock);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_cloexec(int fd, bool cloexec);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint close_all_fds(const int except[], unsigned n_except);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringbool fstype_is_network(const char *fstype);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint chvt(int vt);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekint ask(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint reset_terminal_fd(int fd, bool switch_to_text);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint reset_terminal(const char *name);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint open_terminal(const char *name, int mode);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint release_terminal(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint flush_fd(int fd);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint ignore_signals(int sig, ...);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekint default_signals(int sig, ...);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint sigaction_many(const struct sigaction *sa, ...);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint close_pipe(int p[]);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool is_device_path(const char *path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint dir_is_empty(const char *path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar* dirname_malloc(const char *path);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekvoid rename_process(const char name[8]);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid sigset_add_many(sigset_t *ss, ...);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool hostname_is_set(void);
943aad8ca57a6b5c49c4ea60f9e8c13bf9b20e6cZbigniew Jędrzejewski-Szmek
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekchar* gethostname_malloc(void);
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmekchar* getlogname_malloc(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar* getusername_malloc(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint getttyname_malloc(int fd, char **r);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint getttyname_harder(int fd, char **r);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_ctty_devnr(pid_t pid, dev_t *d);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_ctty(pid_t, dev_t *_devnr, char **r);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint pipe_eof(int fd);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringcpu_set_t* cpu_set_malloc(unsigned *ncpus);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint status_welcome(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_columns(int fd);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringunsigned columns(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_lines(int fd);
4ec24515daa780118709e04cd78dae93f25a8428Zbigniew Jędrzejewski-Szmekunsigned lines(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid columns_lines_cache_reset(int _unused_ signum);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool on_tty(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *ansi_highlight(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_ON : "";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *ansi_highlight_red(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *ansi_highlight_green(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *ansi_highlight_yellow(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic inline const char *ansi_highlight_blue(void) {
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringstatic inline const char *ansi_highlight_off(void) {
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint running_in_chroot(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *ellipsize(const char *s, size_t length, unsigned percent);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* bytes columns */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint touch(const char *path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *unquote(const char *s, const char *quotes);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *normalize_env_assignment(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint wait_for_terminate(pid_t pid, siginfo_t *status);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint wait_for_terminate_and_warn(const char *name, pid_t pid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringnoreturn void freeze(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool null_or_empty(struct stat *st) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint null_or_empty_path(const char *fn);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart PoetteringDIR *xopendirat(int dirfd, const char *name, int flags);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *fstab_node_to_udev_node(const char *p);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringchar *resolve_dev_console(char **active);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool tty_is_vc(const char *tty);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool tty_is_vc_resolve(const char *tty);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool tty_is_console(const char *tty) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint vtnr_from_tty(const char *tty);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringconst char *default_term_for_tty(const char *tty);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringvoid execute_directory(const char *directory, DIR *_d, char *argv[]);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint kill_and_sigcont(pid_t pid, int sig);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool nulstr_contains(const char*nulstr, const char *needle);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool plymouth_running(void);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool hostname_is_valid(const char *s) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar* hostname_cleanup(char *s, bool lowercase);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar* strshorten(char *s, size_t l);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint terminal_vhangup_fd(int fd);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringint terminal_vhangup(const char *name);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poetteringint vt_disallocate(const char *name);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint copy_file(const char *from, const char *to, int flags);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint symlink_atomic(const char *from, const char *to);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fchmod_umask(int fd, mode_t mode);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool display_is_local(const char *display) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint socket_from_display(const char *display, char **path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_group_creds(const char **groupname, gid_t *gid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint in_gid(gid_t gid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint in_group(const char *name);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar* uid_to_name(uid_t uid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar* gid_to_name(gid_t gid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint glob_exists(const char *path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint glob_extend(char ***strv, const char *path);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint dirent_ensure_type(DIR *d, struct dirent *de);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint in_search_path(const char *path, char **search);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_files_in_directory(const char *path, char ***list);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strjoin(const char *x, ...) _sentinel_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool is_main_thread(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool in_charset(const char *s, const char* charset) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint block_get_whole_disk(dev_t d, dev_t *ret);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint file_is_priv_sticky(const char *p);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint strdup_or_null(const char *a, char **b);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define NULSTR_FOREACH(i, l) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define NULSTR_FOREACH_PAIR(i, j, l) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint ioprio_class_to_string_alloc(int i, char **s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint ioprio_class_from_string(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringconst char *sigchld_code_to_string(int i) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint sigchld_code_from_string(const char *s) _pure_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint log_facility_unshifted_to_string_alloc(int i, char **s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint log_facility_unshifted_from_string(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint log_level_to_string_alloc(int i, char **s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint log_level_from_string(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint sched_policy_to_string_alloc(int i, char **s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint sched_policy_from_string(const char *s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringconst char *rlimit_to_string(int i) _const_;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint rlimit_from_string(const char *s) _pure_;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekint ip_tos_to_string_alloc(int i, char **s);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekint ip_tos_from_string(const char *s);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekconst char *signal_to_string(int i) _const_;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekint signal_from_string(const char *s) _pure_;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekint signal_from_string_try_harder(const char *s);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekextern int saved_argc;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekextern char **saved_argv;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekbool kexec_loaded(void);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekint prot_from_flags(int flags) _const_;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekchar *format_bytes(char *buf, size_t l, off_t t);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_wait_for_event(int fd, int event, usec_t timeout);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid* memdup(const void *p, size_t l) _alloc_(2);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint is_kernel_thread(pid_t pid);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_inc_sndbuf(int fd, size_t n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fd_inc_rcvbuf(int fd, size_t n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint setrlimit_closest(int resource, const struct rlimit *rlim);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint getenv_for_pid(pid_t pid, const char *field, char **_value);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool is_valid_documentation_url(const char *url) _pure_;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringbool in_initrd(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid warn_melody(void);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_home_dir(char **ret);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint get_shell(char **_ret);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekstatic inline void freep(void *p) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(*(void**) p);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering static inline void func##p(type *p) { \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (*p) \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering func(*p); \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } \
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekstatic inline void closep(int *fd) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek if (*fd >= 0)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek close_nointr_nofail(*fd);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek}
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekstatic inline void umaskp(mode_t *u) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek umask(*u);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek}
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekstatic inline void close_pipep(int (*p)[2]) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek close_pipe(*p);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek}
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-SzmekDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-SzmekDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-SzmekDEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-SzmekDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_free_ _cleanup_(freep)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_close_ _cleanup_(closep)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_umask_ _cleanup_(umaskp)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_globfree_ _cleanup_(globfree)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_fclose_ _cleanup_(fclosep)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_pclose_ _cleanup_(pclosep)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_closedir_ _cleanup_(closedirp)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_endmntent_ _cleanup_(endmntentp)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek#define _cleanup_close_pipe_ _cleanup_(close_pipep)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek return NULL;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek return malloc(a * b);
}
_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
return NULL;
return memdup(p, a * b);
}
bool filename_is_safe(const char *p) _pure_;
bool path_is_safe(const char *p) _pure_;
bool string_is_safe(const char *p) _pure_;
bool string_has_cc(const char *p) _pure_;
/**
* Check if a string contains any glob patterns.
*/
_pure_ static inline bool string_is_glob(const char *p) {
return !!strpbrk(p, GLOB_CHARS);
}
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *, void *),
void *arg);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
DRAW_TREE_VERT,
DRAW_TREE_BRANCH,
DRAW_TREE_RIGHT,
DRAW_TREE_SPACE,
DRAW_TRIANGULAR_BULLET,
DRAW_BLACK_CIRCLE,
_DRAW_SPECIAL_CHAR_MAX
} DrawSpecialChar;
const char *draw_special_char(DrawSpecialChar ch);
char *strreplace(const char *text, const char *old_string, const char *new_string);
char *strip_tab_ansi(char **p, size_t *l);
int on_ac_power(void);
int search_and_fopen(const char *path, const char *mode, const char **search, FILE **_f);
int search_and_fopen_nulstr(const char *path, const char *mode, const char *search, FILE **_f);
#define FOREACH_LINE(line, f, on_error) \
for (;;) \
if (!fgets(line, sizeof(line), f)) { \
if (ferror(f)) { \
on_error; \
} \
break; \
} else
#define FOREACH_DIRENT(de, d, on_error) \
for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
if (!de) { \
if (errno > 0) { \
on_error; \
} \
break; \
} else if (ignore_file((de)->d_name)) \
continue; \
else
static inline void *mempset(void *s, int c, size_t n) {
memset(s, c, n);
return (uint8_t*)s + n;
}
char *hexmem(const void *p, size_t l);
void *unhexmem(const char *p, size_t l);
char *strextend(char **x, ...) _sentinel_;
char *strrep(const char *s, unsigned n);
void* greedy_realloc(void **p, size_t *allocated, size_t need);
void* greedy_realloc0(void **p, size_t *allocated, size_t need);
#define GREEDY_REALLOC(array, allocated, need) \
greedy_realloc((void**) &(array), &(allocated), sizeof((array)[0]) * (need))
#define GREEDY_REALLOC0(array, allocated, need) \
greedy_realloc0((void**) &(array), &(allocated), sizeof((array)[0]) * (need))
static inline void _reset_errno_(int *saved_errno) {
errno = *saved_errno;
}
#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
struct _umask_struct_ {
mode_t mask;
bool quit;
};
static inline void _reset_umask_(struct _umask_struct_ *s) {
umask(s->mask);
};
#define RUN_WITH_UMASK(mask) \
for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
!_saved_umask_.quit ; \
_saved_umask_.quit = true)
static inline unsigned u64log2(uint64_t n) {
#if __SIZEOF_LONG_LONG__ == 8
return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
#else
#error "Wut?"
#endif
}
static inline unsigned u32ctz(uint32_t n) {
#if __SIZEOF_INT__ == 4
return __builtin_ctz(n);
#else
#error "Wut?"
#endif
}
static inline bool logind_running(void) {
return access("/run/systemd/seats/", F_OK) >= 0;
}
#define DECIMAL_STR_WIDTH(x) \
({ \
typeof(x) _x_ = (x); \
unsigned ans = 1; \
while (_x_ /= 10) \
ans++; \
ans; \
})
int unlink_noerrno(const char *path);
#define alloca0(n) \
({ \
char *_new_; \
size_t _len_ = n; \
_new_ = alloca(_len_); \
(void *) memset(_new_, 0, _len_); \
})
#define strappenda(a, b) \
({ \
const char *_a_ = (a), *_b_ = (b); \
char *_c_; \
size_t _x_, _y_; \
_x_ = strlen(_a_); \
_y_ = strlen(_b_); \
_c_ = alloca(_x_ + _y_ + 1); \
strcpy(stpcpy(_c_, _a_), _b_); \
_c_; \
})
#define procfs_file_alloca(pid, field) \
({ \
pid_t _pid_ = (pid); \
const char *_r_; \
if (_pid_ == 0) { \
_r_ = ("/proc/self/" field); \
} else { \
_r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
} \
_r_; \
})
struct _locale_struct_ {
locale_t saved_locale;
locale_t new_locale;
bool quit;
};
static inline void _reset_locale_(struct _locale_struct_ *s) {
PROTECT_ERRNO;
if (s->saved_locale != (locale_t) 0)
uselocale(s->saved_locale);
if (s->new_locale != (locale_t) 0)
freelocale(s->new_locale);
}
#define RUN_WITH_LOCALE(mask, loc) \
for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
({ \
if (!_saved_locale_.quit) { \
PROTECT_ERRNO; \
_saved_locale_.new_locale = newlocale((mask), (loc), (locale_t) 0); \
if (_saved_locale_.new_locale != (locale_t) 0) \
_saved_locale_.saved_locale = uselocale(_saved_locale_.new_locale); \
} \
!_saved_locale_.quit; }) ; \
_saved_locale_.quit = true)
bool id128_is_valid(const char *s) _pure_;
void parse_user_at_host(char *arg, char **user, char **host);
int split_pair(const char *s, const char *sep, char **l, char **r);
int shall_restore_state(void);
/**
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
static inline void qsort_safe(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *)) {
if (nmemb) {
assert(base);
qsort(base, nmemb, size, compar);
}
}
int proc_cmdline(char **ret);
int container_get_leader(const char *machine, pid_t *pid);
int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *root_fd);
int namespace_enter(int pidns_fd, int mntns_fd, int root_fd);
bool pid_valid(pid_t pid);
int getpeercred(int fd, struct ucred *ucred);
int getpeersec(int fd, char **ret);
int writev_safe(int fd, const struct iovec *w, int j);
int mkostemp_safe(char *pattern, int flags);
int open_tmpfile(const char *path, int flags);
int fd_warn_permissions(const char *path, int fd);