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