forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtSignalState.cs
More file actions
114 lines (99 loc) · 5.06 KB
/
NtSignalState.cs
File metadata and controls
114 lines (99 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using IronPython.Runtime;
#if FEATURE_PROCESS
namespace IronPython.Modules {
public static partial class PythonSignal {
[SupportedOSPlatform("windows")]
internal class NtSignalState : PythonSignalState {
//We use a single Windows event handler to process all signals. This handler simply
//delegates the work out to PySignalToPyHandler.
public NativeSignal.WinSignalsHandler WinAllSignalsHandlerDelegate;
public NtSignalState(PythonContext pc) : base(pc) {
WinAllSignalsHandlerDelegate = new NativeSignal.WinSignalsHandler(WindowsEventHandler);
NativeSignal.SetConsoleCtrlHandler(this.WinAllSignalsHandlerDelegate, true);
}
//Our implementation of WinSignalsHandler
private bool WindowsEventHandler(uint winSignal) {
bool retVal;
int pySignal;
switch (winSignal) {
case CTRL_C_EVENT:
pySignal = SIGINT;
break;
case CTRL_BREAK_EVENT:
pySignal = SIGBREAK;
break;
case CTRL_CLOSE_EVENT:
pySignal = SIGBREAK;
break;
case CTRL_LOGOFF_EVENT:
pySignal = SIGBREAK;
break;
case CTRL_SHUTDOWN_EVENT:
pySignal = SIGBREAK;
break;
default:
throw new Exception("unreachable");
}
lock (PySignalToPyHandler) {
if (PySignalToPyHandler[pySignal].GetType() == typeof(int)) {
int tempId = (int)PySignalToPyHandler[pySignal];
if (tempId == SIG_DFL) {
//SIG_DFL - we let Windows do whatever it normally would
retVal = false;
} else if (tempId == SIG_IGN) {
//SIG_IGN - we do nothing, but tell Windows we handled the signal
retVal = true;
} else {
throw new Exception("unreachable");
}
} else if (PySignalToPyHandler[pySignal] == default_int_handler) {
if (pySignal != SIGINT) {
//We're dealing with the default_int_handlerImpl which we
//know doesn't care about the frame parameter
retVal = true;
default_int_handlerImpl(pySignal, null);
} else {
//Let the real interrupt handler throw a KeyboardInterrupt for SIGINT.
//It handles this far more gracefully than we can
retVal = false;
}
} else {
//We're dealing with a callable matching PySignalHandler's signature
retVal = true;
PySignalHandler temp = (PySignalHandler)Converter.ConvertToDelegate(PySignalToPyHandler[pySignal],
typeof(PySignalHandler));
try {
if (SignalPythonContext.PythonOptions.Frames) {
temp.Invoke(pySignal, SysModule._getframeImpl(null,
0,
SignalPythonContext._mainThreadFunctionStack));
} else {
temp.Invoke(pySignal, null);
}
} catch (Exception e) {
System.Console.WriteLine(SignalPythonContext.FormatException(e));
}
}
}
return retVal;
}
}
internal static class NativeSignal {
// Windows API expects to be given a function pointer like this to handle signals
internal delegate bool WinSignalsHandler(uint winSignal);
[DllImport("Kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetConsoleCtrlHandler(WinSignalsHandler Handler, [MarshalAs(UnmanagedType.Bool)]bool Add);
[DllImport("Kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);
}
}
}
#endif