/pypags/psutil-0.7.1/psutil/_psposix.py

https://bitbucket.org/chenfuzhi/scan_proxy_ip
Python | 121 lines | 93 code | 9 blank | 19 comment | 17 complexity | 057b9d548738c592c1996da35f8e2817 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Routines common to all posix systems."""
  6. import os
  7. import errno
  8. import psutil
  9. import sys
  10. import time
  11. import glob
  12. from psutil._error import TimeoutExpired
  13. from psutil._common import nt_diskinfo, usage_percent, memoize
  14. def pid_exists(pid):
  15. """Check whether pid exists in the current process table."""
  16. if pid < 0:
  17. return False
  18. try:
  19. os.kill(pid, 0)
  20. except OSError:
  21. e = sys.exc_info()[1]
  22. return e.errno == errno.EPERM
  23. else:
  24. return True
  25. def wait_pid(pid, timeout=None):
  26. """Wait for process with pid 'pid' to terminate and return its
  27. exit status code as an integer.
  28. If pid is not a children of os.getpid() (current process) just
  29. waits until the process disappears and return None.
  30. If pid does not exist at all return None immediately.
  31. Raise TimeoutExpired on timeout expired.
  32. """
  33. def check_timeout(delay):
  34. if timeout is not None:
  35. if timer() >= stop_at:
  36. raise TimeoutExpired(pid)
  37. time.sleep(delay)
  38. return min(delay * 2, 0.04)
  39. timer = getattr(time, 'monotonic', time.time)
  40. if timeout is not None:
  41. waitcall = lambda: os.waitpid(pid, os.WNOHANG)
  42. stop_at = timer() + timeout
  43. else:
  44. waitcall = lambda: os.waitpid(pid, 0)
  45. delay = 0.0001
  46. while 1:
  47. try:
  48. retpid, status = waitcall()
  49. except OSError:
  50. err = sys.exc_info()[1]
  51. if err.errno == errno.EINTR:
  52. delay = check_timeout(delay)
  53. continue
  54. elif err.errno == errno.ECHILD:
  55. # This has two meanings:
  56. # - pid is not a child of os.getpid() in which case
  57. # we keep polling until it's gone
  58. # - pid never existed in the first place
  59. # In both cases we'll eventually return None as we
  60. # can't determine its exit status code.
  61. while 1:
  62. if pid_exists(pid):
  63. delay = check_timeout(delay)
  64. else:
  65. return
  66. else:
  67. raise
  68. else:
  69. if retpid == 0:
  70. # WNOHANG was used, pid is still running
  71. delay = check_timeout(delay)
  72. continue
  73. # process exited due to a signal; return the integer of
  74. # that signal
  75. if os.WIFSIGNALED(status):
  76. return os.WTERMSIG(status)
  77. # process exited using exit(2) system call; return the
  78. # integer exit(2) system call has been called with
  79. elif os.WIFEXITED(status):
  80. return os.WEXITSTATUS(status)
  81. else:
  82. # should never happen
  83. raise RuntimeError("unknown process exit status")
  84. def get_disk_usage(path):
  85. """Return disk usage associated with path."""
  86. st = os.statvfs(path)
  87. free = (st.f_bavail * st.f_frsize)
  88. total = (st.f_blocks * st.f_frsize)
  89. used = (st.f_blocks - st.f_bfree) * st.f_frsize
  90. percent = usage_percent(used, total, _round=1)
  91. # NB: the percentage is -5% than what shown by df due to
  92. # reserved blocks that we are currently not considering:
  93. # http://goo.gl/sWGbH
  94. return nt_diskinfo(total, used, free, percent)
  95. @memoize
  96. def _get_terminal_map():
  97. ret = {}
  98. ls = glob.glob('/dev/tty*') + glob.glob('/dev/pts/*')
  99. for name in ls:
  100. assert name not in ret
  101. try:
  102. ret[os.stat(name).st_rdev] = name
  103. except OSError:
  104. err = sys.exc_info()[1]
  105. if err.errno != errno.ENOENT:
  106. raise
  107. return ret