[][src]Function users::all_users

pub unsafe fn all_users() -> impl Iterator<Item = User>

Creates a new iterator over every user present on the system.

libc functions used

Safety

This constructor is marked as unsafe, which is odd for a crate that’s meant to be a safe interface. It has to be unsafe because we cannot guarantee that the underlying C functions, getpwent/setpwent/endpwent that iterate over the system’s passwd entries, are called in a thread-safe manner.

These functions modify a global state, and if any are used at the same time, the state could be reset, resulting in a data race. We cannot even place it behind an internal Mutex, as there is nothing stopping another extern function definition from calling it!

So to iterate all users, construct the iterator inside an unsafe block, then make sure to not make a new instance of it until iteration is over.

Examples

use users::all_users;

let iter = unsafe { all_users() };
for user in iter {
    println!("User #{} ({:?})", user.uid(), user.name());
}