common_test_agent.c 7.9 KB

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