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.

74 lines
2.1 KiB

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