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.

75 lines
2.1 KiB

7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
  1. use std::borrow::{ Borrow, Cow };
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::path::PathBuf;
  5. use command_runner::CommandRunner;
  6. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  7. #[derive(Debug)]
  8. pub enum SystemdUserSessionError<E: Error> {
  9. ExecError(E),
  10. GenericError
  11. }
  12. impl<E: Error> Error for SystemdUserSessionError<E> {
  13. fn description(&self) -> &str {
  14. match self {
  15. &SystemdUserSessionError::ExecError(ref e) => e.description(),
  16. &SystemdUserSessionError::GenericError => "Generic error"
  17. }
  18. }
  19. fn cause(&self) -> Option<&Error> {
  20. match self {
  21. &SystemdUserSessionError::ExecError(ref e) => Some(e),
  22. _ => None
  23. }
  24. }
  25. }
  26. impl<E: Error> fmt::Display for SystemdUserSessionError<E> {
  27. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  28. write!(f, "{}", self.description())
  29. }
  30. }
  31. pub struct SystemdUserSession<'a> {
  32. user_name: Cow<'a, str>,
  33. command_runner: &'a CommandRunner
  34. }
  35. impl<'a> SystemdUserSession<'a> {
  36. pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> Self {
  37. SystemdUserSession {
  38. user_name: user_name,
  39. command_runner: command_runner
  40. }
  41. }
  42. }
  43. impl<'a> Symbol for SystemdUserSession<'a> {
  44. fn target_reached(&self) -> Result<bool, Box<Error>> {
  45. let mut path = PathBuf::from("/var/lib/systemd/linger");
  46. path.push(self.user_name.borrow() as &str);
  47. Ok(path.exists())
  48. // Could also do `loginctl show-user ${self.user_name} | grep -F 'Linger=yes`
  49. }
  50. fn execute(&self) -> Result<(), Box<Error>> {
  51. self.command_runner.run_successfully("loginctl", &["enable-linger", self.user_name.borrow()])
  52. }
  53. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  54. Box::new(SymbolAction::new(runner, self))
  55. }
  56. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  57. Box::new(OwnedSymbolAction::new(runner, *self))
  58. }
  59. }
  60. impl<'a> fmt::Display for SystemdUserSession<'a> {
  61. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
  62. write!(f, "Systemd user session for {}", self.user_name)
  63. }
  64. }