util.h revision 56cf987fe74270bde4e16c7ec9e0414a9030723b
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering/*-*- Mode: C; c-basic-offset: 8 -*-*/
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#ifndef fooutilhfoo
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers#define fooutilhfoo
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering/***
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering This file is part of systemd.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Copyright 2010 Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is free software; you can redistribute it and/or modify it
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering under the terms of the GNU General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering the Free Software Foundation; either version 2 of the License, or
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering (at your option) any later version.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is distributed in the hope that it will be useful, but
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering General Public License for more details.
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón You should have received a copy of the GNU General Public License
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón along with systemd; If not, see <http://www.gnu.org/licenses/>.
dd050decb6ad131ebdeabb71c4f9ecb4733269c0David Herrmann***/
1a435084b7f55bc24042f9bc47c18e4e2381f667Kay Sievers
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <inttypes.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <time.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <sys/time.h>
78a825f216d39ee0295b00647b059d45467e1d02Kay Sievers#include <stdbool.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <stdlib.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <stdio.h>
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón#include <signal.h>
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón#include <sched.h>
4db6d587c37c0357d20c79bf1a7c9afd4c7ced61Kay Sievers#include <limits.h>
907dd1953b7517534d646f5b2777780020c896e2Kay Sievers
eb7bbee6cd182d5c4eb1e1180631c35158f59379Kay Sievers#include "macro.h"
6aad7f2cd75c88f990c7857bb173b3e8d0faee0fUmut Tezduyar Lindskog
91ca5bf0b6f3b487a16cc262527c9de6744db624Martin Pitttypedef uint64_t usec_t;
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringtypedef struct dual_timestamp {
22be093ffb403a1c474037939ca9b88b1ee39f77Lennart Poettering usec_t realtime;
4fecbe59dadaed314b792ed35ef5dc24c7047a18Jan Alexander Steffens (heftig) usec_t monotonic;
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)} dual_timestamp;
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define MSEC_PER_SEC 1000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define USEC_PER_SEC 1000000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define USEC_PER_MSEC 1000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define NSEC_PER_SEC 1000000000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define NSEC_PER_MSEC 1000000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define NSEC_PER_USEC 1000ULL
4fecbe59dadaed314b792ed35ef5dc24c7047a18Jan Alexander Steffens (heftig)
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers#define USEC_PER_MINUTE (60ULL*USEC_PER_SEC)
8666abb452db73d9a11ead61251eec42bc531cceKay Sievers#define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE)
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#define USEC_PER_DAY (24ULL*USEC_PER_HOUR)
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez#define USEC_PER_WEEK (7ULL*USEC_PER_DAY)
3d585edbb14b9705c80183aeb16dfd0a28df0ac9Lennart Poettering
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez/* What is interpreted as whitespace? */
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers#define WHITESPACE " \t\n\r"
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers#define NEWLINE "\n\r"
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers
4c6abc93c708762ae3f377eab8dbd357262cc432Koen Kooi#define FORMAT_TIMESTAMP_MAX 64
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger#define FORMAT_TIMESPAN_MAX 64
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger#define ANSI_HIGHLIGHT_ON "\x1B[1;31m"
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerusec_t now(clockid_t clock);
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerdual_timestamp* dual_timestamp_get(dual_timestamp *ts);
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerusec_t timespec_load(const struct timespec *ts);
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerstruct timespec *timespec_store(struct timespec *ts, usec_t u);
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburgerusec_t timeval_load(const struct timeval *tv);
a59f16ce4a11c440cb2136ad3d5e3184714c545eRobert Schielestruct timeval *timeval_store(struct timeval *tv, usec_t u);
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera#define streq(a,b) (strcmp((a),(b)) == 0)
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburgerbool streq_ptr(const char *a, const char *b);
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger#define new(t, n) ((t*) malloc(sizeof(t)*(n)))
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger#define new0(t, n) ((t*) calloc((n), sizeof(t)))
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera#define malloc0(n) (calloc((n), 1))
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocerastatic inline const char* yes_no(bool b) {
20f56fddcd58c84fa73597486e905c652667214fDidier Roche return b ? "yes" : "no";
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera}
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poetteringstatic inline const char* strempty(const char *s) {
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering return s ? s : "";
3ce4fad8f548db9edb19869ea540e3192d2123f4Kay Sievers}
f975e971accc4d50c73ae53167db3df7a7099cf2Lennart Poettering
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poetteringstatic inline const char* strnull(const char *s) {
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers return s ? s : "(null)";
92ec4495f76a7a2a6c31f5bb2a5240f78dcfe1d2Javier Jardón}
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenstatic inline const char *strna(const char *s) {
b51fc639f01ee6194af3e7e944a79accce474fe1Dave Reisner return s ? s : "n/a";
dd5ae4c36c89da5dbe8d1628939b26c00db98753Przemyslaw Kedzierski}
dd5ae4c36c89da5dbe8d1628939b26c00db98753Przemyslaw Kedzierski
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenstatic inline bool is_path_absolute(const char *p) {
96ede2601f27cd5fe52eed96b873bef55cd0ce23Lennart Poettering return *p == '/';
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen}
edeb68c53f1cdc452016b4c8512586a70b1262e3Tom Gundersen
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenbool endswith(const char *s, const char *postfix);
7801356442578ff6e1c65844eb9e65c819af4660Zbigniew Jędrzejewski-Szmekbool startswith(const char *s, const char *prefix);
66b0e0e0e3652227fe107ab9d09fa14fd4bc4dfaCristian Rodríguezbool startswith_no_case(const char *s, const char *prefix);
66b0e0e0e3652227fe107ab9d09fa14fd4bc4dfaCristian Rodríguez
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkovbool first_word(const char *s, const char *word);
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkov
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkovint close_nointr(int fd);
446883528524429283626208928b51f49f28f810Lennart Poetteringvoid close_nointr_nofail(int fd);
446883528524429283626208928b51f49f28f810Lennart Poetteringvoid close_many(const int fds[], unsigned n_fd);
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmekint parse_boolean(const char *v);
c4a77bcb9a50f152557e25c90837d8bfef858729Marc-Antoine Perennouint parse_usec(const char *t, usec_t *usec);
ac714a78fdca481488d88f84b6332d28083a4511Martin Jansaint parse_pid(const char *s, pid_t* ret_pid);
ac714a78fdca481488d88f84b6332d28083a4511Martin Jansa
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Bieblint safe_atou(const char *s, unsigned *ret_u);
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Bieblint safe_atoi(const char *s, int *ret_i);
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl
9a60da2834074d970ca063c210fe9d2f05c70532Thierry Redingint safe_atollu(const char *s, unsigned long long *ret_u);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekint safe_atolli(const char *s, long long int *ret_i);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#if __WORDSIZE == 32
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(unsigned long) == sizeof(unsigned));
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek return safe_atou(s, (unsigned*) ret_u);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek}
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoli(const char *s, long int *ret_u) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(long int) == sizeof(int));
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek return safe_atoi(s, (int*) ret_u);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek}
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#else
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atollu(s, (unsigned long long*) ret_u);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek}
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoli(const char *s, long int *ret_u) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(long int) == sizeof(long long int));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atolli(s, (long long int*) ret_u);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek}
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek#endif
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(uint32_t) == sizeof(unsigned));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atou(s, (unsigned*) ret_u);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek}
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(int32_t) == sizeof(int));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atoi(s, (int*) ret_i);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek}
eb2e280f9c59b66965c9316eadc4c113a13ca744Lucas De Marchi
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poetteringstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return safe_atollu(s, (unsigned long long*) ret_u);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering}
780040dc2a4b08a2c1fe5bd8db3a70e966c2acb3Kay Sievers
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_cc(sizeof(int64_t) == sizeof(long long int));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return safe_atolli(s, (long long int*) ret_i);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering}
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
03930e48636e86ad84946253ca2bf4f91deeb645Lennart Poetteringchar *split(const char *c, size_t *l, const char *separator, char **state);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar *split_quoted(const char *c, size_t *l, char **state);
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poettering
8dc31a63981267f257583ef82ceb79859ff243f8Daniel Mack#define FOREACH_WORD(word, length, s, state) \
8dc31a63981267f257583ef82ceb79859ff243f8Daniel Mack for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
8dc31a63981267f257583ef82ceb79859ff243f8Daniel Mack
8dc31a63981267f257583ef82ceb79859ff243f8Daniel Mack#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define FOREACH_WORD_QUOTED(word, length, s, state) \
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar **split_path_and_make_absolute(const char *p);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering
f73fb7b742f294b6d2126afa16001bd2ff6ab461Filipe Brandenburgerint write_one_line_file(const char *fn, const char *line);
0a98d66159e474915afd6597d3aa444a698fdd2dDavid Herrmannint read_one_line_file(const char *fn, char **line);
d200735e13c52dcfe36c0e066f9f6c2fbfb85a9cMichal Schmidt
213298fb822258bb69c6b85b7c8d7f019fd2306aLennart Poetteringchar *strappend(const char *s, const char *suffix);
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poetteringchar *strnappend(const char *s, const char *suffix, size_t length);
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar *replace_env(const char *format, char **env);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar **replace_env_argv(char **argv, char **env);
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieriint readlink_malloc(const char *p, char **r);
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poetteringint readlink_and_make_absolute(const char *p, char **r);
aca33b078cd32c5416a7fa3d5020a5d461c130eeCristian Rodríguez
2a4d1ec1520c926b0160efe14142634e164ddc30Cristian Rodríguezchar *file_name_from_path(const char *p);
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poetteringbool is_path(const char *p);
4f47bb8c5e5f234c614dc14532a9483328e61002Zbigniew Jędrzejewski-Szmek
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerbool path_is_absolute(const char *p);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *path_make_absolute(const char *p, const char *prefix);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *path_make_absolute_cwd(const char *p);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburger
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar **strv_path_make_absolute_cwd(char **l);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar **strv_path_canonicalize(char **l);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburger
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerint reset_all_signal_handlers(void);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburger
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *strstrip(char *s);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *delete_chars(char *s, const char *bad);
4f47bb8c5e5f234c614dc14532a9483328e61002Zbigniew Jędrzejewski-Szmekchar *truncate_nl(char *s);
4f47bb8c5e5f234c614dc14532a9483328e61002Zbigniew Jędrzejewski-Szmek
65e3a2cf7c3b399853dd309f702ca5078b7d16eaZbigniew Jędrzejewski-Szmekchar *file_in_same_dir(const char *path, const char *filename);
65e3a2cf7c3b399853dd309f702ca5078b7d16eaZbigniew Jędrzejewski-Szmekint safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
65e3a2cf7c3b399853dd309f702ca5078b7d16eaZbigniew Jędrzejewski-Szmekint mkdir_parents(const char *path, mode_t mode);
4f47bb8c5e5f234c614dc14532a9483328e61002Zbigniew Jędrzejewski-Szmekint mkdir_p(const char *path, mode_t mode);
0289f2fb2a64df53b589b771f69c43126b029590Zbigniew Jędrzejewski-Szmek
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sieversint parent_of_path(const char *path, char **parent);
4fecbe59dadaed314b792ed35ef5dc24c7047a18Jan Alexander Steffens (heftig)
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sieversint rmdir_parents(const char *path, const char *stop);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek
5a45a93627609451784a04366cfa1150d32611d1Lennart Poetteringint get_process_name(pid_t pid, char **name);
0289f2fb2a64df53b589b771f69c43126b029590Zbigniew Jędrzejewski-Szmekint get_process_cmdline(pid_t pid, size_t max_length, char **line);
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekchar hexchar(int x);
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekint unhexchar(char c);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekchar octchar(int x);
9e7adc3ae1133fa08a468768a490812299fad030Lucas De Marchiint unoctchar(char c);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar decchar(int x);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieversint undecchar(char c);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sievers
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *cescape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *cunescape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *cunescape_length(const char *s, size_t length);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sievers
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *xescape(const char *s, const char *bad);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sievers
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *bus_path_escape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *bus_path_unescape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sievers
9e7adc3ae1133fa08a468768a490812299fad030Lucas De Marchichar *path_kill_slashes(char *path);
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri
9d2d0fe1e3f28a639c26b62391f79cfd1450d91bEvangelos Foutrasbool path_startswith(const char *path, const char *prefix);
5a45a93627609451784a04366cfa1150d32611d1Lennart Poetteringbool path_equal(const char *a, const char *b);
39c4ead2323b45bbe9866e0f97fd8dcfb8a0bedeZbigniew Jędrzejewski-Szmek
2a4d1ec1520c926b0160efe14142634e164ddc30Cristian Rodríguezchar *ascii_strlower(char *path);
39c4ead2323b45bbe9866e0f97fd8dcfb8a0bedeZbigniew Jędrzejewski-Szmek
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekbool ignore_file(const char *filename);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmekbool chars_intersect(const char *a, const char *b);
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek
1c231f56482546725c4dbd3303f70300bd3c63e9Lennart Poetteringchar *format_timestamp(char *buf, size_t l, usec_t t);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmekchar *format_timespan(char *buf, size_t l, usec_t t);
de99c9dcbaf6e474551266d8f0b519bf2d8d0522Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmekint make_stdio(int fd);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmekbool is_clean_exit(int code, int status);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmekunsigned long long random_ull(void);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
2799e519cabb6dfa99341b9a56ebd4dc2a4ec22aZbigniew Jędrzejewski-Szmek#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek const char *name##_to_string(type i) { \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek return NULL; \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek return name##_table[i]; \
32dcef3ab1eb91ee469c3246ef859578dccd8a45Zbigniew Jędrzejewski-Szmek } \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek type name##_from_string(const char *s) { \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek type i; \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger unsigned u = 0; \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger assert(s); \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger if (name##_table[i] && \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger streq(name##_table[i], s)) \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger return i; \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger if (safe_atou(s, &u) >= 0 && \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek u < ELEMENTSOF(name##_table)) \
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek return (type) u; \
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek return (type) -1; \
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek } \
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek struct __useless_struct_to_allow_trailing_semicolon__
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint fd_nonblock(int fd, bool nonblock);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint fd_cloexec(int fd, bool cloexec);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint close_all_fds(const int except[], unsigned n_except);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineaubool fstype_is_network(const char *fstype);
27c64db6dff88ebe9761dfe3b0c073d2a9bf2e41Zbigniew Jędrzejewski-Szmek
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversint chvt(int vt);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversint read_one_char(FILE *f, char *ret, bool *need_nl);
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Bieblint ask(char *ret, const char *replies, const char *text, ...);
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Bieblint reset_terminal(int fd);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringint open_terminal(const char *name, int mode);
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Bieblint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
5fd2e2284323304ad28f8ab80041f3cf6632dc72Michal Schmidtint release_terminal(void);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversint flush_fd(int fd);
27c64db6dff88ebe9761dfe3b0c073d2a9bf2e41Zbigniew Jędrzejewski-Szmek
4db17f291c627c885de668200ff8cce2e57c933fZbigniew Jędrzejewski-Szmekint ignore_signals(int sig, ...);
c932fb71cc90461b88ecdffe47c071d001d78fb4Shawn Landdenint default_signals(int sig, ...);
c932fb71cc90461b88ecdffe47c071d001d78fb4Shawn Landdenint sigaction_many(const struct sigaction *sa, ...);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekint close_pipe(int p[]);
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poettering
9388e99e208a6487b26dcbda86005ee9eba8d93dMichael Olbrichssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
3b794314149e40afaf3c456285e1e529747b6560Holger Schurigssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
5f381b355a95b953654e46ba3ccdc81bdec165eaLennart Poettering
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekint path_is_mount_point(const char *path);
539618a0ddc2dc7f0fbe28de2ae0e07b34c81e60Lennart Poettering
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekbool is_device_path(const char *path);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
84dd59b51c0db34f0334e90a5da2a45abcc9a52aDjalal Harouniint dir_is_empty(const char *path);
84dd59b51c0db34f0334e90a5da2a45abcc9a52aDjalal Harouni
c4a5ddc9f29cf910fac9d814cd898b4cc2bd79b1Tom Gundersenvoid rename_process(const char name[8]);
6589d0dba2b1ccf2406db527c2c1b51c7143e117Jean-André Santoni
75616a1332aff00d27db713cda3bd93c508a5b59Zbigniew Jędrzejewski-Szmekvoid sigset_add_many(sigset_t *ss, ...);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekchar* gethostname_malloc(void);
583c14fc04a089e9af70a3fa0b8c0a8c27c06ec0Michael Olbrichchar* getlogname_malloc(void);
56bf3853b4aefad31edf8631668446007e2347cbSusant Sahaniint getttyname_malloc(char **r);
66f4bc7795eb8ba1259db7fbdc567306eec89df7Susant Sahaniint getmachineid_malloc(char **r);
8ecec322fe6b34b64868d8cc3808b5613f09e8d3Zbigniew Jędrzejewski-Szmek
38a0245fb2248744f3c38f451231c4f45bffdc42Susant Sahaniint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
c3eae485bb639352122ac0fbf30490e887f3bc44Susant Sahani
34f7b9f98facbf3431c6849622104cee992f2b7dLennart Poetteringint rm_rf(const char *path, bool only_dirs, bool delete_root);
34f7b9f98facbf3431c6849622104cee992f2b7dLennart Poettering
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekcpu_set_t* cpu_set_malloc(unsigned *ncpus);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekvoid status_vprintf(const char *format, va_list ap);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekvoid status_printf(const char *format, ...);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekvoid status_welcome(void);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekint columns(void);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekint running_in_chroot(void);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
34f7b9f98facbf3431c6849622104cee992f2b7dLennart Poetteringchar *ellipsize(const char *s, unsigned length, unsigned percent);
cf1755bac0426132c21fdca519a336ce7d920277Michael Olbrich
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekconst char *ioprio_class_to_string(int i);
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poetteringint ioprio_class_from_string(const char *s);
b237ef2cfac7ab0b33170809e8cb64628606207dTollef Fog Heen
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremerconst char *sigchld_code_to_string(int i);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringint sigchld_code_from_string(const char *s);
2270309471213a3c960543e523130627e9cb10e2Kay Sievers
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char *log_facility_to_string(int i);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint log_facility_from_string(const char *s);
8114dedc5910d9a9cec702f6b6658551a0cd9edeChengwei Yang
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char *log_level_to_string(int i);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint log_level_from_string(const char *s);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char *sched_policy_to_string(int i);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint sched_policy_from_string(const char *s);
2270309471213a3c960543e523130627e9cb10e2Kay Sievers
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineauconst char *rlimit_to_string(int i);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint rlimit_from_string(const char *s);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthing
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingconst char *ip_tos_to_string(int i);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint ip_tos_from_string(const char *s);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthing
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingconst char *signal_to_string(int i);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint signal_from_string(const char *s);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthing
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint label_init(void);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint label_fix(const char *path);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekvoid label_finish(void);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekint label_socket_set(const char *label);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekvoid label_socket_clear(void);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekint label_fifofile_set(const char *label, const char *path);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekvoid label_file_clear(void);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekvoid label_free(const char *label);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekint label_get_socket_label_from_exe(const char *exe, char **label);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek#endif
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek