]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/util.h
units: run sysv related scripts with TERM=linux
[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>
00dc5d76 34#include <sys/stat.h>
3b63d2d3 35#include <dirent.h>
60918275 36
a838e6a1
LP
37#include "macro.h"
38
60918275
LP
39typedef uint64_t usec_t;
40
63983207 41typedef 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
74usec_t now(clockid_t clock);
75
63983207 76dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
871d7de4 77
b0c918b9
LP
78#define dual_timestamp_is_set(ts) ((ts)->realtime > 0)
79
60918275
LP
80usec_t timespec_load(const struct timespec *ts);
81struct timespec *timespec_store(struct timespec *ts, usec_t u);
82
83usec_t timeval_load(const struct timeval *tv);
84struct 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
89bool 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
97static inline const char* yes_no(bool b) {
98 return b ? "yes" : "no";
99}
100
101static inline const char* strempty(const char *s) {
102 return s ? s : "";
103}
104
105static inline const char* strnull(const char *s) {
106 return s ? s : "(null)";
107}
108
04fd6fe4
LP
109static inline const char *strna(const char *s) {
110 return s ? s : "n/a";
111}
112
113static inline bool is_path_absolute(const char *p) {
114 return *p == '/';
115}
116
60918275
LP
117bool endswith(const char *s, const char *postfix);
118bool startswith(const char *s, const char *prefix);
3177a7fa 119bool startswith_no_case(const char *s, const char *prefix);
60918275 120
79d6d816
LP
121bool first_word(const char *s, const char *word);
122
42f4e3c4 123int close_nointr(int fd);
85f136b5 124void close_nointr_nofail(int fd);
5b6319dc 125void close_many(const int fds[], unsigned n_fd);
60918275 126
85261803 127int parse_boolean(const char *v);
24a6e4a4 128int parse_usec(const char *t, usec_t *usec);
3ba686c1 129int parse_pid(const char *s, pid_t* ret_pid);
85261803
LP
130
131int safe_atou(const char *s, unsigned *ret_u);
132int safe_atoi(const char *s, int *ret_i);
133
8f75a603
LP
134int safe_atollu(const char *s, unsigned long long *ret_u);
135int safe_atolli(const char *s, long long int *ret_i);
136
137#if __WORDSIZE == 32
138static 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}
142static 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
147static 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}
151static 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
157static 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 162static 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
167static 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
172static 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 177char *split(const char *c, size_t *l, const char *separator, char **state);
034c6ed7 178char *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 189char **split_path_and_make_absolute(const char *p);
82919e3d 190
034c6ed7
LP
191pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
192
193int write_one_line_file(const char *fn, const char *line);
194int read_one_line_file(const char *fn, char **line);
97c4a07d
LP
195int read_full_file(const char *fn, char **contents);
196
c899f8c6 197int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
034c6ed7 198
44d8db9e 199char *strappend(const char *s, const char *suffix);
fab56fc5
LP
200char *strnappend(const char *s, const char *suffix, size_t length);
201
202char *replace_env(const char *format, char **env);
203char **replace_env_argv(char **argv, char **env);
44d8db9e 204
87f0e418 205int readlink_malloc(const char *p, char **r);
2c7108c4 206int readlink_and_make_absolute(const char *p, char **r);
87f0e418
LP
207
208char *file_name_from_path(const char *p);
0301abf4
LP
209bool is_path(const char *p);
210
211bool path_is_absolute(const char *p);
212char *path_make_absolute(const char *p, const char *prefix);
65d2ebdc 213char *path_make_absolute_cwd(const char *p);
c3f6d675 214
65d2ebdc 215char **strv_path_make_absolute_cwd(char **l);
c3f6d675 216char **strv_path_canonicalize(char **l);
87f0e418 217
2a987ee8
LP
218int reset_all_signal_handlers(void);
219
4a72ff34 220char *strstrip(char *s);
ee9b5e01 221char *delete_chars(char *s, const char *bad);
7072ced8 222char *truncate_nl(char *s);
ee9b5e01 223
4a72ff34 224char *file_in_same_dir(const char *path, const char *filename);
8c6db833 225int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
a9f5d454 226int mkdir_parents(const char *path, mode_t mode);
bbd67135 227int mkdir_p(const char *path, mode_t mode);
4a72ff34 228
35d2e7ec
LP
229int parent_of_path(const char *path, char **parent);
230
c32dd69b
LP
231int rmdir_parents(const char *path, const char *stop);
232
7072ced8 233int get_process_name(pid_t pid, char **name);
c59760ee 234int get_process_cmdline(pid_t pid, size_t max_length, char **line);
7072ced8 235
fb624d04 236char hexchar(int x);
4fe88d28
LP
237int unhexchar(char c);
238char octchar(int x);
239int unoctchar(char c);
5af98f82
LP
240char decchar(int x);
241int undecchar(char c);
4fe88d28
LP
242
243char *cescape(const char *s);
244char *cunescape(const char *s);
6febfd0d
LP
245char *cunescape_length(const char *s, size_t length);
246
247char *xescape(const char *s, const char *bad);
248
249char *bus_path_escape(const char *s);
250char *bus_path_unescape(const char *s);
4fe88d28
LP
251
252char *path_kill_slashes(char *path);
253
254bool path_startswith(const char *path, const char *prefix);
15ae422b 255bool path_equal(const char *a, const char *b);
4fe88d28
LP
256
257char *ascii_strlower(char *path);
258
c85dc17b
LP
259bool ignore_file(const char *filename);
260
db12775d
LP
261bool chars_intersect(const char *a, const char *b);
262
8b6c7120 263char *format_timestamp(char *buf, size_t l, usec_t t);
584be568 264char *format_timestamp_pretty(char *buf, size_t l, usec_t t);
871d7de4 265char *format_timespan(char *buf, size_t l, usec_t t);
8b6c7120 266
843d2643
LP
267int make_stdio(int fd);
268
cb8a8f78 269bool is_clean_exit(int code, int status);
d06dacd0 270bool is_clean_exit_lsb(int code, int status);
cb8a8f78 271
d3782d60
LP
272unsigned long long random_ull(void);
273
1dccbe19
LP
274#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
275 const char *name##_to_string(type i) { \
276 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
277 return NULL; \
278 return name##_table[i]; \
279 } \
280 type name##_from_string(const char *s) { \
281 type i; \
a7610064 282 unsigned u = 0; \
1dccbe19
LP
283 assert(s); \
284 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
4fd5948e
LP
285 if (name##_table[i] && \
286 streq(name##_table[i], s)) \
1dccbe19 287 return i; \
d3725859
LP
288 if (safe_atou(s, &u) >= 0 && \
289 u < ELEMENTSOF(name##_table)) \
290 return (type) u; \
1dccbe19
LP
291 return (type) -1; \
292 } \
293 struct __useless_struct_to_allow_trailing_semicolon__
294
295
3a0ecb08
LP
296int fd_nonblock(int fd, bool nonblock);
297int fd_cloexec(int fd, bool cloexec);
298
a0d40ac5
LP
299int close_all_fds(const int except[], unsigned n_except);
300
42856c10
LP
301bool fstype_is_network(const char *fstype);
302
601f6a1e
LP
303int chvt(int vt);
304
80876c20
LP
305int read_one_char(FILE *f, char *ret, bool *need_nl);
306int ask(char *ret, const char *replies, const char *text, ...);
307
308int reset_terminal(int fd);
309int open_terminal(const char *name, int mode);
21de3988 310int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
80876c20
LP
311int release_terminal(void);
312
313int flush_fd(int fd);
314
9a34ec5f
LP
315int ignore_signals(int sig, ...);
316int default_signals(int sig, ...);
317int sigaction_many(const struct sigaction *sa, ...);
a337c6fc 318
8d567588
LP
319int close_pipe(int p[]);
320
eb22ac37
LP
321ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
322ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
8d567588
LP
323
324int path_is_mount_point(const char *path);
325
8407a5d0
LP
326bool is_device_path(const char *path);
327
01f78473
LP
328int dir_is_empty(const char *path);
329
5b6319dc 330void rename_process(const char name[8]);
2d368c14 331
7d793605
LP
332void sigset_add_many(sigset_t *ss, ...);
333
ef2f1067
LP
334char* gethostname_malloc(void);
335char* getlogname_malloc(void);
8c6db833 336int getttyname_malloc(char **r);
8c6db833
LP
337
338int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
339
340int rm_rf(const char *path, bool only_dirs, bool delete_root);
ef2f1067 341
82c121a4
LP
342cpu_set_t* cpu_set_malloc(unsigned *ncpus);
343
9e58ff9c 344void status_vprintf(const char *format, va_list ap);
c846ff47
LP
345void status_printf(const char *format, ...);
346void status_welcome(void);
9e58ff9c 347
fa776d8e
LP
348int columns(void);
349
b4f10a5e
LP
350int running_in_chroot(void);
351
8fe914ec
LP
352char *ellipsize(const char *s, unsigned length, unsigned percent);
353
f6144808
LP
354int touch(const char *path);
355
97c4a07d 356char *unquote(const char *s, const char *quotes);
11ce3427 357
8e12a6ae 358int wait_for_terminate(pid_t pid, siginfo_t *status);
97c4a07d 359int wait_for_terminate_and_warn(const char *name, pid_t pid);
2e78aa99 360
3c14d26c
LP
361_noreturn_ void freeze(void);
362
00dc5d76
LP
363bool null_or_empty(struct stat *st);
364
3b63d2d3
LP
365DIR *xopendirat(int dirfd, const char *name);
366
ec863ba6
LP
367int ask_password_tty(const char *message, usec_t until, const char *flag_file, char **_passphrase);
368
10717a1a
LP
369void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
370void dual_timestamp_deserialize(FILE *f, const char *line, dual_timestamp *t);
371
c4e2ceae
LP
372#define NULSTR_FOREACH(i, l) \
373 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
374
1dccbe19
LP
375const char *ioprio_class_to_string(int i);
376int ioprio_class_from_string(const char *s);
377
378const char *sigchld_code_to_string(int i);
379int sigchld_code_from_string(const char *s);
380
381const char *log_facility_to_string(int i);
382int log_facility_from_string(const char *s);
383
384const char *log_level_to_string(int i);
385int log_level_from_string(const char *s);
386
387const char *sched_policy_to_string(int i);
388int sched_policy_from_string(const char *s);
389
390const char *rlimit_to_string(int i);
391int rlimit_from_string(const char *s);
392
4fd5948e
LP
393const char *ip_tos_to_string(int i);
394int ip_tos_from_string(const char *s);
395
2e22afe9
LP
396const char *signal_to_string(int i);
397int signal_from_string(const char *s);
398
8a0867d6
LP
399int signal_from_string_try_harder(const char *s);
400
60918275 401#endif