procmonitor.nim 882 B

1234567891011121314151617181920212223242526272829303132333435
  1. # Monitor a client process and shutdown the current process, if the client
  2. # process is found to be dead
  3. import os
  4. when defined(posix):
  5. import posix_utils
  6. import posix
  7. when defined(windows):
  8. import winlean
  9. when defined(posix):
  10. proc monitorClientProcessIdThreadProc(pid: int) {.thread.} =
  11. while true:
  12. sleep(1000)
  13. try:
  14. sendSignal(Pid(pid), 0)
  15. except:
  16. discard kill(Pid(getCurrentProcessId()), cint(SIGTERM))
  17. when defined(windows):
  18. proc monitorClientProcessIdThreadProc(pid: int) {.thread.} =
  19. var process = openProcess(SYNCHRONIZE, 0, DWORD(pid))
  20. if process != 0:
  21. discard waitForSingleObject(process, INFINITE)
  22. discard closeHandle(process)
  23. quit(0)
  24. var tid: Thread[int]
  25. proc hookProcMonitor*(pid: int) =
  26. when defined(posix) or defined(windows):
  27. createThread(tid, monitorClientProcessIdThreadProc, pid)