chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (2/9) src/basic
[elogind.git] / src / basic / util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <alloca.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/inotify.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/statfs.h>
40 #include <sys/sysmacros.h>
41 #include <sys/types.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 #include "format-util.h"
46 #include "macro.h"
47 #include "missing.h"
48 #include "time-util.h"
49
50 size_t page_size(void) _pure_;
51 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
52
53 static inline const char* yes_no(bool b) {
54         return b ? "yes" : "no";
55 }
56
57 static inline const char* true_false(bool b) {
58         return b ? "true" : "false";
59 }
60
61 static inline const char* one_zero(bool b) {
62         return b ? "1" : "0";
63 }
64
65 static inline const char* enable_disable(bool b) {
66         return b ? "enable" : "disable";
67 }
68
69 #if 0 /// UNNEEDED by elogind
70 bool plymouth_running(void);
71 #endif // 0
72
73 bool display_is_local(const char *display) _pure_;
74 int socket_from_display(const char *display, char **path);
75
76 #if 0 /// UNNEEDED by elogind
77 int block_get_whole_disk(dev_t d, dev_t *ret);
78 #endif // 0
79
80 #define NULSTR_FOREACH(i, l)                                    \
81         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
82
83 #define NULSTR_FOREACH_PAIR(i, j, l)                             \
84         for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
85
86 extern int saved_argc;
87 extern char **saved_argv;
88
89 #if 0 /// UNNEEDED by elogind
90 bool kexec_loaded(void);
91
92 int prot_from_flags(int flags) _const_;
93 #endif // 0
94
95 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
96
97 bool in_initrd(void);
98 void in_initrd_force(bool value);
99
100 #if 0 /// UNNEEDED by elogind
101 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
102                  int (*compar) (const void *, const void *, void *),
103                  void *arg);
104 #endif // 0
105
106 /**
107  * Normal qsort requires base to be nonnull. Here were require
108  * that only if nmemb > 0.
109  */
110 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
111         if (nmemb <= 1)
112                 return;
113
114         assert(base);
115         qsort(base, nmemb, size, compar);
116 }
117
118 /**
119  * Normal memcpy requires src to be nonnull. We do nothing if n is 0.
120  */
121 static inline void memcpy_safe(void *dst, const void *src, size_t n) {
122         if (n == 0)
123                 return;
124         assert(src);
125         memcpy(dst, src, n);
126 }
127
128 #if 0 /// UNNEEDED by elogind
129 int on_ac_power(void);
130 #endif // 0
131
132 #define memzero(x,l) (memset((x), 0, (l)))
133 #define zero(x) (memzero(&(x), sizeof(x)))
134
135 static inline void *mempset(void *s, int c, size_t n) {
136         memset(s, c, n);
137         return (uint8_t*)s + n;
138 }
139
140 static inline void _reset_errno_(int *saved_errno) {
141         errno = *saved_errno;
142 }
143
144 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
145
146 static inline int negative_errno(void) {
147         /* This helper should be used to shut up gcc if you know 'errno' is
148          * negative. Instead of "return -errno;", use "return negative_errno();"
149          * It will suppress bogus gcc warnings in case it assumes 'errno' might
150          * be 0 and thus the caller's error-handling might not be triggered. */
151         assert_return(errno > 0, -EINVAL);
152         return -errno;
153 }
154
155 static inline unsigned u64log2(uint64_t n) {
156 #if __SIZEOF_LONG_LONG__ == 8
157         return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
158 #else
159 #error "Wut?"
160 #endif
161 }
162
163 static inline unsigned u32ctz(uint32_t n) {
164 #if __SIZEOF_INT__ == 4
165         return __builtin_ctz(n);
166 #else
167 #error "Wut?"
168 #endif
169 }
170
171 static inline unsigned log2i(int x) {
172         assert(x > 0);
173
174         return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
175 }
176
177 static inline unsigned log2u(unsigned x) {
178         assert(x > 0);
179
180         return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
181 }
182
183 static inline unsigned log2u_round_up(unsigned x) {
184         assert(x > 0);
185
186         if (x == 1)
187                 return 0;
188
189         return log2u(x - 1) + 1;
190 }
191
192 int container_get_leader(const char *machine, pid_t *pid);
193
194 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
195 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
196
197 uint64_t physical_memory(void);
198 uint64_t physical_memory_scale(uint64_t v, uint64_t max);
199
200 uint64_t system_tasks_max(void);
201 uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
202
203 #if 0 /// UNNEEDED by elogind
204 int update_reboot_parameter_and_warn(const char *param);
205 #endif // 0
206
207 int version(void);
208
209 #if 0 /// UNNEEDED by elogind
210 int get_block_device(const char *path, dev_t *dev);
211 int get_block_device_harder(const char *path, dev_t *dev);
212 #endif // 0