unix-pipe.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  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. #define _GNU_SOURCE
  22. #include "core/private.h"
  23. int
  24. lws_plat_pipe_create(struct lws *wsi)
  25. {
  26. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  27. #if defined(LWS_HAVE_PIPE2)
  28. return pipe2(pt->dummy_pipe_fds, O_NONBLOCK);
  29. #else
  30. return pipe(pt->dummy_pipe_fds);
  31. #endif
  32. }
  33. int
  34. lws_plat_pipe_signal(struct lws *wsi)
  35. {
  36. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  37. char buf = 0;
  38. int n;
  39. n = write(pt->dummy_pipe_fds[1], &buf, 1);
  40. return n != 1;
  41. }
  42. void
  43. lws_plat_pipe_close(struct lws *wsi)
  44. {
  45. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  46. if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != -1)
  47. close(pt->dummy_pipe_fds[0]);
  48. if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != -1)
  49. close(pt->dummy_pipe_fds[1]);
  50. pt->dummy_pipe_fds[0] = pt->dummy_pipe_fds[1] = -1;
  51. }