From ed224cf912104853bf94ac723512ef189654b745 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 18 Jul 2024 19:31:25 +0800 Subject: [PATCH] os.Sysctl --- c/os/os.go | 32 +++++++++++++++++++++++++++++ internal/lib/syscall/syscall_bsd.go | 12 +++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/c/os/os.go b/c/os/os.go index e3efb039..d250ae84 100644 --- a/c/os/os.go +++ b/c/os/os.go @@ -328,3 +328,35 @@ func Getrlimit(resource c.Int, rlp *syscall.Rlimit) c.Int func Setrlimit(resource c.Int, rlp *syscall.Rlimit) c.Int // ----------------------------------------------------------------------------- + +// Upon successful completion, the value 0 is returned; otherwise the value -1 +// is returned and the global variable errno is set to indicate the error. +// +//go:linkname Sysctl C.sysctl +func Sysctl( + name *c.Int, namelen c.Uint, + oldp c.Pointer, oldlenp *uintptr, + newp c.Pointer, newlen uintptr) c.Int + +//go:linkname Sysctlbyname C.sysctlbyname +func Sysctlbyname( + name *c.Char, oldp c.Pointer, oldlenp *uintptr, + newp c.Pointer, newlen uintptr) c.Int + +// The sysctlnametomib() function accepts an ASCII representation of the +// name, looks up the integer name vector, and returns the numeric repre- +// sentation in the mib array pointed to by mibp. The number of elements +// in the mib array is given by the location specified by sizep before the +// call, and that location gives the number of entries copied after a suc- +// cessful call. The resulting mib and size may be used in subsequent +// sysctl() calls to get the data associated with the requested ASCII +// name. This interface is intended for use by applications that want to +// repeatedly request the same variable (the sysctl() function runs in +// about a third the time as the same request made via the sysctlbyname() +// function). The sysctlnametomib() function is also useful for fetching +// mib prefixes and then adding a final component. +// +//go:linkname Sysctlnametomib C.sysctlnametomib +func Sysctlnametomib(name *c.Char, mibp *c.Int, sizep *uintptr) c.Int + +// ----------------------------------------------------------------------------- diff --git a/internal/lib/syscall/syscall_bsd.go b/internal/lib/syscall/syscall_bsd.go index 715b463d..86c34364 100644 --- a/internal/lib/syscall/syscall_bsd.go +++ b/internal/lib/syscall/syscall_bsd.go @@ -13,7 +13,10 @@ package syscall import ( + "unsafe" + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/os" "github.com/goplus/llgo/c/syscall" ) @@ -456,11 +459,16 @@ func SysctlUint32(name string) (value uint32, err error) { return 0, err } if n != 4 { - return 0, EIO + return 0, Errno(syscall.EIO) } return *(*uint32)(unsafe.Pointer(&buf[0])), nil */ - panic("todo: syscall.SysctlUint32") + n := uintptr(4) + ret := os.Sysctlbyname(c.AllocaCStr(name), unsafe.Pointer(&value), &n, nil, 0) + if ret != 0 { + err = Errno(os.Errno) + } + return } /*