mirror of https://github.com/winsw/winsw
* Fixes #95 - Service not sending SIGINT properly to java
Detach from console process after sending SIGINT to java.
Note: we still need <stopparentprocessfirst> to be set to true, so the
parent (java) process is shut down first.
Moved exception handling to GetChildPids.
StopProcessAndChildren now gets a fresh list of childPids after stopping
a parent process, as that may have caused some child processes to
terminate.
* Fixes #181 - V2 : WinSW.NET2.exe not working
Latest version of ILMerge targets .NET4 by default when merging
assemblies; specify target platform version for the .NET2 executable.
* Fixes #95 - Service not sending SIGINT properly to java
Error-checking for console detach, spaces indentation.
#181 fix moved to separate branch.
Revert "Fixes #181 - V2 :
WinSW.NET2.exe not working"
This reverts
commit
a089755cb9
.
pull/195/head
parent
ac7a8b6f99
commit
de83539bef
|
@ -22,14 +22,22 @@ namespace winsw.Util
|
||||||
/// <returns>List of child process PIDs</returns>
|
/// <returns>List of child process PIDs</returns>
|
||||||
public static List<int> GetChildPids(int pid)
|
public static List<int> GetChildPids(int pid)
|
||||||
{
|
{
|
||||||
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
|
|
||||||
var childPids = new List<int>();
|
var childPids = new List<int>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
|
||||||
foreach (var mo in searcher.Get())
|
foreach (var mo in searcher.Get())
|
||||||
{
|
{
|
||||||
var childProcessId = mo["ProcessID"];
|
var childProcessId = mo["ProcessID"];
|
||||||
Logger.Info("Found child process: " + childProcessId + " Name: " + mo["Name"]);
|
Logger.Info("Found child process: " + childProcessId + " Name: " + mo["Name"]);
|
||||||
childPids.Add(Convert.ToInt32(childProcessId));
|
childPids.Add(Convert.ToInt32(childProcessId));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.Warn("Failed to locate children of the process with PID=" + pid + ". Child processes won't be terminated", ex);
|
||||||
|
}
|
||||||
|
|
||||||
return childPids;
|
return childPids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,19 +98,9 @@ namespace winsw.Util
|
||||||
/// <param name="stopParentProcessFirst">If enabled, the perent process will be terminated before its children on all levels</param>
|
/// <param name="stopParentProcessFirst">If enabled, the perent process will be terminated before its children on all levels</param>
|
||||||
public static void StopProcessAndChildren(int pid, TimeSpan stopTimeout, bool stopParentProcessFirst)
|
public static void StopProcessAndChildren(int pid, TimeSpan stopTimeout, bool stopParentProcessFirst)
|
||||||
{
|
{
|
||||||
List<int> childPids = null;
|
if (!stopParentProcessFirst)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
childPids = GetChildPids(pid);
|
foreach (var childPid in GetChildPids(pid))
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.Warn("Failed to locate children of the process with PID=" + pid + ". Child processes won't be terminated", ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stopParentProcessFirst && childPids != null)
|
|
||||||
{
|
|
||||||
foreach (var childPid in childPids)
|
|
||||||
{
|
{
|
||||||
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
|
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
|
||||||
}
|
}
|
||||||
|
@ -110,9 +108,9 @@ namespace winsw.Util
|
||||||
|
|
||||||
StopProcess(pid, stopTimeout);
|
StopProcess(pid, stopTimeout);
|
||||||
|
|
||||||
if (stopParentProcessFirst && childPids != null)
|
if (stopParentProcessFirst)
|
||||||
{
|
{
|
||||||
foreach (var childPid in childPids)
|
foreach (var childPid in GetChildPids(pid))
|
||||||
{
|
{
|
||||||
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
|
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
using System;
|
using log4net;
|
||||||
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using winsw.Native;
|
||||||
|
|
||||||
namespace winsw.Util
|
namespace winsw.Util
|
||||||
{
|
{
|
||||||
public static class SigIntHelper
|
public static class SigIntHelper
|
||||||
{
|
{
|
||||||
|
private static readonly ILog Logger = LogManager.GetLogger(typeof(SigIntHelper));
|
||||||
|
|
||||||
private const string KERNEL32 = "kernel32.dll";
|
private const string KERNEL32 = "kernel32.dll";
|
||||||
|
|
||||||
[DllImport(KERNEL32, SetLastError = true)]
|
[DllImport(KERNEL32, SetLastError = true)]
|
||||||
|
@ -49,6 +53,14 @@ namespace winsw.Util
|
||||||
|
|
||||||
process.WaitForExit((int)shutdownTimeout.TotalMilliseconds);
|
process.WaitForExit((int)shutdownTimeout.TotalMilliseconds);
|
||||||
|
|
||||||
|
// Detach from console. Causes child console process to be automatically closed.
|
||||||
|
bool success = FreeConsole();
|
||||||
|
if (!success)
|
||||||
|
{
|
||||||
|
long errorCode = Kernel32.GetLastError();
|
||||||
|
Logger.Warn("Failed to detach from console. Error code: " + errorCode);
|
||||||
|
}
|
||||||
|
|
||||||
return process.HasExited;
|
return process.HasExited;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue