daemon_conn: fix daemon_conn_sync_flush.

We need to set fd to blocking before trying to sync write.  Use
io_fd_block() elsewhere, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-07-04 10:17:02 +09:30
parent 35ec22b946
commit 80886cda8a
4 changed files with 18 additions and 33 deletions

View File

@ -35,21 +35,30 @@ struct io_plan *daemon_conn_write_next(struct io_conn *conn,
bool daemon_conn_sync_flush(struct daemon_conn *dc)
{
const u8 *msg;
int daemon_fd;
/* Flush any current packet. */
if (!io_flush_sync(dc->conn))
return false;
/* Make fd blocking for the duration */
daemon_fd = io_conn_fd(dc->conn);
if (!io_fd_block(daemon_fd, true))
return false;
/* Flush existing messages. */
while ((msg = msg_dequeue(&dc->out)) != NULL) {
int fd = msg_extract_fd(msg);
if (fd >= 0) {
if (!fdpass_send(io_conn_fd(dc->conn), fd))
return false;
} else if (!wire_sync_write(io_conn_fd(dc->conn), take(msg)))
return false;
if (!fdpass_send(daemon_fd, fd))
break;
} else if (!wire_sync_write(daemon_fd, take(msg)))
break;
}
return true;
io_fd_block(daemon_fd, false);
/* Success iff we flushed them all. */
return msg == NULL;
}
static struct io_plan *daemon_conn_start(struct io_conn *conn,

View File

@ -41,18 +41,6 @@ static void connection_destroy(struct connection *c)
command_fail(c->cmd, "Failed to connect to peer");
}
static void set_blocking(int fd, bool block)
{
int flags = fcntl(fd, F_GETFL);
if (block)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
}
static void
PRINTF_FMT(3,4) connection_failed(struct connection *c, struct log *log,
const char *fmt, ...)
@ -202,7 +190,7 @@ static struct io_plan *hsm_then_handshake(struct io_conn *conn,
fatal("Could not read fd from HSM: %s", strerror(errno));
/* Make sure connection fd is blocking */
set_blocking(connfd, true);
io_fd_block(connfd, true);
/* Give handshake daemon the hsm fd. */
handshaked = new_subd(ld, ld,

View File

@ -1,3 +1,4 @@
#include <ccan/io/io.h>
#include <ccan/tal/str/str.h>
#include <fcntl.h>
#include <lightningd/crypto_sync.h>
@ -35,7 +36,7 @@ void peer_failed(int peer_fd, struct crypto_state *cs,
msg = towire_error(errmsg, channel_id, (const u8 *)errmsg);
/* This is only best-effort; don't block. */
fcntl(peer_fd, F_SETFL, fcntl(peer_fd, F_GETFL) | O_NONBLOCK);
io_fd_block(peer_fd, false);
sync_crypto_write(cs, peer_fd, take(msg));
status_failed(error_code, "%s", errmsg);

View File

@ -27,19 +27,6 @@ static bool move_fd(int from, int to)
return true;
}
/* FIXME: Expose the ccan/io version? */
static void set_blocking(int fd, bool block)
{
int flags = fcntl(fd, F_GETFL);
if (block)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
}
struct subd_req {
struct list_node list;
@ -281,7 +268,7 @@ static struct io_plan *read_fds(struct io_conn *conn, struct subd *sd)
/* Don't trust subd to set it blocking. */
for (i = 0; i < tal_count(sd->fds_in); i++)
set_blocking(sd->fds_in[i], true);
io_fd_block(sd->fds_in[i], true);
return sd_msg_read(conn, sd);
}
return io_recv_fd(conn, &sd->fds_in[sd->num_fds_in_read++],