All of lore.kernel.org
 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 v3 1/2] plugins: Use open()/read() instead of fopen()/fread() on autopair
Date: Tue, 21 May 2013 09:06:43 -0400	[thread overview]
Message-ID: <1369141604-1129-1-git-send-email-anderson.lizardo@openbossa.org> (raw)
In-Reply-To: <1368721510-14339-1-git-send-email-anderson.lizardo@openbossa.org>

open()/read() is more common on BlueZ code. Incidentally, get rid of
this compilation error (using gcc 4.6.3):

plugins/autopair.c: In function ‘autopair_init’:
plugins/autopair.c:154:8: error: ignoring return value of ‘fread’,
declared with attribute warn_unused_result [-Werror=unused-result]
---
 plugins/autopair.c |   22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

v3:

* Fix buggy logic
* The obvious code path is tested by just starting bluetoothd, which will call
  autopair_init()
* Maybe overkill, but I used this mockup code to test other unreachable code
  paths: http://ix.io/5JC
* I agree with Marcel that we should just fail if /dev/urandom is not readable.
  Otherwise, we are introducing code that will 99% of the time not be run
  (unless someone confirms that we have systems with unusable /dev/urandom).

diff --git a/plugins/autopair.c b/plugins/autopair.c
index 5d90f9d..5aa80df 100644
--- a/plugins/autopair.c
+++ b/plugins/autopair.c
@@ -27,6 +27,8 @@
 
 #include <stdbool.h>
 #include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include <bluetooth/bluetooth.h>
 #include <glib.h>
@@ -146,14 +148,20 @@ static struct btd_adapter_driver autopair_driver = {
 static int autopair_init(void)
 {
 	/* Initialize the random seed from /dev/urandom */
-	unsigned int seed = time(NULL);
-	FILE *f;
+	unsigned int seed;
+	int fd;
 
-	f = fopen("/dev/urandom", "rb");
-	if (f != NULL) {
-		fread(&seed, sizeof(seed), 1, f);
-		fclose(f);
-	}
+	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);
+
+		close(fd);
+	} else
+		seed = time(NULL);
 
 	srand(seed);
 
-- 
1.7.9.5


  parent reply	other threads:[~2013-05-21 13:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-16 16:25 [PATCH BlueZ 1/2] plugins: Use open()/read() instead of fopen()/fread() on autopair Anderson Lizardo
2013-05-16 16:25 ` [PATCH BlueZ 2/2] core: Avoid unnecessary gboolean on pincode callback API Anderson Lizardo
2013-05-17  5:14 ` [PATCH BlueZ 1/2] plugins: Use open()/read() instead of fopen()/fread() on autopair Marcel Holtmann
2013-05-17 11:05   ` Anderson Lizardo
2013-05-17 12:07     ` Luiz Augusto von Dentz
2013-05-17 14:31 ` [PATCH BlueZ v2 " Anderson Lizardo
2013-05-17 14:31   ` [PATCH BlueZ 2/2] core: Avoid unnecessary gboolean on pincode callback API Anderson Lizardo
2013-05-20 23:51   ` [PATCH BlueZ v2 1/2] plugins: Use open()/read() instead of fopen()/fread() on autopair Johan Hedberg
2013-05-21  6:10     ` Marcel Holtmann
2013-05-21 11:02     ` Anderson Lizardo
2013-05-21 13:06 ` Anderson Lizardo [this message]
2013-05-21 13:06   ` [PATCH BlueZ 2/2] core: Avoid unnecessary gboolean on pincode callback API Anderson Lizardo
2013-05-28 13:25   ` [PATCH BlueZ v3 1/2] plugins: Use open()/read() instead of fopen()/fread() on autopair Anderson Lizardo
2013-06-13 14:26     ` 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=1369141604-1129-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.