Trapping Ctrl+C handler didn't work.

It made Windows Service Controller pause for a while to give processes a
graceful opportunity to exit, then presumably it timed out and killed
all the processes anyway.
pull/37/merge
Kohsuke Kawaguchi 2014-04-01 12:54:29 -07:00
parent 0094a33523
commit 3a74585609
1 changed files with 61 additions and 2 deletions

61
Main.cs
View File

@ -47,6 +47,46 @@ namespace winsw
[DllImport("Kernel32.dll", SetLastError = true)] [DllImport("Kernel32.dll", SetLastError = true)]
public static extern int SetStdHandle(int device, IntPtr handle); public static extern int SetStdHandle(int device, IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine, ref IntPtr lpProcessAttributes,
ref IntPtr lpThreadAttributes, bool bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
[In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwYSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
private SERVICE_STATUS wrapperServiceStatus; private SERVICE_STATUS wrapperServiceStatus;
private Process process = new Process(); private Process process = new Process();
@ -601,8 +641,16 @@ namespace winsw
if (s == null) ThrowNoSuchService(); if (s == null) ThrowNoSuchService();
s.StopService(); s.StopService();
} }
if (args[0] == "restart") if (args[0] == "restart" || args[0]=="restart-self")
{ {
if (args[0] == "restart-self")
{
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs ev)
{
ev.Cancel = true;
};
}
if (s == null) if (s == null)
ThrowNoSuchService(); ThrowNoSuchService();
@ -617,6 +665,17 @@ namespace winsw
s.StartService(); s.StartService();
} }
/*
if (args[0] == "restart-self")
{
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = d.ExecutablePath;
ps.Arguments = "restart";
ps.UseShellExecute = true;
Process.Start(ps);
}
*/
if (args[0] == "status") if (args[0] == "status")
{ {
if (s == null) if (s == null)