unix-socket-ipc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "unix-socket.h"
  37. #include "unix-socket-ipc.h"
  38. int
  39. unix_socket_ipc_init(struct unix_socket_ipc *ipc, const char *socket_file_name, int backlog,
  40. size_t max_clients, size_t max_receive_size, size_t max_send_size)
  41. {
  42. memset(ipc, 0, sizeof(*ipc));
  43. ipc->socket_file_name = strdup(socket_file_name);
  44. if (ipc->socket_file_name == NULL) {
  45. return (-1);
  46. }
  47. unix_socket_client_list_init(&ipc->clients);
  48. ipc->backlog = backlog;
  49. ipc->socket = unix_socket_server_create(ipc->socket_file_name, 1,
  50. backlog);
  51. if (ipc->socket < 0) {
  52. free(ipc->socket_file_name);
  53. return (-1);
  54. }
  55. ipc->max_clients = max_clients;
  56. ipc->max_receive_size = max_receive_size;
  57. ipc->max_send_size = max_send_size;
  58. return (0);
  59. }
  60. int
  61. unix_socket_ipc_close(struct unix_socket_ipc *ipc)
  62. {
  63. int res;
  64. res = 0;
  65. if (ipc->socket < 0) {
  66. return (0);
  67. }
  68. if (unix_socket_server_destroy(ipc->socket, ipc->socket_file_name) != 0) {
  69. res = -1;
  70. }
  71. free(ipc->socket_file_name);
  72. ipc->socket_file_name = NULL;
  73. ipc->socket = -1;
  74. return (res);
  75. }
  76. int
  77. unix_socket_ipc_is_closed(struct unix_socket_ipc *ipc)
  78. {
  79. return (ipc->socket < 0);
  80. }
  81. int
  82. unix_socket_ipc_destroy(struct unix_socket_ipc *ipc)
  83. {
  84. if (unix_socket_ipc_close(ipc) != 0) {
  85. return (-1);
  86. }
  87. unix_socket_client_list_free(&ipc->clients);
  88. return (0);
  89. }
  90. /*
  91. * 0 = No error
  92. * -1 = Can't accept connection (errno set)
  93. * -2 = Too much clients
  94. * -3 = Can't add client to list
  95. */
  96. int
  97. unix_socket_ipc_accept(struct unix_socket_ipc *ipc, struct unix_socket_client **res_client)
  98. {
  99. int client_sock;
  100. struct unix_socket_client *client;
  101. if ((client_sock = unix_socket_server_accept(ipc->socket, 1)) < 0) {
  102. return (-1);
  103. }
  104. if (ipc->max_clients != 0 &&
  105. unix_socket_client_list_no_clients(&ipc->clients) >= ipc->max_clients) {
  106. unix_socket_close(client_sock);
  107. return (-2);
  108. }
  109. client = unix_socket_client_list_add(&ipc->clients, client_sock, ipc->max_receive_size,
  110. ipc->max_send_size, NULL);
  111. if (client == NULL) {
  112. unix_socket_close(client_sock);
  113. return (-3);
  114. }
  115. *res_client = client;
  116. return (0);
  117. }
  118. void
  119. unix_socket_ipc_client_disconnect(struct unix_socket_ipc *ipc, struct unix_socket_client *client)
  120. {
  121. unix_socket_close(client->socket);
  122. unix_socket_client_list_del(&ipc->clients, client);
  123. }