summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2006-04-18 15:16:24 +0000
committerPierre Ossman <ossman@cendio.se>2006-04-18 15:16:24 +0000
commitc22a0c12e49181d6ea042993e6b4b47db69574b1 (patch)
tree80f3ad2b76c6303ab7affbbe309cda1a93b779d5 /src
parente4b2a47bb1de0f457559510837c46707ef9f6d8b (diff)
Make the probe for RNG sources at runtime since the configure script isn't
compatible with cross-compiling. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@744 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'src')
-rw-r--r--src/daemon/main.c26
-rw-r--r--src/polypcore/random.c77
-rw-r--r--src/polypcore/random.h1
3 files changed, 62 insertions, 42 deletions
diff --git a/src/daemon/main.c b/src/daemon/main.c
index d783531a..4ae9fbf1 100644
--- a/src/daemon/main.c
+++ b/src/daemon/main.c
@@ -63,6 +63,7 @@
#include <polypcore/cli-text.h>
#include <polypcore/pid.h>
#include <polypcore/namereg.h>
+#include <polypcore/random.h>
#include "cmdline.h"
#include "cpulimit.h"
@@ -135,29 +136,6 @@ static void close_pipe(int p[2]) {
p[0] = p[1] = -1;
}
-static void set_random_seed(void) {
- unsigned int seed = 0;
-
-#ifdef RANDOM_DEVICE
- int fd;
-
- if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
- ssize_t r;
-
- if ((r = pa_loop_read(fd, &seed, sizeof(seed))) < 0 || (size_t) r != sizeof(seed)) {
- pa_log_error(__FILE__": failed to read entropy from '"RANDOM_DEVICE"'");
- seed += (unsigned int) time(NULL);
- }
-
- close(fd);
- }
-#else
- seed = (unsigned int) time(NULL);
-#endif
-
- srand(seed);
-}
-
int main(int argc, char *argv[]) {
pa_core *c;
pa_strbuf *buf = NULL;
@@ -203,7 +181,7 @@ int main(int argc, char *argv[]) {
}
#endif
- set_random_seed();
+ pa_random_seed();
pa_log_set_ident("polypaudio");
diff --git a/src/polypcore/random.c b/src/polypcore/random.c
index ffd40474..ffe24994 100644
--- a/src/polypcore/random.c
+++ b/src/polypcore/random.c
@@ -36,31 +36,72 @@
#include "random.h"
-void pa_random(void *ret_data, size_t length) {
- int fd;
- ssize_t r = 0;
+static int has_whined = 0;
+
+static const char *devices[] = { "/dev/urandom", "/dev/random", NULL };
+
+static int pa_random_proper(void *ret_data, size_t length) {
assert(ret_data && length);
-#ifdef RANDOM_DEVICE
- if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
+#ifdef OS_IS_WIN32
- if ((r = pa_loop_read(fd, ret_data, length)) < 0 || (size_t) r != length)
- pa_log_error(__FILE__": failed to read entropy from '%s'", RANDOM_DEVICE);
+ return -1;
+
+#else /* OS_IS_WIN32 */
+
+ int fd, ret;
+ ssize_t r = 0;
+ const char **device;
- close(fd);
+ device = devices;
+
+ while (*device) {
+ ret = 0;
+
+ if ((fd = open(*device, O_RDONLY)) >= 0) {
+
+ if ((r = pa_loop_read(fd, ret_data, length)) < 0 || (size_t) r != length)
+ ret = -1;
+
+ close(fd);
+ } else
+ ret = -1;
+
+ if (ret == 0)
+ break;
}
-#endif
- if ((size_t) r != length) {
- uint8_t *p;
- size_t l;
+ return ret;
+#endif /* OS_IS_WIN32 */
+}
-#ifdef RANDOM_DEVICE
- pa_log_warn(__FILE__": WARNING: Failed to open entropy device '"RANDOM_DEVICE"': %s"
- ", falling back to unsecure pseudo RNG.\n", strerror(errno));
-#endif
+void pa_random_seed() {
+ unsigned int seed;
+
+ if (pa_random_proper(&seed, sizeof(unsigned int)) < 0) {
+ if (!has_whined)
+ pa_log_warn(__FILE__": failed to get proper entropy. Falling back to seeding with current time.");
+ has_whined = 1;
- for (p = ret_data, l = length; l > 0; p++, l--)
- *p = (uint8_t) rand();
+ seed = (unsigned int) time(NULL);
}
+
+ srand(seed);
+}
+
+void pa_random(void *ret_data, size_t length) {
+ uint8_t *p;
+ size_t l;
+
+ assert(ret_data && length);
+
+ if (pa_random_proper(ret_data, length) >= 0)
+ return;
+
+ if (!has_whined)
+ pa_log_warn(__FILE__": failed to get proper entropy. Falling back to unsecure pseudo RNG.");
+ has_whined = 1;
+
+ for (p = ret_data, l = length; l > 0; p++, l--)
+ *p = (uint8_t) rand();
}
diff --git a/src/polypcore/random.h b/src/polypcore/random.h
index bfb3df08..94a6d505 100644
--- a/src/polypcore/random.h
+++ b/src/polypcore/random.h
@@ -22,6 +22,7 @@
USA.
***/
+void pa_random_seed();
void pa_random(void *ret_data, size_t length);
#endif