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

Roll std::run into std::io::process #12380

Merged
merged 1 commit into from
Feb 24, 2014
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
27 changes: 15 additions & 12 deletions src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
// except according to those terms.

use std::os;
use std::run;
use std::str;
use std::io::process::ProcessExit;
use std::io::process::{ProcessExit, Process, ProcessConfig, ProcessOutput};

#[cfg(target_os = "win32")]
fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
Expand Down Expand Up @@ -49,17 +48,19 @@ pub fn run(lib_path: &str,
input: Option<~str>) -> Option<Result> {

let env = env + target_env(lib_path, prog);
let mut opt_process = run::Process::new(prog, args, run::ProcessOptions {
env: Some(env),
.. run::ProcessOptions::new()
let mut opt_process = Process::configure(ProcessConfig {
program: prog,
args: args,
env: Some(env.as_slice()),
.. ProcessConfig::new()
});

match opt_process {
Ok(ref mut process) => {
for input in input.iter() {
process.input().write(input.as_bytes()).unwrap();
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
}
let run::ProcessOutput { status, output, error } = process.finish_with_output();
let ProcessOutput { status, output, error } = process.wait_with_output();

Some(Result {
status: status,
Expand All @@ -75,18 +76,20 @@ pub fn run_background(lib_path: &str,
prog: &str,
args: &[~str],
env: ~[(~str, ~str)],
input: Option<~str>) -> Option<run::Process> {
input: Option<~str>) -> Option<Process> {

let env = env + target_env(lib_path, prog);
let opt_process = run::Process::new(prog, args, run::ProcessOptions {
env: Some(env),
.. run::ProcessOptions::new()
let opt_process = Process::configure(ProcessConfig {
program: prog,
args: args,
env: Some(env.as_slice()),
.. ProcessConfig::new()
});

match opt_process {
Ok(mut process) => {
for input in input.iter() {
process.input().write(input.as_bytes()).unwrap();
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
}

Some(process)
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
stdout: out,
stderr: err,
cmdline: cmdline};
process.force_destroy().unwrap();
process.signal_kill().unwrap();
}

_=> {
Expand Down
10 changes: 5 additions & 5 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ impl<'a, T:Send +
#[test]
#[cfg(not(target_os="android"))] // FIXME(#10455)
fn test() {
use std::{os, run};
use std::io::fs;
use std::os;
use std::io::{fs, Process};
use std::str::from_utf8_owned;

// Create a path to a new file 'filename' in the directory in which
Expand Down Expand Up @@ -499,9 +499,9 @@ fn test() {
prep.exec(proc(_exe) {
let out = make_path(~"foo.o");
// FIXME (#9639): This needs to handle non-utf8 paths
run::process_status("gcc", [pth.as_str().unwrap().to_owned(),
~"-o",
out.as_str().unwrap().to_owned()]).unwrap();
Process::status("gcc", [pth.as_str().unwrap().to_owned(),
~"-o",
out.as_str().unwrap().to_owned()]).unwrap();

let _proof_of_concept = subcx.prep("subfn");
// Could run sub-rules inside here.
Expand Down
3 changes: 3 additions & 0 deletions src/libnative/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ impl rtio::IoFactory for IoFactory {
io.move_iter().map(|p| p.map(|p| ~p as ~RtioPipe)).collect())
})
}
fn kill(&mut self, pid: libc::pid_t, signum: int) -> IoResult<()> {
process::Process::kill(pid, signum)
}
fn pipe_open(&mut self, fd: c_int) -> IoResult<~RtioPipe> {
Ok(~file::FileDesc::new(fd, true) as ~RtioPipe)
}
Expand Down
98 changes: 62 additions & 36 deletions src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,33 @@ impl Process {
-> Result<(Process, ~[Option<file::FileDesc>]), io::IoError>
{
// right now we only handle stdin/stdout/stderr.
if config.io.len() > 3 {
if config.extra_io.len() > 0 {
return Err(super::unimpl());
}

fn get_io(io: &[p::StdioContainer],
ret: &mut ~[Option<file::FileDesc>],
idx: uint) -> (Option<os::Pipe>, c_int) {
if idx >= io.len() { return (None, -1); }
ret.push(None);
match io[idx] {
p::Ignored => (None, -1),
p::InheritFd(fd) => (None, fd),
fn get_io(io: p::StdioContainer, ret: &mut ~[Option<file::FileDesc>])
-> (Option<os::Pipe>, c_int)
{
match io {
p::Ignored => { ret.push(None); (None, -1) }
p::InheritFd(fd) => { ret.push(None); (None, fd) }
p::CreatePipe(readable, _writable) => {
let pipe = os::pipe();
let (theirs, ours) = if readable {
(pipe.input, pipe.out)
} else {
(pipe.out, pipe.input)
};
ret[idx] = Some(file::FileDesc::new(ours, true));
ret.push(Some(file::FileDesc::new(ours, true)));
(Some(pipe), theirs)
}
}
}

let mut ret_io = ~[];
let (in_pipe, in_fd) = get_io(config.io, &mut ret_io, 0);
let (out_pipe, out_fd) = get_io(config.io, &mut ret_io, 1);
let (err_pipe, err_fd) = get_io(config.io, &mut ret_io, 2);
let (in_pipe, in_fd) = get_io(config.stdin, &mut ret_io);
let (out_pipe, out_fd) = get_io(config.stdout, &mut ret_io);
let (err_pipe, err_fd) = get_io(config.stderr, &mut ret_io);

let env = config.env.map(|a| a.to_owned());
let cwd = config.cwd.map(|a| Path::new(a));
Expand All @@ -115,6 +113,10 @@ impl Process {
Err(e) => Err(e)
}
}

pub fn kill(pid: libc::pid_t, signum: int) -> IoResult<()> {
unsafe { killpid(pid, signum) }
}
}

impl rtio::RtioProcess for Process {
Expand Down Expand Up @@ -144,34 +146,58 @@ impl rtio::RtioProcess for Process {
None => {}
}
return unsafe { killpid(self.pid, signum) };
}
}

#[cfg(windows)]
unsafe fn killpid(pid: pid_t, signal: int) -> Result<(), io::IoError> {
match signal {
io::process::PleaseExitSignal | io::process::MustDieSignal => {
let ret = libc::TerminateProcess(pid as libc::HANDLE, 1);
super::mkerr_winbool(ret)
}
_ => Err(io::IoError {
impl Drop for Process {
fn drop(&mut self) {
free_handle(self.handle);
}
}

#[cfg(windows)]
unsafe fn killpid(pid: pid_t, signal: int) -> Result<(), io::IoError> {
let handle = libc::OpenProcess(libc::PROCESS_TERMINATE |
libc::PROCESS_QUERY_INFORMATION,
libc::FALSE, pid as libc::DWORD);
if handle.is_null() {
return Err(super::last_error())
}
let ret = match signal {
// test for existence on signal 0
0 => {
let mut status = 0;
let ret = libc::GetExitCodeProcess(handle, &mut status);
if ret == 0 {
Err(super::last_error())
} else if status != libc::STILL_ACTIVE {
Err(io::IoError {
kind: io::OtherIoError,
desc: "unsupported signal on windows",
desc: "process no longer alive",
detail: None,
})
} else {
Ok(())
}
}

#[cfg(not(windows))]
unsafe fn killpid(pid: pid_t, signal: int) -> Result<(), io::IoError> {
let r = libc::funcs::posix88::signal::kill(pid, signal as c_int);
super::mkerr_libc(r)
io::process::PleaseExitSignal | io::process::MustDieSignal => {
let ret = libc::TerminateProcess(handle, 1);
super::mkerr_winbool(ret)
}
}
_ => Err(io::IoError {
kind: io::OtherIoError,
desc: "unsupported signal on windows",
detail: None,
})
};
let _ = libc::CloseHandle(handle);
return ret;
}

impl Drop for Process {
fn drop(&mut self) {
free_handle(self.handle);
}
#[cfg(not(windows))]
unsafe fn killpid(pid: pid_t, signal: int) -> Result<(), io::IoError> {
let r = libc::funcs::posix88::signal::kill(pid, signal as c_int);
super::mkerr_libc(r)
}

struct SpawnProcessResult {
Expand Down Expand Up @@ -536,10 +562,10 @@ fn spawn_process_os(config: p::ProcessConfig,
if !envp.is_null() {
set_environ(envp);
}
});
with_argv(config.program, config.args, |argv| {
let _ = execvp(*argv, argv);
fail(&mut output);
with_argv(config.program, config.args, |argv| {
let _ = execvp(*argv, argv);
fail(&mut output);
})
})
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/librustc/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::io::fs;
use std::io;
use std::libc;
use std::os;
use std::run::{ProcessOptions, Process, ProcessOutput};
use std::io::process::{ProcessConfig, Process, ProcessOutput};
use std::str;
use std::raw;
use extra::tempfile::TempDir;
Expand All @@ -44,16 +44,19 @@ fn run_ar(sess: Session, args: &str, cwd: Option<&Path>,
let mut args = ~[args.to_owned()];
let mut paths = paths.iter().map(|p| p.as_str().unwrap().to_owned());
args.extend(&mut paths);
let mut opts = ProcessOptions::new();
opts.dir = cwd;
debug!("{} {}", ar, args.connect(" "));
match cwd {
Some(p) => { debug!("inside {}", p.display()); }
None => {}
}
match Process::new(ar, args.as_slice(), opts) {
match Process::configure(ProcessConfig {
program: ar.as_slice(),
args: args.as_slice(),
cwd: cwd.map(|a| &*a),
.. ProcessConfig::new()
}) {
Ok(mut prog) => {
let o = prog.finish_with_output();
let o = prog.wait_with_output();
if !o.status.success() {
sess.err(format!("{} {} failed with: {}", ar, args.connect(" "),
o.status));
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use std::c_str::ToCStr;
use std::char;
use std::os::consts::{macos, freebsd, linux, android, win32};
use std::ptr;
use std::run;
use std::str;
use std::io;
use std::io::Process;
use std::io::fs;
use flate;
use serialize::hex::ToHex;
Expand Down Expand Up @@ -101,8 +101,8 @@ pub mod write {
use syntax::abi;

use std::c_str::ToCStr;
use std::io::Process;
use std::libc::{c_uint, c_int};
use std::run;
use std::str;

// On android, we by default compile for armv7 processors. This enables
Expand Down Expand Up @@ -333,7 +333,7 @@ pub mod write {
assembly.as_str().unwrap().to_owned()];

debug!("{} '{}'", cc, args.connect("' '"));
match run::process_output(cc, args) {
match Process::output(cc, args) {
Ok(prog) => {
if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc, prog.status));
Expand Down Expand Up @@ -1033,7 +1033,7 @@ fn link_natively(sess: Session, dylib: bool, obj_filename: &Path,
// Invoke the system linker
debug!("{} {}", cc_prog, cc_args.connect(" "));
let prog = time(sess.time_passes(), "running linker", (), |()|
run::process_output(cc_prog, cc_args));
Process::output(cc_prog, cc_args));
match prog {
Ok(prog) => {
if !prog.status.success() {
Expand All @@ -1054,7 +1054,7 @@ fn link_natively(sess: Session, dylib: bool, obj_filename: &Path,
// the symbols
if sess.targ_cfg.os == abi::OsMacos && sess.opts.debuginfo {
// FIXME (#9639): This needs to handle non-utf8 paths
match run::process_status("dsymutil",
match Process::status("dsymutil",
[out_filename.as_str().unwrap().to_owned()]) {
Ok(..) => {}
Err(e) => {
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
// except according to those terms.

use std::cell::RefCell;
use collections::HashSet;
use std::io::Process;
use std::local_data;
use std::os;
use std::run;
use std::str;

use collections::HashSet;
use testing;
use extra::tempfile::TempDir;
use rustc::back::link;
Expand Down Expand Up @@ -126,7 +126,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool)
driver::compile_input(sess, cfg, &input, &out, &None);

let exe = outdir.path().join("rust_out");
let out = run::process_output(exe.as_str().unwrap(), []);
let out = Process::output(exe.as_str().unwrap(), []);
match out {
Err(e) => fail!("couldn't run the test: {}", e),
Ok(out) => {
Expand Down
14 changes: 13 additions & 1 deletion src/librustuv/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl Process {
-> Result<(~Process, ~[Option<PipeWatcher>]), UvError>
{
let cwd = config.cwd.map(|s| s.to_c_str());
let io = config.io;
let mut io = ~[config.stdin, config.stdout, config.stderr];
for slot in config.extra_io.iter() {
io.push(*slot);
}
let mut stdio = vec::with_capacity::<uvll::uv_stdio_container_t>(io.len());
let mut ret_io = vec::with_capacity(io.len());
unsafe {
Expand Down Expand Up @@ -105,6 +108,15 @@ impl Process {
Err(e) => Err(e),
}
}

pub fn kill(pid: libc::pid_t, signum: int) -> Result<(), UvError> {
match unsafe {
uvll::uv_kill(pid as libc::c_int, signum as libc::c_int)
} {
0 => Ok(()),
n => Err(UvError(n))
}
}
}

extern fn on_exit(handle: *uvll::uv_process_t,
Expand Down
4 changes: 4 additions & 0 deletions src/librustuv/uvio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ impl IoFactory for UvIoFactory {
}
}

fn kill(&mut self, pid: libc::pid_t, signum: int) -> Result<(), IoError> {
Process::kill(pid, signum).map_err(uv_error_to_io_error)
}

fn unix_bind(&mut self, path: &CString) -> Result<~rtio::RtioUnixListener, IoError>
{
match PipeListener::bind(self, path) {
Expand Down
Loading