From 5aa8663c98dd9dfa9d206e34dd569a98d94f2997 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 29 Dec 2021 13:56:43 +1030 Subject: [PATCH] common/socket_close.c: remove obsolete comment. Put more inline comments, and also preserve errno properly from read. Suggested-by: @cdecker Signed-off-by: Rusty Russell --- common/socket_close.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/common/socket_close.c b/common/socket_close.c index 7d19ec768..5abaf80d3 100644 --- a/common/socket_close.c +++ b/common/socket_close.c @@ -7,20 +7,7 @@ #include #include -/* -Simplified (minus all the error checks): - - shutdown(fd, SHUT_WR); - for (;;) { - char unused[64] - sys_res = read(fd, unused, 64); - if (sys_res == 0) - break; - } - close(fd); -*/ - -/* makes read() return EINTR */ +/* makes read() return EINTR after 5 seconds */ static void break_read(int signum) { } @@ -29,8 +16,9 @@ bool socket_close(int fd) { char unused[64]; struct sigaction act, old_act; - int sys_res; + int sys_res, saved_errno; + /* We shutdown. Usually they then shutdown too, and read() gives 0 */ sys_res = shutdown(fd, SHUT_WR); if (sys_res < 0) { close_noerr(fd); @@ -45,12 +33,14 @@ bool socket_close(int fd) alarm(5); while ((sys_res = read(fd, unused, sizeof(unused))) > 0); + saved_errno = errno; alarm(0); sigaction(SIGALRM, &old_act, NULL); if (sys_res < 0) { - close_noerr(fd); + close(fd); + errno = saved_errno; return false; }