1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2021 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/>.
//! [System] and its implementors.
mod errno;
mod fd_flag;
mod file_system;
mod id;
mod open_flag;
#[cfg(unix)]
pub mod real;
pub mod resource;
mod select;
mod shared;
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;
pub use self::file_system::Dir;
pub use self::file_system::DirEntry;
pub use self::file_system::FileType;
pub use self::file_system::Mode;
pub use self::file_system::RawMode;
pub use self::file_system::Stat;
pub use self::file_system::AT_FDCWD;
pub use self::id::Gid;
pub use self::id::RawGid;
pub use self::id::RawUid;
pub use self::id::Uid;
pub use self::open_flag::OfdAccess;
pub use self::open_flag::OpenFlag;
#[cfg(doc)]
use self::r#virtual::VirtualSystem;
#[cfg(all(doc, unix))]
use self::real::RealSystem;
use self::resource::LimitPair;
use self::resource::Resource;
use self::select::SelectSystem;
use self::select::SignalStatus;
pub use self::shared::SharedSystem;
use crate::io::Fd;
use crate::io::MIN_INTERNAL_FD;
use crate::job::Pid;
use crate::job::ProcessState;
use crate::path::Path;
use crate::path::PathBuf;
use crate::semantics::ExitStatus;
use crate::signal;
use crate::str::UnixString;
#[cfg(doc)]
use crate::subshell::Subshell;
use crate::trap::SignalSystem;
use crate::Env;
use enumset::EnumSet;
use std::convert::Infallible;
use std::ffi::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt::Debug;
use std::future::Future;
use std::io::SeekFrom;
use std::pin::Pin;
use std::time::Duration;
use std::time::Instant;
/// API to the system-managed parts of the environment.
///
/// The `System` trait defines a collection of methods to access the underlying
/// operating system from the shell as an application program. There are two
/// substantial implementors for this trait: [`RealSystem`] and
/// [`VirtualSystem`]. Another implementor is [`SharedSystem`], which wraps a
/// `System` instance to extend the interface with asynchronous methods.
pub trait System: Debug {
/// Retrieves metadata of a file.
fn fstat(&self, fd: Fd) -> Result<Stat>;
/// Retrieves metadata of a file.
fn fstatat(&self, dir_fd: Fd, path: &CStr, follow_symlinks: bool) -> Result<Stat>;
/// Whether there is an executable file at the specified path.
#[must_use]
fn is_executable_file(&self, path: &CStr) -> bool;
/// Whether there is a directory at the specified path.
#[must_use]
fn is_directory(&self, path: &CStr) -> bool;
/// Creates an unnamed pipe.
///
/// This is a thin wrapper around the `pipe` system call.
/// If successful, returns the reading and writing ends of the pipe.
fn pipe(&mut self) -> Result<(Fd, Fd)>;
/// Duplicates a file descriptor.
///
/// This is a thin wrapper around the `fcntl` system call that opens a new
/// FD that shares the open file description with `from`. The new FD will be
/// the minimum unused FD not less than `to_min`. The `flags` are set to the
/// new FD.
///
/// If successful, returns `Ok(new_fd)`. On error, returns `Err(_)`.
fn dup(&mut self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd>;
/// Duplicates a file descriptor.
///
/// This is a thin wrapper around the `dup2` system call. If successful,
/// returns `Ok(to)`. On error, returns `Err(_)`.
fn dup2(&mut self, from: Fd, to: Fd) -> Result<Fd>;
/// Opens a file descriptor.
///
/// This is a thin wrapper around the `open` system call.
fn open(
&mut self,
path: &CStr,
access: OfdAccess,
flags: EnumSet<OpenFlag>,
mode: Mode,
) -> Result<Fd>;
/// Opens a file descriptor associated with an anonymous temporary file.
///
/// This function works similarly to the `O_TMPFILE` flag specified to the
/// `open` function.
fn open_tmpfile(&mut self, parent_dir: &Path) -> Result<Fd>;
/// Closes a file descriptor.
///
/// This is a thin wrapper around the `close` system call.
///
/// This function returns `Ok(())` when the FD is already closed.
fn close(&mut self, fd: Fd) -> Result<()>;
/// Returns the open file description access mode.
fn ofd_access(&self, fd: Fd) -> Result<OfdAccess>;
/// Gets and sets the non-blocking mode for the open file description.
///
/// This is a wrapper around the `fcntl` system call.
/// This function sets the non-blocking mode to the given value and returns
/// the previous mode.
fn get_and_set_nonblocking(&mut self, fd: Fd, nonblocking: bool) -> Result<bool>;
/// Returns the attributes for the file descriptor.
///
/// This is a thin wrapper around the `fcntl` system call.
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: EnumSet<FdFlag>) -> Result<()>;
/// Tests if a file descriptor is associated with a terminal device.
///
/// On error, this function simply returns `false` and no detailed error
/// information is provided because POSIX does not require the `isatty`
/// function to set `errno`.
fn isatty(&self, fd: Fd) -> bool;
/// Reads from the file descriptor.
///
/// This is a thin wrapper around the `read` system call.
/// If successful, returns the number of bytes read.
///
/// This function may perform blocking I/O, especially if the `O_NONBLOCK`
/// flag is not set for the FD. Use [`SharedSystem::read_async`] to support
/// concurrent I/O in an `async` function context.
fn read(&mut self, fd: Fd, buffer: &mut [u8]) -> Result<usize>;
/// Writes to the file descriptor.
///
/// This is a thin wrapper around the `write` system call.
/// If successful, returns the number of bytes written.
///
/// This function may write only part of the `buffer` and block if the
/// `O_NONBLOCK` flag is not set for the FD. Use [`SharedSystem::write_all`]
/// to support concurrent I/O in an `async` function context and ensure the
/// whole `buffer` is written.
fn write(&mut self, fd: Fd, buffer: &[u8]) -> Result<usize>;
/// Moves the position of the open file description.
fn lseek(&mut self, fd: Fd, position: SeekFrom) -> Result<u64>;
/// Opens a directory for enumerating entries.
fn fdopendir(&mut self, fd: Fd) -> Result<Box<dyn Dir>>;
/// Opens a directory for enumerating entries.
fn opendir(&mut self, path: &CStr) -> Result<Box<dyn Dir>>;
/// Gets and sets the file creation mode mask.
///
/// This is a thin wrapper around the `umask` system call. It sets the mask
/// to the given value and returns the previous mask.
///
/// You cannot tell the current mask without setting a new one. If you only
/// want to get the current mask, you need to set it back to the original
/// value after getting it.
fn umask(&mut self, new_mask: Mode) -> Mode;
/// Returns the current time.
#[must_use]
fn now(&self) -> Instant;
/// Returns consumed CPU times.
fn times(&self) -> Result<Times>;
/// Tests if a signal number is valid.
///
/// This function returns `Some((name, number))` if the signal number refers
/// to a valid signal supported by the system. Otherwise, it returns `None`.
///
/// Note that one signal number can have multiple names, in which case this
/// function returns the name that is considered the most common.
#[must_use]
fn validate_signal(&self, number: signal::RawNumber) -> Option<(signal::Name, signal::Number)>;
/// Gets the signal number from the signal name.
///
/// This function returns the signal number corresponding to the signal name
/// in the system. If the signal name is not supported, it returns `None`.
#[must_use]
fn signal_number_from_name(&self, name: signal::Name) -> Option<signal::Number>;
/// Gets and/or sets the signal blocking mask.
///
/// This is a low-level function used internally by
/// [`SharedSystem::set_disposition`]. You should not call this function
/// directly, or you will disrupt the behavior of `SharedSystem`. The
/// description below applies if you want to do everything yourself without
/// depending on `SharedSystem`.
///
/// This is a thin wrapper around the `sigprocmask` system call. If `op` is
/// `Some`, this function updates the signal blocking mask by applying the
/// given `SigmaskOp` and signal set to the current mask. If `op` is `None`,
/// this function does not change the mask.
/// If `old_mask` is `Some`, this function sets the previous mask to it.
fn sigmask(
&mut self,
op: Option<(SigmaskOp, &[signal::Number])>,
old_mask: Option<&mut Vec<signal::Number>>,
) -> Result<()>;
/// Gets and sets the disposition for a signal.
///
/// This is a low-level function used internally by
/// [`SharedSystem::set_disposition`]. You should not call this function
/// directly, or you will disrupt the behavior of `SharedSystem`. The
/// description below applies if you want to do everything yourself without
/// depending on `SharedSystem`.
///
/// This is an abstract wrapper around the `sigaction` system call. This
/// function returns the previous disposition if successful.
///
/// When you set the disposition to `Disposition::Catch`, signals sent to
/// this process are accumulated in the `System` instance and made available
/// from [`caught_signals`](Self::caught_signals).
fn sigaction(&mut self, signal: signal::Number, action: Disposition) -> Result<Disposition>;
/// Returns signals this process has caught, if any.
///
/// This is a low-level function used internally by
/// [`SharedSystem::select`]. You should not call this function directly, or
/// you will disrupt the behavior of `SharedSystem`. The description below
/// applies if you want to do everything yourself without depending on
/// `SharedSystem`.
///
/// To catch a signal, you must firstly install a signal handler by calling
/// [`sigaction`](Self::sigaction) with [`Disposition::Catch`]. Once the
/// handler is ready, signals sent to the process are accumulated in the
/// `System`. You call `caught_signals` to obtain a list of caught signals
/// thus far.
///
/// This function clears the internal list of caught signals, so a next call
/// will return an empty list unless another signal is caught since the
/// first call. Because the list size is limited, you should call this
/// function periodically before the list gets full, in which case further
/// caught signals are silently ignored.
///
/// Note that signals become pending if sent while blocked by
/// [`sigmask`](Self::sigmask). They must be unblocked so that they are
/// caught and made available from this function.
fn caught_signals(&mut self) -> Vec<signal::Number>;
/// Sends a signal.
///
/// This is a thin wrapper around the `kill` system call. If `signal` is
/// `None`, permission to send a signal is checked, but no signal is sent.
///
/// The virtual system version of this function blocks the calling thread if
/// the signal stops or terminates the current process, hence returning a
/// future. See [`VirtualSystem::kill`] for details.
fn kill(
&mut self,
target: Pid,
signal: Option<signal::Number>,
) -> Pin<Box<dyn Future<Output = Result<()>>>>;
/// Waits for a next event.
///
/// This is a low-level function used internally by
/// [`SharedSystem::select`]. You should not call this function directly, or
/// you will disrupt the behavior of `SharedSystem`. The description below
/// applies if you want to do everything yourself without depending on
/// `SharedSystem`.
///
/// This function blocks the calling thread until one of the following
/// condition is met:
///
/// - An FD in `readers` becomes ready for reading.
/// - An FD in `writers` becomes ready for writing.
/// - The specified `timeout` duration has passed.
/// - A signal handler catches a signal.
///
/// When this function returns an `Ok`, FDs that are not ready for reading
/// and writing are removed from `readers` and `writers`, respectively. The
/// return value will be the number of FDs left in `readers` and `writers`.
///
/// If `readers` and `writers` contain an FD that is not open for reading
/// and writing, respectively, this function will fail with `EBADF`. In this
/// case, you should remove the FD from `readers` and `writers` and try
/// again.
///
/// If `signal_mask` is `Some` list of signals, it is used as the signal
/// blocking mask while waiting and restored when the function returns.
fn select(
&mut self,
readers: &mut Vec<Fd>,
writers: &mut Vec<Fd>,
timeout: Option<Duration>,
signal_mask: Option<&[signal::Number]>,
) -> Result<c_int>;
/// Returns the process ID of the current process.
#[must_use]
fn getpid(&self) -> Pid;
/// Returns the process ID of the parent process.
#[must_use]
fn getppid(&self) -> Pid;
/// Returns the process group ID of the current process.
#[must_use]
fn getpgrp(&self) -> Pid;
/// Modifies the process group ID of a process.
///
/// This is a thin wrapper around the `setpgid` system call.
fn setpgid(&mut self, pid: Pid, pgid: Pid) -> Result<()>;
/// Returns the current foreground process group ID.
///
/// This is a thin wrapper around the `tcgetpgrp` system call.
fn tcgetpgrp(&self, fd: Fd) -> Result<Pid>;
/// Switches the foreground process group.
///
/// This is a thin wrapper around the `tcsetpgrp` system call.
fn tcsetpgrp(&mut self, fd: Fd, pgid: Pid) -> Result<()>;
/// Creates a new child process.
///
/// This is a thin wrapper around the `fork` system call. Users of `Env`
/// should not call it directly. Instead, use [`Subshell`] so that the
/// environment can condition the state of the child process before it
/// starts running.
///
/// If successful, this function returns a [`ChildProcessStarter`] function. The
/// caller must call the starter exactly once to make sure the parent and
/// child processes perform correctly after forking.
fn new_child_process(&mut self) -> Result<ChildProcessStarter>;
/// Reports updated status of a child process.
///
/// This is a low-level function used internally by
/// [`Env::wait_for_subshell`]. You should not call this function directly,
/// or you will disrupt the behavior of `Env`. The description below applies
/// if you want to do everything yourself without depending on `Env`.
///
/// This function performs
/// `waitpid(target, ..., WUNTRACED | WCONTINUED | WNOHANG)`.
/// Despite the name, this function does not block: it returns the result
/// immediately.
///
/// This function returns a pair of the process ID and the process state if
/// a process matching `target` is found and its state has changed. If all
/// the processes matching `target` have not changed their states, this
/// function returns `Ok(None)`. If an error occurs, this function returns
/// `Err(_)`.
fn wait(&mut self, target: Pid) -> Result<Option<(Pid, ProcessState)>>;
// TODO Consider passing raw pointers for optimization
/// Replaces the current process with an external utility.
///
/// This is a thin wrapper around the `execve` system call.
fn execve(&mut self, path: &CStr, args: &[CString], envs: &[CString]) -> Result<Infallible>;
/// Returns the current working directory path.
fn getcwd(&self) -> Result<PathBuf>;
/// Changes the working directory.
fn chdir(&mut self, path: &CStr) -> Result<()>;
/// Returns the real user ID of the current process.
fn getuid(&self) -> Uid;
/// Returns the effective user ID of the current process.
fn geteuid(&self) -> Uid;
/// Returns the real group ID of the current process.
fn getgid(&self) -> Gid;
/// Returns the effective group ID of the current process.
fn getegid(&self) -> Gid;
/// Returns the home directory path of the given user.
///
/// Returns `Ok(None)` if the user is not found.
fn getpwnam_dir(&self, name: &str) -> Result<Option<PathBuf>>;
/// Returns the standard `$PATH` value where all standard utilities are
/// expected to be found.
///
/// This is a thin wrapper around the `confstr(_CS_PATH, …)`.
fn confstr_path(&self) -> Result<UnixString>;
/// Returns the path to the shell executable.
///
/// If possible, this function should return the path to the current shell
/// executable. Otherwise, it should return the path to the default POSIX
/// shell.
fn shell_path(&self) -> CString;
/// Returns the limits for the specified resource.
///
/// This function returns a pair of the soft and hard limits for the given
/// resource. The soft limit is the current limit, and the hard limit is the
/// maximum value that the soft limit can be set to.
///
/// When no limit is set, the limit value is [`INFINITY`].
///
/// This is a thin wrapper around the `getrlimit` system call.
///
/// [`INFINITY`]: self::resource::INFINITY
fn getrlimit(&self, resource: Resource) -> Result<LimitPair>;
/// Sets the limits for the specified resource.
///
/// Specify [`INFINITY`] as the limit value to remove the limit.
///
/// This is a thin wrapper around the `setrlimit` system call.
///
/// [`INFINITY`]: self::resource::INFINITY
fn setrlimit(&mut self, resource: Resource, limits: LimitPair) -> Result<()>;
}
/// Set of consumed CPU time
///
/// This structure contains four CPU time values, all in seconds.
///
/// This structure is returned by [`System::times`].
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Times {
/// User CPU time consumed by the current process
pub self_user: f64,
/// System CPU time consumed by the current process
pub self_system: f64,
/// User CPU time consumed by the children of the current process
pub children_user: f64,
/// System CPU time consumed by the children of the current process
pub children_system: f64,
}
/// Operation applied to the signal blocking mask
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum SigmaskOp {
/// Add signals to the mask (`SIG_BLOCK`)
Add,
/// Remove signals from the mask (`SIG_UNBLOCK`)
Remove,
/// Set the mask to the given signals (`SIG_SETMASK`)
Set,
}
/// How the shell process responds to a signal
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Disposition {
/// Perform the default action for the signal.
///
/// The default action depends on the signal. For example, `SIGINT` causes
/// the process to terminate, and `SIGTSTP` causes the process to stop.
#[default]
Default,
/// Ignore the signal.
Ignore,
/// Catch the signal.
Catch,
}
/// Task executed in a child process
///
/// This is an argument passed to a [`ChildProcessStarter`]. The task is
/// executed in a child process initiated by the starter. The environment passed
/// to the task is a clone of the parent environment, but it has a different
/// process ID than the parent.
pub type ChildProcessTask =
Box<dyn for<'a> FnOnce(&'a mut Env) -> Pin<Box<dyn Future<Output = ()> + 'a>>>;
/// Abstract function that starts a child process
///
/// [`System::new_child_process`] returns a child process starter. You need to
/// pass the parent environment and a task to run in the child.
///
/// [`RealSystem`]'s `new_child_process` performs a `fork` system call and
/// returns a starter in the parent and child processes. When the starter is
/// called in the parent, it just returns the child process ID. The starter in
/// the child process runs the task and exits the process with the exit status
/// of the task.
///
/// For [`VirtualSystem`], no real child process is created. Instead, the
/// starter runs the task concurrently in the current process using the executor
/// contained in the system. A new [`Process`](virtual::Process) is added to the
/// system to represent the child process. The starter returns its process ID.
/// See also [`VirtualSystem::new_child_process`].
///
/// This function only starts the child, which continues to run asynchronously
/// after the function returns its PID. To wait for the child to finish and
/// obtain its exit status, use [`System::wait`].
pub type ChildProcessStarter = Box<
dyn for<'a> FnOnce(&'a mut Env, ChildProcessTask) -> Pin<Box<dyn Future<Output = Pid> + 'a>>,
>;
/// Extension for [`System`]
///
/// This trait provides some extension methods for `System`.
pub trait SystemEx: System {
/// Moves a file descriptor to [`MIN_INTERNAL_FD`] or larger.
///
/// This function can be used to make sure a file descriptor used by the
/// shell does not conflict with file descriptors used by the user.
/// [`MIN_INTERNAL_FD`] is the minimum file descriptor number the shell
/// uses internally. This function moves the file descriptor to a number
/// larger than or equal to [`MIN_INTERNAL_FD`].
///
/// If the given file descriptor is less than [`MIN_INTERNAL_FD`], this
/// function duplicates the file descriptor with [`System::dup`] and closes
/// the original one. Otherwise, this function does nothing.
///
/// The new file descriptor will have the CLOEXEC flag set when it is
/// dupped. Note that, if the original file descriptor has the CLOEXEC flag
/// unset and is already larger than or equal to [`MIN_INTERNAL_FD`], this
/// function will not set the CLOEXEC flag for the returned file descriptor.
///
/// This function returns the new file descriptor on success. On error, it
/// closes the original file descriptor and returns the error.
fn move_fd_internal(&mut self, from: Fd) -> Result<Fd> {
if from >= MIN_INTERNAL_FD {
return Ok(from);
}
let new = self.dup(from, MIN_INTERNAL_FD, FdFlag::CloseOnExec.into());
self.close(from).ok();
new
}
/// Tests if a file descriptor is a pipe.
fn fd_is_pipe(&self, fd: Fd) -> bool {
self.fstat(fd)
.is_ok_and(|stat| stat.r#type == FileType::Fifo)
}
/// Switches the foreground process group with SIGTTOU blocked.
///
/// This is a convenience function to change the foreground process group
/// safely. If you call [`tcsetpgrp`](System::tcsetpgrp) from a background
/// process, the process is stopped by SIGTTOU by default. To prevent this
/// effect, SIGTTOU must be blocked or ignored when `tcsetpgrp` is called.
/// This function uses [`sigmask`](System::sigmask) to block SIGTTOU before
/// calling [`tcsetpgrp`](System::tcsetpgrp) and also to restore the
/// original signal mask after `tcsetpgrp`.
///
/// Use [`tcsetpgrp_without_block`](Self::tcsetpgrp_without_block) if you
/// need to make sure the shell is in the foreground before changing the
/// foreground job.
fn tcsetpgrp_with_block(&mut self, fd: Fd, pgid: Pid) -> Result<()> {
let sigttou = self
.signal_number_from_name(signal::Name::Ttou)
.ok_or(Errno::EINVAL)?;
let mut old_mask = Vec::new();
self.sigmask(Some((SigmaskOp::Add, &[sigttou])), Some(&mut old_mask))?;
let result = self.tcsetpgrp(fd, pgid);
let result_2 = self.sigmask(Some((SigmaskOp::Set, &old_mask)), None);
result.or(result_2)
}
/// Switches the foreground process group with the default SIGTTOU settings.
///
/// This is a convenience function to ensure the shell has been in the
/// foreground and optionally change the foreground process group. This
/// function calls [`sigaction`](System::sigaction) to restore the action
/// for SIGTTOU to the default disposition (which is to suspend the shell
/// process), [`sigmask`](System::sigmask) to unblock SIGTTOU, and
/// [`tcsetpgrp`](System::tcsetpgrp) to modify the foreground job. If the
/// calling process is not in the foreground, `tcsetpgrp` will suspend the
/// process with SIGTTOU until another job-controlling process resumes it in
/// the foreground. After `tcsetpgrp` completes, this function calls
/// `sigmask` and `sigaction` to restore the original state.
///
/// Note that if `pgid` is the process group ID of the current process, this
/// function does not change the foreground job, but the process is still
/// subject to suspension if it has not been in the foreground.
///
/// Use [`tcsetpgrp_with_block`](Self::tcsetpgrp_with_block) to change the
/// job even if the current shell is not in the foreground.
fn tcsetpgrp_without_block(&mut self, fd: Fd, pgid: Pid) -> Result<()> {
let sigttou = self
.signal_number_from_name(signal::Name::Ttou)
.ok_or(Errno::EINVAL)?;
match self.sigaction(sigttou, Disposition::Default) {
Err(e) => Err(e),
Ok(old_handling) => {
let mut old_mask = Vec::new();
let result = match self
.sigmask(Some((SigmaskOp::Remove, &[sigttou])), Some(&mut old_mask))
{
Err(e) => Err(e),
Ok(()) => {
let result = self.tcsetpgrp(fd, pgid);
let result_2 = self.sigmask(Some((SigmaskOp::Set, &old_mask)), None);
result.or(result_2)
}
};
let result_2 = self.sigaction(sigttou, old_handling).map(drop);
result.or(result_2)
}
}
}
/// Returns the signal name for the signal number.
///
/// This function returns the signal name for the given signal number.
///
/// If the signal number is invalid, this function panics. It may occur if
/// the number is from a different system or was created without checking
/// the validity.
#[must_use]
fn signal_name_from_number(&self, number: signal::Number) -> signal::Name {
self.validate_signal(number.as_raw()).unwrap().0
}
/// Returns the signal number that corresponds to the exit status.
///
/// This function is basically the inverse of `impl From<signal::Number> for
/// ExitStatus`. However, this function supports not only the offset of 384
/// but also the offset of 128 and zero to accept exit statuses returned
/// from other processes.
#[must_use]
fn signal_number_from_exit_status(&self, status: ExitStatus) -> Option<signal::Number> {
[0x180, 0x80, 0].into_iter().find_map(|offset| {
let raw_number = status.0.checked_sub(offset)?;
self.validate_signal(raw_number).map(|(_, number)| number)
})
}
}
impl<T: System + ?Sized> SystemEx for T {}