]>
Commit | Line | Data |
---|---|---|
03467c88 | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 2 | |
c2f1db8f | 3 | #pragma once |
60918275 | 4 | |
a7334b09 LP |
5 | /*** |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2010 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
11 | under the terms of the GNU Lesser General Public License as published by |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
13 | (at your option) any later version. |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 18 | Lesser General Public License for more details. |
a7334b09 | 19 | |
5430f7f2 | 20 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
22 | ***/ | |
23 | ||
31885cd5 | 24 | #include <alloca.h> |
370c860f | 25 | #include <fcntl.h> |
60918275 LP |
26 | #include <inttypes.h> |
27 | #include <time.h> | |
ec2002f8 | 28 | #include <stdarg.h> |
60918275 | 29 | #include <stdbool.h> |
5cb5a6ff | 30 | #include <stdlib.h> |
80876c20 | 31 | #include <stdio.h> |
9a34ec5f | 32 | #include <signal.h> |
82c121a4 | 33 | #include <sched.h> |
8f75a603 | 34 | #include <limits.h> |
25ea79fe | 35 | #include <sys/types.h> |
2c35d880 | 36 | #include <sys/socket.h> |
00dc5d76 | 37 | #include <sys/stat.h> |
3b63d2d3 | 38 | #include <dirent.h> |
7d5e9c0f | 39 | #include <stddef.h> |
79d860fe | 40 | #include <unistd.h> |
d6dd604b | 41 | #include <locale.h> |
5862d652 | 42 | #include <mntent.h> |
0254e944 | 43 | #include <sys/inotify.h> |
c6878637 | 44 | #include <sys/statfs.h> |
60918275 | 45 | |
a838e6a1 | 46 | #include "macro.h" |
dced1557 | 47 | #include "missing.h" |
9a98c7a1 | 48 | #include "time-util.h" |
6482f626 | 49 | #include "formats-util.h" |
871d7de4 | 50 | |
44d8db9e | 51 | /* What is interpreted as whitespace? */ |
4a72ff34 | 52 | #define WHITESPACE " \t\n\r" |
e3e0314b ZJS |
53 | #define NEWLINE "\n\r" |
54 | #define QUOTES "\"\'" | |
55 | #define COMMENTS "#;" | |
56 | #define GLOB_CHARS "*?[" | |
44d8db9e | 57 | |
0ce5a806 MM |
58 | /* What characters are special in the shell? */ |
59 | /* must be escaped outside and inside double-quotes */ | |
60 | #define SHELL_NEED_ESCAPE "\"\\`$" | |
61 | /* can be escaped or double-quoted */ | |
62 | #define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;" | |
63 | ||
a7bc2c2a | 64 | #define FORMAT_BYTES_MAX 8 |
8b6c7120 | 65 | |
c1072ea0 | 66 | #define ANSI_HIGHLIGHT_ON "\x1B[1;39m" |
03b717a3 | 67 | #define ANSI_RED_ON "\x1B[31m" |
281605bf | 68 | #define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m" |
076a24ad | 69 | #define ANSI_GREEN_ON "\x1B[32m" |
2ee68f72 | 70 | #define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m" |
5f23d5b1 | 71 | #define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m" |
c0fdf098 | 72 | #define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m" |
61cbdc4b | 73 | #define ANSI_HIGHLIGHT_OFF "\x1B[0m" |
984a2be4 | 74 | #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K" |
61cbdc4b | 75 | |
2e857429 | 76 | size_t page_size(void) _pure_; |
37f85e66 | 77 | #define PAGE_ALIGN(l) ALIGN_TO((l), page_size()) |
78 | ||
60918275 | 79 | #define streq(a,b) (strcmp((a),(b)) == 0) |
3846aeeb | 80 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) |
b43d1d01 TA |
81 | #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) |
82 | #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) | |
60918275 | 83 | |
44a6b1b6 | 84 | bool streq_ptr(const char *a, const char *b) _pure_; |
e05797fb | 85 | |
4b8772bf | 86 | #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n))) |
60918275 LP |
87 | |
88 | #define new0(t, n) ((t*) calloc((n), sizeof(t))) | |
89 | ||
0f0dbc46 LP |
90 | #define newa(t, n) ((t*) alloca(sizeof(t)*(n))) |
91 | ||
7629889c LP |
92 | #define newa0(t, n) ((t*) alloca0(sizeof(t)*(n))) |
93 | ||
4b8772bf | 94 | #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n))) |
888c7102 | 95 | |
60918275 LP |
96 | #define malloc0(n) (calloc((n), 1)) |
97 | ||
98 | static inline const char* yes_no(bool b) { | |
99 | return b ? "yes" : "no"; | |
100 | } | |
101 | ||
5232c42e LS |
102 | static inline const char* true_false(bool b) { |
103 | return b ? "true" : "false"; | |
104 | } | |
105 | ||
769d324c LP |
106 | static inline const char* one_zero(bool b) { |
107 | return b ? "1" : "0"; | |
108 | } | |
109 | ||
60918275 LP |
110 | static inline const char* strempty(const char *s) { |
111 | return s ? s : ""; | |
112 | } | |
113 | ||
114 | static inline const char* strnull(const char *s) { | |
115 | return s ? s : "(null)"; | |
116 | } | |
117 | ||
04fd6fe4 LP |
118 | static inline const char *strna(const char *s) { |
119 | return s ? s : "n/a"; | |
120 | } | |
121 | ||
9beb3f4d LP |
122 | static inline bool isempty(const char *p) { |
123 | return !p || !p[0]; | |
124 | } | |
125 | ||
11adc1ae LP |
126 | static inline char *startswith(const char *s, const char *prefix) { |
127 | size_t l; | |
128 | ||
129 | l = strlen(prefix); | |
130 | if (strncmp(s, prefix, l) == 0) | |
131 | return (char*) s + l; | |
132 | ||
df89481a KS |
133 | return NULL; |
134 | } | |
135 | ||
11adc1ae LP |
136 | static inline char *startswith_no_case(const char *s, const char *prefix) { |
137 | size_t l; | |
138 | ||
139 | l = strlen(prefix); | |
140 | if (strncasecmp(s, prefix, l) == 0) | |
141 | return (char*) s + l; | |
142 | ||
df89481a KS |
143 | return NULL; |
144 | } | |
145 | ||
44a6b1b6 | 146 | char *endswith(const char *s, const char *postfix) _pure_; |
60918275 | 147 | |
5cb36f41 | 148 | char *first_word(const char *s, const char *word) _pure_; |
79d6d816 | 149 | |
42f4e3c4 | 150 | int close_nointr(int fd); |
03e334a1 | 151 | int safe_close(int fd); |
3d94f76c | 152 | void safe_close_pair(int p[]); |
03e334a1 | 153 | |
5b6319dc | 154 | void close_many(const int fds[], unsigned n_fd); |
60918275 | 155 | |
5556b5fe LP |
156 | int parse_size(const char *t, off_t base, off_t *size); |
157 | ||
44a6b1b6 | 158 | int parse_boolean(const char *v) _pure_; |
3ba686c1 | 159 | int parse_pid(const char *s, pid_t* ret_pid); |
034a2a52 LP |
160 | int parse_uid(const char *s, uid_t* ret_uid); |
161 | #define parse_gid(s, ret_uid) parse_uid(s, ret_uid) | |
85261803 LP |
162 | |
163 | int safe_atou(const char *s, unsigned *ret_u); | |
164 | int safe_atoi(const char *s, int *ret_i); | |
165 | ||
8f75a603 LP |
166 | int safe_atollu(const char *s, unsigned long long *ret_u); |
167 | int safe_atolli(const char *s, long long int *ret_i); | |
168 | ||
f7900e25 TA |
169 | int safe_atod(const char *s, double *ret_d); |
170 | ||
b914e211 LP |
171 | int safe_atou8(const char *s, uint8_t *ret); |
172 | ||
8507eb20 | 173 | #if LONG_MAX == INT_MAX |
8f75a603 LP |
174 | static inline int safe_atolu(const char *s, unsigned long *ret_u) { |
175 | assert_cc(sizeof(unsigned long) == sizeof(unsigned)); | |
176 | return safe_atou(s, (unsigned*) ret_u); | |
177 | } | |
178 | static inline int safe_atoli(const char *s, long int *ret_u) { | |
179 | assert_cc(sizeof(long int) == sizeof(int)); | |
180 | return safe_atoi(s, (int*) ret_u); | |
181 | } | |
182 | #else | |
183 | static inline int safe_atolu(const char *s, unsigned long *ret_u) { | |
184 | assert_cc(sizeof(unsigned long) == sizeof(unsigned long long)); | |
185 | return safe_atollu(s, (unsigned long long*) ret_u); | |
186 | } | |
187 | static inline int safe_atoli(const char *s, long int *ret_u) { | |
188 | assert_cc(sizeof(long int) == sizeof(long long int)); | |
189 | return safe_atolli(s, (long long int*) ret_u); | |
190 | } | |
191 | #endif | |
192 | ||
a838e6a1 LP |
193 | static inline int safe_atou32(const char *s, uint32_t *ret_u) { |
194 | assert_cc(sizeof(uint32_t) == sizeof(unsigned)); | |
195 | return safe_atou(s, (unsigned*) ret_u); | |
196 | } | |
197 | ||
8f75a603 | 198 | static inline int safe_atoi32(const char *s, int32_t *ret_i) { |
a838e6a1 | 199 | assert_cc(sizeof(int32_t) == sizeof(int)); |
8f75a603 | 200 | return safe_atoi(s, (int*) ret_i); |
a838e6a1 LP |
201 | } |
202 | ||
8f75a603 LP |
203 | static inline int safe_atou64(const char *s, uint64_t *ret_u) { |
204 | assert_cc(sizeof(uint64_t) == sizeof(unsigned long long)); | |
205 | return safe_atollu(s, (unsigned long long*) ret_u); | |
206 | } | |
034c6ed7 | 207 | |
8f75a603 LP |
208 | static inline int safe_atoi64(const char *s, int64_t *ret_i) { |
209 | assert_cc(sizeof(int64_t) == sizeof(long long int)); | |
210 | return safe_atolli(s, (long long int*) ret_i); | |
211 | } | |
034c6ed7 | 212 | |
781fa938 LP |
213 | int safe_atou16(const char *s, uint16_t *ret); |
214 | int safe_atoi16(const char *s, int16_t *ret); | |
215 | ||
a2a5291b | 216 | const char* split(const char **state, size_t *l, const char *separator, bool quoted); |
a41e8209 | 217 | |
034c6ed7 | 218 | #define FOREACH_WORD(word, length, s, state) \ |
bf85c24d | 219 | _FOREACH_WORD(word, length, s, WHITESPACE, false, state) |
65d2ebdc LP |
220 | |
221 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
bf85c24d | 222 | _FOREACH_WORD(word, length, s, separator, false, state) |
a41e8209 | 223 | |
034c6ed7 | 224 | #define FOREACH_WORD_QUOTED(word, length, s, state) \ |
bf85c24d SP |
225 | _FOREACH_WORD(word, length, s, WHITESPACE, true, state) |
226 | ||
bf85c24d | 227 | #define _FOREACH_WORD(word, length, s, separator, quoted, state) \ |
a2a5291b | 228 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted))) |
034c6ed7 | 229 | |
44d8db9e | 230 | char *strappend(const char *s, const char *suffix); |
fab56fc5 LP |
231 | char *strnappend(const char *s, const char *suffix, size_t length); |
232 | ||
233 | char *replace_env(const char *format, char **env); | |
234 | char **replace_env_argv(char **argv, char **env); | |
44d8db9e | 235 | |
849958d1 | 236 | int readlinkat_malloc(int fd, const char *p, char **ret); |
87f0e418 | 237 | int readlink_malloc(const char *p, char **r); |
9a67bcf2 | 238 | int readlink_value(const char *p, char **ret); |
2c7108c4 | 239 | int readlink_and_make_absolute(const char *p, char **r); |
83096483 | 240 | int readlink_and_canonicalize(const char *p, char **r); |
87f0e418 | 241 | |
2a987ee8 | 242 | int reset_all_signal_handlers(void); |
1dedb74a | 243 | int reset_signal_mask(void); |
2a987ee8 | 244 | |
4a72ff34 | 245 | char *strstrip(char *s); |
ee9b5e01 | 246 | char *delete_chars(char *s, const char *bad); |
7072ced8 | 247 | char *truncate_nl(char *s); |
ee9b5e01 | 248 | |
4a72ff34 LP |
249 | char *file_in_same_dir(const char *path, const char *filename); |
250 | ||
c32dd69b LP |
251 | int rmdir_parents(const char *path, const char *stop); |
252 | ||
bcb92f48 CR |
253 | char hexchar(int x) _const_; |
254 | int unhexchar(char c) _const_; | |
255 | char octchar(int x) _const_; | |
256 | int unoctchar(char c) _const_; | |
257 | char decchar(int x) _const_; | |
258 | int undecchar(char c) _const_; | |
4fe88d28 LP |
259 | |
260 | char *cescape(const char *s); | |
0b452006 | 261 | size_t cescape_char(char c, char *buf); |
527b7a42 LP |
262 | |
263 | typedef enum UnescapeFlags { | |
264 | UNESCAPE_RELAX = 1, | |
265 | } UnescapeFlags; | |
266 | ||
267 | int cunescape(const char *s, UnescapeFlags flags, char **ret); | |
268 | int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret); | |
269 | int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret); | |
6febfd0d LP |
270 | |
271 | char *xescape(const char *s, const char *bad); | |
272 | ||
4fe88d28 LP |
273 | char *ascii_strlower(char *path); |
274 | ||
44a6b1b6 ZJS |
275 | bool dirent_is_file(const struct dirent *de) _pure_; |
276 | bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_; | |
87d2c1ff | 277 | |
a34bf9db | 278 | bool hidden_file(const char *filename) _pure_; |
c85dc17b | 279 | |
44a6b1b6 | 280 | bool chars_intersect(const char *a, const char *b) _pure_; |
db12775d | 281 | |
843d2643 | 282 | int make_stdio(int fd); |
ade509ce | 283 | int make_null_stdio(void); |
cd3bd60a | 284 | int make_console_stdio(void); |
843d2643 | 285 | |
b89446bb | 286 | int dev_urandom(void *p, size_t n); |
9bf3b535 | 287 | void random_bytes(void *p, size_t n); |
ef309a68 | 288 | void initialize_srand(void); |
9bf3b535 LP |
289 | |
290 | static inline uint64_t random_u64(void) { | |
291 | uint64_t u; | |
292 | random_bytes(&u, sizeof(u)); | |
293 | return u; | |
294 | } | |
295 | ||
296 | static inline uint32_t random_u32(void) { | |
297 | uint32_t u; | |
298 | random_bytes(&u, sizeof(u)); | |
299 | return u; | |
300 | } | |
d3782d60 | 301 | |
f8b69d1d | 302 | /* For basic lookup tables with strictly enumerated entries */ |
b9a5f858 | 303 | #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ |
4e240ab0 | 304 | scope const char *name##_to_string(type i) { \ |
1dccbe19 LP |
305 | if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ |
306 | return NULL; \ | |
307 | return name##_table[i]; \ | |
b9a5f858 LP |
308 | } |
309 | ||
9cad100e BB |
310 | ssize_t string_table_lookup(const char * const *table, size_t len, const char *key); |
311 | ||
312 | #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ | |
313 | scope inline type name##_from_string(const char *s) { \ | |
314 | return (type)string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ | |
b9a5f858 LP |
315 | } |
316 | ||
317 | #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ | |
318 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ | |
319 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ | |
1dccbe19 LP |
320 | struct __useless_struct_to_allow_trailing_semicolon__ |
321 | ||
b9a5f858 LP |
322 | #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) |
323 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) | |
324 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) | |
325 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) | |
1dccbe19 | 326 | |
f8b69d1d MS |
327 | /* For string conversions where numbers are also acceptable */ |
328 | #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ | |
329 | int name##_to_string_alloc(type i, char **str) { \ | |
330 | char *s; \ | |
331 | int r; \ | |
332 | if (i < 0 || i > max) \ | |
333 | return -ERANGE; \ | |
334 | if (i < (type) ELEMENTSOF(name##_table)) { \ | |
335 | s = strdup(name##_table[i]); \ | |
336 | if (!s) \ | |
337 | return log_oom(); \ | |
338 | } else { \ | |
8facc349 | 339 | r = asprintf(&s, "%i", i); \ |
f8b69d1d MS |
340 | if (r < 0) \ |
341 | return log_oom(); \ | |
342 | } \ | |
343 | *str = s; \ | |
344 | return 0; \ | |
345 | } \ | |
346 | type name##_from_string(const char *s) { \ | |
347 | type i; \ | |
348 | unsigned u = 0; \ | |
349 | assert(s); \ | |
350 | for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \ | |
351 | if (name##_table[i] && \ | |
352 | streq(name##_table[i], s)) \ | |
353 | return i; \ | |
8511dd18 | 354 | if (safe_atou(s, &u) >= 0 && u <= max) \ |
f8b69d1d MS |
355 | return (type) u; \ |
356 | return (type) -1; \ | |
357 | } \ | |
358 | struct __useless_struct_to_allow_trailing_semicolon__ | |
359 | ||
3a0ecb08 LP |
360 | int fd_nonblock(int fd, bool nonblock); |
361 | int fd_cloexec(int fd, bool cloexec); | |
362 | ||
a0d40ac5 LP |
363 | int close_all_fds(const int except[], unsigned n_except); |
364 | ||
42856c10 LP |
365 | bool fstype_is_network(const char *fstype); |
366 | ||
601f6a1e LP |
367 | int chvt(int vt); |
368 | ||
8f2d43a0 | 369 | int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl); |
6294aa76 LP |
370 | int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4); |
371 | int ask_string(char **ret, const char *text, ...) _printf_(2, 3); | |
80876c20 | 372 | |
512947d4 | 373 | int reset_terminal_fd(int fd, bool switch_to_text); |
6ea832a2 LP |
374 | int reset_terminal(const char *name); |
375 | ||
80876c20 | 376 | int open_terminal(const char *name, int mode); |
af6da548 | 377 | int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout); |
80876c20 LP |
378 | int release_terminal(void); |
379 | ||
380 | int flush_fd(int fd); | |
381 | ||
9a34ec5f LP |
382 | int ignore_signals(int sig, ...); |
383 | int default_signals(int sig, ...); | |
384 | int sigaction_many(const struct sigaction *sa, ...); | |
a337c6fc | 385 | |
5a3ab509 | 386 | int fopen_temporary(const char *path, FILE **_f, char **_temp_path); |
8d567588 | 387 | |
eb22ac37 | 388 | ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll); |
a6dcc7e5 | 389 | int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll); |
553acb7b | 390 | int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll); |
8d567588 | 391 | |
8407a5d0 LP |
392 | bool is_device_path(const char *path); |
393 | ||
01f78473 | 394 | int dir_is_empty(const char *path); |
844ec79b | 395 | char* dirname_malloc(const char *path); |
01f78473 | 396 | |
7d793605 | 397 | void sigset_add_many(sigset_t *ss, ...); |
856a5a7d | 398 | int sigprocmask_many(int how, ...); |
7d793605 | 399 | |
344de609 | 400 | bool hostname_is_set(void); |
7c5f152a | 401 | |
f1566e63 | 402 | char* lookup_uid(uid_t uid); |
7c5f152a | 403 | char* gethostname_malloc(void); |
ef2f1067 | 404 | char* getlogname_malloc(void); |
7c5f152a | 405 | char* getusername_malloc(void); |
fc116c6a LP |
406 | |
407 | int getttyname_malloc(int fd, char **r); | |
408 | int getttyname_harder(int fd, char **r); | |
409 | ||
4d6d6518 LP |
410 | int get_ctty_devnr(pid_t pid, dev_t *d); |
411 | int get_ctty(pid_t, dev_t *_devnr, char **r); | |
8c6db833 LP |
412 | |
413 | int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); | |
f4b47811 | 414 | int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid); |
8c6db833 | 415 | |
c6878637 LP |
416 | bool is_temporary_fs(const struct statfs *s) _pure_; |
417 | int fd_is_temporary_fs(int fd); | |
ef2f1067 | 418 | |
1325aa42 LP |
419 | int pipe_eof(int fd); |
420 | ||
82c121a4 LP |
421 | cpu_set_t* cpu_set_malloc(unsigned *ncpus); |
422 | ||
44b601bc LP |
423 | int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0); |
424 | int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5); | |
9e58ff9c | 425 | |
5ffa8c81 ZJS |
426 | #define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf)) |
427 | ||
81beb750 | 428 | int fd_columns(int fd); |
72f59706 | 429 | unsigned columns(void); |
8f2d43a0 LP |
430 | int fd_lines(int fd); |
431 | unsigned lines(void); | |
ed757c0c LP |
432 | void columns_lines_cache_reset(int _unused_ signum); |
433 | ||
434 | bool on_tty(void); | |
8f2d43a0 | 435 | |
0b5a519c DS |
436 | static inline const char *ansi_highlight(void) { |
437 | return on_tty() ? ANSI_HIGHLIGHT_ON : ""; | |
438 | } | |
439 | ||
440 | static inline const char *ansi_highlight_red(void) { | |
441 | return on_tty() ? ANSI_HIGHLIGHT_RED_ON : ""; | |
442 | } | |
443 | ||
444 | static inline const char *ansi_highlight_green(void) { | |
445 | return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : ""; | |
446 | } | |
447 | ||
dbc2c080 LP |
448 | static inline const char *ansi_highlight_yellow(void) { |
449 | return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : ""; | |
450 | } | |
451 | ||
c0fdf098 ZJS |
452 | static inline const char *ansi_highlight_blue(void) { |
453 | return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : ""; | |
454 | } | |
455 | ||
0b5a519c DS |
456 | static inline const char *ansi_highlight_off(void) { |
457 | return on_tty() ? ANSI_HIGHLIGHT_OFF : ""; | |
458 | } | |
459 | ||
9d9951a4 HH |
460 | int files_same(const char *filea, const char *fileb); |
461 | ||
b4f10a5e LP |
462 | int running_in_chroot(void); |
463 | ||
72f59706 | 464 | char *ellipsize(const char *s, size_t length, unsigned percent); |
f405e86d | 465 | /* bytes columns */ |
72f59706 | 466 | char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent); |
8fe914ec | 467 | |
c38dfac9 | 468 | int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode); |
f6144808 LP |
469 | int touch(const char *path); |
470 | ||
919ce0b7 | 471 | noreturn void freeze(void); |
3c14d26c | 472 | |
44a6b1b6 | 473 | bool null_or_empty(struct stat *st) _pure_; |
83096483 | 474 | int null_or_empty_path(const char *fn); |
ed88bcfb | 475 | int null_or_empty_fd(int fd); |
00dc5d76 | 476 | |
a247755d | 477 | DIR *xopendirat(int dirfd, const char *name, int flags); |
3b63d2d3 | 478 | |
e23a0ce8 LP |
479 | char *fstab_node_to_udev_node(const char *p); |
480 | ||
21baf21a | 481 | char *resolve_dev_console(char **active); |
f212ac12 | 482 | bool tty_is_vc(const char *tty); |
3043935f | 483 | bool tty_is_vc_resolve(const char *tty); |
44a6b1b6 | 484 | bool tty_is_console(const char *tty) _pure_; |
98a28fef | 485 | int vtnr_from_tty(const char *tty); |
e3aa71c3 LP |
486 | const char *default_term_for_tty(const char *tty); |
487 | ||
e801700e | 488 | void execute_directories(const char* const* directories, usec_t timeout, char *argv[]); |
83cc030f | 489 | |
05feefe0 LP |
490 | bool nulstr_contains(const char*nulstr, const char *needle); |
491 | ||
a88c8750 TG |
492 | bool plymouth_running(void); |
493 | ||
44a6b1b6 | 494 | bool hostname_is_valid(const char *s) _pure_; |
e724b063 | 495 | char* hostname_cleanup(char *s, bool lowercase); |
9beb3f4d | 496 | |
7f0d207d LP |
497 | bool machine_name_is_valid(const char *s) _pure_; |
498 | ||
9beb3f4d LP |
499 | char* strshorten(char *s, size_t l); |
500 | ||
6ea832a2 LP |
501 | int terminal_vhangup_fd(int fd); |
502 | int terminal_vhangup(const char *name); | |
503 | ||
504 | int vt_disallocate(const char *name); | |
505 | ||
424a19f8 | 506 | int symlink_atomic(const char *from, const char *to); |
1554afae LP |
507 | int mknod_atomic(const char *path, mode_t mode, dev_t dev); |
508 | int mkfifo_atomic(const char *path, mode_t mode); | |
34ca941c LP |
509 | |
510 | int fchmod_umask(int fd, mode_t mode); | |
511 | ||
44a6b1b6 | 512 | bool display_is_local(const char *display) _pure_; |
4d6d6518 LP |
513 | int socket_from_display(const char *display, char **path); |
514 | ||
d05c5031 | 515 | int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell); |
4b67834e | 516 | int get_group_creds(const char **groupname, gid_t *gid); |
1cccf435 | 517 | |
4468addc | 518 | int in_gid(gid_t gid); |
43673799 LP |
519 | int in_group(const char *name); |
520 | ||
59164be4 | 521 | char* uid_to_name(uid_t uid); |
4468addc | 522 | char* gid_to_name(gid_t gid); |
59164be4 | 523 | |
8092a428 | 524 | int glob_exists(const char *path); |
8d98da3f | 525 | int glob_extend(char ***strv, const char *path); |
8092a428 | 526 | |
83096483 LP |
527 | int dirent_ensure_type(DIR *d, struct dirent *de); |
528 | ||
034a2a52 | 529 | int get_files_in_directory(const char *path, char ***list); |
83096483 | 530 | |
b7def684 | 531 | char *strjoin(const char *x, ...) _sentinel_; |
911a4828 | 532 | |
b636465b LP |
533 | bool is_main_thread(void); |
534 | ||
01f83c1c JT |
535 | static inline bool _pure_ in_charset(const char *s, const char* charset) { |
536 | assert(s); | |
537 | assert(charset); | |
538 | return s[strspn(s, charset)] == '\0'; | |
539 | } | |
ab1f0633 | 540 | |
94959f0f LP |
541 | int block_get_whole_disk(dev_t d, dev_t *ret); |
542 | ||
e23a0ce8 | 543 | #define NULSTR_FOREACH(i, l) \ |
c4e2ceae LP |
544 | for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1) |
545 | ||
5c0532d1 LP |
546 | #define NULSTR_FOREACH_PAIR(i, j, l) \ |
547 | for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i)) | |
548 | ||
f8b69d1d | 549 | int ioprio_class_to_string_alloc(int i, char **s); |
1dccbe19 LP |
550 | int ioprio_class_from_string(const char *s); |
551 | ||
44a6b1b6 ZJS |
552 | const char *sigchld_code_to_string(int i) _const_; |
553 | int sigchld_code_from_string(const char *s) _pure_; | |
1dccbe19 | 554 | |
f8b69d1d | 555 | int log_facility_unshifted_to_string_alloc(int i, char **s); |
7d76f312 | 556 | int log_facility_unshifted_from_string(const char *s); |
1dccbe19 | 557 | |
f8b69d1d | 558 | int log_level_to_string_alloc(int i, char **s); |
1dccbe19 LP |
559 | int log_level_from_string(const char *s); |
560 | ||
f8b69d1d | 561 | int sched_policy_to_string_alloc(int i, char **s); |
1dccbe19 LP |
562 | int sched_policy_from_string(const char *s); |
563 | ||
44a6b1b6 ZJS |
564 | const char *rlimit_to_string(int i) _const_; |
565 | int rlimit_from_string(const char *s) _pure_; | |
1dccbe19 | 566 | |
f8b69d1d | 567 | int ip_tos_to_string_alloc(int i, char **s); |
4fd5948e LP |
568 | int ip_tos_from_string(const char *s); |
569 | ||
44a6b1b6 ZJS |
570 | const char *signal_to_string(int i) _const_; |
571 | int signal_from_string(const char *s) _pure_; | |
2e22afe9 | 572 | |
8a0867d6 LP |
573 | int signal_from_string_try_harder(const char *s); |
574 | ||
9a0e6896 LP |
575 | extern int saved_argc; |
576 | extern char **saved_argv; | |
577 | ||
65457142 FC |
578 | bool kexec_loaded(void); |
579 | ||
44a6b1b6 | 580 | int prot_from_flags(int flags) _const_; |
87d2c1ff | 581 | |
babfc091 LP |
582 | char *format_bytes(char *buf, size_t l, off_t t); |
583 | ||
8f2d43a0 | 584 | int fd_wait_for_event(int fd, int event, usec_t timeout); |
df50185b | 585 | |
750ef272 | 586 | void* memdup(const void *p, size_t l) _alloc_(2); |
55d7bfc1 | 587 | |
bb99a35a LP |
588 | int fd_inc_sndbuf(int fd, size_t n); |
589 | int fd_inc_rcvbuf(int fd, size_t n); | |
6bb92a16 | 590 | |
9bdc770c | 591 | int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...); |
6bb92a16 | 592 | |
68faf98c LP |
593 | int setrlimit_closest(int resource, const struct rlimit *rlim); |
594 | ||
a2e03378 LP |
595 | bool http_url_is_valid(const char *url) _pure_; |
596 | bool documentation_url_is_valid(const char *url) _pure_; | |
49dbfa7b | 597 | |
3d7415f4 LP |
598 | bool http_etag_is_valid(const char *etag); |
599 | ||
9be346c9 | 600 | bool in_initrd(void); |
069cfc85 LP |
601 | |
602 | void warn_melody(void); | |
603 | ||
7c5f152a | 604 | int get_home_dir(char **ret); |
2cfbd749 | 605 | int get_shell(char **_ret); |
2fbe635a | 606 | |
a740c14c MS |
607 | static inline void freep(void *p) { |
608 | free(*(void**) p); | |
609 | } | |
610 | ||
dfb33a97 | 611 | static inline void closep(int *fd) { |
03e334a1 | 612 | safe_close(*fd); |
dfb33a97 LP |
613 | } |
614 | ||
dfb33a97 LP |
615 | static inline void umaskp(mode_t *u) { |
616 | umask(*u); | |
763c7aa2 ZJS |
617 | } |
618 | ||
3d94f76c LP |
619 | static inline void close_pairp(int (*p)[2]) { |
620 | safe_close_pair(*p); | |
04d39279 LP |
621 | } |
622 | ||
14bf2c9d LP |
623 | DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose); |
624 | DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose); | |
625 | DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir); | |
626 | DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent); | |
5862d652 | 627 | |
dfb33a97 | 628 | #define _cleanup_free_ _cleanup_(freep) |
dfb33a97 | 629 | #define _cleanup_close_ _cleanup_(closep) |
dfb33a97 LP |
630 | #define _cleanup_umask_ _cleanup_(umaskp) |
631 | #define _cleanup_globfree_ _cleanup_(globfree) | |
1ca208fb ZJS |
632 | #define _cleanup_fclose_ _cleanup_(fclosep) |
633 | #define _cleanup_pclose_ _cleanup_(pclosep) | |
634 | #define _cleanup_closedir_ _cleanup_(closedirp) | |
5862d652 | 635 | #define _cleanup_endmntent_ _cleanup_(endmntentp) |
3d94f76c | 636 | #define _cleanup_close_pair_ _cleanup_(close_pairp) |
c84a9488 | 637 | |
750ef272 | 638 | _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) { |
368504f4 | 639 | if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) |
4b8772bf LP |
640 | return NULL; |
641 | ||
642 | return malloc(a * b); | |
643 | } | |
644 | ||
9489490a DH |
645 | _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) { |
646 | if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) | |
647 | return NULL; | |
648 | ||
649 | return realloc(p, a * b); | |
650 | } | |
651 | ||
750ef272 | 652 | _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) { |
368504f4 | 653 | if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) |
4b8772bf LP |
654 | return NULL; |
655 | ||
656 | return memdup(p, a * b); | |
657 | } | |
0b507b17 | 658 | |
ae6c3cc0 | 659 | bool filename_is_valid(const char *p) _pure_; |
44a6b1b6 ZJS |
660 | bool path_is_safe(const char *p) _pure_; |
661 | bool string_is_safe(const char *p) _pure_; | |
6294aa76 | 662 | bool string_has_cc(const char *p, const char *ok) _pure_; |
cfbc22ab | 663 | |
e3e0314b ZJS |
664 | /** |
665 | * Check if a string contains any glob patterns. | |
666 | */ | |
667 | _pure_ static inline bool string_is_glob(const char *p) { | |
668 | return !!strpbrk(p, GLOB_CHARS); | |
669 | } | |
670 | ||
a9e12476 KS |
671 | void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, |
672 | int (*compar) (const void *, const void *, void *), | |
673 | void *arg); | |
09017585 | 674 | |
20f56fdd DR |
675 | #define _(String) gettext (String) |
676 | void init_gettext(void); | |
09017585 | 677 | bool is_locale_utf8(void); |
c339d977 MS |
678 | |
679 | typedef enum DrawSpecialChar { | |
6b01f1d3 | 680 | DRAW_TREE_VERTICAL, |
45a5ff0d MS |
681 | DRAW_TREE_BRANCH, |
682 | DRAW_TREE_RIGHT, | |
55c0b89c | 683 | DRAW_TREE_SPACE, |
c339d977 | 684 | DRAW_TRIANGULAR_BULLET, |
3deadb91 | 685 | DRAW_BLACK_CIRCLE, |
6b01f1d3 | 686 | DRAW_ARROW, |
13f8b8cb | 687 | DRAW_DASH, |
c339d977 MS |
688 | _DRAW_SPECIAL_CHAR_MAX |
689 | } DrawSpecialChar; | |
6b01f1d3 | 690 | |
c339d977 | 691 | const char *draw_special_char(DrawSpecialChar ch); |
409bc9c3 LP |
692 | |
693 | char *strreplace(const char *text, const char *old_string, const char *new_string); | |
e8bc0ea2 LP |
694 | |
695 | char *strip_tab_ansi(char **p, size_t *l); | |
240dbaa4 LP |
696 | |
697 | int on_ac_power(void); | |
f74e605f | 698 | |
4cf7ea55 MM |
699 | int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f); |
700 | int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f); | |
fabe5c0e | 701 | |
9db11a99 LP |
702 | #define FOREACH_LINE(line, f, on_error) \ |
703 | for (;;) \ | |
f74e605f LP |
704 | if (!fgets(line, sizeof(line), f)) { \ |
705 | if (ferror(f)) { \ | |
706 | on_error; \ | |
707 | } \ | |
708 | break; \ | |
709 | } else | |
9db11a99 LP |
710 | |
711 | #define FOREACH_DIRENT(de, d, on_error) \ | |
712 | for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \ | |
713 | if (!de) { \ | |
8333c77e | 714 | if (errno > 0) { \ |
9db11a99 LP |
715 | on_error; \ |
716 | } \ | |
717 | break; \ | |
a34bf9db | 718 | } else if (hidden_file((de)->d_name)) \ |
9db11a99 LP |
719 | continue; \ |
720 | else | |
cd61c3bf LP |
721 | |
722 | #define FOREACH_DIRENT_ALL(de, d, on_error) \ | |
723 | for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \ | |
724 | if (!de) { \ | |
725 | if (errno > 0) { \ | |
726 | on_error; \ | |
727 | } \ | |
728 | break; \ | |
729 | } else | |
6282c859 MS |
730 | |
731 | static inline void *mempset(void *s, int c, size_t n) { | |
732 | memset(s, c, n); | |
dfb33a97 | 733 | return (uint8_t*)s + n; |
6282c859 | 734 | } |
66e35261 LP |
735 | |
736 | char *hexmem(const void *p, size_t l); | |
2181a7f5 LP |
737 | void *unhexmem(const char *p, size_t l); |
738 | ||
a432cb69 | 739 | char *strextend(char **x, ...) _sentinel_; |
9a17484d | 740 | char *strrep(const char *s, unsigned n); |
392d5b37 | 741 | |
ca2d3784 ZJS |
742 | void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size); |
743 | void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); | |
744 | #define GREEDY_REALLOC(array, allocated, need) \ | |
745 | greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0])) | |
746 | ||
747 | #define GREEDY_REALLOC0(array, allocated, need) \ | |
748 | greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0])) | |
a1937e67 | 749 | |
5c0d398d | 750 | static inline void _reset_errno_(int *saved_errno) { |
5c0aa72a LP |
751 | errno = *saved_errno; |
752 | } | |
753 | ||
2a371001 | 754 | #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno |
5c0d398d | 755 | |
44dd2c6e DH |
756 | static inline int negative_errno(void) { |
757 | /* This helper should be used to shut up gcc if you know 'errno' is | |
758 | * negative. Instead of "return -errno;", use "return negative_errno();" | |
759 | * It will suppress bogus gcc warnings in case it assumes 'errno' might | |
760 | * be 0 and thus the caller's error-handling might not be triggered. */ | |
761 | assert_return(errno > 0, -EINVAL); | |
762 | return -errno; | |
763 | } | |
764 | ||
d6dd604b | 765 | struct _umask_struct_ { |
5c0d398d LP |
766 | mode_t mask; |
767 | bool quit; | |
768 | }; | |
769 | ||
d6dd604b | 770 | static inline void _reset_umask_(struct _umask_struct_ *s) { |
5c0d398d LP |
771 | umask(s->mask); |
772 | }; | |
773 | ||
774 | #define RUN_WITH_UMASK(mask) \ | |
d6dd604b | 775 | for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \ |
5c0d398d LP |
776 | !_saved_umask_.quit ; \ |
777 | _saved_umask_.quit = true) | |
144e51ec CR |
778 | |
779 | static inline unsigned u64log2(uint64_t n) { | |
ec417ccc | 780 | #if __SIZEOF_LONG_LONG__ == 8 |
693eb9a2 | 781 | return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0; |
ec417ccc LP |
782 | #else |
783 | #error "Wut?" | |
784 | #endif | |
785 | } | |
786 | ||
787 | static inline unsigned u32ctz(uint32_t n) { | |
788 | #if __SIZEOF_INT__ == 4 | |
789 | return __builtin_ctz(n); | |
790 | #else | |
791 | #error "Wut?" | |
792 | #endif | |
144e51ec | 793 | } |
79d860fe | 794 | |
7d328b54 | 795 | static inline unsigned log2i(int x) { |
8fe90522 ZJS |
796 | assert(x > 0); |
797 | ||
798 | return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1; | |
799 | } | |
800 | ||
b5de6d98 MS |
801 | static inline unsigned log2u(unsigned x) { |
802 | assert(x > 0); | |
803 | ||
804 | return sizeof(unsigned) * 8 - __builtin_clz(x) - 1; | |
805 | } | |
806 | ||
807 | static inline unsigned log2u_round_up(unsigned x) { | |
808 | assert(x > 0); | |
809 | ||
810 | if (x == 1) | |
811 | return 0; | |
812 | ||
813 | return log2u(x - 1) + 1; | |
814 | } | |
815 | ||
79d860fe MP |
816 | static inline bool logind_running(void) { |
817 | return access("/run/systemd/seats/", F_OK) >= 0; | |
818 | } | |
4b73a0c0 | 819 | |
82da66fb LP |
820 | #define DECIMAL_STR_WIDTH(x) \ |
821 | ({ \ | |
822 | typeof(x) _x_ = (x); \ | |
823 | unsigned ans = 1; \ | |
824 | while (_x_ /= 10) \ | |
825 | ans++; \ | |
826 | ans; \ | |
827 | }) | |
75add28a | 828 | |
4b73a0c0 | 829 | int unlink_noerrno(const char *path); |
ed5c5dbd | 830 | |
82da66fb LP |
831 | #define alloca0(n) \ |
832 | ({ \ | |
833 | char *_new_; \ | |
834 | size_t _len_ = n; \ | |
835 | _new_ = alloca(_len_); \ | |
836 | (void *) memset(_new_, 0, _len_); \ | |
ed5c5dbd | 837 | }) |
95d78c7e | 838 | |
257224b0 | 839 | /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */ |
95d78c7e DH |
840 | #define alloca_align(size, align) \ |
841 | ({ \ | |
842 | void *_ptr_; \ | |
843 | size_t _mask_ = (align) - 1; \ | |
844 | _ptr_ = alloca((size) + _mask_); \ | |
845 | (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ | |
846 | }) | |
847 | ||
848 | #define alloca0_align(size, align) \ | |
849 | ({ \ | |
850 | void *_new_; \ | |
851 | size_t _size_ = (size); \ | |
852 | _new_ = alloca_align(_size_, (align)); \ | |
853 | (void*)memset(_new_, 0, _size_); \ | |
854 | }) | |
66060897 | 855 | |
63c372cb LP |
856 | #define strjoina(a, ...) \ |
857 | ({ \ | |
858 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
859 | char *_d_, *_p_; \ | |
860 | int _len_ = 0; \ | |
861 | unsigned _i_; \ | |
862 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
863 | _len_ += strlen(_appendees_[_i_]); \ | |
864 | _p_ = _d_ = alloca(_len_ + 1); \ | |
865 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
866 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
867 | *_p_ = 0; \ | |
868 | _d_; \ | |
f39d4a08 HH |
869 | }) |
870 | ||
44a6b1b6 | 871 | bool id128_is_valid(const char *s) _pure_; |
d4ac85c6 LP |
872 | |
873 | int split_pair(const char *s, const char *sep, char **l, char **r); | |
7ff7394d | 874 | |
74df0fca | 875 | int shall_restore_state(void); |
295edddf | 876 | |
7ff7394d ZJS |
877 | /** |
878 | * Normal qsort requires base to be nonnull. Here were require | |
879 | * that only if nmemb > 0. | |
880 | */ | |
881 | static inline void qsort_safe(void *base, size_t nmemb, size_t size, | |
882 | int (*compar)(const void *, const void *)) { | |
883 | if (nmemb) { | |
884 | assert(base); | |
885 | qsort(base, nmemb, size, compar); | |
886 | } | |
887 | } | |
74df0fca LP |
888 | |
889 | int proc_cmdline(char **ret); | |
059cb385 | 890 | int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value)); |
1a299299 | 891 | int get_proc_cmdline_key(const char *parameter, char **value); |
bc9fd78c LP |
892 | |
893 | int container_get_leader(const char *machine, pid_t *pid); | |
894 | ||
878cd7e9 LP |
895 | int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd); |
896 | int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd); | |
bf108e55 | 897 | |
eff05270 LP |
898 | int getpeercred(int fd, struct ucred *ucred); |
899 | int getpeersec(int fd, char **ret); | |
8e33886e | 900 | |
65b3903f ZJS |
901 | int writev_safe(int fd, const struct iovec *w, int j); |
902 | ||
903 | int mkostemp_safe(char *pattern, int flags); | |
8e33886e | 904 | int open_tmpfile(const char *path, int flags); |
fdb9161c LP |
905 | |
906 | int fd_warn_permissions(const char *path, int fd); | |
6afc95b7 | 907 | |
ac45f971 LP |
908 | unsigned long personality_from_string(const char *p); |
909 | const char *personality_to_string(unsigned long); | |
1c231f56 LP |
910 | |
911 | uint64_t physical_memory(void); | |
6db615c1 | 912 | |
29bfbcd6 | 913 | void hexdump(FILE *f, const void *p, size_t s); |
370c860f DR |
914 | |
915 | union file_handle_union { | |
c5220a94 MO |
916 | struct file_handle handle; |
917 | char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ]; | |
370c860f | 918 | }; |
2695c5c4 | 919 | #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ } |
c5220a94 MO |
920 | |
921 | int update_reboot_param_file(const char *param); | |
6d313367 LP |
922 | |
923 | int umount_recursive(const char *target, int flags); | |
d6797c92 LP |
924 | |
925 | int bind_remount_recursive(const char *prefix, bool ro); | |
1b992147 LP |
926 | |
927 | int fflush_and_check(FILE *f); | |
2e78fa79 | 928 | |
ae6c3cc0 LP |
929 | int tempfn_xxxxxx(const char *p, char **ret); |
930 | int tempfn_random(const char *p, char **ret); | |
c4e34a61 | 931 | int tempfn_random_child(const char *p, char **ret); |
fecc80c1 LP |
932 | |
933 | bool is_localhost(const char *hostname); | |
45035609 LP |
934 | |
935 | int take_password_lock(const char *root); | |
5261ba90 | 936 | |
7629889c | 937 | int is_symlink(const char *path); |
be57e297 | 938 | int is_dir(const char *path, bool follow); |
7629889c | 939 | |
527b7a42 | 940 | typedef enum UnquoteFlags { |
4034a06d LP |
941 | UNQUOTE_RELAX = 1, |
942 | UNQUOTE_CUNESCAPE = 2, | |
943 | } UnquoteFlags; | |
944 | ||
945 | int unquote_first_word(const char **p, char **ret, UnquoteFlags flags); | |
946 | int unquote_many_words(const char **p, UnquoteFlags flags, ...) _sentinel_; | |
2928b0a8 LP |
947 | |
948 | int free_and_strdup(char **p, const char *s); | |
605f81a8 MS |
949 | |
950 | int sethostname_idempotent(const char *s); | |
f7c1ad4f LP |
951 | |
952 | #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1) | |
953 | ||
954 | #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \ | |
0254e944 SL |
955 | for ((e) = &buffer.ev; \ |
956 | (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \ | |
f7c1ad4f | 957 | (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len)) |
72648326 | 958 | |
0254e944 SL |
959 | union inotify_event_buffer { |
960 | struct inotify_event ev; | |
961 | uint8_t raw[INOTIFY_EVENT_MAX]; | |
962 | }; | |
963 | ||
72648326 | 964 | #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW) |
ee451d76 LP |
965 | |
966 | int ptsname_malloc(int fd, char **ret); | |
5f8cc96a LP |
967 | |
968 | int openpt_in_namespace(pid_t pid, int flags); | |
4a4d89b6 | 969 | |
10f9c755 LP |
970 | ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags); |
971 | ||
4a4d89b6 LP |
972 | int fd_setcrtime(int fd, usec_t usec); |
973 | int fd_getcrtime(int fd, usec_t *usec); | |
974 | int path_getcrtime(const char *p, usec_t *usec); | |
10f9c755 | 975 | int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags); |
a354329f LP |
976 | |
977 | int same_fd(int a, int b); | |
11689d2a | 978 | |
1ed8f8c1 LP |
979 | int chattr_fd(int fd, unsigned value, unsigned mask); |
980 | int chattr_path(const char *p, unsigned value, unsigned mask); | |
de45d726 | 981 | |
01b72568 LP |
982 | int read_attr_fd(int fd, unsigned *ret); |
983 | int read_attr_path(const char *p, unsigned *ret); | |
984 | ||
30535c16 LP |
985 | typedef struct LockFile { |
986 | char *path; | |
987 | int fd; | |
988 | int operation; | |
989 | } LockFile; | |
990 | ||
991 | int make_lock_file(const char *p, int operation, LockFile *ret); | |
992 | int make_lock_file_for(const char *p, int operation, LockFile *ret); | |
993 | void release_lock_file(LockFile *f); | |
994 | ||
995 | #define _cleanup_release_lock_file_ _cleanup_(release_lock_file) | |
996 | ||
997 | #define LOCK_FILE_INIT { .fd = -1, .path = NULL } | |
998 | ||
de45d726 | 999 | #define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim }) |
ff6a7460 LP |
1000 | |
1001 | ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length); | |
3576d631 LP |
1002 | |
1003 | void sigkill_wait(pid_t *pid); | |
1004 | #define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait) | |
3d7415f4 LP |
1005 | |
1006 | int syslog_parse_priority(const char **p, int *priority, bool with_facility); | |
1c8da044 LP |
1007 | |
1008 | void cmsg_close_all(struct msghdr *mh); | |
f85ef957 AC |
1009 | |
1010 | int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); | |
019c7fba LP |
1011 | |
1012 | char *shell_maybe_quote(const char *s); | |
2ff7b0a5 LP |
1013 | |
1014 | int parse_mode(const char *s, mode_t *ret); |