_pswindows.py 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Windows platform implementation."""
  5. import contextlib
  6. import enum
  7. import functools
  8. import os
  9. import signal
  10. import sys
  11. import threading
  12. import time
  13. from . import _common
  14. from . import _ntuples as ntp
  15. from ._common import ENCODING
  16. from ._common import AccessDenied
  17. from ._common import NoSuchProcess
  18. from ._common import TimeoutExpired
  19. from ._common import conn_tmap
  20. from ._common import conn_to_ntuple
  21. from ._common import debug
  22. from ._common import isfile_strict
  23. from ._common import memoize
  24. from ._common import memoize_when_activated
  25. from ._common import parse_environ_block
  26. from ._common import usage_percent
  27. from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS
  28. from ._psutil_windows import BELOW_NORMAL_PRIORITY_CLASS
  29. from ._psutil_windows import HIGH_PRIORITY_CLASS
  30. from ._psutil_windows import IDLE_PRIORITY_CLASS
  31. from ._psutil_windows import NORMAL_PRIORITY_CLASS
  32. from ._psutil_windows import REALTIME_PRIORITY_CLASS
  33. try:
  34. from . import _psutil_windows as cext
  35. except ImportError as err:
  36. if (
  37. str(err).lower().startswith("dll load failed")
  38. and sys.getwindowsversion()[0] < 6
  39. ):
  40. # We may get here if:
  41. # 1) we are on an old Windows version
  42. # 2) psutil was installed via pip + wheel
  43. # See: https://github.com/giampaolo/psutil/issues/811
  44. msg = "this Windows version is too old (< Windows Vista); "
  45. msg += "psutil 3.4.2 is the latest version which supports Windows "
  46. msg += "2000, XP and 2003 server"
  47. raise RuntimeError(msg) from err
  48. else:
  49. raise
  50. # process priority constants, import from __init__.py:
  51. # http://msdn.microsoft.com/en-us/library/ms686219(v=vs.85).aspx
  52. # fmt: off
  53. __extra__all__ = [
  54. "win_service_iter", "win_service_get",
  55. # Process priority
  56. "ABOVE_NORMAL_PRIORITY_CLASS", "BELOW_NORMAL_PRIORITY_CLASS",
  57. "HIGH_PRIORITY_CLASS", "IDLE_PRIORITY_CLASS", "NORMAL_PRIORITY_CLASS",
  58. "REALTIME_PRIORITY_CLASS",
  59. # IO priority
  60. "IOPRIO_VERYLOW", "IOPRIO_LOW", "IOPRIO_NORMAL", "IOPRIO_HIGH",
  61. # others
  62. "CONN_DELETE_TCB", "AF_LINK",
  63. ]
  64. # fmt: on
  65. # =====================================================================
  66. # --- globals
  67. # =====================================================================
  68. CONN_DELETE_TCB = "DELETE_TCB"
  69. ERROR_PARTIAL_COPY = 299
  70. PYPY = '__pypy__' in sys.builtin_module_names
  71. AddressFamily = enum.IntEnum('AddressFamily', {'AF_LINK': -1})
  72. AF_LINK = AddressFamily.AF_LINK
  73. TCP_STATUSES = {
  74. cext.MIB_TCP_STATE_ESTAB: _common.CONN_ESTABLISHED,
  75. cext.MIB_TCP_STATE_SYN_SENT: _common.CONN_SYN_SENT,
  76. cext.MIB_TCP_STATE_SYN_RCVD: _common.CONN_SYN_RECV,
  77. cext.MIB_TCP_STATE_FIN_WAIT1: _common.CONN_FIN_WAIT1,
  78. cext.MIB_TCP_STATE_FIN_WAIT2: _common.CONN_FIN_WAIT2,
  79. cext.MIB_TCP_STATE_TIME_WAIT: _common.CONN_TIME_WAIT,
  80. cext.MIB_TCP_STATE_CLOSED: _common.CONN_CLOSE,
  81. cext.MIB_TCP_STATE_CLOSE_WAIT: _common.CONN_CLOSE_WAIT,
  82. cext.MIB_TCP_STATE_LAST_ACK: _common.CONN_LAST_ACK,
  83. cext.MIB_TCP_STATE_LISTEN: _common.CONN_LISTEN,
  84. cext.MIB_TCP_STATE_CLOSING: _common.CONN_CLOSING,
  85. cext.MIB_TCP_STATE_DELETE_TCB: CONN_DELETE_TCB,
  86. cext.PSUTIL_CONN_NONE: _common.CONN_NONE,
  87. }
  88. class Priority(enum.IntEnum):
  89. ABOVE_NORMAL_PRIORITY_CLASS = ABOVE_NORMAL_PRIORITY_CLASS
  90. BELOW_NORMAL_PRIORITY_CLASS = BELOW_NORMAL_PRIORITY_CLASS
  91. HIGH_PRIORITY_CLASS = HIGH_PRIORITY_CLASS
  92. IDLE_PRIORITY_CLASS = IDLE_PRIORITY_CLASS
  93. NORMAL_PRIORITY_CLASS = NORMAL_PRIORITY_CLASS
  94. REALTIME_PRIORITY_CLASS = REALTIME_PRIORITY_CLASS
  95. globals().update(Priority.__members__)
  96. class IOPriority(enum.IntEnum):
  97. IOPRIO_VERYLOW = 0
  98. IOPRIO_LOW = 1
  99. IOPRIO_NORMAL = 2
  100. IOPRIO_HIGH = 3
  101. globals().update(IOPriority.__members__)
  102. pinfo_map = dict(
  103. num_handles=0,
  104. ctx_switches=1,
  105. user_time=2,
  106. kernel_time=3,
  107. create_time=4,
  108. num_threads=5,
  109. io_rcount=6,
  110. io_wcount=7,
  111. io_rbytes=8,
  112. io_wbytes=9,
  113. io_count_others=10,
  114. io_bytes_others=11,
  115. num_page_faults=12,
  116. peak_wset=13,
  117. wset=14,
  118. peak_paged_pool=15,
  119. paged_pool=16,
  120. peak_non_paged_pool=17,
  121. non_paged_pool=18,
  122. pagefile=19,
  123. peak_pagefile=20,
  124. mem_private=21,
  125. )
  126. # =====================================================================
  127. # --- utils
  128. # =====================================================================
  129. @functools.lru_cache(maxsize=512)
  130. def convert_dos_path(s):
  131. r"""Convert paths using native DOS format like:
  132. "\Device\HarddiskVolume1\Windows\systemew\file.txt" or
  133. "\??\C:\Windows\systemew\file.txt"
  134. into:
  135. "C:\Windows\systemew\file.txt".
  136. """
  137. if s.startswith('\\\\'):
  138. return s
  139. rawdrive = '\\'.join(s.split('\\')[:3])
  140. if rawdrive in {"\\??\\UNC", "\\Device\\Mup"}:
  141. rawdrive = '\\'.join(s.split('\\')[:5])
  142. driveletter = '\\\\' + '\\'.join(s.split('\\')[3:5])
  143. elif rawdrive.startswith('\\??\\'):
  144. driveletter = s.split('\\')[2]
  145. else:
  146. driveletter = cext.QueryDosDevice(rawdrive)
  147. remainder = s[len(rawdrive) :]
  148. return os.path.join(driveletter, remainder)
  149. @memoize
  150. def getpagesize():
  151. return cext.getpagesize()
  152. # =====================================================================
  153. # --- memory
  154. # =====================================================================
  155. def virtual_memory():
  156. """System virtual memory as a namedtuple."""
  157. mem = cext.virtual_mem()
  158. totphys, availphys, _totsys, _availsys = mem
  159. total = totphys
  160. avail = availphys
  161. free = availphys
  162. used = total - avail
  163. percent = usage_percent((total - avail), total, round_=1)
  164. return ntp.svmem(total, avail, percent, used, free)
  165. def swap_memory():
  166. """Swap system memory as a (total, used, free, sin, sout) tuple."""
  167. mem = cext.virtual_mem()
  168. total_phys = mem[0]
  169. total_system = mem[2]
  170. # system memory (commit total/limit) is the sum of physical and swap
  171. # thus physical memory values need to be subtracted to get swap values
  172. total = total_system - total_phys
  173. # commit total is incremented immediately (decrementing free_system)
  174. # while the corresponding free physical value is not decremented until
  175. # pages are accessed, so we can't use free system memory for swap.
  176. # instead, we calculate page file usage based on performance counter
  177. if total > 0:
  178. percentswap = cext.swap_percent()
  179. used = int(0.01 * percentswap * total)
  180. else:
  181. percentswap = 0.0
  182. used = 0
  183. free = total - used
  184. percent = round(percentswap, 1)
  185. return ntp.sswap(total, used, free, percent, 0, 0)
  186. # malloc / heap functions
  187. heap_info = cext.heap_info
  188. heap_trim = cext.heap_trim
  189. # =====================================================================
  190. # --- disk
  191. # =====================================================================
  192. disk_io_counters = cext.disk_io_counters
  193. def disk_usage(path):
  194. """Return disk usage associated with path."""
  195. if isinstance(path, bytes):
  196. # XXX: do we want to use "strict"? Probably yes, in order
  197. # to fail immediately. After all we are accepting input here...
  198. path = path.decode(ENCODING, errors="strict")
  199. total, used, free = cext.disk_usage(path)
  200. percent = usage_percent(used, total, round_=1)
  201. return ntp.sdiskusage(total, used, free, percent)
  202. def disk_partitions(all):
  203. """Return disk partitions."""
  204. rawlist = cext.disk_partitions(all)
  205. return [ntp.sdiskpart(*x) for x in rawlist]
  206. # =====================================================================
  207. # --- CPU
  208. # =====================================================================
  209. def cpu_times():
  210. """Return system CPU times as a named tuple."""
  211. user, system, idle = cext.cpu_times()
  212. # Internally, GetSystemTimes() is used, and it doesn't return
  213. # interrupt and dpc times. cext.per_cpu_times() does, so we
  214. # rely on it to get those only.
  215. percpu_summed = ntp.scputimes(
  216. *[sum(n) for n in zip(*cext.per_cpu_times())]
  217. )
  218. return ntp.scputimes(
  219. user, system, idle, percpu_summed.interrupt, percpu_summed.dpc
  220. )
  221. def per_cpu_times():
  222. """Return system per-CPU times as a list of named tuples."""
  223. ret = []
  224. for user, system, idle, interrupt, dpc in cext.per_cpu_times():
  225. item = ntp.scputimes(user, system, idle, interrupt, dpc)
  226. ret.append(item)
  227. return ret
  228. def cpu_count_logical():
  229. """Return the number of logical CPUs in the system."""
  230. return cext.cpu_count_logical()
  231. def cpu_count_cores():
  232. """Return the number of CPU cores in the system."""
  233. return cext.cpu_count_cores()
  234. def cpu_stats():
  235. """Return CPU statistics."""
  236. ctx_switches, interrupts, _dpcs, syscalls = cext.cpu_stats()
  237. soft_interrupts = 0
  238. return ntp.scpustats(ctx_switches, interrupts, soft_interrupts, syscalls)
  239. def cpu_freq():
  240. """Return CPU frequency.
  241. On Windows per-cpu frequency is not supported.
  242. """
  243. curr, max_ = cext.cpu_freq()
  244. min_ = 0.0
  245. return [ntp.scpufreq(float(curr), min_, float(max_))]
  246. _loadavg_initialized = False
  247. _lock = threading.Lock()
  248. def _getloadavg_impl():
  249. # Drop to 2 decimal points which is what Linux does
  250. raw_loads = cext.getloadavg()
  251. return tuple(round(load, 2) for load in raw_loads)
  252. def getloadavg():
  253. """Return the number of processes in the system run queue averaged
  254. over the last 1, 5, and 15 minutes respectively as a tuple.
  255. """
  256. global _loadavg_initialized
  257. if _loadavg_initialized:
  258. return _getloadavg_impl()
  259. with _lock:
  260. if not _loadavg_initialized:
  261. cext.init_loadavg_counter()
  262. _loadavg_initialized = True
  263. return _getloadavg_impl()
  264. # =====================================================================
  265. # --- network
  266. # =====================================================================
  267. def net_connections(kind, _pid=-1):
  268. """Return socket connections. If pid == -1 return system-wide
  269. connections (as opposed to connections opened by one process only).
  270. """
  271. families, types = conn_tmap[kind]
  272. rawlist = cext.net_connections(_pid, families, types)
  273. ret = set()
  274. for item in rawlist:
  275. fd, fam, type, laddr, raddr, status, pid = item
  276. nt = conn_to_ntuple(
  277. fd,
  278. fam,
  279. type,
  280. laddr,
  281. raddr,
  282. status,
  283. TCP_STATUSES,
  284. pid=pid if _pid == -1 else None,
  285. )
  286. ret.add(nt)
  287. return list(ret)
  288. def net_if_stats():
  289. """Get NIC stats (isup, duplex, speed, mtu)."""
  290. ret = {}
  291. rawdict = cext.net_if_stats()
  292. for name, items in rawdict.items():
  293. isup, duplex, speed, mtu = items
  294. if hasattr(_common, 'NicDuplex'):
  295. duplex = _common.NicDuplex(duplex)
  296. ret[name] = ntp.snicstats(isup, duplex, speed, mtu, '')
  297. return ret
  298. def net_io_counters():
  299. """Return network I/O statistics for every network interface
  300. installed on the system as a dict of raw tuples.
  301. """
  302. return cext.net_io_counters()
  303. def net_if_addrs():
  304. """Return the addresses associated to each NIC."""
  305. return cext.net_if_addrs()
  306. # =====================================================================
  307. # --- sensors
  308. # =====================================================================
  309. def sensors_battery():
  310. """Return battery information."""
  311. # For constants meaning see:
  312. # https://msdn.microsoft.com/en-us/library/windows/desktop/
  313. # aa373232(v=vs.85).aspx
  314. acline_status, flags, percent, secsleft = cext.sensors_battery()
  315. power_plugged = acline_status == 1
  316. no_battery = bool(flags & 128)
  317. charging = bool(flags & 8)
  318. if no_battery:
  319. return None
  320. if power_plugged or charging:
  321. secsleft = _common.POWER_TIME_UNLIMITED
  322. elif secsleft == -1:
  323. secsleft = _common.POWER_TIME_UNKNOWN
  324. return ntp.sbattery(percent, secsleft, power_plugged)
  325. # =====================================================================
  326. # --- other system functions
  327. # =====================================================================
  328. _last_btime = 0
  329. def boot_time():
  330. """The system boot time expressed in seconds since the epoch. This
  331. also includes the time spent during hybernate / suspend.
  332. """
  333. # This dirty hack is to adjust the precision of the returned
  334. # value which may have a 1 second fluctuation, see:
  335. # https://github.com/giampaolo/psutil/issues/1007
  336. global _last_btime
  337. ret = time.time() - cext.uptime()
  338. if abs(ret - _last_btime) <= 1:
  339. return _last_btime
  340. else:
  341. _last_btime = ret
  342. return ret
  343. def users():
  344. """Return currently connected users as a list of namedtuples."""
  345. retlist = []
  346. rawlist = cext.users()
  347. for item in rawlist:
  348. user, hostname, tstamp = item
  349. nt = ntp.suser(user, None, hostname, tstamp, None)
  350. retlist.append(nt)
  351. return retlist
  352. # =====================================================================
  353. # --- Windows services
  354. # =====================================================================
  355. def win_service_iter():
  356. """Yields a list of WindowsService instances."""
  357. for name, display_name in cext.winservice_enumerate():
  358. yield WindowsService(name, display_name)
  359. def win_service_get(name):
  360. """Open a Windows service and return it as a WindowsService instance."""
  361. service = WindowsService(name, None)
  362. service._display_name = service._query_config()['display_name']
  363. return service
  364. class WindowsService: # noqa: PLW1641
  365. """Represents an installed Windows service."""
  366. def __init__(self, name, display_name):
  367. self._name = name
  368. self._display_name = display_name
  369. def __str__(self):
  370. details = f"(name={self._name!r}, display_name={self._display_name!r})"
  371. return f"{self.__class__.__name__}{details}"
  372. def __repr__(self):
  373. return f"<{self.__str__()} at {id(self)}>"
  374. def __eq__(self, other):
  375. # Test for equality with another WindosService object based
  376. # on name.
  377. if not isinstance(other, WindowsService):
  378. return NotImplemented
  379. return self._name == other._name
  380. def __ne__(self, other):
  381. return not self == other
  382. def _query_config(self):
  383. with self._wrap_exceptions():
  384. display_name, binpath, username, start_type = (
  385. cext.winservice_query_config(self._name)
  386. )
  387. # XXX - update _self.display_name?
  388. return dict(
  389. display_name=display_name,
  390. binpath=binpath,
  391. username=username,
  392. start_type=start_type,
  393. )
  394. def _query_status(self):
  395. with self._wrap_exceptions():
  396. status, pid = cext.winservice_query_status(self._name)
  397. if pid == 0:
  398. pid = None
  399. return dict(status=status, pid=pid)
  400. @contextlib.contextmanager
  401. def _wrap_exceptions(self):
  402. """Ctx manager which translates bare OSError and WindowsError
  403. exceptions into NoSuchProcess and AccessDenied.
  404. """
  405. try:
  406. yield
  407. except OSError as err:
  408. name = self._name
  409. if is_permission_err(err):
  410. msg = (
  411. f"service {name!r} is not querable (not enough privileges)"
  412. )
  413. raise AccessDenied(pid=None, name=name, msg=msg) from err
  414. elif err.winerror in {
  415. cext.ERROR_INVALID_NAME,
  416. cext.ERROR_SERVICE_DOES_NOT_EXIST,
  417. }:
  418. msg = f"service {name!r} does not exist"
  419. raise NoSuchProcess(pid=None, name=name, msg=msg) from err
  420. else:
  421. raise
  422. # config query
  423. def name(self):
  424. """The service name. This string is how a service is referenced
  425. and can be passed to win_service_get() to get a new
  426. WindowsService instance.
  427. """
  428. return self._name
  429. def display_name(self):
  430. """The service display name. The value is cached when this class
  431. is instantiated.
  432. """
  433. return self._display_name
  434. def binpath(self):
  435. """The fully qualified path to the service binary/exe file as
  436. a string, including command line arguments.
  437. """
  438. return self._query_config()['binpath']
  439. def username(self):
  440. """The name of the user that owns this service."""
  441. return self._query_config()['username']
  442. def start_type(self):
  443. """A string which can either be "automatic", "manual" or
  444. "disabled".
  445. """
  446. return self._query_config()['start_type']
  447. # status query
  448. def pid(self):
  449. """The process PID, if any, else None. This can be passed
  450. to Process class to control the service's process.
  451. """
  452. return self._query_status()['pid']
  453. def status(self):
  454. """Service status as a string."""
  455. return self._query_status()['status']
  456. def description(self):
  457. """Service long description."""
  458. return cext.winservice_query_descr(self.name())
  459. # utils
  460. def as_dict(self):
  461. """Utility method retrieving all the information above as a
  462. dictionary.
  463. """
  464. d = self._query_config()
  465. d.update(self._query_status())
  466. d['name'] = self.name()
  467. d['display_name'] = self.display_name()
  468. d['description'] = self.description()
  469. return d
  470. # actions
  471. # XXX: the necessary C bindings for start() and stop() are
  472. # implemented but for now I prefer not to expose them.
  473. # I may change my mind in the future. Reasons:
  474. # - they require Administrator privileges
  475. # - can't implement a timeout for stop() (unless by using a thread,
  476. # which sucks)
  477. # - would require adding ServiceAlreadyStarted and
  478. # ServiceAlreadyStopped exceptions, adding two new APIs.
  479. # - we might also want to have modify(), which would basically mean
  480. # rewriting win32serviceutil.ChangeServiceConfig, which involves a
  481. # lot of stuff (and API constants which would pollute the API), see:
  482. # http://pyxr.sourceforge.net/PyXR/c/python24/lib/site-packages/
  483. # win32/lib/win32serviceutil.py.html#0175
  484. # - psutil is typically about "read only" monitoring stuff;
  485. # win_service_* APIs should only be used to retrieve a service and
  486. # check whether it's running
  487. # def start(self, timeout=None):
  488. # with self._wrap_exceptions():
  489. # cext.winservice_start(self.name())
  490. # if timeout:
  491. # giveup_at = time.time() + timeout
  492. # while True:
  493. # if self.status() == "running":
  494. # return
  495. # else:
  496. # if time.time() > giveup_at:
  497. # raise TimeoutExpired(timeout)
  498. # else:
  499. # time.sleep(.1)
  500. # def stop(self):
  501. # # Note: timeout is not implemented because it's just not
  502. # # possible, see:
  503. # # http://stackoverflow.com/questions/11973228/
  504. # with self._wrap_exceptions():
  505. # return cext.winservice_stop(self.name())
  506. # =====================================================================
  507. # --- processes
  508. # =====================================================================
  509. pids = cext.pids
  510. pid_exists = cext.pid_exists
  511. ppid_map = cext.ppid_map # used internally by Process.children()
  512. def is_permission_err(exc):
  513. """Return True if this is a permission error."""
  514. assert isinstance(exc, OSError), exc
  515. return isinstance(exc, PermissionError) or exc.winerror in {
  516. cext.ERROR_ACCESS_DENIED,
  517. cext.ERROR_PRIVILEGE_NOT_HELD,
  518. }
  519. def convert_oserror(exc, pid=None, name=None):
  520. """Convert OSError into NoSuchProcess or AccessDenied."""
  521. assert isinstance(exc, OSError), exc
  522. if is_permission_err(exc):
  523. return AccessDenied(pid=pid, name=name)
  524. if isinstance(exc, ProcessLookupError):
  525. return NoSuchProcess(pid=pid, name=name)
  526. raise exc
  527. def wrap_exceptions(fun):
  528. """Decorator which converts OSError into NoSuchProcess or AccessDenied."""
  529. @functools.wraps(fun)
  530. def wrapper(self, *args, **kwargs):
  531. try:
  532. return fun(self, *args, **kwargs)
  533. except OSError as err:
  534. raise convert_oserror(err, pid=self.pid, name=self._name) from err
  535. return wrapper
  536. def retry_error_partial_copy(fun):
  537. """Workaround for https://github.com/giampaolo/psutil/issues/875.
  538. See: https://stackoverflow.com/questions/4457745#4457745.
  539. """
  540. @functools.wraps(fun)
  541. def wrapper(self, *args, **kwargs):
  542. delay = 0.0001
  543. times = 33
  544. for _ in range(times): # retries for roughly 1 second
  545. try:
  546. return fun(self, *args, **kwargs)
  547. except OSError as _:
  548. err = _
  549. if err.winerror == ERROR_PARTIAL_COPY:
  550. time.sleep(delay)
  551. delay = min(delay * 2, 0.04)
  552. continue
  553. raise
  554. msg = (
  555. f"{fun} retried {times} times, converted to AccessDenied as it's "
  556. f"still returning {err}"
  557. )
  558. raise AccessDenied(pid=self.pid, name=self._name, msg=msg)
  559. return wrapper
  560. class Process:
  561. """Wrapper class around underlying C implementation."""
  562. __slots__ = ["_cache", "_name", "_ppid", "pid"]
  563. def __init__(self, pid):
  564. self.pid = pid
  565. self._name = None
  566. self._ppid = None
  567. # --- oneshot() stuff
  568. def oneshot_enter(self):
  569. self._proc_info.cache_activate(self)
  570. self.exe.cache_activate(self)
  571. def oneshot_exit(self):
  572. self._proc_info.cache_deactivate(self)
  573. self.exe.cache_deactivate(self)
  574. @memoize_when_activated
  575. def _proc_info(self):
  576. """Return multiple information about this process as a
  577. raw tuple.
  578. """
  579. ret = cext.proc_info(self.pid)
  580. assert len(ret) == len(pinfo_map)
  581. return ret
  582. def name(self):
  583. """Return process name, which on Windows is always the final
  584. part of the executable.
  585. """
  586. # This is how PIDs 0 and 4 are always represented in taskmgr
  587. # and process-hacker.
  588. if self.pid == 0:
  589. return "System Idle Process"
  590. if self.pid == 4:
  591. return "System"
  592. return os.path.basename(self.exe())
  593. @wrap_exceptions
  594. @memoize_when_activated
  595. def exe(self):
  596. if PYPY:
  597. try:
  598. exe = cext.proc_exe(self.pid)
  599. except OSError as err:
  600. # 24 = ERROR_TOO_MANY_OPEN_FILES. Not sure why this happens
  601. # (perhaps PyPy's JIT delaying garbage collection of files?).
  602. if err.errno == 24:
  603. debug(f"{err!r} translated into AccessDenied")
  604. raise AccessDenied(self.pid, self._name) from err
  605. raise
  606. else:
  607. exe = cext.proc_exe(self.pid)
  608. if exe.startswith('\\'):
  609. return convert_dos_path(exe)
  610. return exe # May be "Registry", "MemCompression", ...
  611. @wrap_exceptions
  612. @retry_error_partial_copy
  613. def cmdline(self):
  614. if cext.WINVER >= cext.WINDOWS_8_1:
  615. # PEB method detects cmdline changes but requires more
  616. # privileges: https://github.com/giampaolo/psutil/pull/1398
  617. try:
  618. return cext.proc_cmdline(self.pid, use_peb=True)
  619. except OSError as err:
  620. if is_permission_err(err):
  621. return cext.proc_cmdline(self.pid, use_peb=False)
  622. else:
  623. raise
  624. else:
  625. return cext.proc_cmdline(self.pid, use_peb=True)
  626. @wrap_exceptions
  627. @retry_error_partial_copy
  628. def environ(self):
  629. s = cext.proc_environ(self.pid)
  630. return parse_environ_block(s)
  631. def ppid(self):
  632. try:
  633. return ppid_map()[self.pid]
  634. except KeyError:
  635. raise NoSuchProcess(self.pid, self._name) from None
  636. def _get_raw_meminfo(self):
  637. try:
  638. return cext.proc_memory_info(self.pid)
  639. except OSError as err:
  640. if is_permission_err(err):
  641. # TODO: the C ext can probably be refactored in order
  642. # to get this from cext.proc_info()
  643. debug("attempting memory_info() fallback (slower)")
  644. info = self._proc_info()
  645. return (
  646. info[pinfo_map['num_page_faults']],
  647. info[pinfo_map['peak_wset']],
  648. info[pinfo_map['wset']],
  649. info[pinfo_map['peak_paged_pool']],
  650. info[pinfo_map['paged_pool']],
  651. info[pinfo_map['peak_non_paged_pool']],
  652. info[pinfo_map['non_paged_pool']],
  653. info[pinfo_map['pagefile']],
  654. info[pinfo_map['peak_pagefile']],
  655. info[pinfo_map['mem_private']],
  656. )
  657. raise
  658. @wrap_exceptions
  659. def memory_info(self):
  660. # on Windows RSS == WorkingSetSize and VSM == PagefileUsage.
  661. # Underlying C function returns fields of PROCESS_MEMORY_COUNTERS
  662. # struct.
  663. t = self._get_raw_meminfo()
  664. rss = t[2] # wset
  665. vms = t[7] # pagefile
  666. return ntp.pmem(*(rss, vms) + t)
  667. @wrap_exceptions
  668. def memory_full_info(self):
  669. basic_mem = self.memory_info()
  670. uss = cext.proc_memory_uss(self.pid)
  671. uss *= getpagesize()
  672. return ntp.pfullmem(*basic_mem + (uss,))
  673. def memory_maps(self):
  674. try:
  675. raw = cext.proc_memory_maps(self.pid)
  676. except OSError as err:
  677. # XXX - can't use wrap_exceptions decorator as we're
  678. # returning a generator; probably needs refactoring.
  679. raise convert_oserror(err, self.pid, self._name) from err
  680. else:
  681. for addr, perm, path, rss in raw:
  682. path = convert_dos_path(path)
  683. addr = hex(addr)
  684. yield (addr, perm, path, rss)
  685. @wrap_exceptions
  686. def kill(self):
  687. return cext.proc_kill(self.pid)
  688. @wrap_exceptions
  689. def send_signal(self, sig):
  690. if sig == signal.SIGTERM:
  691. cext.proc_kill(self.pid)
  692. elif sig in {signal.CTRL_C_EVENT, signal.CTRL_BREAK_EVENT}:
  693. os.kill(self.pid, sig)
  694. else:
  695. msg = (
  696. "only SIGTERM, CTRL_C_EVENT and CTRL_BREAK_EVENT signals "
  697. "are supported on Windows"
  698. )
  699. raise ValueError(msg)
  700. @wrap_exceptions
  701. def wait(self, timeout=None):
  702. if timeout is None:
  703. cext_timeout = cext.INFINITE
  704. else:
  705. # WaitForSingleObject() expects time in milliseconds.
  706. cext_timeout = int(timeout * 1000)
  707. timer = getattr(time, 'monotonic', time.time)
  708. stop_at = timer() + timeout if timeout is not None else None
  709. try:
  710. # Exit code is supposed to come from GetExitCodeProcess().
  711. # May also be None if OpenProcess() failed with
  712. # ERROR_INVALID_PARAMETER, meaning PID is already gone.
  713. exit_code = cext.proc_wait(self.pid, cext_timeout)
  714. except cext.TimeoutExpired as err:
  715. # WaitForSingleObject() returned WAIT_TIMEOUT. Just raise.
  716. raise TimeoutExpired(timeout, self.pid, self._name) from err
  717. except cext.TimeoutAbandoned:
  718. # WaitForSingleObject() returned WAIT_ABANDONED, see:
  719. # https://github.com/giampaolo/psutil/issues/1224
  720. # We'll just rely on the internal polling and return None
  721. # when the PID disappears. Subprocess module does the same
  722. # (return None):
  723. # https://github.com/python/cpython/blob/
  724. # be50a7b627d0aa37e08fa8e2d5568891f19903ce/
  725. # Lib/subprocess.py#L1193-L1194
  726. exit_code = None
  727. # At this point WaitForSingleObject() returned WAIT_OBJECT_0,
  728. # meaning the process is gone. Stupidly there are cases where
  729. # its PID may still stick around so we do a further internal
  730. # polling.
  731. delay = 0.0001
  732. while True:
  733. if not pid_exists(self.pid):
  734. return exit_code
  735. if stop_at and timer() >= stop_at:
  736. raise TimeoutExpired(timeout, pid=self.pid, name=self._name)
  737. time.sleep(delay)
  738. delay = min(delay * 2, 0.04) # incremental delay
  739. @wrap_exceptions
  740. def username(self):
  741. if self.pid in {0, 4}:
  742. return 'NT AUTHORITY\\SYSTEM'
  743. domain, user = cext.proc_username(self.pid)
  744. return f"{domain}\\{user}"
  745. @wrap_exceptions
  746. def create_time(self, fast_only=False):
  747. # Note: proc_times() not put under oneshot() 'cause create_time()
  748. # is already cached by the main Process class.
  749. try:
  750. _user, _system, created = cext.proc_times(self.pid)
  751. return created
  752. except OSError as err:
  753. if is_permission_err(err):
  754. if fast_only:
  755. raise
  756. debug("attempting create_time() fallback (slower)")
  757. return self._proc_info()[pinfo_map['create_time']]
  758. raise
  759. @wrap_exceptions
  760. def num_threads(self):
  761. return self._proc_info()[pinfo_map['num_threads']]
  762. @wrap_exceptions
  763. def threads(self):
  764. rawlist = cext.proc_threads(self.pid)
  765. retlist = []
  766. for thread_id, utime, stime in rawlist:
  767. ntuple = ntp.pthread(thread_id, utime, stime)
  768. retlist.append(ntuple)
  769. return retlist
  770. @wrap_exceptions
  771. def cpu_times(self):
  772. try:
  773. user, system, _created = cext.proc_times(self.pid)
  774. except OSError as err:
  775. if not is_permission_err(err):
  776. raise
  777. debug("attempting cpu_times() fallback (slower)")
  778. info = self._proc_info()
  779. user = info[pinfo_map['user_time']]
  780. system = info[pinfo_map['kernel_time']]
  781. # Children user/system times are not retrievable (set to 0).
  782. return ntp.pcputimes(user, system, 0.0, 0.0)
  783. @wrap_exceptions
  784. def suspend(self):
  785. cext.proc_suspend_or_resume(self.pid, True)
  786. @wrap_exceptions
  787. def resume(self):
  788. cext.proc_suspend_or_resume(self.pid, False)
  789. @wrap_exceptions
  790. @retry_error_partial_copy
  791. def cwd(self):
  792. if self.pid in {0, 4}:
  793. raise AccessDenied(self.pid, self._name)
  794. # return a normalized pathname since the native C function appends
  795. # "\\" at the and of the path
  796. path = cext.proc_cwd(self.pid)
  797. return os.path.normpath(path)
  798. @wrap_exceptions
  799. def open_files(self):
  800. if self.pid in {0, 4}:
  801. return []
  802. ret = set()
  803. # Filenames come in in native format like:
  804. # "\Device\HarddiskVolume1\Windows\systemew\file.txt"
  805. # Convert the first part in the corresponding drive letter
  806. # (e.g. "C:\") by using Windows's QueryDosDevice()
  807. raw_file_names = cext.proc_open_files(self.pid)
  808. for file in raw_file_names:
  809. file = convert_dos_path(file)
  810. if isfile_strict(file):
  811. ntuple = ntp.popenfile(file, -1)
  812. ret.add(ntuple)
  813. return list(ret)
  814. @wrap_exceptions
  815. def net_connections(self, kind='inet'):
  816. return net_connections(kind, _pid=self.pid)
  817. @wrap_exceptions
  818. def nice_get(self):
  819. value = cext.proc_priority_get(self.pid)
  820. value = Priority(value)
  821. return value
  822. @wrap_exceptions
  823. def nice_set(self, value):
  824. return cext.proc_priority_set(self.pid, value)
  825. @wrap_exceptions
  826. def ionice_get(self):
  827. ret = cext.proc_io_priority_get(self.pid)
  828. ret = IOPriority(ret)
  829. return ret
  830. @wrap_exceptions
  831. def ionice_set(self, ioclass, value):
  832. if value:
  833. msg = "value argument not accepted on Windows"
  834. raise TypeError(msg)
  835. if ioclass not in {
  836. IOPriority.IOPRIO_VERYLOW,
  837. IOPriority.IOPRIO_LOW,
  838. IOPriority.IOPRIO_NORMAL,
  839. IOPriority.IOPRIO_HIGH,
  840. }:
  841. msg = f"{ioclass} is not a valid priority"
  842. raise ValueError(msg)
  843. cext.proc_io_priority_set(self.pid, ioclass)
  844. @wrap_exceptions
  845. def io_counters(self):
  846. try:
  847. ret = cext.proc_io_counters(self.pid)
  848. except OSError as err:
  849. if not is_permission_err(err):
  850. raise
  851. debug("attempting io_counters() fallback (slower)")
  852. info = self._proc_info()
  853. ret = (
  854. info[pinfo_map['io_rcount']],
  855. info[pinfo_map['io_wcount']],
  856. info[pinfo_map['io_rbytes']],
  857. info[pinfo_map['io_wbytes']],
  858. info[pinfo_map['io_count_others']],
  859. info[pinfo_map['io_bytes_others']],
  860. )
  861. return ntp.pio(*ret)
  862. @wrap_exceptions
  863. def status(self):
  864. suspended = cext.proc_is_suspended(self.pid)
  865. if suspended:
  866. return _common.STATUS_STOPPED
  867. else:
  868. return _common.STATUS_RUNNING
  869. @wrap_exceptions
  870. def cpu_affinity_get(self):
  871. def from_bitmask(x):
  872. return [i for i in range(64) if (1 << i) & x]
  873. bitmask = cext.proc_cpu_affinity_get(self.pid)
  874. return from_bitmask(bitmask)
  875. @wrap_exceptions
  876. def cpu_affinity_set(self, value):
  877. def to_bitmask(ls):
  878. if not ls:
  879. msg = f"invalid argument {ls!r}"
  880. raise ValueError(msg)
  881. out = 0
  882. for b in ls:
  883. out |= 2**b
  884. return out
  885. # SetProcessAffinityMask() states that ERROR_INVALID_PARAMETER
  886. # is returned for an invalid CPU but this seems not to be true,
  887. # therefore we check CPUs validy beforehand.
  888. allcpus = list(range(len(per_cpu_times())))
  889. for cpu in value:
  890. if cpu not in allcpus:
  891. if not isinstance(cpu, int):
  892. msg = f"invalid CPU {cpu!r}; an integer is required"
  893. raise TypeError(msg)
  894. msg = f"invalid CPU {cpu!r}"
  895. raise ValueError(msg)
  896. bitmask = to_bitmask(value)
  897. cext.proc_cpu_affinity_set(self.pid, bitmask)
  898. @wrap_exceptions
  899. def num_handles(self):
  900. try:
  901. return cext.proc_num_handles(self.pid)
  902. except OSError as err:
  903. if is_permission_err(err):
  904. debug("attempting num_handles() fallback (slower)")
  905. return self._proc_info()[pinfo_map['num_handles']]
  906. raise
  907. @wrap_exceptions
  908. def num_ctx_switches(self):
  909. ctx_switches = self._proc_info()[pinfo_map['ctx_switches']]
  910. # only voluntary ctx switches are supported
  911. return ntp.pctxsw(ctx_switches, 0)