Skip to content

Commit 2af5f08

Browse files
Make changes needed to build on musl
1 parent c98c79f commit 2af5f08

File tree

10 files changed

+159
-33
lines changed

10 files changed

+159
-33
lines changed

plib/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
pub mod curuser;
1111
pub mod group;
1212
pub mod io;
13+
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
14+
pub mod libc_aliases;
1315
pub mod lzw;
1416
pub mod modestr;
1517
pub mod sccsfile;

plib/src/libc_aliases.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[cfg(target_env = "musl")]
2+
pub mod musl;
3+
#[cfg(target_env = "musl")]
4+
pub use musl::*;
5+
6+
#[cfg(not(target_env = "musl"))]
7+
pub use libc::{
8+
endutxent, getutxent, setutxent, BOOT_TIME, DEAD_PROCESS, EMPTY, INIT_PROCESS, LOGIN_PROCESS,
9+
NEW_TIME, OLD_TIME, RUN_LVL, USER_PROCESS,
10+
};

plib/src/libc_aliases/musl.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use libc::{c_short, utmpx};
2+
3+
// https://git.musl-libc.org/cgit/musl/tree/include/utmpx.h?id=1e7f0fcd7ff2096904fd93a2ee6d12a2392be392
4+
pub const EMPTY: c_short = 0_i16;
5+
pub const RUN_LVL: c_short = 1_i16;
6+
pub const BOOT_TIME: c_short = 2_i16;
7+
pub const NEW_TIME: c_short = 3_i16;
8+
pub const OLD_TIME: c_short = 4_i16;
9+
pub const INIT_PROCESS: c_short = 5_i16;
10+
pub const LOGIN_PROCESS: c_short = 6_i16;
11+
pub const USER_PROCESS: c_short = 7_i16;
12+
pub const DEAD_PROCESS: c_short = 8_i16;
13+
14+
// https://github.com/rust-lang/libc/commit/e3caaf6b0ea08ae294e25a861022c256a7535ec4#diff-5822a2981791fb0bb7689a921abdc2133cc73116ee125eabefad3a9374056b7a
15+
extern "C" {
16+
pub fn getutxent() -> *mut utmpx;
17+
pub fn getutxid(ut: *const utmpx) -> *mut utmpx;
18+
pub fn getutxline(ut: *const utmpx) -> *mut utmpx;
19+
pub fn pututxline(ut: *const utmpx) -> *mut utmpx;
20+
pub fn setutxent();
21+
pub fn endutxent();
22+
}

plib/src/utmpx.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
//
99

1010
extern crate libc;
11-
use libc::{endutxent, getutxent, setutxent};
11+
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
12+
use crate::libc_aliases::{endutxent, getutxent, setutxent};
1213
use std::ffi::CStr;
1314

1415
#[derive(Debug)]
@@ -23,6 +24,8 @@ pub struct Utmpx {
2324
}
2425

2526
pub fn ut_type_str(typ: libc::c_short) -> &'static str {
27+
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
28+
use crate::libc_aliases as libc;
2629
match typ {
2730
libc::BOOT_TIME => "BOOT_TIME",
2831
libc::DEAD_PROCESS => "DEAD_PROCESS",

process/renice.rs

+35-8
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,24 @@ fn parse_id(which: u32, input: &str) -> Result<u32, &'static str> {
8282
fn xgetpriority(which: u32, id: u32) -> io::Result<i32> {
8383
set_errno(errno::Errno(0));
8484

85-
#[cfg(not(target_os = "macos"))]
86-
let res = unsafe { libc::getpriority(which, id) };
85+
// Prevent accidental shadowing by using a block
86+
let which_cast = {
87+
#[cfg(all(not(target_os = "macos"), not(target_env = "musl")))]
88+
{
89+
which
90+
}
91+
92+
#[cfg(all(not(target_os = "macos"), target_env = "musl"))]
93+
{
94+
which as i32
95+
}
8796

88-
#[cfg(target_os = "macos")]
89-
let res = unsafe { libc::getpriority(which as i32, id) };
97+
#[cfg(target_os = "macos")]
98+
{
99+
which as i32
100+
}
101+
};
102+
let res = unsafe { libc::getpriority(which_cast, id) };
90103

91104
let errno_res = errno().0;
92105
if errno_res == 0 {
@@ -99,11 +112,25 @@ fn xgetpriority(which: u32, id: u32) -> io::Result<i32> {
99112
}
100113

101114
fn xsetpriority(which: u32, id: u32, prio: i32) -> io::Result<()> {
102-
#[cfg(not(target_os = "macos"))]
103-
let res = unsafe { libc::setpriority(which, id, prio) };
115+
// Prevent accidental shadowing by using a block
116+
let which_cast = {
117+
#[cfg(all(not(target_os = "macos"), not(target_env = "musl")))]
118+
{
119+
which
120+
}
121+
122+
#[cfg(all(not(target_os = "macos"), target_env = "musl"))]
123+
{
124+
which as i32
125+
}
126+
127+
#[cfg(target_os = "macos")]
128+
{
129+
which as i32
130+
}
131+
};
104132

105-
#[cfg(target_os = "macos")]
106-
let res = unsafe { libc::setpriority(which as i32, id, prio) };
133+
let res = unsafe { libc::setpriority(which_cast, id, prio) };
107134

108135
if res < 0 {
109136
let e = io::Error::last_os_error();

sys/ipcrm.rs

+9
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn sem_key_lookup(semkey: i32) -> io::Result<i32> {
126126
}
127127

128128
// Define the union semun as per your requirements
129+
#[cfg(not(target_env = "musl"))]
129130
#[repr(C)]
130131
union semun {
131132
val: c_int, // for SETVAL
@@ -134,6 +135,7 @@ union semun {
134135
// Depending on your platform, you might need to add other fields as well
135136
}
136137

138+
#[cfg(not(target_env = "musl"))]
137139
fn sem_rm(semid: i32) -> io::Result<i32> {
138140
let arg = semun { val: 0 };
139141

@@ -146,6 +148,7 @@ fn sem_rm(semid: i32) -> io::Result<i32> {
146148
}
147149
}
148150

151+
#[cfg(not(target_env = "musl"))]
149152
fn remove_ipcs(args: &Args) -> io::Result<()> {
150153
// remove semaphores
151154
if let Some(semkey) = args.semkey {
@@ -180,6 +183,12 @@ fn remove_ipcs(args: &Args) -> io::Result<()> {
180183
Ok(())
181184
}
182185

186+
#[cfg(target_env = "musl")]
187+
fn remove_ipcs(_args: &Args) -> io::Result<()> {
188+
// TODO
189+
unimplemented!();
190+
}
191+
183192
fn main() -> Result<(), Box<dyn std::error::Error>> {
184193
// parse command line arguments
185194
let args = Args::parse();

sys/ipcs.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ fn display_message_queues(_args: &Args) {
9090
break;
9191
}
9292

93-
let key = msg_ds.msg_perm.__key; // Ensure the correct field name for your system
93+
let key = {
94+
#[cfg(not(target_env = "musl"))]
95+
{
96+
msg_ds.msg_perm.__key // Ensure the correct field name for your system
97+
}
98+
99+
// TODO: What placeholder value should go here?
100+
#[cfg(target_env = "musl")]
101+
{
102+
0_i32
103+
}
104+
};
94105
let mode = msg_ds.msg_perm.mode;
95106
let uid = msg_ds.msg_perm.uid;
96107
let gid = msg_ds.msg_perm.gid;
@@ -154,10 +165,24 @@ fn display_shared_memory(_args: &Args) {
154165
continue;
155166
}
156167

157-
#[cfg(target_os = "macos")]
158-
let key = shmbuf.shm_perm._key; // Check for the correct field name on your system
159-
#[cfg(not(target_os = "macos"))]
160-
let key = shmbuf.shm_perm.__key; // Check for the correct field name on your system
168+
// Prevent accidental shadowing by using a block
169+
let key = {
170+
#[cfg(target_os = "macos")]
171+
{
172+
shmbuf.shm_perm._key // Check for the correct field name on your system
173+
}
174+
175+
#[cfg(all(not(target_os = "macos"), not(target_env = "musl")))]
176+
{
177+
shmbuf.shm_perm.__key // Check for the correct field name on your system
178+
}
179+
180+
// TODO: What placeholder value should go here?
181+
#[cfg(all(not(target_os = "macos"), target_env = "musl"))]
182+
{
183+
0_i32
184+
}
185+
};
161186
let mode = shmbuf.shm_perm.mode;
162187
let uid = shmbuf.shm_perm.uid;
163188
let gid = shmbuf.shm_perm.gid;
@@ -187,6 +212,7 @@ fn display_shared_memory(_args: &Args) {
187212
}
188213
}
189214

215+
#[cfg(not(target_env = "musl"))]
190216
fn display_semaphores(_args: &Args) {
191217
use libc::{semctl, semid_ds, IPC_STAT};
192218
use std::ffi::CStr;
@@ -238,6 +264,12 @@ fn display_semaphores(_args: &Args) {
238264
}
239265
}
240266

267+
#[cfg(target_env = "musl")]
268+
fn display_semaphores(_args: &Args) {
269+
// TODO
270+
unimplemented!();
271+
}
272+
241273
fn get_current_date() -> String {
242274
// Retrieve the current date and time in a human-readable format
243275
let now = Local::now();

sys/who.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,31 @@ fn print_entry(args: &Args, entry: &plib::utmpx::Utmpx) {
129129
}
130130

131131
let mut selected = false;
132-
if (args.boot && entry.typ == libc::BOOT_TIME)
133-
|| (args.userproc && entry.typ == libc::USER_PROCESS)
134-
|| (args.dead && entry.typ == libc::DEAD_PROCESS)
135-
|| (args.login && entry.typ == libc::LOGIN_PROCESS)
136-
|| (args.runlevel && entry.typ == libc::RUN_LVL)
137-
|| (args.process && entry.typ == libc::INIT_PROCESS)
138132
{
139-
selected = true;
133+
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
134+
use plib::libc_aliases as libc;
135+
if (args.boot && entry.typ == libc::BOOT_TIME)
136+
|| (args.userproc && entry.typ == libc::USER_PROCESS)
137+
|| (args.dead && entry.typ == libc::DEAD_PROCESS)
138+
|| (args.login && entry.typ == libc::LOGIN_PROCESS)
139+
|| (args.runlevel && entry.typ == libc::RUN_LVL)
140+
|| (args.process && entry.typ == libc::INIT_PROCESS)
141+
{
142+
selected = true;
143+
}
140144
}
141145

142146
if !selected {
143147
return;
144148
}
145149

146-
let line = match entry.typ {
147-
libc::BOOT_TIME => "system boot",
148-
_ => entry.line.as_str(),
150+
let line = {
151+
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
152+
use plib::libc_aliases as libc;
153+
match entry.typ {
154+
libc::BOOT_TIME => "system boot",
155+
_ => entry.line.as_str(),
156+
}
149157
};
150158

151159
if args.short_format {

tree/ls.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,20 @@ fn get_terminal_width() -> usize {
582582
// Fallback to manually querying via `ioctl`.
583583
unsafe {
584584
let mut winsize: MaybeUninit<libc::winsize> = MaybeUninit::zeroed();
585-
let ret = libc::ioctl(
586-
libc::STDOUT_FILENO,
587-
winsize_request_code(),
588-
winsize.as_mut_ptr(),
589-
);
585+
let request_cast = {
586+
let request = winsize_request_code();
587+
588+
#[cfg(target_env = "musl")]
589+
{
590+
request as i32
591+
}
592+
593+
#[cfg(not(target_env = "musl"))]
594+
{
595+
request
596+
}
597+
};
598+
let ret = libc::ioctl(libc::STDOUT_FILENO, request_cast, winsize.as_mut_ptr());
590599

591600
// We're only interested in stdout here unlike `term_size::dimensions`
592601
// so we won't query further if the first `ioctl` call fails.

users/write.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ fn select_terminal(user_name: &str) -> String {
4444
let entries = plib::utmpx::load();
4545

4646
// Filter the entries to find terminals for the specified user
47-
let user_entries: Vec<_> = entries
48-
.into_iter()
49-
.filter(|entry| entry.user == user_name && entry.typ == libc::USER_PROCESS)
50-
.collect();
47+
let user_entries: Vec<_> = {
48+
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
49+
use plib::libc_aliases as libc;
50+
entries
51+
.into_iter()
52+
.filter(|entry| entry.user == user_name && entry.typ == libc::USER_PROCESS)
53+
.collect()
54+
};
5155

5256
if user_entries.is_empty() {
5357
eprintln!("{}: {}", gettext("No terminals found for user"), user_name);

0 commit comments

Comments
 (0)