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