]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/util.h
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / basic / util.h
CommitLineData
c2f1db8f 1#pragma once
60918275 2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 16 Lesser General Public License for more details.
a7334b09 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
31885cd5 22#include <alloca.h>
11c3a366 23#include <errno.h>
370c860f 24#include <fcntl.h>
60918275 25#include <inttypes.h>
f6c2284a
LP
26#include <limits.h>
27#include <locale.h>
ec2002f8 28#include <stdarg.h>
60918275 29#include <stdbool.h>
f6c2284a 30#include <stddef.h>
11c3a366 31#include <stdint.h>
80876c20 32#include <stdio.h>
f6c2284a 33#include <stdlib.h>
11c3a366 34#include <string.h>
f6c2284a 35#include <sys/inotify.h>
2c35d880 36#include <sys/socket.h>
00dc5d76 37#include <sys/stat.h>
c6878637 38#include <sys/statfs.h>
f6c2284a
LP
39#include <sys/types.h>
40#include <time.h>
41#include <unistd.h>
60918275 42
f6c2284a 43#include "formats-util.h"
a838e6a1 44#include "macro.h"
dced1557 45#include "missing.h"
9a98c7a1 46#include "time-util.h"
871d7de4 47
2e857429 48size_t page_size(void) _pure_;
37f85e66 49#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
50
60918275
LP
51static inline const char* yes_no(bool b) {
52 return b ? "yes" : "no";
53}
54
5232c42e
LS
55static inline const char* true_false(bool b) {
56 return b ? "true" : "false";
57}
58
769d324c
LP
59static inline const char* one_zero(bool b) {
60 return b ? "1" : "0";
61}
62
e801700e 63void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
83cc030f 64
a88c8750
TG
65bool plymouth_running(void);
66
44a6b1b6 67bool display_is_local(const char *display) _pure_;
4d6d6518
LP
68int socket_from_display(const char *display, char **path);
69
94959f0f
LP
70int block_get_whole_disk(dev_t d, dev_t *ret);
71
e23a0ce8 72#define NULSTR_FOREACH(i, l) \
c4e2ceae
LP
73 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
74
5c0532d1
LP
75#define NULSTR_FOREACH_PAIR(i, j, l) \
76 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
77
9a0e6896
LP
78extern int saved_argc;
79extern char **saved_argv;
80
65457142
FC
81bool kexec_loaded(void);
82
44a6b1b6 83int prot_from_flags(int flags) _const_;
87d2c1ff 84
9bdc770c 85int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
6bb92a16 86
9be346c9 87bool in_initrd(void);
069cfc85 88
a9e12476
KS
89void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
90 int (*compar) (const void *, const void *, void *),
91 void *arg);
09017585 92
b5efdb8a
LP
93/**
94 * Normal qsort requires base to be nonnull. Here were require
95 * that only if nmemb > 0.
96 */
97static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
98 if (nmemb <= 1)
99 return;
f74e605f 100
b5efdb8a
LP
101 assert(base);
102 qsort(base, nmemb, size, compar);
6282c859 103}
66e35261 104
b5efdb8a
LP
105int on_ac_power(void);
106
7d50b32a
LP
107#define memzero(x,l) (memset((x), 0, (l)))
108#define zero(x) (memzero(&(x), sizeof(x)))
109
b5efdb8a
LP
110static inline void *mempset(void *s, int c, size_t n) {
111 memset(s, c, n);
112 return (uint8_t*)s + n;
113}
a1937e67 114
5c0d398d 115static inline void _reset_errno_(int *saved_errno) {
5c0aa72a
LP
116 errno = *saved_errno;
117}
118
2a371001 119#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
5c0d398d 120
44dd2c6e
DH
121static inline int negative_errno(void) {
122 /* This helper should be used to shut up gcc if you know 'errno' is
123 * negative. Instead of "return -errno;", use "return negative_errno();"
124 * It will suppress bogus gcc warnings in case it assumes 'errno' might
125 * be 0 and thus the caller's error-handling might not be triggered. */
126 assert_return(errno > 0, -EINVAL);
127 return -errno;
128}
129
144e51ec 130static inline unsigned u64log2(uint64_t n) {
ec417ccc 131#if __SIZEOF_LONG_LONG__ == 8
693eb9a2 132 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
ec417ccc
LP
133#else
134#error "Wut?"
135#endif
136}
137
138static inline unsigned u32ctz(uint32_t n) {
139#if __SIZEOF_INT__ == 4
140 return __builtin_ctz(n);
141#else
142#error "Wut?"
143#endif
144e51ec 144}
79d860fe 145
7d328b54 146static inline unsigned log2i(int x) {
8fe90522
ZJS
147 assert(x > 0);
148
149 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
150}
151
b5de6d98
MS
152static inline unsigned log2u(unsigned x) {
153 assert(x > 0);
154
155 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
156}
157
158static inline unsigned log2u_round_up(unsigned x) {
159 assert(x > 0);
160
161 if (x == 1)
162 return 0;
163
164 return log2u(x - 1) + 1;
165}
166
44a6b1b6 167bool id128_is_valid(const char *s) _pure_;
d4ac85c6 168
bc9fd78c
LP
169int container_get_leader(const char *machine, pid_t *pid);
170
671c3419
RM
171int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
172int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
bf108e55 173
1c231f56 174uint64_t physical_memory(void);
6db615c1 175
c5220a94 176int update_reboot_param_file(const char *param);
6d313367 177
3f6fd1ba 178int version(void);