cpg_test_agent.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 <corosync/list.h>
  51. #include <corosync/cpg.h>
  52. #include "../../exec/crypto.h"
  53. #define SERVER_PORT "9034"
  54. typedef enum {
  55. MSG_OK,
  56. MSG_NODEID_ERR,
  57. MSG_PID_ERR,
  58. MSG_SEQ_ERR,
  59. MSG_SIZE_ERR,
  60. MSG_SHA1_ERR,
  61. } msg_status_t;
  62. typedef struct {
  63. uint32_t nodeid;
  64. pid_t pid;
  65. unsigned char sha1[20];
  66. uint32_t seq;
  67. size_t size;
  68. unsigned char payload[0];
  69. } msg_t;
  70. #define LOG_STR_SIZE 256
  71. typedef struct {
  72. char log[LOG_STR_SIZE];
  73. struct list_head list;
  74. } log_entry_t;
  75. #define HOW_BIG_AND_BUF 4096
  76. static char big_and_buf[HOW_BIG_AND_BUF];
  77. static char big_and_buf_rx[HOW_BIG_AND_BUF];
  78. static int32_t parse_debug = 0;
  79. static int32_t record_config_events_g = 0;
  80. static int32_t record_messages_g = 0;
  81. static cpg_handle_t cpg_handle = 0;
  82. static int32_t cpg_fd = -1;
  83. static struct list_head config_chg_log_head;
  84. static struct list_head msg_log_head;
  85. static pid_t my_pid;
  86. static uint32_t my_nodeid;
  87. static int32_t my_seq;
  88. static int32_t my_msgs_to_send;
  89. static int32_t total_stored_msgs = 0;
  90. static hdb_handle_t poll_handle;
  91. static void send_some_more_messages (void * unused);
  92. static char* err_status_string (char * buf, size_t buf_len, msg_status_t status)
  93. {
  94. switch (status) {
  95. case MSG_OK:
  96. strncpy (buf, "OK", buf_len);
  97. break;
  98. case MSG_NODEID_ERR:
  99. strncpy (buf, "NODEID_ERR", buf_len);
  100. break;
  101. case MSG_PID_ERR:
  102. strncpy (buf, "PID_ERR", buf_len);
  103. break;
  104. case MSG_SEQ_ERR:
  105. strncpy (buf, "SEQ_ERR", buf_len);
  106. break;
  107. case MSG_SIZE_ERR:
  108. strncpy (buf, "SIZE_ERR", buf_len);
  109. break;
  110. case MSG_SHA1_ERR:
  111. strncpy (buf, "SHA1_ERR", buf_len);
  112. break;
  113. default:
  114. strncpy (buf, "UNKNOWN_ERR", buf_len);
  115. break;
  116. }
  117. return buf;
  118. }
  119. static void delivery_callback (
  120. cpg_handle_t handle,
  121. const struct cpg_name *groupName,
  122. uint32_t nodeid,
  123. uint32_t pid,
  124. void *msg,
  125. size_t msg_len)
  126. {
  127. log_entry_t *log_pt;
  128. msg_t *msg_pt = (msg_t*)msg;
  129. msg_status_t status = MSG_OK;
  130. char status_buf[20];
  131. unsigned char sha1_compare[20];
  132. hash_state sha1_hash;
  133. if (record_messages_g == 0) {
  134. return;
  135. }
  136. msg_pt->seq = my_seq;
  137. my_seq++;
  138. if (nodeid != msg_pt->nodeid) {
  139. status = MSG_NODEID_ERR;
  140. }
  141. if (pid != msg_pt->pid) {
  142. status = MSG_PID_ERR;
  143. }
  144. if (msg_len != msg_pt->size) {
  145. status = MSG_SIZE_ERR;
  146. }
  147. sha1_init (&sha1_hash);
  148. sha1_process (&sha1_hash, msg_pt->payload, (msg_pt->size - sizeof (msg_t)));
  149. sha1_done (&sha1_hash, sha1_compare);
  150. if (memcmp (sha1_compare, msg_pt->sha1, 20) != 0) {
  151. syslog (LOG_ERR, "%s(); msg seq:%d; incorrect hash",
  152. __func__, msg_pt->seq);
  153. status = MSG_SHA1_ERR;
  154. }
  155. log_pt = malloc (sizeof(log_entry_t));
  156. list_init (&log_pt->list);
  157. snprintf (log_pt->log, LOG_STR_SIZE, "%d:%d:%d:%s;",
  158. msg_pt->nodeid, msg_pt->pid, msg_pt->seq,
  159. err_status_string (status_buf, 20, status));
  160. list_add_tail (&log_pt->list, &msg_log_head);
  161. total_stored_msgs++;
  162. }
  163. static void config_change_callback (
  164. cpg_handle_t handle,
  165. const struct cpg_name *groupName,
  166. const struct cpg_address *member_list, size_t member_list_entries,
  167. const struct cpg_address *left_list, size_t left_list_entries,
  168. const struct cpg_address *joined_list, size_t joined_list_entries)
  169. {
  170. int i;
  171. log_entry_t *log_pt;
  172. /* group_name,ip,pid,join|leave */
  173. if (record_config_events_g == 0) {
  174. return;
  175. }
  176. for (i = 0; i < left_list_entries; i++) {
  177. syslog (LOG_DEBUG, "%s() inserting leave event into list", __func__);
  178. log_pt = malloc (sizeof(log_entry_t));
  179. list_init (&log_pt->list);
  180. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,left",
  181. groupName->value, left_list[i].nodeid,left_list[i].pid);
  182. list_add_tail(&log_pt->list, &config_chg_log_head);
  183. }
  184. for (i = 0; i < joined_list_entries; i++) {
  185. syslog (LOG_DEBUG, "%s() inserting join event into list", __func__);
  186. log_pt = malloc (sizeof(log_entry_t));
  187. list_init (&log_pt->list);
  188. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,join",
  189. groupName->value, joined_list[i].nodeid,joined_list[i].pid);
  190. list_add_tail (&log_pt->list, &config_chg_log_head);
  191. }
  192. }
  193. static cpg_callbacks_t callbacks = {
  194. .cpg_deliver_fn = delivery_callback,
  195. .cpg_confchg_fn = config_change_callback,
  196. };
  197. static void record_messages (void)
  198. {
  199. record_messages_g = 1;
  200. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_messages_g);
  201. }
  202. static void record_config_events (void)
  203. {
  204. record_config_events_g = 1;
  205. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_config_events_g);
  206. }
  207. static void read_config_event (int sock)
  208. {
  209. const char *empty = "None";
  210. struct list_head * list = config_chg_log_head.next;
  211. log_entry_t *entry;
  212. if (list != &config_chg_log_head) {
  213. entry = list_entry (list, log_entry_t, list);
  214. send (sock, entry->log, strlen (entry->log) + 1, 0);
  215. list_del (&entry->list);
  216. free (entry);
  217. } else {
  218. syslog (LOG_DEBUG,"%s() no events in list", __func__);
  219. send (sock, empty, strlen (empty) + 1, 0);
  220. }
  221. }
  222. static void read_messages (int sock, char* atmost_str)
  223. {
  224. struct list_head * list;
  225. log_entry_t *entry;
  226. int atmost = atoi (atmost_str);
  227. int packed = 0;
  228. if (atmost == 0)
  229. atmost = 1;
  230. if (atmost > (HOW_BIG_AND_BUF / LOG_STR_SIZE))
  231. atmost = (HOW_BIG_AND_BUF / LOG_STR_SIZE);
  232. syslog (LOG_DEBUG, "%s() atmost %d; total_stored_msgs:%d",
  233. __func__, atmost, total_stored_msgs);
  234. big_and_buf[0] = '\0';
  235. for (list = msg_log_head.next;
  236. (!list_empty (&msg_log_head) && packed < atmost); ) {
  237. entry = list_entry (list, log_entry_t, list);
  238. strcat (big_and_buf, entry->log);
  239. packed++;
  240. list = list->next;
  241. list_del (&entry->list);
  242. free (entry);
  243. total_stored_msgs--;
  244. }
  245. syslog (LOG_DEBUG, "%s() sending %d; total_stored_msgs:%d; len:%d",
  246. __func__, packed, total_stored_msgs, (int)strlen (big_and_buf));
  247. if (packed == 0) {
  248. strcpy (big_and_buf, "None");
  249. }
  250. send (sock, big_and_buf, strlen (big_and_buf), 0);
  251. }
  252. static void send_some_more_messages_later (void)
  253. {
  254. poll_timer_handle timer_handle;
  255. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  256. poll_timer_add (
  257. poll_handle,
  258. 100, NULL,
  259. send_some_more_messages,
  260. &timer_handle);
  261. }
  262. static unsigned char buffer[200000];
  263. static void send_some_more_messages (void * unused)
  264. {
  265. msg_t my_msg;
  266. struct iovec iov[2];
  267. int i;
  268. int send_now;
  269. size_t payload_size;
  270. hash_state sha1_hash;
  271. cs_error_t res;
  272. cpg_flow_control_state_t fc_state;
  273. if (cpg_fd < 0)
  274. return;
  275. send_now = my_msgs_to_send;
  276. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  277. my_msg.pid = my_pid;
  278. my_msg.nodeid = my_nodeid;
  279. payload_size = (rand() % 100000);
  280. my_msg.size = sizeof (msg_t) + payload_size;
  281. my_msg.seq = 0;
  282. for (i = 0; i < payload_size; i++) {
  283. buffer[i] = i;
  284. }
  285. sha1_init (&sha1_hash);
  286. sha1_process (&sha1_hash, buffer, payload_size);
  287. sha1_done (&sha1_hash, my_msg.sha1);
  288. iov[0].iov_len = sizeof (msg_t);
  289. iov[0].iov_base = &my_msg;
  290. iov[1].iov_len = payload_size;
  291. iov[1].iov_base = buffer;
  292. for (i = 0; i < send_now; i++) {
  293. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  294. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  295. /* lets do this later */
  296. send_some_more_messages_later ();
  297. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  298. return;
  299. }
  300. res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2);
  301. if (res == CS_ERR_TRY_AGAIN) {
  302. /* lets do this later */
  303. send_some_more_messages_later ();
  304. syslog (LOG_INFO, "%s() cpg_mcast_joined() says try again.",
  305. __func__);
  306. return;
  307. } else
  308. if (res != CS_OK) {
  309. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  310. __func__, res);
  311. exit (-2);
  312. }
  313. my_msgs_to_send--;
  314. }
  315. }
  316. static void msg_blaster (int sock, char* num_to_send_str)
  317. {
  318. my_msgs_to_send = atoi (num_to_send_str);
  319. my_seq = 1;
  320. my_pid = getpid();
  321. cpg_local_get (cpg_handle, &my_nodeid);
  322. /* control the limits */
  323. if (my_msgs_to_send <= 0)
  324. my_msgs_to_send = 1;
  325. if (my_msgs_to_send > 10000)
  326. my_msgs_to_send = 10000;
  327. send_some_more_messages (NULL);
  328. }
  329. static int cpg_dispatch_wrapper_fn (hdb_handle_t handle,
  330. int fd,
  331. int revents,
  332. void *data)
  333. {
  334. cs_error_t error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  335. if (error == CS_ERR_LIBRARY) {
  336. syslog (LOG_ERR, "%s() got LIB error disconnecting from corosync.", __func__);
  337. poll_dispatch_delete (poll_handle, cpg_fd);
  338. close (cpg_fd);
  339. cpg_fd = -1;
  340. }
  341. return 0;
  342. }
  343. static void do_command (int sock, char* func, char*args[], int num_args)
  344. {
  345. int result;
  346. struct cpg_name group_name;
  347. if (parse_debug)
  348. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  349. if (strcmp ("cpg_mcast_joined",func) == 0) {
  350. struct iovec iov[5];
  351. int a;
  352. for (a = 0; a < num_args; a++) {
  353. iov[a].iov_base = args[a];
  354. iov[a].iov_len = strlen(args[a])+1;
  355. }
  356. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  357. } else if (strcmp ("cpg_join",func) == 0) {
  358. strcpy (group_name.value, args[0]);
  359. group_name.length = strlen(args[0]);
  360. result = cpg_join (cpg_handle, &group_name);
  361. if (result != CS_OK) {
  362. syslog (LOG_ERR,
  363. "Could not join process group, error %d\n", result);
  364. exit (1);
  365. }
  366. } else if (strcmp ("cpg_leave",func) == 0) {
  367. strcpy (group_name.value, args[0]);
  368. group_name.length = strlen(args[0]);
  369. result = cpg_leave (cpg_handle, &group_name);
  370. if (result != CS_OK) {
  371. syslog (LOG_ERR,
  372. "Could not leave process group, error %d\n", result);
  373. exit (1);
  374. }
  375. syslog (LOG_INFO, "called cpg_leave()!");
  376. } else if (strcmp ("cpg_initialize",func) == 0) {
  377. int retry_count = 0;
  378. result = cpg_initialize (&cpg_handle, &callbacks);
  379. while (result != CS_OK) {
  380. syslog (LOG_ERR,
  381. "cpg_initialize error %d (attempt %d)\n",
  382. result, retry_count);
  383. if (retry_count >= 3) {
  384. exit (1);
  385. }
  386. sleep(1);
  387. retry_count++;
  388. }
  389. cpg_fd_get (cpg_handle, &cpg_fd);
  390. poll_dispatch_add (poll_handle, cpg_fd, POLLIN|POLLNVAL, NULL, cpg_dispatch_wrapper_fn);
  391. } else if (strcmp ("cpg_local_get", func) == 0) {
  392. unsigned int local_nodeid;
  393. char response[100];
  394. cpg_local_get (cpg_handle, &local_nodeid);
  395. snprintf (response, 100, "%u",local_nodeid);
  396. send (sock, response, strlen (response) + 1, 0);
  397. } else if (strcmp ("cpg_finalize",func) == 0) {
  398. cpg_finalize (cpg_handle);
  399. poll_dispatch_delete (poll_handle, cpg_fd);
  400. cpg_fd = -1;
  401. } else if (strcmp ("record_config_events",func) == 0) {
  402. record_config_events ();
  403. } else if (strcmp ("record_messages",func) == 0) {
  404. record_messages ();
  405. } else if (strcmp ("read_config_event",func) == 0) {
  406. read_config_event (sock);
  407. } else if (strcmp ("read_messages",func) == 0) {
  408. read_messages (sock, args[0]);
  409. } else if (strcmp ("msg_blaster",func) == 0) {
  410. msg_blaster (sock, args[0]);
  411. } else {
  412. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  413. }
  414. }
  415. static void handle_command (int sock, char* msg)
  416. {
  417. int num_args;
  418. char *saveptr = NULL;
  419. char *str = strdup (msg);
  420. char *str_len;
  421. char *str_arg;
  422. char *args[5];
  423. int i = 0;
  424. int a = 0;
  425. char* func = NULL;
  426. if (parse_debug)
  427. syslog (LOG_DEBUG,"%s (MSG:%s)\n", __func__, msg);
  428. str_len = strtok_r (str, ":", &saveptr);
  429. assert (str_len);
  430. num_args = atoi (str_len) * 2;
  431. for (i = 0; i < num_args / 2; i++) {
  432. str_len = strtok_r (NULL, ":", &saveptr);
  433. str_arg = strtok_r (NULL, ":", &saveptr);
  434. if (func == NULL) {
  435. /* first "arg" is the function */
  436. if (parse_debug)
  437. syslog (LOG_DEBUG, "(LEN:%s, FUNC:%s)", str_len, str_arg);
  438. func = str_arg;
  439. a = 0;
  440. } else {
  441. args[a] = str_arg;
  442. a++;
  443. if (parse_debug)
  444. syslog (LOG_DEBUG, "(LEN:%s, ARG:%s)", str_len, str_arg);
  445. }
  446. }
  447. do_command (sock, func, args, a+1);
  448. free (str);
  449. }
  450. static int server_process_data_fn (hdb_handle_t handle,
  451. int fd,
  452. int revents,
  453. void *data)
  454. {
  455. char *saveptr;
  456. char *msg;
  457. char *cmd;
  458. int32_t nbytes;
  459. if ((nbytes = recv (fd, big_and_buf_rx, sizeof (big_and_buf_rx), 0)) <= 0) {
  460. /* got error or connection closed by client */
  461. if (nbytes == 0) {
  462. /* connection closed */
  463. syslog (LOG_WARNING, "socket %d hung up: exiting...\n", fd);
  464. } else {
  465. syslog (LOG_ERR,"recv() failed: %s", strerror(errno));
  466. }
  467. close (fd);
  468. exit (0);
  469. } else {
  470. if (my_msgs_to_send > 0)
  471. send_some_more_messages (NULL);
  472. big_and_buf_rx[nbytes] = '\0';
  473. msg = strtok_r (big_and_buf_rx, ";", &saveptr);
  474. assert (msg);
  475. while (msg) {
  476. cmd = strdup (msg);
  477. handle_command (fd, cmd);
  478. free (cmd);
  479. msg = strtok_r (NULL, ";", &saveptr);
  480. }
  481. }
  482. return 0;
  483. }
  484. static int server_accept_fn (hdb_handle_t handle,
  485. int fd,
  486. int revents,
  487. void *data)
  488. {
  489. socklen_t addrlen;
  490. struct sockaddr_in in_addr;
  491. int new_fd;
  492. int res;
  493. addrlen = sizeof (struct sockaddr_in);
  494. retry_accept:
  495. new_fd = accept (fd, (struct sockaddr *)&in_addr, &addrlen);
  496. if (new_fd == -1 && errno == EINTR) {
  497. goto retry_accept;
  498. }
  499. if (new_fd == -1) {
  500. syslog (LOG_ERR,
  501. "Could not accept connection: %s\n", strerror (errno));
  502. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  503. }
  504. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  505. if (res == -1) {
  506. syslog (LOG_ERR,
  507. "Could not set non-blocking operation on connection: %s\n",
  508. strerror (errno));
  509. close (new_fd);
  510. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  511. }
  512. poll_dispatch_add (poll_handle, new_fd, POLLIN|POLLNVAL, NULL, server_process_data_fn);
  513. return 0;
  514. }
  515. static int create_server_sockect (void)
  516. {
  517. int listener;
  518. int yes = 1;
  519. int rv;
  520. struct addrinfo hints, *ai, *p;
  521. /* get a socket and bind it
  522. */
  523. memset (&hints, 0, sizeof hints);
  524. hints.ai_family = AF_UNSPEC;
  525. hints.ai_socktype = SOCK_STREAM;
  526. hints.ai_flags = AI_PASSIVE;
  527. if ((rv = getaddrinfo (NULL, SERVER_PORT, &hints, &ai)) != 0) {
  528. syslog (LOG_ERR, "%s\n", gai_strerror (rv));
  529. exit (1);
  530. }
  531. for (p = ai; p != NULL; p = p->ai_next) {
  532. listener = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
  533. if (listener < 0) {
  534. continue;
  535. }
  536. /* lose the pesky "address already in use" error message
  537. */
  538. if (setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
  539. &yes, sizeof(int)) < 0) {
  540. syslog (LOG_ERR, "setsockopt() failed: %s\n", strerror (errno));
  541. }
  542. if (bind (listener, p->ai_addr, p->ai_addrlen) < 0) {
  543. syslog (LOG_ERR, "bind() failed: %s\n", strerror (errno));
  544. close (listener);
  545. continue;
  546. }
  547. break;
  548. }
  549. if (p == NULL) {
  550. syslog (LOG_ERR, "failed to bind\n");
  551. exit (2);
  552. }
  553. freeaddrinfo (ai);
  554. if (listen (listener, 10) == -1) {
  555. syslog (LOG_ERR, "listen() failed: %s", strerror(errno));
  556. exit (3);
  557. }
  558. return listener;
  559. }
  560. int main (int argc, char *argv[])
  561. {
  562. int listener;
  563. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  564. list_init (&msg_log_head);
  565. list_init (&config_chg_log_head);
  566. poll_handle = poll_create ();
  567. listener = create_server_sockect ();
  568. poll_dispatch_add (poll_handle, listener, POLLIN|POLLNVAL, NULL, server_accept_fn);
  569. poll_run (poll_handle);
  570. return -1;
  571. }