Ensure proper cleanup is performed if last websocket is
[platform/upstream/libwebsockets.git] / lib / pollfd.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2014 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 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
26                                                        struct libwebsocket *wsi)
27 {
28         struct libwebsocket_pollargs pa = { wsi->sock, LWS_POLLIN, 0 };
29
30         if (context->fds_count >= context->max_fds) {
31                 lwsl_err("Too many fds (%d)\n", context->max_fds);
32                 return 1;
33         }
34
35 #ifndef _WIN32
36         if (wsi->sock >= context->max_fds) {
37                 lwsl_err("Socket fd %d is too high (%d)\n",
38                                                 wsi->sock, context->max_fds);
39                 return 1;
40         }
41 #endif
42
43         assert(wsi);
44         assert(wsi->sock >= 0);
45
46         lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
47                                             wsi, wsi->sock, context->fds_count);
48
49         context->protocols[0].callback(context, wsi,
50                 LWS_CALLBACK_LOCK_POLL,
51                 wsi->user_space, (void *) &pa, 0);
52
53         insert_wsi(context, wsi);
54         wsi->position_in_fds_table = context->fds_count;
55         context->fds[context->fds_count].fd = wsi->sock;
56         context->fds[context->fds_count].events = LWS_POLLIN;
57         
58         lws_plat_insert_socket_into_fds(context, wsi);
59
60         /* external POLL support via protocol 0 */
61         context->protocols[0].callback(context, wsi,
62                 LWS_CALLBACK_ADD_POLL_FD,
63                 wsi->user_space, (void *) &pa, 0);
64
65         context->protocols[0].callback(context, wsi,
66                 LWS_CALLBACK_UNLOCK_POLL,
67                 wsi->user_space, (void *)&pa, 0);
68
69         return 0;
70 }
71
72 int
73 remove_wsi_socket_from_fds(struct libwebsocket_context *context,
74                                                       struct libwebsocket *wsi)
75 {
76         int m;
77         struct libwebsocket_pollargs pa = { wsi->sock, 0, 0 };
78
79         lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
80
81         --context->fds_count;
82
83         if (wsi->sock > context->max_fds) {
84                 lwsl_err("Socket fd %d too high (%d)\n",
85                                                    wsi->sock, context->max_fds);
86                 return 1;
87         }
88
89         lwsl_info("%s: wsi=%p, sock=%d, fds pos=%d\n", __func__,
90                                     wsi, wsi->sock, wsi->position_in_fds_table);
91
92         context->protocols[0].callback(context, wsi,
93                 LWS_CALLBACK_LOCK_POLL,
94                 wsi->user_space, (void *)&pa, 0);
95
96         m = wsi->position_in_fds_table; /* replace the contents for this */
97
98         /* have the last guy take up the vacant slot */
99         context->fds[m] = context->fds[context->fds_count];
100
101         lws_plat_delete_socket_from_fds(context, wsi, m);
102
103         /*
104          * end guy's fds_lookup entry remains unchanged
105          * (still same fd pointing to same wsi)
106          */
107         /* end guy's "position in fds table" changed */
108         wsi_from_fd(context,context->fds[context->fds_count].fd)-> 
109                                         position_in_fds_table = m;
110         /* deletion guy's lws_lookup entry needs nuking */
111         delete_from_fd(context,wsi->sock);
112         /* removed wsi has no position any more */
113         wsi->position_in_fds_table = -1;
114
115         /* remove also from external POLL support via protocol 0 */
116         if (wsi->sock) {
117                 context->protocols[0].callback(context, wsi,
118                     LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
119                     (void *) &pa, 0);
120         }
121         context->protocols[0].callback(context, wsi,
122                                        LWS_CALLBACK_UNLOCK_POLL,
123                                        wsi->user_space, (void *) &pa, 0);
124         return 0;
125 }
126
127 int
128 lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
129 {
130         struct libwebsocket_context *context;
131         int tid;
132         int sampled_tid;
133         struct libwebsocket_pollfd *pfd;
134         struct libwebsocket_pollargs pa;
135
136         if (!wsi || !wsi->protocol || wsi->position_in_fds_table < 0)
137                 return 1;
138         
139         context = wsi->protocol->owning_server;
140         if (!context)
141                 return 1;
142
143         pfd = &context->fds[wsi->position_in_fds_table];
144         pa.fd = wsi->sock;
145
146         context->protocols[0].callback(context, wsi,
147                 LWS_CALLBACK_LOCK_POLL, wsi->user_space,  (void *) &pa, 0);
148
149         pa.prev_events = pfd->events;
150         pa.events = pfd->events = (pfd->events & ~_and) | _or;
151
152         context->protocols[0].callback(context, wsi,
153                         LWS_CALLBACK_CHANGE_MODE_POLL_FD,
154                                 wsi->user_space, (void *) &pa, 0);
155
156         /*
157          * if we changed something in this pollfd...
158          *   ... and we're running in a different thread context
159          *     than the service thread...
160          *       ... and the service thread is waiting ...
161          *         then cancel it to force a restart with our changed events
162          */
163         if (pa.prev_events != pa.events) {
164                 
165                 if (lws_plat_change_pollfd(context, wsi, pfd)) {
166                         lwsl_info("%s failed\n", __func__);
167                         return 1;
168                 }
169
170                 sampled_tid = context->service_tid;
171                 if (sampled_tid) {
172                         tid = context->protocols[0].callback(context, NULL,
173                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
174                         if (tid != sampled_tid)
175                                 libwebsocket_cancel_service(context);
176                 }
177         }
178
179         context->protocols[0].callback(context, wsi,
180                 LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *) &pa, 0);
181         
182         return 0;
183 }
184
185
186 /**
187  * libwebsocket_callback_on_writable() - Request a callback when this socket
188  *                                       becomes able to be written to without
189  *                                       blocking
190  *
191  * @context:    libwebsockets context
192  * @wsi:        Websocket connection instance to get callback for
193  */
194
195 LWS_VISIBLE int
196 libwebsocket_callback_on_writable(struct libwebsocket_context *context,
197                                                       struct libwebsocket *wsi)
198 {
199 #ifdef LWS_USE_HTTP2
200         struct libwebsocket *network_wsi, *wsi2;
201         int already;
202
203         lwsl_info("%s: %p\n", __func__, wsi);
204         
205         if (wsi->mode != LWS_CONNMODE_HTTP2_SERVING)
206                 goto network_sock;
207         
208         if (wsi->u.http2.requested_POLLOUT) {
209                 lwsl_info("already pending writable\n");
210                 return 1;
211         }
212         
213         if (wsi->u.http2.tx_credit <= 0) {
214                 /*
215                  * other side is not able to cope with us sending
216                  * anything so no matter if we have POLLOUT on our side.
217                  * 
218                  * Delay waiting for our POLLOUT until peer indicates he has
219                  * space for more using tx window command in http2 layer
220                  */
221                 lwsl_info("%s: %p: waiting_tx_credit (%d)\n", __func__, wsi, wsi->u.http2.tx_credit);
222                 wsi->u.http2.waiting_tx_credit = 1;
223                 return 0;
224         }
225         
226         network_wsi = lws_http2_get_network_wsi(wsi);
227         already = network_wsi->u.http2.requested_POLLOUT;
228         
229         /* mark everybody above him as requesting pollout */
230         
231         wsi2 = wsi;
232         while (wsi2) {
233                 wsi2->u.http2.requested_POLLOUT = 1;
234                 lwsl_info("mark %p pending writable\n", wsi2);
235                 wsi2 = wsi2->u.http2.parent_wsi;
236         }
237         
238         /* for network action, act only on the network wsi */
239         
240         wsi = network_wsi;
241         if (already)
242                 return 1;
243 network_sock:
244 #endif
245
246         if (lws_ext_callback_for_each_active(wsi,
247                                 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, NULL, 0))
248                 return 1;
249
250         if (wsi->position_in_fds_table < 0) {
251                 lwsl_err("%s: failed to find socket %d\n", __func__, wsi->sock);
252                 return -1;
253         }
254
255         if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
256                 return -1;
257
258         lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_WRITE);
259
260         return 1;
261 }
262
263 /**
264  * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
265  *                      all connections using the given protocol when it
266  *                      becomes possible to write to each socket without
267  *                      blocking in turn.
268  *
269  * @protocol:   Protocol whose connections will get callbacks
270  */
271
272 LWS_VISIBLE int
273 libwebsocket_callback_on_writable_all_protocol(
274                                   const struct libwebsocket_protocols *protocol)
275 {
276         struct libwebsocket_context *context = protocol->owning_server;
277         int n;
278         struct libwebsocket *wsi;
279
280         for (n = 0; n < context->fds_count; n++) {
281                 wsi = wsi_from_fd(context,context->fds[n].fd);
282                 if (!wsi)
283                         continue;
284                 if (wsi->protocol == protocol)
285                         libwebsocket_callback_on_writable(context, wsi);
286         }
287
288         return 0;
289 }