chiark / gitweb /
don't make up buffer sizes, use standard LINE_MAX instead
[elogind.git] / src / util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #ifndef fooutilhfoo
4 #define fooutilhfoo
5
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
25 #include <inttypes.h>
26 #include <time.h>
27 #include <sys/time.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <sched.h>
33 #include <limits.h>
34 #include <sys/stat.h>
35 #include <dirent.h>
36
37 #include "macro.h"
38
39 typedef uint64_t usec_t;
40
41 typedef struct dual_timestamp {
42         usec_t realtime;
43         usec_t monotonic;
44 } dual_timestamp;
45
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
51 #define NSEC_PER_USEC 1000ULL
52
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)
57 #define USEC_PER_MONTH (2629800ULL*USEC_PER_SEC)
58 #define USEC_PER_YEAR (31557600ULL*USEC_PER_SEC)
59
60 /* What is interpreted as whitespace? */
61 #define WHITESPACE " \t\n\r"
62 #define NEWLINE "\n\r"
63 #define QUOTES "\"\'"
64 #define COMMENTS "#;\n"
65
66 #define FORMAT_TIMESTAMP_MAX 64
67 #define FORMAT_TIMESTAMP_PRETTY_MAX 256
68 #define FORMAT_TIMESPAN_MAX 64
69
70 #define ANSI_HIGHLIGHT_ON "\x1B[1;31m"
71 #define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
72 #define ANSI_HIGHLIGHT_OFF "\x1B[0m"
73
74 usec_t now(clockid_t clock);
75
76 dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
77
78 #define dual_timestamp_is_set(ts) ((ts)->realtime > 0)
79
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 size_t page_size(void);
87 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
88
89 #define streq(a,b) (strcmp((a),(b)) == 0)
90 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
91
92 bool streq_ptr(const char *a, const char *b);
93
94 #define new(t, n) ((t*) malloc(sizeof(t)*(n)))
95
96 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
97
98 #define malloc0(n) (calloc((n), 1))
99
100 static inline const char* yes_no(bool b) {
101         return b ? "yes" : "no";
102 }
103
104 static inline const char* strempty(const char *s) {
105         return s ? s : "";
106 }
107
108 static inline const char* strnull(const char *s) {
109         return s ? s : "(null)";
110 }
111
112 static inline const char *strna(const char *s) {
113         return s ? s : "n/a";
114 }
115
116 static inline bool is_path_absolute(const char *p) {
117         return *p == '/';
118 }
119
120 bool endswith(const char *s, const char *postfix);
121 bool startswith(const char *s, const char *prefix);
122 bool startswith_no_case(const char *s, const char *prefix);
123
124 bool first_word(const char *s, const char *word);
125
126 int close_nointr(int fd);
127 void close_nointr_nofail(int fd);
128 void close_many(const int fds[], unsigned n_fd);
129
130 int parse_boolean(const char *v);
131 int parse_usec(const char *t, usec_t *usec);
132 int parse_pid(const char *s, pid_t* ret_pid);
133
134 int safe_atou(const char *s, unsigned *ret_u);
135 int safe_atoi(const char *s, int *ret_i);
136
137 int safe_atollu(const char *s, unsigned long long *ret_u);
138 int safe_atolli(const char *s, long long int *ret_i);
139
140 #if __WORDSIZE == 32
141 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
142         assert_cc(sizeof(unsigned long) == sizeof(unsigned));
143         return safe_atou(s, (unsigned*) ret_u);
144 }
145 static inline int safe_atoli(const char *s, long int *ret_u) {
146         assert_cc(sizeof(long int) == sizeof(int));
147         return safe_atoi(s, (int*) ret_u);
148 }
149 #else
150 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
151         assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
152         return safe_atollu(s, (unsigned long long*) ret_u);
153 }
154 static inline int safe_atoli(const char *s, long int *ret_u) {
155         assert_cc(sizeof(long int) == sizeof(long long int));
156         return safe_atolli(s, (long long int*) ret_u);
157 }
158 #endif
159
160 static inline int safe_atou32(const char *s, uint32_t *ret_u) {
161         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
162         return safe_atou(s, (unsigned*) ret_u);
163 }
164
165 static inline int safe_atoi32(const char *s, int32_t *ret_i) {
166         assert_cc(sizeof(int32_t) == sizeof(int));
167         return safe_atoi(s, (int*) ret_i);
168 }
169
170 static inline int safe_atou64(const char *s, uint64_t *ret_u) {
171         assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
172         return safe_atollu(s, (unsigned long long*) ret_u);
173 }
174
175 static inline int safe_atoi64(const char *s, int64_t *ret_i) {
176         assert_cc(sizeof(int64_t) == sizeof(long long int));
177         return safe_atolli(s, (long long int*) ret_i);
178 }
179
180 char *split(const char *c, size_t *l, const char *separator, char **state);
181 char *split_quoted(const char *c, size_t *l, char **state);
182
183 #define FOREACH_WORD(word, length, s, state)                            \
184         for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
185
186 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
187         for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
188
189 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
190         for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
191
192 char **split_path_and_make_absolute(const char *p);
193
194 pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
195
196 int write_one_line_file(const char *fn, const char *line);
197 int read_one_line_file(const char *fn, char **line);
198 int read_full_file(const char *fn, char **contents);
199
200 int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
201 int load_env_file(const char *fname, char ***l);
202
203 char *strappend(const char *s, const char *suffix);
204 char *strnappend(const char *s, const char *suffix, size_t length);
205
206 char *replace_env(const char *format, char **env);
207 char **replace_env_argv(char **argv, char **env);
208
209 int readlink_malloc(const char *p, char **r);
210 int readlink_and_make_absolute(const char *p, char **r);
211
212 char *file_name_from_path(const char *p);
213 bool is_path(const char *p);
214
215 bool path_is_absolute(const char *p);
216 char *path_make_absolute(const char *p, const char *prefix);
217 char *path_make_absolute_cwd(const char *p);
218
219 char **strv_path_make_absolute_cwd(char **l);
220 char **strv_path_canonicalize(char **l);
221
222 int reset_all_signal_handlers(void);
223
224 char *strstrip(char *s);
225 char *delete_chars(char *s, const char *bad);
226 char *truncate_nl(char *s);
227
228 char *file_in_same_dir(const char *path, const char *filename);
229 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
230 int mkdir_parents(const char *path, mode_t mode);
231 int mkdir_p(const char *path, mode_t mode);
232
233 int parent_of_path(const char *path, char **parent);
234
235 int rmdir_parents(const char *path, const char *stop);
236
237 int get_process_name(pid_t pid, char **name);
238 int get_process_cmdline(pid_t pid, size_t max_length, char **line);
239
240 char hexchar(int x);
241 int unhexchar(char c);
242 char octchar(int x);
243 int unoctchar(char c);
244 char decchar(int x);
245 int undecchar(char c);
246
247 char *cescape(const char *s);
248 char *cunescape(const char *s);
249 char *cunescape_length(const char *s, size_t length);
250
251 char *xescape(const char *s, const char *bad);
252
253 char *bus_path_escape(const char *s);
254 char *bus_path_unescape(const char *s);
255
256 char *path_kill_slashes(char *path);
257
258 bool path_startswith(const char *path, const char *prefix);
259 bool path_equal(const char *a, const char *b);
260
261 char *ascii_strlower(char *path);
262
263 bool ignore_file(const char *filename);
264
265 bool chars_intersect(const char *a, const char *b);
266
267 char *format_timestamp(char *buf, size_t l, usec_t t);
268 char *format_timestamp_pretty(char *buf, size_t l, usec_t t);
269 char *format_timespan(char *buf, size_t l, usec_t t);
270
271 int make_stdio(int fd);
272 int make_null_stdio(void);
273
274 unsigned long long random_ull(void);
275
276 #define DEFINE_STRING_TABLE_LOOKUP(name,type)                           \
277         const char *name##_to_string(type i) {                          \
278                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
279                         return NULL;                                    \
280                 return name##_table[i];                                 \
281         }                                                               \
282         type name##_from_string(const char *s) {                        \
283                 type i;                                                 \
284                 unsigned u = 0;                                         \
285                 assert(s);                                              \
286                 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
287                         if (name##_table[i] &&                          \
288                             streq(name##_table[i], s))                  \
289                                 return i;                               \
290                 if (safe_atou(s, &u) >= 0 &&                            \
291                     u < ELEMENTSOF(name##_table))                       \
292                         return (type) u;                                \
293                 return (type) -1;                                       \
294         }                                                               \
295         struct __useless_struct_to_allow_trailing_semicolon__
296
297
298 int fd_nonblock(int fd, bool nonblock);
299 int fd_cloexec(int fd, bool cloexec);
300
301 int close_all_fds(const int except[], unsigned n_except);
302
303 bool fstype_is_network(const char *fstype);
304
305 int chvt(int vt);
306
307 int read_one_char(FILE *f, char *ret, bool *need_nl);
308 int ask(char *ret, const char *replies, const char *text, ...);
309
310 int reset_terminal(int fd);
311 int open_terminal(const char *name, int mode);
312 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
313 int release_terminal(void);
314
315 int flush_fd(int fd);
316
317 int ignore_signals(int sig, ...);
318 int default_signals(int sig, ...);
319 int sigaction_many(const struct sigaction *sa, ...);
320
321 int close_pipe(int p[]);
322
323 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
324 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
325
326 int path_is_mount_point(const char *path);
327
328 bool is_device_path(const char *path);
329
330 int dir_is_empty(const char *path);
331
332 void rename_process(const char name[8]);
333
334 void sigset_add_many(sigset_t *ss, ...);
335
336 char* gethostname_malloc(void);
337 char* getlogname_malloc(void);
338
339 int getttyname_malloc(int fd, char **r);
340 int getttyname_harder(int fd, char **r);
341
342 int get_ctty_devnr(dev_t *d);
343 int get_ctty(char **r, dev_t *_devnr);
344
345 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
346
347 int rm_rf(const char *path, bool only_dirs, bool delete_root);
348
349 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
350
351 void status_vprintf(const char *format, va_list ap);
352 void status_printf(const char *format, ...);
353 void status_welcome(void);
354
355 int columns(void);
356
357 int running_in_chroot(void);
358
359 char *ellipsize(const char *s, unsigned length, unsigned percent);
360
361 int touch(const char *path);
362
363 char *unquote(const char *s, const char *quotes);
364 char *normalize_env_assignment(const char *s);
365
366 int wait_for_terminate(pid_t pid, siginfo_t *status);
367 int wait_for_terminate_and_warn(const char *name, pid_t pid);
368
369 _noreturn_ void freeze(void);
370
371 bool null_or_empty(struct stat *st);
372
373 DIR *xopendirat(int dirfd, const char *name, int flags);
374
375 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
376 void dual_timestamp_deserialize(const char *value, dual_timestamp *t);
377
378 char *fstab_node_to_udev_node(const char *p);
379
380 void filter_environ(const char *prefix);
381
382 bool tty_is_vc(const char *tty);
383 const char *default_term_for_tty(const char *tty);
384
385 int detect_vm(const char **id);
386 int detect_container(const char **id);
387 int detect_virtualization(const char **id);
388
389 void execute_directory(const char *directory, DIR *_d, char *argv[]);
390
391 int kill_and_sigcont(pid_t pid, int sig);
392
393 bool nulstr_contains(const char*nulstr, const char *needle);
394
395 bool plymouth_running(void);
396
397 void parse_syslog_priority(char **p, int *priority);
398
399 #define NULSTR_FOREACH(i, l)                                    \
400         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
401
402 #define NULSTR_FOREACH_PAIR(i, j, l)                             \
403         for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
404
405 const char *ioprio_class_to_string(int i);
406 int ioprio_class_from_string(const char *s);
407
408 const char *sigchld_code_to_string(int i);
409 int sigchld_code_from_string(const char *s);
410
411 const char *log_facility_unshifted_to_string(int i);
412 int log_facility_unshifted_from_string(const char *s);
413
414 const char *log_level_to_string(int i);
415 int log_level_from_string(const char *s);
416
417 const char *sched_policy_to_string(int i);
418 int sched_policy_from_string(const char *s);
419
420 const char *rlimit_to_string(int i);
421 int rlimit_from_string(const char *s);
422
423 const char *ip_tos_to_string(int i);
424 int ip_tos_from_string(const char *s);
425
426 const char *signal_to_string(int i);
427 int signal_from_string(const char *s);
428
429 int signal_from_string_try_harder(const char *s);
430
431 #endif