functions.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2015 RedHat, Inc.
  2. // Copyright 2015 CoreOS, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package sdjournal
  16. import (
  17. "github.com/coreos/pkg/dlopen"
  18. "sync"
  19. "unsafe"
  20. )
  21. var (
  22. // lazy initialized
  23. libsystemdHandle *dlopen.LibHandle
  24. libsystemdMutex = &sync.Mutex{}
  25. libsystemdFunctions = map[string]unsafe.Pointer{}
  26. libsystemdNames = []string{
  27. // systemd < 209
  28. "libsystemd-journal.so.0",
  29. "libsystemd-journal.so",
  30. // systemd >= 209 merged libsystemd-journal into libsystemd proper
  31. "libsystemd.so.0",
  32. "libsystemd.so",
  33. }
  34. )
  35. func getFunction(name string) (unsafe.Pointer, error) {
  36. libsystemdMutex.Lock()
  37. defer libsystemdMutex.Unlock()
  38. if libsystemdHandle == nil {
  39. h, err := dlopen.GetHandle(libsystemdNames)
  40. if err != nil {
  41. return nil, err
  42. }
  43. libsystemdHandle = h
  44. }
  45. f, ok := libsystemdFunctions[name]
  46. if !ok {
  47. var err error
  48. f, err = libsystemdHandle.GetSymbolPointer(name)
  49. if err != nil {
  50. return nil, err
  51. }
  52. libsystemdFunctions[name] = f
  53. }
  54. return f, nil
  55. }