Skip to content

Commit

Permalink
Don't panic if /var/run/utmp is missing (#136)
Browse files Browse the repository at this point in the history
* Document requirements for runtime environment.

* Don't panic if /var/run/utmp is missing

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 authored May 20, 2024
1 parent 503cc12 commit a479ab8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ The primary way this is used is through [Mustang] and [Eyra], as their libc
implementations. It can also be used as a regular library in
["coexist-with-libc" mode].

## Runtime requirements

Resolving users and DNS records requires the execution of `getent` which
prints the entries on stdout. On a regular glibc system the `getent`
binary is provided by it and uses the NSS setup as usual.
Similar, a musl system also provides `getent` (but does not use NSS).

## Similar crates

Another libc implementation is [relibc]. [tinyrlibc] is a very minimal set of
Expand Down
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 a479ab8

Please sign in to comment.