public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0)
@ 2021-02-01  7:55 Dominik Csapak
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 1/3] Cargo.toml: update mio to 0.7 and proxmox to 0.10 Dominik Csapak
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Dominik Csapak @ 2021-02-01  7:55 UTC (permalink / raw)
  To: pbs-devel

some small changes, nothing too heavy

Dominik Csapak (3):
  Cargo.toml: update mio to 0.7 and proxmox to 0.10
  update to mio 0.7
  termproxy: rewrite read_ticket_line

 Cargo.toml  |  4 +--
 src/main.rs | 91 ++++++++++++++++++++++++++++++-----------------------
 2 files changed, 54 insertions(+), 41 deletions(-)

-- 
2.20.1





^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pbs-devel] [PATCH xtermjs 1/3] Cargo.toml: update mio to 0.7 and proxmox to 0.10
  2021-02-01  7:55 [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Dominik Csapak
@ 2021-02-01  7:55 ` Dominik Csapak
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 2/3] update to mio 0.7 Dominik Csapak
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Dominik Csapak @ 2021-02-01  7:55 UTC (permalink / raw)
  To: pbs-devel

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 Cargo.toml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index baaa075..8b28453 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,7 +11,7 @@ license = "AGPL-3"
 exclude = [ "build", "debian" ]
 
 [dependencies]
-mio = "0.6"
+mio = { version = "0.7", features = [ "net", "os-ext" ] }
 curl = "0.4"
 clap = "2.33"
-proxmox = { version = "0.8.0", default-features = false }
+proxmox = { version = "0.10.0", default-features = false }
-- 
2.20.1





^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pbs-devel] [PATCH xtermjs 2/3] update to mio 0.7
  2021-02-01  7:55 [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Dominik Csapak
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 1/3] Cargo.toml: update mio to 0.7 and proxmox to 0.10 Dominik Csapak
@ 2021-02-01  7:55 ` Dominik Csapak
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 3/3] termproxy: rewrite read_ticket_line Dominik Csapak
  2021-02-02 12:10 ` [pbs-devel] applied-series: [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Fabian Grünbichler
  3 siblings, 0 replies; 5+ messages in thread
From: Dominik Csapak @ 2021-02-01  7:55 UTC (permalink / raw)
  To: pbs-devel

notable changes:
* all polls in mio are now edge triggered, so no need for PollOpt, etc.
* functionality of UnixReady is now in the mio::Event directly
  (UnixReady::is_hup -> event.is_read_closed())
* TcpListener::from_std() is not a result anymore
* EventedFd -> SourceFd
* mio::net::TcpListener cannot accept_std anymore, so convert
  everywhere to mio::net::TcpStream, drop set_read_timeout
  (this warrants another patch)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/main.rs | 48 ++++++++++++++++++++----------------------------
 1 file changed, 20 insertions(+), 28 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 12e70bc..07fa1a8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,7 +2,6 @@ use std::cmp::min;
 use std::collections::HashMap;
 use std::ffi::{OsStr, OsString};
 use std::io::{ErrorKind, Result, Write};
-use std::net::TcpStream;
 use std::os::unix::io::{AsRawFd, FromRawFd};
 use std::os::unix::process::CommandExt;
 use std::process::Command;
@@ -10,9 +9,9 @@ use std::time::{Duration, Instant};
 
 use clap::{App, AppSettings, Arg};
 use curl::easy::Easy;
-use mio::net::TcpListener;
-use mio::unix::{EventedFd, UnixReady};
-use mio::{Events, Poll, PollOpt, Ready, Token};
+use mio::net::{TcpListener, TcpStream};
+use mio::{Events, Poll, Token, Interest};
+use mio::unix::SourceFd;
 
 use proxmox::sys::error::io_err_other;
 use proxmox::sys::linux::pty::{make_controlling_terminal, PTY};
@@ -98,7 +97,6 @@ fn read_ticket_line(
         if buf.is_full() || now.elapsed() >= timeout {
             io_bail!("authentication data is incomplete: {:?}", &buf[..]);
         }
-        stream.set_read_timeout(Some(Duration::new(1, 0)))?;
         match buf.read_from(stream) {
             Ok(n) => {
                 if n == 0 {
@@ -110,7 +108,6 @@ fn read_ticket_line(
         }
     }
 
-    stream.set_read_timeout(None)?;
     let newline_idx = &buf[..].iter().position(|&x| x == b'\n').unwrap();
 
     let line = buf.remove_data(*newline_idx);
@@ -182,10 +179,10 @@ fn listen_and_accept(
         std::net::TcpListener::bind((hostname, port as u16))?
     };
     let port = listener.local_addr()?.port();
-    let listener = TcpListener::from_std(listener)?;
-    let poll = Poll::new()?;
+    let mut listener = TcpListener::from_std(listener);
+    let mut poll = Poll::new()?;
 
-    poll.register(&listener, Token(0), Ready::readable(), PollOpt::edge())?;
+    poll.registry().register(&mut listener, Token(0), Interest::READABLE)?;
 
     let mut events = Events::with_capacity(1);
 
@@ -195,7 +192,7 @@ fn listen_and_accept(
         poll.poll(&mut events, Some(timeout))?;
         let elapsed = now.elapsed();
         if !events.is_empty() {
-            let (stream, client) = listener.accept_std()?;
+            let (stream, client) = listener.accept()?;
             println!("client connection: {:?}", client);
             return Ok((stream, port));
         }
@@ -287,34 +284,30 @@ fn do_main() -> Result<()> {
         return Err(io_format_err!("Invalid FD number"));
     }
 
-    let (mut stream, port) =
+    let (mut tcp_handle, port) =
         listen_and_accept("localhost", port, use_port_as_fd, Duration::new(10, 0))
             .map_err(|err| io_format_err!("failed waiting for client: {}", err))?;
 
-    let (username, ticket) = read_ticket_line(&mut stream, &mut pty_buf, Duration::new(10, 0))
+    let (username, ticket) = read_ticket_line(&mut tcp_handle, &mut pty_buf, Duration::new(10, 0))
         .map_err(|err| io_format_err!("failed reading ticket: {}", err))?;
     let port = if use_port_as_fd { Some(port) } else { None };
     authenticate(&username, &ticket, path, perm, authport, port)?;
-    stream.write_all(b"OK").expect("error writing response");
+    tcp_handle.write_all(b"OK").expect("error writing response");
 
-    let mut tcp_handle = mio::net::TcpStream::from_stream(stream)?;
-
-    let poll = Poll::new()?;
+    let mut poll = Poll::new()?;
     let mut events = Events::with_capacity(128);
 
     let mut pty = run_pty(cmd, cmdparams)?;
 
-    poll.register(
-        &tcp_handle,
+    poll.registry().register(
+        &mut tcp_handle,
         TCP,
-        Ready::readable() | Ready::writable() | UnixReady::hup(),
-        PollOpt::edge(),
+        Interest::READABLE.add(Interest::WRITABLE)
     )?;
-    poll.register(
-        &EventedFd(&pty.as_raw_fd()),
+    poll.registry().register(
+        &mut SourceFd(&pty.as_raw_fd()),
         PTY,
-        Ready::readable() | Ready::writable() | UnixReady::hup(),
-        PollOpt::edge(),
+        Interest::READABLE.add(Interest::WRITABLE)
     )?;
 
     let mut tcp_writable = true;
@@ -332,10 +325,9 @@ fn do_main() -> Result<()> {
         }
 
         for event in &events {
-            let readiness = event.readiness();
-            let writable = readiness.is_writable();
-            let readable = readiness.is_readable();
-            if UnixReady::from(readiness).is_hup() {
+            let writable = event.is_writable();
+            let readable = event.is_readable();
+            if event.is_read_closed() {
                 finished = true;
             }
             match event.token() {
-- 
2.20.1





^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pbs-devel] [PATCH xtermjs 3/3] termproxy: rewrite read_ticket_line
  2021-02-01  7:55 [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Dominik Csapak
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 1/3] Cargo.toml: update mio to 0.7 and proxmox to 0.10 Dominik Csapak
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 2/3] update to mio 0.7 Dominik Csapak
@ 2021-02-01  7:55 ` Dominik Csapak
  2021-02-02 12:10 ` [pbs-devel] applied-series: [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Fabian Grünbichler
  3 siblings, 0 replies; 5+ messages in thread
From: Dominik Csapak @ 2021-02-01  7:55 UTC (permalink / raw)
  To: pbs-devel

since we cannot accept a std TcpStream from a mio::net::TcpListener
anymore, we cannot use set_read_timeout here

instead implement the readloop as a mio poll loop similar
to listen_and_accept, otherwise termproxy will busy loop and
consume 100% of a single core during authentication

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/main.rs | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 07fa1a8..21bd066 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -92,19 +92,40 @@ fn read_ticket_line(
     buf: &mut ByteBuffer,
     timeout: Duration,
 ) -> TicketResult {
-    let now = Instant::now();
-    while !&buf[..].contains(&b'\n') {
-        if buf.is_full() || now.elapsed() >= timeout {
-            io_bail!("authentication data is incomplete: {:?}", &buf[..]);
-        }
-        match buf.read_from(stream) {
-            Ok(n) => {
-                if n == 0 {
-                    io_bail!("connection closed before authentication");
+
+    let mut poll = Poll::new()?;
+    poll.registry().register(stream, Token(0), Interest::READABLE)?;
+    let mut events = Events::with_capacity(1);
+    let mut timeout = timeout;
+
+    loop {
+        let now = Instant::now();
+        poll.poll(&mut events, Some(timeout))?;
+        let elapsed = now.elapsed();
+        if !events.is_empty() {
+            match buf.read_from(stream) {
+                Ok(n) => {
+                    if n == 0 {
+                        io_bail!("connection closed before authentication");
+                    }
                 }
+                Err(err) if err.kind() == ErrorKind::WouldBlock => {}
+                Err(err) => return Err(err),
             }
-            Err(err) if err.kind() == ErrorKind::WouldBlock => {}
-            Err(err) => return Err(err),
+
+            if buf[..].contains(&b'\n') {
+                break;
+            }
+
+            if buf.is_full() {
+                io_bail!("authentication data is incomplete: {:?}", &buf[..]);
+            }
+        }
+
+        if timeout >= elapsed {
+            timeout -= elapsed;
+        } else {
+            io_bail!("timed out");
         }
     }
 
-- 
2.20.1





^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pbs-devel] applied-series: [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0)
  2021-02-01  7:55 [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Dominik Csapak
                   ` (2 preceding siblings ...)
  2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 3/3] termproxy: rewrite read_ticket_line Dominik Csapak
@ 2021-02-02 12:10 ` Fabian Grünbichler
  3 siblings, 0 replies; 5+ messages in thread
From: Fabian Grünbichler @ 2021-02-02 12:10 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion

with a small followup regrading timeout handling for accept and auth, as 
well as a little style/formatting.

On February 1, 2021 8:55 am, Dominik Csapak wrote:
> some small changes, nothing too heavy
> 
> Dominik Csapak (3):
>   Cargo.toml: update mio to 0.7 and proxmox to 0.10
>   update to mio 0.7
>   termproxy: rewrite read_ticket_line
> 
>  Cargo.toml  |  4 +--
>  src/main.rs | 91 ++++++++++++++++++++++++++++++-----------------------
>  2 files changed, 54 insertions(+), 41 deletions(-)
> 
> -- 
> 2.20.1
> 
> 
> 
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
> 
> 
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-02-02 12:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-01  7:55 [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Dominik Csapak
2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 1/3] Cargo.toml: update mio to 0.7 and proxmox to 0.10 Dominik Csapak
2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 2/3] update to mio 0.7 Dominik Csapak
2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 3/3] termproxy: rewrite read_ticket_line Dominik Csapak
2021-02-02 12:10 ` [pbs-devel] applied-series: [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Fabian Grünbichler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal