1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 21:17:46 +00:00

Merge pull request #607 from jbcrail/fix-nohup

Fix nohup.
This commit is contained in:
Heather 2015-05-19 07:14:40 +03:00
commit ff5310a118

View file

@ -1,5 +1,5 @@
#![crate_name = "nohup"]
#![feature(collections, core, old_io, os, old_path, rustc_private, std_misc)]
#![feature(rustc_private)]
/*
* This file is part of the uutils coreutils package.
@ -13,16 +13,18 @@
extern crate getopts;
extern crate libc;
use getopts::{optflag, getopts, usage};
use std::ffi::CString;
use std::old_io::stdio::{stdin_raw, stdout_raw, stderr_raw};
use std::old_io::{File, Open, Read, Append, Write};
use std::os::unix::prelude::*;
use getopts::{getopts, optflag, usage};
use libc::c_char;
use libc::funcs::posix88::unistd::{dup2, execvp};
use libc::consts::os::posix88::SIGHUP;
use libc::funcs::posix01::signal::signal;
use libc::funcs::posix88::unistd::{dup2, execvp, isatty};
use libc::consts::os::posix01::SIG_IGN;
use libc::consts::os::posix88::SIGHUP;
use std::env;
use std::ffi::CString;
use std::fs::{File, OpenOptions};
use std::io::{Error, Write};
use std::os::unix::prelude::*;
use std::path::{Path, PathBuf};
#[path = "../common/util.rs"] #[macro_use] mod util;
#[path = "../common/c_types.rs"] mod c_types;
@ -46,21 +48,21 @@ pub fn uumain(args: Vec<String>) -> i32 {
optflag("V", "version", "Show version and exit"),
];
let opts = match getopts(args.tail(), &options) {
let opts = match getopts(&args[1..], &options) {
Ok(m) => m,
Err(f) => {
show_error!("{}", f);
show_usage(program.as_slice(), &options);
show_usage(program, &options);
return 1
}
};
if opts.opt_present("V") { version(); return 0 }
if opts.opt_present("h") { show_usage(program.as_slice(), &options); return 0 }
if opts.opt_present("h") { show_usage(program, &options); return 0 }
if opts.free.len() == 0 {
show_error!("Missing operand: COMMAND");
println!("Try `{} --help` for more information.", program.as_slice());
println!("Try `{} --help` for more information.", program);
return 1
}
replace_fds();
@ -76,19 +78,19 @@ pub fn uumain(args: Vec<String>) -> i32 {
}
fn replace_fds() {
let replace_stdin = stdin_raw().isatty();
let replace_stdout = stdout_raw().isatty();
let replace_stderr = stderr_raw().isatty();
let replace_stdin = unsafe { isatty(libc::STDIN_FILENO) == 1 };
let replace_stdout = unsafe { isatty(libc::STDOUT_FILENO) == 1 };
let replace_stderr = unsafe { isatty(libc::STDERR_FILENO) == 1 };
if replace_stdin {
let new_stdin = match File::open_mode(&Path::new("/dev/null"), Open, Read) {
let new_stdin = match File::open(Path::new("/dev/null")) {
Ok(t) => t,
Err(e) => {
crash!(2, "Cannot replace STDIN: {}", e)
}
};
if unsafe { dup2(new_stdin.as_raw_fd(), 0) } != 0 {
crash!(2, "Cannot replace STDIN: {}", std::old_io::IoError::last_error())
crash!(2, "Cannot replace STDIN: {}", Error::last_os_error())
}
}
@ -97,33 +99,33 @@ fn replace_fds() {
let fd = new_stdout.as_raw_fd();
if unsafe { dup2(fd, 1) } != 1 {
crash!(2, "Cannot replace STDOUT: {}", std::old_io::IoError::last_error())
crash!(2, "Cannot replace STDOUT: {}", Error::last_os_error())
}
}
if replace_stderr {
if unsafe { dup2(1, 2) } != 2 {
crash!(2, "Cannot replace STDERR: {}", std::old_io::IoError::last_error())
crash!(2, "Cannot replace STDERR: {}", Error::last_os_error())
}
}
}
fn find_stdout() -> File {
match File::open_mode(&Path::new("nohup.out"), Append, Write) {
match OpenOptions::new().write(true).create(true).append(true).open(Path::new("nohup.out")) {
Ok(t) => {
show_warning!("Output is redirected to: nohup.out");
t
},
Err(e) => {
let home = match std::os::getenv("HOME") {
None => crash!(2, "Cannot replace STDOUT: {}", e),
Some(h) => h
let home = match env::var("HOME") {
Err(_) => crash!(2, "Cannot replace STDOUT: {}", e),
Ok(h) => h
};
let mut homeout = Path::new(home);
let mut homeout = PathBuf::from(home);
homeout.push("nohup.out");
match File::open_mode(&homeout, Append, Write) {
match OpenOptions::new().write(true).create(true).append(true).open(&homeout) {
Ok(t) => {
show_warning!("Output is redirected to: {}", homeout.display());
show_warning!("Output is redirected to: {:?}", homeout);
t
},
Err(e) => {