]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/util.h
Turn VALGRIND variable into a meson configuration switch
[thirdparty/systemd.git] / src / basic / util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
60918275 3
a7334b09
LP
4/***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
a7334b09
LP
8***/
9
31885cd5 10#include <alloca.h>
11c3a366 11#include <errno.h>
370c860f 12#include <fcntl.h>
60918275 13#include <inttypes.h>
f6c2284a
LP
14#include <limits.h>
15#include <locale.h>
ec2002f8 16#include <stdarg.h>
60918275 17#include <stdbool.h>
f6c2284a 18#include <stddef.h>
11c3a366 19#include <stdint.h>
80876c20 20#include <stdio.h>
f6c2284a 21#include <stdlib.h>
11c3a366 22#include <string.h>
f6c2284a 23#include <sys/inotify.h>
2c35d880 24#include <sys/socket.h>
00dc5d76 25#include <sys/stat.h>
c6878637 26#include <sys/statfs.h>
27d13af7 27#include <sys/sysmacros.h>
f6c2284a
LP
28#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
60918275 31
f97b34a6 32#include "format-util.h"
a838e6a1 33#include "macro.h"
dced1557 34#include "missing.h"
9a98c7a1 35#include "time-util.h"
871d7de4 36
2e857429 37size_t page_size(void) _pure_;
37f85e66 38#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
39
60918275
LP
40static inline const char* yes_no(bool b) {
41 return b ? "yes" : "no";
42}
43
5232c42e
LS
44static inline const char* true_false(bool b) {
45 return b ? "true" : "false";
46}
47
769d324c
LP
48static inline const char* one_zero(bool b) {
49 return b ? "1" : "0";
50}
51
2d37cd53
ZJS
52static inline const char* enable_disable(bool b) {
53 return b ? "enable" : "disable";
54}
55
a88c8750
TG
56bool plymouth_running(void);
57
44a6b1b6 58bool display_is_local(const char *display) _pure_;
4d6d6518
LP
59int socket_from_display(const char *display, char **path);
60
e23a0ce8 61#define NULSTR_FOREACH(i, l) \
c4e2ceae
LP
62 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
63
5c0532d1
LP
64#define NULSTR_FOREACH_PAIR(i, j, l) \
65 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
66
9a0e6896
LP
67extern int saved_argc;
68extern char **saved_argv;
69
65457142
FC
70bool kexec_loaded(void);
71
44a6b1b6 72int prot_from_flags(int flags) _const_;
87d2c1ff 73
9be346c9 74bool in_initrd(void);
dcd61450 75void in_initrd_force(bool value);
069cfc85 76
a9e12476
KS
77void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
78 int (*compar) (const void *, const void *, void *),
79 void *arg);
09017585 80
d6c5d19b
ZJS
81/**
82 * Normal bsearch requires base to be nonnull. Here were require
83 * that only if nmemb > 0.
84 */
85static inline void* bsearch_safe(const void *key, const void *base,
86 size_t nmemb, size_t size, comparison_fn_t compar) {
87 if (nmemb <= 0)
88 return NULL;
89
90 assert(base);
91 return bsearch(key, base, nmemb, size, compar);
92}
93
b5efdb8a
LP
94/**
95 * Normal qsort requires base to be nonnull. Here were require
96 * that only if nmemb > 0.
97 */
98static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
99 if (nmemb <= 1)
100 return;
f74e605f 101
b5efdb8a
LP
102 assert(base);
103 qsort(base, nmemb, size, compar);
6282c859 104}
66e35261 105
1ce36081
LP
106/* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
107 * is the prototype for the comparison function */
108#define typesafe_qsort(p, n, func) \
109 ({ \
110 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
111 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
112 })
113
adea407d
LP
114static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void *userdata) {
115 if (nmemb <= 1)
116 return;
117
118 assert(base);
119 qsort_r(base, nmemb, size, compar, userdata);
120}
121
75f32f04
ZJS
122/**
123 * Normal memcpy requires src to be nonnull. We do nothing if n is 0.
124 */
125static inline void memcpy_safe(void *dst, const void *src, size_t n) {
126 if (n == 0)
127 return;
128 assert(src);
129 memcpy(dst, src, n);
130}
131
b5efdb8a
LP
132int on_ac_power(void);
133
7d50b32a
LP
134#define memzero(x,l) (memset((x), 0, (l)))
135#define zero(x) (memzero(&(x), sizeof(x)))
136
b5efdb8a
LP
137static inline void *mempset(void *s, int c, size_t n) {
138 memset(s, c, n);
139 return (uint8_t*)s + n;
140}
a1937e67 141
5c0d398d 142static inline void _reset_errno_(int *saved_errno) {
5c0aa72a
LP
143 errno = *saved_errno;
144}
145
2a371001 146#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
5c0d398d 147
44dd2c6e
DH
148static inline int negative_errno(void) {
149 /* This helper should be used to shut up gcc if you know 'errno' is
150 * negative. Instead of "return -errno;", use "return negative_errno();"
151 * It will suppress bogus gcc warnings in case it assumes 'errno' might
152 * be 0 and thus the caller's error-handling might not be triggered. */
153 assert_return(errno > 0, -EINVAL);
154 return -errno;
155}
156
144e51ec 157static inline unsigned u64log2(uint64_t n) {
ec417ccc 158#if __SIZEOF_LONG_LONG__ == 8
693eb9a2 159 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
ec417ccc
LP
160#else
161#error "Wut?"
162#endif
163}
164
165static inline unsigned u32ctz(uint32_t n) {
166#if __SIZEOF_INT__ == 4
167 return __builtin_ctz(n);
168#else
169#error "Wut?"
170#endif
144e51ec 171}
79d860fe 172
7d328b54 173static inline unsigned log2i(int x) {
8fe90522
ZJS
174 assert(x > 0);
175
176 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
177}
178
b5de6d98
MS
179static inline unsigned log2u(unsigned x) {
180 assert(x > 0);
181
182 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
183}
184
185static inline unsigned log2u_round_up(unsigned x) {
186 assert(x > 0);
187
188 if (x == 1)
189 return 0;
190
191 return log2u(x - 1) + 1;
192}
193
bc9fd78c
LP
194int container_get_leader(const char *machine, pid_t *pid);
195
671c3419
RM
196int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
197int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
bf108e55 198
1c231f56 199uint64_t physical_memory(void);
d8cf2ac7 200uint64_t physical_memory_scale(uint64_t v, uint64_t max);
6db615c1 201
83f8e808
LP
202uint64_t system_tasks_max(void);
203uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
204
3f6fd1ba 205int version(void);
68c58c67
LP
206
207int str_verscmp(const char *s1, const char *s2);
9ce17593 208
e557b1a6 209void disable_coredumps(void);