/
plugin_posix.go
68 lines (56 loc) · 1.49 KB
/
plugin_posix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//go:build !windows
// +build !windows
// Copyright 2023 The Yock Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package util
// #include <dlfcn.h>
// #include <stdlib.h>
import "C"
import (
"errors"
"fmt"
"unsafe"
)
var (
_ Plugin = (*PosixPlugin)(nil)
_ PluginFunc = (*PosixPluginFunc)(nil)
)
type PosixPlugin struct {
handle unsafe.Pointer
}
func NewPlugin(path string) (Plugin, error) {
c_str := C.CString(path)
defer C.free(unsafe.Pointer(c_str))
h := C.dlopen(c_str, C.int(C.RTLD_NOW))
if h == nil {
return nil, fmt.Errorf("%s", C.GoString(C.dlerror()))
}
return &PosixPlugin{
handle: h,
}, nil
}
// Func return PluginFunc which is an abstract function to be exported dynamic library
// according to funcName. You can use PluginFunc to call function from dynamic library.
func (pp *PosixPlugin) Func(name string) (PluginFunc, error) {
c_sym := C.CString(name)
defer C.free(unsafe.Pointer(c_sym))
c_addr := C.dlsym(pp.handle, c_sym)
if c_addr == nil {
return nil, fmt.Errorf("%s not found", name)
}
return &PosixPluginFunc{
sym: uintptr(c_addr),
}, nil
}
type PosixPluginFunc struct {
sym uintptr
}
// Call return excuted result from dynamic library
func (ppf *PosixPluginFunc) Call(params ...uintptr) (uintptr, error) {
return uintptr(0), errors.New("fail to assert func")
}
// Call return excuted result from dynamic library
func (ppf *PosixPluginFunc) Addr() uintptr {
return ppf.sym
}