Process.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_POSIX && !IL2CPP_TARGET_DARWIN && !RUNTIME_TINY
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #if IL2CPP_TARGET_LINUX
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #endif
  9. #include "os/Process.h"
  10. #include "utils/Expected.h"
  11. #include "utils/Il2CppError.h"
  12. struct ProcessHandle
  13. {
  14. pid_t pid;
  15. };
  16. namespace il2cpp
  17. {
  18. namespace os
  19. {
  20. int Process::GetCurrentProcessId()
  21. {
  22. return getpid();
  23. }
  24. utils::Expected<ProcessHandle*> Process::GetProcess(int processId)
  25. {
  26. // If/when we implement the CreateProcess_internal icall we will likely
  27. // need to so something smarter here to find the process if we did
  28. // not create it and return a known pseudo-handle. For now this
  29. // is sufficient though.
  30. return (ProcessHandle*)(intptr_t)processId;
  31. }
  32. void Process::FreeProcess(ProcessHandle* handle)
  33. {
  34. // We have nothing to do here.
  35. }
  36. utils::Expected<std::string> Process::GetProcessName(ProcessHandle* handle)
  37. {
  38. #if IL2CPP_TARGET_LINUX
  39. char pathBuffer[32];
  40. snprintf(pathBuffer, IL2CPP_ARRAY_SIZE(pathBuffer), "/proc/%d/comm", static_cast<int>(reinterpret_cast<intptr_t>(handle)));
  41. int fileHandle = open(pathBuffer, 0, O_RDONLY);
  42. if (fileHandle == -1)
  43. return std::string();
  44. std::string processName;
  45. char buffer[256];
  46. ssize_t bytesRead;
  47. do
  48. {
  49. bytesRead = read(fileHandle, buffer, IL2CPP_ARRAY_SIZE(buffer));
  50. if (bytesRead > 0)
  51. processName.append(buffer, bytesRead);
  52. }
  53. while (bytesRead == IL2CPP_ARRAY_SIZE(buffer));
  54. close(fileHandle);
  55. // Truncate name to first line ending
  56. size_t index = processName.find_first_of('\n');
  57. if (index != std::string::npos)
  58. processName.resize(index);
  59. return processName;
  60. #else
  61. return utils::Il2CppError(utils::NotSupported, "GetProcessName is not supported for non-Windows/OSX/Linux desktop platforms");
  62. #endif
  63. }
  64. intptr_t Process::GetMainWindowHandle(int32_t pid)
  65. {
  66. return 0;
  67. }
  68. }
  69. }
  70. #endif