shared/io: Do not return -1
Luiz Augusto von Dentz
maintainerPATCH 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
@@ -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
@@ -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
Luiz Augusto von Dentz
maintainerPATCH 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
@@ -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
@@ -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
@@ -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
Luiz Augusto von Dentz
maintainerPATCH 3/6 shared/att: Make use of io_send
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- src/shared/att.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
src/shared/att.c +9 -6
@@ -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
Luiz Augusto von Dentz
maintainerPATCH 4/6 shared/hci: Make use of io_send
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- src/shared/hci.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
src/shared/hci.c +2 -6
@@ -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
Luiz Augusto von Dentz
maintainerPATCH 5/6 shared/mgmt: Make use of io_send
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- src/shared/mgmt.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
src/shared/mgmt.c +10 -6
@@ -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
Luiz Augusto von Dentz
maintainerPATCH 6/6 shared/uhid: Make use of io_send
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- src/shared/uhid.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
src/shared/uhid.c +4 -3
@@ -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
Luiz Augusto von Dentz
maintainerHi,
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@@ -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@@ -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