cpg_test_agent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 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. qb_log (LOG_ERR, "msg seq:%d; incorrect hash",
  161. 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. qb_log (LOG_INFO, "%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. for (i = 0; i < left_list_entries; i++) {
  187. if (record_config_events_g > 0) {
  188. log_pt = malloc (sizeof(log_entry_t));
  189. list_init (&log_pt->list);
  190. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,left",
  191. groupName->value, left_list[i].nodeid,left_list[i].pid);
  192. list_add_tail(&log_pt->list, &config_chg_log_head);
  193. }
  194. }
  195. for (i = 0; i < joined_list_entries; i++) {
  196. if (record_config_events_g > 0) {
  197. log_pt = malloc (sizeof(log_entry_t));
  198. list_init (&log_pt->list);
  199. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,join",
  200. groupName->value, joined_list[i].nodeid,joined_list[i].pid);
  201. list_add_tail (&log_pt->list, &config_chg_log_head);
  202. }
  203. }
  204. if (pcmk_test == 1) {
  205. in_cnchg = 1;
  206. send_some_more_messages (NULL);
  207. in_cnchg = 0;
  208. }
  209. }
  210. static void my_shutdown_callback (corosync_cfg_handle_t handle,
  211. corosync_cfg_shutdown_flags_t flags)
  212. {
  213. qb_log (LOG_CRIT, "flags:%d", flags);
  214. if (flags & COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST) {
  215. corosync_cfg_replyto_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_YES);
  216. }
  217. }
  218. static corosync_cfg_callbacks_t cfg_callbacks = {
  219. .corosync_cfg_shutdown_callback = my_shutdown_callback,
  220. .corosync_cfg_state_track_callback = NULL,
  221. };
  222. static cpg_callbacks_t callbacks = {
  223. .cpg_deliver_fn = delivery_callback,
  224. .cpg_confchg_fn = config_change_callback,
  225. };
  226. static void record_messages (void)
  227. {
  228. record_messages_g = 1;
  229. qb_log (LOG_DEBUG,"record:%d", record_messages_g);
  230. }
  231. static void record_config_events (int sock)
  232. {
  233. char response[100];
  234. record_config_events_g = 1;
  235. qb_log (LOG_INFO, "record:%d", record_config_events_g);
  236. snprintf (response, 100, "%s", OK_STR);
  237. send (sock, response, strlen (response), 0);
  238. }
  239. static void read_config_event (int sock)
  240. {
  241. const char *empty = "None";
  242. struct list_head * list = config_chg_log_head.next;
  243. log_entry_t *entry;
  244. if (list != &config_chg_log_head) {
  245. entry = list_entry (list, log_entry_t, list);
  246. send (sock, entry->log, strlen (entry->log), 0);
  247. list_del (&entry->list);
  248. free (entry);
  249. } else {
  250. qb_log (LOG_DEBUG, "no events in list");
  251. send (sock, empty, strlen (empty), 0);
  252. }
  253. }
  254. static void read_messages (int sock, char* atmost_str)
  255. {
  256. struct list_head * list;
  257. log_entry_t *entry;
  258. int atmost = atoi (atmost_str);
  259. int packed = 0;
  260. if (atmost == 0)
  261. atmost = 1;
  262. if (atmost > (HOW_BIG_AND_BUF / LOG_STR_SIZE))
  263. atmost = (HOW_BIG_AND_BUF / LOG_STR_SIZE);
  264. qb_log(LOG_DEBUG, "atmost %d; total_stored_msgs:%d",
  265. atmost, total_stored_msgs);
  266. big_and_buf[0] = '\0';
  267. for (list = msg_log_head.next;
  268. (!list_empty (&msg_log_head) && packed < atmost); ) {
  269. entry = list_entry (list, log_entry_t, list);
  270. strcat (big_and_buf, entry->log);
  271. packed++;
  272. list = list->next;
  273. list_del (&entry->list);
  274. free (entry);
  275. total_stored_msgs--;
  276. }
  277. if (packed == 0) {
  278. strcpy (big_and_buf, "None");
  279. } else {
  280. qb_log(LOG_INFO, "sending %d; total_stored_msgs:%d; len:%d",
  281. packed, total_stored_msgs, (int)strlen (big_and_buf));
  282. }
  283. send (sock, big_and_buf, strlen (big_and_buf), 0);
  284. }
  285. static qb_loop_timer_handle more_messages_timer_handle;
  286. static void send_some_more_messages_later (void)
  287. {
  288. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  289. qb_loop_timer_add (
  290. ta_poll_handle_get(),
  291. QB_LOOP_MED,
  292. 300*QB_TIME_NS_IN_MSEC, NULL,
  293. send_some_more_messages,
  294. &more_messages_timer_handle);
  295. }
  296. static void send_some_more_messages_zcb (void)
  297. {
  298. msg_t *my_msg;
  299. int i;
  300. int send_now;
  301. size_t payload_size;
  302. size_t total_size;
  303. hash_state sha1_hash;
  304. cs_error_t res;
  305. cpg_flow_control_state_t fc_state;
  306. void *zcb_buffer;
  307. if (cpg_fd < 0)
  308. return;
  309. send_now = my_msgs_to_send;
  310. payload_size = (rand() % 100000);
  311. total_size = payload_size + sizeof (msg_t);
  312. cpg_zcb_alloc (cpg_handle, total_size, &zcb_buffer);
  313. my_msg = (msg_t*)zcb_buffer;
  314. qb_log(LOG_DEBUG, "send_now:%d", send_now);
  315. my_msg->pid = my_pid;
  316. my_msg->nodeid = my_nodeid;
  317. my_msg->size = sizeof (msg_t) + payload_size;
  318. my_msg->seq = 0;
  319. for (i = 0; i < payload_size; i++) {
  320. my_msg->payload[i] = i;
  321. }
  322. sha1_init (&sha1_hash);
  323. sha1_process (&sha1_hash, my_msg->payload, payload_size);
  324. sha1_done (&sha1_hash, my_msg->sha1);
  325. for (i = 0; i < send_now; i++) {
  326. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  327. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  328. /* lets do this later */
  329. send_some_more_messages_later ();
  330. qb_log (LOG_INFO, "flow control enabled.");
  331. goto free_buffer;
  332. }
  333. res = cpg_zcb_mcast_joined (cpg_handle, CPG_TYPE_AGREED, zcb_buffer, total_size);
  334. if (res == CS_ERR_TRY_AGAIN) {
  335. /* lets do this later */
  336. send_some_more_messages_later ();
  337. // if (i > 0) {
  338. // qb_log (LOG_INFO, "TRY_AGAIN %d to send.",
  339. // my_msgs_to_send);
  340. // }
  341. goto free_buffer;
  342. } else if (res != CS_OK) {
  343. qb_log (LOG_ERR, "cpg_mcast_joined error:%d, exiting.",
  344. res);
  345. exit (-2);
  346. }
  347. my_msgs_sent++;
  348. my_msgs_to_send--;
  349. }
  350. free_buffer:
  351. cpg_zcb_free (cpg_handle, zcb_buffer);
  352. }
  353. #define cs_repeat(counter, max, code) do { \
  354. code; \
  355. if(res == CS_ERR_TRY_AGAIN) { \
  356. counter++; \
  357. sleep(counter); \
  358. } \
  359. } while(res == CS_ERR_TRY_AGAIN && counter < max)
  360. static unsigned char buffer[200000];
  361. static void send_some_more_messages_normal (void)
  362. {
  363. msg_t my_msg;
  364. struct iovec iov[2];
  365. int i;
  366. int send_now;
  367. size_t payload_size;
  368. hash_state sha1_hash;
  369. cs_error_t res;
  370. cpg_flow_control_state_t fc_state;
  371. int retries = 0;
  372. time_t before;
  373. if (cpg_fd < 0)
  374. return;
  375. send_now = my_msgs_to_send;
  376. qb_log (LOG_DEBUG,"send_now:%d", send_now);
  377. my_msg.pid = my_pid;
  378. my_msg.nodeid = my_nodeid;
  379. payload_size = (rand() % 10000);
  380. my_msg.size = sizeof (msg_t) + payload_size;
  381. my_msg.seq = 0;
  382. for (i = 0; i < payload_size; i++) {
  383. buffer[i] = i;
  384. }
  385. sha1_init (&sha1_hash);
  386. sha1_process (&sha1_hash, buffer, payload_size);
  387. sha1_done (&sha1_hash, my_msg.sha1);
  388. iov[0].iov_len = sizeof (msg_t);
  389. iov[0].iov_base = &my_msg;
  390. iov[1].iov_len = payload_size;
  391. iov[1].iov_base = buffer;
  392. for (i = 0; i < send_now; i++) {
  393. if (in_cnchg && pcmk_test) {
  394. retries = 0;
  395. before = time(NULL);
  396. cs_repeat(retries, 30, res = cpg_mcast_joined(cpg_handle, CPG_TYPE_AGREED, iov, 2));
  397. if (retries > 20) {
  398. qb_log (LOG_ERR, "cs_repeat: blocked for :%lu secs.",
  399. (unsigned long)(time(NULL) - before));
  400. }
  401. if (res != CS_OK) {
  402. qb_log (LOG_ERR, "cpg_mcast_joined error:%d.",
  403. res);
  404. return;
  405. }
  406. } else {
  407. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  408. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  409. /* lets do this later */
  410. send_some_more_messages_later ();
  411. qb_log (LOG_INFO, "flow control enabled.");
  412. return;
  413. }
  414. res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2);
  415. if (res == CS_ERR_TRY_AGAIN) {
  416. /* lets do this later */
  417. send_some_more_messages_later ();
  418. if (i > 0) {
  419. qb_log (LOG_INFO, "TRY_AGAIN %d to send.",
  420. my_msgs_to_send);
  421. }
  422. return;
  423. } else if (res != CS_OK) {
  424. qb_log (LOG_ERR, "cpg_mcast_joined error:%d, exiting.",
  425. res);
  426. exit (-2);
  427. }
  428. }
  429. my_msgs_sent++;
  430. my_msgs_to_send--;
  431. }
  432. qb_log (LOG_INFO, "sent %d; to send %d.",
  433. my_msgs_sent, my_msgs_to_send);
  434. }
  435. static void send_some_more_messages (void * unused)
  436. {
  437. if (use_zcb) {
  438. send_some_more_messages_zcb ();
  439. } else {
  440. send_some_more_messages_normal ();
  441. }
  442. }
  443. static void msg_blaster (int sock, char* num_to_send_str)
  444. {
  445. my_msgs_to_send = atoi (num_to_send_str);
  446. my_msgs_sent = 0;
  447. my_seq = 1;
  448. my_pid = getpid();
  449. use_zcb = QB_FALSE;
  450. total_stored_msgs = 0;
  451. cpg_local_get (cpg_handle, &my_nodeid);
  452. /* control the limits */
  453. if (my_msgs_to_send <= 0)
  454. my_msgs_to_send = 1;
  455. if (my_msgs_to_send > 10000)
  456. my_msgs_to_send = 10000;
  457. send_some_more_messages_normal ();
  458. }
  459. static void context_test (int sock)
  460. {
  461. char response[100];
  462. char *cmp;
  463. cpg_context_set (cpg_handle, response);
  464. cpg_context_get (cpg_handle, (void**)&cmp);
  465. if (response != cmp) {
  466. snprintf (response, 100, "%s", FAIL_STR);
  467. }
  468. else {
  469. snprintf (response, 100, "%s", OK_STR);
  470. }
  471. send (sock, response, strlen (response), 0);
  472. }
  473. static void msg_blaster_zcb (int sock, char* num_to_send_str)
  474. {
  475. my_msgs_to_send = atoi (num_to_send_str);
  476. my_seq = 1;
  477. my_pid = getpid();
  478. use_zcb = QB_TRUE;
  479. total_stored_msgs = 0;
  480. cpg_local_get (cpg_handle, &my_nodeid);
  481. /* control the limits */
  482. if (my_msgs_to_send <= 0)
  483. my_msgs_to_send = 1;
  484. if (my_msgs_to_send > 10000)
  485. my_msgs_to_send = 10000;
  486. send_some_more_messages_zcb ();
  487. }
  488. static corosync_cfg_state_notification_t notification_buffer;
  489. static int cfg_dispatch_wrapper_fn (
  490. int fd,
  491. int revents,
  492. void *data)
  493. {
  494. cs_error_t error;
  495. if (revents & POLLHUP || revents & POLLERR) {
  496. qb_log (LOG_ERR, "got POLLHUP disconnecting from CFG");
  497. corosync_cfg_finalize(cfg_handle);
  498. cfg_handle = 0;
  499. return -1;
  500. }
  501. error = corosync_cfg_dispatch (cfg_handle, CS_DISPATCH_ALL);
  502. if (error == CS_ERR_LIBRARY) {
  503. qb_log (LOG_ERR, "got LIB error disconnecting from CFG.");
  504. corosync_cfg_finalize(cfg_handle);
  505. cfg_handle = 0;
  506. return -1;
  507. }
  508. return 0;
  509. }
  510. static int cpg_dispatch_wrapper_fn (
  511. int fd,
  512. int revents,
  513. void *data)
  514. {
  515. cs_error_t error;
  516. if (revents & POLLHUP || revents & POLLERR) {
  517. qb_log (LOG_ERR, "got POLLHUP disconnecting from CPG");
  518. cpg_finalize(cpg_handle);
  519. cpg_handle = 0;
  520. return -1;
  521. }
  522. error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  523. if (error == CS_ERR_LIBRARY) {
  524. qb_log (LOG_ERR, "got LIB error disconnecting from CPG");
  525. cpg_finalize(cpg_handle);
  526. cpg_handle = 0;
  527. return -1;
  528. }
  529. return 0;
  530. }
  531. static void do_command (int sock, char* func, char*args[], int num_args)
  532. {
  533. int result;
  534. char response[100];
  535. struct cpg_name group_name;
  536. qb_log (LOG_TRACE, "RPC:%s() called.", func);
  537. if (strcmp ("cpg_mcast_joined",func) == 0) {
  538. struct iovec iov[5];
  539. int a;
  540. for (a = 0; a < num_args; a++) {
  541. iov[a].iov_base = args[a];
  542. iov[a].iov_len = strlen(args[a])+1;
  543. }
  544. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  545. } else if (strcmp ("cpg_join",func) == 0) {
  546. strcpy (group_name.value, args[0]);
  547. group_name.length = strlen(args[0]);
  548. result = cpg_join (cpg_handle, &group_name);
  549. if (result != CS_OK) {
  550. qb_log (LOG_ERR,
  551. "Could not join process group, error %d", result);
  552. exit (1);
  553. }
  554. qb_log (LOG_INFO, "called cpg_join()!");
  555. } else if (strcmp ("cpg_leave",func) == 0) {
  556. strcpy (group_name.value, args[0]);
  557. group_name.length = strlen(args[0]);
  558. result = cpg_leave (cpg_handle, &group_name);
  559. if (result != CS_OK) {
  560. qb_log (LOG_ERR,
  561. "Could not leave process group, error %d", result);
  562. exit (1);
  563. }
  564. qb_log (LOG_INFO, "called cpg_leave()!");
  565. } else if (strcmp ("cpg_initialize",func) == 0) {
  566. int retry_count = 0;
  567. result = cpg_initialize (&cpg_handle, &callbacks);
  568. while (result != CS_OK) {
  569. qb_log (LOG_ERR,
  570. "cpg_initialize error %d (attempt %d)",
  571. result, retry_count);
  572. if (retry_count >= 3) {
  573. exit (1);
  574. }
  575. sleep(1);
  576. retry_count++;
  577. result = cpg_initialize (&cpg_handle, &callbacks);
  578. }
  579. cpg_fd_get (cpg_handle, &cpg_fd);
  580. qb_loop_poll_add (ta_poll_handle_get(),
  581. QB_LOOP_MED,
  582. cpg_fd,
  583. POLLIN|POLLNVAL,
  584. NULL,
  585. cpg_dispatch_wrapper_fn);
  586. } else if (strcmp ("cpg_local_get", func) == 0) {
  587. unsigned int local_nodeid;
  588. cpg_local_get (cpg_handle, &local_nodeid);
  589. snprintf (response, 100, "%u",local_nodeid);
  590. send (sock, response, strlen (response), 0);
  591. } else if (strcmp ("cpg_finalize", func) == 0) {
  592. if (cpg_handle > 0) {
  593. cpg_finalize (cpg_handle);
  594. cpg_handle = 0;
  595. }
  596. } else if (strcmp ("record_config_events", func) == 0) {
  597. record_config_events (sock);
  598. } else if (strcmp ("record_messages", func) == 0) {
  599. record_messages ();
  600. } else if (strcmp ("read_config_event", func) == 0) {
  601. read_config_event (sock);
  602. } else if (strcmp ("read_messages", func) == 0) {
  603. read_messages (sock, args[0]);
  604. } else if (strcmp ("msg_blaster_zcb", func) == 0) {
  605. msg_blaster_zcb (sock, args[0]);
  606. } else if (strcmp ("pcmk_test", func) == 0) {
  607. pcmk_test = 1;
  608. } else if (strcmp ("msg_blaster", func) == 0) {
  609. msg_blaster (sock, args[0]);
  610. } else if (strcmp ("context_test", func) == 0) {
  611. context_test (sock);
  612. } else if (strcmp ("are_you_ok_dude", func) == 0) {
  613. snprintf (response, 100, "%s", OK_STR);
  614. send (sock, response, strlen (response), 0);
  615. } else if (strcmp ("cfg_shutdown", func) == 0) {
  616. qb_log (LOG_INFO, "calling %s() called!", func);
  617. result = corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  618. qb_log (LOG_INFO,"%s() returned %d!", func, result);
  619. } else if (strcmp ("cfg_initialize",func) == 0) {
  620. int retry_count = 0;
  621. qb_log (LOG_INFO,"%s() called!", func);
  622. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  623. while (result != CS_OK) {
  624. qb_log (LOG_ERR,
  625. "cfg_initialize error %d (attempt %d)",
  626. result, retry_count);
  627. if (retry_count >= 3) {
  628. exit (1);
  629. }
  630. sleep(1);
  631. retry_count++;
  632. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  633. }
  634. qb_log (LOG_INFO,"corosync_cfg_initialize() == %d", result);
  635. result = corosync_cfg_fd_get (cfg_handle, &cfg_fd);
  636. qb_log (LOG_INFO,"corosync_cfg_fd_get() == %d", result);
  637. result = corosync_cfg_state_track (cfg_handle, 0, &notification_buffer);
  638. qb_log (LOG_INFO,"corosync_cfg_state_track() == %d", result);
  639. qb_loop_poll_add (ta_poll_handle_get(),
  640. QB_LOOP_MED,
  641. cfg_fd,
  642. POLLIN|POLLNVAL,
  643. NULL,
  644. cfg_dispatch_wrapper_fn);
  645. } else {
  646. qb_log(LOG_ERR, "RPC:%s not supported!", func);
  647. }
  648. }
  649. static void my_pre_exit(void)
  650. {
  651. qb_log (LOG_INFO, "%s PRE EXIT", __FILE__);
  652. if (cpg_handle > 0) {
  653. cpg_finalize (cpg_handle);
  654. cpg_handle = 0;
  655. }
  656. if (cfg_handle > 0) {
  657. corosync_cfg_finalize (cfg_handle);
  658. cfg_handle = 0;
  659. }
  660. }
  661. int
  662. main(int argc, char *argv[])
  663. {
  664. list_init (&msg_log_head);
  665. list_init (&config_chg_log_head);
  666. return test_agent_run ("cpg_test_agent", 9034, do_command, my_pre_exit);
  667. }