Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/psutil-5.9.9-py3.8-linux-x86_64.egg/psutil/_pssunos.py: 1%

427 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-02 06:13 +0000

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"""Sun OS Solaris platform implementation.""" 

6 

7import errno 

8import functools 

9import os 

10import socket 

11import subprocess 

12import sys 

13from collections import namedtuple 

14from socket import AF_INET 

15 

16from . import _common 

17from . import _psposix 

18from . import _psutil_posix as cext_posix 

19from . import _psutil_sunos as cext 

20from ._common import AF_INET6 

21from ._common import AccessDenied 

22from ._common import NoSuchProcess 

23from ._common import ZombieProcess 

24from ._common import debug 

25from ._common import get_procfs_path 

26from ._common import isfile_strict 

27from ._common import memoize_when_activated 

28from ._common import sockfam_to_enum 

29from ._common import socktype_to_enum 

30from ._common import usage_percent 

31from ._compat import PY3 

32from ._compat import FileNotFoundError 

33from ._compat import PermissionError 

34from ._compat import ProcessLookupError 

35from ._compat import b 

36 

37 

38__extra__all__ = ["CONN_IDLE", "CONN_BOUND", "PROCFS_PATH"] 

39 

40 

41# ===================================================================== 

42# --- globals 

43# ===================================================================== 

44 

45 

46PAGE_SIZE = cext_posix.getpagesize() 

47AF_LINK = cext_posix.AF_LINK 

48IS_64_BIT = sys.maxsize > 2**32 

49 

50CONN_IDLE = "IDLE" 

51CONN_BOUND = "BOUND" 

52 

53PROC_STATUSES = { 

54 cext.SSLEEP: _common.STATUS_SLEEPING, 

55 cext.SRUN: _common.STATUS_RUNNING, 

56 cext.SZOMB: _common.STATUS_ZOMBIE, 

57 cext.SSTOP: _common.STATUS_STOPPED, 

58 cext.SIDL: _common.STATUS_IDLE, 

59 cext.SONPROC: _common.STATUS_RUNNING, # same as run 

60 cext.SWAIT: _common.STATUS_WAITING, 

61} 

62 

63TCP_STATUSES = { 

64 cext.TCPS_ESTABLISHED: _common.CONN_ESTABLISHED, 

65 cext.TCPS_SYN_SENT: _common.CONN_SYN_SENT, 

66 cext.TCPS_SYN_RCVD: _common.CONN_SYN_RECV, 

67 cext.TCPS_FIN_WAIT_1: _common.CONN_FIN_WAIT1, 

68 cext.TCPS_FIN_WAIT_2: _common.CONN_FIN_WAIT2, 

69 cext.TCPS_TIME_WAIT: _common.CONN_TIME_WAIT, 

70 cext.TCPS_CLOSED: _common.CONN_CLOSE, 

71 cext.TCPS_CLOSE_WAIT: _common.CONN_CLOSE_WAIT, 

72 cext.TCPS_LAST_ACK: _common.CONN_LAST_ACK, 

73 cext.TCPS_LISTEN: _common.CONN_LISTEN, 

74 cext.TCPS_CLOSING: _common.CONN_CLOSING, 

75 cext.PSUTIL_CONN_NONE: _common.CONN_NONE, 

76 cext.TCPS_IDLE: CONN_IDLE, # sunos specific 

77 cext.TCPS_BOUND: CONN_BOUND, # sunos specific 

78} 

79 

80proc_info_map = dict( 

81 ppid=0, 

82 rss=1, 

83 vms=2, 

84 create_time=3, 

85 nice=4, 

86 num_threads=5, 

87 status=6, 

88 ttynr=7, 

89 uid=8, 

90 euid=9, 

91 gid=10, 

92 egid=11, 

93) 

94 

95 

96# ===================================================================== 

97# --- named tuples 

98# ===================================================================== 

99 

100 

101# psutil.cpu_times() 

102scputimes = namedtuple('scputimes', ['user', 'system', 'idle', 'iowait']) 

103# psutil.cpu_times(percpu=True) 

104pcputimes = namedtuple( 

105 'pcputimes', ['user', 'system', 'children_user', 'children_system'] 

106) 

107# psutil.virtual_memory() 

108svmem = namedtuple('svmem', ['total', 'available', 'percent', 'used', 'free']) 

109# psutil.Process.memory_info() 

110pmem = namedtuple('pmem', ['rss', 'vms']) 

111pfullmem = pmem 

112# psutil.Process.memory_maps(grouped=True) 

113pmmap_grouped = namedtuple( 

114 'pmmap_grouped', ['path', 'rss', 'anonymous', 'locked'] 

115) 

116# psutil.Process.memory_maps(grouped=False) 

117pmmap_ext = namedtuple( 

118 'pmmap_ext', 'addr perms ' + ' '.join(pmmap_grouped._fields) 

119) 

120 

121 

122# ===================================================================== 

123# --- memory 

124# ===================================================================== 

125 

126 

127def virtual_memory(): 

128 """Report virtual memory metrics.""" 

129 # we could have done this with kstat, but IMHO this is good enough 

130 total = os.sysconf('SC_PHYS_PAGES') * PAGE_SIZE 

131 # note: there's no difference on Solaris 

132 free = avail = os.sysconf('SC_AVPHYS_PAGES') * PAGE_SIZE 

133 used = total - free 

134 percent = usage_percent(used, total, round_=1) 

135 return svmem(total, avail, percent, used, free) 

136 

137 

138def swap_memory(): 

139 """Report swap memory metrics.""" 

140 sin, sout = cext.swap_mem() 

141 # XXX 

142 # we are supposed to get total/free by doing so: 

143 # http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/ 

144 # usr/src/cmd/swap/swap.c 

145 # ...nevertheless I can't manage to obtain the same numbers as 'swap' 

146 # cmdline utility, so let's parse its output (sigh!) 

147 p = subprocess.Popen( 

148 [ 

149 '/usr/bin/env', 

150 'PATH=/usr/sbin:/sbin:%s' % os.environ['PATH'], 

151 'swap', 

152 '-l', 

153 ], 

154 stdout=subprocess.PIPE, 

155 ) 

156 stdout, _ = p.communicate() 

157 if PY3: 

158 stdout = stdout.decode(sys.stdout.encoding) 

159 if p.returncode != 0: 

160 raise RuntimeError("'swap -l' failed (retcode=%s)" % p.returncode) 

161 

162 lines = stdout.strip().split('\n')[1:] 

163 if not lines: 

164 msg = 'no swap device(s) configured' 

165 raise RuntimeError(msg) 

166 total = free = 0 

167 for line in lines: 

168 line = line.split() 

169 t, f = line[3:5] 

170 total += int(int(t) * 512) 

171 free += int(int(f) * 512) 

172 used = total - free 

173 percent = usage_percent(used, total, round_=1) 

174 return _common.sswap( 

175 total, used, free, percent, sin * PAGE_SIZE, sout * PAGE_SIZE 

176 ) 

177 

178 

179# ===================================================================== 

180# --- CPU 

181# ===================================================================== 

182 

183 

184def cpu_times(): 

185 """Return system-wide CPU times as a named tuple.""" 

186 ret = cext.per_cpu_times() 

187 return scputimes(*[sum(x) for x in zip(*ret)]) 

188 

189 

190def per_cpu_times(): 

191 """Return system per-CPU times as a list of named tuples.""" 

192 ret = cext.per_cpu_times() 

193 return [scputimes(*x) for x in ret] 

194 

195 

196def cpu_count_logical(): 

197 """Return the number of logical CPUs in the system.""" 

198 try: 

199 return os.sysconf("SC_NPROCESSORS_ONLN") 

200 except ValueError: 

201 # mimic os.cpu_count() behavior 

202 return None 

203 

204 

205def cpu_count_cores(): 

206 """Return the number of CPU cores in the system.""" 

207 return cext.cpu_count_cores() 

208 

209 

210def cpu_stats(): 

211 """Return various CPU stats as a named tuple.""" 

212 ctx_switches, interrupts, syscalls, traps = cext.cpu_stats() 

213 soft_interrupts = 0 

214 return _common.scpustats( 

215 ctx_switches, interrupts, soft_interrupts, syscalls 

216 ) 

217 

218 

219# ===================================================================== 

220# --- disks 

221# ===================================================================== 

222 

223 

224disk_io_counters = cext.disk_io_counters 

225disk_usage = _psposix.disk_usage 

226 

227 

228def disk_partitions(all=False): 

229 """Return system disk partitions.""" 

230 # TODO - the filtering logic should be better checked so that 

231 # it tries to reflect 'df' as much as possible 

232 retlist = [] 

233 partitions = cext.disk_partitions() 

234 for partition in partitions: 

235 device, mountpoint, fstype, opts = partition 

236 if device == 'none': 

237 device = '' 

238 if not all: 

239 # Differently from, say, Linux, we don't have a list of 

240 # common fs types so the best we can do, AFAIK, is to 

241 # filter by filesystem having a total size > 0. 

242 try: 

243 if not disk_usage(mountpoint).total: 

244 continue 

245 except OSError as err: 

246 # https://github.com/giampaolo/psutil/issues/1674 

247 debug("skipping %r: %s" % (mountpoint, err)) 

248 continue 

249 maxfile = maxpath = None # set later 

250 ntuple = _common.sdiskpart( 

251 device, mountpoint, fstype, opts, maxfile, maxpath 

252 ) 

253 retlist.append(ntuple) 

254 return retlist 

255 

256 

257# ===================================================================== 

258# --- network 

259# ===================================================================== 

260 

261 

262net_io_counters = cext.net_io_counters 

263net_if_addrs = cext_posix.net_if_addrs 

264 

265 

266def net_connections(kind, _pid=-1): 

267 """Return socket connections. If pid == -1 return system-wide 

268 connections (as opposed to connections opened by one process only). 

269 Only INET sockets are returned (UNIX are not). 

270 """ 

271 cmap = _common.conn_tmap.copy() 

272 if _pid == -1: 

273 cmap.pop('unix', 0) 

274 if kind not in cmap: 

275 raise ValueError( 

276 "invalid %r kind argument; choose between %s" 

277 % (kind, ', '.join([repr(x) for x in cmap])) 

278 ) 

279 families, types = _common.conn_tmap[kind] 

280 rawlist = cext.net_connections(_pid) 

281 ret = set() 

282 for item in rawlist: 

283 fd, fam, type_, laddr, raddr, status, pid = item 

284 if fam not in families: 

285 continue 

286 if type_ not in types: 

287 continue 

288 # TODO: refactor and use _common.conn_to_ntuple. 

289 if fam in (AF_INET, AF_INET6): 

290 if laddr: 

291 laddr = _common.addr(*laddr) 

292 if raddr: 

293 raddr = _common.addr(*raddr) 

294 status = TCP_STATUSES[status] 

295 fam = sockfam_to_enum(fam) 

296 type_ = socktype_to_enum(type_) 

297 if _pid == -1: 

298 nt = _common.sconn(fd, fam, type_, laddr, raddr, status, pid) 

299 else: 

300 nt = _common.pconn(fd, fam, type_, laddr, raddr, status) 

301 ret.add(nt) 

302 return list(ret) 

303 

304 

305def net_if_stats(): 

306 """Get NIC stats (isup, duplex, speed, mtu).""" 

307 ret = cext.net_if_stats() 

308 for name, items in ret.items(): 

309 isup, duplex, speed, mtu = items 

310 if hasattr(_common, 'NicDuplex'): 

311 duplex = _common.NicDuplex(duplex) 

312 ret[name] = _common.snicstats(isup, duplex, speed, mtu, '') 

313 return ret 

314 

315 

316# ===================================================================== 

317# --- other system functions 

318# ===================================================================== 

319 

320 

321def boot_time(): 

322 """The system boot time expressed in seconds since the epoch.""" 

323 return cext.boot_time() 

324 

325 

326def users(): 

327 """Return currently connected users as a list of namedtuples.""" 

328 retlist = [] 

329 rawlist = cext.users() 

330 localhost = (':0.0', ':0') 

331 for item in rawlist: 

332 user, tty, hostname, tstamp, user_process, pid = item 

333 # note: the underlying C function includes entries about 

334 # system boot, run level and others. We might want 

335 # to use them in the future. 

336 if not user_process: 

337 continue 

338 if hostname in localhost: 

339 hostname = 'localhost' 

340 nt = _common.suser(user, tty, hostname, tstamp, pid) 

341 retlist.append(nt) 

342 return retlist 

343 

344 

345# ===================================================================== 

346# --- processes 

347# ===================================================================== 

348 

349 

350def pids(): 

351 """Returns a list of PIDs currently running on the system.""" 

352 return [int(x) for x in os.listdir(b(get_procfs_path())) if x.isdigit()] 

353 

354 

355def pid_exists(pid): 

356 """Check for the existence of a unix pid.""" 

357 return _psposix.pid_exists(pid) 

358 

359 

360def wrap_exceptions(fun): 

361 """Call callable into a try/except clause and translate ENOENT, 

362 EACCES and EPERM in NoSuchProcess or AccessDenied exceptions. 

363 """ 

364 

365 @functools.wraps(fun) 

366 def wrapper(self, *args, **kwargs): 

367 try: 

368 return fun(self, *args, **kwargs) 

369 except (FileNotFoundError, ProcessLookupError): 

370 # ENOENT (no such file or directory) gets raised on open(). 

371 # ESRCH (no such process) can get raised on read() if 

372 # process is gone in meantime. 

373 if not pid_exists(self.pid): 

374 raise NoSuchProcess(self.pid, self._name) 

375 else: 

376 raise ZombieProcess(self.pid, self._name, self._ppid) 

377 except PermissionError: 

378 raise AccessDenied(self.pid, self._name) 

379 except OSError: 

380 if self.pid == 0: 

381 if 0 in pids(): 

382 raise AccessDenied(self.pid, self._name) 

383 else: 

384 raise 

385 raise 

386 

387 return wrapper 

388 

389 

390class Process: 

391 """Wrapper class around underlying C implementation.""" 

392 

393 __slots__ = ["pid", "_name", "_ppid", "_procfs_path", "_cache"] 

394 

395 def __init__(self, pid): 

396 self.pid = pid 

397 self._name = None 

398 self._ppid = None 

399 self._procfs_path = get_procfs_path() 

400 

401 def _assert_alive(self): 

402 """Raise NSP if the process disappeared on us.""" 

403 # For those C function who do not raise NSP, possibly returning 

404 # incorrect or incomplete result. 

405 os.stat('%s/%s' % (self._procfs_path, self.pid)) 

406 

407 def oneshot_enter(self): 

408 self._proc_name_and_args.cache_activate(self) 

409 self._proc_basic_info.cache_activate(self) 

410 self._proc_cred.cache_activate(self) 

411 

412 def oneshot_exit(self): 

413 self._proc_name_and_args.cache_deactivate(self) 

414 self._proc_basic_info.cache_deactivate(self) 

415 self._proc_cred.cache_deactivate(self) 

416 

417 @wrap_exceptions 

418 @memoize_when_activated 

419 def _proc_name_and_args(self): 

420 return cext.proc_name_and_args(self.pid, self._procfs_path) 

421 

422 @wrap_exceptions 

423 @memoize_when_activated 

424 def _proc_basic_info(self): 

425 if self.pid == 0 and not os.path.exists( 

426 '%s/%s/psinfo' % (self._procfs_path, self.pid) 

427 ): 

428 raise AccessDenied(self.pid) 

429 ret = cext.proc_basic_info(self.pid, self._procfs_path) 

430 assert len(ret) == len(proc_info_map) 

431 return ret 

432 

433 @wrap_exceptions 

434 @memoize_when_activated 

435 def _proc_cred(self): 

436 return cext.proc_cred(self.pid, self._procfs_path) 

437 

438 @wrap_exceptions 

439 def name(self): 

440 # note: max len == 15 

441 return self._proc_name_and_args()[0] 

442 

443 @wrap_exceptions 

444 def exe(self): 

445 try: 

446 return os.readlink( 

447 "%s/%s/path/a.out" % (self._procfs_path, self.pid) 

448 ) 

449 except OSError: 

450 pass # continue and guess the exe name from the cmdline 

451 # Will be guessed later from cmdline but we want to explicitly 

452 # invoke cmdline here in order to get an AccessDenied 

453 # exception if the user has not enough privileges. 

454 self.cmdline() 

455 return "" 

456 

457 @wrap_exceptions 

458 def cmdline(self): 

459 return self._proc_name_and_args()[1].split(' ') 

460 

461 @wrap_exceptions 

462 def environ(self): 

463 return cext.proc_environ(self.pid, self._procfs_path) 

464 

465 @wrap_exceptions 

466 def create_time(self): 

467 return self._proc_basic_info()[proc_info_map['create_time']] 

468 

469 @wrap_exceptions 

470 def num_threads(self): 

471 return self._proc_basic_info()[proc_info_map['num_threads']] 

472 

473 @wrap_exceptions 

474 def nice_get(self): 

475 # Note #1: getpriority(3) doesn't work for realtime processes. 

476 # Psinfo is what ps uses, see: 

477 # https://github.com/giampaolo/psutil/issues/1194 

478 return self._proc_basic_info()[proc_info_map['nice']] 

479 

480 @wrap_exceptions 

481 def nice_set(self, value): 

482 if self.pid in (2, 3): 

483 # Special case PIDs: internally setpriority(3) return ESRCH 

484 # (no such process), no matter what. 

485 # The process actually exists though, as it has a name, 

486 # creation time, etc. 

487 raise AccessDenied(self.pid, self._name) 

488 return cext_posix.setpriority(self.pid, value) 

489 

490 @wrap_exceptions 

491 def ppid(self): 

492 self._ppid = self._proc_basic_info()[proc_info_map['ppid']] 

493 return self._ppid 

494 

495 @wrap_exceptions 

496 def uids(self): 

497 try: 

498 real, effective, saved, _, _, _ = self._proc_cred() 

499 except AccessDenied: 

500 real = self._proc_basic_info()[proc_info_map['uid']] 

501 effective = self._proc_basic_info()[proc_info_map['euid']] 

502 saved = None 

503 return _common.puids(real, effective, saved) 

504 

505 @wrap_exceptions 

506 def gids(self): 

507 try: 

508 _, _, _, real, effective, saved = self._proc_cred() 

509 except AccessDenied: 

510 real = self._proc_basic_info()[proc_info_map['gid']] 

511 effective = self._proc_basic_info()[proc_info_map['egid']] 

512 saved = None 

513 return _common.puids(real, effective, saved) 

514 

515 @wrap_exceptions 

516 def cpu_times(self): 

517 try: 

518 times = cext.proc_cpu_times(self.pid, self._procfs_path) 

519 except OSError as err: 

520 if err.errno == errno.EOVERFLOW and not IS_64_BIT: 

521 # We may get here if we attempt to query a 64bit process 

522 # with a 32bit python. 

523 # Error originates from read() and also tools like "cat" 

524 # fail in the same way (!). 

525 # Since there simply is no way to determine CPU times we 

526 # return 0.0 as a fallback. See: 

527 # https://github.com/giampaolo/psutil/issues/857 

528 times = (0.0, 0.0, 0.0, 0.0) 

529 else: 

530 raise 

531 return _common.pcputimes(*times) 

532 

533 @wrap_exceptions 

534 def cpu_num(self): 

535 return cext.proc_cpu_num(self.pid, self._procfs_path) 

536 

537 @wrap_exceptions 

538 def terminal(self): 

539 procfs_path = self._procfs_path 

540 hit_enoent = False 

541 tty = wrap_exceptions(self._proc_basic_info()[proc_info_map['ttynr']]) 

542 if tty != cext.PRNODEV: 

543 for x in (0, 1, 2, 255): 

544 try: 

545 return os.readlink( 

546 '%s/%d/path/%d' % (procfs_path, self.pid, x) 

547 ) 

548 except FileNotFoundError: 

549 hit_enoent = True 

550 continue 

551 if hit_enoent: 

552 self._assert_alive() 

553 

554 @wrap_exceptions 

555 def cwd(self): 

556 # /proc/PID/path/cwd may not be resolved by readlink() even if 

557 # it exists (ls shows it). If that's the case and the process 

558 # is still alive return None (we can return None also on BSD). 

559 # Reference: http://goo.gl/55XgO 

560 procfs_path = self._procfs_path 

561 try: 

562 return os.readlink("%s/%s/path/cwd" % (procfs_path, self.pid)) 

563 except FileNotFoundError: 

564 os.stat("%s/%s" % (procfs_path, self.pid)) # raise NSP or AD 

565 return "" 

566 

567 @wrap_exceptions 

568 def memory_info(self): 

569 ret = self._proc_basic_info() 

570 rss = ret[proc_info_map['rss']] * 1024 

571 vms = ret[proc_info_map['vms']] * 1024 

572 return pmem(rss, vms) 

573 

574 memory_full_info = memory_info 

575 

576 @wrap_exceptions 

577 def status(self): 

578 code = self._proc_basic_info()[proc_info_map['status']] 

579 # XXX is '?' legit? (we're not supposed to return it anyway) 

580 return PROC_STATUSES.get(code, '?') 

581 

582 @wrap_exceptions 

583 def threads(self): 

584 procfs_path = self._procfs_path 

585 ret = [] 

586 tids = os.listdir('%s/%d/lwp' % (procfs_path, self.pid)) 

587 hit_enoent = False 

588 for tid in tids: 

589 tid = int(tid) 

590 try: 

591 utime, stime = cext.query_process_thread( 

592 self.pid, tid, procfs_path 

593 ) 

594 except EnvironmentError as err: 

595 if err.errno == errno.EOVERFLOW and not IS_64_BIT: 

596 # We may get here if we attempt to query a 64bit process 

597 # with a 32bit python. 

598 # Error originates from read() and also tools like "cat" 

599 # fail in the same way (!). 

600 # Since there simply is no way to determine CPU times we 

601 # return 0.0 as a fallback. See: 

602 # https://github.com/giampaolo/psutil/issues/857 

603 continue 

604 # ENOENT == thread gone in meantime 

605 if err.errno == errno.ENOENT: 

606 hit_enoent = True 

607 continue 

608 raise 

609 else: 

610 nt = _common.pthread(tid, utime, stime) 

611 ret.append(nt) 

612 if hit_enoent: 

613 self._assert_alive() 

614 return ret 

615 

616 @wrap_exceptions 

617 def open_files(self): 

618 retlist = [] 

619 hit_enoent = False 

620 procfs_path = self._procfs_path 

621 pathdir = '%s/%d/path' % (procfs_path, self.pid) 

622 for fd in os.listdir('%s/%d/fd' % (procfs_path, self.pid)): 

623 path = os.path.join(pathdir, fd) 

624 if os.path.islink(path): 

625 try: 

626 file = os.readlink(path) 

627 except FileNotFoundError: 

628 hit_enoent = True 

629 continue 

630 else: 

631 if isfile_strict(file): 

632 retlist.append(_common.popenfile(file, int(fd))) 

633 if hit_enoent: 

634 self._assert_alive() 

635 return retlist 

636 

637 def _get_unix_sockets(self, pid): 

638 """Get UNIX sockets used by process by parsing 'pfiles' output.""" 

639 # TODO: rewrite this in C (...but the damn netstat source code 

640 # does not include this part! Argh!!) 

641 cmd = ["pfiles", str(pid)] 

642 p = subprocess.Popen( 

643 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE 

644 ) 

645 stdout, stderr = p.communicate() 

646 if PY3: 

647 stdout, stderr = ( 

648 x.decode(sys.stdout.encoding) for x in (stdout, stderr) 

649 ) 

650 if p.returncode != 0: 

651 if 'permission denied' in stderr.lower(): 

652 raise AccessDenied(self.pid, self._name) 

653 if 'no such process' in stderr.lower(): 

654 raise NoSuchProcess(self.pid, self._name) 

655 raise RuntimeError("%r command error\n%s" % (cmd, stderr)) 

656 

657 lines = stdout.split('\n')[2:] 

658 for i, line in enumerate(lines): 

659 line = line.lstrip() 

660 if line.startswith('sockname: AF_UNIX'): 

661 path = line.split(' ', 2)[2] 

662 type = lines[i - 2].strip() 

663 if type == 'SOCK_STREAM': 

664 type = socket.SOCK_STREAM 

665 elif type == 'SOCK_DGRAM': 

666 type = socket.SOCK_DGRAM 

667 else: 

668 type = -1 

669 yield (-1, socket.AF_UNIX, type, path, "", _common.CONN_NONE) 

670 

671 @wrap_exceptions 

672 def connections(self, kind='inet'): 

673 ret = net_connections(kind, _pid=self.pid) 

674 # The underlying C implementation retrieves all OS connections 

675 # and filters them by PID. At this point we can't tell whether 

676 # an empty list means there were no connections for process or 

677 # process is no longer active so we force NSP in case the PID 

678 # is no longer there. 

679 if not ret: 

680 # will raise NSP if process is gone 

681 os.stat('%s/%s' % (self._procfs_path, self.pid)) 

682 

683 # UNIX sockets 

684 if kind in ('all', 'unix'): 

685 ret.extend([ 

686 _common.pconn(*conn) 

687 for conn in self._get_unix_sockets(self.pid) 

688 ]) 

689 return ret 

690 

691 nt_mmap_grouped = namedtuple('mmap', 'path rss anon locked') 

692 nt_mmap_ext = namedtuple('mmap', 'addr perms path rss anon locked') 

693 

694 @wrap_exceptions 

695 def memory_maps(self): 

696 def toaddr(start, end): 

697 return '%s-%s' % ( 

698 hex(start)[2:].strip('L'), 

699 hex(end)[2:].strip('L'), 

700 ) 

701 

702 procfs_path = self._procfs_path 

703 retlist = [] 

704 try: 

705 rawlist = cext.proc_memory_maps(self.pid, procfs_path) 

706 except OSError as err: 

707 if err.errno == errno.EOVERFLOW and not IS_64_BIT: 

708 # We may get here if we attempt to query a 64bit process 

709 # with a 32bit python. 

710 # Error originates from read() and also tools like "cat" 

711 # fail in the same way (!). 

712 # Since there simply is no way to determine CPU times we 

713 # return 0.0 as a fallback. See: 

714 # https://github.com/giampaolo/psutil/issues/857 

715 return [] 

716 else: 

717 raise 

718 hit_enoent = False 

719 for item in rawlist: 

720 addr, addrsize, perm, name, rss, anon, locked = item 

721 addr = toaddr(addr, addrsize) 

722 if not name.startswith('['): 

723 try: 

724 name = os.readlink( 

725 '%s/%s/path/%s' % (procfs_path, self.pid, name) 

726 ) 

727 except OSError as err: 

728 if err.errno == errno.ENOENT: 

729 # sometimes the link may not be resolved by 

730 # readlink() even if it exists (ls shows it). 

731 # If that's the case we just return the 

732 # unresolved link path. 

733 # This seems an inconsistency with /proc similar 

734 # to: http://goo.gl/55XgO 

735 name = '%s/%s/path/%s' % (procfs_path, self.pid, name) 

736 hit_enoent = True 

737 else: 

738 raise 

739 retlist.append((addr, perm, name, rss, anon, locked)) 

740 if hit_enoent: 

741 self._assert_alive() 

742 return retlist 

743 

744 @wrap_exceptions 

745 def num_fds(self): 

746 return len(os.listdir("%s/%s/fd" % (self._procfs_path, self.pid))) 

747 

748 @wrap_exceptions 

749 def num_ctx_switches(self): 

750 return _common.pctxsw( 

751 *cext.proc_num_ctx_switches(self.pid, self._procfs_path) 

752 ) 

753 

754 @wrap_exceptions 

755 def wait(self, timeout=None): 

756 return _psposix.wait_pid(self.pid, timeout, self._name)