unix-plugins.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #if !defined(_GNU_SOURCE)
  25. #define _GNU_SOURCE
  26. #endif
  27. #include "private-lib-core.h"
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #include <dlfcn.h>
  31. const lws_plugin_header_t *
  32. lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
  33. const char *sofilename, const char *_class,
  34. each_plugin_cb_t each, void *each_user)
  35. {
  36. const lws_plugin_header_t *hdr;
  37. struct lws_plugin *pin;
  38. char sym[96];
  39. void *l;
  40. int m;
  41. if (strlen(sofilename) < 6)
  42. /* [lib]...[.so] */
  43. return NULL;
  44. l = dlopen(libpath, RTLD_NOW);
  45. if (!l) {
  46. lwsl_err("%s: Error loading DSO: %s\n", __func__, dlerror());
  47. return NULL;
  48. }
  49. /* we could open it... can we get his export struct? */
  50. m = lws_snprintf(sym, sizeof(sym) - 1, "%s", sofilename);
  51. if (m < 4)
  52. goto bail;
  53. if (!strcmp(&sym[m - 3], ".so"))
  54. sym[m - 3] = '\0'; /* snip the .so */
  55. hdr = (const lws_plugin_header_t *)dlsym(l, sym);
  56. if (!hdr) {
  57. lwsl_err("%s: Failed to get export '%s' from %s: %s\n",
  58. __func__, sym, libpath, dlerror());
  59. goto bail;
  60. }
  61. if (hdr->api_magic != LWS_PLUGIN_API_MAGIC) {
  62. lwsl_err("%s: plugin %s has outdated api %d (vs %d)\n",
  63. __func__, libpath, hdr->api_magic,
  64. LWS_PLUGIN_API_MAGIC);
  65. goto bail;
  66. }
  67. if (strcmp(hdr->_class, _class))
  68. goto bail;
  69. pin = lws_malloc(sizeof(*pin), __func__);
  70. if (!pin)
  71. goto bail;
  72. pin->list = *pplugin;
  73. *pplugin = pin;
  74. pin->u.l = l;
  75. pin->hdr = hdr;
  76. if (each)
  77. each(pin, each_user);
  78. return hdr;
  79. bail:
  80. dlclose(l);
  81. return NULL;
  82. }
  83. int
  84. lws_plat_destroy_dl(struct lws_plugin *p)
  85. {
  86. return dlclose(p->u.l);
  87. }