Coverage Report

Created: 2026-08-31 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/roles/pipe/ops-pipe.c
Line
Count
Source
1
/*
2
 * libwebsockets - small server side websockets and web server implementation
3
 *
4
 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to
8
 * deal in the Software without restriction, including without limitation the
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
23
 */
24
25
#include <private-lib-core.h>
26
27
static lws_handling_result_t
28
rops_handle_POLLIN_pipe(struct lws_context_per_thread *pt, struct lws *wsi,
29
      struct lws_pollfd *pollfd)
30
0
{
31
#if defined(LWS_WITH_LATENCY)
32
  lws_usec_t _pipe_start = lws_now_usecs();
33
#endif
34
0
#if defined(LWS_HAVE_EVENTFD)
35
0
  eventfd_t value;
36
0
  int n;
37
38
0
  n = eventfd_read(wsi->desc.sockfd, &value);
39
0
  if (n < 0) {
40
0
    lwsl_notice("%s: eventfd read %d bailed errno %d\n", __func__,
41
0
        wsi->desc.sockfd, LWS_ERRNO);
42
0
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
43
0
  }
44
#elif !defined(WIN32) && !defined(_WIN32)
45
  char s[100];
46
  int n;
47
48
  /*
49
   * discard the byte(s) that signaled us
50
   * We really don't care about the number of bytes, but coverity
51
   * thinks we should.
52
   */
53
  n = (int)read(wsi->desc.sockfd, s, sizeof(s));
54
  (void)n;
55
  /*
56
   * Only treat a real read error as a reason to close the pipe wsi.
57
   * read() returning 0 happens when the context is tearing down (the
58
   * write end is going away); closing the pipe wsi from inside its own
59
   * POLLIN handler during context destroy frees it out from under the
60
   * service loop that is still iterating on it, observed as a segfault
61
   * during lws_context_destroy() on libuv / distro-recommended builds.
62
   * The pipe is owned by the context and is explicitly closed as part
63
   * of normal pt destroy, so it does not need our help here.
64
   */
65
  if (n < 0)
66
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
67
#elif defined(WIN32)
68
  char s[100];
69
  int n;
70
71
  n = recv(wsi->desc.sockfd, s, sizeof(s), 0);
72
  if (n == SOCKET_ERROR)
73
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
74
#endif
75
76
#if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H)
77
  /*
78
   * threadpools that need to call for on_writable callbacks do it by
79
   * marking the task as needing one for its wsi, then cancelling service.
80
   *
81
   * Each tsi will call this to perform the actual callback_on_writable
82
   * from the correct service thread context
83
   */
84
  lws_threadpool_tsi_context(pt->context, pt->tid);
85
#endif
86
87
#if defined(LWS_WITH_ASYNC_QUEUE)
88
  {
89
    struct lws_dll2_owner handled;
90
91
    lws_dll2_owner_clear(&handled);
92
    pthread_mutex_lock(&pt->context->async_worker_mutex);
93
    if (lws_dll2_count(&pt->context->async_worker_finished)) {
94
      lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, lws_dll2_get_head(&pt->context->async_worker_finished)) {
95
        struct lws_async_job *job = lws_container_of(d, struct lws_async_job, list);
96
97
        if (!job->wsi) {
98
          lws_dll2_remove(d);
99
          if (job->type != LWS_AQ_FILE_READ)
100
            lws_free(job); /* file read frees itself */
101
          continue;
102
        }
103
104
        if (job->wsi->tsi == pt->tid) {
105
          job->handled_by_main = 1;
106
          lws_dll2_remove(d);
107
          lws_dll2_add_tail(d, &handled);
108
        }
109
      } lws_end_foreach_dll_safe(d, d1);
110
    }
111
    pthread_mutex_unlock(&pt->context->async_worker_mutex);
112
113
    lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, lws_dll2_get_head(&handled)) {
114
      struct lws_async_job *job = lws_container_of(d, struct lws_async_job, list);
115
116
      lws_dll2_remove(d);
117
      if (job->type == LWS_AQ_FILE_READ) {
118
        lwsi_set_state(job->wsi, LRS_ISSUING_FILE);
119
        lws_callback_on_writable(job->wsi);
120
      }
121
#if defined(LWS_WITH_TLS) && defined(LWS_WITH_SERVER)
122
      else if (job->type == LWS_AQ_SSL_ACCEPT) {
123
        job->wsi->async_worker_job = NULL;
124
125
        if (lws_tls_server_accept_completed(job->wsi, job->u.ssl.status)) {
126
          lws_close_free_wsi(job->wsi, LWS_CLOSE_STATUS_NOSTATUS, "ssl accept failed");
127
        } else if (lwsi_state(job->wsi) != LRS_SSL_ACK_PENDING) {
128
129
          /* restore POLLIN which was stripped before entering async worker queue */
130
          if (lws_change_pollfd(job->wsi, 0, LWS_POLLIN)) {
131
            lws_close_free_wsi(job->wsi, LWS_CLOSE_STATUS_NOSTATUS, "ssl accept pollin failed");
132
          } else {
133
            if (lws_server_socket_service_ssl(job->wsi, job->wsi->desc.sockfd, 0))
134
              lwsl_notice("OOB ssl success path failed\n");
135
136
            /*
137
             * OpenSSL background accept might have slurped the HTTP/2 preface
138
             * into its internal BIO without generating a kernel POLLIN.
139
             * Force a fake POLLIN by adding to the pending list once, but ONLY if
140
             * it actually has decoded bytes. If not, it will spin WANT_READ endlessly.
141
             */
142
            if (lws_ssl_pending(job->wsi)) {
143
              lws_pt_lock(pt, __func__);
144
              if (lws_dll2_is_detached(&job->wsi->tls.dll_pending_tls)) {
145
                lws_dll2_add_head(&job->wsi->tls.dll_pending_tls,
146
                      &pt->tls.dll_pending_tls_owner);
147
                lwsl_notice("ops-pipe added %s to pending tls list, pos=%d\n", lws_wsi_tag(job->wsi), job->wsi->position_in_fds_table);
148
              }
149
              lws_pt_unlock(pt);
150
            }
151
          }
152
        }
153
      }
154
#endif
155
      if (job->type != LWS_AQ_FILE_READ)
156
        lws_free(job);
157
    } lws_end_foreach_dll_safe(d, d1);
158
  }
159
#endif
160
161
#if LWS_MAX_SMP > 1
162
163
  /*
164
   * Other pts need to take care of their own wsi bound to a vhost that
165
   * is going down
166
   */
167
168
  if(!lws_dll2_is_empty(&pt->context->owner_vh_being_destroyed)) {
169
170
    lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
171
              lws_dll2_get_head(&pt->context->owner_vh_being_destroyed)) {
172
      struct lws_vhost *v =
173
        lws_container_of(d, struct lws_vhost,
174
             vh_being_destroyed_list);
175
176
      lws_vhost_lock(v); /* -------------- vh { */
177
      v->count_bound_wsi++;
178
      __lws_vhost_destroy_pt_wsi_dieback_start(v);
179
      v->count_bound_wsi--;
180
      int do_destroy2 = !v->count_bound_wsi && v->being_destroyed;
181
      lws_vhost_unlock(v); /* } vh -------------- */
182
183
      if (do_destroy2)
184
        __lws_vhost_destroy2(v);
185
186
    } lws_end_foreach_dll_safe(d, d1);
187
  }
188
189
#endif
190
191
0
#if defined(LWS_WITH_SECURE_STREAMS)
192
0
  lws_dll2_foreach_safe(&pt->ss_owner, NULL, lws_ss_cancel_notify_dll);
193
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) && defined(LWS_WITH_CLIENT)
194
  lws_dll2_foreach_safe(&pt->ss_client_owner, NULL, lws_sspc_cancel_notify_dll);
195
#endif
196
0
#endif
197
198
  /*
199
   * the poll() wait, or the event loop for libuv etc is a
200
   * process-wide resource that we interrupted.  So let every
201
   * protocol that may be interested in the pipe event know that
202
   * it happened.
203
   */
204
0
  if (lws_broadcast(pt, LWS_CALLBACK_EVENT_WAIT_CANCELLED, NULL, 0)) {
205
0
    lwsl_info("closed in event cancel\n");
206
0
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
207
0
  }
208
209
#if defined(LWS_WITH_LATENCY)
210
  {
211
    unsigned int ms = (unsigned int)((lws_now_usecs() - _pipe_start) / 1000);
212
    if (ms > 2)
213
      lws_latency_note(pt, _pipe_start, 2000, "pipe:%dms", ms);
214
  }
215
#endif
216
217
0
  return LWS_HPI_RET_HANDLED;
218
0
}
219
220
static const lws_rops_t rops_table_pipe[] = {
221
  /*  1 */ { .handle_POLLIN = rops_handle_POLLIN_pipe },
222
};
223
224
225
const struct lws_role_ops role_ops_pipe = {
226
  /* role name */     "pipe",
227
  /* alpn id */     NULL,
228
229
  /* rops_table */    rops_table_pipe,
230
  /* rops_idx */      {
231
    /* LWS_ROPS_check_upgrades */
232
    /* LWS_ROPS_pt_init_destroy */    0x00,
233
    /* LWS_ROPS_init_vhost */
234
    /* LWS_ROPS_destroy_vhost */      0x00,
235
    /* LWS_ROPS_service_flag_pending */
236
    /* LWS_ROPS_handle_POLLIN */      0x01,
237
    /* LWS_ROPS_handle_POLLOUT */
238
    /* LWS_ROPS_perform_user_POLLOUT */   0x00,
239
    /* LWS_ROPS_callback_on_writable */
240
    /* LWS_ROPS_tx_credit */      0x00,
241
    /* LWS_ROPS_write_role_protocol */
242
    /* LWS_ROPS_encapsulation_parent */   0x00,
243
    /* LWS_ROPS_alpn_negotiated */
244
    /* LWS_ROPS_close_via_role_protocol */  0x00,
245
    /* LWS_ROPS_close_role */
246
    /* LWS_ROPS_close_kill_connection */    0x00,
247
    /* LWS_ROPS_destroy_role */
248
    /* LWS_ROPS_adoption_bind */      0x00,
249
    /* LWS_ROPS_client_bind */
250
    /* LWS_ROPS_issue_keepalive */    0x00,
251
          },
252
253
  /* adoption_cb clnt, srv */ { 0, 0 },
254
  /* rx_cb clnt, srv */   { 0, 0 },
255
  /* writeable cb clnt, srv */  { 0, 0 },
256
  /* close cb clnt, srv */  { 0, 0 },
257
  /* protocol_bind_cb c,s */  { 0, 0 },
258
  /* protocol_unbind_cb c,s */  { 0, 0 },
259
#if defined(WIN32)
260
  /* file_handle (no, UDP) */ 0,
261
#else
262
  /* file_handle */   1,
263
#endif
264
};