common_test_agent.c 7.9 KB

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