@@ -36,6 +36,8 @@ pub extern crate time;
3636use std:: ffi:: CString ;
3737use std:: io:: Result as IOResult ;
3838use std:: marker:: PhantomData ;
39+ #[ cfg( target_os = "linux" ) ]
40+ use std:: mem:: size_of;
3941use std:: os:: unix:: ffi:: OsStrExt ;
4042use std:: path:: Path ;
4143use std:: ptr;
@@ -327,16 +329,25 @@ impl Utmpx {
327329 /// This will use the default location, or the path [`Utmpx::iter_all_records_from`]
328330 /// was most recently called with.
329331 ///
330- /// On Linux, this will try systemd-logind first and fall back to traditional
331- /// utmp files if systemd-logind is unavailable.
332+ /// On Linux, this will use a populated traditional utmp file when available.
333+ /// If the default utmp file is missing or empty, it will try systemd-logind
334+ /// and fall back to traditional utmp if systemd-logind is unavailable.
332335 ///
333336 /// Only one instance of [`UtmpxIter`] may be active at a time. This
334337 /// function will block as long as one is still active. Beware!
335338 pub fn iter_all_records ( ) -> UtmpxIter {
336339 #[ cfg( target_os = "linux" ) ]
337340 {
338- // Prefer systemd-logind on Linux.
339- UtmpxIter :: new_systemd_with_fallback_path ( None )
341+ if traditional_utmp_is_usable ( Path :: new ( DEFAULT_FILE ) ) {
342+ let iter = UtmpxIter :: new ( ) ;
343+ unsafe {
344+ #[ cfg_attr( target_env = "musl" , allow( deprecated) ) ]
345+ setutxent ( ) ;
346+ }
347+ iter
348+ } else {
349+ UtmpxIter :: new_systemd_with_fallback_path ( None )
350+ }
340351 }
341352
342353 #[ cfg( not( target_os = "linux" ) ) ]
@@ -359,15 +370,17 @@ impl Utmpx {
359370 ///
360371 /// This function affects subsequent calls to [`Utmpx::iter_all_records`].
361372 ///
362- /// On Linux, if the path matches the default utmp file, this will try
363- /// systemd-logind first and fall back to that file if it is unavailable.
373+ /// On Linux, if the path matches the default utmp file and that file is
374+ /// missing or empty, this will try systemd-logind first and fall back to
375+ /// that file if systemd-logind is unavailable.
364376 ///
365377 /// The same caveats as for [`Utmpx::iter_all_records`] apply.
366378 pub fn iter_all_records_from < P : AsRef < Path > > ( path : P ) -> UtmpxIter {
367379 #[ cfg( target_os = "linux" ) ]
368380 {
369- // Prefer systemd-logind for the default utmp file on Linux.
370- if path. as_ref ( ) == Path :: new ( DEFAULT_FILE ) {
381+ if path. as_ref ( ) == Path :: new ( DEFAULT_FILE )
382+ && !traditional_utmp_is_usable ( path. as_ref ( ) )
383+ {
371384 return UtmpxIter :: new_systemd_with_fallback_path ( Some ( path. as_ref ( ) ) ) ;
372385 }
373386 }
@@ -401,6 +414,24 @@ impl Utmpx {
401414// ordinary race conditions are also very much possible.
402415static LOCK : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
403416
417+ #[ cfg( target_os = "linux" ) ]
418+ fn traditional_utmp_is_usable ( path : & Path ) -> bool {
419+ #[ cfg( target_env = "musl" ) ]
420+ {
421+ let _ = path;
422+ false
423+ }
424+
425+ #[ cfg( not( target_env = "musl" ) ) ]
426+ {
427+ std:: fs:: metadata ( path) . is_ok_and ( |metadata| {
428+ metadata. is_file ( )
429+ && metadata. len ( ) >= size_of :: < utmpx > ( ) as u64
430+ && std:: fs:: File :: open ( path) . is_ok ( )
431+ } )
432+ }
433+ }
434+
404435/// Iterator of login records
405436pub struct UtmpxIter {
406437 #[ allow( dead_code) ]
@@ -477,7 +508,7 @@ mod tests {
477508 use super :: * ;
478509 use std:: fs:: { File , remove_file} ;
479510 use std:: io:: Write ;
480- use std:: mem:: { size_of , zeroed} ;
511+ use std:: mem:: zeroed;
481512
482513 fn write_utmpx_record ( path : & Path , user : & str ) {
483514 // SAFETY: A zero-filled utmpx value is valid, and the initialized value is
@@ -527,6 +558,18 @@ mod tests {
527558 remove_file ( custom_path) . unwrap ( ) ;
528559 remove_file ( fallback_path) . unwrap ( ) ;
529560 }
561+
562+ #[ test]
563+ fn traditional_utmp_requires_at_least_one_record ( ) {
564+ let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
565+ let path = directory. path ( ) . join ( "utmp" ) ;
566+
567+ File :: create ( & path) . unwrap ( ) ;
568+ assert ! ( !traditional_utmp_is_usable( & path) ) ;
569+
570+ write_utmpx_record ( & path, "user" ) ;
571+ assert ! ( traditional_utmp_is_usable( & path) ) ;
572+ }
530573}
531574
532575/// Wrapper type that can hold either traditional utmpx records or systemd records
0 commit comments