Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefine FdFlag #393

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion yash-builtin/src/source/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async fn report_find_and_open_file_failure(
mod tests {
use super::*;
use assert_matches::assert_matches;
use enumset::EnumSet;
use futures_util::FutureExt as _;
use std::cell::RefCell;
use std::path::Path;
Expand Down Expand Up @@ -259,7 +260,7 @@ mod tests {

let process = system.current_process();
let fd_body = process.get_fd(fd).unwrap();
assert_eq!(fd_body.flag, FdFlag::FD_CLOEXEC);
assert_eq!(fd_body.flags, EnumSet::only(FdFlag::CloseOnExec));
}

#[test]
Expand Down
10 changes: 8 additions & 2 deletions yash-env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- The `OfdAccess`, `OpenFlag`, `Mode`, `RawMode`, `Uid`, `RawUid`, `Gid`, and
`RawGid` types in the `system` module
- The `OfdAccess`, `OpenFlag`, `FdFlag`, `Mode`, `RawMode`, `Uid`, `RawUid`,
`Gid`, and `RawGid` types in the `system` module
- The `System` trait now has the `ofd_access`, `get_and_set_nonblocking`,
`getuid`, `geteuid`, `getgid`, and `getegid` methods.
- `Mode` has been moved from `system::virtual` to `system` and now has constants
Expand All @@ -28,13 +28,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `system::FdFlag` is no longer a re-export of `nix::fcntl::FdFlag`.
- `system::Mode` is no longer a re-export of `nix::sys::stat::Mode`.
- The `system::System::fstatat` method now takes a `follow_symlinks: bool`
parameter instead of an `AtFlags` parameter.
- The `system::System::open` method has been redefined to take `OfdAccess` and
`OpenFlag` parameters instead of `nix::fcntl::OFlag`.
- The `system::System::umask` method now takes and returns a value of the new
`system::Mode` type.
- The `dup`, `fcntl_getfl`, and `fcntl_setfl` methods now operate on an
`EnumSet<FdFlag>` parameter instead of an `nix::fcntl::FdFlag` parameter.
- The `flags: enumset::EnumSet<FdFlag>` field of
`yash_env::system::virtual::FdBody` has replaced
the `flag: nix::fcntl::FdFlag` field.
- External dependency versions:
- Rust 1.77.0 → 1.79.0

Expand Down
12 changes: 6 additions & 6 deletions yash-env/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! [System] and its implementors.

mod errno;
mod fd_flag;
pub mod fd_set;
mod file_system;
mod id;
Expand All @@ -30,6 +31,7 @@ pub mod r#virtual;
pub use self::errno::Errno;
pub use self::errno::RawErrno;
pub use self::errno::Result;
pub use self::fd_flag::FdFlag;
use self::fd_set::FdSet;
pub use self::file_system::Dir;
pub use self::file_system::DirEntry;
Expand Down Expand Up @@ -63,8 +65,6 @@ use crate::trap::SignalSystem;
use crate::Env;
use enumset::EnumSet;
#[doc(no_inline)]
pub use nix::fcntl::FdFlag;
#[doc(no_inline)]
pub use nix::sys::signal::SigmaskHow;
#[doc(no_inline)]
pub use nix::sys::stat::{FileStat, SFlag};
Expand Down Expand Up @@ -119,7 +119,7 @@ pub trait System: Debug {
/// new FD.
///
/// If successful, returns `Ok(new_fd)`. On error, returns `Err(_)`.
fn dup(&mut self, from: Fd, to_min: Fd, flags: FdFlag) -> Result<Fd>;
fn dup(&mut self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd>;

/// Duplicates a file descriptor.
///
Expand Down Expand Up @@ -164,12 +164,12 @@ pub trait System: Debug {
/// Returns the attributes for the file descriptor.
///
/// This is a thin wrapper around the `fcntl` system call.
fn fcntl_getfd(&self, fd: Fd) -> Result<FdFlag>;
fn fcntl_getfd(&self, fd: Fd) -> Result<EnumSet<FdFlag>>;

/// Sets attributes for the file descriptor.
///
/// This is a thin wrapper around the `fcntl` system call.
fn fcntl_setfd(&mut self, fd: Fd, flags: FdFlag) -> Result<()>;
fn fcntl_setfd(&mut self, fd: Fd, flags: EnumSet<FdFlag>) -> Result<()>;

/// Tests if a file descriptor is associated with a terminal device.
fn isatty(&self, fd: Fd) -> Result<bool>;
Expand Down Expand Up @@ -564,7 +564,7 @@ pub trait SystemEx: System {
return Ok(from);
}

let new = self.dup(from, MIN_INTERNAL_FD, FdFlag::FD_CLOEXEC);
let new = self.dup(from, MIN_INTERNAL_FD, FdFlag::CloseOnExec.into());
self.close(from).ok();
new
}
Expand Down
28 changes: 28 additions & 0 deletions yash-env/src/system/fd_flag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2024 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Defines attributes for file descriptors

use enumset::EnumSetType;

/// Attributes for file descriptors
#[derive(Debug, EnumSetType, Hash)]
#[non_exhaustive]
pub enum FdFlag {
/// Close the file descriptor upon execution of an exec family function
CloseOnExec,
// TODO CloseOnFork,
}
23 changes: 16 additions & 7 deletions yash-env/src/system/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ impl System for RealSystem {
Ok((Fd(reader), Fd(writer)))
}

fn dup(&mut self, from: Fd, to_min: Fd, flags: FdFlag) -> Result<Fd> {
let arg = if flags.contains(FdFlag::FD_CLOEXEC) {
fn dup(&mut self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd> {
let arg = if flags.contains(FdFlag::CloseOnExec) {
nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC
} else {
nix::fcntl::FcntlArg::F_DUPFD
Expand Down Expand Up @@ -257,7 +257,7 @@ impl System for RealSystem {
let fd = Fd(file.into_raw_fd());

// Clear the CLOEXEC flag
_ = self.fcntl_setfd(fd, FdFlag::empty());
_ = self.fcntl_setfd(fd, EnumSet::empty());

Ok(fd)
}
Expand Down Expand Up @@ -291,13 +291,22 @@ impl System for RealSystem {
Ok(was_nonblocking)
}

fn fcntl_getfd(&self, fd: Fd) -> Result<FdFlag> {
fn fcntl_getfd(&self, fd: Fd) -> Result<EnumSet<FdFlag>> {
let bits = nix::fcntl::fcntl(fd.0, nix::fcntl::FcntlArg::F_GETFD)?;
Ok(FdFlag::from_bits_truncate(bits))
let bits = nix::fcntl::FdFlag::from_bits_retain(bits);
let mut flags = EnumSet::empty();
if bits.contains(nix::fcntl::FdFlag::FD_CLOEXEC) {
flags.insert(FdFlag::CloseOnExec);
}
Ok(flags)
}

fn fcntl_setfd(&mut self, fd: Fd, flags: FdFlag) -> Result<()> {
let _ = nix::fcntl::fcntl(fd.0, nix::fcntl::FcntlArg::F_SETFD(flags))?;
fn fcntl_setfd(&mut self, fd: Fd, flags: EnumSet<FdFlag>) -> Result<()> {
let mut bits = nix::fcntl::FdFlag::empty();
if flags.contains(FdFlag::CloseOnExec) {
bits.insert(nix::fcntl::FdFlag::FD_CLOEXEC);
}
nix::fcntl::fcntl(fd.0, nix::fcntl::FcntlArg::F_SETFD(bits))?;
Ok(())
}

Expand Down
14 changes: 7 additions & 7 deletions yash-env/src/system/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::signal;
use super::ChildProcessStarter;
use super::Dir;
use super::Errno;
use super::FdFlag;
use super::FdSet;
use super::Gid;
use super::LimitPair;
Expand All @@ -42,7 +43,6 @@ use crate::job::ProcessState;
#[cfg(doc)]
use crate::Env;
use enumset::EnumSet;
use nix::fcntl::FdFlag;
use nix::sys::signal::SigmaskHow;
use nix::sys::stat::FileStat;
use nix::sys::time::TimeSpec;
Expand Down Expand Up @@ -316,7 +316,7 @@ impl System for &SharedSystem {
fn pipe(&mut self) -> Result<(Fd, Fd)> {
self.0.borrow_mut().pipe()
}
fn dup(&mut self, from: Fd, to_min: Fd, flags: FdFlag) -> Result<Fd> {
fn dup(&mut self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd> {
self.0.borrow_mut().dup(from, to_min, flags)
}
fn dup2(&mut self, from: Fd, to: Fd) -> Result<Fd> {
Expand All @@ -343,10 +343,10 @@ impl System for &SharedSystem {
fn get_and_set_nonblocking(&mut self, fd: Fd, nonblocking: bool) -> Result<bool> {
self.0.borrow_mut().get_and_set_nonblocking(fd, nonblocking)
}
fn fcntl_getfd(&self, fd: Fd) -> Result<FdFlag> {
fn fcntl_getfd(&self, fd: Fd) -> Result<EnumSet<FdFlag>> {
self.0.borrow().fcntl_getfd(fd)
}
fn fcntl_setfd(&mut self, fd: Fd, flags: FdFlag) -> Result<()> {
fn fcntl_setfd(&mut self, fd: Fd, flags: EnumSet<FdFlag>) -> Result<()> {
self.0.borrow_mut().fcntl_setfd(fd, flags)
}
fn isatty(&self, fd: Fd) -> Result<bool> {
Expand Down Expand Up @@ -502,7 +502,7 @@ impl System for SharedSystem {
(&mut &*self).pipe()
}
#[inline]
fn dup(&mut self, from: Fd, to_min: Fd, flags: FdFlag) -> Result<Fd> {
fn dup(&mut self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd> {
(&mut &*self).dup(from, to_min, flags)
}
#[inline]
Expand Down Expand Up @@ -536,11 +536,11 @@ impl System for SharedSystem {
(&mut &*self).get_and_set_nonblocking(fd, nonblocking)
}
#[inline]
fn fcntl_getfd(&self, fd: Fd) -> Result<FdFlag> {
fn fcntl_getfd(&self, fd: Fd) -> Result<EnumSet<FdFlag>> {
(&self).fcntl_getfd(fd)
}
#[inline]
fn fcntl_setfd(&mut self, fd: Fd, flags: FdFlag) -> Result<()> {
fn fcntl_setfd(&mut self, fd: Fd, flags: EnumSet<FdFlag>) -> Result<()> {
(&mut &*self).fcntl_setfd(fd, flags)
}
#[inline]
Expand Down
Loading
Loading