local_release.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /****************************************************************************
  2. * net/local/local_release.c
  3. *
  4. * Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  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_LOCAL)
  40. #include <semaphore.h>
  41. #include <errno.h>
  42. #include <queue.h>
  43. #include <assert.h>
  44. #include <nuttx/semaphore.h>
  45. #include <nuttx/net/net.h>
  46. #include <arch/irq.h>
  47. #include "local/local.h"
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: local_release
  53. *
  54. * Description:
  55. * If the local, Unix domain socket is in the connected state, then
  56. * disconnect it. Release the local connection structure in any event
  57. *
  58. * Input Parameters:
  59. * conn - A reference to local connection structure
  60. *
  61. ****************************************************************************/
  62. int local_release(FAR struct local_conn_s *conn)
  63. {
  64. /* There should be no references on this structure */
  65. DEBUGASSERT(conn->lc_crefs == 0);
  66. net_lock();
  67. #ifdef CONFIG_NET_LOCAL_STREAM
  68. /* We should not bet here with state LOCAL_STATE_ACCEPT. That is an
  69. * internal state that should be atomic with respect to socket operations.
  70. */
  71. DEBUGASSERT(conn->lc_state != LOCAL_STATE_ACCEPT);
  72. /* If the socket is connected (SOCK_STREAM client), then disconnect it */
  73. if (conn->lc_state == LOCAL_STATE_CONNECTED ||
  74. conn->lc_state == LOCAL_STATE_DISCONNECTED)
  75. {
  76. DEBUGASSERT(conn->lc_proto == SOCK_STREAM);
  77. /* Just free the connection structure */
  78. }
  79. /* Is the socket is listening socket (SOCK_STREAM server) */
  80. else if (conn->lc_state == LOCAL_STATE_LISTENING)
  81. {
  82. FAR struct local_conn_s *client;
  83. DEBUGASSERT(conn->lc_proto == SOCK_STREAM);
  84. /* Are there still clients waiting for a connection to the server? */
  85. for (client = (FAR struct local_conn_s *)conn->u.server.lc_waiters.head;
  86. client;
  87. client = (FAR struct local_conn_s *)dq_next(&client->lc_node))
  88. {
  89. client->u.client.lc_result = -ENOTCONN;
  90. nxsem_post(&client->lc_waitsem);
  91. }
  92. conn->u.server.lc_pending = 0;
  93. /* Remove the server from the list of listeners. */
  94. dq_rem(&conn->lc_node, &g_local_listeners);
  95. }
  96. #endif /* CONFIG_NET_LOCAL_STREAM */
  97. /* For the remaining states (LOCAL_STATE_UNBOUND and LOCAL_STATE_UNBOUND),
  98. * we simply free the connection structure.
  99. */
  100. /* Free the connection structure */
  101. local_free(conn);
  102. net_unlock();
  103. return OK;
  104. }
  105. #endif /* CONFIG_NET && CONFIG_NET_LOCAL */