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