Skip to content

Commit 995ff9b

Browse files
Make changes needed to build on musl
Also: move some platform-specific code into plib.
1 parent 36b49c6 commit 995ff9b

File tree

16 files changed

+321
-193
lines changed

16 files changed

+321
-193
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

m4/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ path = "src/main.rs"
1616
[dependencies]
1717
clap.workspace = true
1818
env_logger = "0.11"
19-
errno = "0.3"
19+
errno.workspace = true
2020
libc.workspace = true
2121
log = "0.4"
2222
nom = "7.1"

plib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ license = "MIT"
77
repository = "https://github.com/rustcoreutils/posixutils-rs.git"
88

99
[dependencies]
10+
cfg-if = "1.0"
1011
libc.workspace = true
12+
errno.workspace = true
1113

1214
[lib]
1315
doctest = false

plib/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ pub mod group;
1212
pub mod io;
1313
pub mod lzw;
1414
pub mod modestr;
15+
pub mod platform;
16+
pub mod priority;
1517
pub mod sccsfile;
18+
pub mod sys;
19+
pub mod terminal;
1620
pub mod testing;
1721
pub mod utmpx;
1822

plib/src/platform.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// TODO
2+
// Avoid restating local alias names
3+
cfg_if::cfg_if! {
4+
if #[cfg(target_env = "musl")] {
5+
// https://git.musl-libc.org/cgit/musl/tree/include/utmpx.h?id=1e7f0fcd7ff2096904fd93a2ee6d12a2392be392
6+
pub const EMPTY: libc::c_short = 0_i16;
7+
pub const RUN_LVL: libc::c_short = 1_i16;
8+
pub const BOOT_TIME: libc::c_short = 2_i16;
9+
pub const NEW_TIME: libc::c_short = 3_i16;
10+
pub const OLD_TIME: libc::c_short = 4_i16;
11+
pub const INIT_PROCESS: libc::c_short = 5_i16;
12+
pub const LOGIN_PROCESS: libc::c_short = 6_i16;
13+
pub const USER_PROCESS: libc::c_short = 7_i16;
14+
pub const DEAD_PROCESS: libc::c_short = 8_i16;
15+
16+
// Remove when https://github.com/rust-lang/libc/issues/3190 is resolved
17+
// https://github.com/rust-lang/libc/commit/e3caaf6b0ea08ae294e25a861022c256a7535ec4#diff-5822a2981791fb0bb7689a921abdc2133cc73116ee125eabefad3a9374056b7a
18+
extern "C" {
19+
pub fn getutxent() -> *mut libc::utmpx;
20+
pub fn getutxid(ut: *const libc::utmpx) -> *mut libc::utmpx;
21+
pub fn getutxline(ut: *const libc::utmpx) -> *mut libc::utmpx;
22+
pub fn pututxline(ut: *const libc::utmpx) -> *mut libc::utmpx;
23+
pub fn setutxent();
24+
pub fn endutxent();
25+
}
26+
27+
type LocalPIoctlOp = libc::c_int;
28+
type LocalPPriorityWhichT = libc::c_int;
29+
} else {
30+
pub use libc::{
31+
endutxent,
32+
getutxent,
33+
setutxent,
34+
BOOT_TIME,
35+
DEAD_PROCESS,
36+
EMPTY,
37+
INIT_PROCESS,
38+
LOGIN_PROCESS,
39+
NEW_TIME,
40+
OLD_TIME,
41+
RUN_LVL,
42+
USER_PROCESS,
43+
};
44+
45+
cfg_if::cfg_if! {
46+
if #[cfg(target_os = "macos")] {
47+
type LocalPPriorityWhichT = libc::c_int;
48+
} else {
49+
type LocalPPriorityWhichT = libc::__priority_which_t;
50+
}
51+
}
52+
53+
type LocalPIoctlOp = libc::c_ulong;
54+
}
55+
}
56+
57+
// Constants taken from:
58+
// https://docs.rs/term_size/0.3.2/src/term_size/platform/unix.rs.html#5-19
59+
pub(crate) const P_WINSIZE_REQUEST_CODE: LocalPIoctlOp = ({
60+
#[cfg(any(target_os = "linux", target_os = "android"))]
61+
{
62+
0x5413
63+
}
64+
65+
#[cfg(any(
66+
target_os = "macos",
67+
target_os = "ios",
68+
target_os = "dragonfly",
69+
target_os = "freebsd",
70+
target_os = "netbsd",
71+
target_os = "openbsd"
72+
))]
73+
{
74+
0x40087468
75+
}
76+
77+
#[cfg(target_os = "solaris")]
78+
{
79+
0x5468
80+
}
81+
}) as LocalPIoctlOp;
82+
83+
pub(crate) type PPriorityWhichT = LocalPPriorityWhichT;

plib/src/priority.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::platform::PPriorityWhichT;
2+
use std::io;
3+
4+
pub fn getpriority(which: u32, id: u32) -> io::Result<i32> {
5+
errno::set_errno(errno::Errno(0));
6+
7+
let res = unsafe { libc::getpriority(which as PPriorityWhichT, id) };
8+
9+
let errno_res = errno::errno().0;
10+
if errno_res == 0 {
11+
Ok(res)
12+
} else {
13+
let e = io::Error::from_raw_os_error(errno_res);
14+
eprintln!("getpriority: {e}");
15+
Err(e)
16+
}
17+
}
18+
19+
pub fn setpriority(which: u32, id: u32, prio: i32) -> io::Result<()> {
20+
let res = unsafe { libc::setpriority(which as PPriorityWhichT, id, prio) };
21+
22+
if res < 0 {
23+
let e = io::Error::last_os_error();
24+
eprintln!("setpriority: {e}");
25+
Err(e)
26+
} else {
27+
Ok(())
28+
}
29+
}

plib/src/sys.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use libc::ipc_perm;
2+
3+
#[cfg(not(target_os = "macos"))]
4+
pub fn get_key_from_msqid_ds(
5+
#[cfg_attr(target_env = "musl", allow(unused_variables))] msgid_ds_ref: &libc::msqid_ds,
6+
) -> i32 {
7+
// Prevent accidental shadowing by using a block
8+
{
9+
cfg_if::cfg_if! {
10+
if #[cfg(target_env = "musl")] {
11+
0 // TODO: What placeholder value should go here?
12+
} else {
13+
msgid_ds_ref.msg_perm.__key // Ensure the correct field name for your system
14+
}
15+
}
16+
}
17+
}
18+
19+
pub fn get_key_from_ipc_perm(
20+
#[cfg_attr(target_env = "musl", allow(unused_variables))] ipc_perm_ref: &ipc_perm,
21+
) -> i32 {
22+
// Prevent accidental shadowing by using a block
23+
{
24+
cfg_if::cfg_if! {
25+
if #[cfg(target_os = "macos")] {
26+
ipc_perm_ref._key // Check for the correct field name on your system
27+
} else {
28+
cfg_if::cfg_if! {
29+
if #[cfg(target_env = "musl")] {
30+
0 // TODO: What placeholder value should go here?
31+
} else {
32+
ipc_perm_ref.__key // Check for the correct field name on your system
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}

plib/src/terminal.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::platform::P_WINSIZE_REQUEST_CODE;
2+
use std::mem::MaybeUninit;
3+
4+
pub fn get_terminal_width() -> usize {
5+
// COLUMNS is usually automatically set and it even changes when the
6+
// terminal window is resized.
7+
if let Ok(s) = std::env::var("COLUMNS") {
8+
if let Ok(num_columns) = s.parse() {
9+
return num_columns;
10+
}
11+
}
12+
13+
// Fallback to manually querying via `ioctl`.
14+
unsafe {
15+
let mut winsize: MaybeUninit<libc::winsize> = MaybeUninit::zeroed();
16+
let ret = libc::ioctl(
17+
libc::STDOUT_FILENO,
18+
P_WINSIZE_REQUEST_CODE,
19+
winsize.as_mut_ptr(),
20+
);
21+
22+
// We're only interested in stdout here unlike `term_size::dimensions`
23+
// so we won't query further if the first `ioctl` call fails.
24+
if ret == 0 {
25+
let winsize = winsize.assume_init();
26+
return winsize.ws_col as usize;
27+
}
28+
}
29+
30+
// Historical default terminal width is 80
31+
80
32+
}

plib/src/utmpx.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use libc::{endutxent, getutxent, setutxent};
10+
use crate::platform::{self, endutxent, getutxent, setutxent};
1111
use std::ffi::CStr;
1212

1313
#[derive(Debug)]
@@ -23,15 +23,15 @@ pub struct Utmpx {
2323

2424
pub fn ut_type_str(typ: libc::c_short) -> &'static str {
2525
match typ {
26-
libc::BOOT_TIME => "BOOT_TIME",
27-
libc::DEAD_PROCESS => "DEAD_PROCESS",
28-
libc::EMPTY => "EMPTY",
29-
libc::INIT_PROCESS => "INIT_PROCESS",
30-
libc::LOGIN_PROCESS => "LOGIN_PROCESS",
31-
libc::NEW_TIME => "NEW_TIME",
32-
libc::OLD_TIME => "OLD_TIME",
33-
libc::RUN_LVL => "RUN_LVL",
34-
libc::USER_PROCESS => "USER_PROCESS",
26+
platform::BOOT_TIME => "BOOT_TIME",
27+
platform::DEAD_PROCESS => "DEAD_PROCESS",
28+
platform::EMPTY => "EMPTY",
29+
platform::INIT_PROCESS => "INIT_PROCESS",
30+
platform::LOGIN_PROCESS => "LOGIN_PROCESS",
31+
platform::NEW_TIME => "NEW_TIME",
32+
platform::OLD_TIME => "OLD_TIME",
33+
platform::RUN_LVL => "RUN_LVL",
34+
platform::USER_PROCESS => "USER_PROCESS",
3535

3636
_ => "(unknown)",
3737
}

process/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ clap.workspace = true
1212
gettext-rs.workspace = true
1313
libc.workspace = true
1414
atty.workspace = true
15-
errno = "0.3"
1615
dirs = "5.0"
1716

1817
[[bin]]

0 commit comments

Comments
 (0)