proc_alive.cpp 837 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "proc_alive.h"
  2. #include <util/system/platform.h>
  3. #include <util/system/compat.h>
  4. #include <util/system/error.h>
  5. #include <util/system/winint.h>
  6. #include <errno.h>
  7. namespace NYql {
  8. bool IsProcessAlive(TProcessId pid) {
  9. #ifdef _unix_
  10. // If sending a null signal fails with the error ESRCH, then we know
  11. // the process doesn’t exist. If the call fails with the error
  12. // EPERM - the process exists, but we don’t have permission to send
  13. // a signal to it.
  14. kill(pid, 0);
  15. return errno != ESRCH;
  16. #elif defined(_win_)
  17. HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid);
  18. if (process == NULL) {
  19. return false;
  20. }
  21. DWORD ret = WaitForSingleObject(process, 0);
  22. CloseHandle(process);
  23. return ret == WAIT_TIMEOUT;
  24. #else
  25. Y_UNUSED(pid);
  26. return false;
  27. #endif
  28. }
  29. } // NYql