1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
23 #include <sys/inotify.h>
24 #include <sys/socket.h>
26 #include <sys/statfs.h>
27 #include <sys/sysmacros.h>
28 #include <sys/types.h>
32 #include "format-util.h"
35 #include "time-util.h"
37 size_t page_size(void) _pure_;
38 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
40 static inline const char* yes_no(bool b) {
41 return b ? "yes" : "no";
44 static inline const char* true_false(bool b) {
45 return b ? "true" : "false";
48 static inline const char* one_zero(bool b) {
52 static inline const char* enable_disable(bool b) {
53 return b ? "enable" : "disable";
56 #if 0 /// UNNEEDED by elogind
57 bool plymouth_running(void);
60 bool display_is_local(const char *display) _pure_;
61 int socket_from_display(const char *display, char **path);
63 #if 0 /// UNNEEDED by elogind
65 #define NULSTR_FOREACH(i, l) \
66 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
68 #define NULSTR_FOREACH_PAIR(i, j, l) \
69 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
71 extern int saved_argc;
72 extern char **saved_argv;
74 #if 0 /// UNNEEDED by elogind
75 bool kexec_loaded(void);
77 int prot_from_flags(int flags) _const_;
81 #if 0 /// UNNEEDED by elogind
82 void in_initrd_force(bool value);
84 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
85 int (*compar) (const void *, const void *, void *),
90 * Normal bsearch requires base to be nonnull. Here were require
91 * that only if nmemb > 0.
93 static inline void* bsearch_safe(const void *key, const void *base,
94 size_t nmemb, size_t size, comparison_fn_t compar) {
99 return bsearch(key, base, nmemb, size, compar);
103 * Normal qsort requires base to be nonnull. Here were require
104 * that only if nmemb > 0.
106 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
111 qsort(base, nmemb, size, compar);
115 * Normal memcpy requires src to be nonnull. We do nothing if n is 0.
117 static inline void memcpy_safe(void *dst, const void *src, size_t n) {
124 int on_ac_power(void);
126 #define memzero(x,l) (memset((x), 0, (l)))
127 #define zero(x) (memzero(&(x), sizeof(x)))
129 static inline void *mempset(void *s, int c, size_t n) {
131 return (uint8_t*)s + n;
134 static inline void _reset_errno_(int *saved_errno) {
135 errno = *saved_errno;
138 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
140 static inline int negative_errno(void) {
141 /* This helper should be used to shut up gcc if you know 'errno' is
142 * negative. Instead of "return -errno;", use "return negative_errno();"
143 * It will suppress bogus gcc warnings in case it assumes 'errno' might
144 * be 0 and thus the caller's error-handling might not be triggered. */
145 assert_return(errno > 0, -EINVAL);
149 static inline unsigned u64log2(uint64_t n) {
150 #if __SIZEOF_LONG_LONG__ == 8
151 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
157 static inline unsigned u32ctz(uint32_t n) {
158 #if __SIZEOF_INT__ == 4
159 return __builtin_ctz(n);
165 static inline unsigned log2i(int x) {
168 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
171 static inline unsigned log2u(unsigned x) {
174 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
177 static inline unsigned log2u_round_up(unsigned x) {
183 return log2u(x - 1) + 1;
186 int container_get_leader(const char *machine, pid_t *pid);
188 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
189 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
191 uint64_t physical_memory(void);
192 uint64_t physical_memory_scale(uint64_t v, uint64_t max);
194 uint64_t system_tasks_max(void);
195 uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
199 #if 0 /// UNNEEDED by elogind
200 int str_verscmp(const char *s1, const char *s2);
202 void disable_coredumps(void);