Skip to contentSkip to conversation

shared/io: Do not return -1

PATCH In review Luiz Augusto von Dentz 1 participant 7 messages Sep 29, 2014 – Sep 30, 2014 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/6 shared/io: Do not return -1

From: Luiz Augusto von Dentz <[email protected]>

Returning -1 normally is used when errno is set which is not case so instead return -ENOTCONN.

---
 src/shared/io-glib.c     | 4 +++-
 src/shared/io-mainloop.c | 3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)
src/shared/io-glib.c +3 -1
index 6316037..882ebd6 100644
@@ -25,6 +25,8 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
+
 #include <glib.h>
 
 #include "src/shared/io.h"
@@ -122,7 +124,7 @@ void io_destroy(struct io *io)
 int io_get_fd(struct io *io)
 {
 	if (!io)
-		return -1;
+		return -ENOTCONN;
 
 	return g_io_channel_unix_get_fd(io->channel);
 }
src/shared/io-mainloop.c +2 -1
index 1563ce5..b7e0f5f 100644
@@ -26,6 +26,7 @@
 #endif
 
 #include <unistd.h>
+#include <errno.h>
 #include <sys/socket.h>
 
 #include "monitor/mainloop.h"
@@ -192,7 +193,7 @@ void io_destroy(struct io *io)
 int io_get_fd(struct io *io)
 {
 	if (!io)
-		return -1;
+		return -ENOTCONN;
 
 	return io->fd;
 }
Signature · 1 line
1.9.3
author maintainer
#1 lore raw

PATCH 2/6 shared/io: Add io_send

From: Luiz Augusto von Dentz <[email protected]>

---
 src/shared/io-glib.c     | 20 ++++++++++++++++++++
 src/shared/io-mainloop.c | 17 +++++++++++++++++
 src/shared/io.h          |  2 ++
 3 files changed, 39 insertions(+)
src/shared/io-glib.c +20 -0
index 882ebd6..a2ada66 100644
@@ -326,6 +326,26 @@ done:
 	return true;
 }
 
+ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
+{
+	int fd;
+	ssize_t ret;
+
+	if (!io || !io->channel)
+		return -ENOTCONN;
+
+	fd = io_get_fd(io);
+
+	do {
+		ret = writev(fd, iov, iovcnt);
+	} while (ret < 0 && errno == EINTR);
+
+	if (ret < 0)
+		return -errno;
+
+	return ret;
+}
+
 bool io_shutdown(struct io *io)
 {
 	if (!io || !io->channel)
src/shared/io-mainloop.c +17 -0
index b7e0f5f..7f80d8f 100644
@@ -301,6 +301,23 @@ bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback,
 	return true;
 }
 
+ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
+{
+	ssize_t ret;
+
+	if (!io || io->fd < 0)
+		return -ENOTCONN;
+
+	do {
+		ret = writev(io->fd, iov, iovcnt);
+	} while (ret < 0 && errno == EINTR);
+
+	if (ret < 0)
+		return -errno;
+
+	return ret;
+}
+
 bool io_shutdown(struct io *io)
 {
 	if (!io || io->fd < 0)
src/shared/io.h +2 -0
index 8897964..8bc1111 100644
@@ -22,6 +22,7 @@
  */
 
 #include <stdbool.h>
+#include <sys/uio.h>
 
 typedef void (*io_destroy_func_t)(void *data);
 
@@ -33,6 +34,7 @@ void io_destroy(struct io *io);
 int io_get_fd(struct io *io);
 bool io_set_close_on_destroy(struct io *io, bool do_close);
 
+ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt);
 bool io_shutdown(struct io *io);
 
 typedef bool (*io_callback_func_t)(struct io *io, void *user_data);
Signature · 1 line
1.9.3
author maintainer
#2 lore raw

PATCH 3/6 shared/att: Make use of io_send

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 src/shared/att.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
src/shared/att.c +9 -6
index a5cab45..de35aef 100644
@@ -422,16 +422,20 @@ static bool can_write_data(struct io *io, void *user_data)
 	struct bt_att *att = user_data;
 	struct att_send_op *op;
 	struct timeout_data *timeout;
-	ssize_t bytes_written;
+	ssize_t ret;
+	struct iovec iov;
 
 	op = pick_next_send_op(att);
 	if (!op)
 		return false;
 
-	bytes_written = write(att->fd, op->pdu, op->len);
-	if (bytes_written < 0) {
+	iov.iov_base = op->pdu;
+	iov.iov_len = op->len;
+
+	ret = io_send(io, &iov, 1);
+	if (ret < 0) {
 		util_debug(att->debug_callback, att->debug_data,
-					"write failed: %s", strerror(errno));
+					"write failed: %s", strerror(-ret));
 		if (op->callback)
 			op->callback(BT_ATT_OP_ERROR_RSP, NULL, 0,
 							op->user_data);
@@ -443,8 +447,7 @@ static bool can_write_data(struct io *io, void *user_data)
 	util_debug(att->debug_callback, att->debug_data,
 					"ATT op 0x%02x", op->opcode);
 
-	util_hexdump('<', op->pdu, bytes_written,
-					att->debug_callback, att->debug_data);
+	util_hexdump('<', op->pdu, ret, att->debug_callback, att->debug_data);
 
 	/* Based on the operation type, set either the pending request or the
 	 * pending indication. If it came from the write queue, then there is
Signature · 1 line
1.9.3
author maintainer
#3 lore raw

PATCH 4/6 shared/hci: Make use of io_send

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 src/shared/hci.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
src/shared/hci.c +2 -6
index 6ce62eb..9ab8c6e 100644
@@ -113,7 +113,7 @@ static void send_command(struct bt_hci *hci, uint16_t opcode,
 	uint8_t type = BT_H4_CMD_PKT;
 	struct bt_hci_cmd_hdr hdr;
 	struct iovec iov[3];
-	int fd, iovcnt;
+	int iovcnt;
 
 	if (hci->num_cmds < 1)
 		return;
@@ -133,11 +133,7 @@ static void send_command(struct bt_hci *hci, uint16_t opcode,
 	} else
 		iovcnt = 2;
 
-	fd = io_get_fd(hci->io);
-	if (fd < 0)
-		return;
-
-	if (writev(fd, iov, iovcnt) < 0)
+	if (io_send(hci->io, iov, iovcnt) < 0)
 		return;
 
 	hci->num_cmds--;
Signature · 1 line
1.9.3
author maintainer
#4 lore raw

PATCH 5/6 shared/mgmt: Make use of io_send

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 src/shared/mgmt.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
src/shared/mgmt.c +10 -6
index ae90b89..5dae7a9 100644
@@ -162,7 +162,8 @@ static bool can_write_data(struct io *io, void *user_data)
 {
 	struct mgmt *mgmt = user_data;
 	struct mgmt_request *request;
-	ssize_t bytes_written;
+	struct iovec iov;
+	ssize_t ret;
 
 	request = queue_pop_head(mgmt->reply_queue);
 	if (!request) {
@@ -175,10 +176,13 @@ static bool can_write_data(struct io *io, void *user_data)
 			return false;
 	}
 
-	bytes_written = write(mgmt->fd, request->buf, request->len);
-	if (bytes_written < 0) {
+	iov.iov_base = request->buf;
+	iov.iov_len = request->len;
+
+	ret = io_send(io, &iov, 1);
+	if (ret < 0) {
 		util_debug(mgmt->debug_callback, mgmt->debug_data,
-				"write failed: %s", strerror(errno));
+				"write failed: %s", strerror(-ret));
 		if (request->callback)
 			request->callback(MGMT_STATUS_FAILED, 0, NULL,
 							request->user_data);
@@ -190,8 +194,8 @@ static bool can_write_data(struct io *io, void *user_data)
 				"[0x%04x] command 0x%04x",
 				request->index, request->opcode);
 
-	util_hexdump('<', request->buf, bytes_written,
-				mgmt->debug_callback, mgmt->debug_data);
+	util_hexdump('<', request->buf, ret, mgmt->debug_callback,
+							mgmt->debug_data);
 
 	queue_push_tail(mgmt->pending_list, request);
 
Signature · 1 line
1.9.3
author maintainer
#5 lore raw

PATCH 6/6 shared/uhid: Make use of io_send

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 src/shared/uhid.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
src/shared/uhid.c +4 -3
index 28c1792..f7ad0cb 100644
@@ -228,15 +228,16 @@ bool bt_uhid_unregister(struct bt_uhid *uhid, unsigned int id)
 
 int bt_uhid_send(struct bt_uhid *uhid, const struct uhid_event *ev)
 {
-	int fd;
 	ssize_t len;
+	struct iovec iov;
 
 	if (!uhid->io)
 		return -ENOTCONN;
 
-	fd = io_get_fd(uhid->io);
+	iov.iov_base = (void *) ev;
+	iov.iov_len = sizeof(*ev);
 
-	len = write(fd, ev, sizeof(*ev));
+	len = io_send(uhid->io, &iov, 1);
 	if (len < 0)
 		return -errno;
 
Signature · 1 line
1.9.3
author maintainer
#6 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 53 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Returning -1 normally is used when errno is set which is not case so instead return -ENOTCONN.

---
 src/shared/io-glib.c     | 4 +++-
 src/shared/io-mainloop.c | 3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)
src/shared/io-glib.c +3 -1
index 6316037..882ebd6 100644
@@ -25,6 +25,8 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
+
 #include <glib.h>
 
 #include "src/shared/io.h"
@@ -122,7 +124,7 @@ void io_destroy(struct io *io)
 int io_get_fd(struct io *io)
 {
        if (!io)
-               return -1;
+               return -ENOTCONN;
 
        return g_io_channel_unix_get_fd(io->channel);
 }
src/shared/io-mainloop.c +2 -2
index 1563ce5..b7e0f5f 100644
@@ -26,6 +26,7 @@
 #endif
 
 #include <unistd.h>
+#include <errno.h>
 #include <sys/socket.h>
 
 #include "monitor/mainloop.h"
@@ -192,7 +193,7 @@ void io_destroy(struct io *io)
 int io_get_fd(struct io *io)
 {
        if (!io)
-               return -1;
+               return -ENOTCONN;
 
        return io->fd;
 }
--
Quoted 1 line

1.9.3

Pushed.

Signature · 1 line
Luiz Augusto von Dentz