]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
3074029f38b6a2e513a6704a0230bf3c52a218c7
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
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
13 (at your option) any later version.
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
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 #include <sys/inotify.h>
35 #include <sys/socket.h>
37 #include <sys/statfs.h>
38 #include <sys/types.h>
42 #include "formats-util.h"
45 #include "time-util.h"
47 /* What is interpreted as whitespace? */
48 #define WHITESPACE " \t\n\r"
49 #define NEWLINE "\n\r"
52 #define GLOB_CHARS "*?["
54 size_t page_size(void) _pure_
;
55 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
57 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
59 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
61 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
63 #define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
65 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
67 #define malloc0(n) (calloc(1, (n)))
69 static inline void *mfree(void *memory
) {
74 static inline const char* yes_no(bool b
) {
75 return b
? "yes" : "no";
78 static inline const char* true_false(bool b
) {
79 return b
? "true" : "false";
82 static inline const char* one_zero(bool b
) {
86 bool fstype_is_network(const char *fstype
);
88 noreturn
void freeze(void);
90 void execute_directories(const char* const* directories
, usec_t timeout
, char *argv
[]);
92 bool plymouth_running(void);
94 bool display_is_local(const char *display
) _pure_
;
95 int socket_from_display(const char *display
, char **path
);
97 int block_get_whole_disk(dev_t d
, dev_t
*ret
);
99 #define NULSTR_FOREACH(i, l) \
100 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
102 #define NULSTR_FOREACH_PAIR(i, j, l) \
103 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
105 int ioprio_class_to_string_alloc(int i
, char **s
);
106 int ioprio_class_from_string(const char *s
);
108 const char *sigchld_code_to_string(int i
) _const_
;
109 int sigchld_code_from_string(const char *s
) _pure_
;
111 int sched_policy_to_string_alloc(int i
, char **s
);
112 int sched_policy_from_string(const char *s
);
114 extern int saved_argc
;
115 extern char **saved_argv
;
117 bool kexec_loaded(void);
119 int prot_from_flags(int flags
) _const_
;
121 void* memdup(const void *p
, size_t l
) _alloc_(2);
123 int fork_agent(pid_t
*pid
, const int except
[], unsigned n_except
, const char *path
, ...);
125 bool in_initrd(void);
127 static inline void freep(void *p
) {
131 #define _cleanup_free_ _cleanup_(freep)
133 _malloc_
_alloc_(1, 2) static inline void *malloc_multiply(size_t a
, size_t b
) {
134 if (_unlikely_(b
!= 0 && a
> ((size_t) -1) / b
))
137 return malloc(a
* b
);
140 _alloc_(2, 3) static inline void *realloc_multiply(void *p
, size_t a
, size_t b
) {
141 if (_unlikely_(b
!= 0 && a
> ((size_t) -1) / b
))
144 return realloc(p
, a
* b
);
147 _alloc_(2, 3) static inline void *memdup_multiply(const void *p
, size_t a
, size_t b
) {
148 if (_unlikely_(b
!= 0 && a
> ((size_t) -1) / b
))
151 return memdup(p
, a
* b
);
154 void *xbsearch_r(const void *key
, const void *base
, size_t nmemb
, size_t size
,
155 int (*compar
) (const void *, const void *, void *),
158 int on_ac_power(void);
160 static inline void *mempset(void *s
, int c
, size_t n
) {
162 return (uint8_t*)s
+ n
;
165 #define memzero(x,l) (memset((x), 0, (l)))
166 #define zero(x) (memzero(&(x), sizeof(x)))
168 void* greedy_realloc(void **p
, size_t *allocated
, size_t need
, size_t size
);
169 void* greedy_realloc0(void **p
, size_t *allocated
, size_t need
, size_t size
);
170 #define GREEDY_REALLOC(array, allocated, need) \
171 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
173 #define GREEDY_REALLOC0(array, allocated, need) \
174 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
176 static inline void _reset_errno_(int *saved_errno
) {
177 errno
= *saved_errno
;
180 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
182 static inline int negative_errno(void) {
183 /* This helper should be used to shut up gcc if you know 'errno' is
184 * negative. Instead of "return -errno;", use "return negative_errno();"
185 * It will suppress bogus gcc warnings in case it assumes 'errno' might
186 * be 0 and thus the caller's error-handling might not be triggered. */
187 assert_return(errno
> 0, -EINVAL
);
191 static inline unsigned u64log2(uint64_t n
) {
192 #if __SIZEOF_LONG_LONG__ == 8
193 return (n
> 1) ? (unsigned) __builtin_clzll(n
) ^ 63U : 0;
199 static inline unsigned u32ctz(uint32_t n
) {
200 #if __SIZEOF_INT__ == 4
201 return __builtin_ctz(n
);
207 static inline unsigned log2i(int x
) {
210 return __SIZEOF_INT__
* 8 - __builtin_clz(x
) - 1;
213 static inline unsigned log2u(unsigned x
) {
216 return sizeof(unsigned) * 8 - __builtin_clz(x
) - 1;
219 static inline unsigned log2u_round_up(unsigned x
) {
225 return log2u(x
- 1) + 1;
232 _new_ = alloca(_len_); \
233 (void *) memset(_new_, 0, _len_); \
236 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
237 #define alloca_align(size, align) \
240 size_t _mask_ = (align) - 1; \
241 _ptr_ = alloca((size) + _mask_); \
242 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
245 #define alloca0_align(size, align) \
248 size_t _size_ = (size); \
249 _new_ = alloca_align(_size_, (align)); \
250 (void*)memset(_new_, 0, _size_); \
253 bool id128_is_valid(const char *s
) _pure_
;
256 * Normal qsort requires base to be nonnull. Here were require
257 * that only if nmemb > 0.
259 static inline void qsort_safe(void *base
, size_t nmemb
, size_t size
, comparison_fn_t compar
) {
264 qsort(base
, nmemb
, size
, compar
);
267 int container_get_leader(const char *machine
, pid_t
*pid
);
269 int namespace_open(pid_t pid
, int *pidns_fd
, int *mntns_fd
, int *netns_fd
, int *userns_fd
, int *root_fd
);
270 int namespace_enter(int pidns_fd
, int mntns_fd
, int netns_fd
, int userns_fd
, int root_fd
);
272 #ifndef PERSONALITY_INVALID
273 /* personality(7) documents that 0xffffffffUL is used for querying the
274 * current personality, hence let's use that here as error
276 #define PERSONALITY_INVALID 0xffffffffLU
279 unsigned long personality_from_string(const char *p
);
280 const char *personality_to_string(unsigned long);
282 uint64_t physical_memory(void);
284 union file_handle_union
{
285 struct file_handle handle
;
286 char padding
[sizeof(struct file_handle
) + MAX_HANDLE_SZ
];
288 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
290 int update_reboot_param_file(const char *param
);
292 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
294 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
295 for ((e) = &buffer.ev; \
296 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
297 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
299 union inotify_event_buffer
{
300 struct inotify_event ev
;
301 uint8_t raw
[INOTIFY_EVENT_MAX
];
306 bool fdname_is_valid(const char *s
);
308 bool oom_score_adjust_is_valid(int oa
);