util.h revision 984a2be450abac81474889b8bea4b3fbeddb26c5
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart 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 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 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/>.
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering uint8_t storage[offsetof(struct dirent, d_name) +
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering ((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))];
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering/* What is interpreted as whitespace? */
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_ERASE_TO_END_OF_LINE "\x1B[K"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
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)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringbool streq_ptr(const char *a, const char *b);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define new0(t, n) ((t*) calloc((n), sizeof(t)))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringstatic inline const char* yes_no(bool b) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringstatic inline const char* strempty(const char *s) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return s ? s : "";
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringstatic inline const char* strnull(const char *s) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return s ? s : "(null)";
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic inline const char *strna(const char *s) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return s ? s : "n/a";
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline bool isempty(const char *p) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return !p || !p[0];
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 Poetteringbool first_word(const char *s, const char *word);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid close_many(const int fds[], unsigned n_fd);
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 Poetteringint safe_atou(const char *s, unsigned *ret_u);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint safe_atollu(const char *s, unsigned long long *ret_u);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint safe_atolli(const char *s, long long int *ret_i);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poetteringint safe_atod(const char *s, double *ret_d);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_cc(sizeof(unsigned long) == sizeof(unsigned));
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoli(const char *s, long int *ret_u) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert_cc(sizeof(long int) == sizeof(int));
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 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 Poetteringstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering assert_cc(sizeof(uint32_t) == sizeof(unsigned));
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-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-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-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 for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
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#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 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 Gundersenchar *strappend(const char *s, const char *suffix);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poetteringchar *strnappend(const char *s, const char *suffix, size_t length);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekchar *replace_env(const char *format, char **env);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekchar **replace_env_argv(char **argv, char **env);
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-Szmekint reset_all_signal_handlers(void);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekchar *delete_chars(char *s, const char *bad);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekchar *file_in_same_dir(const char *path, const char *filename);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint rmdir_parents(const char *path, const char *stop);
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);
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 Gundersenchar *bus_path_escape(const char *s);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenchar *bus_path_unescape(const char *s);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringbool dirent_is_file(const struct dirent *de);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringbool chars_intersect(const char *a, const char *b);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenunsigned long long random_ull(void);
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)) \
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering scope type name##_from_string(const char *s) { \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
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)
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) { \
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (i < 0 || i > max) \
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (i < (type) ELEMENTSOF(name##_table)) { \
*str = s; \
type i; \
assert(s); \
return (type) u; \
int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
int release_terminal(void);
int close_pipe(int p[]);
bool hostname_is_set(void);
char* gethostname_malloc(void);
char* getlogname_malloc(void);
char* getusername_malloc(void);
int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap);
int status_welcome(void);
unsigned columns(void);
unsigned lines(void);
bool on_tty(void);
int running_in_chroot(void);
char *normalize_env_assignment(const char *s);
char *fstab_node_to_udev_node(const char *p);
bool plymouth_running(void);
bool hostname_is_valid(const char *s);
char* hostname_cleanup(char *s);
int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
bool is_main_thread(void);
int file_is_priv_sticky(const char *p);
int strdup_or_null(const char *a, char **b);
#define NULSTR_FOREACH(i, l) \
#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);
bool in_initrd(void);
void warn_melody(void);
void freep(void *p);
return NULL;
return malloc(a * 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);
int (*compar) (const void *, const void *, void *),
void *arg);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
int on_ac_power(void);
if (ferror(f)) { \
on_error; \
if (!de) { \
if (errno != 0) { \
on_error; \