]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/util.h
service/systemctl: don't consider LSB exit codes 5 and 6 as failure, and decode exit...
[thirdparty/systemd.git] / src / util.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275
LP
2
3#ifndef fooutilhfoo
4#define fooutilhfoo
5
a7334b09
LP
6/***
7 This file is part of systemd.
8
9 Copyright 2010 Lennart Poettering
10
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23***/
24
60918275
LP
25#include <inttypes.h>
26#include <time.h>
27#include <sys/time.h>
28#include <stdbool.h>
5cb5a6ff 29#include <stdlib.h>
80876c20 30#include <stdio.h>
9a34ec5f 31#include <signal.h>
82c121a4 32#include <sched.h>
8f75a603 33#include <limits.h>
60918275 34
a838e6a1
LP
35#include "macro.h"
36
60918275
LP
37typedef uint64_t usec_t;
38
63983207 39typedef struct dual_timestamp {
871d7de4
LP
40 usec_t realtime;
41 usec_t monotonic;
63983207 42} dual_timestamp;
871d7de4 43
034c6ed7
LP
44#define MSEC_PER_SEC 1000ULL
45#define USEC_PER_SEC 1000000ULL
46#define USEC_PER_MSEC 1000ULL
47#define NSEC_PER_SEC 1000000000ULL
48#define NSEC_PER_MSEC 1000000ULL
60918275
LP
49#define NSEC_PER_USEC 1000ULL
50
24a6e4a4
LP
51#define USEC_PER_MINUTE (60ULL*USEC_PER_SEC)
52#define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE)
53#define USEC_PER_DAY (24ULL*USEC_PER_HOUR)
54#define USEC_PER_WEEK (7ULL*USEC_PER_DAY)
55
44d8db9e 56/* What is interpreted as whitespace? */
4a72ff34 57#define WHITESPACE " \t\n\r"
7072ced8 58#define NEWLINE "\n\r"
44d8db9e 59
8b6c7120 60#define FORMAT_TIMESTAMP_MAX 64
871d7de4 61#define FORMAT_TIMESPAN_MAX 64
8b6c7120 62
61cbdc4b 63#define ANSI_HIGHLIGHT_ON "\x1B[1;31m"
2ee68f72 64#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
61cbdc4b
LP
65#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
66
60918275
LP
67usec_t now(clockid_t clock);
68
63983207 69dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
871d7de4 70
60918275
LP
71usec_t timespec_load(const struct timespec *ts);
72struct timespec *timespec_store(struct timespec *ts, usec_t u);
73
74usec_t timeval_load(const struct timeval *tv);
75struct timeval *timeval_store(struct timeval *tv, usec_t u);
76
77#define streq(a,b) (strcmp((a),(b)) == 0)
78
e05797fb
LP
79bool streq_ptr(const char *a, const char *b);
80
60918275
LP
81#define new(t, n) ((t*) malloc(sizeof(t)*(n)))
82
83#define new0(t, n) ((t*) calloc((n), sizeof(t)))
84
85#define malloc0(n) (calloc((n), 1))
86
87static inline const char* yes_no(bool b) {
88 return b ? "yes" : "no";
89}
90
91static inline const char* strempty(const char *s) {
92 return s ? s : "";
93}
94
95static inline const char* strnull(const char *s) {
96 return s ? s : "(null)";
97}
98
04fd6fe4
LP
99static inline const char *strna(const char *s) {
100 return s ? s : "n/a";
101}
102
103static inline bool is_path_absolute(const char *p) {
104 return *p == '/';
105}
106
60918275
LP
107bool endswith(const char *s, const char *postfix);
108bool startswith(const char *s, const char *prefix);
3177a7fa 109bool startswith_no_case(const char *s, const char *prefix);
60918275 110
79d6d816
LP
111bool first_word(const char *s, const char *word);
112
42f4e3c4 113int close_nointr(int fd);
85f136b5 114void close_nointr_nofail(int fd);
5b6319dc 115void close_many(const int fds[], unsigned n_fd);
60918275 116
85261803 117int parse_boolean(const char *v);
24a6e4a4 118int parse_usec(const char *t, usec_t *usec);
3ba686c1 119int parse_pid(const char *s, pid_t* ret_pid);
85261803
LP
120
121int safe_atou(const char *s, unsigned *ret_u);
122int safe_atoi(const char *s, int *ret_i);
123
8f75a603
LP
124int safe_atollu(const char *s, unsigned long long *ret_u);
125int safe_atolli(const char *s, long long int *ret_i);
126
127#if __WORDSIZE == 32
128static inline int safe_atolu(const char *s, unsigned long *ret_u) {
129 assert_cc(sizeof(unsigned long) == sizeof(unsigned));
130 return safe_atou(s, (unsigned*) ret_u);
131}
132static inline int safe_atoli(const char *s, long int *ret_u) {
133 assert_cc(sizeof(long int) == sizeof(int));
134 return safe_atoi(s, (int*) ret_u);
135}
136#else
137static inline int safe_atolu(const char *s, unsigned long *ret_u) {
138 assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
139 return safe_atollu(s, (unsigned long long*) ret_u);
140}
141static inline int safe_atoli(const char *s, long int *ret_u) {
142 assert_cc(sizeof(long int) == sizeof(long long int));
143 return safe_atolli(s, (long long int*) ret_u);
144}
145#endif
146
a838e6a1
LP
147static inline int safe_atou32(const char *s, uint32_t *ret_u) {
148 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
149 return safe_atou(s, (unsigned*) ret_u);
150}
151
8f75a603 152static inline int safe_atoi32(const char *s, int32_t *ret_i) {
a838e6a1 153 assert_cc(sizeof(int32_t) == sizeof(int));
8f75a603 154 return safe_atoi(s, (int*) ret_i);
a838e6a1
LP
155}
156
8f75a603
LP
157static inline int safe_atou64(const char *s, uint64_t *ret_u) {
158 assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
159 return safe_atollu(s, (unsigned long long*) ret_u);
160}
034c6ed7 161
8f75a603
LP
162static inline int safe_atoi64(const char *s, int64_t *ret_i) {
163 assert_cc(sizeof(int64_t) == sizeof(long long int));
164 return safe_atolli(s, (long long int*) ret_i);
165}
034c6ed7 166
65d2ebdc 167char *split(const char *c, size_t *l, const char *separator, char **state);
034c6ed7 168char *split_quoted(const char *c, size_t *l, char **state);
a41e8209 169
034c6ed7 170#define FOREACH_WORD(word, length, s, state) \
f62c0e4f 171 for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
65d2ebdc
LP
172
173#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
f62c0e4f 174 for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
a41e8209 175
034c6ed7 176#define FOREACH_WORD_QUOTED(word, length, s, state) \
f62c0e4f 177 for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
034c6ed7 178
65d2ebdc 179char **split_path_and_make_absolute(const char *p);
82919e3d 180
034c6ed7
LP
181pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
182
183int write_one_line_file(const char *fn, const char *line);
184int read_one_line_file(const char *fn, char **line);
185
44d8db9e 186char *strappend(const char *s, const char *suffix);
fab56fc5
LP
187char *strnappend(const char *s, const char *suffix, size_t length);
188
189char *replace_env(const char *format, char **env);
190char **replace_env_argv(char **argv, char **env);
44d8db9e 191
87f0e418 192int readlink_malloc(const char *p, char **r);
2c7108c4 193int readlink_and_make_absolute(const char *p, char **r);
87f0e418
LP
194
195char *file_name_from_path(const char *p);
0301abf4
LP
196bool is_path(const char *p);
197
198bool path_is_absolute(const char *p);
199char *path_make_absolute(const char *p, const char *prefix);
65d2ebdc 200char *path_make_absolute_cwd(const char *p);
c3f6d675 201
65d2ebdc 202char **strv_path_make_absolute_cwd(char **l);
c3f6d675 203char **strv_path_canonicalize(char **l);
87f0e418 204
2a987ee8
LP
205int reset_all_signal_handlers(void);
206
4a72ff34 207char *strstrip(char *s);
ee9b5e01 208char *delete_chars(char *s, const char *bad);
7072ced8 209char *truncate_nl(char *s);
ee9b5e01 210
4a72ff34 211char *file_in_same_dir(const char *path, const char *filename);
8c6db833 212int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
a9f5d454 213int mkdir_parents(const char *path, mode_t mode);
bbd67135 214int mkdir_p(const char *path, mode_t mode);
4a72ff34 215
35d2e7ec
LP
216int parent_of_path(const char *path, char **parent);
217
c32dd69b
LP
218int rmdir_parents(const char *path, const char *stop);
219
7072ced8 220int get_process_name(pid_t pid, char **name);
c59760ee 221int get_process_cmdline(pid_t pid, size_t max_length, char **line);
7072ced8 222
fb624d04 223char hexchar(int x);
4fe88d28
LP
224int unhexchar(char c);
225char octchar(int x);
226int unoctchar(char c);
5af98f82
LP
227char decchar(int x);
228int undecchar(char c);
4fe88d28
LP
229
230char *cescape(const char *s);
231char *cunescape(const char *s);
6febfd0d
LP
232char *cunescape_length(const char *s, size_t length);
233
234char *xescape(const char *s, const char *bad);
235
236char *bus_path_escape(const char *s);
237char *bus_path_unescape(const char *s);
4fe88d28
LP
238
239char *path_kill_slashes(char *path);
240
241bool path_startswith(const char *path, const char *prefix);
15ae422b 242bool path_equal(const char *a, const char *b);
4fe88d28
LP
243
244char *ascii_strlower(char *path);
245
c85dc17b
LP
246bool ignore_file(const char *filename);
247
db12775d
LP
248bool chars_intersect(const char *a, const char *b);
249
8b6c7120 250char *format_timestamp(char *buf, size_t l, usec_t t);
871d7de4 251char *format_timespan(char *buf, size_t l, usec_t t);
8b6c7120 252
843d2643
LP
253int make_stdio(int fd);
254
cb8a8f78 255bool is_clean_exit(int code, int status);
d06dacd0 256bool is_clean_exit_lsb(int code, int status);
cb8a8f78 257
d3782d60
LP
258unsigned long long random_ull(void);
259
1dccbe19
LP
260#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
261 const char *name##_to_string(type i) { \
262 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
263 return NULL; \
264 return name##_table[i]; \
265 } \
266 type name##_from_string(const char *s) { \
267 type i; \
a7610064 268 unsigned u = 0; \
1dccbe19
LP
269 assert(s); \
270 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
4fd5948e
LP
271 if (name##_table[i] && \
272 streq(name##_table[i], s)) \
1dccbe19 273 return i; \
d3725859
LP
274 if (safe_atou(s, &u) >= 0 && \
275 u < ELEMENTSOF(name##_table)) \
276 return (type) u; \
1dccbe19
LP
277 return (type) -1; \
278 } \
279 struct __useless_struct_to_allow_trailing_semicolon__
280
281
3a0ecb08
LP
282int fd_nonblock(int fd, bool nonblock);
283int fd_cloexec(int fd, bool cloexec);
284
a0d40ac5
LP
285int close_all_fds(const int except[], unsigned n_except);
286
42856c10
LP
287bool fstype_is_network(const char *fstype);
288
601f6a1e
LP
289int chvt(int vt);
290
80876c20
LP
291int read_one_char(FILE *f, char *ret, bool *need_nl);
292int ask(char *ret, const char *replies, const char *text, ...);
293
294int reset_terminal(int fd);
295int open_terminal(const char *name, int mode);
21de3988 296int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
80876c20
LP
297int release_terminal(void);
298
299int flush_fd(int fd);
300
9a34ec5f
LP
301int ignore_signals(int sig, ...);
302int default_signals(int sig, ...);
303int sigaction_many(const struct sigaction *sa, ...);
a337c6fc 304
8d567588
LP
305int close_pipe(int p[]);
306
eb22ac37
LP
307ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
308ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
8d567588
LP
309
310int path_is_mount_point(const char *path);
311
8407a5d0
LP
312bool is_device_path(const char *path);
313
01f78473
LP
314int dir_is_empty(const char *path);
315
5b6319dc 316void rename_process(const char name[8]);
2d368c14 317
7d793605
LP
318void sigset_add_many(sigset_t *ss, ...);
319
ef2f1067
LP
320char* gethostname_malloc(void);
321char* getlogname_malloc(void);
8c6db833 322int getttyname_malloc(char **r);
8c6db833
LP
323
324int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
325
326int rm_rf(const char *path, bool only_dirs, bool delete_root);
ef2f1067 327
82c121a4
LP
328cpu_set_t* cpu_set_malloc(unsigned *ncpus);
329
9e58ff9c 330void status_vprintf(const char *format, va_list ap);
c846ff47
LP
331void status_printf(const char *format, ...);
332void status_welcome(void);
9e58ff9c 333
fa776d8e
LP
334int columns(void);
335
b4f10a5e
LP
336int running_in_chroot(void);
337
8fe914ec
LP
338char *ellipsize(const char *s, unsigned length, unsigned percent);
339
f6144808
LP
340int touch(const char *path);
341
1dccbe19
LP
342const char *ioprio_class_to_string(int i);
343int ioprio_class_from_string(const char *s);
344
345const char *sigchld_code_to_string(int i);
346int sigchld_code_from_string(const char *s);
347
348const char *log_facility_to_string(int i);
349int log_facility_from_string(const char *s);
350
351const char *log_level_to_string(int i);
352int log_level_from_string(const char *s);
353
354const char *sched_policy_to_string(int i);
355int sched_policy_from_string(const char *s);
356
357const char *rlimit_to_string(int i);
358int rlimit_from_string(const char *s);
359
4fd5948e
LP
360const char *ip_tos_to_string(int i);
361int ip_tos_from_string(const char *s);
362
2e22afe9
LP
363const char *signal_to_string(int i);
364int signal_from_string(const char *s);
365
60918275 366#endif