]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/util.h
update fixme
[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)
584be568
LP
55#define USEC_PER_MONTH (2629800ULL*USEC_PER_SEC)
56#define USEC_PER_YEAR (31557600ULL*USEC_PER_SEC)
24a6e4a4 57
44d8db9e 58/* What is interpreted as whitespace? */
4a72ff34 59#define WHITESPACE " \t\n\r"
7072ced8 60#define NEWLINE "\n\r"
97c4a07d
LP
61#define QUOTES "\"\'"
62#define COMMENTS "#;\n"
44d8db9e 63
8b6c7120 64#define FORMAT_TIMESTAMP_MAX 64
584be568 65#define FORMAT_TIMESTAMP_PRETTY_MAX 256
871d7de4 66#define FORMAT_TIMESPAN_MAX 64
8b6c7120 67
61cbdc4b 68#define ANSI_HIGHLIGHT_ON "\x1B[1;31m"
2ee68f72 69#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
61cbdc4b
LP
70#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
71
60918275
LP
72usec_t now(clockid_t clock);
73
63983207 74dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
871d7de4 75
b0c918b9
LP
76#define dual_timestamp_is_set(ts) ((ts)->realtime > 0)
77
60918275
LP
78usec_t timespec_load(const struct timespec *ts);
79struct timespec *timespec_store(struct timespec *ts, usec_t u);
80
81usec_t timeval_load(const struct timeval *tv);
82struct timeval *timeval_store(struct timeval *tv, usec_t u);
83
84#define streq(a,b) (strcmp((a),(b)) == 0)
3846aeeb 85#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
60918275 86
e05797fb
LP
87bool streq_ptr(const char *a, const char *b);
88
60918275
LP
89#define new(t, n) ((t*) malloc(sizeof(t)*(n)))
90
91#define new0(t, n) ((t*) calloc((n), sizeof(t)))
92
93#define malloc0(n) (calloc((n), 1))
94
95static inline const char* yes_no(bool b) {
96 return b ? "yes" : "no";
97}
98
99static inline const char* strempty(const char *s) {
100 return s ? s : "";
101}
102
103static inline const char* strnull(const char *s) {
104 return s ? s : "(null)";
105}
106
04fd6fe4
LP
107static inline const char *strna(const char *s) {
108 return s ? s : "n/a";
109}
110
111static inline bool is_path_absolute(const char *p) {
112 return *p == '/';
113}
114
60918275
LP
115bool endswith(const char *s, const char *postfix);
116bool startswith(const char *s, const char *prefix);
3177a7fa 117bool startswith_no_case(const char *s, const char *prefix);
60918275 118
79d6d816
LP
119bool first_word(const char *s, const char *word);
120
42f4e3c4 121int close_nointr(int fd);
85f136b5 122void close_nointr_nofail(int fd);
5b6319dc 123void close_many(const int fds[], unsigned n_fd);
60918275 124
85261803 125int parse_boolean(const char *v);
24a6e4a4 126int parse_usec(const char *t, usec_t *usec);
3ba686c1 127int parse_pid(const char *s, pid_t* ret_pid);
85261803
LP
128
129int safe_atou(const char *s, unsigned *ret_u);
130int safe_atoi(const char *s, int *ret_i);
131
8f75a603
LP
132int safe_atollu(const char *s, unsigned long long *ret_u);
133int safe_atolli(const char *s, long long int *ret_i);
134
135#if __WORDSIZE == 32
136static inline int safe_atolu(const char *s, unsigned long *ret_u) {
137 assert_cc(sizeof(unsigned long) == sizeof(unsigned));
138 return safe_atou(s, (unsigned*) ret_u);
139}
140static inline int safe_atoli(const char *s, long int *ret_u) {
141 assert_cc(sizeof(long int) == sizeof(int));
142 return safe_atoi(s, (int*) ret_u);
143}
144#else
145static inline int safe_atolu(const char *s, unsigned long *ret_u) {
146 assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
147 return safe_atollu(s, (unsigned long long*) ret_u);
148}
149static inline int safe_atoli(const char *s, long int *ret_u) {
150 assert_cc(sizeof(long int) == sizeof(long long int));
151 return safe_atolli(s, (long long int*) ret_u);
152}
153#endif
154
a838e6a1
LP
155static inline int safe_atou32(const char *s, uint32_t *ret_u) {
156 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
157 return safe_atou(s, (unsigned*) ret_u);
158}
159
8f75a603 160static inline int safe_atoi32(const char *s, int32_t *ret_i) {
a838e6a1 161 assert_cc(sizeof(int32_t) == sizeof(int));
8f75a603 162 return safe_atoi(s, (int*) ret_i);
a838e6a1
LP
163}
164
8f75a603
LP
165static inline int safe_atou64(const char *s, uint64_t *ret_u) {
166 assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
167 return safe_atollu(s, (unsigned long long*) ret_u);
168}
034c6ed7 169
8f75a603
LP
170static inline int safe_atoi64(const char *s, int64_t *ret_i) {
171 assert_cc(sizeof(int64_t) == sizeof(long long int));
172 return safe_atolli(s, (long long int*) ret_i);
173}
034c6ed7 174
65d2ebdc 175char *split(const char *c, size_t *l, const char *separator, char **state);
034c6ed7 176char *split_quoted(const char *c, size_t *l, char **state);
a41e8209 177
034c6ed7 178#define FOREACH_WORD(word, length, s, state) \
f62c0e4f 179 for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
65d2ebdc
LP
180
181#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
f62c0e4f 182 for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
a41e8209 183
034c6ed7 184#define FOREACH_WORD_QUOTED(word, length, s, state) \
f62c0e4f 185 for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
034c6ed7 186
65d2ebdc 187char **split_path_and_make_absolute(const char *p);
82919e3d 188
034c6ed7
LP
189pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
190
191int write_one_line_file(const char *fn, const char *line);
192int read_one_line_file(const char *fn, char **line);
97c4a07d
LP
193int read_full_file(const char *fn, char **contents);
194
c899f8c6 195int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
034c6ed7 196
44d8db9e 197char *strappend(const char *s, const char *suffix);
fab56fc5
LP
198char *strnappend(const char *s, const char *suffix, size_t length);
199
200char *replace_env(const char *format, char **env);
201char **replace_env_argv(char **argv, char **env);
44d8db9e 202
87f0e418 203int readlink_malloc(const char *p, char **r);
2c7108c4 204int readlink_and_make_absolute(const char *p, char **r);
87f0e418
LP
205
206char *file_name_from_path(const char *p);
0301abf4
LP
207bool is_path(const char *p);
208
209bool path_is_absolute(const char *p);
210char *path_make_absolute(const char *p, const char *prefix);
65d2ebdc 211char *path_make_absolute_cwd(const char *p);
c3f6d675 212
65d2ebdc 213char **strv_path_make_absolute_cwd(char **l);
c3f6d675 214char **strv_path_canonicalize(char **l);
87f0e418 215
2a987ee8
LP
216int reset_all_signal_handlers(void);
217
4a72ff34 218char *strstrip(char *s);
ee9b5e01 219char *delete_chars(char *s, const char *bad);
7072ced8 220char *truncate_nl(char *s);
ee9b5e01 221
4a72ff34 222char *file_in_same_dir(const char *path, const char *filename);
8c6db833 223int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
a9f5d454 224int mkdir_parents(const char *path, mode_t mode);
bbd67135 225int mkdir_p(const char *path, mode_t mode);
4a72ff34 226
35d2e7ec
LP
227int parent_of_path(const char *path, char **parent);
228
c32dd69b
LP
229int rmdir_parents(const char *path, const char *stop);
230
7072ced8 231int get_process_name(pid_t pid, char **name);
c59760ee 232int get_process_cmdline(pid_t pid, size_t max_length, char **line);
7072ced8 233
fb624d04 234char hexchar(int x);
4fe88d28
LP
235int unhexchar(char c);
236char octchar(int x);
237int unoctchar(char c);
5af98f82
LP
238char decchar(int x);
239int undecchar(char c);
4fe88d28
LP
240
241char *cescape(const char *s);
242char *cunescape(const char *s);
6febfd0d
LP
243char *cunescape_length(const char *s, size_t length);
244
245char *xescape(const char *s, const char *bad);
246
247char *bus_path_escape(const char *s);
248char *bus_path_unescape(const char *s);
4fe88d28
LP
249
250char *path_kill_slashes(char *path);
251
252bool path_startswith(const char *path, const char *prefix);
15ae422b 253bool path_equal(const char *a, const char *b);
4fe88d28
LP
254
255char *ascii_strlower(char *path);
256
c85dc17b
LP
257bool ignore_file(const char *filename);
258
db12775d
LP
259bool chars_intersect(const char *a, const char *b);
260
8b6c7120 261char *format_timestamp(char *buf, size_t l, usec_t t);
584be568 262char *format_timestamp_pretty(char *buf, size_t l, usec_t t);
871d7de4 263char *format_timespan(char *buf, size_t l, usec_t t);
8b6c7120 264
843d2643
LP
265int make_stdio(int fd);
266
cb8a8f78 267bool is_clean_exit(int code, int status);
d06dacd0 268bool is_clean_exit_lsb(int code, int status);
cb8a8f78 269
d3782d60
LP
270unsigned long long random_ull(void);
271
1dccbe19
LP
272#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
273 const char *name##_to_string(type i) { \
274 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
275 return NULL; \
276 return name##_table[i]; \
277 } \
278 type name##_from_string(const char *s) { \
279 type i; \
a7610064 280 unsigned u = 0; \
1dccbe19
LP
281 assert(s); \
282 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
4fd5948e
LP
283 if (name##_table[i] && \
284 streq(name##_table[i], s)) \
1dccbe19 285 return i; \
d3725859
LP
286 if (safe_atou(s, &u) >= 0 && \
287 u < ELEMENTSOF(name##_table)) \
288 return (type) u; \
1dccbe19
LP
289 return (type) -1; \
290 } \
291 struct __useless_struct_to_allow_trailing_semicolon__
292
293
3a0ecb08
LP
294int fd_nonblock(int fd, bool nonblock);
295int fd_cloexec(int fd, bool cloexec);
296
a0d40ac5
LP
297int close_all_fds(const int except[], unsigned n_except);
298
42856c10
LP
299bool fstype_is_network(const char *fstype);
300
601f6a1e
LP
301int chvt(int vt);
302
80876c20
LP
303int read_one_char(FILE *f, char *ret, bool *need_nl);
304int ask(char *ret, const char *replies, const char *text, ...);
305
306int reset_terminal(int fd);
307int open_terminal(const char *name, int mode);
21de3988 308int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
80876c20
LP
309int release_terminal(void);
310
311int flush_fd(int fd);
312
9a34ec5f
LP
313int ignore_signals(int sig, ...);
314int default_signals(int sig, ...);
315int sigaction_many(const struct sigaction *sa, ...);
a337c6fc 316
8d567588
LP
317int close_pipe(int p[]);
318
eb22ac37
LP
319ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
320ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
8d567588
LP
321
322int path_is_mount_point(const char *path);
323
8407a5d0
LP
324bool is_device_path(const char *path);
325
01f78473
LP
326int dir_is_empty(const char *path);
327
5b6319dc 328void rename_process(const char name[8]);
2d368c14 329
7d793605
LP
330void sigset_add_many(sigset_t *ss, ...);
331
ef2f1067
LP
332char* gethostname_malloc(void);
333char* getlogname_malloc(void);
8c6db833 334int getttyname_malloc(char **r);
8c6db833
LP
335
336int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
337
338int rm_rf(const char *path, bool only_dirs, bool delete_root);
ef2f1067 339
82c121a4
LP
340cpu_set_t* cpu_set_malloc(unsigned *ncpus);
341
9e58ff9c 342void status_vprintf(const char *format, va_list ap);
c846ff47
LP
343void status_printf(const char *format, ...);
344void status_welcome(void);
9e58ff9c 345
fa776d8e
LP
346int columns(void);
347
b4f10a5e
LP
348int running_in_chroot(void);
349
8fe914ec
LP
350char *ellipsize(const char *s, unsigned length, unsigned percent);
351
f6144808
LP
352int touch(const char *path);
353
97c4a07d 354char *unquote(const char *s, const char *quotes);
11ce3427 355
8e12a6ae 356int wait_for_terminate(pid_t pid, siginfo_t *status);
97c4a07d 357int wait_for_terminate_and_warn(const char *name, pid_t pid);
2e78aa99 358
3c14d26c
LP
359_noreturn_ void freeze(void);
360
c4e2ceae
LP
361#define NULSTR_FOREACH(i, l) \
362 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
363
1dccbe19
LP
364const char *ioprio_class_to_string(int i);
365int ioprio_class_from_string(const char *s);
366
367const char *sigchld_code_to_string(int i);
368int sigchld_code_from_string(const char *s);
369
370const char *log_facility_to_string(int i);
371int log_facility_from_string(const char *s);
372
373const char *log_level_to_string(int i);
374int log_level_from_string(const char *s);
375
376const char *sched_policy_to_string(int i);
377int sched_policy_from_string(const char *s);
378
379const char *rlimit_to_string(int i);
380int rlimit_from_string(const char *s);
381
4fd5948e
LP
382const char *ip_tos_to_string(int i);
383int ip_tos_from_string(const char *s);
384
2e22afe9
LP
385const char *signal_to_string(int i);
386int signal_from_string(const char *s);
387
60918275 388#endif