mirror of
https://github.com/AuxXxilium/eudev.git
synced 2024-12-20 21:50:23 +07:00
util: introduce random_ull()
This commit is contained in:
parent
10e87ee7f6
commit
d3782d60cd
20
src/util.c
20
src/util.c
@ -2141,6 +2141,26 @@ int dir_is_empty(const char *path) {
|
||||
return r;
|
||||
}
|
||||
|
||||
unsigned long long random_ull(void) {
|
||||
int fd;
|
||||
uint64_t ull;
|
||||
ssize_t r;
|
||||
|
||||
if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
|
||||
goto fallback;
|
||||
|
||||
r = loop_read(fd, &ull, sizeof(ull));
|
||||
close_nointr_nofail(fd);
|
||||
|
||||
if (r != sizeof(ull))
|
||||
goto fallback;
|
||||
|
||||
return ull;
|
||||
|
||||
fallback:
|
||||
return random() * RAND_MAX + random();
|
||||
}
|
||||
|
||||
static const char *const ioprio_class_table[] = {
|
||||
[IOPRIO_CLASS_NONE] = "none",
|
||||
[IOPRIO_CLASS_RT] = "realtime",
|
||||
|
@ -196,6 +196,8 @@ int make_stdio(int fd);
|
||||
|
||||
bool is_clean_exit(int code, int status);
|
||||
|
||||
unsigned long long random_ull(void);
|
||||
|
||||
#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
|
||||
const char *name##_to_string(type i) { \
|
||||
if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
|
||||
|
Loading…
Reference in New Issue
Block a user