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