usrsock_close.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /****************************************************************************
  2. * net/usrsock/usrsock_close.c
  3. *
  4. * Copyright (C) 2015, 2017 Haltian Ltd. All rights reserved.
  5. * Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #if defined(CONFIG_NET) && defined(CONFIG_NET_USRSOCK)
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <arch/irq.h>
  46. #include <sys/socket.h>
  47. #include <nuttx/semaphore.h>
  48. #include <nuttx/net/net.h>
  49. #include <nuttx/net/usrsock.h>
  50. #include "usrsock/usrsock.h"
  51. /****************************************************************************
  52. * Private Functions
  53. ****************************************************************************/
  54. static uint16_t close_event(FAR struct net_driver_s *dev, FAR void *pvconn,
  55. FAR void *pvpriv, uint16_t flags)
  56. {
  57. FAR struct usrsock_reqstate_s *pstate = pvpriv;
  58. FAR struct usrsock_conn_s *conn = pvconn;
  59. if (flags & USRSOCK_EVENT_ABORT)
  60. {
  61. ninfo("socket aborted.\n");
  62. conn->state = USRSOCK_CONN_STATE_ABORTED;
  63. pstate->result = -ECONNABORTED;
  64. /* Stop further callbacks */
  65. pstate->cb->flags = 0;
  66. pstate->cb->priv = NULL;
  67. pstate->cb->event = NULL;
  68. /* Wake up the waiting thread */
  69. nxsem_post(&pstate->recvsem);
  70. }
  71. else if (flags & USRSOCK_EVENT_REQ_COMPLETE)
  72. {
  73. ninfo("request completed.\n");
  74. pstate->result = conn->resp.result;
  75. /* Stop further callbacks */
  76. pstate->cb->flags = 0;
  77. pstate->cb->priv = NULL;
  78. pstate->cb->event = NULL;
  79. /* Wake up the waiting thread */
  80. nxsem_post(&pstate->recvsem);
  81. }
  82. return flags;
  83. }
  84. /****************************************************************************
  85. * Name: do_close_request
  86. ****************************************************************************/
  87. static int do_close_request(FAR struct usrsock_conn_s *conn)
  88. {
  89. struct usrsock_request_close_s req = {};
  90. struct iovec bufs[1];
  91. /* Prepare request for daemon to read. */
  92. req.head.reqid = USRSOCK_REQUEST_CLOSE;
  93. req.usockid = conn->usockid;
  94. bufs[0].iov_base = (FAR void *)&req;
  95. bufs[0].iov_len = sizeof(req);
  96. return usrsockdev_do_request(conn, bufs, ARRAY_SIZE(bufs));
  97. }
  98. /****************************************************************************
  99. * Public Functions
  100. ****************************************************************************/
  101. /****************************************************************************
  102. * Name: usrsock_close
  103. *
  104. * Description:
  105. *
  106. ****************************************************************************/
  107. int usrsock_close(FAR struct usrsock_conn_s *conn)
  108. {
  109. struct usrsock_reqstate_s state = {};
  110. int ret;
  111. net_lock();
  112. if (conn->state == USRSOCK_CONN_STATE_UNINITIALIZED ||
  113. conn->state == USRSOCK_CONN_STATE_ABORTED)
  114. {
  115. /* Already closed? */
  116. ninfo("usockid=%d; already closed.\n", conn->usockid);
  117. ret = OK;
  118. goto close_out;
  119. }
  120. /* Set up event callback for usrsock. */
  121. ret = usrsock_setup_request_callback(conn, &state, close_event,
  122. USRSOCK_EVENT_ABORT |
  123. USRSOCK_EVENT_REQ_COMPLETE);
  124. if (ret < 0)
  125. {
  126. nwarn("usrsock_setup_request_callback failed: %d\n", ret);
  127. goto errout;
  128. }
  129. /* Request user-space daemon to close socket. */
  130. ret = do_close_request(conn);
  131. if (ret < 0)
  132. {
  133. ret = OK; /* Error? return OK for close. */
  134. }
  135. else
  136. {
  137. /* Wait for completion of request. */
  138. while ((ret = net_lockedwait(&state.recvsem)) < OK)
  139. {
  140. DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
  141. }
  142. ret = state.result;
  143. if (ret < 0)
  144. {
  145. /* TODO: Error handling for close? Mark closed anyway? There is not
  146. * much we can do if this happens.
  147. */
  148. ninfo("user-space daemon reported error %d for usockid=%d\n",
  149. state.result, conn->usockid);
  150. ret = OK;
  151. }
  152. }
  153. usrsock_teardown_request_callback(&state);
  154. close_out:
  155. conn->state = USRSOCK_CONN_STATE_UNINITIALIZED;
  156. conn->usockid = -1;
  157. errout:
  158. net_unlock();
  159. return ret;
  160. }
  161. #endif /* CONFIG_NET && CONFIG_NET_USRSOCK */