LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT to default to runtime ssl disable
[platform/upstream/libwebsockets.git] / lib / libuv.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
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
22 #include "private-libwebsockets.h"
23
24 void
25 lws_feature_status_libuv(struct lws_context_creation_info *info)
26 {
27         if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBUV))
28                 lwsl_notice("libuv support compiled in and enabled\n");
29         else
30                 lwsl_notice("libuv support compiled in but disabled\n");
31 }
32
33 static void
34 lws_io_cb(uv_poll_t *watcher, int status, int revents)
35 {
36         struct lws_io_watcher *lws_io = container_of(watcher,
37                                         struct lws_io_watcher, uv_watcher);
38         struct lws_context *context = lws_io->context;
39         struct lws_pollfd eventfd;
40
41         eventfd.fd = watcher->io_watcher.fd;
42         eventfd.events = 0;
43         eventfd.revents = 0;
44
45         if (status < 0) {
46                 /* at this point status will be an UV error, like UV_EBADF,
47                 we treat all errors as LWS_POLLHUP */
48
49                 /* you might want to return; instead of servicing the fd in some cases */
50                 if (status == UV_EAGAIN)
51                         return;
52
53                 eventfd.events |= LWS_POLLHUP;
54                 eventfd.revents |= LWS_POLLHUP;
55         } else {
56                 if (revents & UV_READABLE) {
57                         eventfd.events |= LWS_POLLIN;
58                         eventfd.revents |= LWS_POLLIN;
59                 }
60                 if (revents & UV_WRITABLE) {
61                         eventfd.events |= LWS_POLLOUT;
62                         eventfd.revents |= LWS_POLLOUT;
63                 }
64         }
65         lws_service_fd(context, &eventfd);
66 }
67
68 LWS_VISIBLE void
69 lws_uv_sigint_cb(uv_signal_t *watcher, int signum)
70 {
71         lwsl_info("internal signal handler caught signal %d\n", signum);
72         lws_libuv_stop(watcher->data);
73 }
74
75 LWS_VISIBLE int
76 lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
77                   uv_signal_cb cb)
78 {
79         context->use_ev_sigint = use_uv_sigint;
80         if (cb)
81                 context->lws_uv_sigint_cb = cb;
82         else
83                 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
84
85         return 0;
86 }
87
88 static void
89 lws_uv_timeout_cb(uv_timer_t *timer)
90 {
91         struct lws_context_per_thread *pt = container_of(timer,
92                         struct lws_context_per_thread, uv_timeout_watcher);
93
94         lwsl_debug("%s\n", __func__);
95         /* do timeout check only */
96         lws_service_fd_tsi(pt->context, NULL, pt->tid);
97 }
98
99 static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE };
100
101 LWS_VISIBLE int
102 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi)
103 {
104         struct lws_context_per_thread *pt = &context->pt[tsi];
105         struct lws *wsi = wsi_from_fd(context, pt->lserv_fd);
106         int status = 0, n;
107
108         if (!loop) {
109                 loop = lws_malloc(sizeof(*loop));
110                 uv_loop_init(loop);
111                 pt->ev_loop_foreign = 0;
112         } else
113                 pt->ev_loop_foreign = 1;
114
115         pt->io_loop_uv = loop;
116
117         if (pt->context->use_ev_sigint) {
118                 assert(ARRAY_SIZE(sigs) <= ARRAY_SIZE(pt->signals));
119                 for (n = 0; n < ARRAY_SIZE(sigs); n++) {
120                         uv_signal_init(loop, &pt->signals[n]);
121                         pt->signals[n].data = pt->context;
122                         uv_signal_start(&pt->signals[n], context->lws_uv_sigint_cb, sigs[n]);
123                 }
124         }
125
126         /*
127          * Initialize the accept wsi read watcher with the listening socket
128          * and register a callback for read operations
129          *
130          * We have to do it here because the uv loop(s) are not
131          * initialized until after context creation.
132          */
133         if (wsi) {
134                 wsi->w_read.context = context;
135                 uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher,
136                              pt->lserv_fd);
137                 uv_poll_start(&wsi->w_read.uv_watcher, UV_READABLE,
138                               lws_io_cb);
139         }
140
141         uv_timer_init(pt->io_loop_uv, &pt->uv_timeout_watcher);
142         uv_timer_start(&pt->uv_timeout_watcher, lws_uv_timeout_cb, 1000, 1000);
143
144         return status;
145 }
146
147 void lws_uv_close_cb(uv_handle_t *handle)
148 {
149
150 }
151
152 void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
153 {
154         uv_close(handle, lws_uv_close_cb);
155 }
156
157 void
158 lws_libuv_destroyloop(struct lws_context *context, int tsi)
159 {
160         struct lws_context_per_thread *pt = &context->pt[tsi];
161         int m;
162
163         if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
164                 return;
165
166         if (!pt->io_loop_uv)
167                 return;
168
169         if (context->use_ev_sigint)
170                 uv_signal_stop(&pt->w_sigint.uv_watcher);
171         for (m = 0; m < ARRAY_SIZE(sigs); m++)
172                 uv_signal_stop(&pt->signals[m]);
173         if (!pt->ev_loop_foreign) {
174                 uv_stop(pt->io_loop_uv);
175                 uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);
176                 while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT));
177                 m = uv_loop_close(pt->io_loop_uv);
178                 if (m == UV_EBUSY)
179                         lwsl_debug("%s: uv_loop_close: UV_EBUSY\n", __func__);
180                 lws_free(pt->io_loop_uv);
181         }
182 }
183
184 void
185 lws_libuv_accept(struct lws *wsi, int accept_fd)
186 {
187         struct lws_context *context = lws_get_context(wsi);
188         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
189
190         if (!LWS_LIBUV_ENABLED(context))
191                 return;
192
193         lwsl_debug("%s: new wsi %p\n", __func__, wsi);
194
195         wsi->w_read.context = context;
196
197         uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher, accept_fd);
198 }
199
200 void
201 lws_libuv_io(struct lws *wsi, int flags)
202 {
203         struct lws_context *context = lws_get_context(wsi);
204         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
205         int current_events = wsi->w_read.uv_watcher.io_watcher.pevents &
206                              (UV_READABLE | UV_WRITABLE);
207         struct lws_io_watcher *w = &wsi->w_read;
208
209         if (!LWS_LIBUV_ENABLED(context))
210                 return;
211
212         lwsl_debug("%s: wsi: %p, flags:%d\n", __func__, wsi, flags);
213
214         if (!pt->io_loop_uv) {
215                 lwsl_info("%s: no io loop yet\n", __func__);
216                 return;
217         }
218
219         assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
220                (flags & (LWS_EV_READ | LWS_EV_WRITE)));
221
222         if (flags & LWS_EV_START) {
223                 if (flags & LWS_EV_WRITE)
224                         current_events |= UV_WRITABLE;
225
226                 if (flags & LWS_EV_READ)
227                         current_events |= UV_READABLE;
228
229                 uv_poll_start(&w->uv_watcher, current_events, lws_io_cb);
230         } else {
231                 if (flags & LWS_EV_WRITE)
232                         current_events &= ~UV_WRITABLE;
233
234                 if (flags & LWS_EV_READ)
235                         current_events &= ~UV_READABLE;
236
237                 if (!(current_events & (UV_READABLE | UV_WRITABLE)))
238                         uv_poll_stop(&w->uv_watcher);
239                 else
240                         uv_poll_start(&w->uv_watcher, current_events,
241                                       lws_io_cb);
242         }
243 }
244
245 int
246 lws_libuv_init_fd_table(struct lws_context *context)
247 {
248         int n;
249
250         if (!LWS_LIBUV_ENABLED(context))
251                 return 0;
252
253         for (n = 0; n < context->count_threads; n++) {
254                 context->pt[n].w_sigint.context = context;
255         }
256
257         return 1;
258 }
259
260 LWS_VISIBLE void
261 lws_libuv_run(const struct lws_context *context, int tsi)
262 {
263         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
264                 uv_run(context->pt[tsi].io_loop_uv, 0);
265 }
266
267 static void
268 lws_libuv_kill(const struct lws_context *context)
269 {
270         int n;
271
272         for (n = 0; n < context->count_threads; n++)
273                 if (context->pt[n].io_loop_uv && LWS_LIBUV_ENABLED(context))
274                         uv_stop(context->pt[n].io_loop_uv);
275         // TODO uv_stop check foreign loop? or not?
276 }
277
278 /*
279  * This does not actually stop the event loop.  The reason is we have to pass
280  * libuv handle closures through its event loop.  So this tries to close all
281  * wsi, and set a flag; when all the wsi closures are finalized then we
282  * actually stop the libuv event loops.
283  */
284
285 LWS_VISIBLE void
286 lws_libuv_stop(struct lws_context *context)
287 {
288         struct lws_context_per_thread *pt;
289         int n, m;
290
291         context->requested_kill = 1;
292
293         m = context->count_threads;
294         context->being_destroyed = 1;
295
296         while (m--) {
297                 pt = &context->pt[m];
298
299                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
300                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
301                         if (!wsi)
302                                 continue;
303
304                         lws_close_free_wsi(wsi,
305                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
306                                 /* no protocol close */);
307                         n--;
308                 }
309         }
310
311         if (context->count_wsi_allocated == 0)
312                 lws_libuv_kill(context);
313 }
314
315 LWS_VISIBLE uv_loop_t *
316 lws_uv_getloop(struct lws_context *context, int tsi)
317 {
318         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
319                 return context->pt[tsi].io_loop_uv;
320
321         return NULL;
322 }
323
324 static void
325 lws_libuv_closewsi(uv_handle_t* handle)
326 {
327         struct lws *n = NULL, *wsi = (struct lws *)(((void *)handle) -
328                           (void *)(&n->w_read.uv_watcher));
329         struct lws_context *context = lws_get_context(wsi);
330
331         lws_close_free_wsi_final(wsi);
332
333         if (context->requested_kill && context->count_wsi_allocated == 0)
334                 lws_libuv_kill(context);
335 }
336
337 void
338 lws_libuv_closehandle(struct lws *wsi)
339 {
340         struct lws_context *context = lws_get_context(wsi);
341
342         /* required to defer actual deletion until libuv has processed it */
343
344         uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
345
346         if (context->requested_kill && context->count_wsi_allocated == 0)
347                 lws_libuv_kill(context);
348 }