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