namespace UCVL.VPNPlus; internal static class ClientShutdownScope { internal record Instance(int Id, int SessionId, string? Executable); // Legacy events are shared within the logon session. Refuse an ambiguous // same-name process rather than signaling or closing an unrelated instance. internal static bool IsUnambiguous(string expected, int session, IEnumerable processes) => processes.Where(p => p.SessionId == session).All(p => !string.IsNullOrWhiteSpace(p.Executable) && string.Equals(Path.GetFullPath(p.Executable), Path.GetFullPath(expected), StringComparison.OrdinalIgnoreCase)); // Call only after path/session verification and pinning exact handles. // All instances consume one shared budget; a timeout never kills a client. internal static bool WaitForExit(IEnumerable processes, TimeSpan? budget = null) { var watch = System.Diagnostics.Stopwatch.StartNew(); var limit = budget ?? TimeSpan.FromSeconds(90); foreach (var process in processes) { var remaining = (int)Math.Clamp(limit.TotalMilliseconds - watch.ElapsedMilliseconds, 0, int.MaxValue); if (!process.HasExited && (remaining == 0 || !process.WaitForExit(remaining))) return false; if (process.ExitCode != 0) return false; } return true; } internal static bool SelfTest() { const string expected = @"C:\fixture\installed\VPNPlus.exe"; return IsUnambiguous(expected, 1, [new(10,1,expected)]) && IsUnambiguous(expected, 1, [new(10,2,@"D:\another-user\VPNPlus.exe")]) && IsUnambiguous(expected, 1, [new(10,1,expected.ToLowerInvariant())]) && !IsUnambiguous(expected, 1, [new(10,1,expected),new(11,1,@"D:\another-app\VPNPlus.exe")]) && !IsUnambiguous(expected, 1, [new(10,1,null)]); } }