qdevice-ipc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "qdevice-config.h"
  35. #include "qdevice-ipc.h"
  36. #include "qdevice-log.h"
  37. #include "unix-socket-ipc.h"
  38. #include "dynar-simple-lex.h"
  39. int
  40. qdevice_ipc_init(struct qdevice_instance *instance)
  41. {
  42. if (unix_socket_ipc_init(&instance->local_ipc, QDEVICE_LOCAL_SOCKET_FILE,
  43. QDEVICE_LOCAL_SOCKET_BACKLOG, QDEVICE_IPC_MAX_CLIENTS, QDEVICE_IPC_MAX_RECEIVE_SIZE,
  44. QDEVICE_IPC_MAX_SEND_SIZE) != 0) {
  45. qdevice_log_err(LOG_ERR, "Can't create unix socket");
  46. return (-1);
  47. }
  48. return (0);
  49. }
  50. int
  51. qdevice_ipc_destroy(struct qdevice_instance *instance)
  52. {
  53. int res;
  54. res = unix_socket_ipc_destroy(&instance->local_ipc);
  55. if (res != 0) {
  56. qdevice_log_err(LOG_WARNING, "Can't destroy local IPC");
  57. }
  58. return (res);
  59. }
  60. int
  61. qdevice_ipc_accept(struct qdevice_instance *instance, struct unix_socket_client **res_client)
  62. {
  63. int res;
  64. int accept_res;
  65. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  66. switch (accept_res) {
  67. case -1:
  68. qdevice_log_err(LOG_ERR, "Can't accept local IPC connection");
  69. res = -1;
  70. break;
  71. case -2:
  72. qdevice_log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  73. res = -1;
  74. break;
  75. case -3:
  76. qdevice_log(LOG_ERR, "Can't add client to list");
  77. res = -1;
  78. break;
  79. default:
  80. unix_socket_client_read_line(*res_client, 1);
  81. res = 0;
  82. break;
  83. }
  84. return (res);
  85. }
  86. void
  87. qdevice_ipc_client_disconnect(struct qdevice_instance *instance, struct unix_socket_client *client)
  88. {
  89. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  90. }
  91. static void
  92. qdevice_ipc_parse_line(struct qdevice_instance *instance, struct unix_socket_client *client)
  93. {
  94. struct dynar_simple_lex lex;
  95. struct dynar *token;
  96. char *str;
  97. dynar_simple_lex_init(&lex, &client->receive_buffer);
  98. token = dynar_simple_lex_token_next(&lex);
  99. if (token == NULL) {
  100. qdevice_log(LOG_ERR, "Can't alloc memory for simple lex");
  101. return;
  102. }
  103. str = dynar_data(token);
  104. if (strcasecmp(str, "") == 0) {
  105. qdevice_log(LOG_DEBUG, "IPC client error: No command specified");
  106. // SEND ERROR
  107. } else if (strcasecmp(str, "shutdown") == 0) {
  108. qdevice_log(LOG_DEBUG, "IPC client requested shutdown");
  109. // Send output?
  110. } else if (strcasecmp(str, "status") == 0) {
  111. qdevice_log(LOG_DEBUG, "IPC client requested status display");
  112. // Send output
  113. } else {
  114. qdevice_log(LOG_DEBUG, "IPC client sent unknown command");
  115. // Send output
  116. }
  117. dynar_simple_lex_destroy(&lex);
  118. }
  119. void
  120. qdevice_ipc_io_read(struct qdevice_instance *instance, struct unix_socket_client *client)
  121. {
  122. int res;
  123. res = unix_socket_client_io_read(client);
  124. switch (res) {
  125. case 0:
  126. /*
  127. * Partial read
  128. */
  129. break;
  130. case -1:
  131. qdevice_log(LOG_DEBUG, "IPC client closed connection");
  132. client->schedule_disconnect = 1;
  133. break;
  134. case -2:
  135. qdevice_log(LOG_ERR, "Can't store message from IPC client. Disconnecting client");
  136. client->schedule_disconnect = 1;
  137. break;
  138. case 1:
  139. /*
  140. * Full message received
  141. */
  142. qdevice_ipc_parse_line(instance, client);
  143. break;
  144. }
  145. }