]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/util.h
resolved: don't check conflicts for DNS-SD enumeration RRs
[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
8
9 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 17 Lesser General Public License for more details.
a7334b09 18
5430f7f2 19 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
31885cd5 23#include <alloca.h>
11c3a366 24#include <errno.h>
370c860f 25#include <fcntl.h>
60918275 26#include <inttypes.h>
f6c2284a
LP
27#include <limits.h>
28#include <locale.h>
ec2002f8 29#include <stdarg.h>
60918275 30#include <stdbool.h>
f6c2284a 31#include <stddef.h>
11c3a366 32#include <stdint.h>
80876c20 33#include <stdio.h>
f6c2284a 34#include <stdlib.h>
11c3a366 35#include <string.h>
f6c2284a 36#include <sys/inotify.h>
2c35d880 37#include <sys/socket.h>
00dc5d76 38#include <sys/stat.h>
c6878637 39#include <sys/statfs.h>
27d13af7 40#include <sys/sysmacros.h>
f6c2284a
LP
41#include <sys/types.h>
42#include <time.h>
43#include <unistd.h>
60918275 44
f97b34a6 45#include "format-util.h"
a838e6a1 46#include "macro.h"
dced1557 47#include "missing.h"
9a98c7a1 48#include "time-util.h"
871d7de4 49
2e857429 50size_t page_size(void) _pure_;
37f85e66 51#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
52
60918275
LP
53static inline const char* yes_no(bool b) {
54 return b ? "yes" : "no";
55}
56
5232c42e
LS
57static inline const char* true_false(bool b) {
58 return b ? "true" : "false";
59}
60
769d324c
LP
61static inline const char* one_zero(bool b) {
62 return b ? "1" : "0";
63}
64
2d37cd53
ZJS
65static inline const char* enable_disable(bool b) {
66 return b ? "enable" : "disable";
67}
68
a88c8750
TG
69bool plymouth_running(void);
70
44a6b1b6 71bool display_is_local(const char *display) _pure_;
4d6d6518
LP
72int socket_from_display(const char *display, char **path);
73
94959f0f
LP
74int block_get_whole_disk(dev_t d, dev_t *ret);
75
e23a0ce8 76#define NULSTR_FOREACH(i, l) \
c4e2ceae
LP
77 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
78
5c0532d1
LP
79#define NULSTR_FOREACH_PAIR(i, j, l) \
80 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
81
9a0e6896
LP
82extern int saved_argc;
83extern char **saved_argv;
84
65457142
FC
85bool kexec_loaded(void);
86
44a6b1b6 87int prot_from_flags(int flags) _const_;
87d2c1ff 88
9bdc770c 89int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
6bb92a16 90
9be346c9 91bool in_initrd(void);
dcd61450 92void in_initrd_force(bool value);
069cfc85 93
a9e12476
KS
94void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
95 int (*compar) (const void *, const void *, void *),
96 void *arg);
09017585 97
b5efdb8a
LP
98/**
99 * Normal qsort requires base to be nonnull. Here were require
100 * that only if nmemb > 0.
101 */
102static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
103 if (nmemb <= 1)
104 return;
f74e605f 105
b5efdb8a
LP
106 assert(base);
107 qsort(base, nmemb, size, compar);
6282c859 108}
66e35261 109
75f32f04
ZJS
110/**
111 * Normal memcpy requires src to be nonnull. We do nothing if n is 0.
112 */
113static inline void memcpy_safe(void *dst, const void *src, size_t n) {
114 if (n == 0)
115 return;
116 assert(src);
117 memcpy(dst, src, n);
118}
119
b5efdb8a
LP
120int on_ac_power(void);
121
7d50b32a
LP
122#define memzero(x,l) (memset((x), 0, (l)))
123#define zero(x) (memzero(&(x), sizeof(x)))
124
b5efdb8a
LP
125static inline void *mempset(void *s, int c, size_t n) {
126 memset(s, c, n);
127 return (uint8_t*)s + n;
128}
a1937e67 129
5c0d398d 130static inline void _reset_errno_(int *saved_errno) {
5c0aa72a
LP
131 errno = *saved_errno;
132}
133
2a371001 134#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
5c0d398d 135
44dd2c6e
DH
136static inline int negative_errno(void) {
137 /* This helper should be used to shut up gcc if you know 'errno' is
138 * negative. Instead of "return -errno;", use "return negative_errno();"
139 * It will suppress bogus gcc warnings in case it assumes 'errno' might
140 * be 0 and thus the caller's error-handling might not be triggered. */
141 assert_return(errno > 0, -EINVAL);
142 return -errno;
143}
144
144e51ec 145static inline unsigned u64log2(uint64_t n) {
ec417ccc 146#if __SIZEOF_LONG_LONG__ == 8
693eb9a2 147 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
ec417ccc
LP
148#else
149#error "Wut?"
150#endif
151}
152
153static inline unsigned u32ctz(uint32_t n) {
154#if __SIZEOF_INT__ == 4
155 return __builtin_ctz(n);
156#else
157#error "Wut?"
158#endif
144e51ec 159}
79d860fe 160
7d328b54 161static inline unsigned log2i(int x) {
8fe90522
ZJS
162 assert(x > 0);
163
164 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
165}
166
b5de6d98
MS
167static inline unsigned log2u(unsigned x) {
168 assert(x > 0);
169
170 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
171}
172
173static inline unsigned log2u_round_up(unsigned x) {
174 assert(x > 0);
175
176 if (x == 1)
177 return 0;
178
179 return log2u(x - 1) + 1;
180}
181
bc9fd78c
LP
182int container_get_leader(const char *machine, pid_t *pid);
183
671c3419
RM
184int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
185int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
bf108e55 186
1c231f56 187uint64_t physical_memory(void);
d8cf2ac7 188uint64_t physical_memory_scale(uint64_t v, uint64_t max);
6db615c1 189
83f8e808
LP
190uint64_t system_tasks_max(void);
191uint64_t system_tasks_max_scale(uint64_t v, uint64_t max);
192
27c06cb5 193int update_reboot_parameter_and_warn(const char *param);
6d313367 194
3f6fd1ba 195int version(void);
c43b2b9c
FB
196
197int get_block_device(const char *path, dev_t *dev);
198int get_block_device_harder(const char *path, dev_t *dev);