context deprecation
[platform/upstream/libwebsockets.git] / lib / pollfd.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 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 int
25 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa)
26 {
27         struct lws_context_per_thread *pt;
28         struct lws_context *context;
29         int ret = 0, pa_events = 1;
30         struct lws_pollfd *pfd;
31         int sampled_tid, tid;
32
33         if (!wsi || wsi->position_in_fds_table < 0)
34                 return 0;
35
36         context = wsi->context;
37         pt = &context->pt[(int)wsi->tsi];
38         assert(wsi->position_in_fds_table >= 0 &&
39                wsi->position_in_fds_table < pt->fds_count);
40
41         pfd = &pt->fds[wsi->position_in_fds_table];
42         pa->fd = wsi->sock;
43         pa->prev_events = pfd->events;
44         pa->events = pfd->events = (pfd->events & ~_and) | _or;
45
46         //lwsl_notice("%s: wsi %p, posin %d. from %d -> %d\n", __func__, wsi, wsi->position_in_fds_table, pa->prev_events, pa->events);
47
48
49         if (wsi->http2_substream)
50                 return 0;
51
52         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CHANGE_MODE_POLL_FD,
53                                            wsi->user_space, (void *)pa, 0)) {
54                 ret = -1;
55                 goto bail;
56         }
57
58         if (_and & LWS_POLLIN) {
59                 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ);
60                 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ);
61         }
62         if (_or & LWS_POLLIN) {
63                 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
64                 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
65         }
66         if (_and & LWS_POLLOUT) {
67                 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
68                 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
69         }
70         if (_or & LWS_POLLOUT) {
71                 lws_libev_io(wsi, LWS_EV_START | LWS_EV_WRITE);
72                 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_WRITE);
73         }
74
75         /*
76          * if we changed something in this pollfd...
77          *   ... and we're running in a different thread context
78          *     than the service thread...
79          *       ... and the service thread is waiting ...
80          *         then cancel it to force a restart with our changed events
81          */
82 #if LWS_POSIX
83         pa_events = pa->prev_events != pa->events;
84 #endif
85
86         if (pa_events) {
87
88                 if (lws_plat_change_pollfd(context, wsi, pfd)) {
89                         lwsl_info("%s failed\n", __func__);
90                         ret = -1;
91                         goto bail;
92                 }
93
94                 sampled_tid = context->service_tid;
95                 if (sampled_tid) {
96                         tid = wsi->vhost->protocols[0].callback(wsi,
97                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
98                         if (tid == -1) {
99                                 ret = -1;
100                                 goto bail;
101                         }
102                         if (tid != sampled_tid)
103                                 lws_cancel_service_pt(wsi);
104                 }
105         }
106 bail:
107         return ret;
108 }
109
110 #ifndef LWS_NO_SERVER
111 static void
112 lws_accept_modulation(struct lws_context_per_thread *pt, int allow)
113 {
114 // multithread listen seems broken
115 #if 0
116         struct lws_vhost *vh = context->vhost_list;
117         struct lws_pollargs pa1;
118
119         while (vh) {
120                 if (allow)
121                         _lws_change_pollfd(pt->wsi_listening,
122                                            0, LWS_POLLIN, &pa1);
123                 else
124                         _lws_change_pollfd(pt->wsi_listening,
125                                            LWS_POLLIN, 0, &pa1);
126                 vh = vh->vhost_next;
127         }
128 #endif
129 }
130 #endif
131
132 int
133 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
134 {
135         struct lws_pollargs pa = { wsi->sock, LWS_POLLIN, 0 };
136         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
137         int ret = 0;
138
139
140         lwsl_debug("%s: %p: tsi=%d, sock=%d, pos-in-fds=%d\n",
141                   __func__, wsi, wsi->tsi, wsi->sock, pt->fds_count);
142
143         if ((unsigned int)pt->fds_count >= context->fd_limit_per_thread) {
144                 lwsl_err("Too many fds (%d vs %d)\n", context->max_fds,
145                                 context->fd_limit_per_thread    );
146                 return 1;
147         }
148
149 #if !defined(_WIN32) && !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
150         if (wsi->sock >= context->max_fds) {
151                 lwsl_err("Socket fd %d is too high (%d)\n",
152                          wsi->sock, context->max_fds);
153                 return 1;
154         }
155 #endif
156
157         assert(wsi);
158         assert(wsi->vhost);
159         assert(lws_socket_is_valid(wsi->sock));
160
161         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
162                                            wsi->user_space, (void *) &pa, 1))
163                 return -1;
164
165         lws_pt_lock(pt);
166         pt->count_conns++;
167         insert_wsi(context, wsi);
168 #if defined(LWS_WITH_ESP8266)
169         if (wsi->position_in_fds_table == -1)
170 #endif
171                 wsi->position_in_fds_table = pt->fds_count;
172
173         // lwsl_notice("%s: %p: setting posinfds %d\n", __func__, wsi, wsi->position_in_fds_table);
174
175         pt->fds[wsi->position_in_fds_table].fd = wsi->sock;
176 #if LWS_POSIX
177         pt->fds[wsi->position_in_fds_table].events = LWS_POLLIN;
178 #else
179         pt->fds[wsi->position_in_fds_table].events = 0; // LWS_POLLIN;
180 #endif
181         pa.events = pt->fds[pt->fds_count].events;
182
183         lws_plat_insert_socket_into_fds(context, wsi);
184
185         /* external POLL support via protocol 0 */
186         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_ADD_POLL_FD,
187                                            wsi->user_space, (void *) &pa, 0))
188                 ret =  -1;
189 #ifndef LWS_NO_SERVER
190         /* if no more room, defeat accepts on this thread */
191         if ((unsigned int)pt->fds_count == context->fd_limit_per_thread - 1)
192                 lws_accept_modulation(pt, 0);
193 #endif
194         lws_pt_unlock(pt);
195
196         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
197                                            wsi->user_space, (void *)&pa, 1))
198                 ret = -1;
199
200         return ret;
201 }
202
203 int
204 remove_wsi_socket_from_fds(struct lws *wsi)
205 {
206         struct lws_context *context = wsi->context;
207         struct lws_pollargs pa = { wsi->sock, 0, 0 };
208 #if !defined(LWS_WITH_ESP8266)
209         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
210         struct lws *end_wsi;
211         int v;
212 #endif
213         int m, ret = 0;
214
215 #if !defined(_WIN32) && !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
216         if (wsi->sock > context->max_fds) {
217                 lwsl_err("fd %d too high (%d)\n", wsi->sock, context->max_fds);
218                 return 1;
219         }
220 #endif
221
222         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
223                                            wsi->user_space, (void *)&pa, 1))
224                 return -1;
225
226         /*
227          * detach ourselves from vh protocol list if we're on one
228          * A -> B -> C
229          * A -> C , or, B -> C, or A -> B
230          */
231         lwsl_info("%s: removing same prot wsi %p\n", __func__, wsi);
232         if (wsi->same_vh_protocol_prev) {
233                 assert (*(wsi->same_vh_protocol_prev) == wsi);
234                 lwsl_info("have prev %p, setting him to our next %p\n",
235                          wsi->same_vh_protocol_prev,
236                          wsi->same_vh_protocol_next);
237
238                 /* guy who pointed to us should point to our next */
239                 *(wsi->same_vh_protocol_prev) = wsi->same_vh_protocol_next;
240         } //else
241                 //lwsl_err("null wsi->prev\n");
242         /* our next should point back to our prev */
243         if (wsi->same_vh_protocol_next) {
244                 lwsl_info("have next %p\n");
245                 wsi->same_vh_protocol_next->same_vh_protocol_prev =
246                                 wsi->same_vh_protocol_prev;
247         } //else
248                 //lwsl_err("null wsi->next\n");
249
250         wsi->same_vh_protocol_prev = NULL;
251         wsi->same_vh_protocol_next = NULL;
252
253         /* the guy who is to be deleted's slot index in pt->fds */
254         m = wsi->position_in_fds_table;
255         
256 #if !defined(LWS_WITH_ESP8266)
257         lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE | LWS_EV_PREPARE_DELETION);
258         lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE | LWS_EV_PREPARE_DELETION);
259
260         lws_pt_lock(pt);
261
262         lwsl_debug("%s: wsi=%p, sock=%d, fds pos=%d, end guy pos=%d, endfd=%d\n",
263                   __func__, wsi, wsi->sock, wsi->position_in_fds_table,
264                   pt->fds_count, pt->fds[pt->fds_count].fd);
265
266         /* have the last guy take up the now vacant slot */
267         pt->fds[m] = pt->fds[pt->fds_count - 1];
268 #endif
269         /* this decrements pt->fds_count */
270         lws_plat_delete_socket_from_fds(context, wsi, m);
271 #if !defined(LWS_WITH_ESP8266)
272         v = (int) pt->fds[m].fd;
273         /* end guy's "position in fds table" is now the deletion guy's old one */
274         end_wsi = wsi_from_fd(context, v);
275         if (!end_wsi) {
276                 lwsl_err("no wsi found for sock fd %d at pos %d, pt->fds_count=%d\n", (int)pt->fds[m].fd, m, pt->fds_count);
277                 assert(0);
278         } else
279                 end_wsi->position_in_fds_table = m;
280
281         /* deletion guy's lws_lookup entry needs nuking */
282         delete_from_fd(context, wsi->sock);
283         /* removed wsi has no position any more */
284         wsi->position_in_fds_table = -1;
285
286         /* remove also from external POLL support via protocol 0 */
287         if (lws_socket_is_valid(wsi->sock))
288                 if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_DEL_POLL_FD,
289                                                    wsi->user_space, (void *) &pa, 0))
290                         ret = -1;
291 #ifndef LWS_NO_SERVER
292         if (!context->being_destroyed)
293                 /* if this made some room, accept connects on this thread */
294                 if ((unsigned int)pt->fds_count < context->fd_limit_per_thread - 1)
295                         lws_accept_modulation(pt, 1);
296 #endif
297         lws_pt_unlock(pt);
298
299         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
300                                            wsi->user_space, (void *) &pa, 1))
301                 ret = -1;
302 #endif
303         return ret;
304 }
305
306 int
307 lws_change_pollfd(struct lws *wsi, int _and, int _or)
308 {
309         struct lws_context_per_thread *pt;
310         struct lws_context *context;
311         struct lws_pollargs pa;
312         int ret = 0;
313
314         if (!wsi || !wsi->protocol || wsi->position_in_fds_table < 0)
315                 return 1;
316
317         context = lws_get_context(wsi);
318         if (!context)
319                 return 1;
320
321         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
322                                            wsi->user_space,  (void *) &pa, 0))
323                 return -1;
324
325         pt = &context->pt[(int)wsi->tsi];
326
327         lws_pt_lock(pt);
328         ret = _lws_change_pollfd(wsi, _and, _or, &pa);
329         lws_pt_unlock(pt);
330         if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_UNLOCK_POLL,
331                                            wsi->user_space, (void *) &pa, 0))
332                 ret = -1;
333
334         return ret;
335 }
336
337 LWS_VISIBLE int
338 lws_callback_on_writable(struct lws *wsi)
339 {
340 #ifdef LWS_USE_HTTP2
341         struct lws *network_wsi, *wsi2;
342         int already;
343 #endif
344
345         if (wsi->state == LWSS_SHUTDOWN)
346                 return 0;
347
348         if (wsi->socket_is_permanently_unusable)
349                 return 0;
350
351 #ifdef LWS_USE_HTTP2
352         lwsl_info("%s: %p\n", __func__, wsi);
353
354         if (wsi->mode != LWSCM_HTTP2_SERVING)
355                 goto network_sock;
356
357         if (wsi->u.http2.requested_POLLOUT) {
358                 lwsl_info("already pending writable\n");
359                 return 1;
360         }
361
362         if (wsi->u.http2.tx_credit <= 0) {
363                 /*
364                  * other side is not able to cope with us sending
365                  * anything so no matter if we have POLLOUT on our side.
366                  *
367                  * Delay waiting for our POLLOUT until peer indicates he has
368                  * space for more using tx window command in http2 layer
369                  */
370                 lwsl_info("%s: %p: waiting_tx_credit (%d)\n", __func__, wsi,
371                           wsi->u.http2.tx_credit);
372                 wsi->u.http2.waiting_tx_credit = 1;
373                 return 0;
374         }
375
376         network_wsi = lws_http2_get_network_wsi(wsi);
377         already = network_wsi->u.http2.requested_POLLOUT;
378
379         /* mark everybody above him as requesting pollout */
380
381         wsi2 = wsi;
382         while (wsi2) {
383                 wsi2->u.http2.requested_POLLOUT = 1;
384                 lwsl_info("mark %p pending writable\n", wsi2);
385                 wsi2 = wsi2->u.http2.parent_wsi;
386         }
387
388         /* for network action, act only on the network wsi */
389
390         wsi = network_wsi;
391         if (already)
392                 return 1;
393 network_sock:
394 #endif
395
396         if (lws_ext_cb_active(wsi, LWS_EXT_CB_REQUEST_ON_WRITEABLE, NULL, 0))
397                 return 1;
398
399         if (wsi->position_in_fds_table < 0) {
400                 lwsl_err("%s: failed to find socket %d\n", __func__, wsi->sock);
401                 return -1;
402         }
403
404         if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
405                 return -1;
406
407         return 1;
408 }
409
410 LWS_VISIBLE int
411 lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
412                                       const struct lws_protocols *protocol)
413 {
414         struct lws *wsi;
415
416         if (protocol < vhost->protocols ||
417             protocol >= (vhost->protocols + vhost->count_protocols)) {
418                 lwsl_err("%s: protocol is not from vhost\n", __func__);
419
420                 return -1;
421         }
422
423         wsi = vhost->same_vh_protocol_list[protocol - vhost->protocols];
424         //lwsl_notice("%s: protocol %p, start wsi %p\n", __func__, protocol, wsi);
425         while (wsi) {
426                 //lwsl_notice("%s: protocol %p, this wsi %p (wsi->protocol=%p)\n",
427                 //              __func__, protocol, wsi, wsi->protocol);
428                 assert(wsi->protocol == protocol);
429                 assert(*wsi->same_vh_protocol_prev == wsi);
430                 if (wsi->same_vh_protocol_next) {
431                 //      lwsl_err("my next says %p\n", wsi->same_vh_protocol_next);
432                 //      lwsl_err("my next's prev says %p\n",
433                 //              wsi->same_vh_protocol_next->same_vh_protocol_prev);
434                         assert(wsi->same_vh_protocol_next->same_vh_protocol_prev == &wsi->same_vh_protocol_next);
435                 }
436                 //lwsl_notice("  apv: %p\n", wsi);
437                 lws_callback_on_writable(wsi);
438                 wsi = wsi->same_vh_protocol_next;
439         }
440
441         return 0;
442 }
443
444 LWS_VISIBLE int
445 lws_callback_on_writable_all_protocol(const struct lws_context *context,
446                                       const struct lws_protocols *protocol)
447 {
448         struct lws_vhost *vhost = context->vhost_list;
449         int n;
450
451         while (vhost) {
452                 for (n = 0; n < vhost->count_protocols; n++)
453                         if (protocol->callback ==
454                             vhost->protocols[n].callback &&
455                             !strcmp(protocol->name, vhost->protocols[n].name))
456                                 break;
457                 if (n != vhost->count_protocols)
458                         lws_callback_on_writable_all_protocol_vhost(
459                                 vhost, &vhost->protocols[n]);
460
461                 vhost = vhost->vhost_next;
462         }
463
464         return 0;
465 }