[c5c522c] | 1 | From: Aron Xu <aron@debian.org> |
---|
| 2 | Date: Mon, 13 Feb 2012 15:08:33 +0800 |
---|
| 3 | Subject: poll hup |
---|
| 4 | |
---|
| 5 | --- |
---|
| 6 | netcat.c | 24 +++++++++++++++++------- |
---|
| 7 | 1 file changed, 17 insertions(+), 7 deletions(-) |
---|
| 8 | |
---|
| 9 | diff --git a/netcat.c b/netcat.c |
---|
| 10 | index d912544..fdaca44 100644 |
---|
| 11 | --- a/netcat.c |
---|
| 12 | +++ b/netcat.c |
---|
| 13 | @@ -884,9 +884,7 @@ readwrite(int nfd) |
---|
| 14 | if ((n = read(nfd, buf, plen)) < 0) |
---|
| 15 | return; |
---|
| 16 | else if (n == 0) { |
---|
| 17 | - shutdown(nfd, SHUT_RD); |
---|
| 18 | - pfd[0].fd = -1; |
---|
| 19 | - pfd[0].events = 0; |
---|
| 20 | + goto shutdown_rd; |
---|
| 21 | } else { |
---|
| 22 | if (tflag) |
---|
| 23 | atelnet(nfd, buf, n); |
---|
| 24 | @@ -894,18 +892,30 @@ readwrite(int nfd) |
---|
| 25 | return; |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | + else if (pfd[0].revents & POLLHUP) { |
---|
| 29 | + shutdown_rd: |
---|
| 30 | + shutdown(nfd, SHUT_RD); |
---|
| 31 | + pfd[0].fd = -1; |
---|
| 32 | + pfd[0].events = 0; |
---|
| 33 | + } |
---|
| 34 | |
---|
| 35 | - if (!dflag && pfd[1].revents & POLLIN) { |
---|
| 36 | + if (!dflag) { |
---|
| 37 | + if(pfd[1].revents & POLLIN) { |
---|
| 38 | if ((n = read(wfd, buf, plen)) < 0) |
---|
| 39 | return; |
---|
| 40 | else if (n == 0) { |
---|
| 41 | - shutdown(nfd, SHUT_WR); |
---|
| 42 | - pfd[1].fd = -1; |
---|
| 43 | - pfd[1].events = 0; |
---|
| 44 | + goto shutdown_wr; |
---|
| 45 | } else { |
---|
| 46 | if (atomicio(vwrite, nfd, buf, n) != n) |
---|
| 47 | return; |
---|
| 48 | } |
---|
| 49 | + } |
---|
| 50 | + else if (pfd[1].revents & POLLHUP) { |
---|
| 51 | + shutdown_wr: |
---|
| 52 | + shutdown(nfd, SHUT_WR); |
---|
| 53 | + pfd[1].fd = -1; |
---|
| 54 | + pfd[1].events = 0; |
---|
| 55 | + } |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | -- |
---|