• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of eudev, forked from systemd.
3 
4   Copyright 2010 Lennart Poettering
5 
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10 
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #pragma once
21 
22 #include <fcntl.h>
23 #include <string.h>
24 #include <time.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sched.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <stddef.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <sys/inotify.h>
35 
36 #include "time-util.h"
37 #include "missing.h"
38 #include "config.h"
39 
40 #include "macro.h"
41 #include "missing.h"
42 #include "formats-util.h"
43 
44 /* What is interpreted as whitespace? */
45 #define WHITESPACE " \t\n\r"
46 #define NEWLINE    "\n\r"
47 #define QUOTES     "\"\'"
48 #define COMMENTS   "#;"
49 #define GLOB_CHARS "*?["
50 
51 #define FORMAT_BYTES_MAX 8
52 
53 size_t page_size(void) _pure_;
54 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
55 
56 #define streq(a,b) (strcmp((a),(b)) == 0)
57 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
58 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
59 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
60 
61 bool streq_ptr(const char *a, const char *b) _pure_;
62 
63 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
64 
65 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
66 #define malloc0(n) (calloc((n), 1))
67 
one_zero(bool b)68 static inline const char* one_zero(bool b) {
69         return b ? "1" : "0";
70 }
71 
isempty(const char * p)72 static inline bool isempty(const char *p) {
73         return !p || !p[0];
74 }
75 
startswith(const char * s,const char * prefix)76 static inline char *startswith(const char *s, const char *prefix) {
77         size_t l;
78 
79         l = strlen(prefix);
80         if (strncmp(s, prefix, l) == 0)
81                 return (char*) s + l;
82 
83         return NULL;
84 }
85 
86 char *endswith(const char *s, const char *postfix) _pure_;
87 
88 int close_nointr(int fd);
89 int safe_close(int fd);
90 
91 void close_many(const int fds[], unsigned n_fd);
92 
93 int parse_uid(const char *s, uid_t* ret_uid);
94 #define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
95 
96 int safe_atou(const char *s, unsigned *ret_u);
97 int safe_atoi(const char *s, int *ret_i);
98 
99 int safe_atollu(const char *s, unsigned long long *ret_u);
100 int safe_atolli(const char *s, long long int *ret_i);
101 
102 
103 #if LONG_MAX == INT_MAX
safe_atolu(const char * s,unsigned long * ret_u)104 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
105         assert_cc(sizeof(unsigned long) == sizeof(unsigned));
106         return safe_atou(s, (unsigned*) ret_u);
107 }
safe_atoli(const char * s,long int * ret_u)108 static inline int safe_atoli(const char *s, long int *ret_u) {
109         assert_cc(sizeof(long int) == sizeof(int));
110         return safe_atoi(s, (int*) ret_u);
111 }
112 #else
safe_atolu(const char * s,unsigned long * ret_u)113 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
114         assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
115         return safe_atollu(s, (unsigned long long*) ret_u);
116 }
safe_atoli(const char * s,long int * ret_u)117 static inline int safe_atoli(const char *s, long int *ret_u) {
118         assert_cc(sizeof(long int) == sizeof(long long int));
119         return safe_atolli(s, (long long int*) ret_u);
120 }
121 #endif
122 
safe_atou64(const char * s,uint64_t * ret_u)123 static inline int safe_atou64(const char *s, uint64_t *ret_u) {
124         assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
125         return safe_atollu(s, (unsigned long long*) ret_u);
126 }
127 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
128 
129 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
130         _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
131 
132 #define _FOREACH_WORD(word, length, s, separator, quoted, state)        \
133         for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
134 
135 char *strappend(const char *s, const char *suffix);
136 char *strnappend(const char *s, const char *suffix, size_t length);
137 
138 char *truncate_nl(char *s);
139 
140 int rmdir_parents(const char *path, const char *stop);
141 
142 char hexchar(int x) _const_;
143 int unhexchar(char c) _const_;
144 char octchar(int x) _const_;
145 int unoctchar(char c) _const_;
146 
147 char *cescape(const char *s);
148 size_t cescape_char(char c, char *buf);
149 char *xescape(const char *s, const char *bad);
150 
151 bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
152 
153 bool hidden_file(const char *filename) _pure_;
154 
155 /* For basic lookup tables with strictly enumerated entries */
156 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
157         scope const char *name##_to_string(type i) {                    \
158                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
159                         return NULL;                                    \
160                 return name##_table[i];                                 \
161         }
162 
163 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
164         scope type name##_from_string(const char *s) {                  \
165                 type i;                                                 \
166                 if (!s)                                                 \
167                         return (type) -1;                               \
168                 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
169                         if (name##_table[i] &&                          \
170                             streq(name##_table[i], s))                  \
171                                 return i;                               \
172                 return (type) -1;                                       \
173         }
174 
175 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                    \
176         _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
177         _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
178         struct __useless_struct_to_allow_trailing_semicolon__
179 
180 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
181 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
182 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
183 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
184 
185 /* For string conversions where numbers are also acceptable */
186 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max)         \
187         int name##_to_string_alloc(type i, char **str) {                \
188                 char *s;                                                \
189                 int r;                                                  \
190                 if (i < 0 || i > max)                                   \
191                         return -ERANGE;                                 \
192                 if (i < (type) ELEMENTSOF(name##_table)) {              \
193                         s = strdup(name##_table[i]);                    \
194                         if (!s)                                         \
195                                 return log_oom();                       \
196                 } else {                                                \
197                         r = asprintf(&s, "%i", i);                      \
198                         if (r < 0)                                      \
199                                 return log_oom();                       \
200                 }                                                       \
201                 *str = s;                                               \
202                 return 0;                                               \
203         }                                                               \
204         type name##_from_string(const char *s) {                        \
205                 type i;                                                 \
206                 unsigned u = 0;                                         \
207                 assert(s);                                              \
208                 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
209                         if (name##_table[i] &&                          \
210                             streq(name##_table[i], s))                  \
211                                 return i;                               \
212                 if (safe_atou(s, &u) >= 0 && u <= max)                  \
213                         return (type) u;                                \
214                 return (type) -1;                                       \
215         }                                                               \
216         struct __useless_struct_to_allow_trailing_semicolon__
217 
218 int flush_fd(int fd);
219 
220 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
221 
222 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
223 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
224 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
225 
226 char* dirname_malloc(const char *path);
227 
228 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
229 
230 #define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
231 
232 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
233 int touch(const char *path);
234 
235 bool null_or_empty(struct stat *st) _pure_;
236 int null_or_empty_path(const char *fn);
237 int null_or_empty_fd(int fd);
238 
239 bool nulstr_contains(const char*nulstr, const char *needle);
240 
241 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
242 int get_group_creds(const char **groupname, gid_t *gid);
243 
244 char *strjoin(const char *x, ...) _sentinel_;
245 
246 bool is_main_thread(void);
247 
248 #define NULSTR_FOREACH(i, l)                                    \
249         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
250 
251 #define NULSTR_FOREACH_PAIR(i, j, l)                             \
252         for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
253 
254 int ioprio_class_to_string_alloc(int i, char **s);
255 int ioprio_class_from_string(const char *s);
256 
257 const char *sigchld_code_to_string(int i) _const_;
258 int sigchld_code_from_string(const char *s) _pure_;
259 
260 int log_facility_unshifted_to_string_alloc(int i, char **s);
261 int log_facility_unshifted_from_string(const char *s);
262 
263 int log_level_to_string_alloc(int i, char **s);
264 int log_level_from_string(const char *s);
265 
266 int sched_policy_to_string_alloc(int i, char **s);
267 int sched_policy_from_string(const char *s);
268 
269 const char *rlimit_to_string(int i) _const_;
270 int rlimit_from_string(const char *s) _pure_;
271 
272 int ip_tos_to_string_alloc(int i, char **s);
273 int ip_tos_from_string(const char *s);
274 
275 const char *signal_to_string(int i) _const_;
276 
277 extern int saved_argc;
278 extern char **saved_argv;
279 
280 int fd_wait_for_event(int fd, int event, usec_t timeout);
281 int fd_inc_sndbuf(int fd, size_t n);
282 
283 bool in_initrd(void);
284 
freep(void * p)285 static inline void freep(void *p) {
286         free(*(void**) p);
287 }
288 
closep(int * fd)289 static inline void closep(int *fd) {
290         safe_close(*fd);
291 }
292 
umaskp(mode_t * u)293 static inline void umaskp(mode_t *u) {
294         umask(*u);
295 }
296 
297 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
298 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
299 #define _cleanup_free_ _cleanup_(freep)
300 #define _cleanup_close_ _cleanup_(closep)
301 #define _cleanup_umask_ _cleanup_(umaskp)
302 #define _cleanup_fclose_ _cleanup_(fclosep)
303 #define _cleanup_closedir_ _cleanup_(closedirp)
304 
malloc_multiply(size_t a,size_t b)305 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
306         if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
307                 return NULL;
308 
309         return malloc(a * b);
310 }
311 
realloc_multiply(void * p,size_t a,size_t b)312 _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
313         if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
314                 return NULL;
315 
316         return realloc(p, a * b);
317 }
318 
319 bool filename_is_valid(const char *p) _pure_;
320 /**
321  * Check if a string contains any glob patterns.
322  */
string_is_glob(const char * p)323 _pure_ static inline bool string_is_glob(const char *p) {
324         return !!strpbrk(p, GLOB_CHARS);
325 }
326 
327 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
328                  int (*compar) (const void *, const void *, void *),
329                  void *arg);
330 
331 #define FOREACH_LINE(line, f, on_error)                         \
332         for (;;)                                                \
333                 if (!fgets(line, sizeof(line), f)) {            \
334                         if (ferror(f)) {                        \
335                                 on_error;                       \
336                         }                                       \
337                         break;                                  \
338                 } else
339 
mempset(void * s,int c,size_t n)340 static inline void *mempset(void *s, int c, size_t n) {
341         memset(s, c, n);
342         return (uint8_t*)s + n;
343 }
344 
345 
346 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
347 #define GREEDY_REALLOC(array, allocated, need)                          \
348         greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
_reset_errno_(int * saved_errno)349 static inline void _reset_errno_(int *saved_errno) {
350         errno = *saved_errno;
351 }
352 
353 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
354 
log2u(unsigned x)355 static inline unsigned log2u(unsigned x) {
356         assert(x > 0);
357 
358         return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
359 }
360 
log2u_round_up(unsigned x)361 static inline unsigned log2u_round_up(unsigned x) {
362         assert(x > 0);
363 
364         if (x == 1)
365                 return 0;
366 
367         return log2u(x - 1) + 1;
368 }
369 
370 int unlink_noerrno(const char *path);
371 
372 #define strjoina(a, ...)                                                \
373         ({                                                              \
374                 const char *_appendees_[] = { a, __VA_ARGS__ };         \
375                 char *_d_, *_p_;                                        \
376                 int _len_ = 0;                                          \
377                 unsigned _i_;                                           \
378                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
379                         _len_ += strlen(_appendees_[_i_]);              \
380                 _p_ = _d_ = alloca(_len_ + 1);                          \
381                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
382                         _p_ = stpcpy(_p_, _appendees_[_i_]);            \
383                 *_p_ = 0;                                               \
384                 _d_;                                                    \
385         })
386 
qsort_safe(void * base,size_t nmemb,size_t size,int (* compar)(const void *,const void *))387 static inline void qsort_safe(void *base, size_t nmemb, size_t size,
388                               int (*compar)(const void *, const void *)) {
389        if (nmemb <= 1)
390                return;
391 
392         assert(base);
393         qsort(base, nmemb, size, compar);
394 }
395 
396 int proc_cmdline(char **ret);
397 int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
398 int getpeercred(int fd, struct ucred *ucred);
399 
400 #if HAVE_DECL_MKOSTEMP
401 int mkostemp_safe(char *pattern, int flags);
402 #else
403 int mkstemp_safe(char *pattern);
404 #endif
405 
406 union file_handle_union {
407         struct file_handle handle;
408         char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
409 };
410 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
411 
412 int tempfn_xxxxxx(const char *p, char **ret);
413 
414 int is_dir(const char *path, bool follow);
415 
416 typedef enum UnquoteFlags {
417         UNQUOTE_RELAX     = 1,
418         UNQUOTE_CUNESCAPE = 2,
419 } UnquoteFlags;
420 
421 int unquote_first_word(const char **p, char **ret, UnquoteFlags flags);
422 
423 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
424 
425 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
426         for ((e) = &buffer.ev;                                \
427              (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
428              (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
429 
430 union inotify_event_buffer {
431         struct inotify_event ev;
432         uint8_t raw[INOTIFY_EVENT_MAX];
433 };
434 
435 void cmsg_close_all(struct msghdr *mh);
436