Commit b444d438 authored by David Crawshaw's avatar David Crawshaw

plugin: darwin support

Change-Id: I76981d1d83da401178226634d076371a04f5ccb7
Reviewed-on: https://go-review.googlesource.com/29392
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent b4c9829c
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build linux,cgo // +build linux,cgo darwin,cgo
package plugin package plugin
...@@ -13,6 +13,8 @@ package plugin ...@@ -13,6 +13,8 @@ package plugin
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
static uintptr_t pluginOpen(const char* path, char** err) { static uintptr_t pluginOpen(const char* path, char** err) {
void* h = dlopen(path, RTLD_NOW|RTLD_GLOBAL); void* h = dlopen(path, RTLD_NOW|RTLD_GLOBAL);
if (h == NULL) { if (h == NULL) {
...@@ -38,12 +40,18 @@ import ( ...@@ -38,12 +40,18 @@ import (
) )
func open(name string) (*Plugin, error) { func open(name string) (*Plugin, error) {
pluginsMu.Lock() cPath := (*C.char)(C.malloc(C.PATH_MAX + 1))
defer C.free(unsafe.Pointer(cPath))
cRelName := C.CString(name) cRelName := C.CString(name)
cPath := C.realpath(cRelName, nil) if C.realpath(cRelName, cPath) == nil {
return nil, errors.New("plugin.Open(" + name + "): realpath failed")
}
C.free(unsafe.Pointer(cRelName)) C.free(unsafe.Pointer(cRelName))
defer C.free(unsafe.Pointer(cPath))
path := C.GoString(cPath) path := C.GoString(cPath)
pluginsMu.Lock()
if p := plugins[path]; p != nil { if p := plugins[path]; p != nil {
pluginsMu.Unlock() pluginsMu.Unlock()
<-p.loaded <-p.loaded
...@@ -61,6 +69,7 @@ func open(name string) (*Plugin, error) { ...@@ -61,6 +69,7 @@ func open(name string) (*Plugin, error) {
if len(name) > 3 && name[len(name)-3:] == ".so" { if len(name) > 3 && name[len(name)-3:] == ".so" {
name = name[:len(name)-3] name = name[:len(name)-3]
} }
syms := lastmoduleinit() syms := lastmoduleinit()
if plugins == nil { if plugins == nil {
plugins = make(map[string]*Plugin) plugins = make(map[string]*Plugin)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build !linux !cgo // +build !linux,!darwin !cgo
package plugin package plugin
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment