common_test_agent.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <qb/qbloop.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 qb_loop_t *poll_handle;
  55. qb_loop_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 (
  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. qb_loop_stop (ta_poll_handle_get());
  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 (
  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. qb_loop_poll_add (poll_handle,
  153. QB_LOOP_MED,
  154. new_fd,
  155. POLLIN|POLLNVAL,
  156. NULL,
  157. server_process_data_fn);
  158. return 0;
  159. }
  160. static int create_server_sockect (int server_port)
  161. {
  162. int listener;
  163. int yes = 1;
  164. int rv;
  165. struct addrinfo hints, *ai, *p;
  166. char server_port_str[16];
  167. /* get a socket and bind it
  168. */
  169. sprintf(server_port_str, "%d", server_port);
  170. memset (&hints, 0, sizeof hints);
  171. hints.ai_family = AF_UNSPEC;
  172. hints.ai_socktype = SOCK_STREAM;
  173. hints.ai_flags = AI_PASSIVE;
  174. if ((rv = getaddrinfo (NULL, server_port_str, &hints, &ai)) != 0) {
  175. syslog (LOG_ERR, "%s\n", gai_strerror (rv));
  176. exit (1);
  177. }
  178. for (p = ai; p != NULL; p = p->ai_next) {
  179. listener = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
  180. if (listener < 0) {
  181. continue;
  182. }
  183. /* lose the pesky "address already in use" error message
  184. */
  185. if (setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
  186. &yes, sizeof(int)) < 0) {
  187. syslog (LOG_ERR, "setsockopt() failed: %s\n", strerror (errno));
  188. }
  189. if (bind (listener, p->ai_addr, p->ai_addrlen) < 0) {
  190. syslog (LOG_ERR, "bind() failed: %s\n", strerror (errno));
  191. close (listener);
  192. continue;
  193. }
  194. break;
  195. }
  196. if (p == NULL) {
  197. syslog (LOG_ERR, "failed to bind\n");
  198. exit (2);
  199. }
  200. freeaddrinfo (ai);
  201. if (listen (listener, 10) == -1) {
  202. syslog (LOG_ERR, "listen() failed: %s", strerror(errno));
  203. exit (3);
  204. }
  205. return listener;
  206. }
  207. static int32_t sig_exit_handler (int num, void *data)
  208. {
  209. qb_loop_stop(poll_handle);
  210. return 0;
  211. }
  212. int test_agent_run(int server_port, ta_do_command_fn func)
  213. {
  214. int listener;
  215. do_command = func;
  216. poll_handle = qb_loop_create ();
  217. qb_loop_signal_add(poll_handle, QB_LOOP_HIGH,
  218. SIGINT, NULL, sig_exit_handler, NULL);
  219. qb_loop_signal_add(poll_handle, QB_LOOP_HIGH,
  220. SIGQUIT, NULL, sig_exit_handler, NULL);
  221. qb_loop_signal_add(poll_handle, QB_LOOP_HIGH,
  222. SIGTERM, NULL, sig_exit_handler, NULL);
  223. listener = create_server_sockect (server_port);
  224. qb_loop_poll_add (poll_handle,
  225. QB_LOOP_MED,
  226. listener,
  227. POLLIN|POLLNVAL,
  228. NULL, server_accept_fn);
  229. qb_loop_run (poll_handle);
  230. return 0;
  231. }