Stop uv loop in default signal handler, clean-ups
[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 (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_accept_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         if (status < 0)
42                 return;
43
44         eventfd.fd = watcher->io_watcher.fd;
45         eventfd.events = 0;
46         eventfd.revents = 0;
47         if (revents & UV_READABLE) {
48                 eventfd.events |= LWS_POLLIN;
49                 eventfd.revents |= LWS_POLLIN;
50         }
51         if (revents & UV_WRITABLE) {
52                 eventfd.events |= LWS_POLLOUT;
53                 eventfd.revents |= LWS_POLLOUT;
54         }
55         lws_service_fd(context, &eventfd);
56 }
57
58 LWS_VISIBLE void
59 lws_uv_sigint_cb(uv_loop_t *loop, uv_signal_t *watcher, int revents)
60 {
61         uv_stop(loop);
62 }
63
64 LWS_VISIBLE int
65 lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
66                   lws_uv_signal_cb_t *cb)
67 {
68         context->use_ev_sigint = use_uv_sigint;
69         if (cb)
70                 context->lws_uv_sigint_cb = cb;
71         else
72                 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
73
74         return 0;
75 }
76
77 static const int sigs[] = { SIGINT, SIGTERM, SIGSEGV, SIGFPE };
78
79 LWS_VISIBLE int
80 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, uv_signal_cb cb,
81                 int tsi)
82 {
83         struct lws_context_per_thread *pt = &context->pt[tsi];
84         struct lws *wsi = wsi_from_fd(context, pt->lserv_fd);
85         int status = 0, n;
86
87         if (!loop) {
88                 loop = lws_malloc(sizeof(*loop));
89                 uv_loop_init(loop);
90                 pt->ev_loop_foreign = 0;
91         } else
92                 pt->ev_loop_foreign = 1;
93
94         pt->io_loop_uv = loop;
95
96         assert(ARRAY_SIZE(sigs) <= ARRAY_SIZE(pt->signals));
97         for (n = 0; n < ARRAY_SIZE(sigs); n++) {
98                 uv_signal_init(loop, &pt->signals[n]);
99                 uv_signal_start(&pt->signals[n], cb, sigs[n]);
100         }
101
102         /*
103          * Initialize the accept wsi read watcher with the listening socket
104          * and register a callback for read operations
105          *
106          * We have to do it here because the uv loop(s) are not
107          * initialized until after context creation.
108          */
109         if (wsi) {
110                 wsi->w_read.context = context;
111                 uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher, pt->lserv_fd);
112                 uv_poll_start(&wsi->w_read.uv_watcher, UV_READABLE, lws_accept_cb);
113         }
114
115         return status;
116 }
117
118 void lws_uv_close_cb(uv_handle_t *handle)
119 {
120
121 }
122
123 void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
124 {
125         uv_close(handle, lws_uv_close_cb);
126 }
127
128 void
129 lws_libuv_destroyloop(struct lws_context *context, int tsi)
130 {
131         struct lws_context_per_thread *pt = &context->pt[tsi];
132         int m;
133
134         if (!(context->options & LWS_SERVER_OPTION_LIBUV))
135                 return;
136
137         if (!pt->io_loop_uv)
138                 return;
139
140         if (context->use_ev_sigint)
141                 uv_signal_stop(&pt->w_sigint.uv_watcher);
142         for (m = 0; m < ARRAY_SIZE(sigs); m++)
143                 uv_signal_stop(&pt->signals[m]);
144         if (!pt->ev_loop_foreign) {
145                 uv_stop(pt->io_loop_uv);
146                 uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);
147                 while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT));
148                 m = uv_loop_close(pt->io_loop_uv);
149                 if (m == UV_EBUSY)
150                         lwsl_debug("%s: uv_loop_close: UV_EBUSY\n", __func__);
151                 lws_free(pt->io_loop_uv);
152         }
153 }
154
155 void
156 lws_libuv_accept(struct lws *wsi, int accept_fd)
157 {
158         struct lws_context *context = lws_get_context(wsi);
159         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
160
161         if (!LWS_LIBUV_ENABLED(context))
162                 return;
163
164         lwsl_debug("%s: new wsi %p\n", __func__, wsi);
165
166         wsi->w_read.context = context;
167
168         uv_poll_init(pt->io_loop_uv, &wsi->w_read.uv_watcher, accept_fd);
169 }
170
171 void
172 lws_libuv_io(struct lws *wsi, int flags)
173 {
174         struct lws_context *context = lws_get_context(wsi);
175         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
176         int current_events = wsi->w_read.uv_watcher.io_watcher.pevents &
177                              (UV_READABLE | UV_WRITABLE);
178         struct lws_io_watcher *w = &wsi->w_read;
179
180         if (!LWS_LIBUV_ENABLED(context))
181                 return;
182
183         lwsl_debug("%s: wsi: %p, flags:%d\n", __func__, wsi, flags);
184
185         if (!pt->io_loop_uv) {
186                 lwsl_info("%s: no io loop yet\n", __func__);
187                 return;
188         }
189
190         assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
191                (flags & (LWS_EV_READ | LWS_EV_WRITE)));
192
193         if (flags & LWS_EV_START) {
194                 if (flags & LWS_EV_WRITE)
195                         current_events |= UV_WRITABLE;
196
197                 if (flags & LWS_EV_READ)
198                         current_events |= UV_READABLE;
199
200                 uv_poll_start(&w->uv_watcher, current_events, lws_accept_cb);
201         } else {
202                 if (flags & LWS_EV_WRITE)
203                         current_events &= ~UV_WRITABLE;
204
205                 if (flags & LWS_EV_READ)
206                         current_events &= ~UV_READABLE;
207
208                 if (!(current_events & (UV_READABLE | UV_WRITABLE)))
209                         uv_poll_stop(&w->uv_watcher);
210                 else
211                         uv_poll_start(&w->uv_watcher, current_events,
212                                       lws_accept_cb);
213         }
214 }
215
216 int
217 lws_libuv_init_fd_table(struct lws_context *context)
218 {
219         int n;
220
221         if (!LWS_LIBUV_ENABLED(context))
222                 return 0;
223
224         for (n = 0; n < context->count_threads; n++) {
225                 context->pt[n].w_sigint.context = context;
226         }
227
228         return 1;
229 }
230
231 LWS_VISIBLE void
232 lws_libuv_run(const struct lws_context *context, int tsi)
233 {
234         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
235                 uv_run(context->pt[tsi].io_loop_uv, 0);
236 }
237
238 static void
239 lws_libuv_kill(const struct lws_context *context)
240 {
241         int n;
242
243         for (n = 0; n < context->count_threads; n++)
244                 if (context->pt[n].io_loop_uv && LWS_LIBUV_ENABLED(context))
245                         uv_stop(context->pt[n].io_loop_uv);
246 }
247
248 /*
249  * This does not actually stop the event loop.  The reason is we have to pass
250  * libuv handle closures through its event loop.  So this tries to close all
251  * wsi, and set a flag; when all the wsi closures are finalized then we
252  * actually stop the libuv event loops.
253  */
254
255 LWS_VISIBLE void
256 lws_libuv_stop(struct lws_context *context)
257 {
258         struct lws_context_per_thread *pt;
259         int n, m;
260
261         context->requested_kill = 1;
262
263         m = context->count_threads;
264         context->being_destroyed = 1;
265
266         while (m--) {
267                 pt = &context->pt[m];
268
269                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
270                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
271                         if (!wsi)
272                                 continue;
273
274                         lws_close_free_wsi(wsi,
275                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
276                                 /* no protocol close */);
277                         n--;
278                 }
279         }
280
281         if (context->count_wsi_allocated == 0)
282                 lws_libuv_kill(context);
283 }
284
285 LWS_VISIBLE uv_loop_t *
286 lws_uv_getloop(struct lws_context *context, int tsi)
287 {
288         if (context->pt[tsi].io_loop_uv && LWS_LIBUV_ENABLED(context))
289                 return context->pt[tsi].io_loop_uv;
290
291         return NULL;
292 }
293
294 static void
295 lws_libuv_closewsi(uv_handle_t* handle)
296 {
297         struct lws *n = NULL, *wsi = (struct lws *)(((void *)handle) -
298                           (void *)(&n->w_read.uv_watcher));
299         struct lws_context *context = lws_get_context(wsi);
300
301         lws_close_free_wsi_final(wsi);
302
303         if (context->requested_kill && context->count_wsi_allocated == 0)
304                 lws_libuv_kill(context);
305 }
306
307 void
308 lws_libuv_closehandle(struct lws *wsi)
309 {
310         struct lws_context *context = lws_get_context(wsi);
311
312         /* required to defer actual deletion until libuv has processed it */
313
314         uv_close((uv_handle_t*)&wsi->w_read.uv_watcher, lws_libuv_closewsi);
315
316         if (context->requested_kill && context->count_wsi_allocated == 0)
317                 lws_libuv_kill(context);
318 }