[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Polling POLLRDHUP events?


On Thursday, January 10, 2019 6:12:36 PM CET Ludovic Courtès wrote:
> Hello,

Hi Ludovic,
 
> Apparently libssh only ever polls for POLLIN events; on GNU/Linux, I
> think it would be useful to poll for POLLIN | POLLRDHUP, which would
> allow connection shutdowns to be caught, as opposed to sitting in a
> poll(2) call.  I believe the change would be along these lines:

does the attached patch look fine for you?


Thanks,


	Andreas


-- 
Andreas Schneider                 asn@xxxxxxxxxxxxxx
GPG-ID:     8DFF53E18F2ABC8D8F3C92237EE0FC4DCC014E3D
From 6a06baf393b631986165644cfe13067025fcc940 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@xxxxxxxxxxxxxx>
Date: Mon, 14 Jan 2019 11:26:29 +0100
Subject: [PATCH 1/2] bind: Reformat ssh_bind_get_poll()

Signed-off-by: Andreas Schneider <asn@xxxxxxxxxxxxxx>
---
 src/bind.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/bind.c b/src/bind.c
index 63ef3a94..a91b3ad8 100644
--- a/src/bind.c
+++ b/src/bind.c
@@ -343,12 +343,17 @@ static int ssh_bind_poll_callback(ssh_poll_handle sshpoll,
  * @param sshbind the ssh_bind object
  * @returns a ssh_poll handle suitable for operation
  */
-ssh_poll_handle ssh_bind_get_poll(ssh_bind sshbind){
-  if(sshbind->poll)
+ssh_poll_handle ssh_bind_get_poll(ssh_bind sshbind)
+{
+    if (sshbind->poll) {
+        return sshbind->poll;
+    }
+    sshbind->poll = ssh_poll_new(sshbind->bindfd,
+                                 POLLIN,
+                                 ssh_bind_poll_callback,
+                                 sshbind);
+
     return sshbind->poll;
-  sshbind->poll=ssh_poll_new(sshbind->bindfd,POLLIN,
-      ssh_bind_poll_callback,sshbind);
-  return sshbind->poll;
 }
 
 void ssh_bind_set_blocking(ssh_bind sshbind, int blocking) {
-- 
2.20.1


From fd184aaf8b111841d8674237cc19a584ef1741f5 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@xxxxxxxxxxxxxx>
Date: Mon, 14 Jan 2019 11:32:28 +0100
Subject: [PATCH 2/2] bind: Check for POLLRDHUP on the server if available
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a feature on modern Linux.

Thanks to Ludovic Courtès <ludo@xxxxxxx> for the pointer.

Signed-off-by: Andreas Schneider <asn@xxxxxxxxxxxxxx>
---
 src/CMakeLists.txt | 10 ++++++++--
 src/bind.c         |  9 ++++++++-
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 21e94488..d48ff258 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -300,7 +300,10 @@ if (WITH_SYMBOL_VERSIONING AND HAVE_LD_VERSION_SCRIPT AND ABIMAP_FOUND)
 endif (WITH_SYMBOL_VERSIONING AND HAVE_LD_VERSION_SCRIPT AND ABIMAP_FOUND)
 
 add_library(${LIBSSH_SHARED_LIBRARY} SHARED ${libssh_SRCS})
-target_compile_options(${LIBSSH_SHARED_LIBRARY} PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
+target_compile_options(${LIBSSH_SHARED_LIBRARY}
+                       PRIVATE
+                           ${DEFAULT_C_COMPILE_FLAGS}
+                           -D_GNU_SOURCE)
 
 target_link_libraries(${LIBSSH_SHARED_LIBRARY} ${LIBSSH_LINK_LIBRARIES})
 
@@ -348,7 +351,10 @@ install(
 
 if (BUILD_STATIC_LIB)
   add_library(${LIBSSH_STATIC_LIBRARY} STATIC ${libssh_SRCS})
-  target_compile_options(${LIBSSH_STATIC_LIBRARY} PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
+  target_compile_options(${LIBSSH_STATIC_LIBRARY}
+                         PRIVATE
+                            ${DEFAULT_C_COMPILE_FLAGS}
+                            -D_GNU_SOURCE)
 
   if (MSVC)
     set(OUTPUT_SUFFIX static)
diff --git a/src/bind.c b/src/bind.c
index a91b3ad8..4e28f774 100644
--- a/src/bind.c
+++ b/src/bind.c
@@ -345,11 +345,18 @@ static int ssh_bind_poll_callback(ssh_poll_handle sshpoll,
  */
 ssh_poll_handle ssh_bind_get_poll(ssh_bind sshbind)
 {
+    short events = POLLIN;
+
     if (sshbind->poll) {
         return sshbind->poll;
     }
+
+#ifdef POLLRDHUP
+    events |= POLLRDHUP;
+#endif /* POLLRDHUP */
+
     sshbind->poll = ssh_poll_new(sshbind->bindfd,
-                                 POLLIN,
+                                 events,
                                  ssh_bind_poll_callback,
                                  sshbind);
 
-- 
2.20.1


Follow-Ups:
Re: Polling POLLRDHUP events?Ludovic Courtès <ludo@xxxxxxx>
References:
Polling POLLRDHUP events?Ludovic Courtès <ludo@xxxxxxx>
Archive administrator: postmaster@lists.cynapses.org