Make len a size_t, don't overwrite the len builtin.

This commit is contained in:
Thomas Hurst 2008-07-17 01:11:56 +01:00
parent 8d207af1e4
commit a1c3bc264d
1 changed files with 21 additions and 16 deletions

View File

@ -1,20 +1,21 @@
#!/usr/local/bin/python #!/usr/local/bin/python
# ------------------------------------------------------------------ #
# Copyright (c) 2008, Thomas Hurst <tom@hur.st> # Copyright (c) 2008, Thomas Hurst <tom@hur.st>
# #
# Use of this file is unrestricted provided this notice is retained. # 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. # If you use it, it'd be nice if you dropped me a note. Also beer.
# ------------------------------------------------------------------
# """
# freebsd.get_process_cwd(pid): freebsd.get_process_cwd(pid):
# Use sysctl() to retrieve the cwd of an arbitrary process on FreeBSD Use sysctl() to retrieve the cwd of an arbitrary process on FreeBSD
# using kern.proc.filedesc, as used by procstat(1). using kern.proc.filedesc, as used by procstat(1).
# Tested on FreeBSD 7-STABLE/amd64 from April 11 2008. Tested on FreeBSD 7-STABLE/amd64 from April 11 2008.
"""
from ctypes import * from ctypes import *
# This is padded awkwardly, see /usr/include/sys/socket.h
class sockaddr_storage(Structure): class sockaddr_storage(Structure):
"""struct sockaddr_storage, defined in /usr/include/sys/socket.h"""
_fields_ = [ _fields_ = [
('ss_len', c_char), ('ss_len', c_char),
('ss_family', c_char), # /usr/include/sys/_types.h; _uint8_t ('ss_family', c_char), # /usr/include/sys/_types.h; _uint8_t
@ -24,15 +25,15 @@ class sockaddr_storage(Structure):
# sizeof(ss_pad1) - sizeof(int64)) # sizeof(ss_pad1) - sizeof(int64))
] ]
# struct kinfo_file, defined in /usr/include/sys/user.h
class kinfo_file(Structure): class kinfo_file(Structure):
"""struct kinfo_file, defined in /usr/include/sys/user.h """
_fields_ = [ _fields_ = [
('kf_structsize', c_int), ('kf_structsize', c_int),
('kf_type', c_int), ('kf_type', c_int),
('kf_fd', c_int), ('kf_fd', c_int),
('kf_ref_count', c_int), ('kf_ref_count', c_int),
('kf_flags', c_int), ('kf_flags', c_int),
('kf_offset', c_long), # this is a off_t, a pointer ('kf_offset', c_size_t), # this is a off_t, a pointer
('kf_vnode_type', c_int), ('kf_vnode_type', c_int),
('kf_sock_domain', c_int), ('kf_sock_domain', c_int),
('kf_sock_type', c_int), ('kf_sock_type', c_int),
@ -44,34 +45,38 @@ class kinfo_file(Structure):
libc = CDLL('libc.so') libc = CDLL('libc.so')
len = c_uint(sizeof(c_uint)) uintlen = c_size_t(sizeof(c_uint))
ver = c_uint(0) ver = c_uint(0)
if (libc.sysctlbyname('kern.osreldate', byref(ver), byref(len), None, 0) < 0): if (libc.sysctlbyname('kern.osreldate', byref(ver), byref(uintlen), None, 0) < 0):
raise OSError, "sysctlbyname returned < 0" raise OSError, "sysctlbyname returned < 0"
# kern.proc.filedesc added for procstat(1) after these __FreeBSD_versions # kern.proc.filedesc added for procstat(1) after these __FreeBSD_versions
if ver.value < 700104 and ver.value < 800019: if ver.value < 700104 and ver.value < 800019:
raise NotImplementedError, "cwd detection requires a recent 7.0-STABLE or 8-CURRENT" raise NotImplementedError, "cwd detection requires a recent 7.0-STABLE or 8-CURRENT"
def get_process_cwd(pid): def get_process_cwd(pid):
"""Return string containing the current working directory of the given pid,
or None on failure."""
# /usr/include/sys/sysctl.h # /usr/include/sys/sysctl.h
# CTL_KERN, KERN_PROC, KERN_PROC_FILEDESC # CTL_KERN, KERN_PROC, KERN_PROC_FILEDESC
oid = (c_uint * 4)(1, 14, 14, pid) oid = (c_uint * 4)(1, 14, 14, pid)
if libc.sysctl(oid, 4, None, byref(len), None, 0) < 0: if libc.sysctl(oid, 4, None, byref(uintlen), None, 0) < 0:
return None return None
buf = c_char_p(" " * len.value) buf = c_char_p(" " * uintlen.value)
if libc.sysctl(oid, 4, buf, byref(len), None, 0) < 0: if libc.sysctl(oid, 4, buf, byref(uintlen), None, 0) < 0:
return None return None
kifs = cast(buf, POINTER(kinfo_file)) kifs = cast(buf, POINTER(kinfo_file))
for i in xrange(0, len.value / sizeof(kinfo_file)): for i in xrange(0, uintlen.value / sizeof(kinfo_file)):
kif = kifs[i] kif = kifs[i]
if kif.kf_fd == -1: # KF_FD_TYPE_CWD if kif.kf_fd == -1: # KF_FD_TYPE_CWD
return kif.kf_path return kif.kf_path
if __name__ == '__main__': if __name__ == '__main__':
import os, sys import os, sys
print " => %d cwd = %s" % (os.getpid(), get_process_cwd(os.getpid())) print " => %d cwd = %s" % (os.getpid(), get_process_cwd(os.getpid()))