/src/chilli_module.c

https://gitlab.com/grase/coova-chilli · C · 70 lines · 39 code · 13 blank · 18 comment · 7 complexity · 7a3051f2664c598f88cda6288b01c4e7 MD5 · raw file

  1. /* -*- mode: c; c-basic-offset: 2 -*- */
  2. /*
  3. * Copyright (C) 2007-2012 David Bird (Coova Technologies) <support@coova.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "chilli.h"
  20. #include "chilli_module.h"
  21. #include <dlfcn.h>
  22. int chilli_module_load(void **ctx, char *name) {
  23. struct chilli_module *m = (struct chilli_module *)0;
  24. char path[512] = "";
  25. void *lib_handle = NULL;
  26. char *error = NULL;
  27. void *sym = NULL;
  28. int len = 0;
  29. snprintf(path, sizeof(path), "%s/%s.so",
  30. _options.moddir ? _options.moddir : DEFLIBDIR, name);
  31. lib_handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
  32. if (!lib_handle) {
  33. syslog(LOG_ERR, "%s: chilli_module_load() %s", strerror(errno), dlerror());
  34. return -1;
  35. }
  36. snprintf(path, sizeof(path), "%s_module", name);
  37. len = strlen(path);
  38. while(len-- > 0)
  39. if (path[len]=='-')
  40. path[len] = '_';
  41. sym = dlsym(lib_handle, path);
  42. if ((sym == NULL) || ((error = dlerror()) != NULL)) {
  43. dlclose(lib_handle);
  44. syslog(LOG_ERR, "%s: %s", strerror(errno), error);
  45. return -1;
  46. }
  47. m = (struct chilli_module *) sym;
  48. m->lib = lib_handle;
  49. syslog(LOG_DEBUG, "Loaded module %s", name);
  50. *ctx = m;
  51. return 0;
  52. }
  53. int chilli_module_unload(void *ctx) {
  54. struct chilli_module *m = (struct chilli_module *)ctx;
  55. dlclose(m->lib);
  56. return 0;
  57. }