all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty
@ 2023-11-20  8:16 Maximiliano Sandoval R
  2023-11-20  8:16 ` [pbs-devel] [PATCH proxmox v2 2/2] router: " Maximiliano Sandoval R
  2023-11-27  9:15 ` [pbs-devel] [PATCH proxmox v2 1/2] sys: " Fabian Grünbichler
  0 siblings, 2 replies; 6+ messages in thread
From: Maximiliano Sandoval R @ 2023-11-20  8:16 UTC (permalink / raw)
  To: pbs-devel

Use the `std::io::IsTerminal` trait introduced in Rust 1.70.

Internally it calls `libc::isatty`, see [1, 2]. Note that it switches
the comparison from `== 1` to `!= 0` which shouldn't make a difference
assuming that libc::isatty upholds the promises made in its man page.

The MSRV was set on the workspace to reflect this change.

[1] https://doc.rust-lang.org/src/std/io/stdio.rs.html#1079
[2] https://doc.rust-lang.org/src/std/sys/unix/io.rs.html#79

Signed-off-by: Maximiliano Sandoval R <m.sandoval@proxmox.com>
---
Differences from v1:
  - Rebased
  - Set MSRV to 1.70


 Cargo.toml                   |  1 +
 proxmox-sys/src/linux/tty.rs | 20 +++++++-------------
 2 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index d4d532b..b89d9d7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -42,6 +42,7 @@ license = "AGPL-3"
 repository = "https://git.proxmox.com/?p=proxmox.git"
 homepage = "https://proxmox.com"
 exclude = [ "debian" ]
+rust-version = "1.70"
 
 [workspace.dependencies]
 # any features enabled here are enabled on all members using 'workspace = true'!
diff --git a/proxmox-sys/src/linux/tty.rs b/proxmox-sys/src/linux/tty.rs
index fdea162..375e0ec 100644
--- a/proxmox-sys/src/linux/tty.rs
+++ b/proxmox-sys/src/linux/tty.rs
@@ -1,4 +1,4 @@
-use std::io::{self, Read, Write};
+use std::io::{self, IsTerminal, Read, Write};
 use std::mem::MaybeUninit;
 use std::os::unix::io::{AsRawFd, OwnedFd};
 
@@ -25,20 +25,14 @@ pub fn stdout_terminal_size() -> (usize, usize) {
     (winsize.ws_row as usize, winsize.ws_col as usize)
 }
 
-/// Returns whether the current stdout is a tty .
-/// # Safety
-///
-/// uses unsafe call to libc::isatty
+/// Returns whether the current stdout is a tty.
 pub fn stdout_isatty() -> bool {
-    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
+    std::io::stdout().is_terminal()
 }
 
-/// Returns whether the current stdin is a tty .
-/// # Safety
-///
-/// uses unsafe call to libc::isatty
+/// Returns whether the current stdin is a tty.
 pub fn stdin_isatty() -> bool {
-    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
+    std::io::stdin().is_terminal()
 }
 
 pub enum TtyOutput {
@@ -75,7 +69,7 @@ impl TtyOutput {
     /// Get an output file descriptor for the current terminal.
     pub fn open() -> io::Result<Option<Self>> {
         let stdout = std::io::stdout();
-        if unsafe { libc::isatty(stdout.as_raw_fd()) } == 1 {
+        if stdout.is_terminal() {
             Ok(Some(TtyOutput::Stdout(stdout)))
         } else {
             match crate::fd::open(
@@ -97,7 +91,7 @@ impl TtyOutput {
 /// first.
 pub fn read_password(query: &str) -> Result<Vec<u8>, Error> {
     let input = std::io::stdin();
-    if unsafe { libc::isatty(input.as_raw_fd()) } != 1 {
+    if !input.is_terminal() {
         let mut out = String::new();
         input.read_line(&mut out)?;
         return Ok(out.into_bytes());
-- 
2.39.2





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

* [pbs-devel] [PATCH proxmox v2 2/2] router: Use safe wrapper for libc::isatty
  2023-11-20  8:16 [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty Maximiliano Sandoval R
@ 2023-11-20  8:16 ` Maximiliano Sandoval R
  2023-11-27  9:15 ` [pbs-devel] [PATCH proxmox v2 1/2] sys: " Fabian Grünbichler
  1 sibling, 0 replies; 6+ messages in thread
From: Maximiliano Sandoval R @ 2023-11-20  8:16 UTC (permalink / raw)
  To: pbs-devel

Signed-off-by: Maximiliano Sandoval R <m.sandoval@proxmox.com>
---
 proxmox-router/src/cli/text_table.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/proxmox-router/src/cli/text_table.rs b/proxmox-router/src/cli/text_table.rs
index 9bc7210..3368605 100644
--- a/proxmox-router/src/cli/text_table.rs
+++ b/proxmox-router/src/cli/text_table.rs
@@ -1,4 +1,4 @@
-use std::io::Write;
+use std::io::{IsTerminal, Write};
 
 use anyhow::{bail, Error};
 use serde_json::Value;
@@ -245,7 +245,7 @@ impl TableFormatOptions {
     pub fn new() -> Self {
         let mut me = Self::default();
 
-        let is_tty = unsafe { libc::isatty(libc::STDOUT_FILENO) == 1 };
+        let is_tty = std::io::stdout().is_terminal();
 
         if is_tty {
             let (_rows, columns) = stdout_terminal_size();
-- 
2.39.2





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

* Re: [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty
  2023-11-20  8:16 [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty Maximiliano Sandoval R
  2023-11-20  8:16 ` [pbs-devel] [PATCH proxmox v2 2/2] router: " Maximiliano Sandoval R
@ 2023-11-27  9:15 ` Fabian Grünbichler
  2023-11-27 10:09   ` Maximiliano Sandoval
  2023-11-27 13:15   ` Wolfgang Bumiller
  1 sibling, 2 replies; 6+ messages in thread
From: Fabian Grünbichler @ 2023-11-27  9:15 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion

On November 20, 2023 9:16 am, Maximiliano Sandoval R wrote:
> Use the `std::io::IsTerminal` trait introduced in Rust 1.70.
> 
> Internally it calls `libc::isatty`, see [1, 2]. Note that it switches
> the comparison from `== 1` to `!= 0` which shouldn't make a difference
> assuming that libc::isatty upholds the promises made in its man page.
> 
> The MSRV was set on the workspace to reflect this change.
> 
> [1] https://doc.rust-lang.org/src/std/io/stdio.rs.html#1079
> [2] https://doc.rust-lang.org/src/std/sys/unix/io.rs.html#79
> 
> Signed-off-by: Maximiliano Sandoval R <m.sandoval@proxmox.com>
> ---
> Differences from v1:
>   - Rebased
>   - Set MSRV to 1.70
> 
> 
>  Cargo.toml                   |  1 +
>  proxmox-sys/src/linux/tty.rs | 20 +++++++-------------
>  2 files changed, 8 insertions(+), 13 deletions(-)
> 
> diff --git a/Cargo.toml b/Cargo.toml
> index d4d532b..b89d9d7 100644
> --- a/Cargo.toml
> +++ b/Cargo.toml
> @@ -42,6 +42,7 @@ license = "AGPL-3"
>  repository = "https://git.proxmox.com/?p=proxmox.git"
>  homepage = "https://proxmox.com"
>  exclude = [ "debian" ]
> +rust-version = "1.70"
>  
>  [workspace.dependencies]
>  # any features enabled here are enabled on all members using 'workspace = true'!
> diff --git a/proxmox-sys/src/linux/tty.rs b/proxmox-sys/src/linux/tty.rs
> index fdea162..375e0ec 100644
> --- a/proxmox-sys/src/linux/tty.rs
> +++ b/proxmox-sys/src/linux/tty.rs
> @@ -1,4 +1,4 @@
> -use std::io::{self, Read, Write};
> +use std::io::{self, IsTerminal, Read, Write};
>  use std::mem::MaybeUninit;
>  use std::os::unix::io::{AsRawFd, OwnedFd};
>  
> @@ -25,20 +25,14 @@ pub fn stdout_terminal_size() -> (usize, usize) {
>      (winsize.ws_row as usize, winsize.ws_col as usize)
>  }
>  
> -/// Returns whether the current stdout is a tty .
> -/// # Safety
> -///
> -/// uses unsafe call to libc::isatty
> +/// Returns whether the current stdout is a tty.
>  pub fn stdout_isatty() -> bool {
> -    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
> +    std::io::stdout().is_terminal()
>  }
>  
> -/// Returns whether the current stdin is a tty .
> -/// # Safety
> -///
> -/// uses unsafe call to libc::isatty
> +/// Returns whether the current stdin is a tty.
>  pub fn stdin_isatty() -> bool {
> -    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
> +    std::io::stdin().is_terminal()
>  }

do we still need these two? I assume everything calling this would then
do something with stdin/stdout anyway, and could just as well call
is_terminal() on them? might be a candidate for deprecation, since the
original reason for their existence (pulling out the `unsafe` part into
a helper) is gone..

>  
>  pub enum TtyOutput {
> @@ -75,7 +69,7 @@ impl TtyOutput {
>      /// Get an output file descriptor for the current terminal.
>      pub fn open() -> io::Result<Option<Self>> {
>          let stdout = std::io::stdout();
> -        if unsafe { libc::isatty(stdout.as_raw_fd()) } == 1 {
> +        if stdout.is_terminal() {
>              Ok(Some(TtyOutput::Stdout(stdout)))
>          } else {
>              match crate::fd::open(
> @@ -97,7 +91,7 @@ impl TtyOutput {
>  /// first.
>  pub fn read_password(query: &str) -> Result<Vec<u8>, Error> {
>      let input = std::io::stdin();
> -    if unsafe { libc::isatty(input.as_raw_fd()) } != 1 {
> +    if !input.is_terminal() {
>          let mut out = String::new();
>          input.read_line(&mut out)?;
>          return Ok(out.into_bytes());
> -- 
> 2.39.2
> 
> 
> 
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
> 
> 
> 




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

* Re: [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty
  2023-11-27  9:15 ` [pbs-devel] [PATCH proxmox v2 1/2] sys: " Fabian Grünbichler
@ 2023-11-27 10:09   ` Maximiliano Sandoval
  2023-11-27 13:15   ` Wolfgang Bumiller
  1 sibling, 0 replies; 6+ messages in thread
From: Maximiliano Sandoval @ 2023-11-27 10:09 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion


Fabian Grünbichler <f.gruenbichler@proxmox.com> writes:

> do we still need these two? I assume everything calling this would then
> do something with stdin/stdout anyway, and could just as well call
> is_terminal() on them? might be a candidate for deprecation, since the
> original reason for their existence (pulling out the `unsafe` part into
> a helper) is gone..

I tried sending patches for all possible users of those two functions,
but I might have missed some. I would mark them as deprecated once the
patches are applied.

--
Maximiliano




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

* Re: [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty
  2023-11-27  9:15 ` [pbs-devel] [PATCH proxmox v2 1/2] sys: " Fabian Grünbichler
  2023-11-27 10:09   ` Maximiliano Sandoval
@ 2023-11-27 13:15   ` Wolfgang Bumiller
  1 sibling, 0 replies; 6+ messages in thread
From: Wolfgang Bumiller @ 2023-11-27 13:15 UTC (permalink / raw)
  To: Fabian Grünbichler; +Cc: Proxmox Backup Server development discussion

On Mon, Nov 27, 2023 at 10:15:02AM +0100, Fabian Grünbichler wrote:
> On November 20, 2023 9:16 am, Maximiliano Sandoval R wrote:
> > Use the `std::io::IsTerminal` trait introduced in Rust 1.70.
> > 
> > Internally it calls `libc::isatty`, see [1, 2]. Note that it switches
> > the comparison from `== 1` to `!= 0` which shouldn't make a difference
> > assuming that libc::isatty upholds the promises made in its man page.
> > 
> > The MSRV was set on the workspace to reflect this change.
> > 
> > [1] https://doc.rust-lang.org/src/std/io/stdio.rs.html#1079
> > [2] https://doc.rust-lang.org/src/std/sys/unix/io.rs.html#79
> > 
> > Signed-off-by: Maximiliano Sandoval R <m.sandoval@proxmox.com>
> > ---
> > Differences from v1:
> >   - Rebased
> >   - Set MSRV to 1.70
> > 
> > 
> >  Cargo.toml                   |  1 +
> >  proxmox-sys/src/linux/tty.rs | 20 +++++++-------------
> >  2 files changed, 8 insertions(+), 13 deletions(-)
> > 
> > diff --git a/Cargo.toml b/Cargo.toml
> > index d4d532b..b89d9d7 100644
> > --- a/Cargo.toml
> > +++ b/Cargo.toml
> > @@ -42,6 +42,7 @@ license = "AGPL-3"
> >  repository = "https://git.proxmox.com/?p=proxmox.git"
> >  homepage = "https://proxmox.com"
> >  exclude = [ "debian" ]
> > +rust-version = "1.70"
> >  
> >  [workspace.dependencies]
> >  # any features enabled here are enabled on all members using 'workspace = true'!
> > diff --git a/proxmox-sys/src/linux/tty.rs b/proxmox-sys/src/linux/tty.rs
> > index fdea162..375e0ec 100644
> > --- a/proxmox-sys/src/linux/tty.rs
> > +++ b/proxmox-sys/src/linux/tty.rs
> > @@ -1,4 +1,4 @@
> > -use std::io::{self, Read, Write};
> > +use std::io::{self, IsTerminal, Read, Write};
> >  use std::mem::MaybeUninit;
> >  use std::os::unix::io::{AsRawFd, OwnedFd};
> >  
> > @@ -25,20 +25,14 @@ pub fn stdout_terminal_size() -> (usize, usize) {
> >      (winsize.ws_row as usize, winsize.ws_col as usize)
> >  }
> >  
> > -/// Returns whether the current stdout is a tty .
> > -/// # Safety
> > -///
> > -/// uses unsafe call to libc::isatty
> > +/// Returns whether the current stdout is a tty.
> >  pub fn stdout_isatty() -> bool {
> > -    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
> > +    std::io::stdout().is_terminal()
> >  }
> >  
> > -/// Returns whether the current stdin is a tty .
> > -/// # Safety
> > -///
> > -/// uses unsafe call to libc::isatty
> > +/// Returns whether the current stdin is a tty.
> >  pub fn stdin_isatty() -> bool {
> > -    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
> > +    std::io::stdin().is_terminal()
> >  }
> 
> do we still need these two? I assume everything calling this would then
> do something with stdin/stdout anyway, and could just as well call
> is_terminal() on them? might be a candidate for deprecation, since the
> original reason for their existence (pulling out the `unsafe` part into
> a helper) is gone..

Is this actually the case code-wise, though? Because generally speaking
this could just be to check whether we should use table-output or colors
etc (though we don't have colors... but anyway, we might just want to
know what stdout is and then still use println!() ...)?
But I don't mind deprecating them on the grounds of them being quite
trivial now.




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

* [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty
@ 2024-02-16 14:59 Maximiliano Sandoval
  0 siblings, 0 replies; 6+ messages in thread
From: Maximiliano Sandoval @ 2024-02-16 14:59 UTC (permalink / raw)
  To: pbs-devel

From: Maximiliano Sandoval R <m.sandoval@proxmox.com>

Use the `std::io::IsTerminal` trait introduced in Rust 1.70.

Internally it calls `libc::isatty`, see [1, 2]. Note that it switches
the comparison from `== 1` to `!= 0` which shouldn't make a difference
assuming that libc::isatty upholds the promises made in its man page.

The MSRV was set on the workspace to reflect this change.

[1] https://doc.rust-lang.org/src/std/io/stdio.rs.html#1079
[2] https://doc.rust-lang.org/src/std/sys/unix/io.rs.html#79

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
Differences from v1:
 - Mark old fns as depreacted

 Cargo.toml                   |  1 +
 proxmox-sys/src/linux/tty.rs | 22 +++++++++-------------
 2 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index de79f7ca..58d5a67e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -44,6 +44,7 @@ license = "AGPL-3"
 repository = "https://git.proxmox.com/?p=proxmox.git"
 homepage = "https://proxmox.com"
 exclude = [ "debian" ]
+rust-version = "1.70"
 
 [workspace.dependencies]
 # any features enabled here are enabled on all members using 'workspace = true'!
diff --git a/proxmox-sys/src/linux/tty.rs b/proxmox-sys/src/linux/tty.rs
index fdea1629..9a1e4679 100644
--- a/proxmox-sys/src/linux/tty.rs
+++ b/proxmox-sys/src/linux/tty.rs
@@ -1,4 +1,4 @@
-use std::io::{self, Read, Write};
+use std::io::{self, IsTerminal, Read, Write};
 use std::mem::MaybeUninit;
 use std::os::unix::io::{AsRawFd, OwnedFd};
 
@@ -25,20 +25,16 @@ pub fn stdout_terminal_size() -> (usize, usize) {
     (winsize.ws_row as usize, winsize.ws_col as usize)
 }
 
-/// Returns whether the current stdout is a tty .
-/// # Safety
-///
-/// uses unsafe call to libc::isatty
+/// Returns whether the current stdout is a tty.
+#[deprecated(note = "Use std::io::stdout().is_terminal()")]
 pub fn stdout_isatty() -> bool {
-    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
+    std::io::stdout().is_terminal()
 }
 
-/// Returns whether the current stdin is a tty .
-/// # Safety
-///
-/// uses unsafe call to libc::isatty
+/// Returns whether the current stdin is a tty.
+#[deprecated(note = "Use std::io::stdin().is_terminal()")]
 pub fn stdin_isatty() -> bool {
-    unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
+    std::io::stdin().is_terminal()
 }
 
 pub enum TtyOutput {
@@ -75,7 +71,7 @@ impl TtyOutput {
     /// Get an output file descriptor for the current terminal.
     pub fn open() -> io::Result<Option<Self>> {
         let stdout = std::io::stdout();
-        if unsafe { libc::isatty(stdout.as_raw_fd()) } == 1 {
+        if stdout.is_terminal() {
             Ok(Some(TtyOutput::Stdout(stdout)))
         } else {
             match crate::fd::open(
@@ -97,7 +93,7 @@ impl TtyOutput {
 /// first.
 pub fn read_password(query: &str) -> Result<Vec<u8>, Error> {
     let input = std::io::stdin();
-    if unsafe { libc::isatty(input.as_raw_fd()) } != 1 {
+    if !input.is_terminal() {
         let mut out = String::new();
         input.read_line(&mut out)?;
         return Ok(out.into_bytes());
-- 
2.39.2





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

end of thread, other threads:[~2024-02-16 14:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-20  8:16 [pbs-devel] [PATCH proxmox v2 1/2] sys: Use safe wrapper for libc::isatty Maximiliano Sandoval R
2023-11-20  8:16 ` [pbs-devel] [PATCH proxmox v2 2/2] router: " Maximiliano Sandoval R
2023-11-27  9:15 ` [pbs-devel] [PATCH proxmox v2 1/2] sys: " Fabian Grünbichler
2023-11-27 10:09   ` Maximiliano Sandoval
2023-11-27 13:15   ` Wolfgang Bumiller
2024-02-16 14:59 Maximiliano Sandoval

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal