Skip to content

Commit

Permalink
Merge pull request #557 from 0-wiz-0/master
Browse files Browse the repository at this point in the history
Add basic NetBSD support.
  • Loading branch information
giampaolo committed Jan 13, 2016
2 parents d780526 + 343d21b commit 859d615
Show file tree
Hide file tree
Showing 11 changed files with 1,402 additions and 63 deletions.
3 changes: 2 additions & 1 deletion psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
elif sys.platform.startswith("darwin"):
from . import _psosx as _psplatform

elif sys.platform.startswith("freebsd") or sys.platform.startswith("openbsd"):
elif sys.platform.startswith("freebsd") or sys.platform.startswith("openbsd") \
or sys.platform.startswith("netbsd"):
from . import _psbsd as _psplatform

elif sys.platform.startswith("sunos"):
Expand Down
41 changes: 36 additions & 5 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

FREEBSD = sys.platform.startswith("freebsd")
OPENBSD = sys.platform.startswith("openbsd")
NETBSD = sys.platform.startswith("netbsd")

if FREEBSD:
PROC_STATUSES = {
Expand Down Expand Up @@ -60,6 +61,15 @@
cext.SRUN: _common.STATUS_WAKING,
cext.SONPROC: _common.STATUS_RUNNING,
}
elif NETBSD:
PROC_STATUSES = {
cext.SIDL: _common.STATUS_IDLE,
cext.SACTIVE: _common.STATUS_RUNNING,
cext.SDYING: _common.STATUS_ZOMBIE,
cext.SSTOP: _common.STATUS_STOPPED,
cext.SZOMB: _common.STATUS_ZOMBIE,
cext.SDEAD: _common.STATUS_DEAD,
}

TCP_STATUSES = {
cext.TCPS_ESTABLISHED: _common.CONN_ESTABLISHED,
Expand All @@ -76,7 +86,10 @@
cext.PSUTIL_CONN_NONE: _common.CONN_NONE,
}

PAGESIZE = os.sysconf("SC_PAGE_SIZE")
if NETBSD:
PAGESIZE = os.sysconf("SC_PAGESIZE")
else:
PAGESIZE = os.sysconf("SC_PAGE_SIZE")
AF_LINK = cext_posix.AF_LINK

# extend base mem ntuple with BSD-specific memory metrics
Expand Down Expand Up @@ -156,9 +169,9 @@ def cpu_count_logical():
return cext.cpu_count_logical()


if OPENBSD:
if OPENBSD or NETBSD:
def cpu_count_physical():
# OpenBSD does not implement this.
# OpenBSD and NetBSD do not implement this.
return 1 if cpu_count_logical() == 1 else None
else:
def cpu_count_physical():
Expand Down Expand Up @@ -273,7 +286,7 @@ def net_if_stats():
return ret


if OPENBSD:
if OPENBSD or NETBSD:
def pid_exists(pid):
exists = _psposix.pid_exists(pid)
if not exists:
Expand Down Expand Up @@ -333,7 +346,7 @@ def name(self):

@wrap_exceptions
def exe(self):
if FREEBSD:
if FREEBSD or NETBSD:
return cext.proc_exe(self.pid)
else:
# exe cannot be determined on OpenBSD; references:
Expand Down Expand Up @@ -426,6 +439,24 @@ def connections(self, kind='inet'):
if kind not in conn_tmap:
raise ValueError("invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in conn_tmap])))

if NETBSD:
families, types = conn_tmap[kind]
ret = set()
rawlist = cext.proc_connections(self.pid)
for item in rawlist:
fd, fam, type, laddr, raddr, status = item
if fam in families and type in types:
try:
status = TCP_STATUSES[status]
except KeyError:
status = TCP_STATUSES[cext.PSUTIL_CONN_NONE]
fam = sockfam_to_enum(fam)
type = socktype_to_enum(type)
nt = _common.pconn(fd, fam, type, laddr, raddr, status)
ret.add(nt)
return list(ret)

families, types = conn_tmap[kind]
rawlist = cext.proc_connections(self.pid, families, types)
ret = []
Expand Down
Loading

0 comments on commit 859d615

Please sign in to comment.