]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: add random-util.[ch]
authorRonny Chevalier <chevalier.ronny@gmail.com>
Fri, 10 Apr 2015 20:27:10 +0000 (22:27 +0200)
committerRonny Chevalier <chevalier.ronny@gmail.com>
Fri, 10 Apr 2015 22:11:13 +0000 (00:11 +0200)
24 files changed:
Makefile.am
src/firstboot/firstboot.c
src/journal/journal-file.c
src/journal/journald-rate-limit.c
src/journal/test-compress.c
src/libsystemd-network/sd-dhcp-client.c
src/libsystemd-network/sd-dhcp6-client.c
src/libsystemd-network/sd-ipv4ll.c
src/libsystemd-network/sd-pppoe.c
src/libsystemd-terminal/modeset.c
src/libsystemd/sd-id128/sd-id128.c
src/nspawn/nspawn.c
src/resolve/resolved-dns-scope.c
src/resolve/resolved-dns-transaction.c
src/resolve/resolved-manager.c
src/shared/ask-password-api.c
src/shared/hashmap.c
src/shared/random-util.c [new file with mode: 0644]
src/shared/random-util.h [new file with mode: 0644]
src/shared/util.c
src/shared/util.h
src/udev/cdrom_id/cdrom_id.c
src/udev/net/link-config.c
src/udev/scsi_id/scsi_serial.c

index ceb1d7d565f41806b79ee46938dc09ac99bb0b4e..f348ef90f36bf857b20d60e2540f07406f33cc8a 100644 (file)
@@ -901,6 +901,8 @@ libsystemd_shared_la_SOURCES = \
        src/shared/memfd-util.h \
        src/shared/process-util.c \
        src/shared/process-util.h \
+       src/shared/random-util.c \
+       src/shared/random-util.h \
        src/shared/uid-range.c \
        src/shared/uid-range.h \
        src/shared/nss-util.h \
index a37ca170fb550f685e91685ef0fb8c3f54f529e6..c92f379806d5816aafe8258063ea806516ad7a86 100644 (file)
@@ -32,6 +32,7 @@
 #include "mkdir.h"
 #include "time-util.h"
 #include "path-util.h"
+#include "random-util.h"
 #include "locale-util.h"
 #include "ask-password-api.h"
 
index 6bbcc6d777dde8aaffa0df7ecea3c0dcea51e779..a432eb08a613b483eeceee10d207b4cde6d975b0 100644 (file)
@@ -34,6 +34,7 @@
 #include "journal-authenticate.h"
 #include "lookup3.h"
 #include "compress.h"
+#include "random-util.h"
 
 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem))
 #define DEFAULT_FIELD_HASH_TABLE_SIZE (333ULL*sizeof(HashItem))
index 6d779d2966abc6f061938fdcfa55181b7963b2d8..6f83035a4ee36f050bbfb26620c8839e65893a09 100644 (file)
@@ -26,6 +26,7 @@
 #include "list.h"
 #include "util.h"
 #include "hashmap.h"
+#include "random-util.h"
 
 #define POOLS_MAX 5
 #define BUCKETS_MAX 127
index ae41c0c4c792c5a05b4d20e9f85d808a2c7f3817..41a566d7144d44f5514080881749e7f135682857 100644 (file)
@@ -20,6 +20,7 @@
 #include "compress.h"
 #include "util.h"
 #include "macro.h"
+#include "random-util.h"
 
 #ifdef HAVE_XZ
 # define XZ_OK 0
index 4224e0197cdfa6729dfcb57ba21e8bb95c82a4a5..c44392eb81d26682a72ff3a7cecaf9afc0897874 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "util.h"
 #include "refcnt.h"
+#include "random-util.h"
 #include "async.h"
 
 #include "dhcp-protocol.h"
index 3db1cb09dbe88cd40f047dde7665bd30388a9be5..9d88d469f3753fe800ab6e0e244c4f9e051220f6 100644 (file)
@@ -28,6 +28,7 @@
 #include "udev-util.h"
 #include "util.h"
 #include "refcnt.h"
+#include "random-util.h"
 
 #include "network-internal.h"
 #include "sd-dhcp6-client.h"
index 02f2f9e0a91cf3216e9f9df360ee206243957975..9e04db96bb61fa179fe3f005bb7c473021bc08be 100644 (file)
@@ -27,6 +27,7 @@
 #include "siphash24.h"
 #include "list.h"
 #include "refcnt.h"
+#include "random-util.h"
 
 #include "ipv4ll-internal.h"
 #include "sd-ipv4ll.h"
index 601f3bd400a84961a5a66311c5f445275e152461..1de8a5e8bf1b9eac2cabf0bbcdf0d8e3968a5262 100644 (file)
@@ -33,6 +33,7 @@
 #include "event-util.h"
 
 #include "util.h"
+#include "random-util.h"
 #include "socket-util.h"
 #include "async.h"
 #include "refcnt.h"
index cccaabaa0622037de627e1795861872c5abb2271..621d6c4fa3dca7cb6ac2f4fe13b2568e798aa951 100644 (file)
 #include <systemd/sd-login.h>
 #include <termios.h>
 #include <unistd.h>
+
 #include "build.h"
 #include "grdev.h"
 #include "macro.h"
 #include "sysview.h"
 #include "util.h"
+#include "random-util.h"
 
 typedef struct Modeset Modeset;
 
index f0ffedc38b39c9c363a4a1b80ab2760a77108f33..46f2181ea809504b3126819fec1fd8c8d30814b3 100644 (file)
@@ -26,6 +26,7 @@
 #include "util.h"
 #include "macro.h"
 #include "sd-id128.h"
+#include "random-util.h"
 
 _public_ char *sd_id128_to_string(sd_id128_t id, char s[33]) {
         unsigned n;
index 96075327df449adbfb702edfb2aa9baff6290a7b..6417a8c3b3bb201acaa0384c8b255fbd4c3e1b7e 100644 (file)
@@ -52,6 +52,7 @@
 #include <blkid/blkid.h>
 #endif
 
+#include "random-util.h"
 #include "sd-daemon.h"
 #include "sd-bus.h"
 #include "sd-id128.h"
index ba116de6f5ad599d07ba27c9a55ec27d7e4785f6..b2bc09266ce8842fadfe64a53736845fe97c8af5 100644 (file)
@@ -27,6 +27,7 @@
 #include "af-list.h"
 #include "resolved-dns-domain.h"
 #include "resolved-dns-scope.h"
+#include "random-util.h"
 
 #define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
 #define MULTICAST_RATELIMIT_BURST 1000
index bc1a90db1bb22ebdabf1316e71df6502743237b3..214938986d14dc37cdce9efb8dbeeccbfc0f39ba 100644 (file)
@@ -22,6 +22,7 @@
 #include "af-list.h"
 
 #include "resolved-dns-transaction.h"
+#include "random-util.h"
 
 DnsTransaction* dns_transaction_free(DnsTransaction *t) {
         DnsQuery *q;
index adaa6c6e7f0df81d5bccd4ddb06949f6fda00ef3..a10a3277bef40923de08d571f00698b00902edb5 100644 (file)
@@ -31,6 +31,7 @@
 #include "utf8.h"
 #include "fileio-label.h"
 #include "ordered-set.h"
+#include "random-util.h"
 
 #include "resolved-dns-domain.h"
 #include "resolved-conf.h"
index dfc89d23fd91cadb07d193fea2130bf65265c44f..ad1a7731ff197c15ab6927eebe8550c2ca9b492a 100644 (file)
@@ -35,6 +35,7 @@
 #include "formats-util.h"
 #include "mkdir.h"
 #include "strv.h"
+#include "random-util.h"
 
 #include "ask-password-api.h"
 
index 48d75023918afadc54df0bf7597a9d38d79f485f..d8ea9d5aa8f7a15872c8fd4392617f50d05add93 100644 (file)
@@ -30,6 +30,7 @@
 #include "siphash24.h"
 #include "strv.h"
 #include "mempool.h"
+#include "random-util.h"
 
 #ifdef ENABLE_DEBUG_HASHMAP
 #include "list.h"
diff --git a/src/shared/random-util.c b/src/shared/random-util.c
new file mode 100644 (file)
index 0000000..88f5182
--- /dev/null
@@ -0,0 +1,127 @@
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdint.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <time.h>
+#include <sys/auxv.h>
+#include <linux/random.h>
+
+#include "random-util.h"
+#include "time-util.h"
+#include "missing.h"
+#include "util.h"
+
+int dev_urandom(void *p, size_t n) {
+        static int have_syscall = -1;
+
+        _cleanup_close_ int fd = -1;
+        int r;
+
+        /* Gathers some randomness from the kernel. This call will
+         * never block, and will always return some data from the
+         * kernel, regardless if the random pool is fully initialized
+         * or not. It thus makes no guarantee for the quality of the
+         * returned entropy, but is good enough for or usual usecases
+         * of seeding the hash functions for hashtable */
+
+        /* Use the getrandom() syscall unless we know we don't have
+         * it, or when the requested size is too large for it. */
+        if (have_syscall != 0 || (size_t) (int) n != n) {
+                r = getrandom(p, n, GRND_NONBLOCK);
+                if (r == (int) n) {
+                        have_syscall = true;
+                        return 0;
+                }
+
+                if (r < 0) {
+                        if (errno == ENOSYS)
+                                /* we lack the syscall, continue with
+                                 * reading from /dev/urandom */
+                                have_syscall = false;
+                        else if (errno == EAGAIN)
+                                /* not enough entropy for now. Let's
+                                 * remember to use the syscall the
+                                 * next time, again, but also read
+                                 * from /dev/urandom for now, which
+                                 * doesn't care about the current
+                                 * amount of entropy.  */
+                                have_syscall = true;
+                        else
+                                return -errno;
+                } else
+                        /* too short read? */
+                        return -ENODATA;
+        }
+
+        fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+        if (fd < 0)
+                return errno == ENOENT ? -ENOSYS : -errno;
+
+        return loop_read_exact(fd, p, n, true);
+}
+
+void initialize_srand(void) {
+        static bool srand_called = false;
+        unsigned x;
+#ifdef HAVE_SYS_AUXV_H
+        void *auxv;
+#endif
+
+        if (srand_called)
+                return;
+
+        x = 0;
+
+#ifdef HAVE_SYS_AUXV_H
+        /* The kernel provides us with a bit of entropy in auxv, so
+         * let's try to make use of that to seed the pseudo-random
+         * generator. It's better than nothing... */
+
+        auxv = (void*) getauxval(AT_RANDOM);
+        if (auxv)
+                x ^= *(unsigned*) auxv;
+#endif
+
+        x ^= (unsigned) now(CLOCK_REALTIME);
+        x ^= (unsigned) gettid();
+
+        srand(x);
+        srand_called = true;
+}
+
+void random_bytes(void *p, size_t n) {
+        uint8_t *q;
+        int r;
+
+        r = dev_urandom(p, n);
+        if (r >= 0)
+                return;
+
+        /* If some idiot made /dev/urandom unavailable to us, he'll
+         * get a PRNG instead. */
+
+        initialize_srand();
+
+        for (q = p; q < (uint8_t*) p + n; q ++)
+                *q = rand();
+}
diff --git a/src/shared/random-util.h b/src/shared/random-util.h
new file mode 100644 (file)
index 0000000..f7862c8
--- /dev/null
@@ -0,0 +1,38 @@
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdint.h>
+
+int dev_urandom(void *p, size_t n);
+void random_bytes(void *p, size_t n);
+void initialize_srand(void);
+
+static inline uint64_t random_u64(void) {
+        uint64_t u;
+        random_bytes(&u, sizeof(u));
+        return u;
+}
+
+static inline uint32_t random_u32(void) {
+        uint32_t u;
+        random_bytes(&u, sizeof(u));
+        return u;
+}
index d4753f1ee8779d5dabf5321f51ed2a5f0b5b6d5a..e4c4dd92f13d2961ff35e9f4c45b1817f2b01d29 100644 (file)
@@ -94,6 +94,7 @@
 #include "sparse-endian.h"
 #include "formats-util.h"
 #include "process-util.h"
+#include "random-util.h"
 
 /* Put this test here for a lack of better place */
 assert_cc(EAGAIN == EWOULDBLOCK);
@@ -2387,101 +2388,6 @@ char* dirname_malloc(const char *path) {
         return dir;
 }
 
-int dev_urandom(void *p, size_t n) {
-        static int have_syscall = -1;
-
-        _cleanup_close_ int fd = -1;
-        int r;
-
-        /* Gathers some randomness from the kernel. This call will
-         * never block, and will always return some data from the
-         * kernel, regardless if the random pool is fully initialized
-         * or not. It thus makes no guarantee for the quality of the
-         * returned entropy, but is good enough for or usual usecases
-         * of seeding the hash functions for hashtable */
-
-        /* Use the getrandom() syscall unless we know we don't have
-         * it, or when the requested size is too large for it. */
-        if (have_syscall != 0 || (size_t) (int) n != n) {
-                r = getrandom(p, n, GRND_NONBLOCK);
-                if (r == (int) n) {
-                        have_syscall = true;
-                        return 0;
-                }
-
-                if (r < 0) {
-                        if (errno == ENOSYS)
-                                /* we lack the syscall, continue with
-                                 * reading from /dev/urandom */
-                                have_syscall = false;
-                        else if (errno == EAGAIN)
-                                /* not enough entropy for now. Let's
-                                 * remember to use the syscall the
-                                 * next time, again, but also read
-                                 * from /dev/urandom for now, which
-                                 * doesn't care about the current
-                                 * amount of entropy.  */
-                                have_syscall = true;
-                        else
-                                return -errno;
-                } else
-                        /* too short read? */
-                        return -ENODATA;
-        }
-
-        fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return errno == ENOENT ? -ENOSYS : -errno;
-
-        return loop_read_exact(fd, p, n, true);
-}
-
-void initialize_srand(void) {
-        static bool srand_called = false;
-        unsigned x;
-#ifdef HAVE_SYS_AUXV_H
-        void *auxv;
-#endif
-
-        if (srand_called)
-                return;
-
-        x = 0;
-
-#ifdef HAVE_SYS_AUXV_H
-        /* The kernel provides us with a bit of entropy in auxv, so
-         * let's try to make use of that to seed the pseudo-random
-         * generator. It's better than nothing... */
-
-        auxv = (void*) getauxval(AT_RANDOM);
-        if (auxv)
-                x ^= *(unsigned*) auxv;
-#endif
-
-        x ^= (unsigned) now(CLOCK_REALTIME);
-        x ^= (unsigned) gettid();
-
-        srand(x);
-        srand_called = true;
-}
-
-void random_bytes(void *p, size_t n) {
-        uint8_t *q;
-        int r;
-
-        r = dev_urandom(p, n);
-        if (r >= 0)
-                return;
-
-        /* If some idiot made /dev/urandom unavailable to us, he'll
-         * get a PRNG instead. */
-
-        initialize_srand();
-
-        for (q = p; q < (uint8_t*) p + n; q ++)
-                *q = rand();
-}
-
 void rename_process(const char name[8]) {
         assert(name);
 
index b939d7f67ee213d3f67212689c6b2271faa1bb95..4d5162f4ca15689769655cc7a994685daed53d64 100644 (file)
@@ -283,22 +283,6 @@ int make_stdio(int fd);
 int make_null_stdio(void);
 int make_console_stdio(void);
 
-int dev_urandom(void *p, size_t n);
-void random_bytes(void *p, size_t n);
-void initialize_srand(void);
-
-static inline uint64_t random_u64(void) {
-        uint64_t u;
-        random_bytes(&u, sizeof(u));
-        return u;
-}
-
-static inline uint32_t random_u32(void) {
-        uint32_t u;
-        random_bytes(&u, sizeof(u));
-        return u;
-}
-
 /* For basic lookup tables with strictly enumerated entries */
 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
         scope const char *name##_to_string(type i) {                    \
index 54a50756a8d15d0444aca106e24fba9d432c0f09..3d74ae50f1b9287580dfbcc4c32b705eb14f67da 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "libudev.h"
 #include "libudev-private.h"
+#include "random-util.h"
 
 /* device info */
 static unsigned int cd_cd_rom;
index 86d09bbff21050a3a0d5ff2b7ba344b795ea1673..00052dd8c18a948a6e421b60be098193b39dbc39 100644 (file)
@@ -37,6 +37,7 @@
 #include "conf-files.h"
 #include "rtnl-util.h"
 #include "network-internal.h"
+#include "random-util.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
index dcfff1d4ea07594630c5508f810312de03d80f65..3691a69d48edb4674903fb0960038058778c0a3f 100644 (file)
@@ -37,6 +37,7 @@
 #include "libudev-private.h"
 #include "scsi.h"
 #include "scsi_id.h"
+#include "random-util.h"
 
 /*
  * A priority based list of id, naa, and binary/ascii for the identifier