I'm trying to determine the current working directory of a process given its PID. The command-line utility lsof does something similar. Here's the source to the python script:
import ctypes
from ctypes import util
import sys
PROC_PIDVNODEPATHINFO = 9
proc = ctypes.cdll.LoadLibrary(util.find_library("libproc"))
print(proc.proc_pidinfo)
class vnode_info(ctypes.Structure):
_fields_ = [('data', ctypes.c_ubyte * 152)]
class vnode_info_path(ctypes.Structure):
_fields_ = [('vip_vi', vnode_info), ('vip_path', ctypes.c_char * 1024)]
class proc_vnodepathinfo(ctypes.Structure):
_fields_ = [('pvi_cdir', vnode_info_path), ('pvi_rdir', vnode_info_path)]
inst = proc_vnodepathinfo()
pid = int(sys.argv[1])
ret = proc.proc_pidinfo( pid, PROC_PIDVNODEPATHINFO, 0, ctypes.byref(inst), ctypes.sizeof(inst) )
print(ret, inst.pvi_cdir.vip_path)
However, even though this script behaves as expected on Python 2.6, it does not work in Python 2.5:
host:dir user$ sudo /usr/bin/python2.6 script.py 2698
<_FuncPtr object at 0x100419ae0>
(2352, '/')
host:dir user$ sudo /usr/bin/python2.5 script.py 2698
<_FuncPtr object at 0x19fdc0>
(0, '')
(PID 2698 is "Activity Monitor.app"). Note the different return values. Since this program strongly based on ctypes, I can't imagine any difference in Python itself that would cause this. The same behavior (as Python 2.5) occurs with my self-built Python 3.2.
I'm not sure what versioning information I can give to help track down the weirdness -- or even come up with a solution for 2.5 -- but here's some stuff:
host:dir user$ otool -L /usr/bin/python2.6
/usr/bin/python2.6:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
host:dir user$ otool -L /usr/bin/python2.5
/usr/bin/python2.5 (architecture i386):
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
/usr/bin/python2.5 (architecture ppc7400):
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
host:dir user$ uname -a
Darwin host.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
Thanks to anyone that has a clue about what's going on here:)
I'm not sure what's wrong with your code but I've just implemented this in psutil. These are the parts of interest:
https://code.google.com/p/psutil/source/browse/psutil/_psutil_osx.c?spec=svn46552ff6ac2be53d14b10487ee1cd95d2ef3f57b&r=a025c656b0bfefdca7fad9e0c542a911e97d8cea#132
https://code.google.com/p/psutil/source/browse/psutil/arch/osx/process_info.c?spec=svn46552ff6ac2be53d14b10487ee1cd95d2ef3f57b&r=f93197af5e4af938a9f5ca5117103b1d63b6ad3c#269
You can either decide to translate that in ctypes or write your own extension modudle.