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