Launcher_UnixLike.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "Launcher_UnixLike.h"
  9. #include <AzCore/base.h>
  10. #include <AzCore/Debug/Trace.h>
  11. #include <sys/resource.h>
  12. #include <sys/types.h>
  13. #include <cerrno>
  14. #include <cstring>
  15. #include <limits.h>
  16. #include <stdlib.h>
  17. namespace
  18. {
  19. // return true if the limit was updated and setlimit needs to be called, false otherwise
  20. typedef bool(*ResourceLimitUpdater)(rlimit&);
  21. bool IncreaseMaxToInfinity(rlimit& limit)
  22. {
  23. if (limit.rlim_max != RLIM_INFINITY)
  24. {
  25. limit.rlim_max = RLIM_INFINITY;
  26. return true;
  27. }
  28. return false;
  29. }
  30. bool IncreaseCurrentToMax(rlimit& limit)
  31. {
  32. if (limit.rlim_cur < limit.rlim_max)
  33. {
  34. limit.rlim_cur = limit.rlim_max;
  35. return true;
  36. }
  37. return false;
  38. }
  39. bool IncreaseResourceLimit(int resource, ResourceLimitUpdater updateLimit)
  40. {
  41. rlimit limit;
  42. if (getrlimit(resource, &limit) != 0)
  43. {
  44. AZ_Warning("Launcher", false, "[WARNING] Unable to get limit for resource %d. Error: %s", resource, strerror(errno));
  45. }
  46. if (updateLimit(limit))
  47. {
  48. if (setrlimit(resource, &limit) != 0)
  49. {
  50. AZ_Warning("Launcher", false, "[WARNING] Unable to update resource limit for resource %d. Error: %s", resource, strerror(errno));
  51. }
  52. }
  53. return true;
  54. }
  55. }
  56. namespace O3DELauncher
  57. {
  58. bool IncreaseResourceLimits()
  59. {
  60. return (IncreaseResourceLimit(RLIMIT_CORE, IncreaseMaxToInfinity)
  61. && IncreaseResourceLimit(RLIMIT_STACK, IncreaseCurrentToMax));
  62. }
  63. const char* GetAbsolutePath(char* absolutePathBuffer, size_t absolutePathBufferSize, const char* inputPath)
  64. {
  65. // Normalize the path
  66. AZ_Assert(absolutePathBufferSize>0,"Input buffer size for absolutePathBuffer must be greater than zero.");
  67. char normalizedFullPathBuffer[PATH_MAX];
  68. const char* normalizedFullPath = NULL;
  69. if (strlen(inputPath)>0)
  70. {
  71. normalizedFullPath = realpath(inputPath, normalizedFullPathBuffer);
  72. }
  73. if (normalizedFullPath == NULL)
  74. {
  75. // Unable to resolve the absolute path, set the buffer to blank
  76. absolutePathBuffer[0] = '\0';
  77. }
  78. else
  79. {
  80. // The path was resolved to an absolute path, copy to the input buffer the result
  81. azstrncpy(absolutePathBuffer, absolutePathBufferSize, normalizedFullPath,strlen(normalizedFullPath)+1);
  82. }
  83. return absolutePathBuffer;
  84. }
  85. }