From afbed94a6c0cc1454a02568f3daf2c81af9fb7c1 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 11 Sep 2019 09:23:41 +0930 Subject: [PATCH] gossipd: work around missing pwritev(). Signed-off-by: Rusty Russell --- CHANGELOG.md | 1 + gossipd/gossip_store.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44e7874f2..1b1913668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ changes. ### Fixed - Relative `--lightning_dir` is now working again. +- Build: MacOS now builds again (missing pwritev). ### Security diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index 634ccdd89..f8132cd94 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -55,6 +55,38 @@ static void gossip_store_destroy(struct gossip_store *gs) close(gs->fd); } +#if HAVE_PWRITEV +static ssize_t gossip_pwritev(int fd, const struct iovec *iov, int iovcnt, + off_t offset) +{ + return pwritev(fd, iov, iovcnt, offset); +} +#else /* Hello MacOS! */ +static ssize_t gossip_pwritev(int fd, const struct iovec *iov, int iovcnt, + off_t offset) +{ + u8 *buf; + size_t len; + ssize_t ret; + + /* Make a temporary linear buffer to fall back to pwrite() */ + len = 0; + for (size_t i = 0; i < iovcnt; i++) + len += iov[i].iov_len; + + buf = tal_arr(NULL, u8, len); + len = 0; + for (size_t i = 0; i < iovcnt; i++) { + memcpy(buf + len, iov[i].iov_base, iov[i].iov_len); + len += iov[i].iov_len; + } + + ret = pwrite(fd, buf, len, offset); + tal_free(buf); + return ret; +} +#endif /* !HAVE_PWRITEV */ + static bool append_msg(int fd, const u8 *msg, u32 timestamp, u64 *len) { struct gossip_hdr hdr; @@ -71,7 +103,7 @@ static bool append_msg(int fd, const u8 *msg, u32 timestamp, u64 *len) iov[0].iov_len = sizeof(hdr); iov[1].iov_base = (void *)msg; iov[1].iov_len = msglen; - if (pwritev(fd, iov, ARRAY_SIZE(iov), *len) != sizeof(hdr) + msglen) + if (gossip_pwritev(fd, iov, ARRAY_SIZE(iov), *len) != sizeof(hdr) + msglen) return false; *len += sizeof(hdr) + msglen; return true;