cpg_test_agent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 <corosync/list.h>
  49. #include <qb/qbdefs.h>
  50. #include <qb/qbutil.h>
  51. #include <qb/qbloop.h>
  52. #include <qb/qblog.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 80
  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*
  99. err_status_string (char * buf, size_t buf_len, msg_status_t status)
  100. {
  101. switch (status) {
  102. case MSG_OK:
  103. strncpy (buf, "OK", buf_len);
  104. break;
  105. case MSG_NODEID_ERR:
  106. strncpy (buf, "NODEID_ERR", buf_len);
  107. break;
  108. case MSG_PID_ERR:
  109. strncpy (buf, "PID_ERR", buf_len);
  110. break;
  111. case MSG_SEQ_ERR:
  112. strncpy (buf, "SEQ_ERR", buf_len);
  113. break;
  114. case MSG_SIZE_ERR:
  115. strncpy (buf, "SIZE_ERR", buf_len);
  116. break;
  117. case MSG_SHA1_ERR:
  118. strncpy (buf, "SHA1_ERR", buf_len);
  119. break;
  120. default:
  121. strncpy (buf, "UNKNOWN_ERR", buf_len);
  122. break;
  123. }
  124. if (buf_len > 0) {
  125. buf[buf_len - 1] = '\0';
  126. }
  127. return buf;
  128. }
  129. static void delivery_callback (
  130. cpg_handle_t handle,
  131. const struct cpg_name *groupName,
  132. uint32_t nodeid,
  133. uint32_t pid,
  134. void *msg,
  135. size_t msg_len)
  136. {
  137. log_entry_t *log_pt;
  138. msg_t *msg_pt = (msg_t*)msg;
  139. msg_status_t status = MSG_OK;
  140. char status_buf[20];
  141. unsigned char sha1_compare[20];
  142. hash_state sha1_hash;
  143. if (record_messages_g == 0) {
  144. return;
  145. }
  146. if (nodeid != msg_pt->nodeid) {
  147. status = MSG_NODEID_ERR;
  148. }
  149. if (pid != msg_pt->pid) {
  150. status = MSG_PID_ERR;
  151. }
  152. if (msg_len != msg_pt->size) {
  153. status = MSG_SIZE_ERR;
  154. }
  155. sha1_init (&sha1_hash);
  156. sha1_process (&sha1_hash, msg_pt->payload, (msg_pt->size - sizeof (msg_t)));
  157. sha1_done (&sha1_hash, sha1_compare);
  158. if (memcmp (sha1_compare, msg_pt->sha1, 20) != 0) {
  159. qb_log (LOG_ERR, "msg seq:%d; incorrect hash",
  160. msg_pt->seq);
  161. status = MSG_SHA1_ERR;
  162. }
  163. log_pt = malloc (sizeof(log_entry_t));
  164. list_init (&log_pt->list);
  165. snprintf (log_pt->log, LOG_STR_SIZE, "%d:%d:%d:%s;",
  166. msg_pt->nodeid, msg_pt->seq, my_seq,
  167. err_status_string (status_buf, 20, status));
  168. list_add_tail (&log_pt->list, &msg_log_head);
  169. total_stored_msgs++;
  170. total_msgs_revd++;
  171. my_seq++;
  172. if ((total_msgs_revd % 1000) == 0) {
  173. qb_log (LOG_INFO, "rx %d", 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. if (record_config_events_g > 0) {
  187. qb_log (LOG_INFO, "got cpg event[recording] for group %s", groupName->value);
  188. } else {
  189. qb_log (LOG_INFO, "got cpg event[ignoring] for group %s", groupName->value);
  190. }
  191. for (i = 0; i < left_list_entries; i++) {
  192. if (record_config_events_g > 0) {
  193. log_pt = malloc (sizeof(log_entry_t));
  194. list_init (&log_pt->list);
  195. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,left",
  196. groupName->value, left_list[i].nodeid,left_list[i].pid);
  197. list_add_tail(&log_pt->list, &config_chg_log_head);
  198. qb_log (LOG_INFO, "cpg event %s", log_pt->log);
  199. }
  200. }
  201. for (i = 0; i < joined_list_entries; i++) {
  202. if (record_config_events_g > 0) {
  203. log_pt = malloc (sizeof(log_entry_t));
  204. list_init (&log_pt->list);
  205. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,join",
  206. groupName->value, joined_list[i].nodeid,joined_list[i].pid);
  207. list_add_tail (&log_pt->list, &config_chg_log_head);
  208. qb_log (LOG_INFO, "cpg event %s", log_pt->log);
  209. }
  210. }
  211. if (pcmk_test == 1) {
  212. in_cnchg = 1;
  213. send_some_more_messages (NULL);
  214. in_cnchg = 0;
  215. }
  216. }
  217. static void my_shutdown_callback (corosync_cfg_handle_t handle,
  218. corosync_cfg_shutdown_flags_t flags)
  219. {
  220. qb_log (LOG_CRIT, "flags:%d", flags);
  221. if (flags & COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST) {
  222. corosync_cfg_replyto_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_YES);
  223. }
  224. }
  225. static corosync_cfg_callbacks_t cfg_callbacks = {
  226. .corosync_cfg_shutdown_callback = my_shutdown_callback,
  227. };
  228. static cpg_callbacks_t callbacks = {
  229. .cpg_deliver_fn = delivery_callback,
  230. .cpg_confchg_fn = config_change_callback,
  231. };
  232. static void record_messages (void)
  233. {
  234. record_messages_g = 1;
  235. qb_log (LOG_INFO, "record:%d", record_messages_g);
  236. }
  237. static void record_config_events (int sock)
  238. {
  239. char response[100];
  240. record_config_events_g = 1;
  241. qb_log (LOG_INFO, "record:%d", record_config_events_g);
  242. snprintf (response, 100, "%s", OK_STR);
  243. send (sock, response, strlen (response), 0);
  244. }
  245. static void read_config_event (int sock)
  246. {
  247. const char *empty = "None";
  248. struct list_head * list = config_chg_log_head.next;
  249. log_entry_t *entry;
  250. if (list != &config_chg_log_head) {
  251. entry = list_entry (list, log_entry_t, list);
  252. send (sock, entry->log, strlen (entry->log), 0);
  253. list_del (&entry->list);
  254. free (entry);
  255. } else {
  256. qb_log (LOG_DEBUG, "no events in list");
  257. send (sock, empty, strlen (empty), 0);
  258. }
  259. }
  260. static void read_messages (int sock, char* atmost_str)
  261. {
  262. struct list_head * list;
  263. log_entry_t *entry;
  264. int atmost = atoi (atmost_str);
  265. int packed = 0;
  266. ssize_t rc;
  267. if (atmost == 0)
  268. atmost = 1;
  269. if (atmost > (HOW_BIG_AND_BUF / LOG_STR_SIZE))
  270. atmost = (HOW_BIG_AND_BUF / LOG_STR_SIZE);
  271. big_and_buf[0] = '\0';
  272. for (list = msg_log_head.next;
  273. (!list_empty (&msg_log_head) && packed < atmost); ) {
  274. entry = list_entry (list, log_entry_t, list);
  275. strcat (big_and_buf, entry->log);
  276. packed++;
  277. list = list->next;
  278. list_del (&entry->list);
  279. free (entry);
  280. total_stored_msgs--;
  281. }
  282. if (packed == 0) {
  283. strcpy (big_and_buf, "None");
  284. } else {
  285. if ((total_stored_msgs % 1000) == 0) {
  286. qb_log(LOG_INFO, "sending %d; total_stored_msgs:%d; len:%d",
  287. packed, total_stored_msgs, (int)strlen (big_and_buf));
  288. }
  289. }
  290. rc = send (sock, big_and_buf, strlen (big_and_buf), 0);
  291. assert(rc = strlen (big_and_buf));
  292. }
  293. static qb_loop_timer_handle more_messages_timer_handle;
  294. static void send_some_more_messages_later (void)
  295. {
  296. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  297. qb_loop_timer_add (
  298. ta_poll_handle_get(),
  299. QB_LOOP_MED,
  300. 300*QB_TIME_NS_IN_MSEC, NULL,
  301. send_some_more_messages,
  302. &more_messages_timer_handle);
  303. }
  304. static void send_some_more_messages_zcb (void)
  305. {
  306. msg_t *my_msg;
  307. int i;
  308. int send_now;
  309. size_t payload_size;
  310. size_t total_size;
  311. hash_state sha1_hash;
  312. cs_error_t res;
  313. cpg_flow_control_state_t fc_state;
  314. void *zcb_buffer;
  315. if (cpg_fd < 0)
  316. return;
  317. send_now = my_msgs_to_send;
  318. payload_size = (rand() % 100000);
  319. total_size = payload_size + sizeof (msg_t);
  320. cpg_zcb_alloc (cpg_handle, total_size, &zcb_buffer);
  321. my_msg = (msg_t*)zcb_buffer;
  322. qb_log(LOG_DEBUG, "send_now:%d", send_now);
  323. my_msg->pid = my_pid;
  324. my_msg->nodeid = my_nodeid;
  325. my_msg->size = sizeof (msg_t) + payload_size;
  326. my_msg->seq = my_msgs_sent;
  327. for (i = 0; i < payload_size; i++) {
  328. my_msg->payload[i] = i;
  329. }
  330. sha1_init (&sha1_hash);
  331. sha1_process (&sha1_hash, my_msg->payload, payload_size);
  332. sha1_done (&sha1_hash, my_msg->sha1);
  333. for (i = 0; i < send_now; i++) {
  334. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  335. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  336. /* lets do this later */
  337. send_some_more_messages_later ();
  338. qb_log (LOG_INFO, "flow control enabled.");
  339. goto free_buffer;
  340. }
  341. res = cpg_zcb_mcast_joined (cpg_handle, CPG_TYPE_AGREED, zcb_buffer, total_size);
  342. if (res == CS_ERR_TRY_AGAIN) {
  343. /* lets do this later */
  344. send_some_more_messages_later ();
  345. goto free_buffer;
  346. } else if (res != CS_OK) {
  347. qb_log (LOG_ERR, "cpg_mcast_joined error:%d, exiting.",
  348. 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. qb_log (LOG_TRACE, "send_now:%d", 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 = my_msgs_sent;
  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. qb_log (LOG_ERR, "cs_repeat: blocked for :%lu secs.",
  403. (unsigned long)(time(NULL) - before));
  404. }
  405. if (res != CS_OK) {
  406. qb_log (LOG_ERR, "cpg_mcast_joined error:%d.",
  407. 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. qb_log (LOG_INFO, "flow control enabled.");
  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. qb_log (LOG_INFO, "TRY_AGAIN %d to send.",
  424. my_msgs_to_send);
  425. }
  426. return;
  427. } else if (res != CS_OK) {
  428. qb_log (LOG_ERR, "cpg_mcast_joined error:%d, exiting.",
  429. res);
  430. exit (-2);
  431. }
  432. }
  433. my_msgs_sent++;
  434. my_msg.seq = my_msgs_sent;
  435. my_msgs_to_send--;
  436. }
  437. qb_log (LOG_TRACE, "sent %d; to send %d.",
  438. my_msgs_sent, my_msgs_to_send);
  439. }
  440. static void send_some_more_messages (void * unused)
  441. {
  442. if (use_zcb) {
  443. send_some_more_messages_zcb ();
  444. } else {
  445. send_some_more_messages_normal ();
  446. }
  447. }
  448. static void msg_blaster (int sock, char* num_to_send_str)
  449. {
  450. my_msgs_to_send = atoi (num_to_send_str);
  451. my_msgs_sent = 0;
  452. my_seq = 1;
  453. my_pid = getpid();
  454. use_zcb = QB_FALSE;
  455. total_stored_msgs = 0;
  456. cpg_local_get (cpg_handle, &my_nodeid);
  457. /* control the limits */
  458. if (my_msgs_to_send <= 0)
  459. my_msgs_to_send = 1;
  460. if (my_msgs_to_send > 10000)
  461. my_msgs_to_send = 10000;
  462. send_some_more_messages_normal ();
  463. }
  464. static void context_test (int sock)
  465. {
  466. char response[100];
  467. char *cmp;
  468. cpg_context_set (cpg_handle, response);
  469. cpg_context_get (cpg_handle, (void**)&cmp);
  470. if (response != cmp) {
  471. snprintf (response, 100, "%s", FAIL_STR);
  472. }
  473. else {
  474. snprintf (response, 100, "%s", OK_STR);
  475. }
  476. send (sock, response, strlen (response), 0);
  477. }
  478. static void msg_blaster_zcb (int sock, char* num_to_send_str)
  479. {
  480. my_msgs_to_send = atoi (num_to_send_str);
  481. my_seq = 1;
  482. my_pid = getpid();
  483. use_zcb = QB_TRUE;
  484. total_stored_msgs = 0;
  485. cpg_local_get (cpg_handle, &my_nodeid);
  486. /* control the limits */
  487. if (my_msgs_to_send <= 0)
  488. my_msgs_to_send = 1;
  489. if (my_msgs_to_send > 10000)
  490. my_msgs_to_send = 10000;
  491. send_some_more_messages_zcb ();
  492. }
  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. qb_log (LOG_ERR, "got POLLHUP disconnecting from CFG");
  501. corosync_cfg_finalize(cfg_handle);
  502. cfg_handle = 0;
  503. return -1;
  504. }
  505. error = corosync_cfg_dispatch (cfg_handle, CS_DISPATCH_ALL);
  506. if (error == CS_ERR_LIBRARY) {
  507. qb_log (LOG_ERR, "got LIB error disconnecting from CFG.");
  508. corosync_cfg_finalize(cfg_handle);
  509. cfg_handle = 0;
  510. return -1;
  511. }
  512. return 0;
  513. }
  514. static int cpg_dispatch_wrapper_fn (
  515. int fd,
  516. int revents,
  517. void *data)
  518. {
  519. cs_error_t error;
  520. if (revents & POLLHUP || revents & POLLERR) {
  521. qb_log (LOG_ERR, "got POLLHUP disconnecting from CPG");
  522. cpg_finalize(cpg_handle);
  523. cpg_handle = 0;
  524. return -1;
  525. }
  526. error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  527. if (error == CS_ERR_LIBRARY) {
  528. qb_log (LOG_ERR, "got LIB error disconnecting from CPG");
  529. cpg_finalize(cpg_handle);
  530. cpg_handle = 0;
  531. return -1;
  532. }
  533. return 0;
  534. }
  535. static void do_command (int sock, char* func, char*args[], int num_args)
  536. {
  537. int result;
  538. char response[100];
  539. struct cpg_name group_name;
  540. qb_log (LOG_TRACE, "RPC:%s() called.", func);
  541. if (strcmp ("cpg_mcast_joined",func) == 0) {
  542. struct iovec iov[5];
  543. int a;
  544. for (a = 0; a < num_args; a++) {
  545. iov[a].iov_base = args[a];
  546. iov[a].iov_len = strlen(args[a])+1;
  547. }
  548. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  549. } else if (strcmp ("cpg_join",func) == 0) {
  550. strcpy (group_name.value, args[0]);
  551. group_name.length = strlen(args[0]);
  552. result = cpg_join (cpg_handle, &group_name);
  553. if (result != CS_OK) {
  554. qb_log (LOG_ERR,
  555. "Could not join process group, error %d", result);
  556. exit (1);
  557. }
  558. qb_log (LOG_INFO, "called cpg_join(%s)!", group_name.value);
  559. } else if (strcmp ("cpg_leave",func) == 0) {
  560. strcpy (group_name.value, args[0]);
  561. group_name.length = strlen(args[0]);
  562. result = cpg_leave (cpg_handle, &group_name);
  563. if (result != CS_OK) {
  564. qb_log (LOG_ERR,
  565. "Could not leave process group, error %d", result);
  566. exit (1);
  567. }
  568. qb_log (LOG_INFO, "called cpg_leave(%s)!", group_name.value);
  569. } else if (strcmp ("cpg_initialize",func) == 0) {
  570. int retry_count = 0;
  571. result = cpg_initialize (&cpg_handle, &callbacks);
  572. while (result != CS_OK) {
  573. qb_log (LOG_ERR,
  574. "cpg_initialize error %d (attempt %d)",
  575. result, retry_count);
  576. if (retry_count >= 3) {
  577. exit (1);
  578. }
  579. sleep(1);
  580. retry_count++;
  581. result = cpg_initialize (&cpg_handle, &callbacks);
  582. }
  583. cpg_fd_get (cpg_handle, &cpg_fd);
  584. qb_loop_poll_add (ta_poll_handle_get(),
  585. QB_LOOP_MED,
  586. cpg_fd,
  587. POLLIN|POLLNVAL,
  588. NULL,
  589. cpg_dispatch_wrapper_fn);
  590. } else if (strcmp ("cpg_local_get", func) == 0) {
  591. unsigned int local_nodeid;
  592. cpg_local_get (cpg_handle, &local_nodeid);
  593. snprintf (response, 100, "%u",local_nodeid);
  594. send (sock, response, strlen (response), 0);
  595. } else if (strcmp ("cpg_finalize", func) == 0) {
  596. if (cpg_handle > 0) {
  597. cpg_finalize (cpg_handle);
  598. cpg_handle = 0;
  599. }
  600. } else if (strcmp ("record_config_events", func) == 0) {
  601. record_config_events (sock);
  602. } else if (strcmp ("record_messages", func) == 0) {
  603. record_messages ();
  604. } else if (strcmp ("read_config_event", func) == 0) {
  605. read_config_event (sock);
  606. } else if (strcmp ("read_messages", func) == 0) {
  607. read_messages (sock, args[0]);
  608. } else if (strcmp ("msg_blaster_zcb", func) == 0) {
  609. msg_blaster_zcb (sock, args[0]);
  610. } else if (strcmp ("pcmk_test", func) == 0) {
  611. pcmk_test = 1;
  612. } else if (strcmp ("msg_blaster", func) == 0) {
  613. msg_blaster (sock, args[0]);
  614. } else if (strcmp ("context_test", func) == 0) {
  615. context_test (sock);
  616. } else if (strcmp ("are_you_ok_dude", func) == 0) {
  617. snprintf (response, 100, "%s", OK_STR);
  618. send (sock, response, strlen (response), 0);
  619. } else if (strcmp ("cfg_shutdown", func) == 0) {
  620. qb_log (LOG_INFO, "calling %s() called!", func);
  621. result = corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  622. qb_log (LOG_INFO,"%s() returned %d!", func, result);
  623. } else if (strcmp ("cfg_initialize",func) == 0) {
  624. int retry_count = 0;
  625. qb_log (LOG_INFO,"%s() called!", func);
  626. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  627. while (result != CS_OK) {
  628. qb_log (LOG_ERR,
  629. "cfg_initialize error %d (attempt %d)",
  630. result, retry_count);
  631. if (retry_count >= 3) {
  632. exit (1);
  633. }
  634. sleep(1);
  635. retry_count++;
  636. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  637. }
  638. qb_log (LOG_INFO,"corosync_cfg_initialize() == %d", result);
  639. result = corosync_cfg_fd_get (cfg_handle, &cfg_fd);
  640. qb_log (LOG_INFO,"corosync_cfg_fd_get() == %d", result);
  641. qb_loop_poll_add (ta_poll_handle_get(),
  642. QB_LOOP_MED,
  643. cfg_fd,
  644. POLLIN|POLLNVAL,
  645. NULL,
  646. cfg_dispatch_wrapper_fn);
  647. } else {
  648. qb_log(LOG_ERR, "RPC:%s not supported!", func);
  649. }
  650. }
  651. static void my_pre_exit(void)
  652. {
  653. qb_log (LOG_INFO, "%s PRE EXIT", __FILE__);
  654. if (cpg_handle > 0) {
  655. cpg_finalize (cpg_handle);
  656. cpg_handle = 0;
  657. }
  658. if (cfg_handle > 0) {
  659. corosync_cfg_finalize (cfg_handle);
  660. cfg_handle = 0;
  661. }
  662. }
  663. int
  664. main(int argc, char *argv[])
  665. {
  666. list_init (&msg_log_head);
  667. list_init (&config_chg_log_head);
  668. return test_agent_run ("cpg_test_agent", 9034, do_command, my_pre_exit);
  669. }