Skip to content

Make utmp(x) support more platforms #353

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

Merged
merged 5 commits into from
Aug 9, 2016
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
1 change: 1 addition & 0 deletions libc-test/build-generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
.flag("-Wall")
.flag("-Wextra")
.flag("-Werror")
.flag("-Wno-deprecated-declarations")
.flag("-Wno-type-limits")
.compile("liball.a");
}
11 changes: 10 additions & 1 deletion libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fn main() {
cfg.header("ws2tcpip.h");
}
} else {
cfg.flag("-Wno-deprecated-declarations");

cfg.header("ctype.h");
cfg.header("dirent.h");
if openbsd {
Expand Down Expand Up @@ -281,6 +283,11 @@ fn main() {
match ty {
"sockaddr_nl" => musl,

// On Linux, the type of `ut_tv` field of `struct utmpx`
// can be an anonymous struct, so an extra struct,
// which is absent in glibc, has to be defined.
"__timeval" if linux => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate a bit on this exception? How come we can't match what libc does?

Copy link
Contributor Author

@knight42 knight42 Aug 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The declaration of struct utmpx on Linux:

struct utmpx {
...
...

    /* The ut_session and ut_tv fields must be the same size when
       compiled 32- and 64-bit.  This allows data files and shared
       memory to be shared between 32- and 64-bit applications. */
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
    int32_t ut_session;           /* Session ID (getsid(2)),
                                     used for windowing */
    struct {
        int32_t tv_sec;           /* Seconds */
        int32_t tv_usec;          /* Microseconds */
    } ut_tv;                      /* Time entry was made */
#else
     long   ut_session;           /* Session ID */
     struct timeval ut_tv;        /* Time entry was made */
#endif

...
};

As shown above, the type of field ut_tv can be an anonymous struct, so I have to define a struct __timeval which is nonexistent in glibc to handle this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Can that be added in a comment here?


// The alignment of this is 4 on 64-bit OSX...
"kevent" if apple && x86_64 => true,

Expand Down Expand Up @@ -429,7 +436,9 @@ fn main() {
// This is a weird union, don't check the type.
(struct_ == "ifaddrs" && field == "ifa_ifu") ||
// sighandler_t type is super weird
(struct_ == "sigaction" && field == "sa_sigaction")
(struct_ == "sigaction" && field == "sa_sigaction") ||
// __timeval type is a patch which doesn't exist in glibc
(linux && struct_ == "utmpx" && field == "ut_tv")
});

cfg.skip_field(move |struct_, field| {
Expand Down
2 changes: 1 addition & 1 deletion libc-test/generate-files/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ pub type sem_t = ::c_int;
pub enum timezone {}

s! {
pub struct utmpx {
pub ut_user: [::c_char; _UTX_USERSIZE],
pub ut_id: [::c_char; _UTX_IDSIZE],
pub ut_line: [::c_char; _UTX_LINESIZE],
pub ut_pid: ::pid_t,
pub ut_type: ::c_short,
pub ut_tv: ::timeval,
pub ut_host: [::c_char; _UTX_HOSTSIZE],
ut_pad: [::uint32_t; 16],
}

pub struct glob_t {
pub gl_pathc: ::size_t,
__unused1: ::c_int,
Expand Down Expand Up @@ -294,6 +305,24 @@ s! {
}
}

pub const _UTX_USERSIZE: usize = 256;
pub const _UTX_LINESIZE: usize = 32;
pub const _UTX_IDSIZE: usize = 4;
pub const _UTX_HOSTSIZE: usize = 256;

pub const EMPTY: ::c_short = 0;
pub const RUN_LVL: ::c_short = 1;
pub const BOOT_TIME: ::c_short = 2;
pub const OLD_TIME: ::c_short = 3;
pub const NEW_TIME: ::c_short = 4;
pub const INIT_PROCESS: ::c_short = 5;
pub const LOGIN_PROCESS: ::c_short = 6;
pub const USER_PROCESS: ::c_short = 7;
pub const DEAD_PROCESS: ::c_short = 8;
pub const ACCOUNTING: ::c_short = 9;
pub const SIGNATURE: ::c_short = 10;
pub const SHUTDOWN_TIME: ::c_short = 11;

pub const LC_COLLATE_MASK: ::c_int = (1 << 0);
pub const LC_CTYPE_MASK: ::c_int = (1 << 1);
pub const LC_MESSAGES_MASK: ::c_int = (1 << 2);
Expand Down Expand Up @@ -1286,6 +1315,14 @@ f! {
}

extern {
pub fn getutxent() -> *mut utmpx;
pub fn getutxid(ut: *const utmpx) -> *mut utmpx;
pub fn getutxline(ut: *const utmpx) -> *mut utmpx;
pub fn pututxline(ut: *const utmpx) -> *mut utmpx;
pub fn setutxent();
pub fn endutxent();
pub fn utmpxname(file: *const ::c_char) -> ::c_int;

pub fn getnameinfo(sa: *const ::sockaddr,
salen: ::socklen_t,
host: *mut ::c_char,
Expand Down
32 changes: 32 additions & 0 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ pub type sem_t = _sem;
pub enum timezone {}

s! {
pub struct utmpx {
pub ut_type: ::c_short,
pub ut_tv: ::timeval,
pub ut_id: [::c_char; 8],
pub ut_pid: ::pid_t,
pub ut_user: [::c_char; 32],
pub ut_line: [::c_char; 16],
pub ut_host: [::c_char; 128],
pub __ut_spare: [::c_char; 64],
}

pub struct glob_t {
pub gl_pathc: ::size_t,
__unused1: ::size_t,
Expand Down Expand Up @@ -158,6 +169,16 @@ s! {
}
}

pub const EMPTY: ::c_short = 0;
pub const BOOT_TIME: ::c_short = 1;
pub const OLD_TIME: ::c_short = 2;
pub const NEW_TIME: ::c_short = 3;
pub const USER_PROCESS: ::c_short = 4;
pub const INIT_PROCESS: ::c_short = 5;
pub const LOGIN_PROCESS: ::c_short = 6;
pub const DEAD_PROCESS: ::c_short = 7;
pub const SHUTDOWN_TIME: ::c_short = 8;

pub const LC_COLLATE_MASK: ::c_int = (1 << 0);
pub const LC_CTYPE_MASK: ::c_int = (1 << 1);
pub const LC_MESSAGES_MASK: ::c_int = (1 << 2);
Expand Down Expand Up @@ -701,6 +722,17 @@ f! {
}
}

extern {
pub fn endutxent();
pub fn getutxent() -> *mut utmpx;
pub fn getutxid(ut: *const utmpx) -> *mut utmpx;
pub fn getutxline(ut: *const utmpx) -> *mut utmpx;
pub fn pututxline(ut: *const utmpx) -> *mut utmpx;
pub fn setutxent();
pub fn getutxuser(user: *const ::c_char) -> *mut utmpx;
pub fn setutxdb(_type: ::c_int, file: *const ::c_char) -> ::c_int;
}

#[link(name = "util")]
extern {
pub fn getnameinfo(sa: *const ::sockaddr,
Expand Down
44 changes: 0 additions & 44 deletions src/unix/notbsd/linux/other/b32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,6 @@ s! {
pub mem_unit: ::c_uint,
pub _f: [::c_char; 8],
}

pub struct __exit_status {
pub e_termination: ::c_short,
pub e_exit: ::c_short,
}

pub struct utmpx {
pub ut_type: ::c_short,
pub ut_pid: ::pid_t,
pub ut_line: [::c_char; __UT_LINESIZE],
pub ut_id: [::c_char; 4],

pub ut_user: [::c_char; __UT_NAMESIZE],
pub ut_host: [::c_char; __UT_HOSTSIZE],
pub ut_exit: __exit_status,
pub ut_session: ::c_long,
pub ut_tv: ::timeval,

pub ut_addr_v6: [::int32_t; 4],
__glibc_reserved: [::c_char; 20],
}
}

pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 4;
Expand All @@ -118,29 +97,6 @@ pub const PTRACE_SETFPXREGS: ::c_uint = 19;
pub const PTRACE_GETREGS: ::c_uint = 12;
pub const PTRACE_SETREGS: ::c_uint = 13;

pub const __UT_LINESIZE: usize = 32;
pub const __UT_NAMESIZE: usize = 32;
pub const __UT_HOSTSIZE: usize = 256;
pub const EMPTY: ::c_short = 0;
pub const RUN_LVL: ::c_short = 1;
pub const BOOT_TIME: ::c_short = 2;
pub const NEW_TIME: ::c_short = 3;
pub const OLD_TIME: ::c_short = 4;
pub const INIT_PROCESS: ::c_short = 5;
pub const LOGIN_PROCESS: ::c_short = 6;
pub const USER_PROCESS: ::c_short = 7;
pub const DEAD_PROCESS: ::c_short = 8;
pub const ACCOUNTING: ::c_short = 9;

extern {
pub fn getutxent() -> *mut utmpx;
pub fn getutxid(ut: *const utmpx) -> *mut utmpx;
pub fn getutxline(ut: *const utmpx) -> *mut utmpx;
pub fn pututxline(ut: *const utmpx) -> *mut utmpx;
pub fn setutxent();
pub fn endutxent();
}

cfg_if! {
if #[cfg(target_arch = "x86")] {
mod x86;
Expand Down
58 changes: 58 additions & 0 deletions src/unix/notbsd/linux/other/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@ pub type rlim_t = c_ulong;
pub type __priority_which_t = ::c_uint;

s! {
pub struct __exit_status {
pub e_termination: ::c_short,
pub e_exit: ::c_short,
}

pub struct __timeval {
pub tv_sec: ::int32_t,
pub tv_usec: ::int32_t,
}

pub struct utmpx {
pub ut_type: ::c_short,
pub ut_pid: ::pid_t,
pub ut_line: [::c_char; __UT_LINESIZE],
pub ut_id: [::c_char; 4],

pub ut_user: [::c_char; __UT_NAMESIZE],
pub ut_host: [::c_char; __UT_HOSTSIZE],
pub ut_exit: __exit_status,

#[cfg(any(target_arch = "aarch64", target_pointer_width = "32"))]
pub ut_session: ::c_long,
#[cfg(any(target_arch = "aarch64", target_pointer_width = "32"))]
pub ut_tv: ::timeval,

#[cfg(not(any(target_arch = "aarch64", target_pointer_width = "32")))]
pub ut_session: ::int32_t,
#[cfg(not(any(target_arch = "aarch64", target_pointer_width = "32")))]
pub ut_tv: __timeval,

pub ut_addr_v6: [::int32_t; 4],
__glibc_reserved: [::c_char; 20],
}

pub struct sigaction {
pub sa_sigaction: ::sighandler_t,
pub sa_mask: ::sigset_t,
Expand Down Expand Up @@ -132,6 +166,20 @@ s! {
}
}

pub const __UT_LINESIZE: usize = 32;
pub const __UT_NAMESIZE: usize = 32;
pub const __UT_HOSTSIZE: usize = 256;
pub const EMPTY: ::c_short = 0;
pub const RUN_LVL: ::c_short = 1;
pub const BOOT_TIME: ::c_short = 2;
pub const NEW_TIME: ::c_short = 3;
pub const OLD_TIME: ::c_short = 4;
pub const INIT_PROCESS: ::c_short = 5;
pub const LOGIN_PROCESS: ::c_short = 6;
pub const USER_PROCESS: ::c_short = 7;
pub const DEAD_PROCESS: ::c_short = 8;
pub const ACCOUNTING: ::c_short = 9;

pub const RLIMIT_RSS: ::c_int = 5;
pub const RLIMIT_NOFILE: ::c_int = 7;
pub const RLIMIT_AS: ::c_int = 9;
Expand Down Expand Up @@ -498,6 +546,16 @@ cfg_if! {
}
}

extern {
pub fn utmpxname(file: *const ::c_char) -> ::c_int;
pub fn getutxent() -> *mut utmpx;
pub fn getutxid(ut: *const utmpx) -> *mut utmpx;
pub fn getutxline(ut: *const utmpx) -> *mut utmpx;
pub fn pututxline(ut: *const utmpx) -> *mut utmpx;
pub fn setutxent();
pub fn endutxent();
}

#[link(name = "util")]
extern {
pub fn sysctl(name: *mut ::c_int,
Expand Down