Linux bluetooth development
 help / color / mirror / Atom feed
From: Anderson Lizardo <anderson.lizardo@openbossa.org>
To: linux-bluetooth@vger.kernel.org
Cc: Anderson Lizardo <anderson.lizardo@openbossa.org>
Subject: [PATCH BlueZ] autopair: Remove time(NULL) fallback when seeding rand()
Date: Mon,  7 Oct 2013 17:05:24 -0400	[thread overview]
Message-ID: <1381179924-19529-1-git-send-email-anderson.lizardo@openbossa.org> (raw)

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.
---
 plugins/autopair.c |   27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/plugins/autopair.c b/plugins/autopair.c
index e6e5035..266b695 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>
@@ -149,19 +150,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 %d bytes from /dev/urandom",
+								sizeof(seed));
 		close(fd);
-	} else
-		seed = time(NULL);
+		return err;
+	}
+
+	close(fd);
 
 	srand(seed);
 
-- 
1.7.9.5


             reply	other threads:[~2013-10-07 21:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-07 21:05 Anderson Lizardo [this message]
2014-03-05 16:55 ` [PATCH BlueZ] autopair: Remove time(NULL) fallback when seeding rand() Johan Hedberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1381179924-19529-1-git-send-email-anderson.lizardo@openbossa.org \
    --to=anderson.lizardo@openbossa.org \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox