コード例 #1
0
ファイル: host.go プロジェクト: Harvey-OS/go
// testUnnamed tests that two plugins built with .go files passed on
// the command line do not have overlapping symbols. That is,
// unnamed1.so/FuncInt and unnamed2.so/FuncInt should be distinct functions.
func testUnnamed() {
	p, err := plugin.Open("unnamed1.so")
	if err != nil {
		log.Fatalf(`plugin.Open("unnamed1.so"): %v`, err)
	}
	fn, err := p.Lookup("FuncInt")
	if err != nil {
		log.Fatalf(`unnamed1.so: Lookup("FuncInt") failed: %v`, err)
	}
	if got, want := fn.(func() int)(), 1; got != want {
		log.Fatalf("unnamed1.so: FuncInt()=%d, want %d", got, want)
	}

	p, err = plugin.Open("unnamed2.so")
	if err != nil {
		log.Fatalf(`plugin.Open("unnamed2.so"): %v`, err)
	}
	fn, err = p.Lookup("FuncInt")
	if err != nil {
		log.Fatalf(`unnamed2.so: Lookup("FuncInt") failed: %v`, err)
	}
	if got, want := fn.(func() int)(), 2; got != want {
		log.Fatalf("unnamed2.so: FuncInt()=%d, want %d", got, want)
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: kavehmz/garbage
func loadMyApp() {
	timer := time.NewTicker(time.Second)
	for {
		<-timer.C
		glog.Info("Checking time ", time.Now().Unix())
		fname := fmt.Sprintf("%d.so", time.Now().Unix())
		if _, err := os.Stat(fname); os.IsNotExist(err) {
			continue
		}
		p, err := plugin.Open(fname)
		if err != nil {
			log.Println(err)
			continue
		}
		glog.Info("Plugin loaded: ", fname)

		f, err := p.Lookup("MyAppHandler")
		if err != nil {
			log.Println(err)
			continue
		}
		glog.Info("Func loaded")

		myApp.app = p
		h := f.(func(http.ResponseWriter, *http.Request))
		myApp.Lock()
		myApp.handler = &h
		myApp.Unlock()
	}
}
コード例 #3
0
ファイル: host.go プロジェクト: achanda/go
func main() {
	if got, want := common.X, 3*5; got != want {
		log.Fatalf("before plugin load common.X=%d, want %d", got, want)
	}

	p, err := plugin.Open("plugin1.so")
	if err != nil {
		log.Fatalf("plugin.Open failed: %v", err)
	}

	const wantX = 3 * 5 * 7
	if got := common.X; got != wantX {
		log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)
	}

	seven, err := p.Lookup("Seven")
	if err != nil {
		log.Fatalf(`Lookup("Seven") failed: %v`, err)
	}
	if got, want := *seven.(*int), 7; got != want {
		log.Fatalf("via lookup plugin1.Seven=%d, want %d", got, want)
	}

	readFunc, err := p.Lookup("ReadCommonX")
	if err != nil {
		log.Fatalf(`Lookup("ReadCommonX") failed: %v`, err)
	}
	if got := readFunc.(func() int)(); got != wantX {
		log.Fatalf("via lookup plugin1.ReadCommonX()=%d, want %d", got, wantX)
	}

	fmt.Println("PASS")
}
コード例 #4
0
ファイル: driver.go プロジェクト: skx/go-experiments
//
// Call the `HANDLES` method of the specified plugin.
//
func plugin_handles(path string) string {
	p, err := plugin.Open(path)
	if err != nil {
		panic(err)
	}
	f, err := p.Lookup("HANDLES")
	if err != nil {
		panic(err)
	}
	x := f.(func() string)()
	return (x)
}
コード例 #5
0
ファイル: main.go プロジェクト: kavehmz/garbage
func main() {

	p, err := plugin.Open("plugin.so")
	if err != nil {
		log.Panic(err)
	}

	f, err := p.Lookup("Say")
	if err != nil {
		log.Panic(err)
	}
	f.(func())() // prints "Hello or Bye based on which plugin is available in plugin.so"

}
コード例 #6
0
ファイル: driver.go プロジェクト: skx/go-experiments
//
// Given the path to a plugin, and a string argument, invoke the plugin
// method.
//
func run_plugin(path string, arg string) {
	p, err := plugin.Open(path)
	if err != nil {
		panic(err)
	}
	v, err := p.Lookup("INPUT")
	if err != nil {
		panic(err)
	}
	f, err := p.Lookup("RUN_TEST")
	if err != nil {
		panic(err)
	}
	*v.(*string) = arg
	f.(func())() // call the plugin

}
コード例 #7
0
ファイル: host.go プロジェクト: Harvey-OS/go
func main() {
	if got, want := common.X, 3*5; got != want {
		log.Fatalf("before plugin load common.X=%d, want %d", got, want)
	}

	p, err := plugin.Open("plugin1.so")
	if err != nil {
		log.Fatalf("plugin.Open failed: %v", err)
	}

	const wantX = 3 * 5 * 7
	if got := common.X; got != wantX {
		log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)
	}

	seven, err := p.Lookup("Seven")
	if err != nil {
		log.Fatalf(`Lookup("Seven") failed: %v`, err)
	}
	if got, want := *seven.(*int), 7; got != want {
		log.Fatalf("plugin1.Seven=%d, want %d", got, want)
	}

	readFunc, err := p.Lookup("ReadCommonX")
	if err != nil {
		log.Fatalf(`plugin1.Lookup("ReadCommonX") failed: %v`, err)
	}
	if got := readFunc.(func() int)(); got != wantX {
		log.Fatalf("plugin1.ReadCommonX()=%d, want %d", got, wantX)
	}

	// sub/plugin1.so is a different plugin with the same name as
	// the already loaded plugin. It also depends on common. Test
	// that we can load the different plugin, it is actually
	// different, and that it sees the same common package.
	subpPath, err := filepath.Abs("sub/plugin1.so")
	if err != nil {
		log.Fatalf("filepath.Abs(%q) failed: %v", subpPath, err)
	}
	subp, err := plugin.Open(subpPath)
	if err != nil {
		log.Fatalf("plugin.Open(%q) failed: %v", subpPath, err)
	}

	funcVar, err := subp.Lookup("FuncVar")
	if err != nil {
		log.Fatalf(`sub/plugin1.Lookup("FuncVar") failed: %v`, err)
	}
	called := false
	*funcVar.(*func()) = func() {
		called = true
	}

	readFunc, err = subp.Lookup("ReadCommonX")
	if err != nil {
		log.Fatalf(`sub/plugin1.Lookup("ReadCommonX") failed: %v`, err)
	}
	if got := readFunc.(func() int)(); got != wantX {
		log.Fatalf("sub/plugin1.ReadCommonX()=%d, want %d", got, wantX)
	}
	if !called {
		log.Fatal("calling ReadCommonX did not call FuncVar")
	}

	subf, err := subp.Lookup("F")
	if err != nil {
		log.Fatalf(`sub/plugin1.Lookup("F") failed: %v`, err)
	}
	if gotf := subf.(func() int)(); gotf != 17 {
		log.Fatalf(`sub/plugin1.F()=%d, want 17`, gotf)
	}
	f, err := p.Lookup("F")
	if err != nil {
		log.Fatalf(`plugin1.Lookup("F") failed: %v`, err)
	}
	if gotf := f.(func() int)(); gotf != 3 {
		log.Fatalf(`plugin1.F()=%d, want 17`, gotf)
	}

	// plugin2 has no exported symbols, only an init function.
	if _, err := plugin.Open("plugin2.so"); err != nil {
		log.Fatalf("plugin.Open failed: %v", err)
	}
	if got, want := common.X, 2; got != want {
		log.Fatalf("after loading plugin2, common.X=%d, want %d", got, want)
	}

	_, err = plugin.Open("plugin-mismatch.so")
	if err == nil {
		log.Fatal(`plugin.Open("plugin-mismatch.so"): should have failed`)
	}
	if s := err.Error(); !strings.Contains(s, "different version") {
		log.Fatalf(`plugin.Open("plugin-mismatch.so"): error does not mention "different version": %v`, s)
	}

	testUnnamed()

	fmt.Println("PASS")
}