github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/plugin/plugin_dlopen.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build linux,cgo darwin,cgo
     6  
     7  package plugin
     8  
     9  /*
    10  #cgo linux LDFLAGS: -ldl
    11  #include <dlfcn.h>
    12  #include <limits.h>
    13  #include <stdlib.h>
    14  #include <stdint.h>
    15  
    16  #include <stdio.h>
    17  
    18  static uintptr_t pluginOpen(const char* path, char** err) {
    19  	void* h = dlopen(path, RTLD_NOW|RTLD_GLOBAL);
    20  	if (h == NULL) {
    21  		*err = (char*)dlerror();
    22  	}
    23  	return (uintptr_t)h;
    24  }
    25  
    26  static void* pluginLookup(uintptr_t h, const char* name, char** err) {
    27  	void* r = dlsym((void*)h, name);
    28  	if (r == NULL) {
    29  		*err = (char*)dlerror();
    30  	}
    31  	return r;
    32  }
    33  */
    34  import "C"
    35  
    36  import (
    37  	"errors"
    38  	"sync"
    39  	"unsafe"
    40  )
    41  
    42  func open(name string) (*Plugin, error) {
    43  	cPath := (*C.char)(C.malloc(C.PATH_MAX + 1))
    44  	defer C.free(unsafe.Pointer(cPath))
    45  
    46  	cRelName := C.CString(name)
    47  	defer C.free(unsafe.Pointer(cRelName))
    48  	if C.realpath(cRelName, cPath) == nil {
    49  		return nil, errors.New("plugin.Open(" + name + "): realpath failed")
    50  	}
    51  
    52  	filepath := C.GoString(cPath)
    53  
    54  	pluginsMu.Lock()
    55  	if p := plugins[filepath]; p != nil {
    56  		pluginsMu.Unlock()
    57  		<-p.loaded
    58  		return p, nil
    59  	}
    60  	var cErr *C.char
    61  	h := C.pluginOpen(cPath, &cErr)
    62  	if h == 0 {
    63  		pluginsMu.Unlock()
    64  		return nil, errors.New("plugin.Open: " + C.GoString(cErr))
    65  	}
    66  	// TODO(crawshaw): look for plugin note, confirm it is a Go plugin
    67  	// and it was built with the correct toolchain.
    68  	if len(name) > 3 && name[len(name)-3:] == ".so" {
    69  		name = name[:len(name)-3]
    70  	}
    71  
    72  	pluginpath, syms := lastmoduleinit()
    73  	if plugins == nil {
    74  		plugins = make(map[string]*Plugin)
    75  	}
    76  	// This function can be called from the init function of a plugin.
    77  	// Drop a placeholder in the map so subsequent opens can wait on it.
    78  	p := &Plugin{
    79  		pluginpath: pluginpath,
    80  		loaded:     make(chan struct{}),
    81  		syms:       syms,
    82  	}
    83  	plugins[filepath] = p
    84  	pluginsMu.Unlock()
    85  
    86  	initStr := C.CString(pluginpath + ".init")
    87  	initFuncPC := C.pluginLookup(h, initStr, &cErr)
    88  	C.free(unsafe.Pointer(initStr))
    89  	if initFuncPC != nil {
    90  		initFuncP := &initFuncPC
    91  		initFunc := *(*func())(unsafe.Pointer(&initFuncP))
    92  		initFunc()
    93  	}
    94  
    95  	// Fill out the value of each plugin symbol.
    96  	for symName, sym := range syms {
    97  		isFunc := symName[0] == '.'
    98  		if isFunc {
    99  			delete(syms, symName)
   100  			symName = symName[1:]
   101  		}
   102  
   103  		cname := C.CString(pluginpath + "." + symName)
   104  		p := C.pluginLookup(h, cname, &cErr)
   105  		C.free(unsafe.Pointer(cname))
   106  		if p == nil {
   107  			return nil, errors.New("plugin.Open: could not find symbol " + symName + ": " + C.GoString(cErr))
   108  		}
   109  		valp := (*[2]unsafe.Pointer)(unsafe.Pointer(&sym))
   110  		if isFunc {
   111  			(*valp)[1] = unsafe.Pointer(&p)
   112  		} else {
   113  			(*valp)[1] = p
   114  		}
   115  		syms[symName] = sym
   116  	}
   117  	close(p.loaded)
   118  	return p, nil
   119  }
   120  
   121  func lookup(p *Plugin, symName string) (Symbol, error) {
   122  	if s := p.syms[symName]; s != nil {
   123  		return s, nil
   124  	}
   125  	return nil, errors.New("plugin: symbol " + symName + " not found in plugin " + p.pluginpath)
   126  }
   127  
   128  var (
   129  	pluginsMu sync.Mutex
   130  	plugins   map[string]*Plugin
   131  )
   132  
   133  // lastmoduleinit is defined in package runtime
   134  func lastmoduleinit() (pluginpath string, syms map[string]interface{})