common_test_agent.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld (asalkeld@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 MontaVista Software, 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 <errno.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <assert.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <netdb.h>
  45. #include <syslog.h>
  46. #include <poll.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <corosync/totem/coropoll.h>
  50. #include "common_test_agent.h"
  51. int32_t parse_debug = 0;
  52. static char big_and_buf_rx[HOW_BIG_AND_BUF];
  53. ta_do_command_fn do_command;
  54. static hdb_handle_t poll_handle;
  55. hdb_handle_t ta_poll_handle_get(void)
  56. {
  57. return poll_handle;
  58. }
  59. static void ta_handle_command (int sock, char* msg)
  60. {
  61. int num_args;
  62. char *saveptr = NULL;
  63. char *str = strdup (msg);
  64. char *str_len;
  65. char *str_arg;
  66. char *args[5];
  67. int i = 0;
  68. int a = 0;
  69. char* func = NULL;
  70. if (parse_debug)
  71. syslog (LOG_DEBUG,"%s (MSG:%s)\n", __func__, msg);
  72. str_len = strtok_r (str, ":", &saveptr);
  73. assert (str_len);
  74. num_args = atoi (str_len) * 2;
  75. for (i = 0; i < num_args / 2; i++) {
  76. str_len = strtok_r (NULL, ":", &saveptr);
  77. str_arg = strtok_r (NULL, ":", &saveptr);
  78. if (func == NULL) {
  79. /* first "arg" is the function */
  80. if (parse_debug)
  81. syslog (LOG_DEBUG, "(LEN:%s, FUNC:%s)", str_len, str_arg);
  82. func = str_arg;
  83. a = 0;
  84. } else {
  85. args[a] = str_arg;
  86. a++;
  87. if (parse_debug)
  88. syslog (LOG_DEBUG, "(LEN:%s, ARG:%s)", str_len, str_arg);
  89. }
  90. }
  91. do_command (sock, func, args, a+1);
  92. free (str);
  93. }
  94. static int server_process_data_fn (hdb_handle_t handle,
  95. int fd,
  96. int revents,
  97. void *data)
  98. {
  99. char *saveptr;
  100. char *msg;
  101. char *cmd;
  102. int32_t nbytes;
  103. if ((nbytes = recv (fd, big_and_buf_rx, sizeof (big_and_buf_rx), 0)) <= 0) {
  104. /* got error or connection closed by client */
  105. if (nbytes == 0) {
  106. /* connection closed */
  107. syslog (LOG_WARNING, "socket %d hung up: exiting...\n", fd);
  108. } else {
  109. syslog (LOG_ERR,"recv() failed: %s", strerror(errno));
  110. }
  111. close (fd);
  112. poll_stop (handle);
  113. } else {
  114. big_and_buf_rx[nbytes] = '\0';
  115. msg = strtok_r (big_and_buf_rx, ";", &saveptr);
  116. assert (msg);
  117. while (msg) {
  118. cmd = strdup (msg);
  119. ta_handle_command (fd, cmd);
  120. free (cmd);
  121. msg = strtok_r (NULL, ";", &saveptr);
  122. }
  123. }
  124. return 0;
  125. }
  126. static int server_accept_fn (hdb_handle_t handle,
  127. int fd, int revents, void *data)
  128. {
  129. socklen_t addrlen;
  130. struct sockaddr_in in_addr;
  131. int new_fd;
  132. int res;
  133. addrlen = sizeof (struct sockaddr_in);
  134. retry_accept:
  135. new_fd = accept (fd, (struct sockaddr *)&in_addr, &addrlen);
  136. if (new_fd == -1 && errno == EINTR) {
  137. goto retry_accept;
  138. }
  139. if (new_fd == -1) {
  140. syslog (LOG_ERR,
  141. "Could not accept connection: %s\n", strerror (errno));
  142. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  143. }
  144. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  145. if (res == -1) {
  146. syslog (LOG_ERR,
  147. "Could not set non-blocking operation on connection: %s\n",
  148. strerror (errno));
  149. close (new_fd);
  150. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  151. }
  152. poll_dispatch_add (poll_handle, new_fd, POLLIN|POLLNVAL, NULL, server_process_data_fn);
  153. return 0;
  154. }
  155. static int create_server_sockect (int server_port)
  156. {
  157. int listener;
  158. int yes = 1;
  159. int rv;
  160. struct addrinfo hints, *ai, *p;
  161. char server_port_str[16];
  162. /* get a socket and bind it
  163. */
  164. sprintf(server_port_str, "%d", server_port);
  165. memset (&hints, 0, sizeof hints);
  166. hints.ai_family = AF_UNSPEC;
  167. hints.ai_socktype = SOCK_STREAM;
  168. hints.ai_flags = AI_PASSIVE;
  169. if ((rv = getaddrinfo (NULL, server_port_str, &hints, &ai)) != 0) {
  170. syslog (LOG_ERR, "%s\n", gai_strerror (rv));
  171. exit (1);
  172. }
  173. for (p = ai; p != NULL; p = p->ai_next) {
  174. listener = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
  175. if (listener < 0) {
  176. continue;
  177. }
  178. /* lose the pesky "address already in use" error message
  179. */
  180. if (setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
  181. &yes, sizeof(int)) < 0) {
  182. syslog (LOG_ERR, "setsockopt() failed: %s\n", strerror (errno));
  183. }
  184. if (bind (listener, p->ai_addr, p->ai_addrlen) < 0) {
  185. syslog (LOG_ERR, "bind() failed: %s\n", strerror (errno));
  186. close (listener);
  187. continue;
  188. }
  189. break;
  190. }
  191. if (p == NULL) {
  192. syslog (LOG_ERR, "failed to bind\n");
  193. exit (2);
  194. }
  195. freeaddrinfo (ai);
  196. if (listen (listener, 10) == -1) {
  197. syslog (LOG_ERR, "listen() failed: %s", strerror(errno));
  198. exit (3);
  199. }
  200. return listener;
  201. }
  202. int test_agent_run(int server_port, ta_do_command_fn func)
  203. {
  204. int listener;
  205. do_command = func;
  206. poll_handle = poll_create ();
  207. listener = create_server_sockect (server_port);
  208. poll_dispatch_add (poll_handle, listener, POLLIN|POLLNVAL, NULL, server_accept_fn);
  209. return poll_run (poll_handle);
  210. }