• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 #include <inttypes.h>
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>
10 #include <time.h>
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)
18 char *strchr_replace(char *s, char c, char r);
19 void *memdup(const void *p, size_t n) __attribute__((nonnull(1)));
20 
21 /* module-related functions                                                 */
22 /* ************************************************************************ */
23 #define KMOD_EXTENSION_UNCOMPRESSED ".ko"
24 
25 int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
26 int underscores(char *s) _must_check_;
27 char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
28 char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(2)));
29 bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1)));
30 
31 /* read-like and fread-like functions                                       */
32 /* ************************************************************************ */
33 ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2)));
34 ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((nonnull(2)));
35 int read_str_long(int fd, long *value, int base) _must_check_ __attribute__((nonnull(2)));
36 int read_str_ulong(int fd, unsigned long *value, int base) _must_check_ __attribute__((nonnull(2)));
37 char *freadline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1)));
38 
39 /* path handling functions                                                  */
40 /* ************************************************************************ */
41 bool path_is_absolute(const char *p) _must_check_ __attribute__((nonnull(1)));
42 char *path_make_absolute_cwd(const char *p) _must_check_ __attribute__((nonnull(1)));
43 int mkdir_p(const char *path, int len, mode_t mode);
44 int mkdir_parents(const char *path, mode_t mode);
45 unsigned long long stat_mstamp(const struct stat *st);
46 
47 /* time-related functions
48  * ************************************************************************ */
49 #define USEC_PER_SEC	1000000ULL
50 #define USEC_PER_MSEC	1000ULL
51 #define MSEC_PER_SEC	1000ULL
52 #define NSEC_PER_MSEC	1000000ULL
53 
54 unsigned long long ts_usec(const struct timespec *ts);
55 unsigned long long ts_msec(const struct timespec *ts);
56 unsigned long long now_usec(void);
57 unsigned long long now_msec(void);
58 int sleep_until_msec(unsigned long long msec);
59 unsigned long long get_backoff_delta_msec(unsigned long long t0,
60 					  unsigned long long tend,
61 					  unsigned long long *delta);
62 
63 
64 /* endianess and alignments                                                 */
65 /* ************************************************************************ */
66 #define get_unaligned(ptr)			\
67 ({						\
68 	struct __attribute__((packed)) {	\
69 		typeof(*(ptr)) __v;		\
70 	} *__p = (typeof(__p)) (ptr);		\
71 	__p->__v;				\
72 })
73 
74 #define put_unaligned(val, ptr)			\
75 do {						\
76 	struct __attribute__((packed)) {	\
77 		typeof(*(ptr)) __v;		\
78 	} *__p = (typeof(__p)) (ptr);		\
79 	__p->__v = (val);			\
80 } while(0)
81 
ALIGN_POWER2(unsigned int u)82 static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
83 {
84 	return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
85 }
86 
87 /* misc                                                                     */
88 /* ************************************************************************ */
freep(void * p)89 static inline void freep(void *p) {
90         free(*(void**) p);
91 }
92 #define _cleanup_free_ _cleanup_(freep)
93 
addu64_overflow(uint64_t a,uint64_t b,uint64_t * res)94 static inline bool addu64_overflow(uint64_t a, uint64_t b, uint64_t *res)
95 {
96 #if (HAVE___BUILTIN_UADDL_OVERFLOW && HAVE___BUILTIN_UADDLL_OVERFLOW)
97 #if __SIZEOF_LONG__ == 8
98 	return __builtin_uaddl_overflow(a, b, res);
99 #elif __SIZEOF_LONG_LONG__ == 8
100 	return __builtin_uaddll_overflow(a, b, res);
101 #else
102 #error "sizeof(long long) != 8"
103 #endif
104 #endif
105 	*res = a + b;
106 	return UINT64_MAX - a < b;
107 }
108