dlopen.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package dlopen provides some convenience functions to dlopen a library and
  15. // get its symbols.
  16. package dlopen
  17. // #cgo LDFLAGS: -ldl
  18. // #include <stdlib.h>
  19. // #include <dlfcn.h>
  20. import "C"
  21. import (
  22. "errors"
  23. "fmt"
  24. "unsafe"
  25. )
  26. var ErrSoNotFound = errors.New("unable to open a handle to the library")
  27. // LibHandle represents an open handle to a library (.so)
  28. type LibHandle struct {
  29. Handle unsafe.Pointer
  30. Libname string
  31. }
  32. // GetHandle tries to get a handle to a library (.so), attempting to access it
  33. // by the names specified in libs and returning the first that is successfully
  34. // opened. Callers are responsible for closing the handler. If no library can
  35. // be successfully opened, an error is returned.
  36. func GetHandle(libs []string) (*LibHandle, error) {
  37. for _, name := range libs {
  38. libname := C.CString(name)
  39. defer C.free(unsafe.Pointer(libname))
  40. handle := C.dlopen(libname, C.RTLD_LAZY)
  41. if handle != nil {
  42. h := &LibHandle{
  43. Handle: handle,
  44. Libname: name,
  45. }
  46. return h, nil
  47. }
  48. }
  49. return nil, ErrSoNotFound
  50. }
  51. // GetSymbolPointer takes a symbol name and returns a pointer to the symbol.
  52. func (l *LibHandle) GetSymbolPointer(symbol string) (unsafe.Pointer, error) {
  53. sym := C.CString(symbol)
  54. defer C.free(unsafe.Pointer(sym))
  55. C.dlerror()
  56. p := C.dlsym(l.Handle, sym)
  57. e := C.dlerror()
  58. if e != nil {
  59. return nil, fmt.Errorf("error resolving symbol %q: %v", symbol, errors.New(C.GoString(e)))
  60. }
  61. return p, nil
  62. }
  63. // Close closes a LibHandle.
  64. func (l *LibHandle) Close() error {
  65. C.dlerror()
  66. C.dlclose(l.Handle)
  67. e := C.dlerror()
  68. if e != nil {
  69. return fmt.Errorf("error closing %v: %v", l.Libname, errors.New(C.GoString(e)))
  70. }
  71. return nil
  72. }