_pswindows.py 36 KB

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