]> git.ipfire.org Git - thirdparty/kmod.git/blame - shared/util.h
libkmod: keep KMOD_FILE_COMPRESSION_NONE/load_reg in comp_types
[thirdparty/kmod.git] / shared / util.h
CommitLineData
96573a02
LDM
1#pragma once
2
aac5f451 3#include <inttypes.h>
96573a02
LDM
4#include <limits.h>
5#include <stdbool.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <sys/types.h>
9#include <sys/stat.h>
5622f1da 10#include <time.h>
96573a02
LDM
11
12#include <shared/macro.h>
13
14/* string handling functions and memory allocations */
15/* ************************************************************************ */
16#define streq(a, b) (strcmp((a), (b)) == 0)
17#define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0)
9a437531 18char *strchr_replace(char *s, char c, char r);
96573a02 19void *memdup(const void *p, size_t n) __attribute__((nonnull(1)));
2b0104fe
LDM
20
21/* module-related functions */
22/* ************************************************************************ */
f4e8c162
LDM
23#define KMOD_EXTENSION_UNCOMPRESSED ".ko"
24
2b0104fe 25int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
52c9c990 26int underscores(char *s) _must_check_;
f4e8c162
LDM
27char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
28char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(2)));
29bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1)));
96573a02
LDM
30
31/* read-like and fread-like functions */
32/* ************************************************************************ */
33ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2)));
34ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((nonnull(2)));
35int read_str_long(int fd, long *value, int base) _must_check_ __attribute__((nonnull(2)));
36int read_str_ulong(int fd, unsigned long *value, int base) _must_check_ __attribute__((nonnull(2)));
aafd3835 37char *freadline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1)));
96573a02
LDM
38
39/* path handling functions */
40/* ************************************************************************ */
96573a02
LDM
41char *path_make_absolute_cwd(const char *p) _must_check_ __attribute__((nonnull(1)));
42int mkdir_p(const char *path, int len, mode_t mode);
43int mkdir_parents(const char *path, mode_t mode);
44unsigned long long stat_mstamp(const struct stat *st);
5622f1da
LDM
45
46/* time-related functions
47 * ************************************************************************ */
48#define USEC_PER_SEC 1000000ULL
49#define USEC_PER_MSEC 1000ULL
ba105faf
LDM
50#define MSEC_PER_SEC 1000ULL
51#define NSEC_PER_MSEC 1000000ULL
5622f1da 52
5622f1da 53unsigned long long now_usec(void);
ba105faf 54unsigned long long now_msec(void);
8ab15ece
LDM
55int sleep_until_msec(unsigned long long msec);
56unsigned long long get_backoff_delta_msec(unsigned long long t0,
57 unsigned long long tend,
58 unsigned long long *delta);
59
96573a02
LDM
60
61/* endianess and alignments */
62/* ************************************************************************ */
63#define get_unaligned(ptr) \
64({ \
65 struct __attribute__((packed)) { \
66 typeof(*(ptr)) __v; \
67 } *__p = (typeof(__p)) (ptr); \
68 __p->__v; \
69})
70
71#define put_unaligned(val, ptr) \
72do { \
73 struct __attribute__((packed)) { \
74 typeof(*(ptr)) __v; \
75 } *__p = (typeof(__p)) (ptr); \
76 __p->__v = (val); \
77} while(0)
78
79static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
80{
81 return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
82}
83
84/* misc */
85/* ************************************************************************ */
86static inline void freep(void *p) {
87 free(*(void**) p);
88}
89#define _cleanup_free_ _cleanup_(freep)
aac5f451
LDM
90
91static inline bool addu64_overflow(uint64_t a, uint64_t b, uint64_t *res)
92{
93#if (HAVE___BUILTIN_UADDL_OVERFLOW && HAVE___BUILTIN_UADDLL_OVERFLOW)
94#if __SIZEOF_LONG__ == 8
95 return __builtin_uaddl_overflow(a, b, res);
96#elif __SIZEOF_LONG_LONG__ == 8
97 return __builtin_uaddll_overflow(a, b, res);
98#else
99#error "sizeof(long long) != 8"
100#endif
101#endif
102 *res = a + b;
a8c73b86 103 return UINT64_MAX - a < b;
aac5f451 104}