Skip to content

Commit

Permalink
Don't panic if /var/run/utmp is missing
Browse files Browse the repository at this point in the history
In a container as set up by Podman there is no utmp file in (/var)/run.
When no file is present, we can return NULL and report the error which
applications normally would handle gracefully.
  • Loading branch information
pothos committed May 20, 2024
1 parent 4feefe4 commit b11d8c7
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions c-gull/src/utmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ unsafe extern "C" fn getutxent() -> *mut libc::utmpx {

ensure_open_file(&mut lock);

let (_path, file, entry) = lock.get_mut().unwrap();
let (_path, Some(ref mut file), entry) = lock.get_mut().unwrap() else {
set_errno(Errno(libc::ENOENT));
return null_mut();
};

match file.as_ref().unwrap().read_exact(unsafe {
match file.read_exact(unsafe {
slice::from_raw_parts_mut(entry as *mut utmpx as *mut u8, size_of::<utmpx>())
}) {
Ok(()) => entry,
Expand All @@ -43,10 +46,13 @@ unsafe extern "C" fn getutxid(ut: *const libc::utmpx) -> *mut libc::utmpx {

ensure_open_file(&mut lock);

let (_path, file, entry) = lock.get_mut().unwrap();
let (_path, Some(ref mut file), entry) = lock.get_mut().unwrap() else {
set_errno(Errno(libc::ENOENT));
return null_mut();
};

loop {
match file.as_ref().unwrap().read_exact(unsafe {
match file.read_exact(unsafe {
slice::from_raw_parts_mut(entry as *mut utmpx as *mut u8, size_of::<utmpx>())
}) {
Ok(()) => {}
Expand Down Expand Up @@ -80,10 +86,13 @@ unsafe extern "C" fn getutxline(ut: *const libc::utmpx) -> *mut libc::utmpx {

ensure_open_file(&mut lock);

let (_path, file, entry) = lock.get_mut().unwrap();
let (_path, Some(ref mut file), entry) = lock.get_mut().unwrap() else {
set_errno(Errno(libc::ENOENT));
return null_mut();
};

loop {
match file.as_ref().unwrap().read_exact(unsafe {
match file.read_exact(unsafe {
slice::from_raw_parts_mut(entry as *mut utmpx as *mut u8, size_of::<utmpx>())
}) {
Ok(()) => {}
Expand Down

0 comments on commit b11d8c7

Please sign in to comment.