|
| 1 | +// This file is part of the uutils util-linux package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use clap::{crate_version, Arg, ArgAction, Command as ClapCommand}; |
| 7 | +use uucore::{ |
| 8 | + error::{UResult, USimpleError}, |
| 9 | + format_usage, help_about, help_usage, |
| 10 | +}; |
| 11 | + |
| 12 | +const ABOUT: &str = help_about!("setpgid.md"); |
| 13 | +const USAGE: &str = help_usage!("setpgid.md"); |
| 14 | + |
| 15 | +#[cfg(target_family = "unix")] |
| 16 | +#[uucore::main] |
| 17 | +pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
| 18 | + use std::ffi::CString; |
| 19 | + use std::fs::File; |
| 20 | + use std::os::unix::io::AsRawFd; |
| 21 | + |
| 22 | + let matches = uu_app().try_get_matches_from(args)?; |
| 23 | + |
| 24 | + let remaining_args: Vec<String> = matches |
| 25 | + .get_many::<String>("args") |
| 26 | + .unwrap() |
| 27 | + .cloned() |
| 28 | + .collect(); |
| 29 | + |
| 30 | + if unsafe { libc::setpgid(0, 0) } != 0 { |
| 31 | + return Err(USimpleError::new( |
| 32 | + 1, |
| 33 | + format!( |
| 34 | + "failed to create new process group: {}", |
| 35 | + std::io::Error::last_os_error() |
| 36 | + ), |
| 37 | + )); |
| 38 | + } |
| 39 | + |
| 40 | + if matches.get_flag("foreground") { |
| 41 | + if let Ok(tty_file) = File::open("/dev/tty") { |
| 42 | + unsafe { |
| 43 | + libc::tcsetpgrp(tty_file.as_raw_fd(), libc::getpgrp()); |
| 44 | + } |
| 45 | + } |
| 46 | + // According to strace open("/dev/tty") failure is ignored. |
| 47 | + } |
| 48 | + |
| 49 | + let program = &remaining_args[0]; |
| 50 | + let program_args = &remaining_args[1..]; |
| 51 | + |
| 52 | + // Command line arguments can't contain NUL bytes, so unwrap() is safe here. |
| 53 | + let program_cstr = CString::new(program.as_str()).unwrap(); |
| 54 | + let mut argv = vec![program_cstr.clone()]; |
| 55 | + for arg in program_args { |
| 56 | + argv.push(CString::new(arg.as_str()).unwrap()); |
| 57 | + } |
| 58 | + |
| 59 | + let Err(e) = nix::unistd::execvp(&program_cstr, &argv); |
| 60 | + Err(USimpleError::new( |
| 61 | + 1, |
| 62 | + format!("failed to execute '{}': {}", program, e), |
| 63 | + )) |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(not(target_family = "unix"))] |
| 67 | +#[uucore::main] |
| 68 | +pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
| 69 | + let _matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?; |
| 70 | + |
| 71 | + Err(USimpleError::new( |
| 72 | + 1, |
| 73 | + "`setpgid` is unavailable on non-UNIX-like platforms.", |
| 74 | + )) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn uu_app() -> ClapCommand { |
| 78 | + ClapCommand::new(uucore::util_name()) |
| 79 | + .version(crate_version!()) |
| 80 | + .about(ABOUT) |
| 81 | + .override_usage(format_usage(USAGE)) |
| 82 | + .infer_long_args(true) |
| 83 | + .arg( |
| 84 | + Arg::new("foreground") |
| 85 | + .short('f') |
| 86 | + .long("foreground") |
| 87 | + .help("Make a foreground process group") |
| 88 | + .action(ArgAction::SetTrue), |
| 89 | + ) |
| 90 | + .arg( |
| 91 | + Arg::new("args") |
| 92 | + .hide_short_help(true) |
| 93 | + .hide_long_help(true) |
| 94 | + .required(true) |
| 95 | + .action(ArgAction::Append) |
| 96 | + .num_args(1..) |
| 97 | + .trailing_var_arg(true), |
| 98 | + ) |
| 99 | +} |
0 commit comments