common_test_agent.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. static pre_exit_fn pre_exit = NULL;
  56. qb_loop_t *ta_poll_handle_get(void)
  57. {
  58. return poll_handle;
  59. }
  60. static void shut_me_down(void)
  61. {
  62. if (pre_exit) {
  63. pre_exit();
  64. }
  65. qb_loop_stop(poll_handle);
  66. }
  67. static void ta_handle_command (int sock, char* msg)
  68. {
  69. int num_args;
  70. char *saveptr = NULL;
  71. char *str = strdup (msg);
  72. char *str_len;
  73. char *str_arg;
  74. char *args[5];
  75. int i = 0;
  76. int a = 0;
  77. char* func = NULL;
  78. if (parse_debug)
  79. syslog (LOG_DEBUG,"%s (MSG:%s)\n", __func__, msg);
  80. str_len = strtok_r (str, ":", &saveptr);
  81. assert (str_len);
  82. num_args = atoi (str_len) * 2;
  83. for (i = 0; i < num_args / 2; i++) {
  84. str_len = strtok_r (NULL, ":", &saveptr);
  85. str_arg = strtok_r (NULL, ":", &saveptr);
  86. if (func == NULL) {
  87. /* first "arg" is the function */
  88. if (parse_debug)
  89. syslog (LOG_DEBUG, "(LEN:%s, FUNC:%s)", str_len, str_arg);
  90. func = str_arg;
  91. a = 0;
  92. } else {
  93. args[a] = str_arg;
  94. a++;
  95. if (parse_debug)
  96. syslog (LOG_DEBUG, "(LEN:%s, ARG:%s)", str_len, str_arg);
  97. }
  98. }
  99. do_command (sock, func, args, a+1);
  100. free (str);
  101. }
  102. static int server_process_data_fn (
  103. int fd,
  104. int revents,
  105. void *data)
  106. {
  107. char *saveptr;
  108. char *msg;
  109. char *cmd;
  110. int32_t nbytes;
  111. if (revents & POLLHUP) {
  112. shut_me_down();
  113. return -1;
  114. }
  115. if ((nbytes = recv (fd, big_and_buf_rx, sizeof (big_and_buf_rx), 0)) <= 0) {
  116. /* got error or connection closed by client */
  117. if (nbytes == 0) {
  118. /* connection closed */
  119. syslog (LOG_WARNING, "socket %d hung up: exiting...\n", fd);
  120. } else {
  121. syslog (LOG_ERR,"recv() failed: %s", strerror(errno));
  122. }
  123. shut_me_down();
  124. return -1;
  125. } else {
  126. big_and_buf_rx[nbytes] = '\0';
  127. msg = strtok_r (big_and_buf_rx, ";", &saveptr);
  128. assert (msg);
  129. while (msg) {
  130. cmd = strdup (msg);
  131. ta_handle_command (fd, cmd);
  132. free (cmd);
  133. msg = strtok_r (NULL, ";", &saveptr);
  134. }
  135. }
  136. return 0;
  137. }
  138. static int server_accept_fn (
  139. int fd, int revents, void *data)
  140. {
  141. socklen_t addrlen;
  142. struct sockaddr_in in_addr;
  143. int new_fd;
  144. int res;
  145. if (revents & POLLHUP) {
  146. shut_me_down();
  147. return -1;
  148. }
  149. addrlen = sizeof (struct sockaddr_in);
  150. retry_accept:
  151. new_fd = accept (fd, (struct sockaddr *)&in_addr, &addrlen);
  152. if (new_fd == -1 && errno == EINTR) {
  153. goto retry_accept;
  154. }
  155. if (new_fd == -1) {
  156. syslog (LOG_ERR,
  157. "Could not accept connection: %s\n", strerror (errno));
  158. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  159. }
  160. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  161. if (res == -1) {
  162. syslog (LOG_ERR,
  163. "Could not set non-blocking operation on connection: %s\n",
  164. strerror (errno));
  165. close (new_fd);
  166. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  167. }
  168. qb_loop_poll_add (poll_handle,
  169. QB_LOOP_MED,
  170. new_fd,
  171. POLLIN|POLLNVAL,
  172. NULL,
  173. server_process_data_fn);
  174. return 0;
  175. }
  176. static int create_server_sockect (int server_port)
  177. {
  178. int listener;
  179. int yes = 1;
  180. int rv;
  181. struct addrinfo hints, *ai, *p;
  182. char server_port_str[16];
  183. char addr_str[INET_ADDRSTRLEN];
  184. void *ptr = NULL;
  185. /* get a socket and bind it
  186. */
  187. sprintf(server_port_str, "%d", server_port);
  188. memset (&hints, 0, sizeof hints);
  189. hints.ai_family = AF_UNSPEC;
  190. hints.ai_socktype = SOCK_STREAM;
  191. hints.ai_flags = AI_PASSIVE;
  192. if ((rv = getaddrinfo (NULL, server_port_str, &hints, &ai)) != 0) {
  193. syslog (LOG_ERR, "%s", gai_strerror (rv));
  194. exit (1);
  195. }
  196. for (p = ai; p != NULL; p = p->ai_next) {
  197. listener = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
  198. if (listener < 0) {
  199. continue;
  200. }
  201. /* lose the pesky "address already in use" error message
  202. */
  203. if (setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
  204. &yes, sizeof(int)) < 0) {
  205. syslog (LOG_ERR, "setsockopt() failed: %s", strerror (errno));
  206. }
  207. switch (p->ai_family)
  208. {
  209. case AF_INET:
  210. ptr = &((struct sockaddr_in *) p->ai_addr)->sin_addr;
  211. break;
  212. case AF_INET6:
  213. ptr = &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr;
  214. break;
  215. default:
  216. syslog (LOG_ERR, "address family wrong");
  217. exit (4);
  218. }
  219. if (inet_ntop(p->ai_family, ptr, addr_str, INET_ADDRSTRLEN) == NULL) {
  220. syslog (LOG_ERR, "inet_ntop() failed: %s", strerror (errno));
  221. }
  222. if (bind (listener, p->ai_addr, p->ai_addrlen) < 0) {
  223. syslog (LOG_ERR, "bind(%s) failed: %s\n", addr_str, strerror (errno));
  224. close (listener);
  225. continue;
  226. }
  227. break;
  228. }
  229. if (p == NULL) {
  230. syslog (LOG_ERR, "failed to bind");
  231. exit (2);
  232. }
  233. freeaddrinfo (ai);
  234. if (listen (listener, 10) == -1) {
  235. syslog (LOG_ERR, "listen() failed: %s", strerror(errno));
  236. exit (3);
  237. }
  238. return listener;
  239. }
  240. static int32_t sig_exit_handler (int num, void *data)
  241. {
  242. shut_me_down();
  243. return 0;
  244. }
  245. int test_agent_run(int server_port, ta_do_command_fn func, pre_exit_fn exit_fn)
  246. {
  247. int listener;
  248. do_command = func;
  249. pre_exit = exit_fn;
  250. poll_handle = qb_loop_create ();
  251. if (exit_fn) {
  252. qb_loop_signal_add(poll_handle, QB_LOOP_HIGH,
  253. SIGINT, NULL, sig_exit_handler, NULL);
  254. qb_loop_signal_add(poll_handle, QB_LOOP_HIGH,
  255. SIGQUIT, NULL, sig_exit_handler, NULL);
  256. qb_loop_signal_add(poll_handle, QB_LOOP_HIGH,
  257. SIGTERM, NULL, sig_exit_handler, NULL);
  258. }
  259. listener = create_server_sockect (server_port);
  260. qb_loop_poll_add (poll_handle,
  261. QB_LOOP_MED,
  262. listener,
  263. POLLIN|POLLNVAL,
  264. NULL, server_accept_fn);
  265. qb_loop_run (poll_handle);
  266. return 0;
  267. }