chiark / gitweb /
util: introduce random_ull()
authorLennart Poettering <lennart@poettering.net>
Wed, 16 Jun 2010 03:05:36 +0000 (05:05 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 16 Jun 2010 03:05:36 +0000 (05:05 +0200)
src/util.c
src/util.h

index 2bc90da3d2b20bf92ad239c265d42f924e968e77..71409034a4e15cb0c35fbbd2994e520cccfbea7c 100644 (file)
@@ -2141,6 +2141,26 @@ int dir_is_empty(const char *path) {
         return r;
 }
 
         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",
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
index cacc3969b2e9f3e28cd7a5587b63d6165c10def2..0b397ac0d4deb40631d9917af032be3a8e666596 100644 (file)
@@ -196,6 +196,8 @@ int make_stdio(int fd);
 
 bool is_clean_exit(int code, int status);
 
 
 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))      \
 #define DEFINE_STRING_TABLE_LOOKUP(name,type)                           \
         const char *name##_to_string(type i) {                          \
                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \