chiark / gitweb /
fs-util: add new CHASE_NON_EXISTING flag to chase_symlinks()
[elogind.git] / src / basic / random-util.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 #include <linux/random.h>
27 #include <stdint.h>
28
29 #ifdef HAVE_SYS_AUXV_H
30 #include <sys/auxv.h>
31 #endif
32
33 #include "fd-util.h"
34 #include "io-util.h"
35 #include "missing.h"
36 #include "random-util.h"
37 #include "time-util.h"
38
39 int dev_urandom(void *p, size_t n) {
40         static int have_syscall = -1;
41
42         _cleanup_close_ int fd = -1;
43         int r;
44
45         /* Gathers some randomness from the kernel. This call will
46          * never block, and will always return some data from the
47          * kernel, regardless if the random pool is fully initialized
48          * or not. It thus makes no guarantee for the quality of the
49          * returned entropy, but is good enough for our usual usecases
50          * of seeding the hash functions for hashtable */
51
52         /* Use the getrandom() syscall unless we know we don't have
53          * it, or when the requested size is too large for it. */
54         if (have_syscall != 0 || (size_t) (int) n != n) {
55                 r = getrandom(p, n, GRND_NONBLOCK);
56                 if (r == (int) n) {
57                         have_syscall = true;
58                         return 0;
59                 }
60
61                 if (r < 0) {
62                         if (errno == ENOSYS)
63                                 /* we lack the syscall, continue with
64                                  * reading from /dev/urandom */
65                                 have_syscall = false;
66                         else if (errno == EAGAIN)
67                                 /* not enough entropy for now. Let's
68                                  * remember to use the syscall the
69                                  * next time, again, but also read
70                                  * from /dev/urandom for now, which
71                                  * doesn't care about the current
72                                  * amount of entropy.  */
73                                 have_syscall = true;
74                         else
75                                 return -errno;
76                 } else
77                         /* too short read? */
78                         return -ENODATA;
79         }
80
81         fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
82         if (fd < 0)
83                 return errno == ENOENT ? -ENOSYS : -errno;
84
85         return loop_read_exact(fd, p, n, true);
86 }
87
88 void initialize_srand(void) {
89         static bool srand_called = false;
90         unsigned x;
91 #ifdef HAVE_SYS_AUXV_H
92         void *auxv;
93 #endif
94
95         if (srand_called)
96                 return;
97
98 #ifdef HAVE_SYS_AUXV_H
99         /* The kernel provides us with 16 bytes of entropy in auxv, so let's try to make use of that to seed the
100          * pseudo-random generator. It's better than nothing... */
101
102         auxv = (void*) getauxval(AT_RANDOM);
103         if (auxv) {
104                 assert_cc(sizeof(x) < 16);
105                 memcpy(&x, auxv, sizeof(x));
106         } else
107 #endif
108                 x = 0;
109
110
111         x ^= (unsigned) now(CLOCK_REALTIME);
112         x ^= (unsigned) gettid();
113
114         srand(x);
115         srand_called = true;
116 }
117
118 void random_bytes(void *p, size_t n) {
119         uint8_t *q;
120         int r;
121
122         r = dev_urandom(p, n);
123         if (r >= 0)
124                 return;
125
126         /* If some idiot made /dev/urandom unavailable to us, he'll
127          * get a PRNG instead. */
128
129         initialize_srand();
130
131         for (q = p; q < (uint8_t*) p + n; q ++)
132                 *q = rand();
133 }