|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#nullable enable |
| 6 | + |
| 7 | +#if FEATURE_PROCESS |
| 8 | +#if NET |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Runtime.InteropServices; |
| 12 | +using System.Runtime.Versioning; |
| 13 | + |
| 14 | +using Mono.Unix; |
| 15 | +using Mono.Unix.Native; |
| 16 | + |
| 17 | +using IronPython.Runtime; |
| 18 | +using IronPython.Runtime.Operations; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using Microsoft.Scripting.Runtime; |
| 21 | +using Microsoft.Scripting.Hosting.Shell; |
| 22 | + |
| 23 | +namespace IronPython.Modules { |
| 24 | + public static partial class PythonSignal { |
| 25 | + |
| 26 | + [SupportedOSPlatform("linux")] |
| 27 | + [SupportedOSPlatform("macos")] |
| 28 | + private class PosixSignalState : PythonSignalState { |
| 29 | + private readonly PosixSignalRegistration?[] _signalRegistrations; |
| 30 | + private ConsoleCancelEventHandler? _consoleHandler; |
| 31 | + |
| 32 | + |
| 33 | + public PosixSignalState(PythonContext pc) : base(pc) { |
| 34 | + _signalRegistrations = new PosixSignalRegistration[NSIG]; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + protected override void Dispose(bool disposing) { |
| 39 | + if (disposing) { |
| 40 | + Array.ForEach(_signalRegistrations, reg => reg?.Dispose()); |
| 41 | + Array.Clear(_signalRegistrations, 0, _signalRegistrations.Length); |
| 42 | + } |
| 43 | + base.Dispose(disposing); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public override void SetPyHandler(int signalnum, object value) { |
| 48 | + // normalize handler reference |
| 49 | + if (value is int i) { |
| 50 | + value = (i == SIG_IGN) ? sig_ign : sig_dfl; |
| 51 | + } |
| 52 | + |
| 53 | + if (TryGetPyHandler(signalnum, out object? existingValue) && ReferenceEquals(existingValue, value)) { |
| 54 | + // no change |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + base.SetPyHandler(signalnum, value); |
| 59 | + |
| 60 | + if (signalnum == SIGINT) { |
| 61 | + // SIGINT is special, we need to disable the handler on the console so that we can handle Ctrl+C here |
| 62 | + if (DefaultContext.DefaultPythonContext.Console is BasicConsole console) { |
| 63 | + if (!ReferenceEquals(value, default_int_handler)) { |
| 64 | + // save the console handler so it can be restored later if necessary |
| 65 | + _consoleHandler ??= console.ConsoleCancelEventHandler; |
| 66 | + // disable the console handler |
| 67 | + console.ConsoleCancelEventHandler = null; |
| 68 | + } else if (_consoleHandler != null) { |
| 69 | + // default_int_handler: restore the console handler, which is de facto default_int_handler |
| 70 | + console.ConsoleCancelEventHandler = _consoleHandler; |
| 71 | + _consoleHandler = null; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // remember to unregister any previous handler |
| 77 | + var oldReg = _signalRegistrations[signalnum]; |
| 78 | + |
| 79 | + if (ReferenceEquals(value, sig_dfl)) { |
| 80 | + _signalRegistrations[signalnum] = null; |
| 81 | + } else if (ReferenceEquals(value, sig_ign)) { |
| 82 | + _signalRegistrations[signalnum] = PosixSignalRegistration.Create((PosixSignal)signalnum, |
| 83 | + (PosixSignalContext psc) => { |
| 84 | + psc.Cancel = true; |
| 85 | + }); |
| 86 | + } else { |
| 87 | + _signalRegistrations[signalnum] = PosixSignalRegistration.Create((PosixSignal)signalnum, |
| 88 | + (PosixSignalContext psc) => { |
| 89 | + // This is called on a thread from the thread pool for most signals, |
| 90 | + // and on a dedicated thread ".NET Signal Handler" for SIGINT, SIGQUIT, and SIGTERM. |
| 91 | + CallPythonHandler(signalnum, value); |
| 92 | + psc.Cancel = true; |
| 93 | + }); |
| 94 | + } |
| 95 | + oldReg?.Dispose(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#endif // NET |
| 102 | +#endif // FEATURE_PROCESS |
0 commit comments