From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2 5/5] meshctl: Switch from write to sendmsg for Acquire*
Date: Tue, 20 Nov 2018 12:39:59 +0200 [thread overview]
Message-ID: <20181120103959.23502-5-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20181120103959.23502-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Use sendmsg with MSG_NOSIGNAL to prevent crashes involving SIGPIPE.
---
tools/mesh/gatt.c | 64 +++++++++++++++++++++++++++++++++--------------
1 file changed, 45 insertions(+), 19 deletions(-)
diff --git a/tools/mesh/gatt.c b/tools/mesh/gatt.c
index 2c29c09b5..2269a20cf 100644
--- a/tools/mesh/gatt.c
+++ b/tools/mesh/gatt.c
@@ -166,7 +166,25 @@ uint16_t mesh_gatt_sar(uint8_t **pkt, uint16_t size)
}
}
-static bool pipe_write(struct io *io, void *user_data)
+static int sock_send(struct io *io, struct iovec *iov, size_t iovlen)
+{
+ struct msghdr msg;
+ int ret;
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_iov = iov;
+ msg.msg_iovlen = iovlen;
+
+ ret = sendmsg(io_get_fd(io), &msg, MSG_NOSIGNAL);
+ if (ret < 0) {
+ ret = -errno;
+ bt_shell_printf("sendmsg: %s", strerror(-ret));
+ }
+
+ return ret;
+}
+
+static bool sock_write(struct io *io, void *user_data)
{
struct write_data *data = user_data;
struct iovec iov[2];
@@ -200,9 +218,8 @@ static bool pipe_write(struct io *io, void *user_data)
iov[1] = data->iov;
- err = io_send(io, iov, 2);
+ err = sock_send(io, iov, 2);
if (err < 0) {
- bt_shell_printf("Failed to write: %s\n", strerror(-err));
write_data_free(data);
return false;
}
@@ -247,7 +264,7 @@ static void notify_io_destroy(void)
notify_mtu = 0;
}
-static bool pipe_hup(struct io *io, void *user_data)
+static bool sock_hup(struct io *io, void *user_data)
{
bt_shell_printf("%s closed\n", io == notify_io ? "Notify" : "Write");
@@ -259,7 +276,7 @@ static bool pipe_hup(struct io *io, void *user_data)
return false;
}
-static struct io *pipe_io_new(int fd)
+static struct io *sock_io_new(int fd)
{
struct io *io;
@@ -267,7 +284,7 @@ static struct io *pipe_io_new(int fd)
io_set_close_on_destroy(io, true);
- io_set_disconnect_handler(io, pipe_hup, NULL, NULL);
+ io_set_disconnect_handler(io, sock_hup, NULL, NULL);
return io;
}
@@ -296,9 +313,9 @@ static void acquire_write_reply(DBusMessage *message, void *user_data)
bt_shell_printf("AcquireWrite success: fd %d MTU %u\n", fd, write_mtu);
- write_io = pipe_io_new(fd);
+ write_io = sock_io_new(fd);
- pipe_write(write_io, data);
+ sock_write(write_io, data);
}
static void acquire_setup(DBusMessageIter *iter, void *user_data)
@@ -342,7 +359,7 @@ bool mesh_gatt_write(GDBusProxy *proxy, uint8_t *buf, uint16_t len,
data->cb = cb;
if (write_io)
- return pipe_write(write_io, data);
+ return sock_write(write_io, data);
if (g_dbus_proxy_method_call(proxy, "AcquireWrite",
acquire_setup, acquire_write_reply,
@@ -377,9 +394,11 @@ done:
g_free(data);
}
-static bool pipe_read(struct io *io, bool prov, void *user_data)
+static bool sock_read(struct io *io, bool prov, void *user_data)
{
struct mesh_node *node = user_data;
+ struct msghdr msg;
+ struct iovec iov;
uint8_t buf[512];
uint8_t *res;
int fd = io_get_fd(io);
@@ -388,9 +407,16 @@ static bool pipe_read(struct io *io, bool prov, void *user_data)
if (io != notify_io)
return true;
- while ((len = read(fd, buf, sizeof(buf)))) {
+ iov.iov_base = buf;
+ iov.iov_len = sizeof(buf);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+
+ while ((len = recvmsg(fd, &msg, MSG_DONTWAIT))) {
if (len <= 0)
- break;
+ return false;
res = buf;
len_sar = mesh_gatt_sar(&res, len);
@@ -404,14 +430,14 @@ static bool pipe_read(struct io *io, bool prov, void *user_data)
return true;
}
-static bool pipe_read_prov(struct io *io, void *user_data)
+static bool sock_read_prov(struct io *io, void *user_data)
{
- return pipe_read(io, true, user_data);
+ return sock_read(io, true, user_data);
}
-static bool pipe_read_proxy(struct io *io, void *user_data)
+static bool sock_read_proxy(struct io *io, void *user_data)
{
- return pipe_read(io, false, user_data);
+ return sock_read(io, false, user_data);
}
static void acquire_notify_reply(DBusMessage *message, void *user_data)
@@ -457,15 +483,15 @@ static void acquire_notify_reply(DBusMessage *message, void *user_data)
if (g_dbus_proxy_get_property(data->proxy, "UUID", &iter) == FALSE)
goto done;
- notify_io = pipe_io_new(fd);
+ notify_io = sock_io_new(fd);
dbus_message_iter_get_basic(&iter, &uuid);
if (!bt_uuid_strcmp(uuid, MESH_PROV_DATA_OUT_UUID_STR))
- io_set_read_handler(notify_io, pipe_read_prov, data->user_data,
+ io_set_read_handler(notify_io, sock_read_prov, data->user_data,
NULL);
else if (!bt_uuid_strcmp(uuid, MESH_PROXY_DATA_OUT_UUID_STR))
- io_set_read_handler(notify_io, pipe_read_proxy, data->user_data,
+ io_set_read_handler(notify_io, sock_read_proxy, data->user_data,
NULL);
done:
--
2.17.2
prev parent reply other threads:[~2018-11-20 10:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-20 10:39 [PATCH v2 1/5] gatt: Fix invalid read when disconnecting Luiz Augusto von Dentz
2018-11-20 10:39 ` [PATCH v2 2/5] doc/gatt-api: Restrict supported file descriptors Luiz Augusto von Dentz
2018-11-20 10:39 ` [PATCH v2 3/5] gatt: Switch from pipe2 to sockepair for Acquire* Luiz Augusto von Dentz
2018-11-20 10:39 ` [PATCH v2 4/5] client: Switch from write to sendmsg " Luiz Augusto von Dentz
2018-11-20 10:39 ` Luiz Augusto von Dentz [this message]
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=20181120103959.23502-5-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--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;
as well as URLs for NNTP newsgroup(s).