Skip to content

Commit 3b3d5a7

Browse files
authored
Port to NetBSD (#11388)
1 parent 5dcde30 commit 3b3d5a7

10 files changed

Lines changed: 71 additions & 29 deletions

File tree

src/uu/chroot/src/chroot.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ fn set_supplemental_gids(gids: &[libc::gid_t]) -> std::io::Result<()> {
308308
target_vendor = "apple",
309309
target_os = "freebsd",
310310
target_os = "openbsd",
311-
target_os = "cygwin"
311+
target_os = "cygwin",
312+
target_os = "netbsd"
312313
))]
313314
let n = gids.len() as libc::c_int;
314315
#[cfg(any(target_os = "linux", target_os = "android"))]

src/uu/id/src/id.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,8 @@ fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
562562
target_os = "linux",
563563
target_os = "android",
564564
target_os = "openbsd",
565-
target_os = "cygwin"
565+
target_os = "cygwin",
566+
target_os = "netbsd"
566567
))]
567568
fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
568569
let uid = possible_uid.unwrap_or_else(getuid);
@@ -586,7 +587,8 @@ fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
586587
target_os = "linux",
587588
target_os = "android",
588589
target_os = "openbsd",
589-
target_os = "cygwin"
590+
target_os = "cygwin",
591+
target_os = "netbsd"
590592
))]
591593
#[allow(clippy::unnecessary_wraps)]
592594
fn auditid() -> io::Result<()> {
@@ -597,7 +599,8 @@ fn auditid() -> io::Result<()> {
597599
target_os = "linux",
598600
target_os = "android",
599601
target_os = "openbsd",
600-
target_os = "cygwin"
602+
target_os = "cygwin",
603+
target_os = "netbsd"
601604
)))]
602605
fn auditid() -> io::Result<()> {
603606
use std::mem::MaybeUninit;

src/uu/nohup/src/nohup.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ unsafe extern "C" {
187187
target_os = "android",
188188
target_os = "freebsd",
189189
target_os = "openbsd",
190-
target_os = "cygwin"
190+
target_os = "cygwin",
191+
target_os = "netbsd"
191192
))]
192193
/// # Safety
193194
/// This function is unsafe because it dereferences a raw pointer.

src/uu/uptime/src/uptime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ fn process_utmpx(file: Option<&OsString>) -> (Option<time_t>, usize) {
246246

247247
for line in records {
248248
match line.record_type() {
249-
USER_PROCESS => nusers += 1,
250-
BOOT_TIME => {
249+
x if x == USER_PROCESS => nusers += 1,
250+
x if x == BOOT_TIME => {
251251
let dt = line.login_time();
252252
if dt.unix_timestamp() > 0 {
253253
boot_time = Some(dt.unix_timestamp() as time_t);

src/uu/who/src/platform/unix.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,21 @@ impl Who {
235235
self.print_runlevel(&ut);
236236
}
237237
}
238-
utmpx::BOOT_TIME if self.need_boottime => self.print_boottime(&ut),
239-
utmpx::NEW_TIME if self.need_clockchange => self.print_clockchange(&ut),
240-
utmpx::INIT_PROCESS if self.need_initspawn => self.print_initspawn(&ut),
241-
utmpx::LOGIN_PROCESS if self.need_login => self.print_login(&ut),
242-
utmpx::DEAD_PROCESS if self.need_deadprocs => self.print_deadprocs(&ut),
238+
x if x == utmpx::BOOT_TIME && self.need_boottime => {
239+
self.print_boottime(&ut);
240+
}
241+
x if x == utmpx::NEW_TIME && self.need_clockchange => {
242+
self.print_clockchange(&ut);
243+
}
244+
x if x == utmpx::INIT_PROCESS && self.need_initspawn => {
245+
self.print_initspawn(&ut);
246+
}
247+
x if x == utmpx::LOGIN_PROCESS && self.need_login => {
248+
self.print_login(&ut);
249+
}
250+
x if x == utmpx::DEAD_PROCESS && self.need_deadprocs => {
251+
self.print_deadprocs(&ut);
252+
}
243253
_ => {}
244254
}
245255
}

src/uucore/src/lib/features/fs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl FileInformation {
164164
#[cfg(all(not(any(target_os = "netbsd")), target_pointer_width = "64"))]
165165
return self.0.st_ino;
166166
#[cfg(any(target_os = "netbsd", not(target_pointer_width = "64")))]
167+
#[allow(clippy::useless_conversion)]
167168
return self.0.st_ino.into();
168169
}
169170
}

src/uucore/src/lib/features/fsext.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ pub struct FsUsage {
556556

557557
impl FsUsage {
558558
#[cfg(unix)]
559+
#[allow(clippy::unnecessary_cast)]
559560
pub fn new(statvfs: StatFs) -> Self {
560561
{
561562
#[cfg(all(
@@ -815,10 +816,12 @@ impl FsMeta for StatFs {
815816
}
816817

817818
#[cfg(any(target_os = "linux", target_os = "android"))]
819+
#[allow(clippy::unnecessary_cast)]
818820
fn io_size(&self) -> u64 {
819821
self.f_frsize as u64
820822
}
821823
#[cfg(any(target_vendor = "apple", target_os = "freebsd", target_os = "netbsd"))]
824+
#[allow(clippy::unnecessary_cast)]
822825
fn io_size(&self) -> u64 {
823826
#[cfg(target_os = "freebsd")]
824827
return self.f_iosize;
@@ -864,6 +867,7 @@ impl FsMeta for StatFs {
864867
target_os = "android",
865868
target_os = "openbsd"
866869
)))]
870+
#[allow(clippy::unnecessary_cast)]
867871
fn fsid(&self) -> u64 {
868872
self.f_fsid as u64
869873
}
@@ -877,6 +881,7 @@ impl FsMeta for StatFs {
877881
1024
878882
}
879883
#[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
884+
#[allow(clippy::unnecessary_cast)]
880885
fn namelen(&self) -> u64 {
881886
self.f_namemax as u64 // spell-checker:disable-line
882887
}

src/uucore/src/lib/features/safe_traversal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ impl std::os::unix::fs::MetadataExt for Metadata {
707707
fn atime_nsec(&self) -> i64 {
708708
#[cfg(target_os = "netbsd")]
709709
{
710-
self.stat.st_atimensec as i64
710+
self.stat.st_atimensec
711711
}
712712

713713
#[cfg(not(target_os = "netbsd"))]
@@ -737,7 +737,7 @@ impl std::os::unix::fs::MetadataExt for Metadata {
737737
fn mtime_nsec(&self) -> i64 {
738738
#[cfg(target_os = "netbsd")]
739739
{
740-
self.stat.st_mtimensec as i64
740+
self.stat.st_mtimensec
741741
}
742742

743743
#[cfg(not(target_os = "netbsd"))]
@@ -767,7 +767,7 @@ impl std::os::unix::fs::MetadataExt for Metadata {
767767
fn ctime_nsec(&self) -> i64 {
768768
#[cfg(target_os = "netbsd")]
769769
{
770-
self.stat.st_ctimensec as i64
770+
self.stat.st_ctimensec
771771
}
772772

773773
#[cfg(not(target_os = "netbsd"))]

src/uucore/src/lib/features/uptime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ pub fn get_uptime(_boot_time: Option<time_t>) -> UResult<i64> {
136136
#[cfg(unix)]
137137
#[cfg(not(target_os = "openbsd"))]
138138
pub fn get_uptime(boot_time: Option<time_t>) -> UResult<i64> {
139+
use crate::utmpx::BOOT_TIME;
139140
use crate::utmpx::Utmpx;
140-
use libc::BOOT_TIME;
141141
use std::fs::File;
142142
use std::io::Read;
143143

@@ -298,8 +298,8 @@ pub fn get_formatted_uptime(
298298
#[cfg(not(target_os = "openbsd"))]
299299
// see: https://gitlab.com/procps-ng/procps/-/blob/4740a0efa79cade867cfc7b32955fe0f75bf5173/library/uptime.c#L63-L115
300300
pub fn get_nusers() -> usize {
301+
use crate::utmpx::USER_PROCESS;
301302
use crate::utmpx::Utmpx;
302-
use libc::USER_PROCESS;
303303

304304
let mut num_user = 0;
305305
Utmpx::iter_all_records().for_each(|ut| {

src/uucore/src/lib/features/utmpx.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,16 @@ mod ut {
172172
pub use libc::_UTX_USERSIZE as UT_NAMESIZE;
173173

174174
pub use libc::ACCOUNTING;
175-
pub use libc::DEAD_PROCESS;
176-
pub use libc::EMPTY;
177-
pub use libc::INIT_PROCESS;
178-
pub use libc::LOGIN_PROCESS;
179-
pub use libc::NEW_TIME;
180-
pub use libc::OLD_TIME;
181-
pub use libc::RUN_LVL;
182-
pub use libc::SIGNATURE;
183-
pub use libc::USER_PROCESS;
175+
pub const BOOT_TIME: i16 = libc::BOOT_TIME as i16;
176+
pub const DEAD_PROCESS: i16 = libc::DEAD_PROCESS as i16;
177+
pub const EMPTY: i16 = libc::EMPTY as i16;
178+
pub const INIT_PROCESS: i16 = libc::INIT_PROCESS as i16;
179+
pub const LOGIN_PROCESS: i16 = libc::LOGIN_PROCESS as i16;
180+
pub const NEW_TIME: i16 = libc::NEW_TIME as i16;
181+
pub const OLD_TIME: i16 = libc::OLD_TIME as i16;
182+
pub const RUN_LVL: i16 = libc::RUN_LVL as i16;
183+
pub const SIGNATURE: i16 = libc::SIGNATURE as i16;
184+
pub const USER_PROCESS: i16 = libc::USER_PROCESS as i16;
184185
}
185186

186187
#[cfg(target_os = "cygwin")]
@@ -207,10 +208,30 @@ pub struct Utmpx {
207208
inner: utmpx,
208209
}
209210

211+
#[cfg(target_os = "netbsd")]
212+
impl Utmpx {
213+
fn ut_type(&self) -> i16 {
214+
self.inner.ut_type as i16
215+
}
216+
fn ut_user(&self) -> String {
217+
chars2string!(self.inner.ut_name)
218+
}
219+
}
220+
221+
#[cfg(not(target_os = "netbsd"))]
222+
impl Utmpx {
223+
fn ut_type(&self) -> i16 {
224+
self.inner.ut_type
225+
}
226+
fn ut_user(&self) -> String {
227+
chars2string!(self.inner.ut_user)
228+
}
229+
}
230+
210231
impl Utmpx {
211232
/// A.K.A. ut.ut_type
212233
pub fn record_type(&self) -> i16 {
213-
self.inner.ut_type
234+
self.ut_type()
214235
}
215236
/// A.K.A. ut.ut_pid
216237
pub fn pid(&self) -> i32 {
@@ -220,9 +241,9 @@ impl Utmpx {
220241
pub fn terminal_suffix(&self) -> String {
221242
chars2string!(self.inner.ut_id)
222243
}
223-
/// A.K.A. ut.ut_user
244+
/// A.K.A. ut.ut_user / ut.ut_name (NetBSD)
224245
pub fn user(&self) -> String {
225-
chars2string!(self.inner.ut_user)
246+
self.ut_user()
226247
}
227248
/// A.K.A. ut.ut_host
228249
pub fn host(&self) -> String {

0 commit comments

Comments
 (0)