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