Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/psutil/_psbsd.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 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 | |
| 5 """FreeBSD, OpenBSD and NetBSD platforms implementation.""" | |
| 6 | |
| 7 import contextlib | |
| 8 import errno | |
| 9 import functools | |
| 10 import os | |
| 11 import xml.etree.ElementTree as ET | |
| 12 from collections import namedtuple | |
| 13 from collections import defaultdict | |
| 14 | |
| 15 from . import _common | |
| 16 from . import _psposix | |
| 17 from . import _psutil_bsd as cext | |
| 18 from . import _psutil_posix as cext_posix | |
| 19 from ._common import AccessDenied | |
| 20 from ._common import conn_tmap | |
| 21 from ._common import conn_to_ntuple | |
| 22 from ._common import FREEBSD | |
| 23 from ._common import memoize | |
| 24 from ._common import memoize_when_activated | |
| 25 from ._common import NETBSD | |
| 26 from ._common import NoSuchProcess | |
| 27 from ._common import OPENBSD | |
| 28 from ._common import usage_percent | |
| 29 from ._common import ZombieProcess | |
| 30 from ._compat import FileNotFoundError | |
| 31 from ._compat import PermissionError | |
| 32 from ._compat import ProcessLookupError | |
| 33 from ._compat import which | |
| 34 | |
| 35 | |
| 36 __extra__all__ = [] | |
| 37 | |
| 38 | |
| 39 # ===================================================================== | |
| 40 # --- globals | |
| 41 # ===================================================================== | |
| 42 | |
| 43 | |
| 44 if FREEBSD: | |
| 45 PROC_STATUSES = { | |
| 46 cext.SIDL: _common.STATUS_IDLE, | |
| 47 cext.SRUN: _common.STATUS_RUNNING, | |
| 48 cext.SSLEEP: _common.STATUS_SLEEPING, | |
| 49 cext.SSTOP: _common.STATUS_STOPPED, | |
| 50 cext.SZOMB: _common.STATUS_ZOMBIE, | |
| 51 cext.SWAIT: _common.STATUS_WAITING, | |
| 52 cext.SLOCK: _common.STATUS_LOCKED, | |
| 53 } | |
| 54 elif OPENBSD: | |
| 55 PROC_STATUSES = { | |
| 56 cext.SIDL: _common.STATUS_IDLE, | |
| 57 cext.SSLEEP: _common.STATUS_SLEEPING, | |
| 58 cext.SSTOP: _common.STATUS_STOPPED, | |
| 59 # According to /usr/include/sys/proc.h SZOMB is unused. | |
| 60 # test_zombie_process() shows that SDEAD is the right | |
| 61 # equivalent. Also it appears there's no equivalent of | |
| 62 # psutil.STATUS_DEAD. SDEAD really means STATUS_ZOMBIE. | |
| 63 # cext.SZOMB: _common.STATUS_ZOMBIE, | |
| 64 cext.SDEAD: _common.STATUS_ZOMBIE, | |
| 65 cext.SZOMB: _common.STATUS_ZOMBIE, | |
| 66 # From http://www.eecs.harvard.edu/~margo/cs161/videos/proc.h.txt | |
| 67 # OpenBSD has SRUN and SONPROC: SRUN indicates that a process | |
| 68 # is runnable but *not* yet running, i.e. is on a run queue. | |
| 69 # SONPROC indicates that the process is actually executing on | |
| 70 # a CPU, i.e. it is no longer on a run queue. | |
| 71 # As such we'll map SRUN to STATUS_WAKING and SONPROC to | |
| 72 # STATUS_RUNNING | |
| 73 cext.SRUN: _common.STATUS_WAKING, | |
| 74 cext.SONPROC: _common.STATUS_RUNNING, | |
| 75 } | |
| 76 elif NETBSD: | |
| 77 PROC_STATUSES = { | |
| 78 cext.SIDL: _common.STATUS_IDLE, | |
| 79 cext.SSLEEP: _common.STATUS_SLEEPING, | |
| 80 cext.SSTOP: _common.STATUS_STOPPED, | |
| 81 cext.SZOMB: _common.STATUS_ZOMBIE, | |
| 82 cext.SRUN: _common.STATUS_WAKING, | |
| 83 cext.SONPROC: _common.STATUS_RUNNING, | |
| 84 } | |
| 85 | |
| 86 TCP_STATUSES = { | |
| 87 cext.TCPS_ESTABLISHED: _common.CONN_ESTABLISHED, | |
| 88 cext.TCPS_SYN_SENT: _common.CONN_SYN_SENT, | |
| 89 cext.TCPS_SYN_RECEIVED: _common.CONN_SYN_RECV, | |
| 90 cext.TCPS_FIN_WAIT_1: _common.CONN_FIN_WAIT1, | |
| 91 cext.TCPS_FIN_WAIT_2: _common.CONN_FIN_WAIT2, | |
| 92 cext.TCPS_TIME_WAIT: _common.CONN_TIME_WAIT, | |
| 93 cext.TCPS_CLOSED: _common.CONN_CLOSE, | |
| 94 cext.TCPS_CLOSE_WAIT: _common.CONN_CLOSE_WAIT, | |
| 95 cext.TCPS_LAST_ACK: _common.CONN_LAST_ACK, | |
| 96 cext.TCPS_LISTEN: _common.CONN_LISTEN, | |
| 97 cext.TCPS_CLOSING: _common.CONN_CLOSING, | |
| 98 cext.PSUTIL_CONN_NONE: _common.CONN_NONE, | |
| 99 } | |
| 100 | |
| 101 PAGESIZE = cext_posix.getpagesize() | |
| 102 AF_LINK = cext_posix.AF_LINK | |
| 103 | |
| 104 HAS_PER_CPU_TIMES = hasattr(cext, "per_cpu_times") | |
| 105 HAS_PROC_NUM_THREADS = hasattr(cext, "proc_num_threads") | |
| 106 HAS_PROC_OPEN_FILES = hasattr(cext, 'proc_open_files') | |
| 107 HAS_PROC_NUM_FDS = hasattr(cext, 'proc_num_fds') | |
| 108 | |
| 109 kinfo_proc_map = dict( | |
| 110 ppid=0, | |
| 111 status=1, | |
| 112 real_uid=2, | |
| 113 effective_uid=3, | |
| 114 saved_uid=4, | |
| 115 real_gid=5, | |
| 116 effective_gid=6, | |
| 117 saved_gid=7, | |
| 118 ttynr=8, | |
| 119 create_time=9, | |
| 120 ctx_switches_vol=10, | |
| 121 ctx_switches_unvol=11, | |
| 122 read_io_count=12, | |
| 123 write_io_count=13, | |
| 124 user_time=14, | |
| 125 sys_time=15, | |
| 126 ch_user_time=16, | |
| 127 ch_sys_time=17, | |
| 128 rss=18, | |
| 129 vms=19, | |
| 130 memtext=20, | |
| 131 memdata=21, | |
| 132 memstack=22, | |
| 133 cpunum=23, | |
| 134 name=24, | |
| 135 ) | |
| 136 | |
| 137 | |
| 138 # ===================================================================== | |
| 139 # --- named tuples | |
| 140 # ===================================================================== | |
| 141 | |
| 142 | |
| 143 # psutil.virtual_memory() | |
| 144 svmem = namedtuple( | |
| 145 'svmem', ['total', 'available', 'percent', 'used', 'free', | |
| 146 'active', 'inactive', 'buffers', 'cached', 'shared', 'wired']) | |
| 147 # psutil.cpu_times() | |
| 148 scputimes = namedtuple( | |
| 149 'scputimes', ['user', 'nice', 'system', 'idle', 'irq']) | |
| 150 # psutil.Process.memory_info() | |
| 151 pmem = namedtuple('pmem', ['rss', 'vms', 'text', 'data', 'stack']) | |
| 152 # psutil.Process.memory_full_info() | |
| 153 pfullmem = pmem | |
| 154 # psutil.Process.cpu_times() | |
| 155 pcputimes = namedtuple('pcputimes', | |
| 156 ['user', 'system', 'children_user', 'children_system']) | |
| 157 # psutil.Process.memory_maps(grouped=True) | |
| 158 pmmap_grouped = namedtuple( | |
| 159 'pmmap_grouped', 'path rss, private, ref_count, shadow_count') | |
| 160 # psutil.Process.memory_maps(grouped=False) | |
| 161 pmmap_ext = namedtuple( | |
| 162 'pmmap_ext', 'addr, perms path rss, private, ref_count, shadow_count') | |
| 163 # psutil.disk_io_counters() | |
| 164 if FREEBSD: | |
| 165 sdiskio = namedtuple('sdiskio', ['read_count', 'write_count', | |
| 166 'read_bytes', 'write_bytes', | |
| 167 'read_time', 'write_time', | |
| 168 'busy_time']) | |
| 169 else: | |
| 170 sdiskio = namedtuple('sdiskio', ['read_count', 'write_count', | |
| 171 'read_bytes', 'write_bytes']) | |
| 172 | |
| 173 | |
| 174 # ===================================================================== | |
| 175 # --- memory | |
| 176 # ===================================================================== | |
| 177 | |
| 178 | |
| 179 def virtual_memory(): | |
| 180 """System virtual memory as a namedtuple.""" | |
| 181 mem = cext.virtual_mem() | |
| 182 total, free, active, inactive, wired, cached, buffers, shared = mem | |
| 183 if NETBSD: | |
| 184 # On NetBSD buffers and shared mem is determined via /proc. | |
| 185 # The C ext set them to 0. | |
| 186 with open('/proc/meminfo', 'rb') as f: | |
| 187 for line in f: | |
| 188 if line.startswith(b'Buffers:'): | |
| 189 buffers = int(line.split()[1]) * 1024 | |
| 190 elif line.startswith(b'MemShared:'): | |
| 191 shared = int(line.split()[1]) * 1024 | |
| 192 avail = inactive + cached + free | |
| 193 used = active + wired + cached | |
| 194 percent = usage_percent((total - avail), total, round_=1) | |
| 195 return svmem(total, avail, percent, used, free, | |
| 196 active, inactive, buffers, cached, shared, wired) | |
| 197 | |
| 198 | |
| 199 def swap_memory(): | |
| 200 """System swap memory as (total, used, free, sin, sout) namedtuple.""" | |
| 201 total, used, free, sin, sout = cext.swap_mem() | |
| 202 percent = usage_percent(used, total, round_=1) | |
| 203 return _common.sswap(total, used, free, percent, sin, sout) | |
| 204 | |
| 205 | |
| 206 # ===================================================================== | |
| 207 # --- CPU | |
| 208 # ===================================================================== | |
| 209 | |
| 210 | |
| 211 def cpu_times(): | |
| 212 """Return system per-CPU times as a namedtuple""" | |
| 213 user, nice, system, idle, irq = cext.cpu_times() | |
| 214 return scputimes(user, nice, system, idle, irq) | |
| 215 | |
| 216 | |
| 217 if HAS_PER_CPU_TIMES: | |
| 218 def per_cpu_times(): | |
| 219 """Return system CPU times as a namedtuple""" | |
| 220 ret = [] | |
| 221 for cpu_t in cext.per_cpu_times(): | |
| 222 user, nice, system, idle, irq = cpu_t | |
| 223 item = scputimes(user, nice, system, idle, irq) | |
| 224 ret.append(item) | |
| 225 return ret | |
| 226 else: | |
| 227 # XXX | |
| 228 # Ok, this is very dirty. | |
| 229 # On FreeBSD < 8 we cannot gather per-cpu information, see: | |
| 230 # https://github.com/giampaolo/psutil/issues/226 | |
| 231 # If num cpus > 1, on first call we return single cpu times to avoid a | |
| 232 # crash at psutil import time. | |
| 233 # Next calls will fail with NotImplementedError | |
| 234 def per_cpu_times(): | |
| 235 """Return system CPU times as a namedtuple""" | |
| 236 if cpu_count_logical() == 1: | |
| 237 return [cpu_times()] | |
| 238 if per_cpu_times.__called__: | |
| 239 raise NotImplementedError("supported only starting from FreeBSD 8") | |
| 240 per_cpu_times.__called__ = True | |
| 241 return [cpu_times()] | |
| 242 | |
| 243 per_cpu_times.__called__ = False | |
| 244 | |
| 245 | |
| 246 def cpu_count_logical(): | |
| 247 """Return the number of logical CPUs in the system.""" | |
| 248 return cext.cpu_count_logical() | |
| 249 | |
| 250 | |
| 251 if OPENBSD or NETBSD: | |
| 252 def cpu_count_physical(): | |
| 253 # OpenBSD and NetBSD do not implement this. | |
| 254 return 1 if cpu_count_logical() == 1 else None | |
| 255 else: | |
| 256 def cpu_count_physical(): | |
| 257 """Return the number of physical CPUs in the system.""" | |
| 258 # From the C module we'll get an XML string similar to this: | |
| 259 # http://manpages.ubuntu.com/manpages/precise/man4/smp.4freebsd.html | |
| 260 # We may get None in case "sysctl kern.sched.topology_spec" | |
| 261 # is not supported on this BSD version, in which case we'll mimic | |
| 262 # os.cpu_count() and return None. | |
| 263 ret = None | |
| 264 s = cext.cpu_count_phys() | |
| 265 if s is not None: | |
| 266 # get rid of padding chars appended at the end of the string | |
| 267 index = s.rfind("</groups>") | |
| 268 if index != -1: | |
| 269 s = s[:index + 9] | |
| 270 root = ET.fromstring(s) | |
| 271 try: | |
| 272 ret = len(root.findall('group/children/group/cpu')) or None | |
| 273 finally: | |
| 274 # needed otherwise it will memleak | |
| 275 root.clear() | |
| 276 if not ret: | |
| 277 # If logical CPUs are 1 it's obvious we'll have only 1 | |
| 278 # physical CPU. | |
| 279 if cpu_count_logical() == 1: | |
| 280 return 1 | |
| 281 return ret | |
| 282 | |
| 283 | |
| 284 def cpu_stats(): | |
| 285 """Return various CPU stats as a named tuple.""" | |
| 286 if FREEBSD: | |
| 287 # Note: the C ext is returning some metrics we are not exposing: | |
| 288 # traps. | |
| 289 ctxsw, intrs, soft_intrs, syscalls, traps = cext.cpu_stats() | |
| 290 elif NETBSD: | |
| 291 # XXX | |
| 292 # Note about intrs: the C extension returns 0. intrs | |
| 293 # can be determined via /proc/stat; it has the same value as | |
| 294 # soft_intrs thought so the kernel is faking it (?). | |
| 295 # | |
| 296 # Note about syscalls: the C extension always sets it to 0 (?). | |
| 297 # | |
| 298 # Note: the C ext is returning some metrics we are not exposing: | |
| 299 # traps, faults and forks. | |
| 300 ctxsw, intrs, soft_intrs, syscalls, traps, faults, forks = \ | |
| 301 cext.cpu_stats() | |
| 302 with open('/proc/stat', 'rb') as f: | |
| 303 for line in f: | |
| 304 if line.startswith(b'intr'): | |
| 305 intrs = int(line.split()[1]) | |
| 306 elif OPENBSD: | |
| 307 # Note: the C ext is returning some metrics we are not exposing: | |
| 308 # traps, faults and forks. | |
| 309 ctxsw, intrs, soft_intrs, syscalls, traps, faults, forks = \ | |
| 310 cext.cpu_stats() | |
| 311 return _common.scpustats(ctxsw, intrs, soft_intrs, syscalls) | |
| 312 | |
| 313 | |
| 314 # ===================================================================== | |
| 315 # --- disks | |
| 316 # ===================================================================== | |
| 317 | |
| 318 | |
| 319 def disk_partitions(all=False): | |
| 320 """Return mounted disk partitions as a list of namedtuples. | |
| 321 'all' argument is ignored, see: | |
| 322 https://github.com/giampaolo/psutil/issues/906 | |
| 323 """ | |
| 324 retlist = [] | |
| 325 partitions = cext.disk_partitions() | |
| 326 for partition in partitions: | |
| 327 device, mountpoint, fstype, opts = partition | |
| 328 maxfile = maxpath = None # set later | |
| 329 ntuple = _common.sdiskpart(device, mountpoint, fstype, opts, | |
| 330 maxfile, maxpath) | |
| 331 retlist.append(ntuple) | |
| 332 return retlist | |
| 333 | |
| 334 | |
| 335 disk_usage = _psposix.disk_usage | |
| 336 disk_io_counters = cext.disk_io_counters | |
| 337 | |
| 338 | |
| 339 # ===================================================================== | |
| 340 # --- network | |
| 341 # ===================================================================== | |
| 342 | |
| 343 | |
| 344 net_io_counters = cext.net_io_counters | |
| 345 net_if_addrs = cext_posix.net_if_addrs | |
| 346 | |
| 347 | |
| 348 def net_if_stats(): | |
| 349 """Get NIC stats (isup, duplex, speed, mtu).""" | |
| 350 names = net_io_counters().keys() | |
| 351 ret = {} | |
| 352 for name in names: | |
| 353 try: | |
| 354 mtu = cext_posix.net_if_mtu(name) | |
| 355 isup = cext_posix.net_if_is_running(name) | |
| 356 duplex, speed = cext_posix.net_if_duplex_speed(name) | |
| 357 except OSError as err: | |
| 358 # https://github.com/giampaolo/psutil/issues/1279 | |
| 359 if err.errno != errno.ENODEV: | |
| 360 raise | |
| 361 else: | |
| 362 if hasattr(_common, 'NicDuplex'): | |
| 363 duplex = _common.NicDuplex(duplex) | |
| 364 ret[name] = _common.snicstats(isup, duplex, speed, mtu) | |
| 365 return ret | |
| 366 | |
| 367 | |
| 368 def net_connections(kind): | |
| 369 """System-wide network connections.""" | |
| 370 if OPENBSD: | |
| 371 ret = [] | |
| 372 for pid in pids(): | |
| 373 try: | |
| 374 cons = Process(pid).connections(kind) | |
| 375 except (NoSuchProcess, ZombieProcess): | |
| 376 continue | |
| 377 else: | |
| 378 for conn in cons: | |
| 379 conn = list(conn) | |
| 380 conn.append(pid) | |
| 381 ret.append(_common.sconn(*conn)) | |
| 382 return ret | |
| 383 | |
| 384 if kind not in _common.conn_tmap: | |
| 385 raise ValueError("invalid %r kind argument; choose between %s" | |
| 386 % (kind, ', '.join([repr(x) for x in conn_tmap]))) | |
| 387 families, types = conn_tmap[kind] | |
| 388 ret = set() | |
| 389 if NETBSD: | |
| 390 rawlist = cext.net_connections(-1) | |
| 391 else: | |
| 392 rawlist = cext.net_connections() | |
| 393 for item in rawlist: | |
| 394 fd, fam, type, laddr, raddr, status, pid = item | |
| 395 # TODO: apply filter at C level | |
| 396 if fam in families and type in types: | |
| 397 nt = conn_to_ntuple(fd, fam, type, laddr, raddr, status, | |
| 398 TCP_STATUSES, pid) | |
| 399 ret.add(nt) | |
| 400 return list(ret) | |
| 401 | |
| 402 | |
| 403 # ===================================================================== | |
| 404 # --- sensors | |
| 405 # ===================================================================== | |
| 406 | |
| 407 | |
| 408 if FREEBSD: | |
| 409 | |
| 410 def sensors_battery(): | |
| 411 """Return battery info.""" | |
| 412 try: | |
| 413 percent, minsleft, power_plugged = cext.sensors_battery() | |
| 414 except NotImplementedError: | |
| 415 # See: https://github.com/giampaolo/psutil/issues/1074 | |
| 416 return None | |
| 417 power_plugged = power_plugged == 1 | |
| 418 if power_plugged: | |
| 419 secsleft = _common.POWER_TIME_UNLIMITED | |
| 420 elif minsleft == -1: | |
| 421 secsleft = _common.POWER_TIME_UNKNOWN | |
| 422 else: | |
| 423 secsleft = minsleft * 60 | |
| 424 return _common.sbattery(percent, secsleft, power_plugged) | |
| 425 | |
| 426 def sensors_temperatures(): | |
| 427 "Return CPU cores temperatures if available, else an empty dict." | |
| 428 ret = defaultdict(list) | |
| 429 num_cpus = cpu_count_logical() | |
| 430 for cpu in range(num_cpus): | |
| 431 try: | |
| 432 current, high = cext.sensors_cpu_temperature(cpu) | |
| 433 if high <= 0: | |
| 434 high = None | |
| 435 name = "Core %s" % cpu | |
| 436 ret["coretemp"].append( | |
| 437 _common.shwtemp(name, current, high, high)) | |
| 438 except NotImplementedError: | |
| 439 pass | |
| 440 | |
| 441 return ret | |
| 442 | |
| 443 def cpu_freq(): | |
| 444 """Return frequency metrics for CPUs. As of Dec 2018 only | |
| 445 CPU 0 appears to be supported by FreeBSD and all other cores | |
| 446 match the frequency of CPU 0. | |
| 447 """ | |
| 448 ret = [] | |
| 449 num_cpus = cpu_count_logical() | |
| 450 for cpu in range(num_cpus): | |
| 451 try: | |
| 452 current, available_freq = cext.cpu_frequency(cpu) | |
| 453 except NotImplementedError: | |
| 454 continue | |
| 455 if available_freq: | |
| 456 try: | |
| 457 min_freq = int(available_freq.split(" ")[-1].split("/")[0]) | |
| 458 except(IndexError, ValueError): | |
| 459 min_freq = None | |
| 460 try: | |
| 461 max_freq = int(available_freq.split(" ")[0].split("/")[0]) | |
| 462 except(IndexError, ValueError): | |
| 463 max_freq = None | |
| 464 ret.append(_common.scpufreq(current, min_freq, max_freq)) | |
| 465 return ret | |
| 466 | |
| 467 | |
| 468 # ===================================================================== | |
| 469 # --- other system functions | |
| 470 # ===================================================================== | |
| 471 | |
| 472 | |
| 473 def boot_time(): | |
| 474 """The system boot time expressed in seconds since the epoch.""" | |
| 475 return cext.boot_time() | |
| 476 | |
| 477 | |
| 478 def users(): | |
| 479 """Return currently connected users as a list of namedtuples.""" | |
| 480 retlist = [] | |
| 481 rawlist = cext.users() | |
| 482 for item in rawlist: | |
| 483 user, tty, hostname, tstamp, pid = item | |
| 484 if pid == -1: | |
| 485 assert OPENBSD | |
| 486 pid = None | |
| 487 if tty == '~': | |
| 488 continue # reboot or shutdown | |
| 489 nt = _common.suser(user, tty or None, hostname, tstamp, pid) | |
| 490 retlist.append(nt) | |
| 491 return retlist | |
| 492 | |
| 493 | |
| 494 # ===================================================================== | |
| 495 # --- processes | |
| 496 # ===================================================================== | |
| 497 | |
| 498 | |
| 499 @memoize | |
| 500 def _pid_0_exists(): | |
| 501 try: | |
| 502 Process(0).name() | |
| 503 except NoSuchProcess: | |
| 504 return False | |
| 505 except AccessDenied: | |
| 506 return True | |
| 507 else: | |
| 508 return True | |
| 509 | |
| 510 | |
| 511 def pids(): | |
| 512 """Returns a list of PIDs currently running on the system.""" | |
| 513 ret = cext.pids() | |
| 514 if OPENBSD and (0 not in ret) and _pid_0_exists(): | |
| 515 # On OpenBSD the kernel does not return PID 0 (neither does | |
| 516 # ps) but it's actually querable (Process(0) will succeed). | |
| 517 ret.insert(0, 0) | |
| 518 return ret | |
| 519 | |
| 520 | |
| 521 if OPENBSD or NETBSD: | |
| 522 def pid_exists(pid): | |
| 523 """Return True if pid exists.""" | |
| 524 exists = _psposix.pid_exists(pid) | |
| 525 if not exists: | |
| 526 # We do this because _psposix.pid_exists() lies in case of | |
| 527 # zombie processes. | |
| 528 return pid in pids() | |
| 529 else: | |
| 530 return True | |
| 531 else: | |
| 532 pid_exists = _psposix.pid_exists | |
| 533 | |
| 534 | |
| 535 def is_zombie(pid): | |
| 536 try: | |
| 537 st = cext.proc_oneshot_info(pid)[kinfo_proc_map['status']] | |
| 538 return st == cext.SZOMB | |
| 539 except Exception: | |
| 540 return False | |
| 541 | |
| 542 | |
| 543 def wrap_exceptions(fun): | |
| 544 """Decorator which translates bare OSError exceptions into | |
| 545 NoSuchProcess and AccessDenied. | |
| 546 """ | |
| 547 @functools.wraps(fun) | |
| 548 def wrapper(self, *args, **kwargs): | |
| 549 try: | |
| 550 return fun(self, *args, **kwargs) | |
| 551 except ProcessLookupError: | |
| 552 if is_zombie(self.pid): | |
| 553 raise ZombieProcess(self.pid, self._name, self._ppid) | |
| 554 else: | |
| 555 raise NoSuchProcess(self.pid, self._name) | |
| 556 except PermissionError: | |
| 557 raise AccessDenied(self.pid, self._name) | |
| 558 except OSError: | |
| 559 if self.pid == 0: | |
| 560 if 0 in pids(): | |
| 561 raise AccessDenied(self.pid, self._name) | |
| 562 else: | |
| 563 raise | |
| 564 raise | |
| 565 return wrapper | |
| 566 | |
| 567 | |
| 568 @contextlib.contextmanager | |
| 569 def wrap_exceptions_procfs(inst): | |
| 570 """Same as above, for routines relying on reading /proc fs.""" | |
| 571 try: | |
| 572 yield | |
| 573 except (ProcessLookupError, FileNotFoundError): | |
| 574 # ENOENT (no such file or directory) gets raised on open(). | |
| 575 # ESRCH (no such process) can get raised on read() if | |
| 576 # process is gone in meantime. | |
| 577 if is_zombie(inst.pid): | |
| 578 raise ZombieProcess(inst.pid, inst._name, inst._ppid) | |
| 579 else: | |
| 580 raise NoSuchProcess(inst.pid, inst._name) | |
| 581 except PermissionError: | |
| 582 raise AccessDenied(inst.pid, inst._name) | |
| 583 | |
| 584 | |
| 585 class Process(object): | |
| 586 """Wrapper class around underlying C implementation.""" | |
| 587 | |
| 588 __slots__ = ["pid", "_name", "_ppid", "_cache"] | |
| 589 | |
| 590 def __init__(self, pid): | |
| 591 self.pid = pid | |
| 592 self._name = None | |
| 593 self._ppid = None | |
| 594 | |
| 595 def _assert_alive(self): | |
| 596 """Raise NSP if the process disappeared on us.""" | |
| 597 # For those C function who do not raise NSP, possibly returning | |
| 598 # incorrect or incomplete result. | |
| 599 cext.proc_name(self.pid) | |
| 600 | |
| 601 @wrap_exceptions | |
| 602 @memoize_when_activated | |
| 603 def oneshot(self): | |
| 604 """Retrieves multiple process info in one shot as a raw tuple.""" | |
| 605 ret = cext.proc_oneshot_info(self.pid) | |
| 606 assert len(ret) == len(kinfo_proc_map) | |
| 607 return ret | |
| 608 | |
| 609 def oneshot_enter(self): | |
| 610 self.oneshot.cache_activate(self) | |
| 611 | |
| 612 def oneshot_exit(self): | |
| 613 self.oneshot.cache_deactivate(self) | |
| 614 | |
| 615 @wrap_exceptions | |
| 616 def name(self): | |
| 617 name = self.oneshot()[kinfo_proc_map['name']] | |
| 618 return name if name is not None else cext.proc_name(self.pid) | |
| 619 | |
| 620 @wrap_exceptions | |
| 621 def exe(self): | |
| 622 if FREEBSD: | |
| 623 if self.pid == 0: | |
| 624 return '' # else NSP | |
| 625 return cext.proc_exe(self.pid) | |
| 626 elif NETBSD: | |
| 627 if self.pid == 0: | |
| 628 # /proc/0 dir exists but /proc/0/exe doesn't | |
| 629 return "" | |
| 630 with wrap_exceptions_procfs(self): | |
| 631 return os.readlink("/proc/%s/exe" % self.pid) | |
| 632 else: | |
| 633 # OpenBSD: exe cannot be determined; references: | |
| 634 # https://chromium.googlesource.com/chromium/src/base/+/ | |
| 635 # master/base_paths_posix.cc | |
| 636 # We try our best guess by using which against the first | |
| 637 # cmdline arg (may return None). | |
| 638 cmdline = self.cmdline() | |
| 639 if cmdline: | |
| 640 return which(cmdline[0]) or "" | |
| 641 else: | |
| 642 return "" | |
| 643 | |
| 644 @wrap_exceptions | |
| 645 def cmdline(self): | |
| 646 if OPENBSD and self.pid == 0: | |
| 647 return [] # ...else it crashes | |
| 648 elif NETBSD: | |
| 649 # XXX - most of the times the underlying sysctl() call on Net | |
| 650 # and Open BSD returns a truncated string. | |
| 651 # Also /proc/pid/cmdline behaves the same so it looks | |
| 652 # like this is a kernel bug. | |
| 653 try: | |
| 654 return cext.proc_cmdline(self.pid) | |
| 655 except OSError as err: | |
| 656 if err.errno == errno.EINVAL: | |
| 657 if is_zombie(self.pid): | |
| 658 raise ZombieProcess(self.pid, self._name, self._ppid) | |
| 659 elif not pid_exists(self.pid): | |
| 660 raise NoSuchProcess(self.pid, self._name, self._ppid) | |
| 661 else: | |
| 662 # XXX: this happens with unicode tests. It means the C | |
| 663 # routine is unable to decode invalid unicode chars. | |
| 664 return [] | |
| 665 else: | |
| 666 raise | |
| 667 else: | |
| 668 return cext.proc_cmdline(self.pid) | |
| 669 | |
| 670 @wrap_exceptions | |
| 671 def environ(self): | |
| 672 return cext.proc_environ(self.pid) | |
| 673 | |
| 674 @wrap_exceptions | |
| 675 def terminal(self): | |
| 676 tty_nr = self.oneshot()[kinfo_proc_map['ttynr']] | |
| 677 tmap = _psposix.get_terminal_map() | |
| 678 try: | |
| 679 return tmap[tty_nr] | |
| 680 except KeyError: | |
| 681 return None | |
| 682 | |
| 683 @wrap_exceptions | |
| 684 def ppid(self): | |
| 685 self._ppid = self.oneshot()[kinfo_proc_map['ppid']] | |
| 686 return self._ppid | |
| 687 | |
| 688 @wrap_exceptions | |
| 689 def uids(self): | |
| 690 rawtuple = self.oneshot() | |
| 691 return _common.puids( | |
| 692 rawtuple[kinfo_proc_map['real_uid']], | |
| 693 rawtuple[kinfo_proc_map['effective_uid']], | |
| 694 rawtuple[kinfo_proc_map['saved_uid']]) | |
| 695 | |
| 696 @wrap_exceptions | |
| 697 def gids(self): | |
| 698 rawtuple = self.oneshot() | |
| 699 return _common.pgids( | |
| 700 rawtuple[kinfo_proc_map['real_gid']], | |
| 701 rawtuple[kinfo_proc_map['effective_gid']], | |
| 702 rawtuple[kinfo_proc_map['saved_gid']]) | |
| 703 | |
| 704 @wrap_exceptions | |
| 705 def cpu_times(self): | |
| 706 rawtuple = self.oneshot() | |
| 707 return _common.pcputimes( | |
| 708 rawtuple[kinfo_proc_map['user_time']], | |
| 709 rawtuple[kinfo_proc_map['sys_time']], | |
| 710 rawtuple[kinfo_proc_map['ch_user_time']], | |
| 711 rawtuple[kinfo_proc_map['ch_sys_time']]) | |
| 712 | |
| 713 if FREEBSD: | |
| 714 @wrap_exceptions | |
| 715 def cpu_num(self): | |
| 716 return self.oneshot()[kinfo_proc_map['cpunum']] | |
| 717 | |
| 718 @wrap_exceptions | |
| 719 def memory_info(self): | |
| 720 rawtuple = self.oneshot() | |
| 721 return pmem( | |
| 722 rawtuple[kinfo_proc_map['rss']], | |
| 723 rawtuple[kinfo_proc_map['vms']], | |
| 724 rawtuple[kinfo_proc_map['memtext']], | |
| 725 rawtuple[kinfo_proc_map['memdata']], | |
| 726 rawtuple[kinfo_proc_map['memstack']]) | |
| 727 | |
| 728 memory_full_info = memory_info | |
| 729 | |
| 730 @wrap_exceptions | |
| 731 def create_time(self): | |
| 732 return self.oneshot()[kinfo_proc_map['create_time']] | |
| 733 | |
| 734 @wrap_exceptions | |
| 735 def num_threads(self): | |
| 736 if HAS_PROC_NUM_THREADS: | |
| 737 # FreeBSD | |
| 738 return cext.proc_num_threads(self.pid) | |
| 739 else: | |
| 740 return len(self.threads()) | |
| 741 | |
| 742 @wrap_exceptions | |
| 743 def num_ctx_switches(self): | |
| 744 rawtuple = self.oneshot() | |
| 745 return _common.pctxsw( | |
| 746 rawtuple[kinfo_proc_map['ctx_switches_vol']], | |
| 747 rawtuple[kinfo_proc_map['ctx_switches_unvol']]) | |
| 748 | |
| 749 @wrap_exceptions | |
| 750 def threads(self): | |
| 751 # Note: on OpenSBD this (/dev/mem) requires root access. | |
| 752 rawlist = cext.proc_threads(self.pid) | |
| 753 retlist = [] | |
| 754 for thread_id, utime, stime in rawlist: | |
| 755 ntuple = _common.pthread(thread_id, utime, stime) | |
| 756 retlist.append(ntuple) | |
| 757 if OPENBSD: | |
| 758 self._assert_alive() | |
| 759 return retlist | |
| 760 | |
| 761 @wrap_exceptions | |
| 762 def connections(self, kind='inet'): | |
| 763 if kind not in conn_tmap: | |
| 764 raise ValueError("invalid %r kind argument; choose between %s" | |
| 765 % (kind, ', '.join([repr(x) for x in conn_tmap]))) | |
| 766 | |
| 767 if NETBSD: | |
| 768 families, types = conn_tmap[kind] | |
| 769 ret = [] | |
| 770 rawlist = cext.net_connections(self.pid) | |
| 771 for item in rawlist: | |
| 772 fd, fam, type, laddr, raddr, status, pid = item | |
| 773 assert pid == self.pid | |
| 774 if fam in families and type in types: | |
| 775 nt = conn_to_ntuple(fd, fam, type, laddr, raddr, status, | |
| 776 TCP_STATUSES) | |
| 777 ret.append(nt) | |
| 778 self._assert_alive() | |
| 779 return list(ret) | |
| 780 | |
| 781 families, types = conn_tmap[kind] | |
| 782 rawlist = cext.proc_connections(self.pid, families, types) | |
| 783 ret = [] | |
| 784 for item in rawlist: | |
| 785 fd, fam, type, laddr, raddr, status = item | |
| 786 nt = conn_to_ntuple(fd, fam, type, laddr, raddr, status, | |
| 787 TCP_STATUSES) | |
| 788 ret.append(nt) | |
| 789 | |
| 790 if OPENBSD: | |
| 791 self._assert_alive() | |
| 792 | |
| 793 return ret | |
| 794 | |
| 795 @wrap_exceptions | |
| 796 def wait(self, timeout=None): | |
| 797 return _psposix.wait_pid(self.pid, timeout, self._name) | |
| 798 | |
| 799 @wrap_exceptions | |
| 800 def nice_get(self): | |
| 801 return cext_posix.getpriority(self.pid) | |
| 802 | |
| 803 @wrap_exceptions | |
| 804 def nice_set(self, value): | |
| 805 return cext_posix.setpriority(self.pid, value) | |
| 806 | |
| 807 @wrap_exceptions | |
| 808 def status(self): | |
| 809 code = self.oneshot()[kinfo_proc_map['status']] | |
| 810 # XXX is '?' legit? (we're not supposed to return it anyway) | |
| 811 return PROC_STATUSES.get(code, '?') | |
| 812 | |
| 813 @wrap_exceptions | |
| 814 def io_counters(self): | |
| 815 rawtuple = self.oneshot() | |
| 816 return _common.pio( | |
| 817 rawtuple[kinfo_proc_map['read_io_count']], | |
| 818 rawtuple[kinfo_proc_map['write_io_count']], | |
| 819 -1, | |
| 820 -1) | |
| 821 | |
| 822 @wrap_exceptions | |
| 823 def cwd(self): | |
| 824 """Return process current working directory.""" | |
| 825 # sometimes we get an empty string, in which case we turn | |
| 826 # it into None | |
| 827 if OPENBSD and self.pid == 0: | |
| 828 return None # ...else it would raise EINVAL | |
| 829 elif NETBSD or HAS_PROC_OPEN_FILES: | |
| 830 # FreeBSD < 8 does not support functions based on | |
| 831 # kinfo_getfile() and kinfo_getvmmap() | |
| 832 return cext.proc_cwd(self.pid) or None | |
| 833 else: | |
| 834 raise NotImplementedError( | |
| 835 "supported only starting from FreeBSD 8" if | |
| 836 FREEBSD else "") | |
| 837 | |
| 838 nt_mmap_grouped = namedtuple( | |
| 839 'mmap', 'path rss, private, ref_count, shadow_count') | |
| 840 nt_mmap_ext = namedtuple( | |
| 841 'mmap', 'addr, perms path rss, private, ref_count, shadow_count') | |
| 842 | |
| 843 def _not_implemented(self): | |
| 844 raise NotImplementedError | |
| 845 | |
| 846 # FreeBSD < 8 does not support functions based on kinfo_getfile() | |
| 847 # and kinfo_getvmmap() | |
| 848 if HAS_PROC_OPEN_FILES: | |
| 849 @wrap_exceptions | |
| 850 def open_files(self): | |
| 851 """Return files opened by process as a list of namedtuples.""" | |
| 852 rawlist = cext.proc_open_files(self.pid) | |
| 853 return [_common.popenfile(path, fd) for path, fd in rawlist] | |
| 854 else: | |
| 855 open_files = _not_implemented | |
| 856 | |
| 857 # FreeBSD < 8 does not support functions based on kinfo_getfile() | |
| 858 # and kinfo_getvmmap() | |
| 859 if HAS_PROC_NUM_FDS: | |
| 860 @wrap_exceptions | |
| 861 def num_fds(self): | |
| 862 """Return the number of file descriptors opened by this process.""" | |
| 863 ret = cext.proc_num_fds(self.pid) | |
| 864 if NETBSD: | |
| 865 self._assert_alive() | |
| 866 return ret | |
| 867 else: | |
| 868 num_fds = _not_implemented | |
| 869 | |
| 870 # --- FreeBSD only APIs | |
| 871 | |
| 872 if FREEBSD: | |
| 873 | |
| 874 @wrap_exceptions | |
| 875 def cpu_affinity_get(self): | |
| 876 return cext.proc_cpu_affinity_get(self.pid) | |
| 877 | |
| 878 @wrap_exceptions | |
| 879 def cpu_affinity_set(self, cpus): | |
| 880 # Pre-emptively check if CPUs are valid because the C | |
| 881 # function has a weird behavior in case of invalid CPUs, | |
| 882 # see: https://github.com/giampaolo/psutil/issues/586 | |
| 883 allcpus = tuple(range(len(per_cpu_times()))) | |
| 884 for cpu in cpus: | |
| 885 if cpu not in allcpus: | |
| 886 raise ValueError("invalid CPU #%i (choose between %s)" | |
| 887 % (cpu, allcpus)) | |
| 888 try: | |
| 889 cext.proc_cpu_affinity_set(self.pid, cpus) | |
| 890 except OSError as err: | |
| 891 # 'man cpuset_setaffinity' about EDEADLK: | |
| 892 # <<the call would leave a thread without a valid CPU to run | |
| 893 # on because the set does not overlap with the thread's | |
| 894 # anonymous mask>> | |
| 895 if err.errno in (errno.EINVAL, errno.EDEADLK): | |
| 896 for cpu in cpus: | |
| 897 if cpu not in allcpus: | |
| 898 raise ValueError( | |
| 899 "invalid CPU #%i (choose between %s)" % ( | |
| 900 cpu, allcpus)) | |
| 901 raise | |
| 902 | |
| 903 @wrap_exceptions | |
| 904 def memory_maps(self): | |
| 905 return cext.proc_memory_maps(self.pid) | |
| 906 | |
| 907 @wrap_exceptions | |
| 908 def rlimit(self, resource, limits=None): | |
| 909 if limits is None: | |
| 910 return cext.proc_getrlimit(self.pid, resource) | |
| 911 else: | |
| 912 if len(limits) != 2: | |
| 913 raise ValueError( | |
| 914 "second argument must be a (soft, hard) tuple, " | |
| 915 "got %s" % repr(limits)) | |
| 916 soft, hard = limits | |
| 917 return cext.proc_setrlimit(self.pid, resource, soft, hard) |
