Skip to content

Commit

Permalink
#728 / solaris: implement PROCFS_PATH for the python code (C still mi…
Browse files Browse the repository at this point in the history
…ssing)
  • Loading branch information
giampaolo committed Dec 26, 2015
1 parent 769352c commit 404c2b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
from ._pssunos import CONN_BOUND # NOQA
from ._pssunos import CONN_IDLE # NOQA

# This is public API and it will be retrieved from _pssunos.py
# via sys.modules.
PROCFS_PATH = "/proc"

else: # pragma: no cover
raise NotImplementedError('platform %s is not supported' % sys.platform)

Expand Down
51 changes: 34 additions & 17 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
from ._common import sockfam_to_enum
from ._common import socktype_to_enum
from ._common import usage_percent
from ._compat import b
from ._compat import PY3


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

PAGE_SIZE = os.sysconf('SC_PAGE_SIZE')
AF_LINK = cext_posix.AF_LINK
Expand Down Expand Up @@ -70,6 +71,13 @@
AccessDenied = None
TimeoutExpired = None


# --- utils

def get_procfs_path():
return sys.modules['psutil'].PROCFS_PATH


# --- functions

disk_io_counters = cext.disk_io_counters
Expand Down Expand Up @@ -122,7 +130,7 @@ def swap_memory():

def pids():
"""Returns a list of PIDs currently running on the system."""
return [int(x) for x in os.listdir('/proc') if x.isdigit()]
return [int(x) for x in os.listdir(b(get_procfs_path())) if x.isdigit()]


def pid_exists(pid):
Expand Down Expand Up @@ -287,7 +295,8 @@ def name(self):
@wrap_exceptions
def exe(self):
try:
return os.readlink("/proc/%s/path/a.out" % self.pid)
return os.readlink(
"%s/%s/path/a.out" % (get_procfs_path(), self.pid))
except OSError:
pass # continue and guess the exe name from the cmdline
# Will be guessed later from cmdline but we want to explicitly
Expand Down Expand Up @@ -358,33 +367,36 @@ def cpu_times(self):

@wrap_exceptions
def terminal(self):
procfs_path = get_procfs_path()
hit_enoent = False
tty = wrap_exceptions(
cext.proc_basic_info(self.pid)[0])
if tty != cext.PRNODEV:
for x in (0, 1, 2, 255):
try:
return os.readlink('/proc/%d/path/%d' % (self.pid, x))
return os.readlink(
'%s/%d/path/%d' % (procfs_path, self.pid, x))
except OSError as err:
if err.errno == errno.ENOENT:
hit_enoent = True
continue
raise
if hit_enoent:
# raise NSP if the process disappeared on us
os.stat('/proc/%s' % self.pid)
os.stat('%s/%s' % (procfs_path, self.pid))

@wrap_exceptions
def cwd(self):
# /proc/PID/path/cwd may not be resolved by readlink() even if
# it exists (ls shows it). If that's the case and the process
# is still alive return None (we can return None also on BSD).
# Reference: http://goo.gl/55XgO
procfs_path = get_procfs_path()
try:
return os.readlink("/proc/%s/path/cwd" % self.pid)
return os.readlink("%s/%s/path/cwd" % (procfs_path, self.pid))
except OSError as err:
if err.errno == errno.ENOENT:
os.stat("/proc/%s" % self.pid)
os.stat("%s/%s" % (procfs_path, self.pid))
return None
raise

Expand All @@ -405,8 +417,9 @@ def status(self):

@wrap_exceptions
def threads(self):
procfs_path = get_procfs_path()
ret = []
tids = os.listdir('/proc/%d/lwp' % self.pid)
tids = os.listdir('%s/%d/lwp' % (procfs_path, self.pid))
hit_enoent = False
for tid in tids:
tid = int(tid)
Expand All @@ -424,15 +437,16 @@ def threads(self):
ret.append(nt)
if hit_enoent:
# raise NSP if the process disappeared on us
os.stat('/proc/%s' % self.pid)
os.stat('%s/%s' % (procfs_path, self.pid))
return ret

@wrap_exceptions
def open_files(self):
retlist = []
hit_enoent = False
pathdir = '/proc/%d/path' % self.pid
for fd in os.listdir('/proc/%d/fd' % self.pid):
procfs_path = get_procfs_path()
pathdir = '%s/%d/path' % (procfs_path, self.pid)
for fd in os.listdir('%s/%d/fd' % (procfs_path, self.pid)):
path = os.path.join(pathdir, fd)
if os.path.islink(path):
try:
Expand All @@ -448,7 +462,7 @@ def open_files(self):
retlist.append(_common.popenfile(file, int(fd)))
if hit_enoent:
# raise NSP if the process disappeared on us
os.stat('/proc/%s' % self.pid)
os.stat('%s/%s' % (procfs_path, self.pid))
return retlist

def _get_unix_sockets(self, pid):
Expand Down Expand Up @@ -492,7 +506,8 @@ def connections(self, kind='inet'):
# process is no longer active so we force NSP in case the PID
# is no longer there.
if not ret:
os.stat('/proc/%s' % self.pid) # will raise NSP if process is gone
# will raise NSP if process is gone
os.stat('%s/%s' % (get_procfs_path(), self.pid))

# UNIX sockets
if kind in ('all', 'unix'):
Expand All @@ -509,6 +524,7 @@ def toaddr(start, end):
return '%s-%s' % (hex(start)[2:].strip('L'),
hex(end)[2:].strip('L'))

procfs_path = get_procfs_path()
retlist = []
rawlist = cext.proc_memory_maps(self.pid)
hit_enoent = False
Expand All @@ -517,7 +533,8 @@ def toaddr(start, end):
addr = toaddr(addr, addrsize)
if not name.startswith('['):
try:
name = os.readlink('/proc/%s/path/%s' % (self.pid, name))
name = os.readlink(
'%s/%s/path/%s' % (procfs_path, self.pid, name))
except OSError as err:
if err.errno == errno.ENOENT:
# sometimes the link may not be resolved by
Expand All @@ -526,19 +543,19 @@ def toaddr(start, end):
# unresolved link path.
# This seems an incosistency with /proc similar
# to: http://goo.gl/55XgO
name = '/proc/%s/path/%s' % (self.pid, name)
name = '%s/%s/path/%s' % (procfs_path, self.pid, name)
hit_enoent = True
else:
raise
retlist.append((addr, perm, name, rss, anon, locked))
if hit_enoent:
# raise NSP if the process disappeared on us
os.stat('/proc/%s' % self.pid)
os.stat('%s/%s' % (procfs_path, self.pid))
return retlist

@wrap_exceptions
def num_fds(self):
return len(os.listdir("/proc/%s/fd" % self.pid))
return len(os.listdir("%s/%s/fd" % (get_procfs_path(), self.pid)))

@wrap_exceptions
def num_ctx_switches(self):
Expand Down

0 comments on commit 404c2b0

Please sign in to comment.