This commit is contained in:
Adrian Heine 2016-03-13 11:15:12 +01:00
commit 2ba4c3b1c2
10 changed files with 556 additions and 0 deletions

View file

@ -0,0 +1 @@
pub mod user_session;

View file

@ -0,0 +1,67 @@
use std::error::Error;
use std::fmt;
use std::io::Error as IoError;
use std::path::PathBuf;
use command_runner::CommandRunner;
use symbols::Symbol;
#[derive(Debug)]
pub enum SystemdUserSessionError<E: Error> {
ExecError(E)
}
impl<E: Error> Error for SystemdUserSessionError<E> {
fn description(&self) -> &str {
match self {
&SystemdUserSessionError::ExecError(ref e) => e.description()
}
}
fn cause(&self) -> Option<&Error> {
match self {
&SystemdUserSessionError::ExecError(ref e) => Some(e)
}
}
}
impl<E: Error> fmt::Display for SystemdUserSessionError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.description())
}
}
pub struct SystemdUserSession<'a> {
user_name: &'a str,
command_runner: &'a CommandRunner
}
impl<'a> SystemdUserSession<'a> {
pub fn new(user_name: &'a str, command_runner: &'a CommandRunner) -> Self {
SystemdUserSession {
user_name: user_name,
command_runner: command_runner
}
}
}
impl<'a> Symbol for SystemdUserSession<'a> {
type Error = SystemdUserSessionError<IoError>;
fn target_reached(&self) -> Result<bool, Self::Error> {
let mut path = PathBuf::from("/var/lib/systemd/linger");
path.push(self.user_name);
Ok(path.exists())
}
fn execute(&self) -> Result<(), Self::Error> {
match self.command_runner.run_with_args("loginctl", &["enable-linger", self.user_name]) {
Ok(_) => Ok(()),
Err(e) => Err(SystemdUserSessionError::ExecError(e))
}
}
}
impl<'a> fmt::Display for SystemdUserSession<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
write!(f, "Systemd user session for {}", self.user_name)
}
}