]> git.ipfire.org Git - thirdparty/kmod.git/blame - shared/util.h
testsuite: simplify syscall number handling
[thirdparty/kmod.git] / shared / util.h
CommitLineData
96573a02
LDM
1#pragma once
2
aac5f451 3#include <inttypes.h>
96573a02 4#include <limits.h>
1f49475e 5#include <stdarg.h>
96573a02
LDM
6#include <stdbool.h>
7#include <stdlib.h>
8#include <stdio.h>
11eb9bc6 9#include <string.h>
96573a02
LDM
10#include <sys/types.h>
11#include <sys/stat.h>
5622f1da 12#include <time.h>
96573a02
LDM
13
14#include <shared/macro.h>
15
16/* string handling functions and memory allocations */
17/* ************************************************************************ */
18#define streq(a, b) (strcmp((a), (b)) == 0)
19#define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0)
9a437531 20char *strchr_replace(char *s, char c, char r);
26f2c17b 21_nonnull_all_ void *memdup(const void *p, size_t n);
2b0104fe
LDM
22
23/* module-related functions */
24/* ************************************************************************ */
f4e8c162
LDM
25#define KMOD_EXTENSION_UNCOMPRESSED ".ko"
26
1d0117f8
EV
27_must_check_ _nonnull_(1, 2) int alias_normalize(const char *alias,
28 char buf[static PATH_MAX], size_t *len);
2f3be925 29_must_check_ int underscores(char *s);
1d0117f8
EV
30_nonnull_(1, 2) char *modname_normalize(const char *modname, char buf[static PATH_MAX],
31 size_t *len);
32_nonnull_(2) char *path_to_modname(const char *path, char buf[static PATH_MAX],
33 size_t *len);
26f2c17b 34_nonnull_all_ bool path_ends_with_kmod_ext(const char *path, size_t len);
96573a02
LDM
35
36/* read-like and fread-like functions */
37/* ************************************************************************ */
8d03b6c7
TS
38_must_check_ _nonnull_(2) ssize_t pread_str_safe(int fd, char *buf, size_t buflen,
39 off_t off);
1d20e3e8
EV
40_must_check_ _nonnull_(2) ssize_t read_str_safe(int fd, char *buf, size_t buflen);
41_nonnull_(2) ssize_t write_str_safe(int fd, const char *buf, size_t buflen);
42_must_check_ _nonnull_(2) int read_str_long(int fd, long *value, int base);
43_must_check_ _nonnull_(2) int read_str_ulong(int fd, unsigned long *value, int base);
44_nonnull_(1) char *freadline_wrapped(FILE *fp, unsigned int *linenum);
96573a02
LDM
45
46/* path handling functions */
47/* ************************************************************************ */
26f2c17b 48_must_check_ _nonnull_all_ char *path_make_absolute_cwd(const char *p);
63302cf6
LDM
49static inline _must_check_ _nonnull_all_ bool path_is_absolute(const char *p)
50{
51 return p[0] == '/';
52}
53
96573a02
LDM
54int mkdir_p(const char *path, int len, mode_t mode);
55int mkdir_parents(const char *path, mode_t mode);
56unsigned long long stat_mstamp(const struct stat *st);
9ad9a8d6 57_nonnull_all_ int fd_lookup_path(int fd, char *path, size_t pathlen);
5622f1da
LDM
58
59/* time-related functions
60 * ************************************************************************ */
1d0117f8
EV
61#define USEC_PER_SEC 1000000ULL
62#define USEC_PER_MSEC 1000ULL
63#define MSEC_PER_SEC 1000ULL
64#define NSEC_PER_MSEC 1000000ULL
5622f1da 65
24979504 66unsigned long long ts_usec(const struct timespec *ts);
5622f1da 67unsigned long long now_usec(void);
ba105faf 68unsigned long long now_msec(void);
8ab15ece 69int sleep_until_msec(unsigned long long msec);
d2ee70ea 70unsigned long long get_backoff_delta_msec(unsigned long long tend,
8ab15ece
LDM
71 unsigned long long *delta);
72
717091ac 73/* endianness and alignments */
96573a02 74/* ************************************************************************ */
1d0117f8
EV
75#define get_unaligned(ptr) \
76 ({ \
77 struct __attribute__((packed)) { \
78 typeof(*(ptr)) __v; \
79 } *__p = (typeof(__p))(ptr); \
80 __p->__v; \
81 })
96573a02 82
1d0117f8
EV
83#define put_unaligned(val, ptr) \
84 do { \
85 struct __attribute__((packed)) { \
86 typeof(*(ptr)) __v; \
87 } *__p = (typeof(__p))(ptr); \
88 __p->__v = (val); \
89 } while (0)
96573a02 90
bffe1a78 91static inline unsigned int align_power2(unsigned int u)
96573a02
LDM
92{
93 return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
94}
95
96/* misc */
97/* ************************************************************************ */
aac5f451 98
8808f0ee
EV
99static inline bool uadd32_overflow(uint32_t a, uint32_t b, uint32_t *res)
100{
101#if (HAVE___BUILTIN_UADD_OVERFLOW && __SIZEOF_INT__ == 4)
102 return __builtin_uadd_overflow(a, b, res);
103#else
104 *res = a + b;
105 return UINT32_MAX - a < b;
106#endif
107}
108
b9019723 109static inline bool uadd64_overflow(uint64_t a, uint64_t b, uint64_t *res)
aac5f451 110{
8fa87bf4 111#if (HAVE___BUILTIN_UADDL_OVERFLOW && __SIZEOF_LONG__ == 8)
aac5f451 112 return __builtin_uaddl_overflow(a, b, res);
8fa87bf4 113#elif (HAVE___BUILTIN_UADDLL_OVERFLOW && __SIZEOF_LONG_LONG__ == 8)
aac5f451
LDM
114 return __builtin_uaddll_overflow(a, b, res);
115#else
aac5f451 116 *res = a + b;
a8c73b86 117 return UINT64_MAX - a < b;
8fa87bf4 118#endif
aac5f451 119}
7eba0cd2 120
88c11d28
EV
121static inline bool uaddsz_overflow(size_t a, size_t b, size_t *res)
122{
123#if __SIZEOF_SIZE_T__ == 8
124 return uadd64_overflow(a, b, res);
125#elif __SIZEOF_SIZE_T__ == 4
126 return uadd32_overflow(a, b, res);
127#else
128#error "Unknown sizeof(size_t)"
129#endif
130}
131
7eba0cd2
EV
132static inline bool umul32_overflow(uint32_t a, uint32_t b, uint32_t *res)
133{
134#if (HAVE___BUILTIN_UMUL_OVERFLOW && __SIZEOF_INT__ == 4)
135 return __builtin_umul_overflow(a, b, res);
136#else
137 *res = a * b;
138 return UINT32_MAX / a < b;
139#endif
140}
141
142static inline bool umul64_overflow(uint64_t a, uint64_t b, uint64_t *res)
143{
144#if (HAVE___BUILTIN_UMULL_OVERFLOW && __SIZEOF_LONG__ == 8)
145 return __builtin_umull_overflow(a, b, res);
146#elif (HAVE___BUILTIN_UMULLL_OVERFLOW && __SIZEOF_LONG_LONG__ == 8)
147 return __builtin_umulll_overflow(a, b, res);
148#else
149 *res = a * b;
150 return UINT64_MAX / a < b;
151#endif
152}
88c11d28 153
65afd0f1
TS
154static inline bool umulll_overflow(unsigned long long a, unsigned long long b,
155 unsigned long long *res)
156{
157#if (HAVE___BUILTIN_UMULLL_OVERFLOW)
158 return __builtin_umulll_overflow(a, b, res);
159#else
160 *res = a * b;
161 return UINT64_MAX / a < b;
162#endif
163}
164
88c11d28
EV
165static inline bool umulsz_overflow(size_t a, size_t b, size_t *res)
166{
167#if __SIZEOF_SIZE_T__ == 8
168 return umul64_overflow(a, b, res);
169#elif __SIZEOF_SIZE_T__ == 4
170 return umul32_overflow(a, b, res);
171#else
172#error "Unknown sizeof(size_t)"
173#endif
174}
1c8fae2f
LDM
175
176#define TAKE_PTR(x) \
177 ({ \
178 typeof(x) x__ = x; \
179 (x) = NULL; \
180 x__; \
181 })
1f49475e
LDM
182
183/* dlfcn helpers */
184/* ************************************************************************ */
185
186/*
187 * Load many various symbols from @filename.
188 * @dlp: pointer to the previous results of this call: it's set when it succeeds
189 * @filename: the library to dlopen() and look for symbols
190 * @...: or 1 more tuples created by DLSYM_ARG() with ( &var, "symbol name" ).
191 */
192_sentinel_ int dlsym_many(void **dlp, const char *filename, ...);
193
194/*
195 * Helper to create tuples passed as arguments to dlsym_many().
196 * @symbol__: symbol to create arguments for. Example: DLSYM_ARG(foo) expands to
197 * `&sym_foo, "foo"`
198 */
199#define DLSYM_ARG(symbol__) &sym_##symbol__, STRINGIFY(symbol__),
200
201/* For symbols being dynamically loaded */
13b646a7 202#define DECLARE_DLSYM(symbol) static typeof(symbol) *sym_##symbol
1f49475e
LDM
203
204/* Pointer indirection to support linking directly */
e3acc663 205#define DECLARE_PTRSYM(symbol) static typeof(symbol) *sym_##symbol = symbol
1f49475e
LDM
206
207/*
208 * Helper defines, to be done locally before including this header to switch between
209 * implementations
210 */
211#if defined(DLSYM_LOCALLY_ENABLED) && DLSYM_LOCALLY_ENABLED
212#define DECLARE_SYM(sym__) DECLARE_DLSYM(sym__);
213#else
214#define DECLARE_SYM(sym__) DECLARE_PTRSYM(sym__);
215#endif