All of lore.kernel.org
 help / color / mirror / Atom feed
From: Szymon Janc <szymon.janc@tieto.com>
To: <linux-bluetooth@vger.kernel.org>
Cc: Anderson Lizardo <anderson.lizardo@openbossa.org>,
	Szymon Janc <szymon.janc@tieto.com>
Subject: [RFC v3] adaptername: Refactor handle_inotify_cb
Date: Fri, 31 Aug 2012 16:29:43 +0200	[thread overview]
Message-ID: <1346423383-27319-1-git-send-email-szymon.janc@tieto.com> (raw)
In-Reply-To: <1346416811-23484-13-git-send-email-szymon.janc@tieto.com>

Refactor handle_inotify_cb to avoid unaligned memory access.
Instead of reading whole events buffer and cast to event struct when
iterating over it, read data directly to struct inotify_event one
event at time.

This fix following build error on ARM.

  CC     plugins/bluetoothd-adaptername.o
plugins/adaptername.c: In function handle_inotify_cb:
plugins/adaptername.c:244:34: error: cast increases required alignment
	of target type [-Werror=cast-align]
cc1: all warnings being treated as errors
make[1]: *** [plugins/bluetoothd-adaptername.o] Error 1
make: *** [all] Error 2

---
 plugins/adaptername.c |   48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/plugins/adaptername.c b/plugins/adaptername.c
index d3341b5..588c8db 100644
--- a/plugins/adaptername.c
+++ b/plugins/adaptername.c
@@ -45,8 +45,6 @@
 #include "log.h"
 
 #include <sys/inotify.h>
-#define EVENT_SIZE  (sizeof (struct inotify_event))
-#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
 
 #define MACHINE_INFO_DIR "/etc/"
 #define MACHINE_INFO_FILE "machine-info"
@@ -226,38 +224,40 @@ static int adaptername_probe(struct btd_adapter *adapter)
 static gboolean handle_inotify_cb(GIOChannel *channel, GIOCondition cond,
 								gpointer data)
 {
-	char buf[EVENT_BUF_LEN];
+	struct inotify_event event;
+	gsize len;
 	GIOStatus err;
-	gsize len, i;
-	gboolean changed;
+	char name[FILENAME_MAX + 1];
 
-	changed = FALSE;
+	while ((err = g_io_channel_read_chars(channel, (gchar *)&event,
+			sizeof(event), &len, NULL)) != G_IO_STATUS_AGAIN) {
+		if (err != G_IO_STATUS_NORMAL || len != sizeof(event) ||
+				event.len > sizeof(name))
+			goto fail;
 
-	err = g_io_channel_read_chars(channel, buf, EVENT_BUF_LEN, &len, NULL);
-	if (err != G_IO_STATUS_NORMAL) {
-		error("Error reading inotify event: %d\n", err);
-		return FALSE;
-	}
+		if (event.len == 0)
+			continue;
 
-	i = 0;
-	while (i < len) {
-		struct inotify_event *pevent = (struct inotify_event *) &buf[i];
+		err = g_io_channel_read_chars(channel, name, event.len, &len,
+									NULL);
 
-		/* check that it's ours */
-		if (pevent->len && pevent->name != NULL &&
-				strcmp(pevent->name, MACHINE_INFO_FILE) == 0)
-			changed = TRUE;
+		if (err != G_IO_STATUS_NORMAL || len != event.len)
+			goto fail;
 
-		i += EVENT_SIZE + pevent->len;
-	}
+		if (strncmp(name, MACHINE_INFO_FILE, event.len) == 0) {
+			DBG(MACHINE_INFO_DIR MACHINE_INFO_FILE
+					" changed, updating adapters' names");
 
-	if (changed != FALSE) {
-		DBG(MACHINE_INFO_DIR MACHINE_INFO_FILE
-				" changed, changing names for adapters");
-		manager_foreach_adapter((adapter_cb) adaptername_probe, NULL);
+			manager_foreach_adapter((adapter_cb) adaptername_probe,
+									NULL);
+			break;
+		}
 	}
 
 	return TRUE;
+fail:
+	error("Error reading inotify event");
+	return FALSE;
 }
 
 static void adaptername_remove(struct btd_adapter *adapter)
-- 
1.7.9.5


  parent reply	other threads:[~2012-08-31 14:29 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-31 12:39 [RFC v2 00/15] Unaligned memory access fixes Szymon Janc
2012-08-31 12:39 ` [RFC v2 01/15] lib: Add host order unaligned access functions Szymon Janc
2012-08-31 12:39 ` [RFC v2 02/15] sap-u8500: Fix compile error due to unaligned memory access Szymon Janc
2012-08-31 12:39 ` [RFC v2 03/15] sdp: Use helper functions instead of bt_get_unaligned macro Szymon Janc
2012-08-31 12:40 ` [RFC v2 04/15] Add helper functions for putting integers on unaligned memory address Szymon Janc
2012-08-31 12:40 ` [RFC v2 05/15] sdp: Fix compilation errors due to unaligned memory access Szymon Janc
2012-08-31 12:40 ` [RFC v2 06/15] l2test: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 07/15] rctest: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 08/15] monitor: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 09/15] scotest: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 10/15] avrcp: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 11/15] sap: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 12/15] adaptername: Refactor handle_inotify_cb Szymon Janc
2012-08-31 13:19   ` Anderson Lizardo
2012-08-31 14:25     ` Szymon Janc
2012-08-31 14:29   ` Szymon Janc [this message]
2012-08-31 12:40 ` [RFC v2 13/15] sdpd-request: Fix build errors due to unaligned memory access Szymon Janc
2012-08-31 12:40 ` [RFC v2 14/15] sdpd-service: " Szymon Janc
2012-08-31 12:40 ` [RFC v2 15/15] hciemu: " Szymon Janc
2012-08-31 15:19   ` Anderson Lizardo
2012-09-03  6:42     ` Szymon Janc

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=1346423383-27319-1-git-send-email-szymon.janc@tieto.com \
    --to=szymon.janc@tieto.com \
    --cc=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.