unix-socket-ipc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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,
  40. int set_socket_umask, mode_t socket_umask, gid_t socket_gid, int backlog,
  41. size_t max_clients, size_t max_receive_size, size_t max_send_size)
  42. {
  43. memset(ipc, 0, sizeof(*ipc));
  44. ipc->socket_file_name = strdup(socket_file_name);
  45. if (ipc->socket_file_name == NULL) {
  46. return (-1);
  47. }
  48. unix_socket_client_list_init(&ipc->clients);
  49. ipc->backlog = backlog;
  50. ipc->socket = unix_socket_server_create(ipc->socket_file_name,
  51. set_socket_umask, socket_umask, socket_gid, 1, backlog);
  52. if (ipc->socket < 0) {
  53. free(ipc->socket_file_name);
  54. return (-1);
  55. }
  56. ipc->max_clients = max_clients;
  57. ipc->max_receive_size = max_receive_size;
  58. ipc->max_send_size = max_send_size;
  59. return (0);
  60. }
  61. int
  62. unix_socket_ipc_close(struct unix_socket_ipc *ipc)
  63. {
  64. int res;
  65. res = 0;
  66. if (ipc->socket < 0) {
  67. return (0);
  68. }
  69. if (unix_socket_server_destroy(ipc->socket, ipc->socket_file_name) != 0) {
  70. res = -1;
  71. }
  72. free(ipc->socket_file_name);
  73. ipc->socket_file_name = NULL;
  74. ipc->socket = -1;
  75. return (res);
  76. }
  77. int
  78. unix_socket_ipc_is_closed(struct unix_socket_ipc *ipc)
  79. {
  80. return (ipc->socket < 0);
  81. }
  82. int
  83. unix_socket_ipc_destroy(struct unix_socket_ipc *ipc)
  84. {
  85. if (unix_socket_ipc_close(ipc) != 0) {
  86. return (-1);
  87. }
  88. unix_socket_client_list_free(&ipc->clients);
  89. return (0);
  90. }
  91. /*
  92. * 0 = No error
  93. * -1 = Can't accept connection (errno set)
  94. * -2 = Too much clients
  95. * -3 = Can't add client to list
  96. */
  97. int
  98. unix_socket_ipc_accept(struct unix_socket_ipc *ipc, struct unix_socket_client **res_client)
  99. {
  100. int client_sock;
  101. struct unix_socket_client *client;
  102. if ((client_sock = unix_socket_server_accept(ipc->socket, 1)) < 0) {
  103. return (-1);
  104. }
  105. if (ipc->max_clients != 0 &&
  106. unix_socket_client_list_no_clients(&ipc->clients) >= ipc->max_clients) {
  107. unix_socket_close(client_sock);
  108. return (-2);
  109. }
  110. client = unix_socket_client_list_add(&ipc->clients, client_sock, ipc->max_receive_size,
  111. ipc->max_send_size, NULL);
  112. if (client == NULL) {
  113. unix_socket_close(client_sock);
  114. return (-3);
  115. }
  116. *res_client = client;
  117. return (0);
  118. }
  119. void
  120. unix_socket_ipc_client_disconnect(struct unix_socket_ipc *ipc, struct unix_socket_client *client)
  121. {
  122. unix_socket_close(client->socket);
  123. unix_socket_client_list_del(&ipc->clients, client);
  124. }