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
rops_handle_POLLIN_pipe(struct lws_context_per_thread * pt,struct lws * wsi,struct lws_pollfd * pollfd)28 rops_handle_POLLIN_pipe(struct lws_context_per_thread *pt, struct lws *wsi,
29 			struct lws_pollfd *pollfd)
30 {
31 #if defined(LWS_HAVE_EVENTFD)
32 	eventfd_t value;
33 	if (eventfd_read(wsi->desc.sockfd, &value) < 0)
34 		return LWS_HPI_RET_PLEASE_CLOSE_ME;
35 #elif !defined(WIN32) && !defined(_WIN32)
36 	char s[100];
37 	int n;
38 
39 	/*
40 	 * discard the byte(s) that signaled us
41 	 * We really don't care about the number of bytes, but coverity
42 	 * thinks we should.
43 	 */
44 	n = read(wsi->desc.sockfd, s, sizeof(s));
45 	(void)n;
46 	if (n < 0)
47 		return LWS_HPI_RET_PLEASE_CLOSE_ME;
48 #endif
49 
50 #if defined(LWS_WITH_THREADPOOL)
51 	/*
52 	 * threadpools that need to call for on_writable callbacks do it by
53 	 * marking the task as needing one for its wsi, then cancelling service.
54 	 *
55 	 * Each tsi will call this to perform the actual callback_on_writable
56 	 * from the correct service thread context
57 	 */
58 	lws_threadpool_tsi_context(pt->context, pt->tid);
59 #endif
60 
61 	/*
62 	 * the poll() wait, or the event loop for libuv etc is a
63 	 * process-wide resource that we interrupted.  So let every
64 	 * protocol that may be interested in the pipe event know that
65 	 * it happened.
66 	 */
67 	if (lws_broadcast(pt, LWS_CALLBACK_EVENT_WAIT_CANCELLED, NULL, 0)) {
68 		lwsl_info("closed in event cancel\n");
69 		return LWS_HPI_RET_PLEASE_CLOSE_ME;
70 	}
71 
72 	return LWS_HPI_RET_HANDLED;
73 }
74 
75 const struct lws_role_ops role_ops_pipe = {
76 	/* role name */			"pipe",
77 	/* alpn id */			NULL,
78 	/* check_upgrades */		NULL,
79 	/* pt_init_destroy */		NULL,
80 	/* init_vhost */		NULL,
81 	/* destroy_vhost */		NULL,
82 	/* service_flag_pending */	NULL,
83 	/* handle_POLLIN */		rops_handle_POLLIN_pipe,
84 	/* handle_POLLOUT */		NULL,
85 	/* perform_user_POLLOUT */	NULL,
86 	/* callback_on_writable */	NULL,
87 	/* tx_credit */			NULL,
88 	/* write_role_protocol */	NULL,
89 	/* encapsulation_parent */	NULL,
90 	/* alpn_negotiated */		NULL,
91 	/* close_via_role_protocol */	NULL,
92 	/* close_role */		NULL,
93 	/* close_kill_connection */	NULL,
94 	/* destroy_role */		NULL,
95 	/* adoption_bind */		NULL,
96 	/* client_bind */		NULL,
97 	/* issue_keepalive */		NULL,
98 	/* adoption_cb clnt, srv */	{ 0, 0 },
99 	/* rx_cb clnt, srv */		{ 0, 0 },
100 	/* writeable cb clnt, srv */	{ 0, 0 },
101 	/* close cb clnt, srv */	{ 0, 0 },
102 	/* protocol_bind_cb c,s */	{ 0, 0 },
103 	/* protocol_unbind_cb c,s */	{ 0, 0 },
104 	/* file_handle */		1,
105 };
106