]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/util.h
test-fileio: do not use variable before checking return value
[thirdparty/systemd.git] / src / basic / util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
60918275 3
31885cd5 4#include <alloca.h>
11c3a366 5#include <errno.h>
370c860f 6#include <fcntl.h>
60918275 7#include <inttypes.h>
f6c2284a
LP
8#include <limits.h>
9#include <locale.h>
ec2002f8 10#include <stdarg.h>
60918275 11#include <stdbool.h>
f6c2284a 12#include <stddef.h>
11c3a366 13#include <stdint.h>
80876c20 14#include <stdio.h>
f6c2284a 15#include <stdlib.h>
11c3a366 16#include <string.h>
f6c2284a 17#include <sys/inotify.h>
2c35d880 18#include <sys/socket.h>
00dc5d76 19#include <sys/stat.h>
c6878637 20#include <sys/statfs.h>
27d13af7 21#include <sys/sysmacros.h>
f6c2284a
LP
22#include <sys/types.h>
23#include <time.h>
24#include <unistd.h>
60918275 25
f97b34a6 26#include "format-util.h"
a838e6a1 27#include "macro.h"
9a98c7a1 28#include "time-util.h"
871d7de4 29
60918275
LP
30static inline const char* yes_no(bool b) {
31 return b ? "yes" : "no";
32}
33
5232c42e
LS
34static inline const char* true_false(bool b) {
35 return b ? "true" : "false";
36}
37
769d324c
LP
38static inline const char* one_zero(bool b) {
39 return b ? "1" : "0";
40}
41
2d37cd53
ZJS
42static inline const char* enable_disable(bool b) {
43 return b ? "enable" : "disable";
44}
45
9a0e6896
LP
46extern int saved_argc;
47extern char **saved_argv;
48
36fea155
LP
49static inline void save_argc_argv(int argc, char **argv) {
50 saved_argc = argc;
51 saved_argv = argv;
52}
53
65457142
FC
54bool kexec_loaded(void);
55
44a6b1b6 56int prot_from_flags(int flags) _const_;
87d2c1ff 57
9be346c9 58bool in_initrd(void);
dcd61450 59void in_initrd_force(bool value);
069cfc85 60
b5efdb8a
LP
61int on_ac_power(void);
62
144e51ec 63static inline unsigned u64log2(uint64_t n) {
ec417ccc 64#if __SIZEOF_LONG_LONG__ == 8
693eb9a2 65 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
ec417ccc
LP
66#else
67#error "Wut?"
68#endif
69}
70
71static inline unsigned u32ctz(uint32_t n) {
72#if __SIZEOF_INT__ == 4
1a359852 73 return n != 0 ? __builtin_ctz(n) : 32;
ec417ccc
LP
74#else
75#error "Wut?"
76#endif
144e51ec 77}
79d860fe 78
7d328b54 79static inline unsigned log2i(int x) {
8fe90522
ZJS
80 assert(x > 0);
81
82 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
83}
84
b5de6d98
MS
85static inline unsigned log2u(unsigned x) {
86 assert(x > 0);
87
88 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
89}
90
91static inline unsigned log2u_round_up(unsigned x) {
92 assert(x > 0);
93
94 if (x == 1)
95 return 0;
96
97 return log2u(x - 1) + 1;
98}
99
bc9fd78c
LP
100int container_get_leader(const char *machine, pid_t *pid);
101
3f6fd1ba 102int version(void);
68c58c67
LP
103
104int str_verscmp(const char *s1, const char *s2);
9ce17593 105
e557b1a6 106void disable_coredumps(void);