hardware.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2008 The Android Open Source Project
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <hardware/hardware.h>
  8. #include <dlfcn.h>
  9. #include <string.h>
  10. #include <pthread.h>
  11. #include <errno.h>
  12. #include <limits.h>
  13. #include <stdio.h>
  14. #define LOG_TAG "HAL"
  15. #define LOG_INFO " I"
  16. #define LOG_WARN " W"
  17. #define LOG_ERROR " E"
  18. #define LOG_DEBUG " D"
  19. #define ALOG(pri, tag, fmt, arg...) fprintf(stderr, tag pri": " fmt"\n", ##arg)
  20. #define info(fmt, arg...) ALOG(LOG_INFO, LOG_TAG, fmt, ##arg)
  21. #define warn(fmt, arg...) ALOG(LOG_WARN, LOG_TAG, fmt, ##arg)
  22. #define error(fmt, arg...) ALOG(LOG_ERROR, LOG_TAG, fmt, ##arg)
  23. /**
  24. * Load the file defined by the variant and if successful
  25. * return the dlopen handle and the hmi.
  26. * @return 0 = success, !0 = failure.
  27. */
  28. static int load(const char *id,
  29. const char *path,
  30. const struct hw_module_t **pHmi)
  31. {
  32. int status;
  33. void *handle;
  34. struct hw_module_t *hmi;
  35. const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
  36. /*
  37. * load the symbols resolving undefined symbols before
  38. * dlopen returns. Since RTLD_GLOBAL is not or'd in with
  39. * RTLD_NOW the external symbols will not be global
  40. */
  41. handle = dlopen(path, RTLD_NOW);
  42. if (handle == NULL) {
  43. char const *err_str = dlerror();
  44. error("load: module=%s\n%s", path, err_str?err_str:"unknown");
  45. status = -EINVAL;
  46. goto done;
  47. }
  48. /* Get the address of the struct hal_module_info. */
  49. hmi = (struct hw_module_t *)dlsym(handle, sym);
  50. if (hmi == NULL) {
  51. error("load: couldn't find symbol %s", sym);
  52. status = -EINVAL;
  53. goto done;
  54. }
  55. /* Check that the id matches */
  56. if (strcmp(id, hmi->id) != 0) {
  57. error("load: id=%s != hmi->id=%s", id, hmi->id);
  58. status = -EINVAL;
  59. goto done;
  60. }
  61. hmi->dso = handle;
  62. *pHmi = hmi;
  63. info("loaded HAL id=%s path=%s hmi=%p handle=%p",
  64. id, path, *pHmi, handle);
  65. return 0;
  66. done:
  67. hmi = NULL;
  68. if (handle != NULL) {
  69. dlclose(handle);
  70. handle = NULL;
  71. }
  72. return status;
  73. }
  74. int hw_get_module_by_class(const char *class_id, const char *inst,
  75. const struct hw_module_t **module)
  76. {
  77. char path[PATH_MAX];
  78. char name[PATH_MAX/2];
  79. if (inst)
  80. snprintf(name, sizeof(name), "%s.%s", class_id, inst);
  81. else
  82. snprintf(name, sizeof(name), "%s", class_id);
  83. /*
  84. * Here we rely on the fact that calling dlopen multiple times on
  85. * the same .so will simply increment a refcount (and not load
  86. * a new copy of the library).
  87. * We also assume that dlopen() is thread-safe.
  88. */
  89. snprintf(path, sizeof(path), "%s/%s.default.so", PLUGINDIR, name);
  90. return load(class_id, path, module);
  91. }
  92. int hw_get_module(const char *id, const struct hw_module_t **module)
  93. {
  94. return hw_get_module_by_class(id, NULL, module);
  95. }