Skip to content

Conversation

@NiltonVolpato
Copy link

This adds EINVAL to the error codes that trigger fallback from TCGETS2/TCSETS2 to the legacy TCGETS/TCSETS ioctls.

Motivation

Some emulated and virtualized environments don't implement TCGETS2/TCSETS2 and return EINVAL rather than ENOTTY or EACCES. This includes CheerpX/WebVM (x86-to-WebAssembly virtualization in the browser).

The fallback logic already exists for ENOTTY and EACCES. This change simply adds EINVAL as another trigger.

Reproducing

use rustix::pty::*;
use rustix::termios::*;

fn main() {
    let pty = openpt(OpenptFlags::empty()).expect("openpt failed");
    println!("tcgetattr: {:?}", tcgetattr(&pty));
}

cargo build --target i686-unknown-linux-musl --release -F termios,pty

Running on https://webvm.io/ outputs:

tcgetattr: Err(Os { code: 22, kind: InvalidInput, message: "Invalid argument" })

With this change, the fallback to TCGETS succeeds.

Changes

  • tcgetattr: add Errno::INVAL to fallback match arm
  • tcsetattr: add Errno::INVAL to fallback match arm

@sunfishcode
Copy link
Member

Could this be considered a bug in CheerpX/WebVM? Either for lacking TCGETS2. or for having an unrecognize ioctl fail with EINVAL instead of ENOTTY, which is what actual Linux does for unrecognized ioctl codes?

@NiltonVolpato
Copy link
Author

Sorry for the late response. My interpretation of the ioctl man pages is also that ENOTTY is a more appropriate error to be returned, in this case. That doesn't seem obvious to everyone, though, since there other platforms that are already special cased. But I agree that if CheerpX could fix that, then it would be ideal.

As observed libc seems to fallback to the old ioctl calls anyway. And I also haven't had problems executing other C programs with raw access to the terminal, or Rust programs not using reedline (which indirectly depends on rustix)

I created leaningtech/webvm#214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants