use heim_common::prelude::*;
use super::into_cow;
#[derive(Debug)]
pub struct User {
username: String,
terminal: String,
pid: libc::pid_t,
}
impl User {
pub fn username(&self) -> &str {
&self.username
}
pub fn terminal(&self) -> Option<&str> {
Some(&self.terminal)
}
pub fn pid(&self) -> libc::pid_t {
self.pid
}
}
impl From<libc::utmpx> for User {
fn from(entry: libc::utmpx) -> User {
User{
username: unsafe { into_cow(&entry.ut_user).into_owned() },
terminal: unsafe { into_cow(&entry.ut_line).into_owned() },
pid: entry.ut_pid,
}
}
}
pub fn users() -> impl Stream<Item=Result<User>> {
let mut users = vec![];
unsafe {
libc::setutxent();
loop {
let entry = libc::getutxent();
if entry.is_null() {
break
}
if (*entry).ut_type != libc::USER_PROCESS {
continue;
}
users.push(Ok(User::from(*entry)))
}
libc::endutxent();
}
stream::iter(users)
}