]> git.ipfire.org Git - thirdparty/kmod.git/blame_incremental - shared/util.h
testsuite: simplify syscall number handling
[thirdparty/kmod.git] / shared / util.h
... / ...
CommitLineData
1#pragma once
2
3#include <inttypes.h>
4#include <limits.h>
5#include <stdarg.h>
6#include <stdbool.h>
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <time.h>
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)
20char *strchr_replace(char *s, char c, char r);
21_nonnull_all_ void *memdup(const void *p, size_t n);
22
23/* module-related functions */
24/* ************************************************************************ */
25#define KMOD_EXTENSION_UNCOMPRESSED ".ko"
26
27_must_check_ _nonnull_(1, 2) int alias_normalize(const char *alias,
28 char buf[static PATH_MAX], size_t *len);
29_must_check_ int underscores(char *s);
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);
34_nonnull_all_ bool path_ends_with_kmod_ext(const char *path, size_t len);
35
36/* read-like and fread-like functions */
37/* ************************************************************************ */
38_must_check_ _nonnull_(2) ssize_t pread_str_safe(int fd, char *buf, size_t buflen,
39 off_t off);
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);
45
46/* path handling functions */
47/* ************************************************************************ */
48_must_check_ _nonnull_all_ char *path_make_absolute_cwd(const char *p);
49static inline _must_check_ _nonnull_all_ bool path_is_absolute(const char *p)
50{
51 return p[0] == '/';
52}
53
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);
57_nonnull_all_ int fd_lookup_path(int fd, char *path, size_t pathlen);
58
59/* time-related functions
60 * ************************************************************************ */
61#define USEC_PER_SEC 1000000ULL
62#define USEC_PER_MSEC 1000ULL
63#define MSEC_PER_SEC 1000ULL
64#define NSEC_PER_MSEC 1000000ULL
65
66unsigned long long ts_usec(const struct timespec *ts);
67unsigned long long now_usec(void);
68unsigned long long now_msec(void);
69int sleep_until_msec(unsigned long long msec);
70unsigned long long get_backoff_delta_msec(unsigned long long tend,
71 unsigned long long *delta);
72
73/* endianness and alignments */
74/* ************************************************************************ */
75#define get_unaligned(ptr) \
76 ({ \
77 struct __attribute__((packed)) { \
78 typeof(*(ptr)) __v; \
79 } *__p = (typeof(__p))(ptr); \
80 __p->__v; \
81 })
82
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)
90
91static inline unsigned int align_power2(unsigned int u)
92{
93 return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
94}
95
96/* misc */
97/* ************************************************************************ */
98
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
109static inline bool uadd64_overflow(uint64_t a, uint64_t b, uint64_t *res)
110{
111#if (HAVE___BUILTIN_UADDL_OVERFLOW && __SIZEOF_LONG__ == 8)
112 return __builtin_uaddl_overflow(a, b, res);
113#elif (HAVE___BUILTIN_UADDLL_OVERFLOW && __SIZEOF_LONG_LONG__ == 8)
114 return __builtin_uaddll_overflow(a, b, res);
115#else
116 *res = a + b;
117 return UINT64_MAX - a < b;
118#endif
119}
120
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
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}
153
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
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}
175
176#define TAKE_PTR(x) \
177 ({ \
178 typeof(x) x__ = x; \
179 (x) = NULL; \
180 x__; \
181 })
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 */
202#define DECLARE_DLSYM(symbol) static typeof(symbol) *sym_##symbol
203
204/* Pointer indirection to support linking directly */
205#define DECLARE_PTRSYM(symbol) static typeof(symbol) *sym_##symbol = symbol
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