mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
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:
parent
35ec22b946
commit
80886cda8a
@ -35,21 +35,30 @@ struct io_plan *daemon_conn_write_next(struct io_conn *conn,
|
|||||||
bool daemon_conn_sync_flush(struct daemon_conn *dc)
|
bool daemon_conn_sync_flush(struct daemon_conn *dc)
|
||||||
{
|
{
|
||||||
const u8 *msg;
|
const u8 *msg;
|
||||||
|
int daemon_fd;
|
||||||
|
|
||||||
/* Flush any current packet. */
|
/* Flush any current packet. */
|
||||||
if (!io_flush_sync(dc->conn))
|
if (!io_flush_sync(dc->conn))
|
||||||
return false;
|
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. */
|
/* Flush existing messages. */
|
||||||
while ((msg = msg_dequeue(&dc->out)) != NULL) {
|
while ((msg = msg_dequeue(&dc->out)) != NULL) {
|
||||||
int fd = msg_extract_fd(msg);
|
int fd = msg_extract_fd(msg);
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
if (!fdpass_send(io_conn_fd(dc->conn), fd))
|
if (!fdpass_send(daemon_fd, fd))
|
||||||
return false;
|
break;
|
||||||
} else if (!wire_sync_write(io_conn_fd(dc->conn), take(msg)))
|
} else if (!wire_sync_write(daemon_fd, take(msg)))
|
||||||
return false;
|
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,
|
static struct io_plan *daemon_conn_start(struct io_conn *conn,
|
||||||
|
@ -41,18 +41,6 @@ static void connection_destroy(struct connection *c)
|
|||||||
command_fail(c->cmd, "Failed to connect to peer");
|
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
|
static void
|
||||||
PRINTF_FMT(3,4) connection_failed(struct connection *c, struct log *log,
|
PRINTF_FMT(3,4) connection_failed(struct connection *c, struct log *log,
|
||||||
const char *fmt, ...)
|
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));
|
fatal("Could not read fd from HSM: %s", strerror(errno));
|
||||||
|
|
||||||
/* Make sure connection fd is blocking */
|
/* Make sure connection fd is blocking */
|
||||||
set_blocking(connfd, true);
|
io_fd_block(connfd, true);
|
||||||
|
|
||||||
/* Give handshake daemon the hsm fd. */
|
/* Give handshake daemon the hsm fd. */
|
||||||
handshaked = new_subd(ld, ld,
|
handshaked = new_subd(ld, ld,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <ccan/io/io.h>
|
||||||
#include <ccan/tal/str/str.h>
|
#include <ccan/tal/str/str.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <lightningd/crypto_sync.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);
|
msg = towire_error(errmsg, channel_id, (const u8 *)errmsg);
|
||||||
|
|
||||||
/* This is only best-effort; don't block. */
|
/* 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));
|
sync_crypto_write(cs, peer_fd, take(msg));
|
||||||
|
|
||||||
status_failed(error_code, "%s", errmsg);
|
status_failed(error_code, "%s", errmsg);
|
||||||
|
@ -27,19 +27,6 @@ static bool move_fd(int from, int to)
|
|||||||
return true;
|
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 subd_req {
|
||||||
struct list_node list;
|
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. */
|
/* Don't trust subd to set it blocking. */
|
||||||
for (i = 0; i < tal_count(sd->fds_in); i++)
|
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 sd_msg_read(conn, sd);
|
||||||
}
|
}
|
||||||
return io_recv_fd(conn, &sd->fds_in[sd->num_fds_in_read++],
|
return io_recv_fd(conn, &sd->fds_in[sd->num_fds_in_read++],
|
||||||
|
Loading…
Reference in New Issue
Block a user