X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Futil.c;h=71409034a4e15cb0c35fbbd2994e520cccfbea7c;hp=2bc90da3d2b20bf92ad239c265d42f924e968e77;hb=d3782d60cd47f57f48a9229bdd3badbd2f4bae44;hpb=10e87ee7f66b59a504c0ed2025463ba5faa69923;ds=sidebyside diff --git a/src/util.c b/src/util.c index 2bc90da3d..71409034a 100644 --- a/src/util.c +++ b/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",