From 78c6d0b3833e591d1432787cc19dfe45810e5642 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 23 Sep 2014 15:40:31 +0200 Subject: [PATCH] FreeBSD support --- src/chroot/chroot.rs | 2 ++ src/common/c_types.rs | 10 +++++++++- src/common/signals.rs | 1 + src/common/utmpx.rs | 33 +++++++++++++++++++++++++++++++++ src/hostname/hostname.rs | 2 ++ src/id/id.rs | 1 + src/uptime/uptime.rs | 7 +++++++ src/users/users.rs | 7 +++++++ 8 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/chroot/chroot.rs b/src/chroot/chroot.rs index fa2e3d768..555ff6f5f 100644 --- a/src/chroot/chroot.rs +++ b/src/chroot/chroot.rs @@ -25,6 +25,7 @@ extern { } #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] extern { fn setgroups(size: libc::c_int, list: *const libc::gid_t) -> libc::c_int; } @@ -151,6 +152,7 @@ fn set_main_group(group: &str) { } #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] fn set_groups(groups: Vec) -> libc::c_int { unsafe { setgroups(groups.len() as libc::c_int, diff --git a/src/common/c_types.rs b/src/common/c_types.rs index 94124cfc1..465d163db 100644 --- a/src/common/c_types.rs +++ b/src/common/c_types.rs @@ -8,7 +8,12 @@ use self::libc::{ uid_t, gid_t, }; -#[cfg(target_os = "macos")] use self::libc::{int32_t, time_t}; +#[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] +use self::libc::time_t; +#[cfg(target_os = "macos")] +use self::libc::int32_t; + use self::libc::funcs::posix88::unistd::getgroups; use std::vec::Vec; @@ -18,6 +23,7 @@ use std::ptr::{null_mut, read}; use std::string::raw::from_buf; #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] #[repr(C)] pub struct c_passwd { pub pw_name: *const c_char, /* user name */ @@ -45,6 +51,7 @@ pub struct c_passwd { } #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] #[repr(C)] pub struct utsname { pub sysname: [c_char, ..256], @@ -164,6 +171,7 @@ pub fn get_group_list(name: *const c_char, gid: gid_t) -> Vec { } #[cfg(target_os = "linux")] +#[cfg(target_os = "freebsd")] #[inline(always)] unsafe fn get_group_list_internal(name: *const c_char, gid: gid_t, groups: *mut gid_t, grcnt: *mut c_int) -> c_int { getgrouplist(name, gid, groups, grcnt) diff --git a/src/common/signals.rs b/src/common/signals.rs index 828f7ad3a..ac0542751 100644 --- a/src/common/signals.rs +++ b/src/common/signals.rs @@ -105,6 +105,7 @@ No Name Default Action Description */ #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] pub static ALL_SIGNALS:[Signal<'static>, ..31] = [ Signal{ name: "HUP", value:1 }, Signal{ name: "INT", value:2 }, diff --git a/src/common/utmpx.rs b/src/common/utmpx.rs index 08e96ee90..3acf2adb5 100644 --- a/src/common/utmpx.rs +++ b/src/common/utmpx.rs @@ -91,3 +91,36 @@ mod utmpx { } } +#[cfg(target_os = "freebsd")] +mod utmpx { + use super::libc; + + pub static DEFAULT_FILE : &'static str = ""; + + pub static UT_LINESIZE : uint = 16; + pub static UT_NAMESIZE : uint = 32; + pub static UT_IDSIZE : uint = 8; + pub static UT_HOSTSIZE : uint = 128; + + pub static EMPTY : libc::c_short = 0; + pub static BOOT_TIME : libc::c_short = 1; + pub static OLD_TIME : libc::c_short = 2; + pub static NEW_TIME : libc::c_short = 3; + pub static USER_PROCESS : libc::c_short = 4; + pub static INIT_PROCESS : libc::c_short = 5; + pub static LOGIN_PROCESS : libc::c_short = 6; + pub static DEAD_PROCESS : libc::c_short = 7; + pub static SHUTDOWN_TIME : libc::c_short = 8; + + #[repr(C)] + pub struct c_utmp { + pub ut_type : libc::c_short, + pub ut_tv : libc::timeval, + pub ut_id : [libc::c_char, ..UT_IDSIZE], + pub ut_pid : libc::pid_t, + pub ut_user : [libc::c_char, ..UT_NAMESIZE], + pub ut_line : [libc::c_char, ..UT_LINESIZE], + pub ut_host : [libc::c_char, ..UT_HOSTSIZE], + pub ut_spare : [libc::c_char, ..64], + } +} diff --git a/src/hostname/hostname.rs b/src/hostname/hostname.rs index 56dc60f3e..063a5773a 100644 --- a/src/hostname/hostname.rs +++ b/src/hostname/hostname.rs @@ -32,6 +32,7 @@ extern { } #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] extern { fn sethostname(name: *const libc::c_char, namelen: libc::c_int) -> libc::c_int; } @@ -148,6 +149,7 @@ fn xgethostname() -> String { } #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] fn xsethostname(name: &str) { let vec_name: Vec = name.bytes().map(|c| c as libc::c_char).collect(); diff --git a/src/id/id.rs b/src/id/id.rs index 5a22a5bad..599ff4a78 100644 --- a/src/id/id.rs +++ b/src/id/id.rs @@ -249,6 +249,7 @@ fn pretty(possible_pw: Option) { } #[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] fn pline(possible_pw: Option) { let pw = if possible_pw.is_none() { unsafe { read(getpwuid(getuid())) } diff --git a/src/uptime/uptime.rs b/src/uptime/uptime.rs index 283e6cb82..feb151fdd 100644 --- a/src/uptime/uptime.rs +++ b/src/uptime/uptime.rs @@ -43,9 +43,16 @@ extern { fn setutxent(); fn endutxent(); + #[cfg(target_os = "linux")] + #[cfg(target_os = "macs")] fn utmpxname(file: *const c_char) -> c_int; } +#[cfg(target_os = "freebsd")] +unsafe extern fn utmpxname(_file: *const c_char) -> c_int { + 0 +} + pub fn uumain(args: Vec) -> int { let program = args[0].clone(); let opts = [ diff --git a/src/users/users.rs b/src/users/users.rs index dd7a937c5..57b0ccbb3 100644 --- a/src/users/users.rs +++ b/src/users/users.rs @@ -41,9 +41,16 @@ extern { fn setutxent(); fn endutxent(); + #[cfg(target_os = "linux")] + #[cfg(target_os = "macos")] fn utmpxname(file: *const libc::c_char) -> libc::c_int; } +#[cfg(target_os = "freebsd")] +unsafe extern fn utmpxname(_file: *const libc::c_char) -> libc::c_int { + 0 +} + static NAME: &'static str = "users"; pub fn uumain(args: Vec) -> int {