Access to sysctl on FreeBSD?

Thomas Heller theller at python.net
Wed May 21 02:29:46 EDT 2008


Skye schrieb:
> Great, thanks for the help (I'm fairly new to Python, didn't know
> about ctypes)
> 
>     from ctypes import *
>     libc = CDLL("libc.so.7")
>     size = c_uint(0)
>     libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
>     buf = c_char_p(" " * size.value)
>     libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)

IIUC, sysctlbyname writes the information you requested into the buffer supplied
as second argument.  If this is true, then the above code is incorrect.
c_char_p(src) creates an object that shares the buffer with the Python string 'src';
but Python strings are immutable and you must not write ctypes code that modifies them.

Instead, you should code:

     from ctypes import *
     libc = CDLL("libc.so.7")
     size = c_uint(0)
     libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
     buf = create_string_buffer(size.value)
     libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)

Thomas



More information about the Python-list mailing list