Beispiel #1
0
 def _init(self, pid, _ignore_nsp=False):
     if pid is None:
         pid = os.getpid()
     else:
         if not PY3 and not isinstance(pid, (int, long)):  # noqa F821
             raise TypeError("pid must be an integer (got %r)" % pid)
         if pid < 0:
             raise ValueError("pid must be a positive integer (got %s)" %
                              pid)
     self._pid = pid
     self._create_time = None
     # used for caching on Windows only (on POSIX ppid may change)
     self._ppid = None
     # platform-specific modules define an _psplatform.Process
     # implementation class
     self._proc = _psplatform.Process(pid)
     self._last_sys_cpu_times = None
     self._last_proc_cpu_times = None
     # cache creation time for later use in is_running() method
     try:
         self.create_time()
     except NoSuchProcess:
         if not _ignore_nsp:
             raise NoSuchProcess()
         else:
             self._gone = True
     # This pair is supposed to indentify a Process instance
     # univocally over time (the PID alone is not enough as
     # it might refer to a process whose PID has been reused).
     # This will be used later in __eq__() and is_running().
     self._ident = (self.pid, self._create_time)
Beispiel #2
0
 def wrapper(self, *args, **kwargs):
     try:
         return fun(self, *args, **kwargs)
     except OSError as err:
         if err.errno in ACCESS_DENIED_ERRSET:
             raise AccessDenied()
         if err.errno == errno.ESRCH:
             raise NoSuchProcess()
         raise
Beispiel #3
0
 def wrapper(self, *args, **kwargs):
     try:
         return fun(self, *args, **kwargs)
     except EnvironmentError as err:
         if err.errno in (errno.EPERM, errno.EACCES):
             raise AccessDenied()
         # ESRCH (no such process) can be raised on read() if
         # process is gone in the meantime.
         if err.errno == errno.ESRCH:
             raise NoSuchProcess()
         # ENOENT (no such file or directory) can be raised on open().
         if err.errno == errno.ENOENT and not os.path.exists(
             "%s/%s" % (self._procfs_path, self.pid)
         ):
             raise NoSuchProcess()
         # Note: zombies will keep existing under /proc until they're
         # gone so there's no way to distinguish them in here.
         raise
Beispiel #4
0
 def cpu_times(self):
     creation_time = ctypes.c_ulonglong()
     exit_time = ctypes.c_ulonglong()
     kernel_time = FILETIME()
     user_time = FILETIME()
     hProcess = handle_from_pid(self.pid)
     ret = ctypes.windll.kernel32.GetProcessTimes(
         hProcess,
         ctypes.byref(creation_time),
         ctypes.byref(exit_time),
         ctypes.byref(kernel_time),
         ctypes.byref(user_time),
     )
     kernel32.CloseHandle(hProcess)
     if not ret:
         raise NoSuchProcess()
     user = HI_T * user_time.dwHighDateTime + LO_T * user_time.dwLowDateTime
     kernel = HI_T * kernel_time.dwHighDateTime + LO_T * kernel_time.dwLowDateTime
     return pcputimes(user, kernel, 0.0, 0.0)
Beispiel #5
0
    def create_time(self):
        hProcess = handle_from_pid(self.pid)

        creation_time = FILETIME()
        exit_time = ctypes.c_ulonglong()
        kernel_time = ctypes.c_ulonglong()
        user_time = ctypes.c_ulonglong()

        ret = ctypes.windll.kernel32.GetProcessTimes(
            hProcess,
            ctypes.byref(creation_time),
            ctypes.byref(exit_time),
            ctypes.byref(kernel_time),
            ctypes.byref(user_time),
        )
        kernel32.CloseHandle(hProcess)
        if not ret:
            raise NoSuchProcess()
        return HI_T * creation_time.dwHighDateTime + LO_T * creation_time.dwLowDateTime
Beispiel #6
0
 def _get_raw_meminfo(self):
     hProcess = handle_from_pid(self.pid)
     counters = PROCESS_MEMORY_COUNTERS_EX()
     ret = psapi.GetProcessMemoryInfo(
         hProcess, ctypes.byref(counters), ctypes.sizeof(counters)
     )
     if not ret:
         raise NoSuchProcess()
     kernel32.CloseHandle(hProcess)
     info = (
         counters.PageFaultCount,
         counters.PeakWorkingSetSize,
         counters.WorkingSetSize,
         counters.QuotaPeakPagedPoolUsage,
         counters.QuotaPagedPoolUsage,
         counters.QuotaPeakNonPagedPoolUsage,
         counters.QuotaNonPagedPoolUsage,
         counters.PagefileUsage,
         counters.PeakPagefileUsage,
     )
     return info
Beispiel #7
0
 def cmdline(self):
     try:
         # pass as the startupinfo keyword argument:
         out, err = subprocess.Popen(
             "wmic path win32_process get Processid,Commandline",
             stdout=subprocess.PIPE,
             stderr=subprocess.PIPE,
             stdin=subprocess.PIPE,
         ).communicate()
     except subprocess.CalledProcessError:
         pass
     elements = out.split()
     b_pid = str(self.pid).encode("ascii")
     found = False
     for pos, element in enumerate(elements):
         if element == b_pid:
             found = True
             break
     if not found:
         raise NoSuchProcess()
     return elements[pos - 1].decode("utf-8", "slashescape")