util.h revision 20f56fddcd58c84fa73597486e905c652667214f
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#pragma once
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers/***
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering This file is part of systemd.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Copyright 2010 Lennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is free software; you can redistribute it and/or modify it
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering under the terms of the GNU Lesser General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering (at your option) any later version.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering systemd is distributed in the hope that it will be useful, but
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering Lesser General Public License for more details.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón You should have received a copy of the GNU Lesser General Public License
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón along with systemd; If not, see <http://www.gnu.org/licenses/>.
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón***/
62ca29b81b5f19007889439b744a16776607a55eLennart Poettering
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <alloca.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <fcntl.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <inttypes.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <time.h>
78a825f216d39ee0295b00647b059d45467e1d02Kay Sievers#include <sys/time.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <stdarg.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <stdbool.h>
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón#include <stdlib.h>
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón#include <stdio.h>
4db6d587c37c0357d20c79bf1a7c9afd4c7ced61Kay Sievers#include <signal.h>
907dd1953b7517534d646f5b2777780020c896e2Kay Sievers#include <sched.h>
eb7bbee6cd182d5c4eb1e1180631c35158f59379Kay Sievers#include <limits.h>
6aad7f2cd75c88f990c7857bb173b3e8d0faee0fUmut Tezduyar Lindskog#include <sys/types.h>
bbd9b8c2139a70005e4e83d198575e2a10fe1db2Lennart Poettering#include <sys/stat.h>
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers#include <dirent.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <sys/resource.h>
22be093ffb403a1c474037939ca9b88b1ee39f77Lennart Poettering#include <stddef.h>
d59d0a2b4b41a75eaf618b26b8f8bd1e17de7e2bcee#include <unistd.h>
d59d0a2b4b41a75eaf618b26b8f8bd1e17de7e2bcee#include <locale.h>
d59d0a2b4b41a75eaf618b26b8f8bd1e17de7e2bcee#include <mntent.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <sys/socket.h>
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers#include <sys/inotify.h>
8666abb452db73d9a11ead61251eec42bc531cceKay Sievers
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#if SIZEOF_PID_T == 4
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez# define PID_PRI PRIi32
3d585edbb14b9705c80183aeb16dfd0a28df0ac9Lennart Poettering#elif SIZEOF_PID_T == 2
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez# define PID_PRI PRIi16
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera#else
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera# error Unknown pid_t size
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera#endif
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera#define PID_FMT "%" PID_PRI
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera#if SIZEOF_UID_T == 4
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering# define UID_FMT "%" PRIu32
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering#elif SIZEOF_UID_T == 2
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering# define UID_FMT "%" PRIu16
3ce4fad8f548db9edb19869ea540e3192d2123f4Kay Sievers#else
f975e971accc4d50c73ae53167db3df7a7099cf2Lennart Poettering# error Unknown uid_t size
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering#endif
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#if SIZEOF_GID_T == 4
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers# define GID_FMT "%" PRIu32
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek#elif SIZEOF_GID_T == 2
92ec4495f76a7a2a6c31f5bb2a5240f78dcfe1d2Javier Jardón# define GID_FMT "%" PRIu16
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen#else
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen# error Unknown gid_t size
b51fc639f01ee6194af3e7e944a79accce474fe1Dave Reisner#endif
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen
86b2e20a5e5abf222fb81edcb5d58d012e35cbaaLennart Poettering#if SIZEOF_TIME_T == 8
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen# define PRI_TIME PRIi64
96ede2601f27cd5fe52eed96b873bef55cd0ce23Lennart Poettering#elif SIZEOF_TIME_T == 4
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen# define PRI_TIME PRIu32
edeb68c53f1cdc452016b4c8512586a70b1262e3Tom Gundersen#else
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen# error Unknown time_t size
7801356442578ff6e1c65844eb9e65c819af4660Zbigniew Jędrzejewski-Szmek#endif
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek#if SIZEOF_RLIM_T == 8
80a5cbace45a6adbf2f9119edc5a4b10db493064Kay Sievers# define RLIM_FMT "%" PRIu64
b37250d661ed67d07c734630617d73e64f6d7e49Zbigniew Jędrzejewski-Szmek#elif SIZEOF_RLIM_T == 4
b37250d661ed67d07c734630617d73e64f6d7e49Zbigniew Jędrzejewski-Szmek# define RLIM_FMT "%" PRIu32
6581f00f7eabdaccf587a4b6af60ed4696dd2791Zbigniew Jędrzejewski-Szmek#else
6581f00f7eabdaccf587a4b6af60ed4696dd2791Zbigniew Jędrzejewski-Szmek# error Unknown rlim_t size
92ec4495f76a7a2a6c31f5bb2a5240f78dcfe1d2Javier Jardón#endif
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek#include "macro.h"
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek#include "missing.h"
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek#include "time-util.h"
6e92b23f0d6dd398848376bbaf47e54a90ed3389Kay Sievers
0eaeca1f2373a323b98c86b47561d98e59c67b25Kay Sievers/* What is interpreted as whitespace? */
d66ee73a3dd7b5433fc0da3125bbdff740de7745Zbigniew Jędrzejewski-Szmek#define WHITESPACE " \t\n\r"
d66ee73a3dd7b5433fc0da3125bbdff740de7745Zbigniew Jędrzejewski-Szmek#define NEWLINE "\n\r"
d66ee73a3dd7b5433fc0da3125bbdff740de7745Zbigniew Jędrzejewski-Szmek#define QUOTES "\"\'"
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers#define COMMENTS "#;"
ac714a78fdca481488d88f84b6332d28083a4511Martin Jansa#define GLOB_CHARS "*?["
ac714a78fdca481488d88f84b6332d28083a4511Martin Jansa
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl/* What characters are special in the shell? */
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl/* must be escaped outside and inside double-quotes */
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl#define SHELL_NEED_ESCAPE "\"\\`$"
9a60da2834074d970ca063c210fe9d2f05c70532Thierry Reding/* can be escaped or double-quoted */
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define FORMAT_BYTES_MAX 8
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_RED_ON "\x1B[31m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_GREEN_ON "\x1B[32m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
eb2e280f9c59b66965c9316eadc4c113a13ca744Lucas De Marchisize_t page_size(void) _pure_;
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define streq(a,b) (strcmp((a),(b)) == 0)
27765dfc7a32d790badb29e6498b34edb0b60c33Lennart Poettering#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
780040dc2a4b08a2c1fe5bd8db3a70e966c2acb3Kay Sievers#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringbool streq_ptr(const char *a, const char *b) _pure_;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define new0(t, n) ((t*) calloc((n), sizeof(t)))
03930e48636e86ad84946253ca2bf4f91deeb645Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define malloc0(n) (calloc((n), 1))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringstatic inline const char* yes_no(bool b) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return b ? "yes" : "no";
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering}
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poetteringstatic inline const char* true_false(bool b) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return b ? "true" : "false";
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering}
8745297f9853c4a17bac69e1b7e652fe81bc1940Lennart Poettering
d200735e13c52dcfe36c0e066f9f6c2fbfb85a9cMichal Schmidtstatic inline const char* one_zero(bool b) {
213298fb822258bb69c6b85b7c8d7f019fd2306aLennart Poettering return b ? "1" : "0";
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poettering}
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringstatic inline const char* strempty(const char *s) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return s ? s : "";
9f64229f9ca69f5652b238a67531432e56108bd6Zbigniew Jędrzejewski-Szmek}
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieristatic inline const char* strnull(const char *s) {
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri return s ? s : "(null)";
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering}
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poettering
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poetteringstatic inline const char *strna(const char *s) {
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sievers return s ? s : "n/a";
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sievers}
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sievers
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sieversstatic inline bool isempty(const char *p) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek return !p || !p[0];
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering}
03a170c03cd4a09721369ece48affb70d66c6b90Kay Sievers
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekstatic inline char *startswith(const char *s, const char *prefix) {
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek size_t l;
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek l = strlen(prefix);
9e7adc3ae1133fa08a468768a490812299fad030Lucas De Marchi if (strncmp(s, prefix, l) == 0)
9e7adc3ae1133fa08a468768a490812299fad030Lucas De Marchi return (char*) s + l;
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri
9d2d0fe1e3f28a639c26b62391f79cfd1450d91bEvangelos Foutras return NULL;
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering}
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering
39c4ead2323b45bbe9866e0f97fd8dcfb8a0bedeZbigniew Jędrzejewski-Szmekstatic inline char *startswith_no_case(const char *s, const char *prefix) {
39c4ead2323b45bbe9866e0f97fd8dcfb8a0bedeZbigniew Jędrzejewski-Szmek size_t l;
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering l = strlen(prefix);
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek if (strncasecmp(s, prefix, l) == 0)
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek return (char*) s + l;
1c231f56482546725c4dbd3303f70300bd3c63e9Lennart Poettering
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek return NULL;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek}
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekchar *endswith(const char *s, const char *postfix) _pure_;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekchar *first_word(const char *s, const char *word) _pure_;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
32dcef3ab1eb91ee469c3246ef859578dccd8a45Zbigniew Jędrzejewski-Szmekint close_nointr(int fd);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint safe_close(int fd);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekvoid safe_close_pair(int p[]);
32dcef3ab1eb91ee469c3246ef859578dccd8a45Zbigniew Jędrzejewski-Szmek
32dcef3ab1eb91ee469c3246ef859578dccd8a45Zbigniew Jędrzejewski-Szmekvoid close_many(const int fds[], unsigned n_fd);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint parse_size(const char *t, off_t base, off_t *size);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint parse_boolean(const char *v) _pure_;
25ee45f9953c121fc26a54a85ad7bb3a3180152bMichael Bieblint parse_pid(const char *s, pid_t* ret_pid);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint parse_uid(const char *s, uid_t* ret_uid);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmekint safe_atou(const char *s, unsigned *ret_u);
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmekint safe_atoi(const char *s, int *ret_i);
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmek
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekint safe_atollu(const char *s, unsigned long long *ret_u);
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekint safe_atolli(const char *s, long long int *ret_i);
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmek
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekint safe_atod(const char *s, double *ret_d);
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekint safe_atou8(const char *s, uint8_t *ret);
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek#if LONG_MAX == INT_MAX
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek assert_cc(sizeof(unsigned long) == sizeof(unsigned));
f2ec0646aba7c6703a6c79603957e805b74c3befZbigniew Jędrzejewski-Szmek return safe_atou(s, (unsigned*) ret_u);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek}
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekstatic inline int safe_atoli(const char *s, long int *ret_u) {
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek assert_cc(sizeof(long int) == sizeof(int));
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek return safe_atoi(s, (int*) ret_u);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek}
afea26ad7d406d8b6c95d2642cb5a1d807b87546Lennart Poettering#else
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers return safe_atollu(s, (unsigned long long*) ret_u);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers}
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversstatic inline int safe_atoli(const char *s, long int *ret_u) {
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl assert_cc(sizeof(long int) == sizeof(long long int));
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl return safe_atolli(s, (long long int*) ret_u);
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl}
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#endif
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers assert_cc(sizeof(uint32_t) == sizeof(unsigned));
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers return safe_atou(s, (unsigned*) ret_u);
fba1ea06bb5b653e9eb0cc1b6004af8da273a4abShawn Landden}
fba1ea06bb5b653e9eb0cc1b6004af8da273a4abShawn Landden
fba1ea06bb5b653e9eb0cc1b6004af8da273a4abShawn Landdenstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl assert_cc(sizeof(int32_t) == sizeof(int));
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering return safe_atoi(s, (int*) ret_i);
9388e99e208a6487b26dcbda86005ee9eba8d93dMichael Olbrich}
4db17f291c627c885de668200ff8cce2e57c933fZbigniew Jędrzejewski-Szmek
3b794314149e40afaf3c456285e1e529747b6560Holger Schurigstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poettering assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
9388e99e208a6487b26dcbda86005ee9eba8d93dMichael Olbrich return safe_atollu(s, (unsigned long long*) ret_u);
3b794314149e40afaf3c456285e1e529747b6560Holger Schurig}
3b794314149e40afaf3c456285e1e529747b6560Holger Schurig
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poetteringstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
b237ef2cfac7ab0b33170809e8cb64628606207dTollef Fog Heen assert_cc(sizeof(int64_t) == sizeof(long long int));
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer return safe_atolli(s, (long long int*) ret_i);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering}
2270309471213a3c960543e523130627e9cb10e2Kay Sievers
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint safe_atou16(const char *s, uint16_t *ret);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint safe_atoi16(const char *s, int16_t *ret);
8114dedc5910d9a9cec702f6b6658551a0cd9edeChengwei Yang
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char* split(const char **state, size_t *l, const char *separator, bool quoted);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek#define FOREACH_WORD(word, length, s, state) \
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek
2270309471213a3c960543e523130627e9cb10e2Kay Sievers#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau _FOREACH_WORD(word, length, s, separator, false, state)
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek#define FOREACH_WORD_QUOTED(word, length, s, state) \
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekchar *strappend(const char *s, const char *suffix);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekchar *strnappend(const char *s, const char *suffix, size_t length);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenchar *replace_env(const char *format, char **env);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenchar **replace_env_argv(char **argv, char **env);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint readlinkat_malloc(int fd, const char *p, char **ret);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint readlink_malloc(const char *p, char **r);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint readlink_value(const char *p, char **ret);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint readlink_and_make_absolute(const char *p, char **r);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint readlink_and_canonicalize(const char *p, char **r);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint reset_all_signal_handlers(void);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint reset_signal_mask(void);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenchar *strstrip(char *s);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenchar *delete_chars(char *s, const char *bad);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenchar *truncate_nl(char *s);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenchar *file_in_same_dir(const char *path, const char *filename);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint rmdir_parents(const char *path, const char *stop);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint get_process_state(pid_t pid);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersenint get_process_comm(pid_t pid, char **name);
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint get_process_exe(pid_t pid, char **name);
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint get_process_uid(pid_t pid, uid_t *uid);
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint get_process_gid(pid_t pid, gid_t *gid);
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisnerint get_process_capeff(pid_t pid, char **capeff);
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisnerint get_process_cwd(pid_t pid, char **cwd);
a18535d9e138c525d0443ec9f30a90b3e2184686Tom Gundersenint get_process_root(pid_t pid, char **root);
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisnerint get_process_environ(pid_t pid, char **environ);
70d8320978dcbce022d9acbb953a10a7aca049abDavid Strauss
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisnerchar hexchar(int x) _const_;
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint unhexchar(char c) _const_;
c4955740969d7ba8ba43b024bca1a0a5b56eb8e8Tom Gundersenchar octchar(int x) _const_;
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint unoctchar(char c) _const_;
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenchar decchar(int x) _const_;
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersenint undecchar(char c) _const_;
f553b3b1074151200187df916427a1468186435eAnders Olofsson
f553b3b1074151200187df916427a1468186435eAnders Olofssonchar *cescape(const char *s);
f553b3b1074151200187df916427a1468186435eAnders Olofssonchar *cunescape(const char *s);
f553b3b1074151200187df916427a1468186435eAnders Olofssonchar *cunescape_length(const char *s, size_t length);
f553b3b1074151200187df916427a1468186435eAnders Olofssonchar *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
f553b3b1074151200187df916427a1468186435eAnders Olofsson
f553b3b1074151200187df916427a1468186435eAnders Olofssonchar *xescape(const char *s, const char *bad);
f553b3b1074151200187df916427a1468186435eAnders Olofsson
f553b3b1074151200187df916427a1468186435eAnders Olofssonchar *ascii_strlower(char *path);
f553b3b1074151200187df916427a1468186435eAnders Olofsson
f553b3b1074151200187df916427a1468186435eAnders Olofssonbool dirent_is_file(const struct dirent *de) _pure_;
f553b3b1074151200187df916427a1468186435eAnders Olofssonbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
728beb28a713709f521d374c9f8f3da781969d26Tom Gundersen
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalierbool hidden_file(const char *filename) _pure_;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalierbool chars_intersect(const char *a, const char *b) _pure_;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalierint make_stdio(int fd);
17df7223be064b1542dbe868e3b35cca977ee639Lennart Poetteringint make_null_stdio(void);
17df7223be064b1542dbe868e3b35cca977ee639Lennart Poetteringint make_console_stdio(void);
17df7223be064b1542dbe868e3b35cca977ee639Lennart Poettering
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalierint dev_urandom(void *p, size_t n);
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevaliervoid random_bytes(void *p, size_t n);
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevaliervoid initialize_srand(void);
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalierstatic inline uint64_t random_u64(void) {
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier uint64_t u;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier random_bytes(&u, sizeof(u));
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers return u;
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu}
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassustatic inline uint32_t random_u32(void) {
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu uint32_t u;
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu random_bytes(&u, sizeof(u));
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu return u;
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu}
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu/* For basic lookup tables with strictly enumerated entries */
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu scope const char *name##_to_string(type i) { \
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu return NULL; \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering return name##_table[i]; \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering }
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poetteringssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering scope inline type name##_from_string(const char *s) { \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering return (type)string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering }
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering/* For string conversions where numbers are also acceptable */
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering int name##_to_string_alloc(type i, char **str) { \
3f8cc098d218525710e5cbad9adf37001d3b6060Jan Engelhardt char *s; \
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering int r; \
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering if (i < 0 || i > max) \
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering return -ERANGE; \
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering if (i < (type) ELEMENTSOF(name##_table)) { \
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering s = strdup(name##_table[i]); \
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering if (!s) \
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering return log_oom(); \
56cf987fe74270bde4e16c7ec9e0414a9030723bDaniel J Walsh } else { \
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering r = asprintf(&s, "%i", i); \
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez if (r < 0) \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer return log_oom(); \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer } \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer *str = s; \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer return 0; \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer } \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer type name##_from_string(const char *s) { \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer type i; \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer unsigned u = 0; \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer assert(s); \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer if (name##_table[i] && \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer streq(name##_table[i], s)) \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer return i; \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer if (safe_atou(s, &u) >= 0 && u <= max) \
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer return (type) u; \
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez return (type) -1; \
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez } \
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez struct __useless_struct_to_allow_trailing_semicolon__
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezint fd_nonblock(int fd, bool nonblock);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezint fd_cloexec(int fd, bool cloexec);
5ec6b15b65304d94dc1c8cbad05c8b996b470d3aKay Sievers
56cf987fe74270bde4e16c7ec9e0414a9030723bDaniel J Walshint close_all_fds(const int except[], unsigned n_except);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezbool fstype_is_network(const char *fstype);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezint chvt(int vt);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezint read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezint ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezint ask_string(char **ret, const char *text, ...) _printf_(2, 3);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint reset_terminal_fd(int fd, bool switch_to_text);
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint reset_terminal(const char *name);
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint open_terminal(const char *name, int mode);
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint release_terminal(void);
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint flush_fd(int fd);
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint ignore_signals(int sig, ...);
807e17f05e217b474af39503efb9503d81b12596Lennart Poetteringint default_signals(int sig, ...);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint sigaction_many(const struct sigaction *sa, ...);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poettering
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringint fopen_temporary(const char *path, FILE **_f, char **_temp_path);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poettering
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringbool is_device_path(const char *path);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringint dir_is_empty(const char *path);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringchar* dirname_malloc(const char *path);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poettering
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringvoid rename_process(const char name[8]);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringvoid sigset_add_many(sigset_t *ss, ...);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringint sigprocmask_many(int how, ...);
812cce323db081634f37e4ec6d29f2b9328a3f52Lennart Poettering
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringbool hostname_is_set(void);
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringchar* lookup_uid(uid_t uid);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringchar* gethostname_malloc(void);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringchar* getlogname_malloc(void);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringchar* getusername_malloc(void);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poettering
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringint getttyname_malloc(int fd, char **r);
0213c3f8102bdc934c629d11a44ca0b408762287Lennart Poetteringint getttyname_harder(int fd, char **r);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint get_ctty_devnr(pid_t pid, dev_t *d);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint get_ctty(pid_t, dev_t *_devnr, char **r);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint is_fd_on_temporary_fs(int fd);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint pipe_eof(int fd);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringcpu_set_t* cpu_set_malloc(unsigned *ncpus);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint fd_columns(int fd);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringunsigned columns(void);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringint fd_lines(int fd);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringunsigned lines(void);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringvoid columns_lines_cache_reset(int _unused_ signum);
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poetteringbool on_tty(void);
812cce323db081634f37e4ec6d29f2b9328a3f52Lennart Poettering
812cce323db081634f37e4ec6d29f2b9328a3f52Lennart Poetteringstatic inline const char *ansi_highlight(void) {
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering return on_tty() ? ANSI_HIGHLIGHT_ON : "";
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering}
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poetteringstatic inline const char *ansi_highlight_red(void) {
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering}
5b6319dceedd81f3f1ce7eb70ea5defaef43bcecLennart Poettering
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversstatic inline const char *ansi_highlight_green(void) {
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering}
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringstatic inline const char *ansi_highlight_yellow(void) {
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering}
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringstatic inline const char *ansi_highlight_blue(void) {
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering}
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringstatic inline const char *ansi_highlight_off(void) {
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering}
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint files_same(const char *filea, const char *fileb);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint running_in_chroot(void);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringchar *ellipsize(const char *s, size_t length, unsigned percent);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering /* bytes columns */
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringchar *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint touch(const char *path);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringchar *unquote(const char *s, const char *quotes);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringchar *normalize_env_assignment(const char *s);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint wait_for_terminate(pid_t pid, siginfo_t *status);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringnoreturn void freeze(void);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poettering
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringbool null_or_empty(struct stat *st) _pure_;
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint null_or_empty_path(const char *fn);
5eda94dda25bccda928c4b33c790dbe748573a22Lennart Poetteringint null_or_empty_fd(int fd);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart PoetteringDIR *xopendirat(int dirfd, const char *name, int flags);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringchar *fstab_node_to_udev_node(const char *p);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringchar *resolve_dev_console(char **active);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool tty_is_vc(const char *tty);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool tty_is_vc_resolve(const char *tty);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool tty_is_console(const char *tty) _pure_;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint vtnr_from_tty(const char *tty);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringconst char *default_term_for_tty(const char *tty);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringvoid execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint kill_and_sigcont(pid_t pid, int sig);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool nulstr_contains(const char*nulstr, const char *needle);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool plymouth_running(void);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool hostname_is_valid(const char *s) _pure_;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringchar* hostname_cleanup(char *s, bool lowercase);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringbool machine_name_is_valid(const char *s) _pure_;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringchar* strshorten(char *s, size_t l);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint terminal_vhangup_fd(int fd);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint terminal_vhangup(const char *name);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint vt_disallocate(const char *name);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint symlink_atomic(const char *from, const char *to);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint mknod_atomic(const char *path, mode_t mode, dev_t dev);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint mkfifo_atomic(const char *path, mode_t mode);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poetteringint fchmod_umask(int fd, mode_t mode);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskibool display_is_local(const char *display) _pure_;
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint socket_from_display(const char *display, char **path);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint get_group_creds(const char **groupname, gid_t *gid);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint in_gid(gid_t gid);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint in_group(const char *name);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskichar* uid_to_name(uid_t uid);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskichar* gid_to_name(gid_t gid);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint glob_exists(const char *path);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint glob_extend(char ***strv, const char *path);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint dirent_ensure_type(DIR *d, struct dirent *de);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poetteringint get_files_in_directory(const char *path, char ***list);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskichar *strjoin(const char *x, ...) _sentinel_;
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskibool is_main_thread(void);
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kok
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kokstatic inline bool _pure_ in_charset(const char *s, const char* charset) {
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kok assert(s);
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kok assert(charset);
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kok return s[strspn(s, charset)] == '\0';
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kok}
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowskiint block_get_whole_disk(dev_t d, dev_t *ret);
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski
2b3e18de74ca89b374dd4f7a2c30e5731d347841Karol Lewandowski#define NULSTR_FOREACH(i, l) \
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
feb12d3ed2c7f9132c64773c7c41b9e3a608a814Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering#define NULSTR_FOREACH_PAIR(i, j, l) \
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint ioprio_class_to_string_alloc(int i, char **s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint ioprio_class_from_string(const char *s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringconst char *sigchld_code_to_string(int i) _const_;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint sigchld_code_from_string(const char *s) _pure_;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint log_facility_unshifted_to_string_alloc(int i, char **s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint log_facility_unshifted_from_string(const char *s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint log_level_to_string_alloc(int i, char **s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint log_level_from_string(const char *s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint sched_policy_to_string_alloc(int i, char **s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint sched_policy_from_string(const char *s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringconst char *rlimit_to_string(int i) _const_;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint rlimit_from_string(const char *s) _pure_;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint ip_tos_to_string_alloc(int i, char **s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint ip_tos_from_string(const char *s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringconst char *signal_to_string(int i) _const_;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint signal_from_string(const char *s) _pure_;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringint signal_from_string_try_harder(const char *s);
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringextern int saved_argc;
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poetteringextern char **saved_argv;
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringbool kexec_loaded(void);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint prot_from_flags(int flags) _const_;
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringchar *format_bytes(char *buf, size_t l, off_t t);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint fd_wait_for_event(int fd, int event, usec_t timeout);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringvoid* memdup(const void *p, size_t l) _alloc_(2);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint is_kernel_thread(pid_t pid);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint fd_inc_sndbuf(int fd, size_t n);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint fd_inc_rcvbuf(int fd, size_t n);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint setrlimit_closest(int resource, const struct rlimit *rlim);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint getenv_for_pid(pid_t pid, const char *field, char **_value);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringbool http_url_is_valid(const char *url) _pure_;
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringbool documentation_url_is_valid(const char *url) _pure_;
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringbool http_etag_is_valid(const char *etag);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringbool in_initrd(void);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringvoid warn_melody(void);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint get_home_dir(char **ret);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringint get_shell(char **_ret);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poetteringstatic inline void freep(void *p) {
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering free(*(void**) p);
4927fcae48de061393b3ce9c12d49f80d73fbf1dLennart Poettering}
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
7f4e08056de0184b205a20632e62db73d299937eLennart Poetteringstatic inline void closep(int *fd) {
7f4e08056de0184b205a20632e62db73d299937eLennart Poettering safe_close(*fd);
7f4e08056de0184b205a20632e62db73d299937eLennart Poettering}
8cf3ca80680b43015971cbbf4625517ae859d50cJan Janssen
7f4e08056de0184b205a20632e62db73d299937eLennart Poetteringstatic inline void umaskp(mode_t *u) {
7f4e08056de0184b205a20632e62db73d299937eLennart Poettering umask(*u);
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering}
7f4e08056de0184b205a20632e62db73d299937eLennart Poettering
7f4e08056de0184b205a20632e62db73d299937eLennart Poetteringstatic inline void close_pairp(int (*p)[2]) {
7f4e08056de0184b205a20632e62db73d299937eLennart Poettering safe_close_pair(*p);
7f4e08056de0184b205a20632e62db73d299937eLennart Poettering}
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define _cleanup_free_ _cleanup_(freep)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define _cleanup_close_ _cleanup_(closep)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define _cleanup_umask_ _cleanup_(umaskp)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define _cleanup_globfree_ _cleanup_(globfree)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define _cleanup_fclose_ _cleanup_(fclosep)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define _cleanup_pclose_ _cleanup_(pclosep)
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering#define _cleanup_closedir_ _cleanup_(closedirp)
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering#define _cleanup_endmntent_ _cleanup_(endmntentp)
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering#define _cleanup_close_pair_ _cleanup_(close_pairp)
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering
59bb9d9a14889bee001706a32a518fe0a5009048Zbigniew Jędrzejewski-Szmek_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering return NULL;
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering return malloc(a * b);
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering}
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering
7b17a7d72f5ba5ad838b19803534c56a46f3bce9Lennart Poettering_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
27669061f40766457db93d5cc3dfe00dce240806Miklos Vajna return NULL;
27669061f40766457db93d5cc3dfe00dce240806Miklos Vajna
27669061f40766457db93d5cc3dfe00dce240806Miklos Vajna return realloc(p, a * b);
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers}
27669061f40766457db93d5cc3dfe00dce240806Miklos Vajna
27669061f40766457db93d5cc3dfe00dce240806Miklos Vajna_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
27669061f40766457db93d5cc3dfe00dce240806Miklos Vajna if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers return NULL;
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering return memdup(p, a * b);
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering}
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poetteringbool filename_is_valid(const char *p) _pure_;
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poetteringbool path_is_safe(const char *p) _pure_;
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poetteringbool string_is_safe(const char *p) _pure_;
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversbool string_has_cc(const char *p, const char *ok) _pure_;
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering/**
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering * Check if a string contains any glob patterns.
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers */
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering_pure_ static inline bool string_is_glob(const char *p) {
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering return !!strpbrk(p, GLOB_CHARS);
e5e83e8362e946890ac991fc86a2c5869f9befdfLennart Poettering}
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kok
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kokvoid *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kok int (*compar) (const void *, const void *, void *),
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kok void *arg);
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kok
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kok#define _(String) gettext (String)
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kokvoid init_gettext(void);
83fdc450aa8f79941bec84488ffd5bf8eadab18eAuke Kokbool is_locale_utf8(void);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poetteringtypedef enum DrawSpecialChar {
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering DRAW_TREE_VERTICAL,
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering DRAW_TREE_BRANCH,
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers DRAW_TREE_RIGHT,
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering DRAW_TREE_SPACE,
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering DRAW_TRIANGULAR_BULLET,
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering DRAW_BLACK_CIRCLE,
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyar DRAW_ARROW,
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyar DRAW_DASH,
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyar _DRAW_SPECIAL_CHAR_MAX
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyar} DrawSpecialChar;
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyar
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyarconst char *draw_special_char(DrawSpecialChar ch);
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyar
6351163bf3e519cc07adb2732d12450741f5a0d3Umut Tezduyarchar *strreplace(const char *text, const char *old_string, const char *new_string);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poetteringchar *strip_tab_ansi(char **p, size_t *l);
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poetteringint on_ac_power(void);
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poetteringint search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poetteringint search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
4de856120f252e7aa19c923c10fbf23310d623aaLennart Poettering
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering#define FOREACH_LINE(line, f, on_error) \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering for (;;) \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering if (!fgets(line, sizeof(line), f)) { \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering if (ferror(f)) { \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering on_error; \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering } \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering break; \
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering } else
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering#define FOREACH_DIRENT(de, d, on_error) \
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering if (!de) { \
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering if (errno > 0) { \
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering on_error; \
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering } \
3990f247652c3bd41e4ea074e6302277eb9c7aa3Lennart Poettering break; \
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers } else if (hidden_file((de)->d_name)) \
2a018e83ded29c9719b2478a65ee6245c829c0f5Kay Sievers continue; \
2a018e83ded29c9719b2478a65ee6245c829c0f5Kay Sievers else
2a018e83ded29c9719b2478a65ee6245c829c0f5Kay Sievers
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers#define FOREACH_DIRENT_ALL(de, d, on_error) \
2a018e83ded29c9719b2478a65ee6245c829c0f5Kay Sievers for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
2a018e83ded29c9719b2478a65ee6245c829c0f5Kay Sievers if (!de) { \
4c80c73c2b804576b1de27e644c1da4dab2f9026Kay Sievers if (errno > 0) { \
2a018e83ded29c9719b2478a65ee6245c829c0f5Kay Sievers on_error; \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break; \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic inline void *mempset(void *s, int c, size_t n) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering memset(s, c, n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return (uint8_t*)s + n;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieverschar *hexmem(const void *p, size_t l);
b2e9fb99ab288e8817302851743ed1a3cddd384bMiklos Vajnavoid *unhexmem(const char *p, size_t l);
b2e9fb99ab288e8817302851743ed1a3cddd384bMiklos Vajna
b2e9fb99ab288e8817302851743ed1a3cddd384bMiklos Vajnachar *strextend(char **x, ...) _sentinel_;
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sieverschar *strrep(const char *s, unsigned n);
b2e9fb99ab288e8817302851743ed1a3cddd384bMiklos Vajna
b2e9fb99ab288e8817302851743ed1a3cddd384bMiklos Vajnavoid* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
b2e9fb99ab288e8817302851743ed1a3cddd384bMiklos Vajnavoid* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers#define GREEDY_REALLOC(array, allocated, need) \
f47cd184c0ff80e025428e9e385e61bda1ef3d69Miklos Vajna greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
f47cd184c0ff80e025428e9e385e61bda1ef3d69Miklos Vajna
f47cd184c0ff80e025428e9e385e61bda1ef3d69Miklos Vajna#define GREEDY_REALLOC0(array, allocated, need) \
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
f47cd184c0ff80e025428e9e385e61bda1ef3d69Miklos Vajna
f47cd184c0ff80e025428e9e385e61bda1ef3d69Miklos Vajnastatic inline void _reset_errno_(int *saved_errno) {
f47cd184c0ff80e025428e9e385e61bda1ef3d69Miklos Vajna errno = *saved_errno;
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers}
4cd1eaa54507a65286413363216ad407fa7c6e50Miklos Vajna
4cd1eaa54507a65286413363216ad407fa7c6e50Miklos Vajna#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
4cd1eaa54507a65286413363216ad407fa7c6e50Miklos Vajna
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sieversstatic inline int negative_errno(void) {
4cd1eaa54507a65286413363216ad407fa7c6e50Miklos Vajna /* This helper should be used to shut up gcc if you know 'errno' is
4cd1eaa54507a65286413363216ad407fa7c6e50Miklos Vajna * negative. Instead of "return -errno;", use "return negative_errno();"
4cd1eaa54507a65286413363216ad407fa7c6e50Miklos Vajna * It will suppress bogus gcc warnings in case it assumes 'errno' might
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers * be 0 and thus the caller's error-handling might not be triggered. */
f5e04665ebf7124f3ea17dcf258793ed73a95fe1Lennart Poettering assert_return(errno > 0, -EINVAL);
f5e04665ebf7124f3ea17dcf258793ed73a95fe1Lennart Poettering return -errno;
f5e04665ebf7124f3ea17dcf258793ed73a95fe1Lennart Poettering}
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers
f5e04665ebf7124f3ea17dcf258793ed73a95fe1Lennart Poetteringstruct _umask_struct_ {
f5e04665ebf7124f3ea17dcf258793ed73a95fe1Lennart Poettering mode_t mask;
f5e04665ebf7124f3ea17dcf258793ed73a95fe1Lennart Poettering bool quit;
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering};
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poetteringstatic inline void _reset_umask_(struct _umask_struct_ *s) {
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering umask(s->mask);
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering};
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering#define RUN_WITH_UMASK(mask) \
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
46ba8aae2b82bc5c87ba347e6bf914ecd5e9d51eLennart Poettering !_saved_umask_.quit ; \
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters _saved_umask_.quit = true)
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Waltersstatic inline unsigned u64log2(uint64_t n) {
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters#if __SIZEOF_LONG_LONG__ == 8
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters#else
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters#error "Wut?"
3c9317d2d93248f3f0b0feaaa113485aee415d8aColin Walters#endif
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering}
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poetteringstatic inline unsigned u32ctz(uint32_t n) {
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering#if __SIZEOF_INT__ == 4
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering return __builtin_ctz(n);
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering#else
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering#error "Wut?"
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering#endif
b872e9a05939bc3e0ac95a042592506a7488dd6fLennart Poettering}
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmek
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmekstatic inline unsigned log2i(int x) {
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmek assert(x > 0);
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmek
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmek return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmek}
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmek
bd441fa27a22b7c6e11d9330560e0622fb69f297Zbigniew Jędrzejewski-Szmekstatic inline unsigned log2u(unsigned x) {
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poettering assert(x > 0);
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poettering
c97a6dbcf10990bcc2455e842a41bf14b01f358dLukasz Skalski return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
146ccaafd5c9d4f2798240b049f595f70f45f77bDave Reisner}
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poettering
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poetteringstatic inline unsigned log2u_round_up(unsigned x) {
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek assert(x > 0);
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poettering
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poettering if (x == 1)
626851be97b4332fc0401d754c81ae7bbc0f5dc4Lennart Poettering return 0;
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering return log2u(x - 1) + 1;
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering}
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poetteringstatic inline bool logind_running(void) {
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering return access("/run/systemd/seats/", F_OK) >= 0;
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering}
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering#define DECIMAL_STR_WIDTH(x) \
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poettering ({ \
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering typeof(x) _x_ = (x); \
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poettering unsigned ans = 1; \
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering while (_x_ /= 10) \
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering ans++; \
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poettering ans; \
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering })
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poetteringint unlink_noerrno(const char *path);
b1c4ca25bf58e1925012d1dcdd83d61cecbf87fbLennart Poettering
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering#define alloca0(n) \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering ({ \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering char *_new_; \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering size_t _len_ = n; \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering _new_ = alloca(_len_); \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering (void *) memset(_new_, 0, _len_); \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering })
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering#define alloca_align(size, align) \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering ({ \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering void *_ptr_; \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering size_t _mask_ = (align) - 1; \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering _ptr_ = alloca((size) + _mask_); \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering })
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering#define alloca0_align(size, align) \
a382332eed10d3348231803c47a4c599d24c5e3aLennart Poettering ({ \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez void *_new_; \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez size_t _size_ = (size); \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez _new_ = alloca_align(_size_, (align)); \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez (void*)memset(_new_, 0, _size_); \
abaaabf40a9891014ed4c402d7beb5a67ac256b1Lennart Poettering })
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez#define strjoina(a, ...) \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez ({ \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez const char *_appendees_[] = { a, __VA_ARGS__ }; \
4ad61fd1806dde23d2c99043b4bed91a196d2c82Cristian Rodríguez char *_d_, *_p_; \
54b434b1b5055f934230fe04fad35b01642b8488Lennart Poettering int _len_ = 0; \
7211f918ba2b86e0041413b47d51d7593e73cf5dLennart Poettering unsigned _i_; \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen _len_ += strlen(_appendees_[_i_]); \
f8c5a5815ff7c137d9a1e5d7b48a6532b9f31407Lennart Poettering _p_ = _d_ = alloca(_len_ + 1); \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen _p_ = stpcpy(_p_, _appendees_[_i_]); \
a3b6fafed441d96380a3f089118f7486d6aa3126Lennart Poettering *_p_ = 0; \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen _d_; \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen })
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen#define procfs_file_alloca(pid, field) \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen ({ \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen pid_t _pid_ = (pid); \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen const char *_r_; \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen if (_pid_ == 0) { \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen _r_ = ("/proc/self/" field); \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen } else { \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
d562955eac58d3a5089e0f344ea586412a134451Tom Gundersen } \
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers _r_; \
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers })
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
a3bd8447be4ea2ce230eb8ae0e815c04d85fa15aTom Gundersenbool id128_is_valid(const char *s) _pure_;
a3bd8447be4ea2ce230eb8ae0e815c04d85fa15aTom Gundersen
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint split_pair(const char *s, const char *sep, char **l, char **r);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint shall_restore_state(void);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers/**
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers * Normal qsort requires base to be nonnull. Here were require
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers * that only if nmemb > 0.
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers */
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversstatic inline void qsort_safe(void *base, size_t nmemb, size_t size,
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers int (*compar)(const void *, const void *)) {
a3bd8447be4ea2ce230eb8ae0e815c04d85fa15aTom Gundersen if (nmemb) {
d8d4bee76cf3b40ea923bc57d44aa0815ca9b5ffKay Sievers assert(base);
a3bd8447be4ea2ce230eb8ae0e815c04d85fa15aTom Gundersen qsort(base, nmemb, size, compar);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers }
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers}
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint proc_cmdline(char **ret);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
de1c301ed165eb4d04a0c9d4babe97912b5233bbLennart Poetteringint get_proc_cmdline_key(const char *parameter, char **value);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
de1c301ed165eb4d04a0c9d4babe97912b5233bbLennart Poetteringint container_get_leader(const char *machine, pid_t *pid);
de1c301ed165eb4d04a0c9d4babe97912b5233bbLennart Poettering
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
75db9a77605b33a1b9355eae957f26380441fce6Lennart Poetteringint namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
4c2b0e4e423fe73fafef10a5efa6fb778784cebcLennart Poettering
4ca39b280fce3c60d2fdecbd478fd9bf7f9d3e64Mike Gilbertbool pid_is_alive(pid_t pid);
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmekbool pid_is_unwaited(pid_t pid);
75db9a77605b33a1b9355eae957f26380441fce6Lennart Poettering
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sieversint getpeercred(int fd, struct ucred *ucred);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringint getpeersec(int fd, char **ret);
34eff652cedec3cac34b92629ead5f140334b689Lennart Poettering
34eff652cedec3cac34b92629ead5f140334b689Lennart Poetteringint writev_safe(int fd, const struct iovec *w, int j);
f1dd0c3f9b4a257e81ff9c6a08070c702a0db45aLennart Poettering
12e84679cc81cdf5f042540e54131a0ce37147adLennart Poetteringint mkostemp_safe(char *pattern, int flags);
136337ff74f05be3d42a769d9f0cb99716c5c40fTollef Fog Heenint open_tmpfile(const char *path, int flags);
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poetteringint fd_warn_permissions(const char *path, int fd);
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sievers
be31376e6c9add0786f31a38eec2ecfdb73eb115Kay Sieversunsigned long personality_from_string(const char *p);
136337ff74f05be3d42a769d9f0cb99716c5c40fTollef Fog Heenconst char *personality_to_string(unsigned long);
dee4c244254bb49d1ffa8bd7171ae9cce596d2d0Lennart Poettering
dee4c244254bb49d1ffa8bd7171ae9cce596d2d0Lennart Poetteringuint64_t physical_memory(void);
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poettering
136337ff74f05be3d42a769d9f0cb99716c5c40fTollef Fog Heenvoid hexdump(FILE *f, const void *p, size_t s);
136337ff74f05be3d42a769d9f0cb99716c5c40fTollef Fog Heen
136337ff74f05be3d42a769d9f0cb99716c5c40fTollef Fog Heenunion file_handle_union {
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncio struct file_handle handle;
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncio char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncio};
f975e971accc4d50c73ae53167db3df7a7099cf2Lennart Poettering#define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncio
dee4c244254bb49d1ffa8bd7171ae9cce596d2d0Lennart Poetteringint update_reboot_param_file(const char *param);
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncio
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncioint umount_recursive(const char *target, int flags);
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncio
07459bb6b92268beb2599f65cf195708d88c51ccFabiano Fidêncioint bind_remount_recursive(const char *prefix, bool ro);
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poettering
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poetteringint fflush_and_check(FILE *f);
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poettering
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poetteringint tempfn_xxxxxx(const char *p, char **ret);
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poetteringint tempfn_random(const char *p, char **ret);
bc2708414babc5c99bb8000e63c84e87606cc15dLennart Poetteringint tempfn_random_child(const char *p, char **ret);
bef2733fae665e880da6ea011b1f918e6900bb42Lennart Poettering
a05ea46dc788929bc8b13afb4208ca29cad68572Michael Bieblbool is_localhost(const char *hostname);
bef2733fae665e880da6ea011b1f918e6900bb42Lennart Poettering
bef2733fae665e880da6ea011b1f918e6900bb42Lennart Poetteringint take_password_lock(const char *root);
bef2733fae665e880da6ea011b1f918e6900bb42Lennart Poettering
bef2733fae665e880da6ea011b1f918e6900bb42Lennart Poetteringint is_symlink(const char *path);
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poetteringint is_dir(const char *path, bool follow);
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poetteringint unquote_first_word(const char **p, char **ret, bool relax);
25ee45f9953c121fc26a54a85ad7bb3a3180152bMichael Bieblint unquote_many_words(const char **p, ...) _sentinel_;
d122948d6fbaac4505cf14a08f1237daa89efdd0Lennart Poettering
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poetteringint free_and_strdup(char **p, const char *s);
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poetteringint sethostname_idempotent(const char *s);
25ee45f9953c121fc26a54a85ad7bb3a3180152bMichael Biebl
d122948d6fbaac4505cf14a08f1237daa89efdd0Lennart Poettering#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering for ((e) = &buffer.ev; \
03c149144d374cffd40a17ffb067837e3e220933Michael Biebl (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
d122948d6fbaac4505cf14a08f1237daa89efdd0Lennart Poettering (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poetteringunion inotify_event_buffer {
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poettering struct inotify_event ev;
03c149144d374cffd40a17ffb067837e3e220933Michael Biebl uint8_t raw[INOTIFY_EVENT_MAX];
d122948d6fbaac4505cf14a08f1237daa89efdd0Lennart Poettering};
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Biebl
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Biebl#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Biebl
25ee45f9953c121fc26a54a85ad7bb3a3180152bMichael Bieblint ptsname_malloc(int fd, char **ret);
25ee45f9953c121fc26a54a85ad7bb3a3180152bMichael Biebl
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Bieblint openpt_in_namespace(pid_t pid, int flags);
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Biebl
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Bieblssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
db059f1b031a92ed45b62d1036877abd9c128d1bMichael Biebl
4f87c47b35cf9c1f58872559ae67a2656712fdd6William Giokasint fd_setcrtime(int fd, usec_t usec);
4f87c47b35cf9c1f58872559ae67a2656712fdd6William Giokasint fd_getcrtime(int fd, usec_t *usec);
4f87c47b35cf9c1f58872559ae67a2656712fdd6William Giokasint path_getcrtime(const char *p, usec_t *usec);
4f87c47b35cf9c1f58872559ae67a2656712fdd6William Giokasint fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
b8079ae19b41c9b61850c796dddc601b826850e0Kay Sievers
b8079ae19b41c9b61850c796dddc601b826850e0Kay Sieversint same_fd(int a, int b);
39ad55a9aa99451f982628af6e14581092b2d2f6Kay Sievers
d2d12cd1bfd90f3a13273d82331a7cbd36a93231Lennart Poetteringint chattr_fd(int fd, bool b, unsigned mask);
ae446765eb0605d2451bb4dd7c336672bcc7ab0cKay Sieversint chattr_path(const char *p, bool b, unsigned mask);
ae446765eb0605d2451bb4dd7c336672bcc7ab0cKay Sievers
ae446765eb0605d2451bb4dd7c336672bcc7ab0cKay Sieversint read_attr_fd(int fd, unsigned *ret);
ae446765eb0605d2451bb4dd7c336672bcc7ab0cKay Sieversint read_attr_path(const char *p, unsigned *ret);
ae446765eb0605d2451bb4dd7c336672bcc7ab0cKay Sievers
9d3203b4765e64300ab8e8d6d3d1b9ed0c514d5eKay Sieverstypedef struct LockFile {
b0cca7d886583a10b9342796b4619eca4d582cb2Michael Biebl char *path;
9d3203b4765e64300ab8e8d6d3d1b9ed0c514d5eKay Sievers int fd;
9d3203b4765e64300ab8e8d6d3d1b9ed0c514d5eKay Sievers int operation;
9d3203b4765e64300ab8e8d6d3d1b9ed0c514d5eKay Sievers} LockFile;
5c390a4ae0d383b2003074ed011d47876c7e630cZbigniew Jędrzejewski-Szmek
5c390a4ae0d383b2003074ed011d47876c7e630cZbigniew Jędrzejewski-Szmekint make_lock_file(const char *p, int operation, LockFile *ret);
5c390a4ae0d383b2003074ed011d47876c7e630cZbigniew Jędrzejewski-Szmekint make_lock_file_for(const char *p, int operation, LockFile *ret);
5c390a4ae0d383b2003074ed011d47876c7e630cZbigniew Jędrzejewski-Szmekvoid release_lock_file(LockFile *f);
5c390a4ae0d383b2003074ed011d47876c7e630cZbigniew Jędrzejewski-Szmek
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny#define _cleanup_release_lock_file_ _cleanup_(release_lock_file)
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny#define LOCK_FILE_INIT { .fd = -1, .path = NULL }
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górnyssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górnyvoid sigkill_wait(pid_t *pid);
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górny#define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait)
2c6db6fb9b1a10184b086df0d23228c4c0205a49Lennart Poettering
bc9bdbbab45ea2c10fdd8ad7c517b5e4a358bdfbMichał Górnyint syslog_parse_priority(const char **p, int *priority, bool with_facility);
2c6db6fb9b1a10184b086df0d23228c4c0205a49Lennart Poettering