Coverage Report

Created: 2024-09-08 06:10

/src/libwebsockets/lib/roles/pipe/ops-pipe.c
Line
Count
Source (jump to first uncovered line)
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 int
28
rops_handle_POLLIN_pipe(struct lws_context_per_thread *pt, struct lws *wsi,
29
      struct lws_pollfd *pollfd)
30
0
{
31
0
#if defined(LWS_HAVE_EVENTFD)
32
0
  eventfd_t value;
33
0
  int n;
34
35
0
  n = eventfd_read(wsi->desc.sockfd, &value);
36
0
  if (n < 0) {
37
0
    lwsl_notice("%s: eventfd read %d bailed errno %d\n", __func__,
38
0
        wsi->desc.sockfd, LWS_ERRNO);
39
0
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
40
0
  }
41
#elif !defined(WIN32) && !defined(_WIN32)
42
  char s[100];
43
  int n;
44
45
  /*
46
   * discard the byte(s) that signaled us
47
   * We really don't care about the number of bytes, but coverity
48
   * thinks we should.
49
   */
50
  n = (int)read(wsi->desc.sockfd, s, sizeof(s));
51
  (void)n;
52
  if (n < 0)
53
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
54
#elif defined(WIN32)
55
  char s[100];
56
  int n;
57
58
  n = recv(wsi->desc.sockfd, s, sizeof(s), 0);
59
  if (n == SOCKET_ERROR)
60
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
61
#endif
62
63
#if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H)
64
  /*
65
   * threadpools that need to call for on_writable callbacks do it by
66
   * marking the task as needing one for its wsi, then cancelling service.
67
   *
68
   * Each tsi will call this to perform the actual callback_on_writable
69
   * from the correct service thread context
70
   */
71
  lws_threadpool_tsi_context(pt->context, pt->tid);
72
#endif
73
74
#if LWS_MAX_SMP > 1
75
76
  /*
77
   * Other pts need to take care of their own wsi bound to a vhost that
78
   * is going down
79
   */
80
81
  if (pt->context->owner_vh_being_destroyed.head) {
82
83
    lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
84
              pt->context->owner_vh_being_destroyed.head) {
85
      struct lws_vhost *v =
86
        lws_container_of(d, struct lws_vhost,
87
             vh_being_destroyed_list);
88
89
      lws_vhost_lock(v); /* -------------- vh { */
90
      __lws_vhost_destroy_pt_wsi_dieback_start(v);
91
      lws_vhost_unlock(v); /* } vh -------------- */
92
93
    } lws_end_foreach_dll_safe(d, d1);
94
  }
95
96
#endif
97
98
0
#if defined(LWS_WITH_SECURE_STREAMS)
99
0
  lws_dll2_foreach_safe(&pt->ss_owner, NULL, lws_ss_cancel_notify_dll);
100
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) && defined(LWS_WITH_CLIENT)
101
  lws_dll2_foreach_safe(&pt->ss_client_owner, NULL, lws_sspc_cancel_notify_dll);
102
#endif
103
0
#endif
104
105
  /*
106
   * the poll() wait, or the event loop for libuv etc is a
107
   * process-wide resource that we interrupted.  So let every
108
   * protocol that may be interested in the pipe event know that
109
   * it happened.
110
   */
111
0
  if (lws_broadcast(pt, LWS_CALLBACK_EVENT_WAIT_CANCELLED, NULL, 0)) {
112
0
    lwsl_info("closed in event cancel\n");
113
0
    return LWS_HPI_RET_PLEASE_CLOSE_ME;
114
0
  }
115
116
0
  return LWS_HPI_RET_HANDLED;
117
0
}
118
119
static const lws_rops_t rops_table_pipe[] = {
120
  /*  1 */ { .handle_POLLIN = rops_handle_POLLIN_pipe },
121
};
122
123
124
const struct lws_role_ops role_ops_pipe = {
125
  /* role name */     "pipe",
126
  /* alpn id */     NULL,
127
128
  /* rops_table */    rops_table_pipe,
129
  /* rops_idx */      {
130
    /* LWS_ROPS_check_upgrades */
131
    /* LWS_ROPS_pt_init_destroy */    0x00,
132
    /* LWS_ROPS_init_vhost */
133
    /* LWS_ROPS_destroy_vhost */      0x00,
134
    /* LWS_ROPS_service_flag_pending */
135
    /* LWS_ROPS_handle_POLLIN */      0x01,
136
    /* LWS_ROPS_handle_POLLOUT */
137
    /* LWS_ROPS_perform_user_POLLOUT */   0x00,
138
    /* LWS_ROPS_callback_on_writable */
139
    /* LWS_ROPS_tx_credit */      0x00,
140
    /* LWS_ROPS_write_role_protocol */
141
    /* LWS_ROPS_encapsulation_parent */   0x00,
142
    /* LWS_ROPS_alpn_negotiated */
143
    /* LWS_ROPS_close_via_role_protocol */  0x00,
144
    /* LWS_ROPS_close_role */
145
    /* LWS_ROPS_close_kill_connection */    0x00,
146
    /* LWS_ROPS_destroy_role */
147
    /* LWS_ROPS_adoption_bind */      0x00,
148
    /* LWS_ROPS_client_bind */
149
    /* LWS_ROPS_issue_keepalive */    0x00,
150
          },
151
152
  /* adoption_cb clnt, srv */ { 0, 0 },
153
  /* rx_cb clnt, srv */   { 0, 0 },
154
  /* writeable cb clnt, srv */  { 0, 0 },
155
  /* close cb clnt, srv */  { 0, 0 },
156
  /* protocol_bind_cb c,s */  { 0, 0 },
157
  /* protocol_unbind_cb c,s */  { 0, 0 },
158
#if defined(WIN32)
159
  /* file_handle (no, UDP) */ 0,
160
#else
161
  /* file_handle */   1,
162
#endif
163
};