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.

80 lines
2.2 KiB

5 years ago
8 years ago
7 years ago
8 years ago
7 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
7 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
5 years ago
5 years ago
8 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
8 years ago
5 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<&dyn 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, C: 'a + CommandRunner> {
  32. user_name: Cow<'a, str>,
  33. command_runner: &'a C,
  34. }
  35. impl<'a, C: CommandRunner> SystemdUserSession<'a, C> {
  36. pub fn new(user_name: Cow<'a, str>, command_runner: &'a C) -> Self {
  37. SystemdUserSession {
  38. user_name,
  39. command_runner,
  40. }
  41. }
  42. }
  43. impl<'a, C: CommandRunner> Symbol for SystemdUserSession<'a, C> {
  44. fn target_reached(&self) -> Result<bool, Box<dyn 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<dyn Error>> {
  51. self
  52. .command_runner
  53. .run_successfully("loginctl", &["enable-linger", self.user_name.borrow()])
  54. }
  55. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  56. Box::new(SymbolAction::new(runner, self))
  57. }
  58. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  59. where
  60. Self: 'b,
  61. {
  62. Box::new(OwnedSymbolAction::new(runner, *self))
  63. }
  64. }
  65. impl<'a, C: CommandRunner> fmt::Display for SystemdUserSession<'a, C> {
  66. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  67. write!(f, "Systemd user session for {}", self.user_name)
  68. }
  69. }