plugin.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  3. *
  4. * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
  5. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include <dlfcn.h>
  20. #include "uhttpd.h"
  21. #include "plugin.h"
  22. static LIST_HEAD(plugins);
  23. static const struct uhttpd_ops ops = {
  24. .dispatch_add = uh_dispatch_add,
  25. .path_match = uh_path_match,
  26. .create_process = uh_create_process,
  27. .get_process_vars = uh_get_process_vars,
  28. .http_header = uh_http_header,
  29. .client_error = uh_client_error,
  30. .request_done = uh_request_done,
  31. .chunk_write = uh_chunk_write,
  32. .chunk_printf = uh_chunk_printf,
  33. .urlencode = uh_urlencode,
  34. .urldecode = uh_urldecode,
  35. };
  36. int uh_plugin_init(const char *name)
  37. {
  38. struct uhttpd_plugin *p;
  39. const char *sym;
  40. void *dlh;
  41. dlh = dlopen(name, RTLD_LAZY | RTLD_GLOBAL);
  42. if (!dlh) {
  43. fprintf(stderr, "Could not open plugin %s: %s\n", name, dlerror());
  44. return -ENOENT;
  45. }
  46. sym = "uhttpd_plugin";
  47. p = dlsym(dlh, sym);
  48. if (!p) {
  49. fprintf(stderr, "Could not find symbol '%s' in plugin '%s'\n", sym, name);
  50. return -ENOENT;
  51. }
  52. list_add(&p->list, &plugins);
  53. return p->init(&ops, &conf);
  54. }
  55. void uh_plugin_post_init(void)
  56. {
  57. struct uhttpd_plugin *p;
  58. list_for_each_entry(p, &plugins, list)
  59. if (p->post_init)
  60. p->post_init();
  61. }