]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/random.h
Update bcachefs sources to 99750eab4d bcachefs: Persist stripe blocks_used
[bcachefs-tools-debian] / include / linux / random.h
1 /*
2  * include/linux/random.h
3  *
4  * Include file for the random number generator.
5  */
6 #ifndef _LINUX_RANDOM_H
7 #define _LINUX_RANDOM_H
8
9 #include <unistd.h>
10 #include <sys/syscall.h>
11 #include <linux/bug.h>
12
13 #ifdef __NR_getrandom
14 static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
15 {
16          return syscall(SYS_getrandom, buf, buflen, flags);
17 }
18 #else
19 extern int urandom_fd;
20
21 static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
22 {
23         return read(urandom_fd, buf, buflen);
24 }
25 #endif
26
27 static inline void get_random_bytes(void *buf, int nbytes)
28 {
29         BUG_ON(getrandom(buf, nbytes, 0) != nbytes);
30 }
31
32 static inline void prandom_bytes(void *buf, int nbytes)
33 {
34         return get_random_bytes(buf, nbytes);
35 }
36
37 #define get_random_type(type)                           \
38 static inline type get_random_##type(void)              \
39 {                                                       \
40         type v;                                         \
41                                                         \
42         get_random_bytes(&v, sizeof(v));                \
43         return v;                                       \
44 }
45
46 get_random_type(int);
47 get_random_type(long);
48 get_random_type(u64);
49
50 #endif /* _LINUX_RANDOM_H */