Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / lib / plat / unix / unix-pipe.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2018 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 #define _GNU_SOURCE
23 #include "core/private.h"
24
25
26 int
27 lws_plat_pipe_create(struct lws *wsi)
28 {
29         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
30
31 #if defined(LWS_HAVE_PIPE2)
32         return pipe2(pt->dummy_pipe_fds, O_NONBLOCK);
33 #else
34         return pipe(pt->dummy_pipe_fds);
35 #endif
36 }
37
38 int
39 lws_plat_pipe_signal(struct lws *wsi)
40 {
41         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
42         char buf = 0;
43         int n;
44
45         n = write(pt->dummy_pipe_fds[1], &buf, 1);
46
47         return n != 1;
48 }
49
50 void
51 lws_plat_pipe_close(struct lws *wsi)
52 {
53         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
54
55         if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != -1)
56                 close(pt->dummy_pipe_fds[0]);
57         if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != -1)
58                 close(pt->dummy_pipe_fds[1]);
59
60         pt->dummy_pipe_fds[0] = pt->dummy_pipe_fds[1] = -1;
61 }
62