dlopen.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 yi.qiu first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtm.h>
  12. #include <string.h>
  13. #include "dlmodule.h"
  14. #define MODULE_ROOT_DIR "/modules"
  15. /**
  16. * @brief dynamically load a shared library at runtime.
  17. *
  18. * @param filename the path to the shared library to load, which shouldn't be set to NULL.
  19. * @param flags options for loading the shared library.
  20. * @return void* on success, it returns a handle (a pointer) to the opened shared library, otherwise it returns NULL.
  21. *
  22. * @note This function is an API of POSIX standard, which is used for dynamically loading shared libraries at runtime.
  23. * the function first tries to check if the module is already loaded, by finding module in module list.
  24. * If module is found in memory (RT_NULL check fails), the reference count (nref) is incremented.
  25. * Otherwise, dlmodule_load() will be called to load the module into memory.
  26. * A handle (a pointer to the module) is returned at last, which can be used with other functions like dlsym().
  27. */
  28. void* dlopen(const char *filename, int flags)
  29. {
  30. struct rt_dlmodule *module;
  31. char *fullpath;
  32. const char*def_path = MODULE_ROOT_DIR;
  33. /* check parameters */
  34. RT_ASSERT(filename != RT_NULL);
  35. if (filename[0] != '/') /* it's a relative path, prefix with MODULE_ROOT_DIR */
  36. {
  37. fullpath = rt_malloc(strlen(def_path) + strlen(filename) + 2);
  38. /* join path and file name */
  39. rt_snprintf(fullpath, strlen(def_path) + strlen(filename) + 2,
  40. "%s/%s", def_path, filename);
  41. }
  42. else
  43. {
  44. fullpath = (char*)filename; /* absolute path, use it directly */
  45. }
  46. rt_enter_critical();
  47. /* find in module list */
  48. module = dlmodule_find(fullpath);
  49. if(module != RT_NULL)
  50. {
  51. rt_exit_critical();
  52. module->nref++;
  53. }
  54. else
  55. {
  56. rt_exit_critical();
  57. module = dlmodule_load(fullpath);
  58. }
  59. if(fullpath != filename)
  60. {
  61. rt_free(fullpath);
  62. }
  63. return (void*)module;
  64. }
  65. RTM_EXPORT(dlopen);