util.h revision 6282c859bdc6463cb25734dcfd3cf8628d951088
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#pragma once
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/***
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2010 Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering***/
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <inttypes.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <time.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <sys/time.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <stdarg.h>
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include <stdbool.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <stdlib.h>
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include <stdio.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <signal.h>
3c70e3bb022f0de3317f3600c9366a2f4597339eLennart Poettering#include <sched.h>
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#include <limits.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <sys/types.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <sys/stat.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <dirent.h>
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#include <sys/resource.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <stddef.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "macro.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "time-util.h"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackunion dirent_storage {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering struct dirent de;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering uint8_t storage[offsetof(struct dirent, d_name) +
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering ((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))];
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering};
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/* What is interpreted as whitespace? */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define WHITESPACE " \t\n\r"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define NEWLINE "\n\r"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define QUOTES "\"\'"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define COMMENTS "#;\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define FORMAT_BYTES_MAX 8
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define ANSI_RED_ON "\x1B[31m"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define ANSI_GREEN_ON "\x1B[32m"
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
4f10118016f9b2fd7e1d26c9ef7d91eb33fba694Lennart Poettering#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringsize_t page_size(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define streq(a,b) (strcmp((a),(b)) == 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool streq_ptr(const char *a, const char *b);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define new0(t, n) ((t*) calloc((n), sizeof(t)))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define malloc0(n) (calloc((n), 1))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic inline const char* yes_no(bool b) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return b ? "yes" : "no";
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic inline const char* strempty(const char *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return s ? s : "";
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic inline const char* strnull(const char *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return s ? s : "(null)";
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline const char *strna(const char *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return s ? s : "n/a";
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a1e58e8ee1c84b633d6d6d651d5328d4dd4eba5bLennart Poetteringstatic inline bool isempty(const char *p) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return !p || !p[0];
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *endswith(const char *s, const char *postfix);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *startswith(const char *s, const char *prefix);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *startswith_no_case(const char *s, const char *prefix);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool first_word(const char *s, const char *word);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint close_nointr(int fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid close_nointr_nofail(int fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid close_many(const int fds[], unsigned n_fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint parse_boolean(const char *v);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint parse_bytes(const char *t, off_t *bytes);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint parse_pid(const char *s, pid_t* ret_pid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint parse_uid(const char *s, uid_t* ret_uid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint safe_atou(const char *s, unsigned *ret_u);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint safe_atoi(const char *s, int *ret_i);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint safe_atollu(const char *s, unsigned long long *ret_u);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint safe_atolli(const char *s, long long int *ret_i);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint safe_atod(const char *s, double *ret_d);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#if __WORDSIZE == 32
920b52e4909d9dc812817fd8b82f83ca23a11c91Thomas Hindoe Paaboel Andersenstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_cc(sizeof(unsigned long) == sizeof(unsigned));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return safe_atou(s, (unsigned*) ret_u);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic inline int safe_atoli(const char *s, long int *ret_u) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_cc(sizeof(long int) == sizeof(int));
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen return safe_atoi(s, (int*) ret_u);
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen}
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen#else
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersenstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen return safe_atollu(s, (unsigned long long*) ret_u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline int safe_atoli(const char *s, long int *ret_u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(long int) == sizeof(long long int));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return safe_atolli(s, (long long int*) ret_u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#endif
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(uint32_t) == sizeof(unsigned));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return safe_atou(s, (unsigned*) ret_u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(int32_t) == sizeof(int));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return safe_atoi(s, (int*) ret_i);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return safe_atollu(s, (unsigned long long*) ret_u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(int64_t) == sizeof(long long int));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return safe_atolli(s, (long long int*) ret_i);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poetteringchar *split(const char *c, size_t *l, const char *separator, char **state);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poetteringchar *split_quoted(const char *c, size_t *l, char **state);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define FOREACH_WORD(word, length, s, state) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define FOREACH_WORD_QUOTED(word, length, s, state) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint get_starttime_of_pid(pid_t pid, unsigned long long *st);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackchar *strappend(const char *s, const char *suffix);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackchar *strnappend(const char *s, const char *suffix, size_t length);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackchar *replace_env(const char *format, char **env);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackchar **replace_env_argv(char **argv, char **env);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint readlink_malloc(const char *p, char **r);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint readlink_and_make_absolute(const char *p, char **r);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint readlink_and_canonicalize(const char *p, char **r);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint reset_all_signal_handlers(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *strstrip(char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *delete_chars(char *s, const char *bad);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *truncate_nl(char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *file_in_same_dir(const char *path, const char *filename);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint rmdir_parents(const char *path, const char *stop);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint get_process_comm(pid_t pid, char **name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint get_process_exe(pid_t pid, char **name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint get_process_uid(pid_t pid, uid_t *uid);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint get_process_gid(pid_t pid, gid_t *gid);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar hexchar(int x);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint unhexchar(char c);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar octchar(int x);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint unoctchar(char c);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poetteringchar decchar(int x);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint undecchar(char c);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *cescape(const char *s);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringchar *cunescape(const char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *cunescape_length(const char *s, size_t length);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poetteringchar *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *xescape(const char *s, const char *bad);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *bus_path_escape(const char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *bus_path_unescape(const char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *ascii_strlower(char *path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool dirent_is_file(const struct dirent *de);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool ignore_file(const char *filename);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool chars_intersect(const char *a, const char *b);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint make_stdio(int fd);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint make_null_stdio(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint make_console_stdio(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringunsigned long long random_ull(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/* For basic lookup tables with strictly enumerated entries */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering scope const char *name##_to_string(type i) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return NULL; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return name##_table[i]; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering scope type name##_from_string(const char *s) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering type i; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (name##_table[i] && \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering streq(name##_table[i], s)) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return i; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return (type) -1; \
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering } \
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack struct __useless_struct_to_allow_trailing_semicolon__
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack/* For string conversions where numbers are also acceptable */
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int name##_to_string_alloc(type i, char **str) { \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering char *s; \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r; \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (i < 0 || i > max) \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -ERANGE; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (i < (type) ELEMENTSOF(name##_table)) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s = strdup(name##_table[i]); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return log_oom(); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } else { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = asprintf(&s, "%u", i); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0) \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return log_oom(); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *str = s; \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0; \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } \
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering type name##_from_string(const char *s) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering type i; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unsigned u = 0; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (name##_table[i] && \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering streq(name##_table[i], s)) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return i; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (safe_atou(s, &u) >= 0 && u <= max) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return (type) u; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return (type) -1; \
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering } \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringint fd_nonblock(int fd, bool nonblock);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringint fd_cloexec(int fd, bool cloexec);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringint close_all_fds(const int except[], unsigned n_except);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering
cfa9677bd164574600d29a9bf99f9d1f28a7a170Mantas MikulÄ—nasbool fstype_is_network(const char *fstype);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint chvt(int vt);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringint ask(char *ret, const char *replies, const char *text, ...);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringint reset_terminal_fd(int fd, bool switch_to_text);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringint reset_terminal(const char *name);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint open_terminal(const char *name, int mode);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint release_terminal(void);
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmekint flush_fd(int fd);
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint ignore_signals(int sig, ...);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint default_signals(int sig, ...);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint sigaction_many(const struct sigaction *sa, ...);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint close_pipe(int p[]);
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poetteringint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidtssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poetteringssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool is_device_path(const char *path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint dir_is_empty(const char *path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringvoid rename_process(const char name[8]);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringvoid sigset_add_many(sigset_t *ss, ...);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool hostname_is_set(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar* gethostname_malloc(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar* getlogname_malloc(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar* getusername_malloc(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint getttyname_malloc(int fd, char **r);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint getttyname_harder(int fd, char **r);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint get_ctty_devnr(pid_t pid, dev_t *d);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint get_ctty(pid_t, dev_t *_devnr, char **r);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
be847e82cf95bf8eb589778df2aa2b3d1d7ae99eLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint pipe_eof(int fd);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringcpu_set_t* cpu_set_malloc(unsigned *ncpus);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint status_welcome(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint fd_columns(int fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringunsigned columns(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint fd_lines(int fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringunsigned lines(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid columns_lines_cache_reset(int _unused_ signum);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool on_tty(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint running_in_chroot(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *ellipsize(const char *s, size_t length, unsigned percent);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint touch(const char *path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *unquote(const char *s, const char *quotes);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *normalize_env_assignment(const char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint wait_for_terminate(pid_t pid, siginfo_t *status);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint wait_for_terminate_and_warn(const char *name, pid_t pid);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering_noreturn_ void freeze(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool null_or_empty(struct stat *st);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint null_or_empty_path(const char *fn);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart PoetteringDIR *xopendirat(int dirfd, const char *name, int flags);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *fstab_node_to_udev_node(const char *p);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *resolve_dev_console(char **active);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool tty_is_vc(const char *tty);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool tty_is_vc_resolve(const char *tty);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool tty_is_console(const char *tty);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint vtnr_from_tty(const char *tty);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringconst char *default_term_for_tty(const char *tty);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid execute_directory(const char *directory, DIR *_d, char *argv[]);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint kill_and_sigcont(pid_t pid, int sig);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool nulstr_contains(const char*nulstr, const char *needle);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool plymouth_running(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool hostname_is_valid(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar* hostname_cleanup(char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar* strshorten(char *s, size_t l);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poetteringint terminal_vhangup_fd(int fd);
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poetteringint terminal_vhangup(const char *name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint vt_disallocate(const char *name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint copy_file(const char *from, const char *to);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint symlink_atomic(const char *from, const char *to);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint fchmod_umask(int fd, mode_t mode);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool display_is_local(const char *display);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint socket_from_display(const char *display, char **path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidtint get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint get_group_creds(const char **groupname, gid_t *gid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint in_group(const char *name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar* uid_to_name(uid_t uid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint glob_exists(const char *path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint dirent_ensure_type(DIR *d, struct dirent *de);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint in_search_path(const char *path, char **search);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint get_files_in_directory(const char *path, char ***list);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *strjoin(const char *x, ...) _sentinel_;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool is_main_thread(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool in_charset(const char *s, const char* charset);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint block_get_whole_disk(dev_t d, dev_t *ret);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint file_is_priv_sticky(const char *p);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint strdup_or_null(const char *a, char **b);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering#define NULSTR_FOREACH(i, l) \
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define NULSTR_FOREACH_PAIR(i, j, l) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint ioprio_class_to_string_alloc(int i, char **s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint ioprio_class_from_string(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringconst char *sigchld_code_to_string(int i);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint sigchld_code_from_string(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint log_facility_unshifted_to_string_alloc(int i, char **s);
db2cb23b5b179707000d28a11efb3d888d06ee80Umut Tezduyar Lindskogint log_facility_unshifted_from_string(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint log_level_to_string_alloc(int i, char **s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint log_level_from_string(const char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poetteringint sched_policy_to_string_alloc(int i, char **s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint sched_policy_from_string(const char *s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringconst char *rlimit_to_string(int i);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint rlimit_from_string(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint ip_tos_to_string_alloc(int i, char **s);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poetteringint ip_tos_from_string(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringconst char *signal_to_string(int i);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint signal_from_string(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint signal_from_string_try_harder(const char *s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringextern int saved_argc;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringextern char **saved_argv;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool kexec_loaded(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint prot_from_flags(int flags);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringchar *format_bytes(char *buf, size_t l, off_t t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint fd_wait_for_event(int fd, int event, usec_t timeout);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid* memdup(const void *p, size_t l) _malloc_;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint is_kernel_thread(pid_t pid);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint fd_inc_sndbuf(int fd, size_t n);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint fd_inc_rcvbuf(int fd, size_t n);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint setrlimit_closest(int resource, const struct rlimit *rlim);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackint getenv_for_pid(pid_t pid, const char *field, char **_value);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint can_sleep(const char *type);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint can_sleep_disk(const char *type);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool is_valid_documentation_url(const char *url);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool in_initrd(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid warn_melody(void);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint get_shell(char **ret);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint get_home_dir(char **ret);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic inline void freep(void *p) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(*(void**) p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid fclosep(FILE **f);
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardtvoid pclosep(FILE **f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid closep(int *fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid closedirp(DIR **d);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic inline void umaskp(mode_t *u) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering umask(*u);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering_malloc_ static inline void *malloc_multiply(size_t a, size_t b) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return malloc(a * b);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardt
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering_malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering return memdup(p, a * b);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool filename_is_safe(const char *p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool path_is_safe(const char *p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool string_is_safe(const char *p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringbool string_has_cc(const char *p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int (*compar) (const void *, const void *, void *),
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering void *arg);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool is_locale_utf8(void);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringtypedef enum DrawSpecialChar {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DRAW_TREE_VERT,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DRAW_TREE_BRANCH,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DRAW_TREE_RIGHT,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DRAW_TREE_SPACE,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DRAW_TRIANGULAR_BULLET,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _DRAW_SPECIAL_CHAR_MAX
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering} DrawSpecialChar;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackconst char *draw_special_char(DrawSpecialChar ch);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackchar *strreplace(const char *text, const char *old_string, const char *new_string);
05a08cb60f02970e8476306074c70ee4e6a57fb3Thomas Hindoe Paaboel Andersen
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringchar *strip_tab_ansi(char **p, size_t *l);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint on_ac_power(void);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poetteringint search_and_fopen(const char *path, const char *mode, const char **search, FILE **_f);
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poetteringint search_and_fopen_nulstr(const char *path, const char *mode, const char *search, FILE **_f);
ff975efb2e88dcd5221a2f0d76c4c87e85b821a8Lennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering#define FOREACH_LINE(line, f, on_error) \
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering for (;;) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!fgets(line, sizeof(line), f)) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (ferror(f)) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering on_error; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering break; \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } else
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define FOREACH_DIRENT(de, d, on_error) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!de) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (errno != 0) { \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering on_error; \
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering } \
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering break; \
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering } else if (ignore_file((de)->d_name)) \
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering continue; \
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering else
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic inline void *mempset(void *s, int c, size_t n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering memset(s, c, n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return (char*)s + n;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering