// Lgetxattr retrieves the value of the extended attribute identified by attr // and associated with the given path in the file system. // It will returns a nil slice and nil error if the xattr is not set. func Lgetxattr(path string, attr string) ([]byte, error) { pathBytes, err := syscall.BytePtrFromString(path) if err != nil { return nil, err } attrBytes, err := syscall.BytePtrFromString(attr) if err != nil { return nil, err } dest := make([]byte, 128) destBytes := unsafe.Pointer(&dest[0]) sz, _, errno := syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0) if errno == syscall.ENODATA { return nil, nil } if errno == syscall.ERANGE { dest = make([]byte, sz) destBytes := unsafe.Pointer(&dest[0]) sz, _, errno = syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0) } if errno != 0 { return nil, errno } return dest[:sz], nil }
func executable() (string, error) { var mib [4]int32 switch runtime.GOOS { case "freebsd": mib = [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1} case "darwin": mib = [4]int32{1 /* CTL_KERN */, 38 /* KERN_PROCARGS */, int32(os.Getpid()), -1} } n := uintptr(0) // get length _, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0) if err != 0 { return "", err } if n == 0 { // shouldn't happen return "", nil } buf := make([]byte, n) _, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0) if err != 0 { return "", err } if n == 0 { // shouldn't happen return "", nil } p := string(buf[:n-1]) if !filepath.IsAbs(p) { if cacheWDErr != nil { return p, cacheWDErr } p = filepath.Join(cacheWD, filepath.Clean(p)) } return filepath.EvalSymlinks(p) }
func sysctlCptime(ncpu int, curcpu int, load *[C.CPUSTATES]C.long) error { var mib []int32 // Use the correct mib based on the number of CPUs and fill out the // current CPU number in case of SMP. (0 indexed cf. self.List) if ncpu == 0 { mib = []int32{C.CTL_KERN, C.KERN_CPTIME} } else { mib = []int32{C.CTL_KERN, C.KERN_CPTIME2, int32(curcpu)} } len := len(mib) n := uintptr(0) // First we determine how much memory we'll need to pass later on (via `n`) _, _, errno := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), uintptr(len), 0, uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { return nil } _, _, errno = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), uintptr(len), uintptr(unsafe.Pointer(load)), uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { return nil } return nil }
func lockEcho() (quit chan int, err error) { echoLockMutex.Lock() defer echoLockMutex.Unlock() if echoLocked { err = ErrPoolWasStarted return } echoLocked = true fd := tty.Fd() if _, _, e := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); e != 0 { err = fmt.Errorf("Can't get terminal settings: %v", e) return } newState := oldState newState.Lflag &^= syscall.ECHO newState.Lflag |= syscall.ICANON | syscall.ISIG newState.Iflag |= syscall.ICRNL if _, _, e := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); e != 0 { err = fmt.Errorf("Can't set terminal settings: %v", e) return } quit = make(chan int, 1) go catchTerminate(quit) return }
func (self *CpuList) Get() error { mib := [2]int32{C.CTL_HW, C.HW_NCPU} var ncpu int n := uintptr(0) // First we determine how much memory we'll need to pass later on (via `n`) _, _, errno := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 2, 0, uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { return nil } // Now perform the actual sysctl(3) call, storing the result in ncpu _, _, errno = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 2, uintptr(unsafe.Pointer(&ncpu)), uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { return nil } load := [C.CPUSTATES]C.long{C.CP_USER, C.CP_NICE, C.CP_SYS, C.CP_INTR, C.CP_IDLE} self.List = make([]Cpu, ncpu) for curcpu, _ := range self.List { sysctlCptime(ncpu, curcpu, &load) fillCpu(&self.List[curcpu], load) } return nil }
func inputEventsProducer() { var input k32_input for { // Wait for events r0, _, e1 := syscall.Syscall6(k32_WaitForMultipleObjects.Addr(), 4, uintptr(len(eventHandles)), uintptr(unsafe.Pointer(&eventHandles[0])), 0, 0xFFFFFFFF, 0, 0) if uint32(r0) == 0xFFFFFFFF { input_comm <- keyEvent{err: getError(e1)} } select { case <-cancel_comm: cancel_done_comm <- true return default: } // Get console input r0, _, e1 = syscall.Syscall6(k32_ReadConsoleInputW.Addr(), 4, uintptr(hConsoleIn), uintptr(unsafe.Pointer(&input)), 1, uintptr(unsafe.Pointer(&tmpArg)), 0, 0) if int(r0) == 0 { input_comm <- keyEvent{err: getError(e1)} } if input.event_type == 0x1 { // key_event kEvent := (*k32_event)(unsafe.Pointer(&input.event)) ev, ok := getKeyEvent(kEvent) if ok { for i := 0; i < int(kEvent.repeat_count); i++ { input_comm <- ev } } } } }
func DragQueryFile(hDrop HDROP, iFile uint) (fileName string, fileCount uint) { ret, _, _ := syscall.Syscall6(procDragQueryFile, 4, uintptr(hDrop), uintptr(iFile), 0, 0, 0, 0) fileCount = uint(ret) if iFile != 0xFFFFFFFF { buf := make([]uint16, fileCount+1) ret, _, _ := syscall.Syscall6(procDragQueryFile, 4, uintptr(hDrop), uintptr(iFile), uintptr(unsafe.Pointer(&buf[0])), uintptr(fileCount+1), 0, 0) if ret == 0 { panic("Invoke DragQueryFile error.") } fileName = syscall.UTF16ToString(buf) } return }
func darwinSyscall() (*bytes.Buffer, error) { mib := [4]int32{_CTRL_KERN, _KERN_PROC, _KERN_PROC_ALL, 0} size := uintptr(0) _, _, errno := syscall.Syscall6( syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&size)), 0, 0) if errno != 0 { return nil, errno } bs := make([]byte, size) _, _, errno = syscall.Syscall6( syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&bs[0])), uintptr(unsafe.Pointer(&size)), 0, 0) if errno != 0 { return nil, errno } return bytes.NewBuffer(bs[0:size]), nil }
func (auth *SSPIAuth) Free() { syscall.Syscall6(sec_fn.DeleteSecurityContext, 1, uintptr(unsafe.Pointer(&auth.ctxt)), 0, 0, 0, 0, 0) syscall.Syscall6(sec_fn.FreeCredentialsHandle, 1, uintptr(unsafe.Pointer(&auth.cred)), 0, 0, 0, 0, 0) }
func memoryStats() (*memory, error) { m := new(memory) // get page size pgsz_mib := []uint32{ 6, // CTL_HW 7, // HW_PAGESIZE } pgsz := uintptr(0) sz := uintptr(unsafe.Sizeof(pgsz)) _, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&pgsz_mib[0])), 2, uintptr(unsafe.Pointer(&pgsz)), uintptr(unsafe.Pointer(&sz)), 0, 0) if err != 0 { return nil, err } // get memory stats vm_mib := []uint32{ 2, // CTL_VM 1, // VM_METER } // from OpenBSD /usr/include/sys/vmmeter.h var vmtotal struct { t_rq uint16 /* length of the run queue */ t_dw uint16 /* jobs in ``disk wait'' (neg priority) */ t_pw uint16 /* jobs in page wait */ t_sl uint16 /* jobs sleeping in core */ t_sw uint16 /* swapped out runnable/short block jobs */ t_vm uint32 /* total virtual memory */ t_avm uint32 /* active virtual memory */ t_rm uint32 /* total real memory in use */ t_arm uint32 /* active real memory */ t_vmshr uint32 /* shared virtual memory */ t_avmshr uint32 /* active shared virtual memory */ t_rmshr uint32 /* shared real memory */ t_armshr uint32 /* active shared real memory */ t_free uint32 /* free memory pages */ } sz = uintptr(unsafe.Sizeof(vmtotal)) _, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&vm_mib[0])), 2, uintptr(unsafe.Pointer(&vmtotal)), uintptr(unsafe.Pointer(&sz)), 0, 0) if err != 0 { return nil, err } m.Total = uint64(vmtotal.t_avm+vmtotal.t_free) * uint64(pgsz) m.Usage = uint64(vmtotal.t_avm) * uint64(pgsz) return m, nil }
func getCurrentExecutePath() (string, error) { var mib [4]int32 switch runtime.GOOS { case "freebsd": mib = [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1} case "darwin": mib = [4]int32{1 /* CTL_KERN */, 38 /* KERN_PROCARGS */, int32(os.Getpid()), -1} } n := uintptr(0) // Get length. _, _, errNum := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0) if errNum != 0 { return "", errNum } if n == 0 { // This shouldn't happen. return "", nil } buf := make([]byte, n) _, _, errNum = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0) if errNum != 0 { return "", errNum } if n == 0 { // This shouldn't happen. return "", nil } for i, v := range buf { if v == 0 { buf = buf[:i] break } } var err error execPath := string(buf) // execPath will not be empty due to above checks. // Try to get the absolute path if the execPath is not rooted. if execPath[0] != '/' { execPath, err = getAbs(execPath) if err != nil { return execPath, err } } // For darwin KERN_PROCARGS may return the path to a symlink rather than the // actual executable. if runtime.GOOS == "darwin" { if execPath, err = filepath.EvalSymlinks(execPath); err != nil { return execPath, err } } return execPath, nil }
func NoEcho(fd uintptr) (*Termios, error) { var s Termios if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCGETA), uintptr(unsafe.Pointer(&s)), 0, 0, 0); err != 0 { return nil, err } oldState := s s.Lflag &^= syscall.ECHO | ECHOE | ECHOK if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSETA), uintptr(unsafe.Pointer(&s)), 0, 0, 0); err != 0 { return nil, err } return &oldState, nil }
func main() { var err error var b []byte var l uintptr flag.Parse() if len(flag.Args()) < 1 { flag.PrintDefaults() log.Fatalf("usage: kexec [flags] kernelname") } kernel := flag.Args()[0] if *cmdline != "" { b = append(b, []byte(*cmdline)...) l = uintptr(len(b)) + 1 } else { b, err = ioutil.ReadFile("/proc/cmdline") if err != nil { log.Fatalf("%v", err) } b[len(b)-1] = 0 l = uintptr(len(b)) } p := uintptr(unsafe.Pointer(&b[0])) log.Printf("Loading %v\n", kernel) if kern, err = syscall.Open(kernel, syscall.O_RDONLY, 0); err != nil { log.Fatalf("%v", err) } if ramfs, err = syscall.Open(*initramfs, syscall.O_RDONLY, 0); err != nil { flags |= KEXEC_FILE_NO_INITRAMFS } log.Printf("command line: '%v'", string(b)) log.Printf("%v %v %v %v %v %v", 320, uintptr(kern), uintptr(ramfs), p, l, flags) if *dryrun { log.Printf("Dry run -- exiting now") return } if e1, e2, err := syscall.Syscall6(320, uintptr(kern), uintptr(ramfs), l, p, flags, uintptr(0)); err != 0 { log.Fatalf("a %v b %v err %v", e1, e2, err) } if e1, e2, err := syscall.Syscall6(syscall.SYS_REBOOT, syscall.LINUX_REBOOT_MAGIC1, syscall.LINUX_REBOOT_MAGIC2, syscall.LINUX_REBOOT_CMD_KEXEC, 0, 0, 0); err != 0 { log.Fatalf("a %v b %v err %v", e1, e2, err) } }
func executable() (string, error) { var mib [4]int32 switch runtime.GOOS { case "freebsd": mib = [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1} case "darwin": mib = [4]int32{1 /* CTL_KERN */, 38 /* KERN_PROCARGS */, int32(os.Getpid()), -1} } n := uintptr(0) // get length _, _, err := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0) if err != 0 { return "", err } if n == 0 { // shouldn't happen return "", nil } buf := make([]byte, n) _, _, err = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0) if err != 0 { return "", err } if n == 0 { // shouldn't happen return "", nil } for i, v := range buf { if v == 0 { buf = buf[:i] break } } var strpath string if buf[0] != '/' { var e error if strpath, e = getAbs(buf); e != nil { return strpath, e } } else { strpath = string(buf) } // darwin KERN_PROCARGS may return the path to a symlink rather than the // actual executable if runtime.GOOS == "darwin" { if strpath, err := filepath.EvalSymlinks(strpath); err != nil { return strpath, err } } return strpath, nil }
func MakeCbreak(fd int) (*termState, error) { var oldState termState if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 { return nil, err } newState := oldState.termios newState.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXON | syscall.IXOFF newState.Lflag &^= syscall.ECHO | syscall.ICANON if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(setTermios), uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { return nil, err } return &oldState, nil }
// MakeRaw put the terminal connected to the given file descriptor into raw // mode and returns the previous state of the terminal so that it can be // restored. func MakeRaw(fd int) (*State, error) { var oldState State if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 { return nil, err } newState := oldState.termios newState.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXON | syscall.IXOFF newState.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.ISIG if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { return nil, err } return &oldState, nil }
func kenv_sys(what int, name unsafe.Pointer, value unsafe.Pointer, size int) (int, error) { ret, _, err := syscall.Syscall6(SYS_KENV, uintptr(what), uintptr(name), uintptr(value), uintptr(size), 0, 0) if err != 0 { return -1, syscall.Errno(err) } return int(ret), nil }
func GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) { r0, _, _ := syscall.Syscall6(procGetAdaptersAddresses.Addr(), 5, uintptr(family), uintptr(flags), uintptr(reserved), uintptr(unsafe.Pointer(adapterAddresses)), uintptr(unsafe.Pointer(sizePointer)), 0) if r0 != 0 { errcode = syscall.Errno(r0) } return }
// getTermios gets the current settings for the terminal. func getTermios() syscall.Termios { var state syscall.Termios if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(0), uintptr(syscall.TIOCGETA), uintptr(unsafe.Pointer(&state)), 0, 0, 0); err != 0 { fmt.Fprintln(os.Stderr, err) } return state }
func createEvent() (syscall.Handle, error) { r0, _, e0 := syscall.Syscall6(procCreateEvent.Addr(), 4, 0, 0, 0, 0, 0, 0) if r0 == 0 { return 0, syscall.Errno(e0) } return syscall.Handle(r0), nil }
func NetShareAdd(serverName *uint16, level uint32, buf *byte, parmErr *uint16) (neterr error) { r0, _, _ := syscall.Syscall6(procNetShareAdd.Addr(), 4, uintptr(unsafe.Pointer(serverName)), uintptr(level), uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(parmErr)), 0, 0) if r0 != 0 { neterr = syscall.Errno(r0) } return }
func (k *Kernel) Wrap(p *syscall.LazyProc) CallFunc { return func(args ...uintptr) error { var r0 uintptr var e1 syscall.Errno size := uintptr(len(args)) if len(args) <= 3 { buf := make([]uintptr, 3) copy(buf, args) r0, _, e1 = syscall.Syscall(p.Addr(), size, buf[0], buf[1], buf[2]) } else { buf := make([]uintptr, 6) copy(buf, args) r0, _, e1 = syscall.Syscall6(p.Addr(), size, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], ) } if int(r0) == 0 { if e1 != 0 { return error(e1) } else { return syscall.EINVAL } } return nil } }
func Prctl(option int, arg2, arg3, arg4, arg5 uintptr) (err error) { _, _, e1 := syscall.Syscall6(syscall.SYS_PRCTL, uintptr(option), arg2, arg3, arg4, arg5, 0) if e1 != 0 { err = e1 } return }
func RegSetValueEx(key syscall.Handle, valueName *uint16, reserved uint32, vtype uint32, buf *byte, bufsize uint32) (regerrno error) { r0, _, _ := syscall.Syscall6(procRegSetValueExW.Addr(), 6, uintptr(key), uintptr(unsafe.Pointer(valueName)), uintptr(reserved), uintptr(vtype), uintptr(unsafe.Pointer(buf)), uintptr(bufsize)) if r0 != 0 { regerrno = syscall.Errno(r0) } return }
func StartBusiness() int32 { ret, _, _ := syscall.Syscall6(startBusiness, 1, uintptr(unsafe.Pointer(nil)), 0, 0, 0, 0, 0) return int32(ret) }
func add_key(keyType, keyDesc string, payload []byte, id int32) (int32, error) { var ( err error errno syscall.Errno b1, b2 *byte r1 uintptr pptr unsafe.Pointer ) if b1, err = syscall.BytePtrFromString(keyType); err != nil { return 0, err } if b2, err = syscall.BytePtrFromString(keyDesc); err != nil { return 0, err } if len(payload) > 0 { pptr = unsafe.Pointer(&payload[0]) } r1, _, errno = syscall.Syscall6(syscall_add_key, uintptr(unsafe.Pointer(b1)), uintptr(unsafe.Pointer(b2)), uintptr(pptr), uintptr(len(payload)), uintptr(id), 0) if errno != 0 { err = errno return 0, err } return int32(r1), nil }
func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) { _, _, e1 := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(vallen), 0) if e1 != 0 { err = e1 } return }
func setVfsCap(path string, data *vfscapData) (err error) { var _p0 *byte _p0, err = syscall.BytePtrFromString(path) if err != nil { return } var size uintptr if data.version == 1 { data.magic = vfsCapVer1 size = vfscapDataSizeV1 } else if data.version == 2 { data.magic = vfsCapVer2 if data.effective[0] != 0 || data.effective[1] != 0 { data.magic |= vfsCapFlageffective } size = vfscapDataSizeV2 } else { return syscall.EINVAL } _, _, e1 := syscall.Syscall6(syscall.SYS_SETXATTR, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(_vfsXattrName)), uintptr(unsafe.Pointer(data)), size, 0, 0) if e1 != 0 { err = e1 } return }
// GetSize returns the dimensions of the given terminal. func getTermWidth(writer io.Writer) int { envColumns := os.Getenv("COLUMNS") if envColumns != "" { num, _ := strconv.Atoi(envColumns) if num != 0 { return num } } ws := getWriterState(writer) if ws.termWidth != 0 { return ws.termWidth } var fd int if writer == os.Stdout { fd = syscall.Stdout } else { // For custom writers, just use the width we get for stderr. This might not be true in some // cases (and for those cases, we should add an option to explicitly set width), but it will // be true in most cases. fd = syscall.Stderr } var dimensions [4]uint16 if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 { // Fall back to a width of 80 return 80 } return int(dimensions[1]) }
func TestSupportedSyscalls(t *testing.T) { supp, err := DetectSupportedSyscalls() if err != nil { t.Skipf("skipping: %v", err) } // These are safe to execute with invalid arguments. safe := []string{ "memfd_create", "sendfile", "bpf$MAP_CREATE", "open", "openat", "read", "write", "stat", } for _, name := range safe { c := sys.CallMap[name] if c == nil { t.Fatalf("can't find syscall '%v'", name) } a := ^uintptr(0) - 4097 // hopefully invalid _, _, err := syscall.Syscall6(uintptr(c.NR), a, a, a, a, a, a) if err == 0 { t.Fatalf("%v did not fail", name) } if ok := err != syscall.ENOSYS; ok != supp[c] { t.Fatalf("syscall %v: perse=%v kallsyms=%v", name, ok, supp[c]) } } }