Source file src/plugin/plugin_dlopen.go

Documentation: plugin

     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 := make([]byte, C.PATH_MAX+1)
    44  	cRelName := make([]byte, len(name)+1)
    45  	copy(cRelName, name)
    46  	if C.realpath(
    47  		(*C.char)(unsafe.Pointer(&cRelName[0])),
    48  		(*C.char)(unsafe.Pointer(&cPath[0]))) == nil {
    49  		return nil, errors.New(`plugin.Open("` + name + `"): realpath failed`)
    50  	}
    51  
    52  	filepath := C.GoString((*C.char)(unsafe.Pointer(&cPath[0])))
    53  
    54  	pluginsMu.Lock()
    55  	if p := plugins[filepath]; p != nil {
    56  		pluginsMu.Unlock()
    57  		if p.err != "" {
    58  			return nil, errors.New(`plugin.Open("` + name + `"): ` + p.err + ` (previous failure)`)
    59  		}
    60  		<-p.loaded
    61  		return p, nil
    62  	}
    63  	var cErr *C.char
    64  	h := C.pluginOpen((*C.char)(unsafe.Pointer(&cPath[0])), &cErr)
    65  	if h == 0 {
    66  		pluginsMu.Unlock()
    67  		return nil, errors.New(`plugin.Open("` + name + `"): ` + C.GoString(cErr))
    68  	}
    69  	// TODO(crawshaw): look for plugin note, confirm it is a Go plugin
    70  	// and it was built with the correct toolchain.
    71  	if len(name) > 3 && name[len(name)-3:] == ".so" {
    72  		name = name[:len(name)-3]
    73  	}
    74  	if plugins == nil {
    75  		plugins = make(map[string]*Plugin)
    76  	}
    77  	pluginpath, syms, errstr := lastmoduleinit()
    78  	if errstr != "" {
    79  		plugins[filepath] = &Plugin{
    80  			pluginpath: pluginpath,
    81  			err:        errstr,
    82  		}
    83  		pluginsMu.Unlock()
    84  		return nil, errors.New(`plugin.Open("` + name + `"): ` + errstr)
    85  	}
    86  	// This function can be called from the init function of a plugin.
    87  	// Drop a placeholder in the map so subsequent opens can wait on it.
    88  	p := &Plugin{
    89  		pluginpath: pluginpath,
    90  		loaded:     make(chan struct{}),
    91  	}
    92  	plugins[filepath] = p
    93  	pluginsMu.Unlock()
    94  
    95  	initStr := make([]byte, len(pluginpath)+6)
    96  	copy(initStr, pluginpath)
    97  	copy(initStr[len(pluginpath):], ".init")
    98  
    99  	initFuncPC := C.pluginLookup(h, (*C.char)(unsafe.Pointer(&initStr[0])), &cErr)
   100  	if initFuncPC != nil {
   101  		initFuncP := &initFuncPC
   102  		initFunc := *(*func())(unsafe.Pointer(&initFuncP))
   103  		initFunc()
   104  	}
   105  
   106  	// Fill out the value of each plugin symbol.
   107  	updatedSyms := map[string]interface{}{}
   108  	for symName, sym := range syms {
   109  		isFunc := symName[0] == '.'
   110  		if isFunc {
   111  			delete(syms, symName)
   112  			symName = symName[1:]
   113  		}
   114  
   115  		fullName := pluginpath + "." + symName
   116  		cname := make([]byte, len(fullName)+1)
   117  		copy(cname, fullName)
   118  
   119  		p := C.pluginLookup(h, (*C.char)(unsafe.Pointer(&cname[0])), &cErr)
   120  		if p == nil {
   121  			return nil, errors.New(`plugin.Open("` + name + `"): could not find symbol ` + symName + `: ` + C.GoString(cErr))
   122  		}
   123  		valp := (*[2]unsafe.Pointer)(unsafe.Pointer(&sym))
   124  		if isFunc {
   125  			(*valp)[1] = unsafe.Pointer(&p)
   126  		} else {
   127  			(*valp)[1] = p
   128  		}
   129  		// we can't add to syms during iteration as we'll end up processing
   130  		// some symbols twice with the inability to tell if the symbol is a function
   131  		updatedSyms[symName] = sym
   132  	}
   133  	p.syms = updatedSyms
   134  
   135  	close(p.loaded)
   136  	return p, nil
   137  }
   138  
   139  func lookup(p *Plugin, symName string) (Symbol, error) {
   140  	if s := p.syms[symName]; s != nil {
   141  		return s, nil
   142  	}
   143  	return nil, errors.New("plugin: symbol " + symName + " not found in plugin " + p.pluginpath)
   144  }
   145  
   146  var (
   147  	pluginsMu sync.Mutex
   148  	plugins   map[string]*Plugin
   149  )
   150  
   151  // lastmoduleinit is defined in package runtime
   152  func lastmoduleinit() (pluginpath string, syms map[string]interface{}, errstr string)
   153  

View as plain text