cpg_test_agent.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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/list.h>
  50. #include <qb/qbdefs.h>
  51. #include <qb/qbutil.h>
  52. #include <qb/qbloop.h>
  53. #include <corosync/cpg.h>
  54. #include <corosync/cfg.h>
  55. #include "../../exec/crypto.h"
  56. #include "common_test_agent.h"
  57. typedef enum {
  58. MSG_OK,
  59. MSG_NODEID_ERR,
  60. MSG_PID_ERR,
  61. MSG_SEQ_ERR,
  62. MSG_SIZE_ERR,
  63. MSG_SHA1_ERR,
  64. } msg_status_t;
  65. typedef struct {
  66. uint32_t nodeid;
  67. pid_t pid;
  68. unsigned char sha1[20];
  69. uint32_t seq;
  70. size_t size;
  71. unsigned char payload[0];
  72. } msg_t;
  73. #define LOG_STR_SIZE 256
  74. typedef struct {
  75. char log[LOG_STR_SIZE];
  76. struct list_head list;
  77. } log_entry_t;
  78. static char big_and_buf[HOW_BIG_AND_BUF];
  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 corosync_cfg_handle_t cfg_handle = 0;
  83. static int32_t cpg_fd = -1;
  84. static int32_t cfg_fd = -1;
  85. static struct list_head config_chg_log_head;
  86. static struct list_head msg_log_head;
  87. static pid_t my_pid;
  88. static uint32_t my_nodeid;
  89. static int32_t my_seq;
  90. static int32_t use_zcb = QB_FALSE;
  91. static int32_t my_msgs_to_send;
  92. static int32_t my_msgs_sent;
  93. static int32_t total_stored_msgs = 0;
  94. static int32_t total_msgs_revd = 0;
  95. static int32_t in_cnchg = 0;
  96. static int32_t pcmk_test = 0;
  97. static void send_some_more_messages (void * unused);
  98. static char* err_status_string (char * buf, size_t buf_len, msg_status_t status)
  99. {
  100. switch (status) {
  101. case MSG_OK:
  102. strncpy (buf, "OK", buf_len);
  103. break;
  104. case MSG_NODEID_ERR:
  105. strncpy (buf, "NODEID_ERR", buf_len);
  106. break;
  107. case MSG_PID_ERR:
  108. strncpy (buf, "PID_ERR", buf_len);
  109. break;
  110. case MSG_SEQ_ERR:
  111. strncpy (buf, "SEQ_ERR", buf_len);
  112. break;
  113. case MSG_SIZE_ERR:
  114. strncpy (buf, "SIZE_ERR", buf_len);
  115. break;
  116. case MSG_SHA1_ERR:
  117. strncpy (buf, "SHA1_ERR", buf_len);
  118. break;
  119. default:
  120. strncpy (buf, "UNKNOWN_ERR", buf_len);
  121. break;
  122. }
  123. if (buf_len > 0) {
  124. buf[buf_len - 1] = '\0';
  125. }
  126. return buf;
  127. }
  128. static void delivery_callback (
  129. cpg_handle_t handle,
  130. const struct cpg_name *groupName,
  131. uint32_t nodeid,
  132. uint32_t pid,
  133. void *msg,
  134. size_t msg_len)
  135. {
  136. log_entry_t *log_pt;
  137. msg_t *msg_pt = (msg_t*)msg;
  138. msg_status_t status = MSG_OK;
  139. char status_buf[20];
  140. unsigned char sha1_compare[20];
  141. hash_state sha1_hash;
  142. if (record_messages_g == 0) {
  143. return;
  144. }
  145. msg_pt->seq = my_seq;
  146. my_seq++;
  147. if (nodeid != msg_pt->nodeid) {
  148. status = MSG_NODEID_ERR;
  149. }
  150. if (pid != msg_pt->pid) {
  151. status = MSG_PID_ERR;
  152. }
  153. if (msg_len != msg_pt->size) {
  154. status = MSG_SIZE_ERR;
  155. }
  156. sha1_init (&sha1_hash);
  157. sha1_process (&sha1_hash, msg_pt->payload, (msg_pt->size - sizeof (msg_t)));
  158. sha1_done (&sha1_hash, sha1_compare);
  159. if (memcmp (sha1_compare, msg_pt->sha1, 20) != 0) {
  160. syslog (LOG_ERR, "%s(); msg seq:%d; incorrect hash",
  161. __func__, msg_pt->seq);
  162. status = MSG_SHA1_ERR;
  163. }
  164. log_pt = malloc (sizeof(log_entry_t));
  165. list_init (&log_pt->list);
  166. snprintf (log_pt->log, LOG_STR_SIZE, "%d:%d:%d:%s;",
  167. msg_pt->nodeid, msg_pt->pid, msg_pt->seq,
  168. err_status_string (status_buf, 20, status));
  169. list_add_tail (&log_pt->list, &msg_log_head);
  170. total_stored_msgs++;
  171. total_msgs_revd++;
  172. if ((total_msgs_revd % 100) == 0) {
  173. syslog (LOG_INFO, "%s(); %d", __func__, total_msgs_revd);
  174. }
  175. }
  176. static void config_change_callback (
  177. cpg_handle_t handle,
  178. const struct cpg_name *groupName,
  179. const struct cpg_address *member_list, size_t member_list_entries,
  180. const struct cpg_address *left_list, size_t left_list_entries,
  181. const struct cpg_address *joined_list, size_t joined_list_entries)
  182. {
  183. int i;
  184. log_entry_t *log_pt;
  185. /* group_name,ip,pid,join|leave */
  186. for (i = 0; i < left_list_entries; i++) {
  187. syslog (LOG_INFO, "%s(%d) %d:%s left", __func__, record_config_events_g,
  188. left_list[i].nodeid, groupName->value);
  189. if (record_config_events_g > 0) {
  190. log_pt = malloc (sizeof(log_entry_t));
  191. list_init (&log_pt->list);
  192. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,left",
  193. groupName->value, left_list[i].nodeid,left_list[i].pid);
  194. list_add_tail(&log_pt->list, &config_chg_log_head);
  195. }
  196. }
  197. for (i = 0; i < joined_list_entries; i++) {
  198. syslog (LOG_INFO, "%s(%d) %d:%s joined", __func__, record_config_events_g,
  199. left_list[i].nodeid, groupName->value);
  200. if (record_config_events_g > 0) {
  201. log_pt = malloc (sizeof(log_entry_t));
  202. list_init (&log_pt->list);
  203. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,join",
  204. groupName->value, joined_list[i].nodeid,joined_list[i].pid);
  205. list_add_tail (&log_pt->list, &config_chg_log_head);
  206. }
  207. }
  208. if (pcmk_test == 1) {
  209. in_cnchg = 1;
  210. send_some_more_messages (NULL);
  211. in_cnchg = 0;
  212. }
  213. }
  214. static void my_shutdown_callback (corosync_cfg_handle_t handle,
  215. corosync_cfg_shutdown_flags_t flags)
  216. {
  217. syslog (LOG_CRIT, "%s flags:%d", __func__, flags);
  218. if (flags & COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST) {
  219. corosync_cfg_replyto_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_YES);
  220. }
  221. }
  222. static corosync_cfg_callbacks_t cfg_callbacks = {
  223. .corosync_cfg_shutdown_callback = my_shutdown_callback,
  224. .corosync_cfg_state_track_callback = NULL,
  225. };
  226. static cpg_callbacks_t callbacks = {
  227. .cpg_deliver_fn = delivery_callback,
  228. .cpg_confchg_fn = config_change_callback,
  229. };
  230. static void record_messages (void)
  231. {
  232. record_messages_g = 1;
  233. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_messages_g);
  234. }
  235. static void record_config_events (int sock)
  236. {
  237. char response[100];
  238. record_config_events_g = 1;
  239. syslog (LOG_INFO, "%s() record:%d", __func__, record_config_events_g);
  240. snprintf (response, 100, "%s", OK_STR);
  241. send (sock, response, strlen (response), 0);
  242. }
  243. static void read_config_event (int sock)
  244. {
  245. const char *empty = "None";
  246. struct list_head * list = config_chg_log_head.next;
  247. log_entry_t *entry;
  248. if (list != &config_chg_log_head) {
  249. entry = list_entry (list, log_entry_t, list);
  250. send (sock, entry->log, strlen (entry->log), 0);
  251. list_del (&entry->list);
  252. free (entry);
  253. } else {
  254. syslog (LOG_DEBUG,"%s() no events in list", __func__);
  255. send (sock, empty, strlen (empty), 0);
  256. }
  257. }
  258. static void read_messages (int sock, char* atmost_str)
  259. {
  260. struct list_head * list;
  261. log_entry_t *entry;
  262. int atmost = atoi (atmost_str);
  263. int packed = 0;
  264. if (atmost == 0)
  265. atmost = 1;
  266. if (atmost > (HOW_BIG_AND_BUF / LOG_STR_SIZE))
  267. atmost = (HOW_BIG_AND_BUF / LOG_STR_SIZE);
  268. // syslog (LOG_DEBUG, "%s() atmost %d; total_stored_msgs:%d",
  269. // __func__, atmost, total_stored_msgs);
  270. big_and_buf[0] = '\0';
  271. for (list = msg_log_head.next;
  272. (!list_empty (&msg_log_head) && packed < atmost); ) {
  273. entry = list_entry (list, log_entry_t, list);
  274. strcat (big_and_buf, entry->log);
  275. packed++;
  276. list = list->next;
  277. list_del (&entry->list);
  278. free (entry);
  279. total_stored_msgs--;
  280. }
  281. if (packed == 0) {
  282. strcpy (big_and_buf, "None");
  283. } else {
  284. syslog (LOG_INFO, "%s() sending %d; total_stored_msgs:%d; len:%d",
  285. __func__, packed, total_stored_msgs, (int)strlen (big_and_buf));
  286. }
  287. send (sock, big_and_buf, strlen (big_and_buf), 0);
  288. }
  289. static qb_loop_timer_handle more_messages_timer_handle;
  290. static void send_some_more_messages_later (void)
  291. {
  292. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  293. qb_loop_timer_add (
  294. ta_poll_handle_get(),
  295. QB_LOOP_MED,
  296. 300*QB_TIME_NS_IN_MSEC, NULL,
  297. send_some_more_messages,
  298. &more_messages_timer_handle);
  299. }
  300. static void send_some_more_messages_zcb (void)
  301. {
  302. msg_t *my_msg;
  303. int i;
  304. int send_now;
  305. size_t payload_size;
  306. size_t total_size;
  307. hash_state sha1_hash;
  308. cs_error_t res;
  309. cpg_flow_control_state_t fc_state;
  310. void *zcb_buffer;
  311. if (cpg_fd < 0)
  312. return;
  313. send_now = my_msgs_to_send;
  314. payload_size = (rand() % 100000);
  315. total_size = payload_size + sizeof (msg_t);
  316. cpg_zcb_alloc (cpg_handle, total_size, &zcb_buffer);
  317. my_msg = (msg_t*)zcb_buffer;
  318. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  319. my_msg->pid = my_pid;
  320. my_msg->nodeid = my_nodeid;
  321. my_msg->size = sizeof (msg_t) + payload_size;
  322. my_msg->seq = 0;
  323. for (i = 0; i < payload_size; i++) {
  324. my_msg->payload[i] = i;
  325. }
  326. sha1_init (&sha1_hash);
  327. sha1_process (&sha1_hash, my_msg->payload, payload_size);
  328. sha1_done (&sha1_hash, my_msg->sha1);
  329. for (i = 0; i < send_now; i++) {
  330. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  331. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  332. /* lets do this later */
  333. send_some_more_messages_later ();
  334. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  335. goto free_buffer;
  336. }
  337. res = cpg_zcb_mcast_joined (cpg_handle, CPG_TYPE_AGREED, zcb_buffer, total_size);
  338. if (res == CS_ERR_TRY_AGAIN) {
  339. /* lets do this later */
  340. send_some_more_messages_later ();
  341. // if (i > 0) {
  342. // syslog (LOG_INFO, "%s() TRY_AGAIN %d to send.",
  343. // __func__, my_msgs_to_send);
  344. // }
  345. goto free_buffer;
  346. } else if (res != CS_OK) {
  347. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  348. __func__, res);
  349. exit (-2);
  350. }
  351. my_msgs_sent++;
  352. my_msgs_to_send--;
  353. }
  354. free_buffer:
  355. cpg_zcb_free (cpg_handle, zcb_buffer);
  356. }
  357. #define cs_repeat(counter, max, code) do { \
  358. code; \
  359. if(res == CS_ERR_TRY_AGAIN) { \
  360. counter++; \
  361. sleep(counter); \
  362. } \
  363. } while(res == CS_ERR_TRY_AGAIN && counter < max)
  364. static unsigned char buffer[200000];
  365. static void send_some_more_messages_normal (void)
  366. {
  367. msg_t my_msg;
  368. struct iovec iov[2];
  369. int i;
  370. int send_now;
  371. size_t payload_size;
  372. hash_state sha1_hash;
  373. cs_error_t res;
  374. cpg_flow_control_state_t fc_state;
  375. int retries = 0;
  376. time_t before;
  377. if (cpg_fd < 0)
  378. return;
  379. send_now = my_msgs_to_send;
  380. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  381. my_msg.pid = my_pid;
  382. my_msg.nodeid = my_nodeid;
  383. payload_size = (rand() % 10000);
  384. my_msg.size = sizeof (msg_t) + payload_size;
  385. my_msg.seq = 0;
  386. for (i = 0; i < payload_size; i++) {
  387. buffer[i] = i;
  388. }
  389. sha1_init (&sha1_hash);
  390. sha1_process (&sha1_hash, buffer, payload_size);
  391. sha1_done (&sha1_hash, my_msg.sha1);
  392. iov[0].iov_len = sizeof (msg_t);
  393. iov[0].iov_base = &my_msg;
  394. iov[1].iov_len = payload_size;
  395. iov[1].iov_base = buffer;
  396. for (i = 0; i < send_now; i++) {
  397. if (in_cnchg && pcmk_test) {
  398. retries = 0;
  399. before = time(NULL);
  400. cs_repeat(retries, 30, res = cpg_mcast_joined(cpg_handle, CPG_TYPE_AGREED, iov, 2));
  401. if (retries > 20) {
  402. syslog (LOG_ERR, "%s() -> cs_repeat: blocked for :%lu secs.",
  403. __func__, (unsigned long)(time(NULL) - before));
  404. }
  405. if (res != CS_OK) {
  406. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d.",
  407. __func__, res);
  408. return;
  409. }
  410. } else {
  411. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  412. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  413. /* lets do this later */
  414. send_some_more_messages_later ();
  415. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  416. return;
  417. }
  418. res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2);
  419. if (res == CS_ERR_TRY_AGAIN) {
  420. /* lets do this later */
  421. send_some_more_messages_later ();
  422. if (i > 0) {
  423. syslog (LOG_INFO, "%s() TRY_AGAIN %d to send.",
  424. __func__, my_msgs_to_send);
  425. }
  426. return;
  427. } else if (res != CS_OK) {
  428. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  429. __func__, res);
  430. exit (-2);
  431. }
  432. }
  433. my_msgs_sent++;
  434. my_msgs_to_send--;
  435. }
  436. syslog (LOG_INFO, "%s() sent %d; to send %d.",
  437. __func__, my_msgs_sent, my_msgs_to_send);
  438. }
  439. static void send_some_more_messages (void * unused)
  440. {
  441. if (use_zcb) {
  442. send_some_more_messages_zcb ();
  443. } else {
  444. send_some_more_messages_normal ();
  445. }
  446. }
  447. static void msg_blaster (int sock, char* num_to_send_str)
  448. {
  449. my_msgs_to_send = atoi (num_to_send_str);
  450. my_msgs_sent = 0;
  451. my_seq = 1;
  452. my_pid = getpid();
  453. use_zcb = QB_FALSE;
  454. total_stored_msgs = 0;
  455. cpg_local_get (cpg_handle, &my_nodeid);
  456. /* control the limits */
  457. if (my_msgs_to_send <= 0)
  458. my_msgs_to_send = 1;
  459. if (my_msgs_to_send > 10000)
  460. my_msgs_to_send = 10000;
  461. send_some_more_messages_normal ();
  462. }
  463. static void context_test (int sock)
  464. {
  465. char response[100];
  466. char *cmp;
  467. cpg_context_set (cpg_handle, response);
  468. cpg_context_get (cpg_handle, (void**)&cmp);
  469. if (response != cmp) {
  470. snprintf (response, 100, "%s", FAIL_STR);
  471. }
  472. else {
  473. snprintf (response, 100, "%s", OK_STR);
  474. }
  475. send (sock, response, strlen (response), 0);
  476. }
  477. static void msg_blaster_zcb (int sock, char* num_to_send_str)
  478. {
  479. my_msgs_to_send = atoi (num_to_send_str);
  480. my_seq = 1;
  481. my_pid = getpid();
  482. use_zcb = QB_TRUE;
  483. total_stored_msgs = 0;
  484. cpg_local_get (cpg_handle, &my_nodeid);
  485. /* control the limits */
  486. if (my_msgs_to_send <= 0)
  487. my_msgs_to_send = 1;
  488. if (my_msgs_to_send > 10000)
  489. my_msgs_to_send = 10000;
  490. send_some_more_messages_zcb ();
  491. }
  492. static corosync_cfg_state_notification_t notification_buffer;
  493. static int cfg_dispatch_wrapper_fn (
  494. int fd,
  495. int revents,
  496. void *data)
  497. {
  498. cs_error_t error;
  499. if (revents & POLLHUP || revents & POLLERR) {
  500. syslog (LOG_ERR, "%s() got POLLHUP disconnecting from CFG", __func__);
  501. corosync_cfg_finalize(cfg_handle);
  502. cfg_handle = 0;
  503. qb_loop_poll_del (ta_poll_handle_get(), cfg_fd);
  504. close (cfg_fd);
  505. cfg_fd = -1;
  506. return -1;
  507. }
  508. error = corosync_cfg_dispatch (cfg_handle, CS_DISPATCH_ALL);
  509. if (error == CS_ERR_LIBRARY) {
  510. syslog (LOG_ERR, "%s() got LIB error disconnecting from CFG.", __func__);
  511. corosync_cfg_finalize(cfg_handle);
  512. cfg_handle = 0;
  513. qb_loop_poll_del (ta_poll_handle_get(), cfg_fd);
  514. close (cfg_fd);
  515. cfg_fd = -1;
  516. return -1;
  517. }
  518. return 0;
  519. }
  520. static int cpg_dispatch_wrapper_fn (
  521. int fd,
  522. int revents,
  523. void *data)
  524. {
  525. cs_error_t error;
  526. if (revents & POLLHUP || revents & POLLERR) {
  527. syslog (LOG_ERR, "%s() got POLLHUP disconnecting from CPG", __func__);
  528. cpg_finalize(cpg_handle);
  529. cpg_handle = 0;
  530. qb_loop_poll_del (ta_poll_handle_get(), cpg_fd);
  531. close (cpg_fd);
  532. cpg_fd = -1;
  533. return -1;
  534. }
  535. error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  536. if (error == CS_ERR_LIBRARY) {
  537. syslog (LOG_ERR, "%s() got LIB error disconnecting from CPG", __func__);
  538. cpg_finalize(cpg_handle);
  539. cpg_handle = 0;
  540. qb_loop_poll_del (ta_poll_handle_get(), cpg_fd);
  541. close (cpg_fd);
  542. cpg_fd = -1;
  543. return -1;
  544. }
  545. return 0;
  546. }
  547. static void do_command (int sock, char* func, char*args[], int num_args)
  548. {
  549. int result;
  550. char response[100];
  551. struct cpg_name group_name;
  552. if (parse_debug)
  553. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  554. if (strcmp ("cpg_mcast_joined",func) == 0) {
  555. struct iovec iov[5];
  556. int a;
  557. for (a = 0; a < num_args; a++) {
  558. iov[a].iov_base = args[a];
  559. iov[a].iov_len = strlen(args[a])+1;
  560. }
  561. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  562. } else if (strcmp ("cpg_join",func) == 0) {
  563. strcpy (group_name.value, args[0]);
  564. group_name.length = strlen(args[0]);
  565. result = cpg_join (cpg_handle, &group_name);
  566. if (result != CS_OK) {
  567. syslog (LOG_ERR,
  568. "Could not join process group, error %d\n", result);
  569. exit (1);
  570. }
  571. syslog (LOG_INFO, "called cpg_join()!");
  572. } else if (strcmp ("cpg_leave",func) == 0) {
  573. strcpy (group_name.value, args[0]);
  574. group_name.length = strlen(args[0]);
  575. result = cpg_leave (cpg_handle, &group_name);
  576. if (result != CS_OK) {
  577. syslog (LOG_ERR,
  578. "Could not leave process group, error %d\n", result);
  579. exit (1);
  580. }
  581. syslog (LOG_INFO, "called cpg_leave()!");
  582. } else if (strcmp ("cpg_initialize",func) == 0) {
  583. int retry_count = 0;
  584. result = cpg_initialize (&cpg_handle, &callbacks);
  585. while (result != CS_OK) {
  586. syslog (LOG_ERR,
  587. "cpg_initialize error %d (attempt %d)\n",
  588. result, retry_count);
  589. if (retry_count >= 3) {
  590. exit (1);
  591. }
  592. sleep(1);
  593. retry_count++;
  594. result = cpg_initialize (&cpg_handle, &callbacks);
  595. }
  596. cpg_fd_get (cpg_handle, &cpg_fd);
  597. qb_loop_poll_add (ta_poll_handle_get(),
  598. QB_LOOP_MED,
  599. cpg_fd,
  600. POLLIN|POLLNVAL,
  601. NULL,
  602. cpg_dispatch_wrapper_fn);
  603. } else if (strcmp ("cpg_local_get", func) == 0) {
  604. unsigned int local_nodeid;
  605. cpg_local_get (cpg_handle, &local_nodeid);
  606. snprintf (response, 100, "%u",local_nodeid);
  607. send (sock, response, strlen (response), 0);
  608. } else if (strcmp ("cpg_finalize", func) == 0) {
  609. if (cpg_handle > 0) {
  610. cpg_finalize (cpg_handle);
  611. cpg_handle = 0;
  612. }
  613. if (cpg_fd > 0) {
  614. qb_loop_poll_del (ta_poll_handle_get(), cpg_fd);
  615. cpg_fd = -1;
  616. }
  617. } else if (strcmp ("record_config_events", func) == 0) {
  618. record_config_events (sock);
  619. } else if (strcmp ("record_messages", func) == 0) {
  620. record_messages ();
  621. } else if (strcmp ("read_config_event", func) == 0) {
  622. read_config_event (sock);
  623. } else if (strcmp ("read_messages", func) == 0) {
  624. read_messages (sock, args[0]);
  625. } else if (strcmp ("msg_blaster_zcb", func) == 0) {
  626. msg_blaster_zcb (sock, args[0]);
  627. } else if (strcmp ("pcmk_test", func) == 0) {
  628. pcmk_test = 1;
  629. } else if (strcmp ("msg_blaster", func) == 0) {
  630. msg_blaster (sock, args[0]);
  631. } else if (strcmp ("context_test", func) == 0) {
  632. context_test (sock);
  633. } else if (strcmp ("are_you_ok_dude", func) == 0) {
  634. snprintf (response, 100, "%s", OK_STR);
  635. send (sock, response, strlen (response), 0);
  636. } else if (strcmp ("cfg_shutdown", func) == 0) {
  637. syslog (LOG_INFO,"%s calling %s() called!", __func__, func);
  638. result = corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  639. syslog (LOG_INFO,"%s() returned %d!", func, result);
  640. } else if (strcmp ("cfg_initialize",func) == 0) {
  641. int retry_count = 0;
  642. syslog (LOG_INFO,"%s %s() called!", __func__, func);
  643. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  644. while (result != CS_OK) {
  645. syslog (LOG_ERR,
  646. "cfg_initialize error %d (attempt %d)\n",
  647. result, retry_count);
  648. if (retry_count >= 3) {
  649. exit (1);
  650. }
  651. sleep(1);
  652. retry_count++;
  653. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  654. }
  655. syslog (LOG_INFO,"corosync_cfg_initialize() == %d", result);
  656. result = corosync_cfg_fd_get (cfg_handle, &cfg_fd);
  657. syslog (LOG_INFO,"corosync_cfg_fd_get() == %d", result);
  658. result = corosync_cfg_state_track (cfg_handle, 0, &notification_buffer);
  659. syslog (LOG_INFO,"corosync_cfg_state_track() == %d", result);
  660. qb_loop_poll_add (ta_poll_handle_get(),
  661. QB_LOOP_MED,
  662. cfg_fd,
  663. POLLIN|POLLNVAL,
  664. NULL,
  665. cfg_dispatch_wrapper_fn);
  666. } else {
  667. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  668. }
  669. }
  670. static void cs_ipcs_libqb_log_fn(const char *file_name,
  671. int32_t file_line,
  672. int32_t severity,
  673. const char *msg)
  674. {
  675. syslog(severity, "%s:%d %s() %s", file_name, file_line, __func__, msg);
  676. }
  677. static void my_pre_exit(void)
  678. {
  679. syslog (LOG_INFO, "%s PRE EXIT", __FILE__);
  680. if (cpg_handle > 0) {
  681. cpg_finalize (cpg_handle);
  682. cpg_handle = 0;
  683. }
  684. }
  685. int main (int argc, char *argv[])
  686. {
  687. int res = 0;
  688. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  689. syslog (LOG_INFO, "%s STARTING", __FILE__);
  690. qb_util_set_log_function (cs_ipcs_libqb_log_fn);
  691. list_init (&msg_log_head);
  692. list_init (&config_chg_log_head);
  693. res = test_agent_run (9034, do_command, my_pre_exit);
  694. syslog (LOG_INFO, "%s EXITING", __FILE__);
  695. return res;
  696. }