summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorAnderson Lizardo <anderson.lizardo@openbossa.org>2013-10-07 17:05:24 -0400
committerJohan Hedberg <johan.hedberg@intel.com>2014-03-05 22:31:16 +0200
commitef80103f95410619ed233d8f0f6462358c3d91af (patch)
tree0c566270b3af86d4d6d8c7ad0a807eb79977f8dd /plugins
parenta1d5393e933870336ff3f41640d9b3f24abbce60 (diff)
downloadbluez-ef80103f95410619ed233d8f0f6462358c3d91af.tar.gz
autopair: Remove time(NULL) fallback when seeding rand()
If /dev/urandom cannot be opened or read, just fail the plugin initialization, as it is very unlikely that a fully working Linux system does not have a working /dev/urandom. This also simplifies the code logic.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/autopair.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/plugins/autopair.c b/plugins/autopair.c
index c01d81c45..d63da93ac 100644
--- a/plugins/autopair.c
+++ b/plugins/autopair.c
@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
+#include <errno.h>
#include <bluetooth/bluetooth.h>
#include <glib.h>
@@ -157,19 +158,27 @@ static int autopair_init(void)
{
/* Initialize the random seed from /dev/urandom */
unsigned int seed;
- int fd;
+ int fd, err;
+ ssize_t n;
fd = open("/dev/urandom", O_RDONLY);
- if (fd >= 0) {
- ssize_t n;
-
- n = read(fd, &seed, sizeof(seed));
- if (n < (ssize_t) sizeof(seed))
- seed = time(NULL);
+ if (fd < 0) {
+ err = -errno;
+ error("Failed to open /dev/urandom: %s (%d)", strerror(-err),
+ -err);
+ return err;
+ }
+ n = read(fd, &seed, sizeof(seed));
+ if (n < (ssize_t) sizeof(seed)) {
+ err = (n == -1) ? -errno : -EIO;
+ error("Failed to read %zu bytes from /dev/urandom",
+ sizeof(seed));
close(fd);
- } else
- seed = time(NULL);
+ return err;
+ }
+
+ close(fd);
srand(seed);