diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2018-11-20 09:32:23 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2018-12-07 18:09:27 +0100 |
commit | 8daf03c5640d1e146fe05ebf9c6db2f5704b603d (patch) | |
tree | e06b6d4f5c21700d317e493cfc88d713bf6054a6 | |
parent | 8ece2abfab35da017c58bde04b97fa98f212a1b0 (diff) | |
download | libssh-8daf03c5640d1e146fe05ebf9c6db2f5704b603d.tar.gz libssh-8daf03c5640d1e146fe05ebf9c6db2f5704b603d.tar.xz libssh-8daf03c5640d1e146fe05ebf9c6db2f5704b603d.zip |
session: Check the session timeout and use it if setmaster-fix
This checks if a timeout has been set using ssh_options_set(). If it has
been set it will use that parameter by default for blocking reads.
This is at least what users are expecting.
Fixes T33
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
-rw-r--r-- | src/channels.c | 2 | ||||
-rw-r--r-- | src/session.c | 44 |
2 files changed, 26 insertions, 20 deletions
diff --git a/src/channels.c b/src/channels.c index 8875392a..ab1a1c7d 100644 --- a/src/channels.c +++ b/src/channels.c @@ -2751,7 +2751,7 @@ int ssh_channel_read_timeout(ssh_channel channel, ctx.buffer = stdbuf; ctx.count = 1; - if (timeout_ms < 0) { + if (timeout_ms < SSH_TIMEOUT_DEFAULT) { timeout_ms = SSH_TIMEOUT_INFINITE; } diff --git a/src/session.c b/src/session.c index 9f390f3c..06ed43a7 100644 --- a/src/session.c +++ b/src/session.c @@ -645,11 +645,13 @@ int ssh_handle_packets(ssh_session session, int timeout) { * @param[in] session The session handle to use. * * @param[in] timeout Set an upper limit on the time for which this function - * will block, in milliseconds. Specifying SSH_TIMEOUT_INFINITE - * (-1) means an infinite timeout. + * will block, in milliseconds. Specifying + * SSH_TIMEOUT_INFINITE (-1) means an infinite timeout. * Specifying SSH_TIMEOUT_USER means to use the timeout - * specified in options. 0 means poll will return immediately. - * SSH_TIMEOUT_DEFAULT uses blocking parameters of the session. + * specified in options. 0 means poll will return + * immediately. + * SSH_TIMEOUT_DEFAULT uses the session timeout if set or + * uses blocking parameters of the session. * This parameter is passed to the poll() function. * * @param[in] fct Termination function to be used to determine if it is @@ -663,41 +665,45 @@ int ssh_handle_packets_termination(ssh_session session, void *user) { struct ssh_timestamp ts; + long timeout_ms = SSH_TIMEOUT_INFINITE; /* default timeout */ + long tm; int ret = SSH_OK; - int tm; - if (timeout == SSH_TIMEOUT_USER) { - if (ssh_is_blocking(session)) { - timeout = ssh_make_milliseconds(session->opts.timeout, - session->opts.timeout_usec); - } else { - timeout = SSH_TIMEOUT_NONBLOCKING; - } - } else if (timeout == SSH_TIMEOUT_DEFAULT) { + /* If a timeout has been provided, use it */ + if (timeout > 0) { + timeout_ms = timeout; + } else { if (ssh_is_blocking(session)) { - timeout = SSH_TIMEOUT_INFINITE; + if (timeout == SSH_TIMEOUT_USER || timeout == SSH_TIMEOUT_DEFAULT) { + if (session->opts.timeout > 0 || + session->opts.timeout_usec > 0) { + timeout_ms = + ssh_make_milliseconds(session->opts.timeout, + session->opts.timeout_usec); + } + } } else { - timeout = SSH_TIMEOUT_NONBLOCKING; + timeout_ms = SSH_TIMEOUT_NONBLOCKING; } } /* avoid unnecessary syscall for the SSH_TIMEOUT_NONBLOCKING case */ - if (timeout != SSH_TIMEOUT_NONBLOCKING) { + if (timeout_ms != SSH_TIMEOUT_NONBLOCKING) { ssh_timestamp_init(&ts); } - tm = timeout; + tm = timeout_ms; while(!fct(user)) { ret = ssh_handle_packets(session, tm); if (ret == SSH_ERROR) { break; } - if (ssh_timeout_elapsed(&ts,timeout)) { + if (ssh_timeout_elapsed(&ts, timeout_ms)) { ret = fct(user) ? SSH_OK : SSH_AGAIN; break; } - tm = ssh_timeout_update(&ts, timeout); + tm = ssh_timeout_update(&ts, timeout_ms); } return ret; |