2009-08-11 22:36:37 +00:00
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
2010-01-14 23:33:06 +00:00
|
|
|
"""cwd.py - function necessary to get the cwd for a given pid on various OSes
|
|
|
|
|
|
|
|
>>> cwd = get_default_cwd()
|
|
|
|
>>> cwd.__class__.__name__
|
|
|
|
'str'
|
2015-06-19 01:58:41 +00:00
|
|
|
>>> func = get_pid_cwd()
|
|
|
|
>>> func.__class__.__name__
|
|
|
|
'function'
|
2010-01-14 23:33:06 +00:00
|
|
|
|
|
|
|
"""
|
2009-08-11 22:36:37 +00:00
|
|
|
|
|
|
|
import platform
|
|
|
|
import os
|
2009-08-11 22:48:19 +00:00
|
|
|
import pwd
|
2018-04-24 18:22:10 +00:00
|
|
|
from .util import dbg, err
|
2009-08-11 22:36:37 +00:00
|
|
|
|
2015-09-01 21:05:24 +00:00
|
|
|
try:
|
|
|
|
import psutil
|
|
|
|
psutil_avail = True
|
|
|
|
except (ImportError):
|
|
|
|
dbg('psutil not found')
|
|
|
|
psutil_avail = False
|
|
|
|
|
2009-08-11 22:48:19 +00:00
|
|
|
def get_default_cwd():
|
|
|
|
"""Determine a reasonable default cwd"""
|
2020-05-14 19:39:50 +00:00
|
|
|
try:
|
|
|
|
cwd = os.getcwd()
|
|
|
|
except (FileNotFoundError,OSError):
|
|
|
|
err("unable to set current working directory, does not exist")
|
|
|
|
cwd = '/'
|
2009-08-18 11:59:06 +00:00
|
|
|
|
|
|
|
return(cwd)
|
2009-08-11 22:48:19 +00:00
|
|
|
|
2015-06-19 01:58:41 +00:00
|
|
|
def get_pid_cwd():
|
|
|
|
"""Determine an appropriate cwd function for the OS we are running on"""
|
|
|
|
|
|
|
|
func = lambda pid: None
|
|
|
|
system = platform.system()
|
|
|
|
|
|
|
|
if system == 'Linux':
|
|
|
|
dbg('Using Linux get_pid_cwd')
|
|
|
|
func = linux_get_pid_cwd
|
|
|
|
elif system == 'FreeBSD':
|
|
|
|
try:
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import freebsd
|
2015-06-19 01:58:41 +00:00
|
|
|
func = freebsd.get_process_cwd
|
|
|
|
dbg('Using FreeBSD get_pid_cwd')
|
|
|
|
except (OSError, NotImplementedError, ImportError):
|
|
|
|
dbg('FreeBSD version too old for get_pid_cwd')
|
|
|
|
elif system == 'SunOS':
|
|
|
|
dbg('Using SunOS get_pid_cwd')
|
|
|
|
func = sunos_get_pid_cwd
|
2015-09-01 21:05:24 +00:00
|
|
|
elif psutil_avail:
|
|
|
|
func = psutil_cwd
|
2015-06-19 01:58:41 +00:00
|
|
|
else:
|
|
|
|
dbg('Unable to determine a get_pid_cwd for OS: %s' % system)
|
|
|
|
|
|
|
|
return(func)
|
|
|
|
|
|
|
|
def proc_get_pid_cwd(pid, path):
|
|
|
|
"""Extract the cwd of a PID from proc, given the PID and the /proc path to
|
|
|
|
insert it into, e.g. /proc/%s/cwd"""
|
|
|
|
try:
|
|
|
|
cwd = os.path.realpath(path % pid)
|
2018-04-24 18:22:10 +00:00
|
|
|
except Exception as ex:
|
2015-06-19 01:58:41 +00:00
|
|
|
err('Unable to get cwd for PID %s: %s' % (pid, ex))
|
|
|
|
cwd = '/'
|
|
|
|
|
|
|
|
return(cwd)
|
|
|
|
|
|
|
|
def linux_get_pid_cwd(pid):
|
|
|
|
"""Determine the cwd for a given PID on Linux kernels"""
|
|
|
|
return(proc_get_pid_cwd(pid, '/proc/%s/cwd'))
|
|
|
|
|
|
|
|
def sunos_get_pid_cwd(pid):
|
|
|
|
"""Determine the cwd for a given PID on SunOS kernels"""
|
|
|
|
return(proc_get_pid_cwd(pid, '/proc/%s/path/cwd'))
|
|
|
|
|
2015-09-01 21:05:24 +00:00
|
|
|
def psutil_cwd(pid):
|
|
|
|
"""Determine the cwd using psutil which also supports Darwin"""
|
|
|
|
return psutil.Process(pid).as_dict()['cwd']
|
|
|
|
|
2009-08-11 22:36:37 +00:00
|
|
|
# vim: set expandtab ts=4 sw=4:
|