bcachefs-tools/include/linux/random.h

52 lines
1012 B
C
Raw Normal View History

2017-01-08 12:13:18 +03:00
/*
* include/linux/random.h
*
* Include file for the random number generator.
*/
#ifndef _LINUX_RANDOM_H
#define _LINUX_RANDOM_H
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/bug.h>
2019-04-11 00:37:00 +03:00
#ifdef SYS_getrandom
2017-01-08 12:13:18 +03:00
static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
{
return syscall(SYS_getrandom, buf, buflen, flags);
}
2017-08-16 02:19:06 +03:00
#else
extern int urandom_fd;
static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
{
return read(urandom_fd, buf, buflen);
}
#endif
2017-01-08 12:13:18 +03:00
static inline void get_random_bytes(void *buf, int nbytes)
{
BUG_ON(getrandom(buf, nbytes, 0) != nbytes);
}
static inline void prandom_bytes(void *buf, int nbytes)
{
return get_random_bytes(buf, nbytes);
}
#define get_random_type(type) \
static inline type get_random_##type(void) \
{ \
type v; \
\
get_random_bytes(&v, sizeof(v)); \
return v; \
2017-01-08 12:13:18 +03:00
}
get_random_type(int);
get_random_type(long);
get_random_type(u32);
get_random_type(u64);
2017-12-14 00:00:44 +03:00
2017-01-08 12:13:18 +03:00
#endif /* _LINUX_RANDOM_H */