Skip to content

Commit

Permalink
🍎 Fix smartdns service restart on MacOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish committed Apr 9, 2023
1 parent c50f65a commit 7225b94
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Cli {
};
if let Some(out) = out {
if let Ok(out) = String::from_utf8(out.stdout) {
info!("\n{}", out);
print!("{}", out);
} else {
warn!("get service status failed.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/service/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ impl Installer {
match strategy {
InstallStrategy::Backup => {
let mut path = dest_path.to_path_buf();
path.append_extension("old");
path.append_extension("bak");
fs::copy(dest_path.as_path(), path)?;
}
InstallStrategy::Preserve => {
dest_path.append_extension("new");
dest_path.append_extension("default");
}
InstallStrategy::Overide => (),
}
Expand Down
16 changes: 14 additions & 2 deletions src/service/macos/brew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@ pub fn create_service_definition() -> ServiceDefinition {
args: vec!["services".into(), "restart".into(), SERVICE_NAME.into()],
}),
status: Some(ServiceCommand {
program: brew.into(),
args: vec!["services".into(), "info".into(), SERVICE_NAME.into()],
program: "sh".into(),
args: vec![
"-c".into(),
format!(
r#"
O=$(brew services info {}) && echo "$O" | grep -q "Running: true" &&
(echo "$O" && exit 0) || (echo "$O" && exit 1)
"#,
SERVICE_NAME
)
.lines()
.collect::<String>()
.into(),
],
}),
};

Expand Down
26 changes: 18 additions & 8 deletions src/service/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl From<ServiceDefinition> for ServiceManager {

impl ServiceManager {
pub fn install(&self) -> io::Result<()> {
// try stopping an existing running service.
self.try_stop().unwrap_or_default();

// install files.
self.definition.installer.install()?;

if let Some(install) = self.definition.commands.install.as_ref() {
Expand All @@ -64,7 +68,7 @@ impl ServiceManager {
}

pub fn uninstall(&self, purge: bool) -> io::Result<()> {
self.stop().unwrap_or_default();
self.try_stop().unwrap_or_default();

if let Some(uninstall) = self.definition.commands.uninstall.as_ref() {
uninstall.spawn()?;
Expand All @@ -79,19 +83,23 @@ impl ServiceManager {
pub fn start(&self) -> io::Result<()> {
if !matches!(self.status(), Ok(ServiceStatus::Running(_))) {
self.definition.commands.start.spawn()?;
info!("Successfully started service {}", self.definition.name);
} else {
info!("Service {} already started", self.definition.name);
}

info!("Successfully started service {}", self.definition.name);
Ok(())
}

pub fn stop(&self) -> io::Result<()> {
// Make sure the service is stopped.
self.try_stop()?;
info!("Successfully stopped service {}", self.definition.name);
Ok(())
}

pub fn try_stop(&self) -> io::Result<()> {
if !matches!(self.status(), Ok(ServiceStatus::Dead(_))) {
self.definition.commands.stop.spawn()?;
}

info!("Successfully stopped service {}", self.definition.name);
Ok(())
}

Expand All @@ -102,7 +110,7 @@ impl ServiceManager {
info!("Successfully restarted service {}", self.definition.name);
}
None => {
self.stop().unwrap_or_default();
self.try_stop().unwrap_or_default();
std::thread::sleep(Duration::from_millis(500));
self.start()?;
}
Expand Down Expand Up @@ -250,7 +258,9 @@ mod tests {
assert!(stdout.contains("Linux"));
} else if #[cfg(target_os="macos")] {
assert!(stdout.contains("Darwin"));
} else {
} else if #[cfg(target_os="android")] {
assert!(stdout.contains("Android"));
} else {
unimplemented!()
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/service/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ pub(super) fn create_service_definition() -> ServiceDefinition {
status: Some(ServiceCommand {
program: "cmd.exe".into(),
args: vec![
"/C".into(),
["sc query ", SERVICE_NAME," | findstr STATE.*:.*RUNNING > NUL && (sc query smartdns-rs && exit 0) || (sc query smartdns-rs && exit 1)"].concat().into()
],
"/C".into(),
format!(
r#"
sc query {0} | findstr STATE.*:.*RUNNING > NUL
&& (sc query {0} && exit 0) || (sc query {0} && exit 1)
"#,
SERVICE_NAME
)
.lines()
.collect::<String>()
.into(),
],
}),
};

Expand Down

0 comments on commit 7225b94

Please sign in to comment.