简体   繁体   English

使用 cgo 挂钩系统调用“fork”

[英]using cgo to hook syscall “fork”

I'm trying to hook the fork syscall in Linux using Go.我正在尝试使用 Go 在 Linux 中挂钩 fork 系统调用。

package main

/*
#include <unistd.h>
*/
import "C"

import (
    "fmt"
    "os"

    "github.com/rainycape/dl"
)

func main() {}

//export fork
func fork() C.pid_t {
    lib, err := dl.Open("libc", 0)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer lib.Close()
    var old_fork func() C.pid_t
    lib.Sym("fork", &old_fork)
    return old_fork()
}

When I compile and run as LD_PRELOAD , Panic occurs当我作为LD_PRELOAD编译和运行时,会发生恐慌

panic: reflect.MakeFunc: value of type `int32` is not assignable to type main._Ctype_int

You should set some directives for running that.您应该设置一些指令来运行它。 Please take a look at its official documentation.请查看其官方文档。 using cgo - Golang official doc使用 cgo - Golang 官方文档

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

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