Files
palladum-lightning/common/dev_disconnect.c
Rusty Russell 6e5cb299dd global: remove unnecessary includes from C files.
Basically, `devtools/reduce-includes.sh */*.c`.

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s
After:
	real    0m37.872000-39.974000(39.5466+/-0.59)s
	user    3m1.211000-14.968000(12.4556+/-3.9)s
	sys     0m35.008000-36.830000(36.4143+/-0.5)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

After:
	real    0m18.384000-21.307000(20.8605+/-0.92)s
	user    2m5.585000-26.843000(23.6017+/-6.7)s
	sys     0m19.650000-22.003000(21.4943+/-0.69)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30

132 lines
3.2 KiB
C

#include "config.h"
#include <ccan/err/err.h>
#include <common/dev_disconnect.h>
#include <common/status.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <wire/peer_wire.h>
/* We move the fd if and only if we do a disconnect. */
static int dev_disconnect_fd = -1;
static char dev_disconnect_line[200];
static int dev_disconnect_count, dev_disconnect_len;
static void next_dev_disconnect(void)
{
int r;
char *asterisk;
r = read(dev_disconnect_fd,
dev_disconnect_line, sizeof(dev_disconnect_line)-1);
if (r < 0)
err(1, "Reading dev_disconnect file");
if (lseek(dev_disconnect_fd, -r, SEEK_CUR) < 0) {
err(1, "lseek failure");
}
/* Get first line */
dev_disconnect_line[r] = '\n';
dev_disconnect_len = strcspn(dev_disconnect_line, "\n");
dev_disconnect_line[dev_disconnect_len] = '\0';
asterisk = strchr(dev_disconnect_line, '*');
if (asterisk) {
dev_disconnect_count = atoi(asterisk+1);
if (dev_disconnect_count < 1)
errx(1, "dev_disconnect invalid count: %s",
dev_disconnect_line);
*asterisk = '\0';
} else
dev_disconnect_count = 1;
}
void dev_disconnect_init(int fd)
{
/* So we can move forward if we do use the line. */
dev_disconnect_fd = fd;
}
enum dev_disconnect_out dev_disconnect_out(const struct node_id *id, int pkt_type)
{
if (dev_disconnect_fd == -1)
return DEV_DISCONNECT_OUT_NORMAL;
if (!dev_disconnect_count)
next_dev_disconnect();
if (!dev_disconnect_line[0]
|| dev_disconnect_line[0] == DEV_DISCONNECT_IN_AFTER_RECV
|| !streq(peer_wire_name(pkt_type), dev_disconnect_line+1))
return DEV_DISCONNECT_OUT_NORMAL;
if (--dev_disconnect_count != 0) {
return DEV_DISCONNECT_OUT_NORMAL;
}
if (lseek(dev_disconnect_fd, dev_disconnect_len+1, SEEK_CUR) < 0) {
err(1, "lseek failure");
}
status_peer_debug(id, "dev_disconnect: %s (%s)",
dev_disconnect_line,
peer_wire_name(pkt_type));
return dev_disconnect_line[0];
}
enum dev_disconnect_in dev_disconnect_in(const struct node_id *id, int pkt_type)
{
if (dev_disconnect_fd == -1)
return DEV_DISCONNECT_IN_NORMAL;
if (!dev_disconnect_count)
next_dev_disconnect();
if (dev_disconnect_line[0] != DEV_DISCONNECT_IN_AFTER_RECV
|| !streq(peer_wire_name(pkt_type), dev_disconnect_line+1))
return DEV_DISCONNECT_IN_NORMAL;
if (--dev_disconnect_count != 0) {
return DEV_DISCONNECT_IN_NORMAL;
}
if (lseek(dev_disconnect_fd, dev_disconnect_len+1, SEEK_CUR) < 0) {
err(1, "lseek failure");
}
status_peer_debug(id, "dev_disconnect: %s (%s)",
dev_disconnect_line,
peer_wire_name(pkt_type));
return dev_disconnect_line[0];
}
void dev_sabotage_fd(int fd, bool close_fd)
{
int fds[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0)
err(1, "dev_sabotage_fd: creating socketpair");
#if defined(TCP_NODELAY)
/* On Linux, at least, this flushes. */
int opt = TCP_NODELAY;
int val = 1;
setsockopt(fd, IPPROTO_TCP, opt, &val, sizeof(val));
#else
#error No TCP_NODELAY?
#endif
/* Move fd out the way if we don't want to close it. */
if (!close_fd) {
if (dup(fd) == -1) {
; /* -Wunused-result */
}
} else
/* Close other end of socket. */
close(fds[0]);
/* Move other over to the fd we want to sabotage. */
dup2(fds[1], fd);
close(fds[1]);
}