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