summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-06-16 05:05:36 +0200
committerLennart Poettering <lennart@poettering.net>2010-06-16 05:05:36 +0200
commitd3782d60cd47f57f48a9229bdd3badbd2f4bae44 (patch)
tree37cc65a4ae0a4ee530457ff1d46584edb65e593a /src/util.c
parent10e87ee7f66b59a504c0ed2025463ba5faa69923 (diff)
util: introduce random_ull()
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 2bc90da3d2..71409034a4 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",