diff options
| author | Theodore DeRego <tedsta@google.com> | 2016-12-01 12:01:07 -0800 |
|---|---|---|
| committer | Theodore DeRego <tedsta@google.com> | 2016-12-01 12:01:07 -0800 |
| commit | e1b752b2a1bea9c05e89e52632f2f87ee9777062 (patch) | |
| tree | 92a44d262716efb72afa488edcdba6bb2f0baa58 /src/libstd/sys/unix/process | |
| parent | 8d9d07a1ca25d003b57b08f5930ae3e9a27cd37c (diff) | |
std::process fuchsia support cleanup
Diffstat (limited to 'src/libstd/sys/unix/process')
| -rw-r--r-- | src/libstd/sys/unix/process/magenta.rs | 191 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process/process_common.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process/process_fuchsia.rs | 17 |
4 files changed, 203 insertions, 13 deletions
diff --git a/src/libstd/sys/unix/process/magenta.rs b/src/libstd/sys/unix/process/magenta.rs new file mode 100644 index 00000000000..319fbce35cd --- /dev/null +++ b/src/libstd/sys/unix/process/magenta.rs @@ -0,0 +1,191 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(non_camel_case_types)] + +use convert::TryInto; +use io; +use os::raw::c_char; +use u64; + +use libc::{c_int, c_void}; + +pub type mx_handle_t = i32; +pub type mx_vaddr_t = usize; +pub type mx_rights_t = u32; +pub type mx_status_t = i32; + +pub type mx_size_t = usize; +pub type mx_ssize_t = isize; + +pub const MX_HANDLE_INVALID: mx_handle_t = 0; + +pub type mx_time_t = u64; +pub const MX_TIME_INFINITE : mx_time_t = u64::MAX; + +pub type mx_signals_t = u32; + +pub const MX_OBJECT_SIGNAL_3 : mx_signals_t = 1 << 3; + +pub const MX_TASK_TERMINATED : mx_signals_t = MX_OBJECT_SIGNAL_3; + +pub const MX_RIGHT_SAME_RIGHTS : mx_rights_t = 1 << 31; + +pub type mx_object_info_topic_t = u32; + +pub const MX_INFO_PROCESS : mx_object_info_topic_t = 3; + +pub const MX_HND_TYPE_JOB: u32 = 6; + +pub fn mx_cvt<T>(t: T) -> io::Result<T> where T: TryInto<mx_status_t>+Copy { + if let Ok(status) = TryInto::try_into(t) { + if status < 0 { + Err(io::Error::from_raw_os_error(status)) + } else { + Ok(t) + } + } else { + Err(io::Error::last_os_error()) + } +} + +// Safe wrapper around mx_handle_t +pub struct Handle { + raw: mx_handle_t, +} + +impl Handle { + pub fn new(raw: mx_handle_t) -> Handle { + Handle { + raw: raw, + } + } + + pub fn raw(&self) -> mx_handle_t { + self.raw + } +} + +impl Drop for Handle { + fn drop(&mut self) { + unsafe { mx_cvt(mx_handle_close(self.raw)).expect("Failed to close mx_handle_t"); } + } +} + +// Common MX_INFO header +#[derive(Default)] +#[repr(C)] +pub struct mx_info_header_t { + pub topic: u32, // identifies the info struct + pub avail_topic_size: u16, // “native” size of the struct + pub topic_size: u16, // size of the returned struct (<=topic_size) + pub avail_count: u32, // number of records the kernel has + pub count: u32, // number of records returned (limited by buffer size) +} + +#[derive(Default)] +#[repr(C)] +pub struct mx_record_process_t { + pub return_code: c_int, +} + +// Returned for topic MX_INFO_PROCESS +#[derive(Default)] +#[repr(C)] +pub struct mx_info_process_t { + pub hdr: mx_info_header_t, + pub rec: mx_record_process_t, +} + +extern { + pub fn mx_task_kill(handle: mx_handle_t) -> mx_status_t; + + pub fn mx_handle_close(handle: mx_handle_t) -> mx_status_t; + + pub fn mx_handle_duplicate(handle: mx_handle_t, rights: mx_rights_t, + out: *const mx_handle_t) -> mx_handle_t; + + pub fn mx_handle_wait_one(handle: mx_handle_t, signals: mx_signals_t, timeout: mx_time_t, + pending: *mut mx_signals_t) -> mx_status_t; + + pub fn mx_object_get_info(handle: mx_handle_t, topic: u32, buffer: *mut c_void, + buffer_size: mx_size_t, actual_size: *mut mx_size_t, + avail: *mut mx_size_t) -> mx_status_t; +} + +// Handle Info entries associate a type and optional +// argument with each handle included in the process +// arguments message. +pub fn mx_hnd_info(hnd_type: u32, arg: u32) -> u32 { + (hnd_type & 0xFFFF) | ((arg & 0xFFFF) << 16) +} + +extern { + pub fn mxio_get_startup_handle(id: u32) -> mx_handle_t; +} + +// From `enum special_handles` in system/ulib/launchpad/launchpad.c +#[allow(unused)] pub const HND_LOADER_SVC: usize = 0; +// HND_EXEC_VMO = 1 +#[allow(unused)] pub const HND_SPECIAL_COUNT: usize = 2; + +#[repr(C)] +pub struct launchpad_t { + argc: u32, + envc: u32, + args: *const c_char, + args_len: usize, + env: *const c_char, + env_len: usize, + + handles: *mut mx_handle_t, + handles_info: *mut u32, + handle_count: usize, + handle_alloc: usize, + + entry: mx_vaddr_t, + base: mx_vaddr_t, + vdso_base: mx_vaddr_t, + + stack_size: usize, + + special_handles: [mx_handle_t; HND_SPECIAL_COUNT], + loader_message: bool, +} + +extern { + pub fn launchpad_create(job: mx_handle_t, name: *const c_char, + lp: *mut *mut launchpad_t) -> mx_status_t; + + pub fn launchpad_start(lp: *mut launchpad_t) -> mx_status_t; + + pub fn launchpad_destroy(lp: *mut launchpad_t); + + pub fn launchpad_arguments(lp: *mut launchpad_t, argc: c_int, + argv: *const *const c_char) -> mx_status_t; + + pub fn launchpad_environ(lp: *mut launchpad_t, envp: *const *const c_char) -> mx_status_t; + + pub fn launchpad_clone_mxio_root(lp: *mut launchpad_t) -> mx_status_t; + + pub fn launchpad_clone_mxio_cwd(lp: *mut launchpad_t) -> mx_status_t; + + pub fn launchpad_clone_fd(lp: *mut launchpad_t, fd: c_int, target_fd: c_int) -> mx_status_t; + + pub fn launchpad_transfer_fd(lp: *mut launchpad_t, fd: c_int, target_fd: c_int) -> mx_status_t; + + pub fn launchpad_elf_load(lp: *mut launchpad_t, vmo: mx_handle_t) -> mx_status_t; + + pub fn launchpad_add_vdso_vmo(lp: *mut launchpad_t) -> mx_status_t; + + pub fn launchpad_load_vdso(lp: *mut launchpad_t, vmo: mx_handle_t) -> mx_status_t; + + pub fn launchpad_vmo_from_file(filename: *const c_char) -> mx_handle_t; +} diff --git a/src/libstd/sys/unix/process/mod.rs b/src/libstd/sys/unix/process/mod.rs index 82c3971ee40..b50384d8eee 100644 --- a/src/libstd/sys/unix/process/mod.rs +++ b/src/libstd/sys/unix/process/mod.rs @@ -18,3 +18,5 @@ mod process_inner; #[cfg(target_os = "fuchsia")] #[path = "process_fuchsia.rs"] mod process_inner; +#[cfg(target_os = "fuchsia")] +mod magenta; diff --git a/src/libstd/sys/unix/process/process_common.rs b/src/libstd/sys/unix/process/process_common.rs index 24b8b61edea..3497b266340 100644 --- a/src/libstd/sys/unix/process/process_common.rs +++ b/src/libstd/sys/unix/process/process_common.rs @@ -203,15 +203,15 @@ impl Command { &self.argv } - #[cfg(not(target_os="fuchsia"))] + #[allow(dead_code)] pub fn get_cwd(&self) -> &Option<CString> { &self.cwd } - #[cfg(not(target_os="fuchsia"))] + #[allow(dead_code)] pub fn get_uid(&self) -> Option<uid_t> { self.uid } - #[cfg(not(target_os="fuchsia"))] + #[allow(dead_code)] pub fn get_gid(&self) -> Option<gid_t> { self.gid } diff --git a/src/libstd/sys/unix/process/process_fuchsia.rs b/src/libstd/sys/unix/process/process_fuchsia.rs index 77340664b6a..f0a42b12799 100644 --- a/src/libstd/sys/unix/process/process_fuchsia.rs +++ b/src/libstd/sys/unix/process/process_fuchsia.rs @@ -13,8 +13,7 @@ use libc; use mem; use ptr; -use sys::mx_cvt; -use sys::magenta::{Handle, launchpad_t, mx_handle_t}; +use sys::process::magenta::{Handle, launchpad_t, mx_handle_t}; use sys::process::process_common::*; //////////////////////////////////////////////////////////////////////////////// @@ -53,7 +52,7 @@ impl Command { unsafe fn do_exec(&mut self, stdio: ChildPipes) -> io::Result<(*mut launchpad_t, mx_handle_t)> { - use sys::magenta::*; + use sys::process::magenta::*; let job_handle = mxio_get_startup_handle(mx_hnd_info(MX_HND_TYPE_JOB, 0)); let envp = match *self.get_envp() { @@ -72,11 +71,9 @@ impl Command { // Duplicate the job handle let mut job_copy: mx_handle_t = MX_HANDLE_INVALID; - mx_cvt(mx_handle_duplicate(job_handle, MX_RIGHT_SAME_RIGHTS, - &mut job_copy as *mut mx_handle_t))?; + mx_cvt(mx_handle_duplicate(job_handle, MX_RIGHT_SAME_RIGHTS, &mut job_copy))?; // Create a launchpad - mx_cvt(launchpad_create(job_copy, self.get_argv()[0], - &mut launchpad as *mut *mut launchpad_t))?; + mx_cvt(launchpad_create(job_copy, self.get_argv()[0], &mut launchpad))?; // Set the process argv mx_cvt(launchpad_arguments(launchpad, self.get_argv().len() as i32 - 1, self.get_argv().as_ptr()))?; @@ -138,7 +135,7 @@ impl Process { } pub fn kill(&mut self) -> io::Result<()> { - use sys::magenta::*; + use sys::process::magenta::*; unsafe { mx_cvt(mx_task_kill(self.handle.raw()))?; } @@ -147,7 +144,7 @@ impl Process { pub fn wait(&mut self) -> io::Result<ExitStatus> { use default::Default; - use sys::magenta::*; + use sys::process::magenta::*; let mut proc_info: mx_info_process_t = Default::default(); let mut actual: mx_size_t = 0; @@ -171,7 +168,7 @@ impl Process { impl Drop for Process { fn drop(&mut self) { - use sys::magenta::launchpad_destroy; + use sys::process::magenta::launchpad_destroy; unsafe { launchpad_destroy(self.launchpad); } } } |
