cfg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <signal.h>
  42. #include <pthread.h>
  43. #include <limits.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <sys/select.h>
  47. #include <sys/un.h>
  48. #include <corosync/corotypes.h>
  49. #include <corosync/coroipc_types.h>
  50. #include <corosync/coroipcc.h>
  51. #include <corosync/corodefs.h>
  52. #include <corosync/hdb.h>
  53. #include <corosync/cfg.h>
  54. #include <corosync/ipc_cfg.h>
  55. #include "util.h"
  56. /*
  57. * Data structure for instance data
  58. */
  59. struct cfg_instance {
  60. void *ipc_ctx;
  61. corosync_cfg_callbacks_t callbacks;
  62. cs_name_t comp_name;
  63. int comp_registered;
  64. int finalize;
  65. pthread_mutex_t response_mutex;
  66. pthread_mutex_t dispatch_mutex;
  67. };
  68. static void cfg_handle_instance_destructor (void *);
  69. /*
  70. * All instances in one database
  71. */
  72. DECLARE_HDB_DATABASE (cfg_hdb,cfg_handle_instance_destructor);
  73. /*
  74. * Implementation
  75. */
  76. void cfg_handle_instance_destructor (void *instance)
  77. {
  78. struct cfg_instance *cfg_instance = instance;
  79. pthread_mutex_destroy (&cfg_instance->response_mutex);
  80. pthread_mutex_destroy (&cfg_instance->dispatch_mutex);
  81. }
  82. cs_error_t
  83. corosync_cfg_initialize (
  84. corosync_cfg_handle_t *cfg_handle,
  85. const corosync_cfg_callbacks_t *cfg_callbacks)
  86. {
  87. struct cfg_instance *cfg_instance;
  88. cs_error_t error = CS_OK;
  89. error = hdb_error_to_cs (hdb_handle_create (&cfg_hdb, sizeof (struct cfg_instance), cfg_handle));
  90. if (error != CS_OK) {
  91. goto error_no_destroy;
  92. }
  93. error = hdb_error_to_cs (hdb_handle_get (&cfg_hdb, *cfg_handle, (void *)&cfg_instance));
  94. if (error != CS_OK) {
  95. goto error_destroy;
  96. }
  97. error = coroipcc_service_connect (
  98. COROSYNC_SOCKET_NAME,
  99. CFG_SERVICE,
  100. IPC_REQUEST_SIZE,
  101. IPC_RESPONSE_SIZE,
  102. IPC_DISPATCH_SIZE,
  103. &cfg_instance->ipc_ctx);
  104. if (error != CS_OK) {
  105. goto error_put_destroy;
  106. }
  107. if (cfg_callbacks) {
  108. memcpy (&cfg_instance->callbacks, cfg_callbacks, sizeof (corosync_cfg_callbacks_t));
  109. }
  110. pthread_mutex_init (&cfg_instance->response_mutex, NULL);
  111. pthread_mutex_init (&cfg_instance->dispatch_mutex, NULL);
  112. (void)hdb_handle_put (&cfg_hdb, *cfg_handle);
  113. return (CS_OK);
  114. error_put_destroy:
  115. (void)hdb_handle_put (&cfg_hdb, *cfg_handle);
  116. error_destroy:
  117. (void)hdb_handle_destroy (&cfg_hdb, *cfg_handle);
  118. error_no_destroy:
  119. return (error);
  120. }
  121. cs_error_t
  122. corosync_cfg_fd_get (
  123. corosync_cfg_handle_t cfg_handle,
  124. int32_t *selection_fd)
  125. {
  126. struct cfg_instance *cfg_instance;
  127. cs_error_t error;
  128. error = hdb_error_to_cs (hdb_handle_get (&cfg_hdb, cfg_handle, (void *)&cfg_instance));
  129. if (error != CS_OK) {
  130. return (error);
  131. }
  132. *selection_fd = coroipcc_fd_get (cfg_instance->ipc_ctx);
  133. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  134. return (CS_OK);
  135. }
  136. cs_error_t
  137. corosync_cfg_dispatch (
  138. corosync_cfg_handle_t cfg_handle,
  139. cs_dispatch_flags_t dispatch_flags)
  140. {
  141. int timeout = -1;
  142. cs_error_t error;
  143. int cont = 1; /* always continue do loop except when set to 0 */
  144. int dispatch_avail;
  145. struct cfg_instance *cfg_instance;
  146. struct res_lib_cfg_testshutdown *res_lib_cfg_testshutdown;
  147. corosync_cfg_callbacks_t callbacks;
  148. coroipc_response_header_t *dispatch_data;
  149. error = hdb_error_to_cs (hdb_handle_get (&cfg_hdb, cfg_handle,
  150. (void *)&cfg_instance));
  151. if (error != CS_OK) {
  152. return (error);
  153. }
  154. /*
  155. * Timeout instantly for CS_DISPATCH_ALL
  156. */
  157. if (dispatch_flags == CS_DISPATCH_ALL) {
  158. timeout = 0;
  159. }
  160. do {
  161. pthread_mutex_lock (&cfg_instance->dispatch_mutex);
  162. dispatch_avail = coroipcc_dispatch_get (
  163. cfg_instance->ipc_ctx,
  164. (void **)&dispatch_data,
  165. timeout);
  166. /*
  167. * Handle has been finalized in another thread
  168. */
  169. if (cfg_instance->finalize == 1) {
  170. error = CS_OK;
  171. pthread_mutex_unlock (&cfg_instance->dispatch_mutex);
  172. goto error_put;
  173. }
  174. if (dispatch_avail == 0 && dispatch_flags == CS_DISPATCH_ALL) {
  175. pthread_mutex_unlock (&cfg_instance->dispatch_mutex);
  176. break; /* exit do while cont is 1 loop */
  177. } else
  178. if (dispatch_avail == 0) {
  179. pthread_mutex_unlock (&cfg_instance->dispatch_mutex);
  180. continue; /* next poll */
  181. }
  182. /*
  183. * Make copy of callbacks, message data, unlock instance, and call callback
  184. * A risk of this dispatch method is that the callback routines may
  185. * operate at the same time that cfgFinalize has been called in another thread.
  186. */
  187. memcpy (&callbacks, &cfg_instance->callbacks, sizeof (corosync_cfg_callbacks_t));
  188. pthread_mutex_unlock (&cfg_instance->dispatch_mutex);
  189. /*
  190. * Dispatch incoming response
  191. */
  192. switch (dispatch_data->id) {
  193. case MESSAGE_RES_CFG_TESTSHUTDOWN:
  194. if (callbacks.corosync_cfg_shutdown_callback) {
  195. res_lib_cfg_testshutdown = (struct res_lib_cfg_testshutdown *)dispatch_data;
  196. callbacks.corosync_cfg_shutdown_callback(cfg_handle, res_lib_cfg_testshutdown->flags);
  197. }
  198. break;
  199. default:
  200. coroipcc_dispatch_put (cfg_instance->ipc_ctx);
  201. error = CS_ERR_LIBRARY;
  202. goto error_nounlock;
  203. break;
  204. }
  205. coroipcc_dispatch_put (cfg_instance->ipc_ctx);
  206. /*
  207. * Determine if more messages should be processed
  208. */
  209. switch (dispatch_flags) {
  210. case CS_DISPATCH_ONE:
  211. cont = 0;
  212. break;
  213. case CS_DISPATCH_ALL:
  214. break;
  215. case CS_DISPATCH_BLOCKING:
  216. break;
  217. }
  218. } while (cont);
  219. error_put:
  220. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  221. error_nounlock:
  222. return (error);
  223. }
  224. cs_error_t
  225. corosync_cfg_finalize (
  226. corosync_cfg_handle_t cfg_handle)
  227. {
  228. struct cfg_instance *cfg_instance;
  229. cs_error_t error;
  230. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle, (void *)&cfg_instance));
  231. if (error != CS_OK) {
  232. return (error);
  233. }
  234. pthread_mutex_lock (&cfg_instance->dispatch_mutex);
  235. pthread_mutex_lock (&cfg_instance->response_mutex);
  236. /*
  237. * Another thread has already started finalizing
  238. */
  239. if (cfg_instance->finalize) {
  240. pthread_mutex_unlock (&cfg_instance->response_mutex);
  241. pthread_mutex_unlock (&cfg_instance->dispatch_mutex);
  242. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  243. return (CS_ERR_BAD_HANDLE);
  244. }
  245. cfg_instance->finalize = 1;
  246. coroipcc_service_disconnect (cfg_instance->ipc_ctx);
  247. pthread_mutex_unlock (&cfg_instance->response_mutex);
  248. pthread_mutex_unlock (&cfg_instance->dispatch_mutex);
  249. pthread_mutex_destroy (&cfg_instance->response_mutex);
  250. pthread_mutex_destroy (&cfg_instance->dispatch_mutex);
  251. (void)hdb_handle_destroy (&cfg_hdb, cfg_handle);
  252. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  253. return (error);
  254. }
  255. cs_error_t
  256. corosync_cfg_ring_status_get (
  257. corosync_cfg_handle_t cfg_handle,
  258. char ***interface_names,
  259. char ***status,
  260. unsigned int *interface_count)
  261. {
  262. struct cfg_instance *cfg_instance;
  263. struct req_lib_cfg_ringstatusget req_lib_cfg_ringstatusget;
  264. struct res_lib_cfg_ringstatusget res_lib_cfg_ringstatusget;
  265. unsigned int i;
  266. cs_error_t error;
  267. struct iovec iov;
  268. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle, (void *)&cfg_instance));
  269. if (error != CS_OK) {
  270. return (error);
  271. }
  272. req_lib_cfg_ringstatusget.header.size = sizeof (struct req_lib_cfg_ringstatusget);
  273. req_lib_cfg_ringstatusget.header.id = MESSAGE_REQ_CFG_RINGSTATUSGET;
  274. iov.iov_base = &req_lib_cfg_ringstatusget,
  275. iov.iov_len = sizeof (struct req_lib_cfg_ringstatusget),
  276. pthread_mutex_lock (&cfg_instance->response_mutex);
  277. error = coroipcc_msg_send_reply_receive(cfg_instance->ipc_ctx,
  278. &iov,
  279. 1,
  280. &res_lib_cfg_ringstatusget,
  281. sizeof (struct res_lib_cfg_ringstatusget));
  282. pthread_mutex_unlock (&cfg_instance->response_mutex);
  283. *interface_count = res_lib_cfg_ringstatusget.interface_count;
  284. *interface_names = malloc (sizeof (char *) * *interface_count);
  285. if (*interface_names == NULL) {
  286. return (CS_ERR_NO_MEMORY);
  287. }
  288. memset (*interface_names, 0, sizeof (char *) * *interface_count);
  289. *status = malloc (sizeof (char *) * *interface_count);
  290. if (*status == NULL) {
  291. error = CS_ERR_NO_MEMORY;
  292. goto error_free_interface_names;
  293. }
  294. memset (*status, 0, sizeof (char *) * *interface_count);
  295. for (i = 0; i < res_lib_cfg_ringstatusget.interface_count; i++) {
  296. (*(interface_names))[i] = strdup (res_lib_cfg_ringstatusget.interface_name[i]);
  297. if ((*(interface_names))[i] == NULL) {
  298. error = CS_ERR_NO_MEMORY;
  299. goto error_free_contents;
  300. }
  301. (*(status))[i] = strdup (res_lib_cfg_ringstatusget.interface_status[i]);
  302. if ((*(status))[i] == NULL) {
  303. error = CS_ERR_NO_MEMORY;
  304. goto error_free_contents;
  305. }
  306. }
  307. goto no_error;
  308. error_free_contents:
  309. for (i = 0; i < res_lib_cfg_ringstatusget.interface_count; i++) {
  310. if ((*(interface_names))[i]) {
  311. free ((*(interface_names))[i]);
  312. }
  313. if ((*(status))[i]) {
  314. free ((*(status))[i]);
  315. }
  316. }
  317. free (*status);
  318. error_free_interface_names:
  319. free (*interface_names);
  320. no_error:
  321. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  322. return (error);
  323. }
  324. cs_error_t
  325. corosync_cfg_ring_reenable (
  326. corosync_cfg_handle_t cfg_handle)
  327. {
  328. struct cfg_instance *cfg_instance;
  329. struct req_lib_cfg_ringreenable req_lib_cfg_ringreenable;
  330. struct res_lib_cfg_ringreenable res_lib_cfg_ringreenable;
  331. cs_error_t error;
  332. struct iovec iov;
  333. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle, (void *)&cfg_instance));
  334. if (error != CS_OK) {
  335. return (error);
  336. }
  337. req_lib_cfg_ringreenable.header.size = sizeof (struct req_lib_cfg_ringreenable);
  338. req_lib_cfg_ringreenable.header.id = MESSAGE_REQ_CFG_RINGREENABLE;
  339. iov.iov_base = &req_lib_cfg_ringreenable,
  340. iov.iov_len = sizeof (struct req_lib_cfg_ringreenable);
  341. pthread_mutex_lock (&cfg_instance->response_mutex);
  342. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  343. &iov,
  344. 1,
  345. &res_lib_cfg_ringreenable,
  346. sizeof (struct res_lib_cfg_ringreenable));
  347. pthread_mutex_unlock (&cfg_instance->response_mutex);
  348. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  349. return (error);
  350. }
  351. cs_error_t
  352. corosync_cfg_service_load (
  353. corosync_cfg_handle_t cfg_handle,
  354. const char *service_name,
  355. unsigned int service_ver)
  356. {
  357. struct cfg_instance *cfg_instance;
  358. struct req_lib_cfg_serviceload req_lib_cfg_serviceload;
  359. struct res_lib_cfg_serviceload res_lib_cfg_serviceload;
  360. cs_error_t error;
  361. struct iovec iov;
  362. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle, (void *)&cfg_instance));
  363. if (error != CS_OK) {
  364. return (error);
  365. }
  366. req_lib_cfg_serviceload.header.size = sizeof (struct req_lib_cfg_serviceload);
  367. req_lib_cfg_serviceload.header.id = MESSAGE_REQ_CFG_SERVICELOAD;
  368. memset (&req_lib_cfg_serviceload.service_name, 0,
  369. sizeof (req_lib_cfg_serviceload.service_name));
  370. strncpy (req_lib_cfg_serviceload.service_name, service_name,
  371. sizeof (req_lib_cfg_serviceload.service_name) - 1);
  372. req_lib_cfg_serviceload.service_ver = service_ver;
  373. iov.iov_base = &req_lib_cfg_serviceload;
  374. iov.iov_len = sizeof (req_lib_cfg_serviceload);
  375. pthread_mutex_lock (&cfg_instance->response_mutex);
  376. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  377. &iov,
  378. 1,
  379. &res_lib_cfg_serviceload,
  380. sizeof (struct res_lib_cfg_serviceload));
  381. pthread_mutex_unlock (&cfg_instance->response_mutex);
  382. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  383. return (error);
  384. }
  385. cs_error_t
  386. corosync_cfg_service_unload (
  387. corosync_cfg_handle_t cfg_handle,
  388. const char *service_name,
  389. unsigned int service_ver)
  390. {
  391. struct cfg_instance *cfg_instance;
  392. struct req_lib_cfg_serviceunload req_lib_cfg_serviceunload;
  393. struct res_lib_cfg_serviceunload res_lib_cfg_serviceunload;
  394. cs_error_t error;
  395. struct iovec iov;
  396. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle, (void *)&cfg_instance));
  397. if (error != CS_OK) {
  398. return (error);
  399. }
  400. req_lib_cfg_serviceunload.header.size = sizeof (struct req_lib_cfg_serviceunload);
  401. req_lib_cfg_serviceunload.header.id = MESSAGE_REQ_CFG_SERVICEUNLOAD;
  402. memset (&req_lib_cfg_serviceunload.service_name, 0,
  403. sizeof (req_lib_cfg_serviceunload.service_name));
  404. strncpy (req_lib_cfg_serviceunload.service_name, service_name,
  405. sizeof (req_lib_cfg_serviceunload.service_name) - 1);
  406. req_lib_cfg_serviceunload.service_ver = service_ver;
  407. iov.iov_base = &req_lib_cfg_serviceunload;
  408. iov.iov_len = sizeof (req_lib_cfg_serviceunload);
  409. pthread_mutex_lock (&cfg_instance->response_mutex);
  410. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  411. &iov,
  412. 1,
  413. &res_lib_cfg_serviceunload,
  414. sizeof (struct res_lib_cfg_serviceunload));
  415. pthread_mutex_unlock (&cfg_instance->response_mutex);
  416. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  417. return (error);
  418. }
  419. cs_error_t
  420. corosync_cfg_state_track (
  421. corosync_cfg_handle_t cfg_handle,
  422. uint8_t track_flags,
  423. const corosync_cfg_state_notification_t *notification_buffer)
  424. {
  425. struct cfg_instance *cfg_instance;
  426. struct req_lib_cfg_statetrack req_lib_cfg_statetrack;
  427. struct res_lib_cfg_statetrack res_lib_cfg_statetrack;
  428. cs_error_t error;
  429. struct iovec iov;
  430. req_lib_cfg_statetrack.header.size = sizeof (struct req_lib_cfg_statetrack);
  431. req_lib_cfg_statetrack.header.id = MESSAGE_REQ_CFG_STATETRACKSTART;
  432. req_lib_cfg_statetrack.track_flags = track_flags;
  433. req_lib_cfg_statetrack.notification_buffer_address = (corosync_cfg_state_notification_t *)notification_buffer;
  434. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle,
  435. (void *)&cfg_instance));
  436. if (error != CS_OK) {
  437. return (error);
  438. }
  439. iov.iov_base = &req_lib_cfg_statetrack,
  440. iov.iov_len = sizeof (struct req_lib_cfg_statetrack),
  441. pthread_mutex_lock (&cfg_instance->response_mutex);
  442. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  443. &iov,
  444. 1,
  445. &res_lib_cfg_statetrack,
  446. sizeof (struct res_lib_cfg_statetrack));
  447. pthread_mutex_unlock (&cfg_instance->response_mutex);
  448. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  449. return (error == CS_OK ? res_lib_cfg_statetrack.header.error : error);
  450. }
  451. cs_error_t
  452. corosync_cfg_state_track_stop (
  453. corosync_cfg_handle_t cfg_handle)
  454. {
  455. struct cfg_instance *cfg_instance;
  456. struct req_lib_cfg_statetrackstop req_lib_cfg_statetrackstop;
  457. struct res_lib_cfg_statetrackstop res_lib_cfg_statetrackstop;
  458. cs_error_t error;
  459. struct iovec iov;
  460. error = hdb_error_to_cs (hdb_handle_get (&cfg_hdb, cfg_handle,
  461. (void *)&cfg_instance));
  462. if (error != CS_OK) {
  463. return (error);
  464. }
  465. req_lib_cfg_statetrackstop.header.size = sizeof (struct req_lib_cfg_statetrackstop);
  466. req_lib_cfg_statetrackstop.header.id = MESSAGE_REQ_CFG_STATETRACKSTOP;
  467. iov.iov_base = &req_lib_cfg_statetrackstop,
  468. iov.iov_len = sizeof (struct req_lib_cfg_statetrackstop),
  469. pthread_mutex_lock (&cfg_instance->response_mutex);
  470. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  471. &iov,
  472. 1,
  473. &res_lib_cfg_statetrackstop,
  474. sizeof (struct res_lib_cfg_statetrackstop));
  475. pthread_mutex_unlock (&cfg_instance->response_mutex);
  476. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  477. return (error == CS_OK ? res_lib_cfg_statetrackstop.header.error : error);
  478. }
  479. cs_error_t
  480. corosync_cfg_kill_node (
  481. corosync_cfg_handle_t cfg_handle,
  482. unsigned int nodeid,
  483. const char *reason)
  484. {
  485. struct cfg_instance *cfg_instance;
  486. struct req_lib_cfg_killnode req_lib_cfg_killnode;
  487. struct res_lib_cfg_killnode res_lib_cfg_killnode;
  488. cs_error_t error;
  489. struct iovec iov;
  490. if (strlen(reason) >= CS_MAX_NAME_LENGTH)
  491. return CS_ERR_NAME_TOO_LONG;
  492. error = hdb_error_to_cs (hdb_handle_get (&cfg_hdb, cfg_handle,
  493. (void *)&cfg_instance));
  494. if (error != CS_OK) {
  495. return (error);
  496. }
  497. req_lib_cfg_killnode.header.id = MESSAGE_REQ_CFG_KILLNODE;
  498. req_lib_cfg_killnode.header.size = sizeof (struct req_lib_cfg_killnode);
  499. req_lib_cfg_killnode.nodeid = nodeid;
  500. strcpy((char *)req_lib_cfg_killnode.reason.value, reason);
  501. req_lib_cfg_killnode.reason.length = strlen(reason)+1;
  502. iov.iov_base = &req_lib_cfg_killnode;
  503. iov.iov_len = sizeof (struct req_lib_cfg_killnode);
  504. pthread_mutex_lock (&cfg_instance->response_mutex);
  505. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  506. &iov,
  507. 1,
  508. &res_lib_cfg_killnode,
  509. sizeof (struct res_lib_cfg_killnode));
  510. error = res_lib_cfg_killnode.header.error;
  511. pthread_mutex_unlock (&cfg_instance->response_mutex);
  512. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  513. return (error == CS_OK ? res_lib_cfg_killnode.header.error : error);
  514. }
  515. cs_error_t
  516. corosync_cfg_try_shutdown (
  517. corosync_cfg_handle_t cfg_handle,
  518. corosync_cfg_shutdown_flags_t flags)
  519. {
  520. struct cfg_instance *cfg_instance;
  521. struct req_lib_cfg_tryshutdown req_lib_cfg_tryshutdown;
  522. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  523. cs_error_t error;
  524. struct iovec iov;
  525. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle,
  526. (void *)&cfg_instance));
  527. if (error != CS_OK) {
  528. return (error);
  529. }
  530. req_lib_cfg_tryshutdown.header.id = MESSAGE_REQ_CFG_TRYSHUTDOWN;
  531. req_lib_cfg_tryshutdown.header.size = sizeof (struct req_lib_cfg_tryshutdown);
  532. req_lib_cfg_tryshutdown.flags = flags;
  533. iov.iov_base = &req_lib_cfg_tryshutdown;
  534. iov.iov_len = sizeof (req_lib_cfg_tryshutdown);
  535. pthread_mutex_lock (&cfg_instance->response_mutex);
  536. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  537. &iov,
  538. 1,
  539. &res_lib_cfg_tryshutdown,
  540. sizeof (struct res_lib_cfg_tryshutdown));
  541. pthread_mutex_unlock (&cfg_instance->response_mutex);
  542. (void)hdb_handle_put (&cfg_hdb, cfg_handle);
  543. return (error == CS_OK ? res_lib_cfg_tryshutdown.header.error : error);
  544. }
  545. cs_error_t
  546. corosync_cfg_replyto_shutdown (
  547. corosync_cfg_handle_t cfg_handle,
  548. corosync_cfg_shutdown_reply_flags_t response)
  549. {
  550. struct cfg_instance *cfg_instance;
  551. struct req_lib_cfg_replytoshutdown req_lib_cfg_replytoshutdown;
  552. struct res_lib_cfg_replytoshutdown res_lib_cfg_replytoshutdown;
  553. struct iovec iov;
  554. cs_error_t error;
  555. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle,
  556. (void *)&cfg_instance));
  557. if (error != CS_OK) {
  558. return (error);
  559. }
  560. req_lib_cfg_replytoshutdown.header.id = MESSAGE_REQ_CFG_REPLYTOSHUTDOWN;
  561. req_lib_cfg_replytoshutdown.header.size = sizeof (struct req_lib_cfg_replytoshutdown);
  562. req_lib_cfg_replytoshutdown.response = response;
  563. iov.iov_base = &req_lib_cfg_replytoshutdown;
  564. iov.iov_len = sizeof (struct req_lib_cfg_replytoshutdown);
  565. pthread_mutex_lock (&cfg_instance->response_mutex);
  566. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  567. &iov,
  568. 1,
  569. &res_lib_cfg_replytoshutdown,
  570. sizeof (struct res_lib_cfg_replytoshutdown));
  571. pthread_mutex_unlock (&cfg_instance->response_mutex);
  572. return (error);
  573. }
  574. cs_error_t corosync_cfg_get_node_addrs (
  575. corosync_cfg_handle_t cfg_handle,
  576. int nodeid,
  577. size_t max_addrs,
  578. int *num_addrs,
  579. corosync_cfg_node_address_t *addrs)
  580. {
  581. cs_error_t error;
  582. struct req_lib_cfg_get_node_addrs req_lib_cfg_get_node_addrs;
  583. struct res_lib_cfg_get_node_addrs *res_lib_cfg_get_node_addrs;
  584. struct cfg_instance *cfg_instance;
  585. int addrlen;
  586. int i;
  587. struct iovec iov;
  588. void *return_address;
  589. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, cfg_handle,
  590. (void *)&cfg_instance));
  591. if (error != CS_OK) {
  592. return (error);
  593. }
  594. req_lib_cfg_get_node_addrs.header.size = sizeof (req_lib_cfg_get_node_addrs);
  595. req_lib_cfg_get_node_addrs.header.id = MESSAGE_REQ_CFG_GET_NODE_ADDRS;
  596. req_lib_cfg_get_node_addrs.nodeid = nodeid;
  597. iov.iov_base = (char *)&req_lib_cfg_get_node_addrs;
  598. iov.iov_len = sizeof (req_lib_cfg_get_node_addrs);
  599. pthread_mutex_lock (&cfg_instance->response_mutex);
  600. error = coroipcc_msg_send_reply_receive_in_buf (cfg_instance->ipc_ctx,
  601. &iov,
  602. 1,
  603. &return_address);
  604. res_lib_cfg_get_node_addrs = return_address;
  605. pthread_mutex_unlock (&cfg_instance->response_mutex);
  606. if (error != CS_OK) {
  607. goto error_exit;
  608. }
  609. if (res_lib_cfg_get_node_addrs->family == AF_INET)
  610. addrlen = sizeof(struct sockaddr_in);
  611. if (res_lib_cfg_get_node_addrs->family == AF_INET6)
  612. addrlen = sizeof(struct sockaddr_in6);
  613. for (i=0; i<max_addrs && i<res_lib_cfg_get_node_addrs->num_addrs; i++) {
  614. struct sockaddr_in *in;
  615. struct sockaddr_in6 *in6;
  616. addrs[i].address_length = addrlen;
  617. if (res_lib_cfg_get_node_addrs->family == AF_INET) {
  618. in = (struct sockaddr_in *)addrs[i].address;
  619. in->sin_family = AF_INET;
  620. memcpy(&in->sin_addr, &res_lib_cfg_get_node_addrs->addrs[i][0], sizeof(struct in_addr));
  621. }
  622. if (res_lib_cfg_get_node_addrs->family == AF_INET6) {
  623. in6 = (struct sockaddr_in6 *)addrs[i].address;
  624. in6->sin6_family = AF_INET6;
  625. memcpy(&in6->sin6_addr, &res_lib_cfg_get_node_addrs->addrs[i][0], sizeof(struct in6_addr));
  626. }
  627. }
  628. *num_addrs = res_lib_cfg_get_node_addrs->num_addrs;
  629. errno = error = res_lib_cfg_get_node_addrs->header.error;
  630. error_exit:
  631. return (error);
  632. }
  633. cs_error_t corosync_cfg_local_get (
  634. corosync_cfg_handle_t handle,
  635. unsigned int *local_nodeid)
  636. {
  637. cs_error_t error;
  638. struct cfg_instance *cfg_inst;
  639. struct iovec iov;
  640. struct req_lib_cfg_local_get req_lib_cfg_local_get;
  641. struct res_lib_cfg_local_get res_lib_cfg_local_get;
  642. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, handle, (void *)&cfg_inst));
  643. if (error != CS_OK) {
  644. return (error);
  645. }
  646. req_lib_cfg_local_get.header.size = sizeof (coroipc_request_header_t);
  647. req_lib_cfg_local_get.header.id = MESSAGE_REQ_CFG_LOCAL_GET;
  648. iov.iov_base = &req_lib_cfg_local_get;
  649. iov.iov_len = sizeof (struct req_lib_cfg_local_get);
  650. pthread_mutex_lock (&cfg_inst->response_mutex);
  651. error = coroipcc_msg_send_reply_receive (
  652. cfg_inst->ipc_ctx,
  653. &iov,
  654. 1,
  655. &res_lib_cfg_local_get,
  656. sizeof (struct res_lib_cfg_local_get));
  657. pthread_mutex_unlock (&cfg_inst->response_mutex);
  658. if (error != CS_OK) {
  659. goto error_exit;
  660. }
  661. error = res_lib_cfg_local_get.header.error;
  662. *local_nodeid = res_lib_cfg_local_get.local_nodeid;
  663. error_exit:
  664. (void)hdb_handle_put (&cfg_hdb, handle);
  665. return (error);
  666. }
  667. cs_error_t
  668. corosync_cfg_crypto_set (
  669. corosync_cfg_handle_t handle,
  670. unsigned int type)
  671. {
  672. struct cfg_instance *cfg_instance;
  673. struct req_lib_cfg_crypto_set req_lib_cfg_crypto_set;
  674. struct res_lib_cfg_crypto_set res_lib_cfg_crypto_set;
  675. struct iovec iov;
  676. cs_error_t error;
  677. error = hdb_error_to_cs(hdb_handle_get (&cfg_hdb, handle, (void *)&cfg_instance));
  678. if (error != CS_OK) {
  679. return (error);
  680. }
  681. req_lib_cfg_crypto_set.header.id = MESSAGE_REQ_CFG_CRYPTO_SET;
  682. req_lib_cfg_crypto_set.header.size = sizeof (struct req_lib_cfg_crypto_set);
  683. req_lib_cfg_crypto_set.type = type;
  684. iov.iov_base = &req_lib_cfg_crypto_set;
  685. iov.iov_len = sizeof (struct req_lib_cfg_crypto_set);
  686. pthread_mutex_lock (&cfg_instance->response_mutex);
  687. error = coroipcc_msg_send_reply_receive (cfg_instance->ipc_ctx,
  688. &iov,
  689. 1,
  690. &res_lib_cfg_crypto_set,
  691. sizeof (struct res_lib_cfg_crypto_set));
  692. pthread_mutex_unlock (&cfg_instance->response_mutex);
  693. if (error == CS_OK)
  694. error = res_lib_cfg_crypto_set.header.error;
  695. (void)hdb_handle_put (&cfg_hdb, handle);
  696. return (error);
  697. }