diff --git a/windows/ZeroTierOneService/Service.cs b/windows/ZeroTierOneService/Service.cs index 4ac11bd00..306e8e4d8 100644 --- a/windows/ZeroTierOneService/Service.cs +++ b/windows/ZeroTierOneService/Service.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.ServiceProcess; +using System.Threading; namespace ZeroTierOneService { @@ -18,24 +19,68 @@ namespace ZeroTierOneService this.ztBinary = this.ztHome + Path.DirectorySeparatorChar + (Environment.Is64BitOperatingSystem ? "zerotier-one_x64.exe" : "zerotier-one_x86.exe"); this.ztService = null; + this.ztKiller = null; } protected override void OnStart(string[] args) { - startZeroTierService(); + startZeroTierDaemon(); } protected override void OnStop() { - stopZeroTierService(); + stopZeroTierDaemon(); } - private void startZeroTierService() + private void startZeroTierDaemon() { + if (ztService != null) + return; + ztService = new Process(); + try + { + ztService.StartInfo.UseShellExecute = false; + ztService.StartInfo.FileName = ztBinary; + ztService.StartInfo.Arguments = ""; + ztService.StartInfo.CreateNoWindow = true; + ztService.Exited += ztService_Exited; + ztService.Start(); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + ztService = null; + } } - private void stopZeroTierService() + private void stopZeroTierDaemon() { + while (ztKiller != null) + Thread.Sleep(250); + + ztKiller = new Process(); + try + { + ztKiller.StartInfo.UseShellExecute = false; + ztKiller.StartInfo.FileName = ztBinary; + ztKiller.StartInfo.Arguments = "-q terminate ServiceShutdown"; + ztKiller.StartInfo.CreateNoWindow = true; + ztKiller.Exited += ztKiller_Exited; + ztKiller.Start(); + } + catch (Exception e) + { + ztKiller = null; + } + + int waited = 0; + while (ztKiller != null) + { + Thread.Sleep(250); + if (++waited > 100) + break; + } + if (ztService != null) { ztService.Kill(); @@ -43,15 +88,23 @@ namespace ZeroTierOneService } } + // Event generated when ztService exits private void ztService_Exited(object sender, System.EventArgs e) { ztService = null; } + // Event generated when ztKiller is done + private void ztKiller_Exited(object sender, System.EventArgs e) + { + ztKiller = null; + } + private string ztHome; private string ztUpdatesFolder; private string ztBinary; - private Process ztService; + private volatile Process ztService; + private volatile Process ztKiller; } } diff --git a/windows/ZeroTierOneService/ZeroTierOneService.csproj b/windows/ZeroTierOneService/ZeroTierOneService.csproj index b7c75abcb..f409bbb89 100644 --- a/windows/ZeroTierOneService/ZeroTierOneService.csproj +++ b/windows/ZeroTierOneService/ZeroTierOneService.csproj @@ -11,6 +11,21 @@ ZeroTierOneService v4.5 512 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true AnyCPU @@ -35,6 +50,15 @@ ZeroTierOneService.Program + + false + + + false + + + 5809BB3255B4F32DD93619C1CF26A7DF6C282A89 + @@ -64,6 +88,23 @@ Service.cs + + + False + Microsoft .NET Framework 4.5 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + +