LinuxHelper.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Runtime.Versioning;
  5. namespace Ryujinx.Common.Helper
  6. {
  7. [SupportedOSPlatform("linux")]
  8. public static class LinuxHelper
  9. {
  10. // NOTE: This value was determined by manual tests and might need to be increased again.
  11. public const int RecommendedVmMaxMapCount = 524288;
  12. public const string VmMaxMapCountPath = "/proc/sys/vm/max_map_count";
  13. public const string SysCtlConfigPath = "/etc/sysctl.d/99-Ryujinx.conf";
  14. public static int VmMaxMapCount => int.Parse(File.ReadAllText(VmMaxMapCountPath));
  15. public static string PkExecPath { get; } = GetBinaryPath("pkexec");
  16. private static string GetBinaryPath(string binary)
  17. {
  18. string pathVar = Environment.GetEnvironmentVariable("PATH");
  19. if (pathVar is null || string.IsNullOrEmpty(binary))
  20. {
  21. return null;
  22. }
  23. foreach (var searchPath in pathVar.Split(":", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
  24. {
  25. string binaryPath = Path.Combine(searchPath, binary);
  26. if (File.Exists(binaryPath))
  27. {
  28. return binaryPath;
  29. }
  30. }
  31. return null;
  32. }
  33. public static int RunPkExec(string command)
  34. {
  35. if (PkExecPath == null)
  36. {
  37. return 1;
  38. }
  39. using Process process = new()
  40. {
  41. StartInfo =
  42. {
  43. FileName = PkExecPath,
  44. ArgumentList = { "sh", "-c", command },
  45. },
  46. };
  47. process.Start();
  48. process.WaitForExit();
  49. return process.ExitCode;
  50. }
  51. }
  52. }