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

Vxworks / Unix deduplication #77666

Merged
merged 18 commits into from
Oct 16, 2020
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
15 changes: 15 additions & 0 deletions library/std/src/os/vxworks/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ pub trait MetadataExt {
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_atime(&self) -> i64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_atime_nsec(&self) -> i64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_mtime(&self) -> i64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_mtime_nsec(&self) -> i64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_ctime(&self) -> i64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_ctime_nsec(&self) -> i64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_blksize(&self) -> u64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_blocks(&self) -> u64;
Expand Down Expand Up @@ -66,12 +72,21 @@ impl MetadataExt for Metadata {
fn st_atime(&self) -> i64 {
self.as_inner().as_inner().st_atime as i64
}
fn st_atime_nsec(&self) -> i64 {
0
}
fn st_mtime(&self) -> i64 {
self.as_inner().as_inner().st_mtime as i64
}
fn st_mtime_nsec(&self) -> i64 {
0
}
fn st_ctime(&self) -> i64 {
self.as_inner().as_inner().st_ctime as i64
}
fn st_ctime_nsec(&self) -> i64 {
0
}
fn st_blksize(&self) -> u64 {
self.as_inner().as_inner().st_blksize as u64
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/os/vxworks/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ use crate::os::raw::c_ulong;

#[stable(feature = "pthread_t", since = "1.8.0")]
pub type pthread_t = c_ulong;

#[stable(feature = "raw_ext", since = "1.1.0")]
pub use libc::{blkcnt_t, blksize_t, dev_t, ino_t, mode_t, nlink_t, off_t, time_t};
3 changes: 2 additions & 1 deletion library/std/src/sys/unix/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl DoubleEndedIterator for Args {
target_os = "haiku",
target_os = "l4re",
target_os = "fuchsia",
target_os = "redox"
target_os = "redox",
target_os = "vxworks"
))]
mod imp {
use super::Args;
Expand Down
7 changes: 7 additions & 0 deletions library/std/src/sys/unix/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ pub trait MetadataExt {
/// ```
#[stable(feature = "metadata_ext", since = "1.1.0")]
fn blocks(&self) -> u64;
#[cfg(target_os = "vxworks")]
#[stable(feature = "metadata_ext", since = "1.1.0")]
fn attrib(&self) -> u8;
}

#[stable(feature = "metadata_ext", since = "1.1.0")]
Expand Down Expand Up @@ -702,6 +705,10 @@ impl MetadataExt for fs::Metadata {
fn blocks(&self) -> u64 {
self.st_blocks()
}
#[cfg(target_os = "vxworks")]
fn attrib(&self) -> u8 {
self.st_attrib()
}
}

/// Unix-specific extensions for [`fs::FileType`].
Expand Down
24 changes: 20 additions & 4 deletions library/std/src/sys/unix/ext/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ pub trait CommandExt {
/// `setuid` call in the child process. Failure in the `setuid`
/// call will cause the spawn to fail.
#[stable(feature = "rust1", since = "1.0.0")]
fn uid(&mut self, id: u32) -> &mut process::Command;
fn uid(
&mut self,
#[cfg(not(target_os = "vxworks"))] id: u32,
#[cfg(target_os = "vxworks")] id: u16,
) -> &mut process::Command;

/// Similar to `uid`, but sets the group ID of the child process. This has
/// the same semantics as the `uid` field.
#[stable(feature = "rust1", since = "1.0.0")]
fn gid(&mut self, id: u32) -> &mut process::Command;
fn gid(
&mut self,
#[cfg(not(target_os = "vxworks"))] id: u32,
#[cfg(target_os = "vxworks")] id: u16,
) -> &mut process::Command;

/// Schedules a closure to be run just before the `exec` function is
/// invoked.
Expand Down Expand Up @@ -115,12 +123,20 @@ pub trait CommandExt {

#[stable(feature = "rust1", since = "1.0.0")]
impl CommandExt for process::Command {
fn uid(&mut self, id: u32) -> &mut process::Command {
fn uid(
&mut self,
#[cfg(not(target_os = "vxworks"))] id: u32,
#[cfg(target_os = "vxworks")] id: u16,
) -> &mut process::Command {
self.as_inner_mut().uid(id);
self
}

fn gid(&mut self, id: u32) -> &mut process::Command {
fn gid(
&mut self,
#[cfg(not(target_os = "vxworks"))] id: u32,
#[cfg(target_os = "vxworks")] id: u16,
) -> &mut process::Command {
self.as_inner_mut().gid(id);
self
}
Expand Down
6 changes: 4 additions & 2 deletions library/std/src/sys/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ impl FileDesc {
target_os = "l4re",
target_os = "linux",
target_os = "haiku",
target_os = "redox"
target_os = "redox",
target_os = "vxworks"
)))]
pub fn set_cloexec(&self) -> io::Result<()> {
unsafe {
Expand All @@ -217,7 +218,8 @@ impl FileDesc {
target_os = "l4re",
target_os = "linux",
target_os = "haiku",
target_os = "redox"
target_os = "redox",
target_os = "vxworks"
))]
pub fn set_cloexec(&self) -> io::Result<()> {
unsafe {
Expand Down
56 changes: 49 additions & 7 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,20 +297,38 @@ impl FileAttr {

#[cfg(not(target_os = "netbsd"))]
impl FileAttr {
#[cfg(not(target_os = "vxworks"))]
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_mtime as libc::time_t,
tv_nsec: self.stat.st_mtime_nsec as _,
}))
}

#[cfg(target_os = "vxworks")]
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_mtime as libc::time_t,
tv_nsec: 0,
}))
}

#[cfg(not(target_os = "vxworks"))]
pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_atime as libc::time_t,
tv_nsec: self.stat.st_atime_nsec as _,
}))
}

#[cfg(target_os = "vxworks")]
pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_atime as libc::time_t,
tv_nsec: 0,
}))
}

#[cfg(any(
target_os = "freebsd",
target_os = "openbsd",
Expand Down Expand Up @@ -535,12 +553,22 @@ impl DirEntry {
lstat(&self.path())
}

#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "haiku"))]
#[cfg(any(
target_os = "solaris",
target_os = "illumos",
target_os = "haiku",
target_os = "vxworks"
))]
pub fn file_type(&self) -> io::Result<FileType> {
lstat(&self.path()).map(|m| m.file_type())
}

#[cfg(not(any(target_os = "solaris", target_os = "illumos", target_os = "haiku")))]
#[cfg(not(any(
target_os = "solaris",
target_os = "illumos",
target_os = "haiku",
target_os = "vxworks"
)))]
pub fn file_type(&self) -> io::Result<FileType> {
match self.entry.d_type {
libc::DT_CHR => Ok(FileType { mode: libc::S_IFCHR }),
Expand All @@ -565,7 +593,8 @@ impl DirEntry {
target_os = "haiku",
target_os = "l4re",
target_os = "fuchsia",
target_os = "redox"
target_os = "redox",
target_os = "vxworks"
))]
pub fn ino(&self) -> u64 {
self.entry.d_ino as u64
Expand Down Expand Up @@ -603,7 +632,8 @@ impl DirEntry {
target_os = "linux",
target_os = "emscripten",
target_os = "l4re",
target_os = "haiku"
target_os = "haiku",
target_os = "vxworks"
))]
fn name_bytes(&self) -> &[u8] {
unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()).to_bytes() }
Expand Down Expand Up @@ -901,13 +931,25 @@ impl fmt::Debug for File {
Some(PathBuf::from(OsString::from_vec(buf)))
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
#[cfg(target_os = "vxworks")]
fn get_path(fd: c_int) -> Option<PathBuf> {
let mut buf = vec![0; libc::PATH_MAX as usize];
let n = unsafe { libc::ioctl(fd, libc::FIOGETNAME, buf.as_ptr()) };
if n == -1 {
return None;
}
let l = buf.iter().position(|&c| c == 0).unwrap();
buf.truncate(l as usize);
Some(PathBuf::from(OsString::from_vec(buf)))
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "vxworks")))]
fn get_path(_fd: c_int) -> Option<PathBuf> {
// FIXME(#24570): implement this for other Unix platforms
None
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "vxworks"))]
fn get_mode(fd: c_int) -> Option<(bool, bool)> {
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
if mode == -1 {
Expand All @@ -921,7 +963,7 @@ impl fmt::Debug for File {
}
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "vxworks")))]
fn get_mode(_fd: c_int) -> Option<(bool, bool)> {
// FIXME(#24570): implement this for other Unix platforms
None
Expand Down
10 changes: 8 additions & 2 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Socket {
}
}

#[cfg(not(target_os = "vxworks"))]
pub fn new_pair(fam: c_int, ty: c_int) -> io::Result<(Socket, Socket)> {
unsafe {
let mut fds = [0, 0];
Expand All @@ -98,6 +99,11 @@ impl Socket {
}
}

#[cfg(target_os = "vxworks")]
pub fn new_pair(_fam: c_int, _ty: c_int) -> io::Result<(Socket, Socket)> {
unimplemented!()
}

pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
self.set_nonblocking(true)?;
let r = unsafe {
Expand Down Expand Up @@ -366,7 +372,7 @@ impl IntoInner<c_int> for Socket {
// res_init unconditionally, we call it only when we detect we're linking
// against glibc version < 2.26. (That is, when we both know its needed and
// believe it's thread-safe).
#[cfg(target_env = "gnu")]
#[cfg(all(target_env = "gnu", not(target_os = "vxworks")))]
fn on_resolver_failure() {
use crate::sys;

Expand All @@ -378,5 +384,5 @@ fn on_resolver_failure() {
}
}

#[cfg(not(target_env = "gnu"))]
#[cfg(any(not(target_env = "gnu"), target_os = "vxworks"))]
fn on_resolver_failure() {}
35 changes: 30 additions & 5 deletions library/std/src/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cfg_if::cfg_if! {
}

extern "C" {
#[cfg(not(target_os = "dragonfly"))]
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks")))]
#[cfg_attr(
any(
target_os = "linux",
Expand Down Expand Up @@ -67,18 +67,28 @@ extern "C" {
}

/// Returns the platform-specific value of errno
#[cfg(not(target_os = "dragonfly"))]
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks")))]
pub fn errno() -> i32 {
unsafe { (*errno_location()) as i32 }
}

/// Sets the platform-specific value of errno
#[cfg(all(not(target_os = "linux"), not(target_os = "dragonfly")))] // needed for readdir and syscall!
#[cfg(all(not(target_os = "linux"), not(target_os = "dragonfly"), not(target_os = "vxworks")))] // needed for readdir and syscall!
#[allow(dead_code)] // but not all target cfgs actually end up using it
pub fn set_errno(e: i32) {
unsafe { *errno_location() = e as c_int }
}

#[cfg(target_os = "vxworks")]
pub fn errno() -> i32 {
unsafe { libc::errnoGet() }
}

#[cfg(target_os = "vxworks")]
pub fn set_errno(e: i32) {
unsafe { libc::errnoSet(e as c_int) };
}

#[cfg(target_os = "dragonfly")]
pub fn errno() -> i32 {
extern "C" {
Expand Down Expand Up @@ -439,6 +449,19 @@ pub fn current_exe() -> io::Result<PathBuf> {
Err(io::Error::new(ErrorKind::Other, "Not yet implemented!"))
}

#[cfg(target_os = "vxworks")]
pub fn current_exe() -> io::Result<PathBuf> {
#[cfg(test)]
use realstd::env;

#[cfg(not(test))]
use crate::env;

let exe_path = env::args().next().unwrap();
let path = path::Path::new(&exe_path);
path.canonicalize()
}

pub struct Env {
iter: vec::IntoIter<(OsString, OsString)>,
_dont_send_or_sync_me: PhantomData<*mut ()>,
Expand Down Expand Up @@ -568,7 +591,8 @@ pub fn home_dir() -> Option<PathBuf> {
target_os = "android",
target_os = "ios",
target_os = "emscripten",
target_os = "redox"
target_os = "redox",
target_os = "vxworks"
))]
unsafe fn fallback() -> Option<OsString> {
None
Expand All @@ -577,7 +601,8 @@ pub fn home_dir() -> Option<PathBuf> {
target_os = "android",
target_os = "ios",
target_os = "emscripten",
target_os = "redox"
target_os = "redox",
target_os = "vxworks"
)))]
unsafe fn fallback() -> Option<OsString> {
let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ cfg_if::cfg_if! {
// fuchsia doesn't have /dev/null
} else if #[cfg(target_os = "redox")] {
const DEV_NULL: &str = "null:\0";
} else if #[cfg(target_os = "vxworks")] {
const DEV_NULL: &str = "/null\0";
} else {
const DEV_NULL: &str = "/dev/null\0";
}
Expand All @@ -48,7 +50,7 @@ cfg_if::cfg_if! {
raw[bit / 8] |= 1 << (bit % 8);
return 0;
}
} else {
} else if #[cfg(not(target_os = "vxworks"))] {
pub use libc::{sigemptyset, sigaddset};
}
}
Expand Down
Loading