cpg_test_agent.c 20 KB

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