2008-05-21 10:39:30 +00:00
|
|
|
#!/usr/local/bin/python
|
2008-07-17 00:11:56 +00:00
|
|
|
#
|
2008-07-06 12:45:25 +00:00
|
|
|
# Copyright (c) 2008, Thomas Hurst <tom@hur.st>
|
2008-05-21 10:39:30 +00:00
|
|
|
#
|
2008-07-06 12:45:25 +00:00
|
|
|
# Use of this file is unrestricted provided this notice is retained.
|
|
|
|
# If you use it, it'd be nice if you dropped me a note. Also beer.
|
2008-07-17 00:11:56 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
freebsd.get_process_cwd(pid):
|
|
|
|
Use sysctl() to retrieve the cwd of an arbitrary process on FreeBSD
|
|
|
|
using kern.proc.filedesc, as used by procstat(1).
|
|
|
|
Tested on FreeBSD 7-STABLE/amd64 from April 11 2008.
|
|
|
|
"""
|
2008-05-21 10:39:30 +00:00
|
|
|
|
|
|
|
from ctypes import *
|
|
|
|
|
|
|
|
class sockaddr_storage(Structure):
|
2008-07-17 00:11:56 +00:00
|
|
|
"""struct sockaddr_storage, defined in /usr/include/sys/socket.h"""
|
2008-05-21 10:39:30 +00:00
|
|
|
_fields_ = [
|
|
|
|
('ss_len', c_char),
|
|
|
|
('ss_family', c_char), # /usr/include/sys/_types.h; _uint8_t
|
|
|
|
('__ss_pad1', c_char * 6), # (sizeof(int64) - sizeof(char) - sizeof(ss_family_t))
|
|
|
|
('__ss_align', c_longlong),
|
|
|
|
('__ss_pad2', c_char * 112), # (128(maxsize) - sizeof(char) - sizeof(ss_family_t) -
|
|
|
|
# sizeof(ss_pad1) - sizeof(int64))
|
|
|
|
]
|
|
|
|
|
|
|
|
class kinfo_file(Structure):
|
2008-07-17 00:11:56 +00:00
|
|
|
"""struct kinfo_file, defined in /usr/include/sys/user.h """
|
2008-05-21 10:39:30 +00:00
|
|
|
_fields_ = [
|
|
|
|
('kf_structsize', c_int),
|
|
|
|
('kf_type', c_int),
|
|
|
|
('kf_fd', c_int),
|
|
|
|
('kf_ref_count', c_int),
|
|
|
|
('kf_flags', c_int),
|
2008-07-17 00:11:56 +00:00
|
|
|
('kf_offset', c_size_t), # this is a off_t, a pointer
|
2008-05-21 10:39:30 +00:00
|
|
|
('kf_vnode_type', c_int),
|
|
|
|
('kf_sock_domain', c_int),
|
|
|
|
('kf_sock_type', c_int),
|
|
|
|
('kf_sock_protocol', c_int),
|
|
|
|
('kf_path', c_char * 1024), # PATH_MAX
|
|
|
|
('kf_sa_local', sockaddr_storage),
|
|
|
|
('kf_sa_peer', sockaddr_storage),
|
|
|
|
]
|
|
|
|
|
2008-05-22 19:17:53 +00:00
|
|
|
libc = CDLL('libc.so')
|
2008-05-21 10:39:30 +00:00
|
|
|
|
2008-07-17 00:11:56 +00:00
|
|
|
uintlen = c_size_t(sizeof(c_uint))
|
2008-05-22 19:17:53 +00:00
|
|
|
ver = c_uint(0)
|
2008-05-21 10:39:30 +00:00
|
|
|
|
2008-07-17 00:11:56 +00:00
|
|
|
if (libc.sysctlbyname('kern.osreldate', byref(ver), byref(uintlen), None, 0) < 0):
|
2008-05-22 19:17:53 +00:00
|
|
|
raise OSError, "sysctlbyname returned < 0"
|
2008-05-21 10:39:30 +00:00
|
|
|
|
2008-05-22 19:17:53 +00:00
|
|
|
# kern.proc.filedesc added for procstat(1) after these __FreeBSD_versions
|
|
|
|
if ver.value < 700104 and ver.value < 800019:
|
|
|
|
raise NotImplementedError, "cwd detection requires a recent 7.0-STABLE or 8-CURRENT"
|
2008-05-21 10:39:30 +00:00
|
|
|
|
2008-07-17 00:11:56 +00:00
|
|
|
|
2008-05-22 19:17:53 +00:00
|
|
|
def get_process_cwd(pid):
|
2008-07-17 00:11:56 +00:00
|
|
|
"""Return string containing the current working directory of the given pid,
|
|
|
|
or None on failure."""
|
2008-05-21 10:39:30 +00:00
|
|
|
# /usr/include/sys/sysctl.h
|
|
|
|
# CTL_KERN, KERN_PROC, KERN_PROC_FILEDESC
|
|
|
|
oid = (c_uint * 4)(1, 14, 14, pid)
|
|
|
|
|
2008-07-17 00:11:56 +00:00
|
|
|
if libc.sysctl(oid, 4, None, byref(uintlen), None, 0) < 0:
|
2008-05-21 10:39:30 +00:00
|
|
|
return None
|
|
|
|
|
2008-07-17 00:11:56 +00:00
|
|
|
buf = c_char_p(" " * uintlen.value)
|
|
|
|
if libc.sysctl(oid, 4, buf, byref(uintlen), None, 0) < 0:
|
2008-05-21 10:39:30 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
kifs = cast(buf, POINTER(kinfo_file))
|
2008-07-17 00:11:56 +00:00
|
|
|
for i in xrange(0, uintlen.value / sizeof(kinfo_file)):
|
2008-05-21 10:39:30 +00:00
|
|
|
kif = kifs[i]
|
|
|
|
if kif.kf_fd == -1: # KF_FD_TYPE_CWD
|
|
|
|
return kif.kf_path
|
|
|
|
|
2008-07-17 00:11:56 +00:00
|
|
|
|
2008-05-21 10:39:30 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import os, sys
|
|
|
|
print " => %d cwd = %s" % (os.getpid(), get_process_cwd(os.getpid()))
|
|
|
|
for pid in sys.argv:
|
|
|
|
try:
|
|
|
|
pid = int(pid)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print " => %d cwd = %s" % (pid, get_process_cwd(pid))
|
|
|
|
|