unix-init.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 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. #ifdef LWS_WITH_PLUGINS
  31. #include <dlfcn.h>
  32. #endif
  33. #include <dirent.h>
  34. #if defined(LWS_WITH_NETWORK)
  35. static void
  36. lws_sul_plat_unix(lws_sorted_usec_list_t *sul)
  37. {
  38. struct lws_context_per_thread *pt =
  39. lws_container_of(sul, struct lws_context_per_thread, sul_plat);
  40. struct lws_context *context = pt->context;
  41. #if !defined(LWS_NO_DAEMONIZE)
  42. /* if our parent went down, don't linger around */
  43. if (pt->context->started_with_parent &&
  44. kill(pt->context->started_with_parent, 0) < 0)
  45. kill(getpid(), SIGTERM);
  46. #endif
  47. if (pt->context->deprecated && !pt->context->count_wsi_allocated) {
  48. lwsl_notice("%s: ending deprecated context\n", __func__);
  49. kill(getpid(), SIGINT);
  50. return;
  51. }
  52. lws_check_deferred_free(context, 0, 0);
  53. #if defined(LWS_WITH_SERVER)
  54. lws_context_lock(context, "periodic checks");
  55. lws_start_foreach_llp(struct lws_vhost **, pv,
  56. context->no_listener_vhost_list) {
  57. struct lws_vhost *v = *pv;
  58. lwsl_debug("deferred iface: checking if on vh %s\n", (*pv)->name);
  59. if (_lws_vhost_init_server(NULL, *pv) == 0) {
  60. /* became happy */
  61. lwsl_notice("vh %s: became connected\n", v->name);
  62. *pv = v->no_listener_vhost_list;
  63. v->no_listener_vhost_list = NULL;
  64. break;
  65. }
  66. } lws_end_foreach_llp(pv, no_listener_vhost_list);
  67. lws_context_unlock(context);
  68. #endif
  69. __lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
  70. &pt->sul_plat, 30 * LWS_US_PER_SEC);
  71. }
  72. #endif
  73. #if defined(LWS_WITH_PLUGINS)
  74. static int
  75. protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
  76. {
  77. struct lws_context *context = (struct lws_context *)each_user;
  78. const lws_plugin_protocol_t *plpr =
  79. (const lws_plugin_protocol_t *)pin->hdr;
  80. context->plugin_protocol_count += plpr->count_protocols;
  81. context->plugin_extension_count += plpr->count_extensions;
  82. return 0;
  83. }
  84. #endif
  85. int
  86. lws_plat_init(struct lws_context *context,
  87. const struct lws_context_creation_info *info)
  88. {
  89. int fd;
  90. #if defined(LWS_WITH_NETWORK)
  91. /*
  92. * master context has the process-global fd lookup array. This can be
  93. * done two different ways now; one or the other is done depending on if
  94. * info->fd_limit_per_thread was snonzero
  95. *
  96. * - default: allocate a worst-case lookup array sized for ulimit -n
  97. * and use the fd directly as an index into it
  98. *
  99. * - slow: allocate context->max_fds entries only (which can be
  100. * forced at context creation time to be
  101. * info->fd_limit_per_thread * the number of threads)
  102. * and search the array to lookup fds
  103. *
  104. * the default way is optimized for server, if you only use one or two
  105. * client wsi the slow way may save a lot of memory.
  106. *
  107. * Both ways allocate an array of struct lws *... one allocates it for
  108. * all possible fd indexes the process could produce and uses it as a
  109. * map, the other allocates for an amount of wsi the lws context is
  110. * expected to use and searches through it to manipulate it.
  111. */
  112. context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
  113. context->max_fds, "lws_lookup");
  114. if (!context->lws_lookup) {
  115. lwsl_err("%s: OOM on alloc lws_lookup array for %d conn\n",
  116. __func__, context->max_fds);
  117. return 1;
  118. }
  119. lwsl_info(" mem: platform fd map: %5lu B\n",
  120. (unsigned long)(sizeof(struct lws *) * context->max_fds));
  121. #endif
  122. #if defined(LWS_WITH_FILE_OPS)
  123. fd = lws_open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
  124. #else
  125. fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
  126. #endif
  127. context->fd_random = fd;
  128. if (context->fd_random < 0) {
  129. lwsl_err("Unable to open random device %s %d, errno %d\n",
  130. SYSTEM_RANDOM_FILEPATH, context->fd_random, errno);
  131. return 1;
  132. }
  133. #if defined(LWS_WITH_PLUGINS)
  134. if (info->plugin_dirs)
  135. lws_plugins_init(&context->plugin_list, info->plugin_dirs,
  136. "lws_protocol_plugin", NULL,
  137. protocol_plugin_cb, context);
  138. #endif
  139. #if defined(LWS_WITH_NETWORK)
  140. /* we only need to do this on pt[0] */
  141. context->pt[0].sul_plat.cb = lws_sul_plat_unix;
  142. __lws_sul_insert_us(&context->pt[0].pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
  143. &context->pt[0].sul_plat, 30 * LWS_US_PER_SEC);
  144. #endif
  145. return 0;
  146. }
  147. int
  148. lws_plat_context_early_init(void)
  149. {
  150. #if !defined(LWS_AVOID_SIGPIPE_IGN)
  151. signal(SIGPIPE, SIG_IGN);
  152. #endif
  153. return 0;
  154. }
  155. void
  156. lws_plat_context_early_destroy(struct lws_context *context)
  157. {
  158. }
  159. void
  160. lws_plat_context_late_destroy(struct lws_context *context)
  161. {
  162. #if defined(LWS_WITH_PLUGINS)
  163. if (context->plugin_list)
  164. lws_plugins_destroy(&context->plugin_list, NULL, NULL);
  165. #endif
  166. #if defined(LWS_WITH_NETWORK)
  167. if (context->lws_lookup)
  168. lws_free_set_NULL(context->lws_lookup);
  169. #endif
  170. if (!context->fd_random)
  171. lwsl_err("ZERO RANDOM FD\n");
  172. if (context->fd_random != LWS_INVALID_FILE)
  173. close(context->fd_random);
  174. }