A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8 KiB

use std::borrow::{ Borrow, Cow };
use std::error::Error;
use std::fmt;
use std::path::PathBuf;
use command_runner::CommandRunner;
use symbols::Symbol;
#[derive(Debug)]
pub enum SystemdUserSessionError<E: Error> {
ExecError(E),
GenericError
}
impl<E: Error> Error for SystemdUserSessionError<E> {
fn description(&self) -> &str {
match self {
&SystemdUserSessionError::ExecError(ref e) => e.description(),
&SystemdUserSessionError::GenericError => "Generic error"
}
}
fn cause(&self) -> Option<&Error> {
match self {
&SystemdUserSessionError::ExecError(ref e) => Some(e),
_ => None
}
}
}
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: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> SystemdUserSession<'a> {
pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> Self {
SystemdUserSession {
user_name: user_name,
command_runner: command_runner
}
}
}
impl<'a> Symbol for SystemdUserSession<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let mut path = PathBuf::from("/var/lib/systemd/linger");
path.push(self.user_name.borrow() as &str);
Ok(path.exists())
// Could also do `loginctl show-user ${self.user_name} | grep -F 'Linger=yes`
}
fn execute(&self) -> Result<(), Box<Error>> {
self.command_runner.run_successfully("loginctl", &["enable-linger", self.user_name.borrow()])
}
}
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)
}
}