From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 693E1639D6 for ; Wed, 26 Jan 2022 19:11:04 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 664FC1B04E for ; Wed, 26 Jan 2022 19:11:04 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 9D40D1B040 for ; Wed, 26 Jan 2022 19:11:02 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 70D1846105 for ; Wed, 26 Jan 2022 19:11:02 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Date: Wed, 26 Jan 2022 19:10:54 +0100 Message-Id: <20220126181055.968385-1-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.057 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [main.rs] Subject: [pve-devel] [PATCH 1/2] use anyhow for result/error X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Jan 2022 18:11:04 -0000 it's in out dependency chain anyway through proxmox-sys or -time and it makes life a bit easier. FWIW, I got slightly pressured into this by the future switch from curl to ureq, as the latter brings their own Error type. Signed-off-by: Thomas Lamprecht --- Not a must, we can handle the ureq::Error otherwise, but as we already depend on anyhow indirectly I figured to go for it... Cargo.toml | 1 + src/main.rs | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60c3ae0..25c4569 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ license = "AGPL-3" exclude = [ "build", "debian" ] [dependencies] +anyhow = "1" mio = { version = "0.7", features = [ "net", "os-ext" ] } curl = "0.4" clap = "2.33" diff --git a/src/main.rs b/src/main.rs index 7896a9c..0003834 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ use std::cmp::min; use std::collections::HashMap; use std::ffi::{OsStr, OsString}; -use std::io::{ErrorKind, Result, Write}; +use std::io::{ErrorKind, Write}; use std::os::unix::io::{AsRawFd, FromRawFd}; use std::os::unix::process::CommandExt; use std::process::Command; use std::time::{Duration, Instant}; +use anyhow::{bail, format_err, Result}; use clap::{App, AppSettings, Arg}; use curl::easy::Easy; use mio::net::{TcpListener, TcpStream}; @@ -16,7 +17,6 @@ use mio::{Events, Interest, Poll, Token}; use proxmox_io::ByteBuffer; use proxmox_sys::{ error::io_err_other, - io_bail, io_format_err, linux::pty::{make_controlling_terminal, PTY}, }; @@ -108,11 +108,11 @@ fn read_ticket_line( match buf.read_from(stream) { Ok(n) => { if n == 0 { - io_bail!("connection closed before authentication"); + bail!("connection closed before authentication"); } } Err(err) if err.kind() == ErrorKind::WouldBlock => {} - Err(err) => return Err(err), + Err(err) => return Err(err.into()), } if buf[..].contains(&b'\n') { @@ -120,13 +120,13 @@ fn read_ticket_line( } if buf.is_full() { - io_bail!("authentication data is incomplete: {:?}", &buf[..]); + bail!("authentication data is incomplete: {:?}", &buf[..]); } } elapsed = now.elapsed(); if elapsed > timeout { - io_bail!("timed out"); + bail!("timed out"); } } @@ -140,7 +140,7 @@ fn read_ticket_line( let (username, ticket) = line.split_at(pos); Ok((username.into(), ticket[1..].into())) } - None => io_bail!("authentication data is invalid"), + None => bail!("authentication data is invalid"), } } @@ -183,7 +183,7 @@ fn authenticate( let response_code = curl.response_code()?; if response_code != 200 { - io_bail!("invalid authentication, code {}", response_code); + bail!("invalid authentication, code {}", response_code); } Ok(()) @@ -222,7 +222,7 @@ fn listen_and_accept( elapsed = now.elapsed(); if elapsed > timeout { - io_bail!("timed out"); + bail!("timed out"); } } } @@ -302,17 +302,17 @@ fn do_main() -> Result<()> { let use_port_as_fd = matches.is_present("use-port-as-fd"); if use_port_as_fd && port > u16::MAX as u64 { - return Err(io_format_err!("port too big")); + return Err(format_err!("port too big")); } else if port > i32::MAX as u64 { - return Err(io_format_err!("Invalid FD number")); + return Err(format_err!("Invalid FD number")); } 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))?; + .map_err(|err| format_err!("failed waiting for client: {}", err))?; 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))?; + .map_err(|err| format_err!("failed reading ticket: {}", err))?; let port = if use_port_as_fd { Some(port) } else { None }; authenticate(&username, &ticket, path, perm, authport, port)?; tcp_handle.write_all(b"OK").expect("error writing response"); @@ -383,7 +383,7 @@ fn do_main() -> Result<()> { } Err(err) => { if !finished { - return Err(io_format_err!("error reading from tcp: {}", err)); + return Err(format_err!("error reading from tcp: {}", err)); } break; } @@ -403,7 +403,7 @@ fn do_main() -> Result<()> { } Err(err) => { if !finished { - return Err(io_format_err!("error reading from pty: {}", err)); + return Err(format_err!("error reading from pty: {}", err)); } break; } @@ -423,7 +423,7 @@ fn do_main() -> Result<()> { } Err(err) => { if !finished { - return Err(io_format_err!("error writing to tcp : {}", err)); + return Err(format_err!("error writing to tcp : {}", err)); } break; } @@ -447,7 +447,7 @@ fn do_main() -> Result<()> { } Err(err) => { if !finished { - return Err(io_format_err!("error writing to pty : {}", err)); + return Err(format_err!("error writing to pty : {}", err)); } break; } -- 2.34.1