unix-init.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #define _GNU_SOURCE
  22. #include "core/private.h"
  23. #include <pwd.h>
  24. #include <grp.h>
  25. #ifdef LWS_WITH_PLUGINS
  26. #include <dlfcn.h>
  27. #endif
  28. #include <dirent.h>
  29. int
  30. lws_plat_init(struct lws_context *context,
  31. const struct lws_context_creation_info *info)
  32. {
  33. int fd;
  34. /* master context has the global fd lookup array */
  35. context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
  36. context->max_fds, "lws_lookup");
  37. if (context->lws_lookup == NULL) {
  38. lwsl_err("OOM on lws_lookup array for %d connections\n",
  39. context->max_fds);
  40. return 1;
  41. }
  42. lwsl_info(" mem: platform fd map: %5lu bytes\n",
  43. (unsigned long)(sizeof(struct lws *) * context->max_fds));
  44. fd = lws_open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
  45. context->fd_random = fd;
  46. if (context->fd_random < 0) {
  47. lwsl_err("Unable to open random device %s %d\n",
  48. SYSTEM_RANDOM_FILEPATH, context->fd_random);
  49. return 1;
  50. }
  51. #ifdef LWS_WITH_PLUGINS
  52. if (info->plugin_dirs && (context->options & LWS_SERVER_OPTION_LIBUV))
  53. lws_plat_plugins_init(context, info->plugin_dirs);
  54. #endif
  55. return 0;
  56. }
  57. int
  58. lws_plat_context_early_init(void)
  59. {
  60. #if !defined(LWS_AVOID_SIGPIPE_IGN)
  61. signal(SIGPIPE, SIG_IGN);
  62. #endif
  63. return 0;
  64. }
  65. void
  66. lws_plat_context_early_destroy(struct lws_context *context)
  67. {
  68. }
  69. void
  70. lws_plat_context_late_destroy(struct lws_context *context)
  71. {
  72. #ifdef LWS_WITH_PLUGINS
  73. if (context->plugin_list)
  74. lws_plat_plugins_destroy(context);
  75. #endif
  76. if (context->lws_lookup)
  77. lws_free(context->lws_lookup);
  78. if (!context->fd_random)
  79. lwsl_err("ZERO RANDOM FD\n");
  80. if (context->fd_random != LWS_INVALID_FILE)
  81. close(context->fd_random);
  82. }