chiark / gitweb /
50bac6edf81ca8588132ce7cc0e216c9ab601fc4
[elogind.git] / src / util.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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
34 #include "macro.h"
35
36 typedef uint64_t usec_t;
37
38 typedef struct dual_timestamp {
39         usec_t realtime;
40         usec_t monotonic;
41 } dual_timestamp;
42
43 #define MSEC_PER_SEC  1000ULL
44 #define USEC_PER_SEC  1000000ULL
45 #define USEC_PER_MSEC 1000ULL
46 #define NSEC_PER_SEC  1000000000ULL
47 #define NSEC_PER_MSEC 1000000ULL
48 #define NSEC_PER_USEC 1000ULL
49
50 #define USEC_PER_MINUTE (60ULL*USEC_PER_SEC)
51 #define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE)
52 #define USEC_PER_DAY (24ULL*USEC_PER_HOUR)
53 #define USEC_PER_WEEK (7ULL*USEC_PER_DAY)
54
55 /* What is interpreted as whitespace? */
56 #define WHITESPACE " \t\n\r"
57 #define NEWLINE "\n\r"
58
59 #define FORMAT_TIMESTAMP_MAX 64
60 #define FORMAT_TIMESPAN_MAX 64
61
62 usec_t now(clockid_t clock);
63
64 dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
65
66 usec_t timespec_load(const struct timespec *ts);
67 struct timespec *timespec_store(struct timespec *ts, usec_t u);
68
69 usec_t timeval_load(const struct timeval *tv);
70 struct timeval *timeval_store(struct timeval *tv, usec_t u);
71
72 #define streq(a,b) (strcmp((a),(b)) == 0)
73
74 bool streq_ptr(const char *a, const char *b);
75
76 #define new(t, n) ((t*) malloc(sizeof(t)*(n)))
77
78 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
79
80 #define malloc0(n) (calloc((n), 1))
81
82 static inline const char* yes_no(bool b) {
83         return b ? "yes" : "no";
84 }
85
86 static inline const char* strempty(const char *s) {
87         return s ? s : "";
88 }
89
90 static inline const char* strnull(const char *s) {
91         return s ? s : "(null)";
92 }
93
94 static inline const char *strna(const char *s) {
95         return s ? s : "n/a";
96 }
97
98 static inline bool is_path_absolute(const char *p) {
99         return *p == '/';
100 }
101
102 bool endswith(const char *s, const char *postfix);
103 bool startswith(const char *s, const char *prefix);
104 bool startswith_no_case(const char *s, const char *prefix);
105
106 bool first_word(const char *s, const char *word);
107
108 int close_nointr(int fd);
109 void close_nointr_nofail(int fd);
110 void close_many(const int fds[], unsigned n_fd);
111
112 int parse_boolean(const char *v);
113 int parse_usec(const char *t, usec_t *usec);
114 int parse_pid(const char *s, pid_t* ret_pid);
115
116 int safe_atou(const char *s, unsigned *ret_u);
117 int safe_atoi(const char *s, int *ret_i);
118
119 static inline int safe_atou32(const char *s, uint32_t *ret_u) {
120         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
121         return safe_atou(s, (unsigned*) ret_u);
122 }
123
124 static inline int safe_atoi32(const char *s, int32_t *ret_u) {
125         assert_cc(sizeof(int32_t) == sizeof(int));
126         return safe_atoi(s, (int*) ret_u);
127 }
128
129 int safe_atolu(const char *s, unsigned long *ret_u);
130 int safe_atoli(const char *s, long int *ret_i);
131
132 int safe_atollu(const char *s, unsigned long long *ret_u);
133 int safe_atolli(const char *s, long long int *ret_i);
134
135 char *split(const char *c, size_t *l, const char *separator, char **state);
136 char *split_quoted(const char *c, size_t *l, char **state);
137
138 #define FOREACH_WORD(word, length, s, state)                            \
139         for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
140
141 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
142         for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
143
144 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
145         for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
146
147 char **split_path_and_make_absolute(const char *p);
148
149 pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
150
151 int write_one_line_file(const char *fn, const char *line);
152 int read_one_line_file(const char *fn, char **line);
153
154 char *strappend(const char *s, const char *suffix);
155
156 int readlink_malloc(const char *p, char **r);
157 int readlink_and_make_absolute(const char *p, char **r);
158
159 char *file_name_from_path(const char *p);
160 bool is_path(const char *p);
161
162 bool path_is_absolute(const char *p);
163 char *path_make_absolute(const char *p, const char *prefix);
164 char *path_make_absolute_cwd(const char *p);
165
166 char **strv_path_make_absolute_cwd(char **l);
167 char **strv_path_canonicalize(char **l);
168
169 int reset_all_signal_handlers(void);
170
171 char *strstrip(char *s);
172 char *delete_chars(char *s, const char *bad);
173 char *truncate_nl(char *s);
174
175 char *file_in_same_dir(const char *path, const char *filename);
176 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
177 int mkdir_parents(const char *path, mode_t mode);
178 int mkdir_p(const char *path, mode_t mode);
179
180 int rmdir_parents(const char *path, const char *stop);
181
182 int get_process_name(pid_t pid, char **name);
183
184 char hexchar(int x);
185 int unhexchar(char c);
186 char octchar(int x);
187 int unoctchar(char c);
188 char decchar(int x);
189 int undecchar(char c);
190
191 char *cescape(const char *s);
192 char *cunescape(const char *s);
193
194 char *path_kill_slashes(char *path);
195
196 bool path_startswith(const char *path, const char *prefix);
197 bool path_equal(const char *a, const char *b);
198
199 char *ascii_strlower(char *path);
200
201 char *xescape(const char *s, const char *bad);
202
203 char *bus_path_escape(const char *s);
204 char *bus_path_unescape(const char *s);
205
206 bool ignore_file(const char *filename);
207
208 bool chars_intersect(const char *a, const char *b);
209
210 char *format_timestamp(char *buf, size_t l, usec_t t);
211 char *format_timespan(char *buf, size_t l, usec_t t);
212
213 int make_stdio(int fd);
214
215 bool is_clean_exit(int code, int status);
216
217 unsigned long long random_ull(void);
218
219 #define DEFINE_STRING_TABLE_LOOKUP(name,type)                           \
220         const char *name##_to_string(type i) {                          \
221                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
222                         return NULL;                                    \
223                 return name##_table[i];                                 \
224         }                                                               \
225         type name##_from_string(const char *s) {                        \
226                 type i;                                                 \
227                 unsigned u = 0;                                         \
228                 assert(s);                                              \
229                 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
230                         if (name##_table[i] &&                          \
231                             streq(name##_table[i], s))                  \
232                                 return i;                               \
233                 if (safe_atou(s, &u) >= 0 &&                            \
234                     u < ELEMENTSOF(name##_table))                       \
235                         return (type) u;                                \
236                 return (type) -1;                                       \
237         }                                                               \
238         struct __useless_struct_to_allow_trailing_semicolon__
239
240
241 int fd_nonblock(int fd, bool nonblock);
242 int fd_cloexec(int fd, bool cloexec);
243
244 int close_all_fds(const int except[], unsigned n_except);
245
246 bool fstype_is_network(const char *fstype);
247
248 int chvt(int vt);
249
250 int read_one_char(FILE *f, char *ret, bool *need_nl);
251 int ask(char *ret, const char *replies, const char *text, ...);
252
253 int reset_terminal(int fd);
254 int open_terminal(const char *name, int mode);
255 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
256 int release_terminal(void);
257
258 int flush_fd(int fd);
259
260 int ignore_signals(int sig, ...);
261 int default_signals(int sig, ...);
262 int sigaction_many(const struct sigaction *sa, ...);
263
264 int close_pipe(int p[]);
265
266 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
267 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
268
269 int path_is_mount_point(const char *path);
270
271 bool is_device_path(const char *path);
272
273 int dir_is_empty(const char *path);
274
275 void rename_process(const char name[8]);
276
277 void sigset_add_many(sigset_t *ss, ...);
278
279 char* gethostname_malloc(void);
280 char* getlogname_malloc(void);
281 int getttyname_malloc(char **r);
282 int getmachineid_malloc(char **r);
283
284 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
285
286 int rm_rf(const char *path, bool only_dirs, bool delete_root);
287
288 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
289
290 const char *ioprio_class_to_string(int i);
291 int ioprio_class_from_string(const char *s);
292
293 const char *sigchld_code_to_string(int i);
294 int sigchld_code_from_string(const char *s);
295
296 const char *log_facility_to_string(int i);
297 int log_facility_from_string(const char *s);
298
299 const char *log_level_to_string(int i);
300 int log_level_from_string(const char *s);
301
302 const char *sched_policy_to_string(int i);
303 int sched_policy_from_string(const char *s);
304
305 const char *rlimit_to_string(int i);
306 int rlimit_from_string(const char *s);
307
308 const char *ip_tos_to_string(int i);
309 int ip_tos_from_string(const char *s);
310
311 #endif