diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/rustix/examples | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/examples')
-rw-r--r-- | vendor/rustix/examples/dup2_to_replace_stdio.rs | 55 | ||||
-rw-r--r-- | vendor/rustix/examples/hello.rs | 43 | ||||
-rw-r--r-- | vendor/rustix/examples/process.rs | 105 | ||||
-rw-r--r-- | vendor/rustix/examples/stdio.rs | 465 | ||||
-rw-r--r-- | vendor/rustix/examples/time.rs | 20 |
5 files changed, 688 insertions, 0 deletions
diff --git a/vendor/rustix/examples/dup2_to_replace_stdio.rs b/vendor/rustix/examples/dup2_to_replace_stdio.rs new file mode 100644 index 000000000..cc8565915 --- /dev/null +++ b/vendor/rustix/examples/dup2_to_replace_stdio.rs @@ -0,0 +1,55 @@ +//! This is an example of how to use `dup2` to replace the stdin and stdout +//! file descriptors. + +#[cfg(not(windows))] +fn main() { + use rustix::io::{dup2, pipe}; + use std::io::{BufRead, BufReader}; + use std::mem::forget; + + // Create some new file descriptors that we'll use to replace stdio's file + // descriptors with. + let (reader, writer) = pipe().unwrap(); + + // Acquire `OwnedFd` instances for stdin and stdout. These APIs are `unsafe` + // because in general, with low-level APIs like this, libraries can't assume + // that stdin and stdout will be open or safe to use. It's ok here, because + // we're directly inside `main`, so we know that stdin and stdout haven't + // been closed and aren't being used for other purposes. + let (mut stdin, mut stdout) = unsafe { (rustix::io::take_stdin(), rustix::io::take_stdout()) }; + + // Use `dup2` to copy our new file descriptors over the stdio file descriptors. + // + // These take their second argument as an `&mut OwnedFd` rather than the + // usual `impl AsFd` because they conceptually do a `close` on the original + // file descriptor, which one shouldn't be able to do with just a + // `BorrowedFd`. + dup2(&reader, &mut stdin).unwrap(); + dup2(&writer, &mut stdout).unwrap(); + + // Then, forget the stdio `OwnedFd`s, because actually dropping them would + // close them. Here, we want stdin and stdout to remain open for the rest + // of the program. + forget(stdin); + forget(stdout); + + // We can also drop the original file descriptors now, since `dup2` creates + // new file descriptors with independent lifetimes. + drop(reader); + drop(writer); + + // Now we can print to "stdout" in the usual way, and it'll go to our pipe. + println!("hello, world!"); + + // And we can read from stdin, and it'll read from our pipe. It's a little + // silly that we connected our stdout to our own stdin, but it's just an + // example :-). + let mut s = String::new(); + BufReader::new(std::io::stdin()).read_line(&mut s).unwrap(); + assert_eq!(s, "hello, world!\n"); +} + +#[cfg(windows)] +fn main() { + unimplemented!() +} diff --git a/vendor/rustix/examples/hello.rs b/vendor/rustix/examples/hello.rs new file mode 100644 index 000000000..16ce69bd6 --- /dev/null +++ b/vendor/rustix/examples/hello.rs @@ -0,0 +1,43 @@ +//! Hello world, via plain syscalls. + +#[cfg(all(feature = "std", not(windows)))] +fn main() -> std::io::Result<()> { + // The message to print. It includes an explicit newline because we're + // not using `println!`, so we have to include the newline manually. + let message = "Hello, world!\n"; + + // The bytes to print. The `write` syscall operates on byte buffers and + // returns a byte offset if it writes fewer bytes than requested, so we + // need the ability to compute substrings at arbitrary byte offsets. + let mut bytes = message.as_bytes(); + + // Safety: See [here] for the safety conditions for calling `stdout`. In + // this example, the code is inside `main` itself so we know how `stdout` + // is being used and we know that it's not dropped. + // + // [here]: https://docs.rs/rustix/*/rustix/io/fn.stdout.html#safety + let stdout = unsafe { rustix::io::stdout() }; + + while !bytes.is_empty() { + match rustix::io::write(&stdout, bytes) { + // `write` can write fewer bytes than requested. In that case, + // continue writing with the remainder of the bytes. + Ok(n) => bytes = &bytes[n..], + + // `write` can be interrupted before doing any work; if that + // happens, retry it. + Err(rustix::io::Errno::INTR) => (), + + // `write` can also fail for external reasons, such as running out + // of storage space. + Err(e) => return Err(e.into()), + } + } + + Ok(()) +} + +#[cfg(any(not(feature = "std"), windows))] +fn main() { + unimplemented!() +} diff --git a/vendor/rustix/examples/process.rs b/vendor/rustix/examples/process.rs new file mode 100644 index 000000000..49c6b8d9e --- /dev/null +++ b/vendor/rustix/examples/process.rs @@ -0,0 +1,105 @@ +//! A command which prints out information about the process it runs in. + +use rustix::io; + +#[cfg(all(feature = "process", feature = "param"))] +#[cfg(not(windows))] +fn main() -> io::Result<()> { + use rustix::param::*; + use rustix::process::*; + + println!("Pid: {}", getpid().as_raw_nonzero()); + println!("Parent Pid: {}", Pid::as_raw(getppid())); + println!("Uid: {}", getuid().as_raw()); + println!("Gid: {}", getgid().as_raw()); + #[cfg(any( + all(target_os = "android", target_pointer_width = "64"), + target_os = "linux", + ))] + { + let (a, b) = linux_hwcap(); + println!("Linux hwcap: {:#x}, {:#x}", a, b); + } + println!("Page size: {}", page_size()); + println!("Clock ticks/sec: {}", clock_ticks_per_second()); + println!("Uname: {:?}", uname()); + println!("Process group priority: {}", getpriority_pgrp(None)?); + println!("Process priority: {}", getpriority_process(None)?); + println!("User priority: {}", getpriority_user(Uid::ROOT)?); + println!( + "Current working directory: {}", + getcwd(Vec::new())?.to_string_lossy() + ); + println!("Cpu Limit: {:?}", getrlimit(Resource::Cpu)); + println!("Fsize Limit: {:?}", getrlimit(Resource::Fsize)); + println!("Data Limit: {:?}", getrlimit(Resource::Data)); + println!("Stack Limit: {:?}", getrlimit(Resource::Stack)); + println!("Core Limit: {:?}", getrlimit(Resource::Core)); + println!("Rss Limit: {:?}", getrlimit(Resource::Rss)); + println!("Nproc Limit: {:?}", getrlimit(Resource::Nproc)); + println!("Nofile Limit: {:?}", getrlimit(Resource::Nofile)); + println!("Memlock Limit: {:?}", getrlimit(Resource::Memlock)); + #[cfg(not(target_os = "openbsd"))] + println!("As Limit: {:?}", getrlimit(Resource::As)); + #[cfg(not(any( + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + println!("Locks Limit: {:?}", getrlimit(Resource::Locks)); + #[cfg(not(any( + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + println!("Sigpending Limit: {:?}", getrlimit(Resource::Sigpending)); + #[cfg(not(any( + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + println!("Msgqueue Limit: {:?}", getrlimit(Resource::Msgqueue)); + #[cfg(not(any( + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + println!("Nice Limit: {:?}", getrlimit(Resource::Nice)); + #[cfg(not(any( + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + println!("Rtprio Limit: {:?}", getrlimit(Resource::Rtprio)); + #[cfg(not(any( + target_os = "android", + target_os = "emscripten", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + println!("Rttime Limit: {:?}", getrlimit(Resource::Rttime)); + #[cfg(any( + all(target_os = "android", target_pointer_width = "64"), + target_os = "linux" + ))] + println!("Execfn: {:?}", linux_execfn()); + Ok(()) +} + +#[cfg(any(windows, not(all(feature = "process", feature = "param"))))] +fn main() -> io::Result<()> { + unimplemented!() +} diff --git a/vendor/rustix/examples/stdio.rs b/vendor/rustix/examples/stdio.rs new file mode 100644 index 000000000..9e79d5356 --- /dev/null +++ b/vendor/rustix/examples/stdio.rs @@ -0,0 +1,465 @@ +//! A command which prints out information about the standard input, +//! output, and error streams provided to it. + +#![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + +#[cfg(not(windows))] +use rustix::fd::AsFd; +#[cfg(not(windows))] +use rustix::io::{self, stderr, stdin, stdout}; +#[cfg(feature = "termios")] +#[cfg(not(windows))] +use rustix::termios::isatty; +#[cfg(all(not(windows), feature = "termios", feature = "procfs"))] +use rustix::termios::ttyname; + +#[cfg(not(windows))] +fn main() -> io::Result<()> { + let (stdin, stdout, stderr) = unsafe { (stdin(), stdout(), stderr()) }; + + println!("Stdin:"); + show(&stdin)?; + + println!("Stdout:"); + show(&stdout)?; + + println!("Stderr:"); + show(&stderr)?; + + Ok(()) +} + +#[cfg(not(windows))] +fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> { + let fd = fd.as_fd(); + println!(" - ready: {:?}", rustix::io::ioctl_fionread(fd)?); + + #[cfg(feature = "termios")] + if isatty(fd) { + #[cfg(feature = "procfs")] + println!(" - ttyname: {}", ttyname(fd, Vec::new())?.to_string_lossy()); + println!(" - process group: {:?}", rustix::termios::tcgetpgrp(fd)?); + println!(" - winsize: {:?}", rustix::termios::tcgetwinsize(fd)?); + + { + use rustix::termios::*; + let term = tcgetattr(fd)?; + + if let Some(speed) = speed_value(cfgetispeed(&term)) { + println!(" - ispeed: {}", speed); + } + if let Some(speed) = speed_value(cfgetospeed(&term)) { + println!(" - ospeed: {}", speed); + } + + print!(" - in flags:"); + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IGNBRK) != 0 { + print!(" IGNBRK"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & BRKINT) != 0 { + print!(" BRKINT"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IGNPAR) != 0 { + print!(" IGNPAR"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & PARMRK) != 0 { + print!(" PARMRK"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & INPCK) != 0 { + print!(" INPCK"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & ISTRIP) != 0 { + print!(" ISTRIP"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & INLCR) != 0 { + print!(" INLCR"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IGNCR) != 0 { + print!(" IGNCR"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & ICRNL) != 0 { + print!(" ICRNL"); + } + #[cfg(any( + linux_raw, + all( + libc, + any(target_os = "haiku", target_os = "illumos", target_os = "solaris"), + ) + ))] + if (term.c_iflag & IUCLC) != 0 { + print!(" IUCLC"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IXON) != 0 { + print!(" IXON"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IXANY) != 0 { + print!(" IXANY"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IXOFF) != 0 { + print!(" IXOFF"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_iflag & IMAXBEL) != 0 { + print!(" IMAXBEL"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_iflag & IUTF8) != 0 { + print!(" IUTF8"); + } + println!(); + + print!(" - out flags:"); + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_oflag & OPOST) != 0 { + print!(" OPOST"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "redox", + )))] + if (term.c_oflag & OLCUC) != 0 { + print!(" OLCUC"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_oflag & ONLCR) != 0 { + print!(" ONLCR"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_oflag & OCRNL) != 0 { + print!(" OCRNL"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_oflag & ONOCR) != 0 { + print!(" ONOCR"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_oflag & ONLRET) != 0 { + print!(" ONLRET"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + if (term.c_oflag & OFILL) != 0 { + print!(" OFILL"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + if (term.c_oflag & OFDEL) != 0 { + print!(" OFDEL"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_oflag & NLDLY) != 0 { + print!(" NLDLY"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_oflag & CRDLY) != 0 { + print!(" CRDLY"); + } + #[cfg(not(any( + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "illumos", + target_os = "redox", + )))] + if (term.c_oflag & TABDLY) != 0 { + print!(" TABDLY"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_oflag & BSDLY) != 0 { + print!(" BSDLY"); + } + #[cfg(not(any( + all(libc, target_env = "musl"), + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_oflag & VTDLY) != 0 { + print!(" VTDLY"); + } + #[cfg(not(any( + all(libc, target_env = "musl"), + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_oflag & FFDLY) != 0 { + print!(" FFDLY"); + } + println!(); + + print!(" - control flags:"); + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_cflag & CBAUD) != 0 { + print!(" CBAUD"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_cflag & CBAUDEX) != 0 { + print!(" CBAUDEX"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & CSIZE) != 0 { + print!(" CSIZE"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & CSTOPB) != 0 { + print!(" CSTOPB"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & CREAD) != 0 { + print!(" CREAD"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & PARENB) != 0 { + print!(" PARENB"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & PARODD) != 0 { + print!(" PARODD"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & HUPCL) != 0 { + print!(" HUPCL"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & CLOCAL) != 0 { + print!(" CLOCAL"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_cflag & CIBAUD) != 0 { + print!(" CIBAUD"); + } + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + )))] + if (term.c_cflag & CMSPAR) != 0 { + print!(" CMSPAR"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_cflag & CRTSCTS) != 0 { + print!(" CRTSCTS"); + } + println!(); + + print!(" - local flags:"); + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ISIG) != 0 { + print!(" ISIG"); + } + if (term.c_lflag & ICANON) != 0 { + print!(" ICANON"); + } + #[cfg(any(linux_raw, all(libc, any(target_arch = "s390x", target_os = "haiku"))))] + if (term.c_lflag & XCASE) != 0 { + print!(" XCASE"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHO) != 0 { + print!(" ECHO"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHOE) != 0 { + print!(" ECHOE"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHOK) != 0 { + print!(" ECHOK"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHONL) != 0 { + print!(" ECHONL"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHOCTL) != 0 { + print!(" ECHOCTL"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHOPRT) != 0 { + print!(" ECHOPRT"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & ECHOKE) != 0 { + print!(" ECHOKE"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & FLUSHO) != 0 { + print!(" FLUSHO"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & NOFLSH) != 0 { + print!(" NOFLSH"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & TOSTOP) != 0 { + print!(" TOSTOP"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & PENDIN) != 0 { + print!(" PENDIN"); + } + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + if (term.c_lflag & IEXTEN) != 0 { + print!(" IEXTEN"); + } + println!(); + + println!( + " - keys: INTR={} QUIT={} ERASE={} KILL={} EOF={} TIME={} MIN={} ", + key(term.c_cc[VINTR]), + key(term.c_cc[VQUIT]), + key(term.c_cc[VERASE]), + key(term.c_cc[VKILL]), + key(term.c_cc[VEOF]), + term.c_cc[VTIME], + term.c_cc[VMIN] + ); + println!( + " START={} STOP={} SUSP={} EOL={} REPRINT={} DISCARD={}", + key(term.c_cc[VSTART]), + key(term.c_cc[VSTOP]), + key(term.c_cc[VSUSP]), + key(term.c_cc[VEOL]), + key(term.c_cc[VREPRINT]), + key(term.c_cc[VDISCARD]) + ); + println!( + " WERASE={} LNEXT={} EOL2={}", + key(term.c_cc[VWERASE]), + key(term.c_cc[VLNEXT]), + key(term.c_cc[VEOL2]) + ); + } + } else { + println!(" - is not a tty"); + } + + println!(); + Ok(()) +} + +#[cfg(feature = "termios")] +#[cfg(not(windows))] +fn key(b: u8) -> String { + if b == 0 { + format!("<undef>") + } else if b < 0x20 { + format!("^{}", (b + 0x40) as char) + } else if b == 0x7f { + format!("^?") + } else { + format!("{}", b as char) + } +} + +#[cfg(windows)] +fn main() { + unimplemented!() +} diff --git a/vendor/rustix/examples/time.rs b/vendor/rustix/examples/time.rs new file mode 100644 index 000000000..3648336a7 --- /dev/null +++ b/vendor/rustix/examples/time.rs @@ -0,0 +1,20 @@ +//! A command which prints the current values of the realtime and monotonic +//! clocks it's given. + +#[cfg(not(windows))] +#[cfg(feature = "time")] +fn main() { + println!( + "Real time: {:?}", + rustix::time::clock_gettime(rustix::time::ClockId::Realtime) + ); + println!( + "Monotonic time: {:?}", + rustix::time::clock_gettime(rustix::time::ClockId::Monotonic) + ); +} + +#[cfg(any(windows, not(feature = "time")))] +fn main() { + unimplemented!() +} |