ProcessHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. namespace Microsoft.Extensions.Internal
  9. {
  10. internal static class ProcessExtensions
  11. {
  12. private static readonly bool _isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  13. private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30);
  14. public static void KillTree(this Process process)
  15. {
  16. process.KillTree(_defaultTimeout);
  17. }
  18. public static void KillTree(this Process process, TimeSpan timeout)
  19. {
  20. string stdout;
  21. if (_isWindows)
  22. {
  23. RunProcessAndWaitForExit(
  24. "taskkill",
  25. $"/T /F /PID {process.Id}",
  26. timeout,
  27. out stdout);
  28. }
  29. else
  30. {
  31. var children = new HashSet<int>();
  32. GetAllChildIdsUnix(process.Id, children, timeout);
  33. foreach (var childId in children)
  34. {
  35. KillProcessUnix(childId, timeout);
  36. }
  37. KillProcessUnix(process.Id, timeout);
  38. }
  39. }
  40. private static void GetAllChildIdsUnix(int parentId, ISet<int> children, TimeSpan timeout)
  41. {
  42. string stdout;
  43. var exitCode = RunProcessAndWaitForExit(
  44. "pgrep",
  45. $"-P {parentId}",
  46. timeout,
  47. out stdout);
  48. if (exitCode == 0 && !string.IsNullOrEmpty(stdout))
  49. {
  50. using (var reader = new StringReader(stdout))
  51. {
  52. while (true)
  53. {
  54. var text = reader.ReadLine();
  55. if (text == null)
  56. {
  57. return;
  58. }
  59. int id;
  60. if (int.TryParse(text, out id))
  61. {
  62. children.Add(id);
  63. // Recursively get the children
  64. GetAllChildIdsUnix(id, children, timeout);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. private static void KillProcessUnix(int processId, TimeSpan timeout)
  71. {
  72. string stdout;
  73. RunProcessAndWaitForExit(
  74. "kill",
  75. $"-TERM {processId}",
  76. timeout,
  77. out stdout);
  78. }
  79. private static int RunProcessAndWaitForExit(string fileName, string arguments, TimeSpan timeout, out string stdout)
  80. {
  81. var startInfo = new ProcessStartInfo
  82. {
  83. FileName = fileName,
  84. Arguments = arguments,
  85. RedirectStandardOutput = true,
  86. UseShellExecute = false
  87. };
  88. var process = Process.Start(startInfo);
  89. stdout = null;
  90. if (process.WaitForExit((int)timeout.TotalMilliseconds))
  91. {
  92. stdout = process.StandardOutput.ReadToEnd();
  93. }
  94. else
  95. {
  96. process.Kill();
  97. }
  98. return process.ExitCode;
  99. }
  100. }
  101. }