1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdint.h>
5 
6 #include "macro.h"
7 
8 extern int saved_argc;
9 extern char **saved_argv;
10 
save_argc_argv(int argc,char ** argv)11 static inline void save_argc_argv(int argc, char **argv) {
12 
13         /* Protect against CVE-2021-4034 style attacks */
14         assert_se(argc > 0);
15         assert_se(argv);
16         assert_se(argv[0]);
17 
18         saved_argc = argc;
19         saved_argv = argv;
20 }
21 
22 bool kexec_loaded(void);
23 
24 int prot_from_flags(int flags) _const_;
25 
26 bool in_initrd(void);
27 void in_initrd_force(bool value);
28 
29 /* Note: log2(0) == log2(1) == 0 here and below. */
30 
31 #define CONST_LOG2ULL(x) ((x) > 1 ? (unsigned) __builtin_clzll(x) ^ 63U : 0)
32 #define NONCONST_LOG2ULL(x) ({                                     \
33                 unsigned long long _x = (x);                       \
34                 _x > 1 ? (unsigned) __builtin_clzll(_x) ^ 63U : 0; \
35         })
36 #define LOG2ULL(x) __builtin_choose_expr(__builtin_constant_p(x), CONST_LOG2ULL(x), NONCONST_LOG2ULL(x))
37 
log2u64(uint64_t x)38 static inline unsigned log2u64(uint64_t x) {
39 #if __SIZEOF_LONG_LONG__ == 8
40         return LOG2ULL(x);
41 #else
42 #  error "Wut?"
43 #endif
44 }
45 
u32ctz(uint32_t n)46 static inline unsigned u32ctz(uint32_t n) {
47 #if __SIZEOF_INT__ == 4
48         return n != 0 ? __builtin_ctz(n) : 32;
49 #else
50 #  error "Wut?"
51 #endif
52 }
53 
54 #define CONST_LOG2U(x) ((x) > 1 ? __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1 : 0)
55 #define NONCONST_LOG2U(x) ({                                             \
56                 unsigned _x = (x);                                       \
57                 _x > 1 ? __SIZEOF_INT__ * 8 - __builtin_clz(_x) - 1 : 0; \
58         })
59 #define LOG2U(x) __builtin_choose_expr(__builtin_constant_p(x), CONST_LOG2U(x), NONCONST_LOG2U(x))
60 
log2i(int x)61 static inline unsigned log2i(int x) {
62         return LOG2U(x);
63 }
64 
log2u(unsigned x)65 static inline unsigned log2u(unsigned x) {
66         return LOG2U(x);
67 }
68 
log2u_round_up(unsigned x)69 static inline unsigned log2u_round_up(unsigned x) {
70         if (x <= 1)
71                 return 0;
72 
73         return log2u(x - 1) + 1;
74 }
75 
76 int container_get_leader(const char *machine, pid_t *pid);
77 
78 int version(void);
79 
80 void disable_coredumps(void);
81