Platform.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Runtime.InteropServices;
  2. namespace Terminal.Gui.Drivers;
  3. internal static class Platform
  4. {
  5. private static int _suspendSignal;
  6. /// <summary>Suspends the process by sending SIGTSTP to itself</summary>
  7. /// <returns>True if the suspension was successful.</returns>
  8. public static bool Suspend ()
  9. {
  10. int signal = GetSuspendSignal ();
  11. if (signal == -1)
  12. {
  13. return false;
  14. }
  15. killpg (0, signal);
  16. return true;
  17. }
  18. private static int GetSuspendSignal ()
  19. {
  20. if (_suspendSignal != 0)
  21. {
  22. return _suspendSignal;
  23. }
  24. nint buf = Marshal.AllocHGlobal (8192);
  25. if (uname (buf) != 0)
  26. {
  27. Marshal.FreeHGlobal (buf);
  28. _suspendSignal = -1;
  29. return _suspendSignal;
  30. }
  31. try
  32. {
  33. switch (Marshal.PtrToStringAnsi (buf))
  34. {
  35. case "Darwin":
  36. case "DragonFly":
  37. case "FreeBSD":
  38. case "NetBSD":
  39. case "OpenBSD":
  40. _suspendSignal = 18;
  41. break;
  42. case "Linux":
  43. // TODO: should fetch the machine name and
  44. // if it is MIPS return 24
  45. _suspendSignal = 20;
  46. break;
  47. case "Solaris":
  48. _suspendSignal = 24;
  49. break;
  50. default:
  51. _suspendSignal = -1;
  52. break;
  53. }
  54. return _suspendSignal;
  55. }
  56. finally
  57. {
  58. Marshal.FreeHGlobal (buf);
  59. }
  60. }
  61. [DllImport ("libc")]
  62. private static extern int killpg (int pgrp, int pid);
  63. [DllImport ("libc")]
  64. private static extern int uname (nint buf);
  65. }