cpg_test_agent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld (asalkeld@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <errno.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <assert.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <netdb.h>
  45. #include <syslog.h>
  46. #include <poll.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <corosync/list.h>
  50. #include <qb/qbdefs.h>
  51. #include <qb/qbloop.h>
  52. #include <corosync/cpg.h>
  53. #include <corosync/cfg.h>
  54. #include "../../exec/crypto.h"
  55. #include "common_test_agent.h"
  56. typedef enum {
  57. MSG_OK,
  58. MSG_NODEID_ERR,
  59. MSG_PID_ERR,
  60. MSG_SEQ_ERR,
  61. MSG_SIZE_ERR,
  62. MSG_SHA1_ERR,
  63. } msg_status_t;
  64. typedef struct {
  65. uint32_t nodeid;
  66. pid_t pid;
  67. unsigned char sha1[20];
  68. uint32_t seq;
  69. size_t size;
  70. unsigned char payload[0];
  71. } msg_t;
  72. #define LOG_STR_SIZE 256
  73. typedef struct {
  74. char log[LOG_STR_SIZE];
  75. struct list_head list;
  76. } log_entry_t;
  77. static char big_and_buf[HOW_BIG_AND_BUF];
  78. static int32_t record_config_events_g = 0;
  79. static int32_t record_messages_g = 0;
  80. static cpg_handle_t cpg_handle = 0;
  81. static corosync_cfg_handle_t cfg_handle = 0;
  82. static int32_t cpg_fd = -1;
  83. static int32_t cfg_fd = -1;
  84. static struct list_head config_chg_log_head;
  85. static struct list_head msg_log_head;
  86. static pid_t my_pid;
  87. static uint32_t my_nodeid;
  88. static int32_t my_seq;
  89. static int32_t use_zcb = QB_FALSE;
  90. static int32_t my_msgs_to_send;
  91. static int32_t total_stored_msgs = 0;
  92. static int32_t in_cnchg = 0;
  93. static int32_t pcmk_test = 0;
  94. static void send_some_more_messages (void * unused);
  95. static char* err_status_string (char * buf, size_t buf_len, msg_status_t status)
  96. {
  97. switch (status) {
  98. case MSG_OK:
  99. strncpy (buf, "OK", buf_len);
  100. break;
  101. case MSG_NODEID_ERR:
  102. strncpy (buf, "NODEID_ERR", buf_len);
  103. break;
  104. case MSG_PID_ERR:
  105. strncpy (buf, "PID_ERR", buf_len);
  106. break;
  107. case MSG_SEQ_ERR:
  108. strncpy (buf, "SEQ_ERR", buf_len);
  109. break;
  110. case MSG_SIZE_ERR:
  111. strncpy (buf, "SIZE_ERR", buf_len);
  112. break;
  113. case MSG_SHA1_ERR:
  114. strncpy (buf, "SHA1_ERR", buf_len);
  115. break;
  116. default:
  117. strncpy (buf, "UNKNOWN_ERR", buf_len);
  118. break;
  119. }
  120. if (buf_len > 0) {
  121. buf[buf_len - 1] = '\0';
  122. }
  123. return buf;
  124. }
  125. static void delivery_callback (
  126. cpg_handle_t handle,
  127. const struct cpg_name *groupName,
  128. uint32_t nodeid,
  129. uint32_t pid,
  130. void *msg,
  131. size_t msg_len)
  132. {
  133. log_entry_t *log_pt;
  134. msg_t *msg_pt = (msg_t*)msg;
  135. msg_status_t status = MSG_OK;
  136. char status_buf[20];
  137. unsigned char sha1_compare[20];
  138. hash_state sha1_hash;
  139. if (record_messages_g == 0) {
  140. return;
  141. }
  142. msg_pt->seq = my_seq;
  143. my_seq++;
  144. if (nodeid != msg_pt->nodeid) {
  145. status = MSG_NODEID_ERR;
  146. }
  147. if (pid != msg_pt->pid) {
  148. status = MSG_PID_ERR;
  149. }
  150. if (msg_len != msg_pt->size) {
  151. status = MSG_SIZE_ERR;
  152. }
  153. sha1_init (&sha1_hash);
  154. sha1_process (&sha1_hash, msg_pt->payload, (msg_pt->size - sizeof (msg_t)));
  155. sha1_done (&sha1_hash, sha1_compare);
  156. if (memcmp (sha1_compare, msg_pt->sha1, 20) != 0) {
  157. syslog (LOG_ERR, "%s(); msg seq:%d; incorrect hash",
  158. __func__, msg_pt->seq);
  159. status = MSG_SHA1_ERR;
  160. }
  161. log_pt = malloc (sizeof(log_entry_t));
  162. list_init (&log_pt->list);
  163. snprintf (log_pt->log, LOG_STR_SIZE, "%d:%d:%d:%s;",
  164. msg_pt->nodeid, msg_pt->pid, msg_pt->seq,
  165. err_status_string (status_buf, 20, status));
  166. list_add_tail (&log_pt->list, &msg_log_head);
  167. total_stored_msgs++;
  168. // if ((total_stored_msgs % 100) == 0) {
  169. // syslog (LOG_INFO, "%s(); %d", __func__, total_stored_msgs);
  170. // }
  171. }
  172. static void config_change_callback (
  173. cpg_handle_t handle,
  174. const struct cpg_name *groupName,
  175. const struct cpg_address *member_list, size_t member_list_entries,
  176. const struct cpg_address *left_list, size_t left_list_entries,
  177. const struct cpg_address *joined_list, size_t joined_list_entries)
  178. {
  179. int i;
  180. log_entry_t *log_pt;
  181. /* group_name,ip,pid,join|leave */
  182. for (i = 0; i < left_list_entries; i++) {
  183. syslog (LOG_INFO, "%s(%d) %d:%s left", __func__, record_config_events_g,
  184. left_list[i].nodeid, groupName->value);
  185. if (record_config_events_g > 0) {
  186. log_pt = malloc (sizeof(log_entry_t));
  187. list_init (&log_pt->list);
  188. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,left",
  189. groupName->value, left_list[i].nodeid,left_list[i].pid);
  190. list_add_tail(&log_pt->list, &config_chg_log_head);
  191. }
  192. }
  193. for (i = 0; i < joined_list_entries; i++) {
  194. syslog (LOG_INFO, "%s(%d) %d:%s joined", __func__, record_config_events_g,
  195. left_list[i].nodeid, groupName->value);
  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. syslog (LOG_CRIT, "%s flags:%d", __func__, 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. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_messages_g);
  230. }
  231. static void record_config_events (int sock)
  232. {
  233. char response[100];
  234. record_config_events_g = 1;
  235. syslog (LOG_INFO, "%s() record:%d", __func__, 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. syslog (LOG_DEBUG,"%s() no events in list", __func__);
  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. syslog (LOG_DEBUG, "%s() atmost %d; total_stored_msgs:%d",
  265. __func__, 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. syslog (LOG_DEBUG, "%s() sending %d; total_stored_msgs:%d; len:%d",
  278. __func__, packed, total_stored_msgs, (int)strlen (big_and_buf));
  279. if (packed == 0) {
  280. strcpy (big_and_buf, "None");
  281. }
  282. send (sock, big_and_buf, strlen (big_and_buf), 0);
  283. }
  284. static qb_loop_timer_handle more_messages_timer_handle;
  285. static void send_some_more_messages_later (void)
  286. {
  287. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  288. qb_loop_timer_add (
  289. ta_poll_handle_get(),
  290. QB_LOOP_MED,
  291. 300*QB_TIME_NS_IN_MSEC, NULL,
  292. send_some_more_messages,
  293. &more_messages_timer_handle);
  294. }
  295. static void send_some_more_messages_zcb (void)
  296. {
  297. msg_t *my_msg;
  298. int i;
  299. int send_now;
  300. size_t payload_size;
  301. size_t total_size;
  302. hash_state sha1_hash;
  303. cs_error_t res;
  304. cpg_flow_control_state_t fc_state;
  305. void *zcb_buffer;
  306. if (cpg_fd < 0)
  307. return;
  308. send_now = my_msgs_to_send;
  309. payload_size = (rand() % 100000);
  310. total_size = payload_size + sizeof (msg_t);
  311. cpg_zcb_alloc (cpg_handle, total_size, &zcb_buffer);
  312. my_msg = (msg_t*)zcb_buffer;
  313. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  314. my_msg->pid = my_pid;
  315. my_msg->nodeid = my_nodeid;
  316. my_msg->size = sizeof (msg_t) + payload_size;
  317. my_msg->seq = 0;
  318. for (i = 0; i < payload_size; i++) {
  319. my_msg->payload[i] = i;
  320. }
  321. sha1_init (&sha1_hash);
  322. sha1_process (&sha1_hash, my_msg->payload, payload_size);
  323. sha1_done (&sha1_hash, my_msg->sha1);
  324. for (i = 0; i < send_now; i++) {
  325. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  326. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  327. /* lets do this later */
  328. send_some_more_messages_later ();
  329. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  330. goto free_buffer;
  331. }
  332. res = cpg_zcb_mcast_joined (cpg_handle, CPG_TYPE_AGREED, zcb_buffer, total_size);
  333. if (res == CS_ERR_TRY_AGAIN) {
  334. /* lets do this later */
  335. send_some_more_messages_later ();
  336. // if (i > 0) {
  337. // syslog (LOG_INFO, "%s() TRY_AGAIN %d to send.",
  338. // __func__, my_msgs_to_send);
  339. // }
  340. goto free_buffer;
  341. } else if (res != CS_OK) {
  342. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  343. __func__, res);
  344. exit (-2);
  345. }
  346. my_msgs_to_send--;
  347. }
  348. free_buffer:
  349. cpg_zcb_free (cpg_handle, zcb_buffer);
  350. }
  351. #define cs_repeat(counter, max, code) do { \
  352. code; \
  353. if(res == CS_ERR_TRY_AGAIN) { \
  354. counter++; \
  355. sleep(counter); \
  356. } \
  357. } while(res == CS_ERR_TRY_AGAIN && counter < max)
  358. static unsigned char buffer[200000];
  359. static void send_some_more_messages_normal (void)
  360. {
  361. msg_t my_msg;
  362. struct iovec iov[2];
  363. int i;
  364. int send_now;
  365. size_t payload_size;
  366. hash_state sha1_hash;
  367. cs_error_t res;
  368. cpg_flow_control_state_t fc_state;
  369. int retries = 0;
  370. time_t before;
  371. if (cpg_fd < 0)
  372. return;
  373. send_now = my_msgs_to_send;
  374. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  375. my_msg.pid = my_pid;
  376. my_msg.nodeid = my_nodeid;
  377. payload_size = (rand() % 100000);
  378. my_msg.size = sizeof (msg_t) + payload_size;
  379. my_msg.seq = 0;
  380. for (i = 0; i < payload_size; i++) {
  381. buffer[i] = i;
  382. }
  383. sha1_init (&sha1_hash);
  384. sha1_process (&sha1_hash, buffer, payload_size);
  385. sha1_done (&sha1_hash, my_msg.sha1);
  386. iov[0].iov_len = sizeof (msg_t);
  387. iov[0].iov_base = &my_msg;
  388. iov[1].iov_len = payload_size;
  389. iov[1].iov_base = buffer;
  390. for (i = 0; i < send_now; i++) {
  391. if (in_cnchg && pcmk_test) {
  392. retries = 0;
  393. before = time(NULL);
  394. cs_repeat(retries, 30, res = cpg_mcast_joined(cpg_handle, CPG_TYPE_AGREED, iov, 2));
  395. if (retries > 20) {
  396. syslog (LOG_ERR, "%s() -> cs_repeat: blocked for :%lu secs.",
  397. __func__, (unsigned long)(time(NULL) - before));
  398. }
  399. if (res != CS_OK) {
  400. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d.",
  401. __func__, res);
  402. return;
  403. }
  404. } else {
  405. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  406. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  407. /* lets do this later */
  408. send_some_more_messages_later ();
  409. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  410. return;
  411. }
  412. res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2);
  413. if (res == CS_ERR_TRY_AGAIN) {
  414. /* lets do this later */
  415. send_some_more_messages_later ();
  416. if (i > 0) {
  417. syslog (LOG_INFO, "%s() TRY_AGAIN %d to send.",
  418. __func__, my_msgs_to_send);
  419. }
  420. return;
  421. } else if (res != CS_OK) {
  422. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  423. __func__, res);
  424. exit (-2);
  425. }
  426. }
  427. my_msgs_to_send--;
  428. }
  429. }
  430. static void send_some_more_messages (void * unused)
  431. {
  432. if (use_zcb) {
  433. // send_some_more_messages_zcb ();
  434. } else {
  435. send_some_more_messages_normal ();
  436. }
  437. }
  438. static void msg_blaster (int sock, char* num_to_send_str)
  439. {
  440. my_msgs_to_send = atoi (num_to_send_str);
  441. my_seq = 1;
  442. my_pid = getpid();
  443. use_zcb = QB_FALSE;
  444. total_stored_msgs = 0;
  445. cpg_local_get (cpg_handle, &my_nodeid);
  446. /* control the limits */
  447. if (my_msgs_to_send <= 0)
  448. my_msgs_to_send = 1;
  449. if (my_msgs_to_send > 10000)
  450. my_msgs_to_send = 10000;
  451. send_some_more_messages_normal ();
  452. }
  453. static void context_test (int sock)
  454. {
  455. char response[100];
  456. char *cmp;
  457. cpg_context_set (cpg_handle, response);
  458. cpg_context_get (cpg_handle, (void**)&cmp);
  459. if (response != cmp) {
  460. snprintf (response, 100, "%s", FAIL_STR);
  461. }
  462. else {
  463. snprintf (response, 100, "%s", OK_STR);
  464. }
  465. send (sock, response, strlen (response), 0);
  466. }
  467. static void msg_blaster_zcb (int sock, char* num_to_send_str)
  468. {
  469. my_msgs_to_send = atoi (num_to_send_str);
  470. my_seq = 1;
  471. my_pid = getpid();
  472. use_zcb = QB_TRUE;
  473. total_stored_msgs = 0;
  474. cpg_local_get (cpg_handle, &my_nodeid);
  475. /* control the limits */
  476. if (my_msgs_to_send <= 0)
  477. my_msgs_to_send = 1;
  478. if (my_msgs_to_send > 10000)
  479. my_msgs_to_send = 10000;
  480. send_some_more_messages_zcb ();
  481. }
  482. static corosync_cfg_state_notification_t notification_buffer;
  483. static int cfg_dispatch_wrapper_fn (
  484. int fd,
  485. int revents,
  486. void *data)
  487. {
  488. cs_error_t error;
  489. if (revents & POLLHUP || revents & POLLERR) {
  490. syslog (LOG_ERR, "%s() got POLLHUP disconnecting from CFG", __func__);
  491. qb_loop_poll_del (ta_poll_handle_get(), cfg_fd);
  492. close (cfg_fd);
  493. cfg_fd = -1;
  494. return -1;
  495. }
  496. error = corosync_cfg_dispatch (cfg_handle, CS_DISPATCH_ALL);
  497. if (error == CS_ERR_LIBRARY) {
  498. syslog (LOG_ERR, "%s() got LIB error disconnecting from CFG.", __func__);
  499. qb_loop_poll_del (ta_poll_handle_get(), cfg_fd);
  500. close (cfg_fd);
  501. cfg_fd = -1;
  502. return -1;
  503. }
  504. return 0;
  505. }
  506. static int cpg_dispatch_wrapper_fn (
  507. int fd,
  508. int revents,
  509. void *data)
  510. {
  511. cs_error_t error;
  512. if (revents & POLLHUP || revents & POLLERR) {
  513. syslog (LOG_ERR, "%s() got POLLHUP disconnecting from CPG", __func__);
  514. qb_loop_poll_del (ta_poll_handle_get(), cpg_fd);
  515. close (cpg_fd);
  516. cpg_fd = -1;
  517. return -1;
  518. }
  519. error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  520. if (error == CS_ERR_LIBRARY) {
  521. syslog (LOG_ERR, "%s() got LIB error disconnecting from CPG", __func__);
  522. qb_loop_poll_del (ta_poll_handle_get(), cpg_fd);
  523. close (cpg_fd);
  524. cpg_fd = -1;
  525. return -1;
  526. }
  527. return 0;
  528. }
  529. static void do_command (int sock, char* func, char*args[], int num_args)
  530. {
  531. int result;
  532. char response[100];
  533. struct cpg_name group_name;
  534. if (parse_debug)
  535. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  536. if (strcmp ("cpg_mcast_joined",func) == 0) {
  537. struct iovec iov[5];
  538. int a;
  539. for (a = 0; a < num_args; a++) {
  540. iov[a].iov_base = args[a];
  541. iov[a].iov_len = strlen(args[a])+1;
  542. }
  543. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  544. } else if (strcmp ("cpg_join",func) == 0) {
  545. strcpy (group_name.value, args[0]);
  546. group_name.length = strlen(args[0]);
  547. result = cpg_join (cpg_handle, &group_name);
  548. if (result != CS_OK) {
  549. syslog (LOG_ERR,
  550. "Could not join process group, error %d\n", result);
  551. exit (1);
  552. }
  553. syslog (LOG_INFO, "called cpg_join()!");
  554. } else if (strcmp ("cpg_leave",func) == 0) {
  555. strcpy (group_name.value, args[0]);
  556. group_name.length = strlen(args[0]);
  557. result = cpg_leave (cpg_handle, &group_name);
  558. if (result != CS_OK) {
  559. syslog (LOG_ERR,
  560. "Could not leave process group, error %d\n", result);
  561. exit (1);
  562. }
  563. syslog (LOG_INFO, "called cpg_leave()!");
  564. } else if (strcmp ("cpg_initialize",func) == 0) {
  565. int retry_count = 0;
  566. result = cpg_initialize (&cpg_handle, &callbacks);
  567. while (result != CS_OK) {
  568. syslog (LOG_ERR,
  569. "cpg_initialize error %d (attempt %d)\n",
  570. result, retry_count);
  571. if (retry_count >= 3) {
  572. exit (1);
  573. }
  574. sleep(1);
  575. retry_count++;
  576. result = cpg_initialize (&cpg_handle, &callbacks);
  577. }
  578. cpg_fd_get (cpg_handle, &cpg_fd);
  579. qb_loop_poll_add (ta_poll_handle_get(),
  580. QB_LOOP_MED,
  581. cpg_fd,
  582. POLLIN|POLLNVAL,
  583. NULL,
  584. cpg_dispatch_wrapper_fn);
  585. } else if (strcmp ("cpg_local_get", func) == 0) {
  586. unsigned int local_nodeid;
  587. cpg_local_get (cpg_handle, &local_nodeid);
  588. snprintf (response, 100, "%u",local_nodeid);
  589. send (sock, response, strlen (response), 0);
  590. } else if (strcmp ("cpg_finalize", func) == 0) {
  591. cpg_finalize (cpg_handle);
  592. qb_loop_poll_del (ta_poll_handle_get(), cpg_fd);
  593. cpg_fd = -1;
  594. } else if (strcmp ("record_config_events", func) == 0) {
  595. record_config_events (sock);
  596. } else if (strcmp ("record_messages", func) == 0) {
  597. record_messages ();
  598. } else if (strcmp ("read_config_event", func) == 0) {
  599. read_config_event (sock);
  600. } else if (strcmp ("read_messages", func) == 0) {
  601. read_messages (sock, args[0]);
  602. } else if (strcmp ("msg_blaster_zcb", func) == 0) {
  603. msg_blaster_zcb (sock, args[0]);
  604. } else if (strcmp ("pcmk_test", func) == 0) {
  605. pcmk_test = 1;
  606. } else if (strcmp ("msg_blaster", func) == 0) {
  607. msg_blaster (sock, args[0]);
  608. } else if (strcmp ("context_test", func) == 0) {
  609. context_test (sock);
  610. } else if (strcmp ("are_you_ok_dude", func) == 0) {
  611. snprintf (response, 100, "%s", OK_STR);
  612. send (sock, response, strlen (response), 0);
  613. } else if (strcmp ("cfg_shutdown", func) == 0) {
  614. corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  615. } else if (strcmp ("cfg_initialize",func) == 0) {
  616. int retry_count = 0;
  617. syslog (LOG_INFO,"%s %s() called!", __func__, func);
  618. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  619. while (result != CS_OK) {
  620. syslog (LOG_ERR,
  621. "cfg_initialize error %d (attempt %d)\n",
  622. result, retry_count);
  623. if (retry_count >= 3) {
  624. exit (1);
  625. }
  626. sleep(1);
  627. retry_count++;
  628. result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks);
  629. }
  630. corosync_cfg_fd_get (cfg_handle, &cfg_fd);
  631. corosync_cfg_state_track (cfg_handle, 0, &notification_buffer);
  632. qb_loop_poll_add (ta_poll_handle_get(),
  633. QB_LOOP_MED,
  634. cfg_fd,
  635. POLLIN|POLLNVAL,
  636. NULL,
  637. cfg_dispatch_wrapper_fn);
  638. } else {
  639. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  640. }
  641. }
  642. int main (int argc, char *argv[])
  643. {
  644. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  645. list_init (&msg_log_head);
  646. list_init (&config_chg_log_head);
  647. return test_agent_run (9034, do_command);
  648. }