简体   繁体   English

Golang:如何在 Linux 上使用 syscall.Syscall?

[英]Golang: How to use syscall.Syscall on Linux?

There is a very nice description about loading a shared library and calling a function with the syscall package on Windows ( https://github.com/golang/go/wiki/WindowsDLLs ).关于在 Windows ( https://github.com/golang/go/wiki/WindowsDLLs ) 上加载共享库和使用 syscall 包调用函数的描述非常好。 However, the functions LoadLibrary and GetProcAddress that are used in this description are not available in the syscall package on Linux.但是,本说明中使用的函数LoadLibraryGetProcAddress在 Linux 上的 syscall 包中不可用。 I could not find documentation about how to do this on Linux (or Mac OS).我找不到有关如何在 Linux(或 Mac OS)上执行此操作的文档。

Thanks for help感谢帮助

Linux syscalls are used directly without loading a library, exactly how depends on which system call you would like to perform. Linux 系统调用直接使用而不加载库,具体如何取决于您想要执行的系统调用。

I will use the Linux syscall getpid() as an example, which returns the process ID of the calling process (our process, in this case).我将使用 Linux 系统调用 getpid() 作为示例,它返回调用进程(在本例中为我们的进程)的进程 ID。

package main

import (
    "fmt"
    "syscall"
)

func main() {
    pid, _, _ := syscall.Syscall(syscall.SYS_GETPID, 0, 0, 0)
    fmt.Println("process id: ", pid)
}

I capture the result of the syscall in pid, this particular call returns no errors so I use blank identifiers for the rest of the returns.我在 pid 中捕获了 syscall 的结果,这个特定的调用没有返回错误,所以我对其余的返回值使用空白标识符。 Syscall returns two uintptr and 1 error.系统调用返回两个 uintptr 和 1 个错误。

As you can see I can also just pass in 0 for the rest of the function arguments, as I don't need to pass arguments to this syscall.正如你所看到的,我也可以只为其余的函数参数传递 0,因为我不需要将参数传递给这个系统调用。

The function signature is: func Syscall(trap uintptr, nargs uintptr, a1 uintptr, a2 uintptr, a3 uintptr) (r1 uintptr, r2 uintptr, err Errno).函数签名为:func Syscall(trap uintptr, nargs uintptr, a1 uintptr, a2 uintptr, a3 uintptr) (r1 uintptr, r2 uintptr, err Errno)。

For more information refer to https://golang.org/pkg/syscall/有关更多信息,请参阅https://golang.org/pkg/syscall/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM