corosync-notifyd.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /*
  2. * Copyright (c) 2011-2012 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 <config.h>
  35. #include <sys/select.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/types.h>
  39. #include <netdb.h>
  40. #include <arpa/inet.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <errno.h>
  44. #include <unistd.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <poll.h>
  48. #include <signal.h>
  49. #include <qb/qbdefs.h>
  50. #include <qb/qbloop.h>
  51. #include <qb/qblog.h>
  52. #include <corosync/corotypes.h>
  53. #include <corosync/cfg.h>
  54. #include <corosync/quorum.h>
  55. #include <corosync/cmap.h>
  56. /*
  57. * generic declarations
  58. */
  59. enum {
  60. CS_NTF_LOG,
  61. CS_NTF_STDOUT,
  62. CS_NTF_SNMP,
  63. CS_NTF_DBUS,
  64. CS_NTF_FG,
  65. CS_NTF_MAX,
  66. };
  67. static int conf[CS_NTF_MAX];
  68. static int exit_code = 0;
  69. static int32_t _cs_is_quorate = 0;
  70. typedef void (*node_membership_fn_t)(char *nodename, uint32_t nodeid, char *state, char* ip);
  71. typedef void (*node_quorum_fn_t)(char *nodename, uint32_t nodeid, const char *state);
  72. typedef void (*application_connection_fn_t)(char *nodename, uint32_t nodeid, char *app_name, const char *state);
  73. typedef void (*rrp_faulty_fn_t)(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state);
  74. struct notify_callbacks {
  75. node_membership_fn_t node_membership_fn;
  76. node_quorum_fn_t node_quorum_fn;
  77. application_connection_fn_t application_connection_fn;
  78. rrp_faulty_fn_t rrp_faulty_fn;
  79. };
  80. #define MAX_NOTIFIERS 5
  81. static int num_notifiers = 0;
  82. static struct notify_callbacks notifiers[MAX_NOTIFIERS];
  83. /*
  84. * Global variable with local nodeid
  85. */
  86. static uint32_t g_local_nodeid = 0;
  87. static char local_nodename[CS_MAX_NAME_LENGTH];
  88. static qb_loop_t *main_loop;
  89. static quorum_handle_t quorum_handle;
  90. static void _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip);
  91. static void _cs_node_quorum_event(const char *state);
  92. static void _cs_application_connection_event(char *app_name, const char *state);
  93. static void _cs_rrp_faulty_event(uint32_t iface_no, const char *state);
  94. #ifdef HAVE_DBUS
  95. #include <dbus/dbus.h>
  96. /*
  97. * dbus
  98. */
  99. #define DBUS_CS_NAME "org.corosync"
  100. #define DBUS_CS_IFACE "org.corosync"
  101. #define DBUS_CS_PATH "/org/corosync"
  102. static DBusConnection *db = NULL;
  103. static char _err[512];
  104. static int err_set = 0;
  105. static void _cs_dbus_init(void);
  106. #endif /* HAVE_DBUS */
  107. #ifdef ENABLE_SNMP
  108. #include <net-snmp/net-snmp-config.h>
  109. #include <net-snmp/snmpv3_api.h>
  110. #include <net-snmp/agent/agent_trap.h>
  111. #include <net-snmp/library/mib.h>
  112. #include <net-snmp/library/snmp_api.h>
  113. #include <net-snmp/library/snmp_client.h>
  114. #include <net-snmp/library/snmp_debug.h>
  115. enum snmp_node_status {
  116. SNMP_NODE_STATUS_UNKNOWN = 0,
  117. SNMP_NODE_STATUS_JOINED = 1,
  118. SNMP_NODE_STATUS_LEFT = 2
  119. };
  120. #define SNMP_OID_COROSYNC "1.3.6.1.4.1.35488"
  121. #define SNMP_OID_OBJECT_ROOT SNMP_OID_COROSYNC ".1"
  122. #define SNMP_OID_OBJECT_NODE_NAME SNMP_OID_OBJECT_ROOT ".1"
  123. #define SNMP_OID_OBJECT_NODE_ID SNMP_OID_OBJECT_ROOT ".2"
  124. #define SNMP_OID_OBJECT_NODE_STATUS SNMP_OID_OBJECT_ROOT ".3"
  125. #define SNMP_OID_OBJECT_NODE_ADDR SNMP_OID_OBJECT_ROOT ".4"
  126. #define SNMP_OID_OBJECT_RINGSEQ SNMP_OID_OBJECT_ROOT ".20"
  127. #define SNMP_OID_OBJECT_QUORUM SNMP_OID_OBJECT_ROOT ".21"
  128. #define SNMP_OID_OBJECT_APP_NAME SNMP_OID_OBJECT_ROOT ".40"
  129. #define SNMP_OID_OBJECT_APP_STATUS SNMP_OID_OBJECT_ROOT ".41"
  130. #define SNMP_OID_OBJECT_RRP_IFACE_NO SNMP_OID_OBJECT_ROOT ".60"
  131. #define SNMP_OID_OBJECT_RRP_STATUS SNMP_OID_OBJECT_ROOT ".61"
  132. #define SNMP_OID_TRAPS_ROOT SNMP_OID_COROSYNC ".0"
  133. #define SNMP_OID_TRAPS_NODE SNMP_OID_TRAPS_ROOT ".1"
  134. #define SNMP_OID_TRAPS_QUORUM SNMP_OID_TRAPS_ROOT ".2"
  135. #define SNMP_OID_TRAPS_APP SNMP_OID_TRAPS_ROOT ".3"
  136. #define SNMP_OID_TRAPS_RRP SNMP_OID_TRAPS_ROOT ".4"
  137. #define CS_TIMESTAMP_STR_LEN 20
  138. static const char *local_host = "localhost";
  139. #endif /* ENABLE_SNMP */
  140. static char snmp_manager_buf[CS_MAX_NAME_LENGTH];
  141. static char *snmp_manager = NULL;
  142. static char snmp_community_buf[CS_MAX_NAME_LENGTH];
  143. static char *snmp_community = NULL;
  144. #define CMAP_MAX_RETRIES 10
  145. /*
  146. * cmap
  147. */
  148. static cmap_handle_t cmap_handle;
  149. static int32_t _cs_ip_to_hostname(char* ip, char* name_out)
  150. {
  151. struct sockaddr_in sa;
  152. int rc;
  153. if (strchr(ip, ':') == NULL) {
  154. sa.sin_family = AF_INET;
  155. } else {
  156. sa.sin_family = AF_INET6;
  157. }
  158. rc = inet_pton(sa.sin_family, ip, &sa.sin_addr);
  159. if (rc == 0) {
  160. return -EINVAL;
  161. }
  162. rc = getnameinfo((struct sockaddr*)&sa, sizeof(sa),
  163. name_out, CS_MAX_NAME_LENGTH, NULL, 0, 0);
  164. if (rc != 0) {
  165. qb_log(LOG_ERR, "error looking up %s : %s", ip, gai_strerror(rc));
  166. return -EINVAL;
  167. }
  168. return 0;
  169. }
  170. static void _cs_cmap_members_key_changed (
  171. cmap_handle_t cmap_handle_c,
  172. cmap_track_handle_t cmap_track_handle,
  173. int32_t event,
  174. const char *key_name,
  175. struct cmap_notify_value new_value,
  176. struct cmap_notify_value old_value,
  177. void *user_data)
  178. {
  179. char nodename[CS_MAX_NAME_LENGTH];
  180. char* open_bracket = NULL;
  181. char* close_bracket = NULL;
  182. int res;
  183. uint32_t nodeid;
  184. char *ip_str;
  185. char tmp_key[CMAP_KEYNAME_MAXLEN];
  186. cs_error_t err;
  187. int no_retries;
  188. if (event != CMAP_TRACK_ADD && event != CMAP_TRACK_MODIFY) {
  189. return ;
  190. }
  191. if (NULL == key_name) {
  192. qb_log(LOG_ERR, "key_name: nil");
  193. }
  194. res = sscanf(key_name, "runtime.totem.pg.mrp.srp.members.%u.%s", &nodeid, tmp_key);
  195. if (res != 2)
  196. return ;
  197. if (strcmp(tmp_key, "status") != 0) {
  198. return ;
  199. }
  200. res = snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "runtime.totem.pg.mrp.srp.members.%u.ip", nodeid);
  201. if (res <= 0 || res >= CMAP_KEYNAME_MAXLEN) {
  202. qb_log(LOG_ERR, "temp_key: failed, res: %d, nodeid: %u", res, nodeid);
  203. return ;
  204. }
  205. no_retries = 0;
  206. while ((err = cmap_get_string(cmap_handle, tmp_key, &ip_str)) == CS_ERR_TRY_AGAIN &&
  207. no_retries++ < CMAP_MAX_RETRIES) {
  208. sleep(1);
  209. }
  210. if (err != CS_OK) {
  211. return ;
  212. }
  213. /*
  214. * We want the ip out of: "r(0) ip(192.168.100.92)"
  215. */
  216. open_bracket = strrchr(ip_str, '(');
  217. if (NULL == open_bracket) {
  218. qb_log(LOG_ERR, "ip_str: %s", ip_str);
  219. free(ip_str);
  220. return ;
  221. }
  222. open_bracket++;
  223. close_bracket = strchr(open_bracket, ')');
  224. if (NULL == close_bracket) {
  225. qb_log(LOG_ERR, "open_bracket: %s", open_bracket);
  226. free(ip_str);
  227. return ;
  228. }
  229. *close_bracket = '\0';
  230. res = _cs_ip_to_hostname(open_bracket, nodename);
  231. if (res) {
  232. strncpy(nodename, open_bracket, CS_MAX_NAME_LENGTH-1);
  233. }
  234. _cs_node_membership_event(nodename, nodeid, (char *)new_value.data, open_bracket);
  235. free(ip_str);
  236. }
  237. static void _cs_cmap_connections_key_changed (
  238. cmap_handle_t cmap_handle_c,
  239. cmap_track_handle_t cmap_track_handle,
  240. int32_t event,
  241. const char *key_name,
  242. struct cmap_notify_value new_value,
  243. struct cmap_notify_value old_value,
  244. void *user_data)
  245. {
  246. char obj_name[CS_MAX_NAME_LENGTH];
  247. char conn_str[CMAP_KEYNAME_MAXLEN];
  248. char tmp_key[CMAP_KEYNAME_MAXLEN];
  249. int res;
  250. res = sscanf(key_name, "runtime.connections.%[^.].%s", conn_str, tmp_key);
  251. if (res != 2) {
  252. return ;
  253. }
  254. if (strcmp(tmp_key, "service_id") != 0) {
  255. return ;
  256. }
  257. snprintf(obj_name, CS_MAX_NAME_LENGTH, "%s", conn_str);
  258. if (event == CMAP_TRACK_ADD) {
  259. _cs_application_connection_event(obj_name, "connected");
  260. }
  261. if (event == CMAP_TRACK_DELETE) {
  262. _cs_application_connection_event(obj_name, "disconnected");
  263. }
  264. }
  265. static void _cs_cmap_rrp_faulty_key_changed (
  266. cmap_handle_t cmap_handle_c,
  267. cmap_track_handle_t cmap_track_handle,
  268. int32_t event,
  269. const char *key_name,
  270. struct cmap_notify_value new_value,
  271. struct cmap_notify_value old_value,
  272. void *user_data)
  273. {
  274. uint32_t iface_no;
  275. char tmp_key[CMAP_KEYNAME_MAXLEN];
  276. int res;
  277. int no_retries;
  278. uint8_t faulty;
  279. cs_error_t err;
  280. res = sscanf(key_name, "runtime.totem.pg.mrp.rrp.%u.%s", &iface_no, tmp_key);
  281. if (res != 2) {
  282. return ;
  283. }
  284. if (strcmp(tmp_key, "faulty") != 0) {
  285. return ;
  286. }
  287. no_retries = 0;
  288. while ((err = cmap_get_uint8(cmap_handle, key_name, &faulty)) == CS_ERR_TRY_AGAIN &&
  289. no_retries++ < CMAP_MAX_RETRIES) {
  290. sleep(1);
  291. }
  292. if (err != CS_OK) {
  293. return ;
  294. }
  295. if (faulty) {
  296. _cs_rrp_faulty_event(iface_no, "faulty");
  297. } else {
  298. _cs_rrp_faulty_event(iface_no, "operational");
  299. }
  300. }
  301. static int
  302. _cs_cmap_dispatch(int fd, int revents, void *data)
  303. {
  304. cs_error_t err;
  305. err = cmap_dispatch(cmap_handle, CS_DISPATCH_ONE);
  306. if (err != CS_OK && err != CS_ERR_TRY_AGAIN && err != CS_ERR_TIMEOUT &&
  307. err != CS_ERR_QUEUE_FULL) {
  308. qb_log(LOG_ERR, "Could not dispatch cmap events. Error %u", err);
  309. qb_loop_stop(main_loop);
  310. exit_code = 1;
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. static void _cs_quorum_notification(quorum_handle_t handle,
  316. uint32_t quorate, uint64_t ring_seq,
  317. uint32_t view_list_entries, uint32_t *view_list)
  318. {
  319. if (_cs_is_quorate == quorate) {
  320. return;
  321. }
  322. _cs_is_quorate = quorate;
  323. if (quorate) {
  324. _cs_node_quorum_event("quorate");
  325. } else {
  326. _cs_node_quorum_event("not quorate");
  327. }
  328. }
  329. static int
  330. _cs_quorum_dispatch(int fd, int revents, void *data)
  331. {
  332. cs_error_t err;
  333. err = quorum_dispatch(quorum_handle, CS_DISPATCH_ONE);
  334. if (err != CS_OK && err != CS_ERR_TRY_AGAIN && err != CS_ERR_TIMEOUT &&
  335. err != CS_ERR_QUEUE_FULL) {
  336. qb_log(LOG_ERR, "Could not dispatch quorum events. Error %u", err);
  337. qb_loop_stop(main_loop);
  338. exit_code = 1;
  339. return -1;
  340. }
  341. return 0;
  342. }
  343. static void
  344. _cs_quorum_init(void)
  345. {
  346. cs_error_t rc;
  347. uint32_t quorum_type;
  348. int fd;
  349. quorum_callbacks_t quorum_callbacks = {
  350. .quorum_notify_fn = _cs_quorum_notification,
  351. };
  352. rc = quorum_initialize (&quorum_handle, &quorum_callbacks,
  353. &quorum_type);
  354. if (rc != CS_OK) {
  355. qb_log(LOG_ERR, "Could not connect to corosync(quorum)");
  356. return;
  357. }
  358. quorum_fd_get(quorum_handle, &fd);
  359. qb_loop_poll_add(main_loop, QB_LOOP_MED, fd, POLLIN|POLLNVAL, NULL,
  360. _cs_quorum_dispatch);
  361. rc = quorum_trackstart(quorum_handle, CS_TRACK_CHANGES);
  362. if (rc != CS_OK) {
  363. qb_log(LOG_ERR, "Could not start tracking");
  364. return;
  365. }
  366. }
  367. static void
  368. _cs_quorum_finalize(void)
  369. {
  370. quorum_finalize (quorum_handle);
  371. }
  372. #ifdef HAVE_DBUS
  373. /*
  374. * dbus notifications
  375. */
  376. static void
  377. _cs_dbus_auto_flush(void)
  378. {
  379. dbus_connection_ref(db);
  380. while (dbus_connection_get_dispatch_status(db) == DBUS_DISPATCH_DATA_REMAINS) {
  381. dbus_connection_dispatch(db);
  382. }
  383. while (dbus_connection_has_messages_to_send(db)) {
  384. dbus_connection_flush(db);
  385. }
  386. dbus_connection_unref(db);
  387. }
  388. static void
  389. _cs_dbus_release(void)
  390. {
  391. DBusError err;
  392. if (!db)
  393. return;
  394. dbus_error_init(&err);
  395. dbus_bus_release_name(db, DBUS_CS_NAME, &err);
  396. dbus_error_free(&err);
  397. dbus_connection_unref(db);
  398. db = NULL;
  399. }
  400. static void
  401. _cs_dbus_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  402. {
  403. DBusMessage *msg = NULL;
  404. if (err_set) {
  405. qb_log(LOG_ERR, "%s", _err);
  406. err_set = 0;
  407. }
  408. if (!db) {
  409. goto out_free;
  410. }
  411. if (dbus_connection_get_is_connected(db) != TRUE) {
  412. err_set = 1;
  413. snprintf(_err, sizeof(_err), "DBus connection lost");
  414. _cs_dbus_release();
  415. goto out_unlock;
  416. }
  417. _cs_dbus_auto_flush();
  418. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  419. DBUS_CS_IFACE,
  420. "QuorumStateChange"))) {
  421. qb_log(LOG_ERR, "error creating dbus signal");
  422. goto out_unlock;
  423. }
  424. if (!dbus_message_append_args(msg,
  425. DBUS_TYPE_STRING, &nodename,
  426. DBUS_TYPE_UINT32, &nodeid,
  427. DBUS_TYPE_STRING, &state,
  428. DBUS_TYPE_INVALID)) {
  429. qb_log(LOG_ERR, "error adding args to quorum signal");
  430. goto out_unlock;
  431. }
  432. dbus_connection_send(db, msg, NULL);
  433. out_unlock:
  434. if (msg) {
  435. dbus_message_unref(msg);
  436. }
  437. out_free:
  438. return;
  439. }
  440. static void
  441. _cs_dbus_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  442. {
  443. DBusMessage *msg = NULL;
  444. if (err_set) {
  445. qb_log(LOG_ERR, "%s", _err);
  446. err_set = 0;
  447. }
  448. if (!db) {
  449. goto out_free;
  450. }
  451. if (dbus_connection_get_is_connected(db) != TRUE) {
  452. err_set = 1;
  453. snprintf(_err, sizeof(_err), "DBus connection lost");
  454. _cs_dbus_release();
  455. goto out_unlock;
  456. }
  457. _cs_dbus_auto_flush();
  458. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  459. DBUS_CS_IFACE,
  460. "NodeStateChange"))) {
  461. qb_log(LOG_ERR, "error creating NodeStateChange signal");
  462. goto out_unlock;
  463. }
  464. if (!dbus_message_append_args(msg,
  465. DBUS_TYPE_STRING, &nodename,
  466. DBUS_TYPE_UINT32, &nodeid,
  467. DBUS_TYPE_STRING, &ip,
  468. DBUS_TYPE_STRING, &state,
  469. DBUS_TYPE_INVALID)) {
  470. qb_log(LOG_ERR, "error adding args to NodeStateChange signal");
  471. goto out_unlock;
  472. }
  473. dbus_connection_send(db, msg, NULL);
  474. out_unlock:
  475. if (msg) {
  476. dbus_message_unref(msg);
  477. }
  478. out_free:
  479. return;
  480. }
  481. static void
  482. _cs_dbus_application_connection_event(char *nodename, uint32_t nodeid, char *app_name, const char *state)
  483. {
  484. DBusMessage *msg = NULL;
  485. if (err_set) {
  486. qb_log(LOG_ERR, "%s", _err);
  487. err_set = 0;
  488. }
  489. if (!db) {
  490. goto out_free;
  491. }
  492. if (dbus_connection_get_is_connected(db) != TRUE) {
  493. err_set = 1;
  494. snprintf(_err, sizeof(_err), "DBus connection lost");
  495. _cs_dbus_release();
  496. goto out_unlock;
  497. }
  498. _cs_dbus_auto_flush();
  499. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  500. DBUS_CS_IFACE,
  501. "ConnectionStateChange"))) {
  502. qb_log(LOG_ERR, "error creating ConnectionStateChange signal");
  503. goto out_unlock;
  504. }
  505. if (!dbus_message_append_args(msg,
  506. DBUS_TYPE_STRING, &nodename,
  507. DBUS_TYPE_UINT32, &nodeid,
  508. DBUS_TYPE_STRING, &app_name,
  509. DBUS_TYPE_STRING, &state,
  510. DBUS_TYPE_INVALID)) {
  511. qb_log(LOG_ERR, "error adding args to ConnectionStateChange signal");
  512. goto out_unlock;
  513. }
  514. dbus_connection_send(db, msg, NULL);
  515. out_unlock:
  516. if (msg) {
  517. dbus_message_unref(msg);
  518. }
  519. out_free:
  520. return;
  521. }
  522. static void
  523. _cs_dbus_rrp_faulty_event(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state)
  524. {
  525. DBusMessage *msg = NULL;
  526. if (err_set) {
  527. qb_log(LOG_ERR, "%s", _err);
  528. err_set = 0;
  529. }
  530. if (!db) {
  531. goto out_free;
  532. }
  533. if (dbus_connection_get_is_connected(db) != TRUE) {
  534. err_set = 1;
  535. snprintf(_err, sizeof(_err), "DBus connection lost");
  536. _cs_dbus_release();
  537. goto out_unlock;
  538. }
  539. _cs_dbus_auto_flush();
  540. if (!(msg = dbus_message_new_signal(DBUS_CS_PATH,
  541. DBUS_CS_IFACE,
  542. "QuorumStateChange"))) {
  543. qb_log(LOG_ERR, "error creating dbus signal");
  544. goto out_unlock;
  545. }
  546. if (!dbus_message_append_args(msg,
  547. DBUS_TYPE_STRING, &nodename,
  548. DBUS_TYPE_UINT32, &nodeid,
  549. DBUS_TYPE_UINT32, &iface_no,
  550. DBUS_TYPE_STRING, &state,
  551. DBUS_TYPE_INVALID)) {
  552. qb_log(LOG_ERR, "error adding args to rrp signal");
  553. goto out_unlock;
  554. }
  555. dbus_connection_send(db, msg, NULL);
  556. out_unlock:
  557. if (msg) {
  558. dbus_message_unref(msg);
  559. }
  560. out_free:
  561. return;
  562. }
  563. static void
  564. _cs_dbus_init(void)
  565. {
  566. DBusConnection *dbc = NULL;
  567. DBusError err;
  568. dbus_error_init(&err);
  569. dbc = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
  570. if (!dbc) {
  571. snprintf(_err, sizeof(_err),
  572. "dbus_bus_get: %s", err.message);
  573. err_set = 1;
  574. dbus_error_free(&err);
  575. return;
  576. }
  577. dbus_connection_set_exit_on_disconnect(dbc, FALSE);
  578. db = dbc;
  579. notifiers[num_notifiers].node_membership_fn =
  580. _cs_dbus_node_membership_event;
  581. notifiers[num_notifiers].node_quorum_fn =
  582. _cs_dbus_node_quorum_event;
  583. notifiers[num_notifiers].application_connection_fn =
  584. _cs_dbus_application_connection_event;
  585. notifiers[num_notifiers].rrp_faulty_fn =
  586. _cs_dbus_rrp_faulty_event;
  587. num_notifiers++;
  588. }
  589. #endif /* HAVE_DBUS */
  590. #ifdef ENABLE_SNMP
  591. static netsnmp_session *snmp_init (const char *target)
  592. {
  593. static netsnmp_session *session = NULL;
  594. #ifndef NETSNMPV54
  595. char default_port[128];
  596. snprintf (default_port, sizeof (default_port), "%s:162", target);
  597. #endif
  598. if (session) {
  599. return (session);
  600. }
  601. if (target == NULL) {
  602. return NULL;
  603. }
  604. session = malloc (sizeof (netsnmp_session));
  605. snmp_sess_init (session);
  606. session->version = SNMP_VERSION_2c;
  607. session->callback = NULL;
  608. session->callback_magic = NULL;
  609. if (snmp_community) {
  610. session->community = (u_char *)snmp_community;
  611. session->community_len = strlen(snmp_community_buf);
  612. }
  613. session = snmp_add(session,
  614. #ifdef NETSNMPV54
  615. netsnmp_transport_open_client ("snmptrap", target),
  616. #else
  617. netsnmp_tdomain_transport (default_port, 0, "udp"),
  618. #endif
  619. NULL, NULL);
  620. if (session == NULL) {
  621. qb_log(LOG_ERR, 0, "Could not create snmp transport");
  622. }
  623. return (session);
  624. }
  625. static inline void add_field (
  626. netsnmp_pdu *trap_pdu,
  627. u_char asn_type,
  628. const char *prefix,
  629. void *value,
  630. size_t value_size)
  631. {
  632. oid _oid[MAX_OID_LEN];
  633. size_t _oid_len = MAX_OID_LEN;
  634. if (snmp_parse_oid(prefix, _oid, &_oid_len)) {
  635. snmp_pdu_add_variable (trap_pdu, _oid, _oid_len, asn_type, (u_char *) value, value_size);
  636. }
  637. }
  638. static void
  639. _cs_snmp_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  640. {
  641. int ret;
  642. char csysuptime[CS_TIMESTAMP_STR_LEN];
  643. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  644. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  645. time_t now = time (NULL);
  646. netsnmp_pdu *trap_pdu;
  647. netsnmp_session *session = snmp_init (snmp_manager);
  648. if (session == NULL) {
  649. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  650. return ;
  651. }
  652. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  653. if (!trap_pdu) {
  654. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  655. return ;
  656. }
  657. /* send uptime */
  658. snprintf (csysuptime, CS_TIMESTAMP_STR_LEN, "%ld", now);
  659. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  660. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_NODE);
  661. /* Add extries to the trap */
  662. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  663. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  664. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_ADDR, (void*)ip, strlen (ip));
  665. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_STATUS, (void*)state, strlen (state));
  666. /* Send and cleanup */
  667. ret = snmp_send (session, trap_pdu);
  668. if (ret == 0) {
  669. /* error */
  670. qb_log(LOG_ERR, "Could not send SNMP trap");
  671. snmp_free_pdu (trap_pdu);
  672. }
  673. }
  674. static void
  675. _cs_snmp_node_quorum_event(char *nodename, uint32_t nodeid,
  676. const char *state)
  677. {
  678. int ret;
  679. char csysuptime[20];
  680. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  681. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  682. time_t now = time (NULL);
  683. netsnmp_pdu *trap_pdu;
  684. netsnmp_session *session = snmp_init (snmp_manager);
  685. if (session == NULL) {
  686. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  687. return ;
  688. }
  689. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  690. if (!trap_pdu) {
  691. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  692. return ;
  693. }
  694. /* send uptime */
  695. sprintf (csysuptime, "%ld", now);
  696. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  697. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_QUORUM);
  698. /* Add extries to the trap */
  699. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  700. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  701. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_QUORUM, (void*)state, strlen (state));
  702. /* Send and cleanup */
  703. ret = snmp_send (session, trap_pdu);
  704. if (ret == 0) {
  705. /* error */
  706. qb_log(LOG_ERR, "Could not send SNMP trap");
  707. snmp_free_pdu (trap_pdu);
  708. }
  709. }
  710. static void
  711. _cs_snmp_rrp_faulty_event(char *nodename, uint32_t nodeid,
  712. uint32_t iface_no, const char *state)
  713. {
  714. int ret;
  715. char csysuptime[20];
  716. static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  717. static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 };
  718. time_t now = time (NULL);
  719. netsnmp_pdu *trap_pdu;
  720. netsnmp_session *session = snmp_init (snmp_manager);
  721. if (session == NULL) {
  722. qb_log(LOG_NOTICE, "Failed to init SNMP session.");
  723. return ;
  724. }
  725. trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2);
  726. if (!trap_pdu) {
  727. qb_log(LOG_NOTICE, "Failed to create SNMP notification.");
  728. return ;
  729. }
  730. /* send uptime */
  731. sprintf (csysuptime, "%ld", now);
  732. snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime);
  733. snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_RRP);
  734. /* Add extries to the trap */
  735. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename));
  736. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid));
  737. add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_RRP_IFACE_NO, (void*)&iface_no, sizeof (iface_no));
  738. add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_RRP_STATUS, (void*)state, strlen (state));
  739. /* Send and cleanup */
  740. ret = snmp_send (session, trap_pdu);
  741. if (ret == 0) {
  742. /* error */
  743. qb_log(LOG_ERR, "Could not send SNMP trap");
  744. snmp_free_pdu (trap_pdu);
  745. }
  746. }
  747. static void
  748. _cs_snmp_init(void)
  749. {
  750. if (snmp_manager == NULL) {
  751. snmp_manager = (char*)local_host;
  752. }
  753. notifiers[num_notifiers].node_membership_fn =
  754. _cs_snmp_node_membership_event;
  755. notifiers[num_notifiers].node_quorum_fn =
  756. _cs_snmp_node_quorum_event;
  757. notifiers[num_notifiers].application_connection_fn = NULL;
  758. notifiers[num_notifiers].rrp_faulty_fn =
  759. _cs_snmp_rrp_faulty_event;
  760. num_notifiers++;
  761. }
  762. #endif /* ENABLE_SNMP */
  763. static void
  764. _cs_syslog_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  765. {
  766. qb_log(LOG_NOTICE, "%s[%d] ip:%s %s", nodename, nodeid, ip, state);
  767. }
  768. static void
  769. _cs_syslog_node_quorum_event(char *nodename, uint32_t nodeid, const char *state)
  770. {
  771. if (strcmp(state, "quorate") == 0) {
  772. qb_log(LOG_NOTICE, "%s[%d] is now %s", nodename, nodeid, state);
  773. } else {
  774. qb_log(LOG_NOTICE, "%s[%d] has lost quorum", nodename, nodeid);
  775. }
  776. }
  777. static void
  778. _cs_syslog_application_connection_event(char *nodename, uint32_t nodeid, char* app_name, const char *state)
  779. {
  780. if (strcmp(state, "connected") == 0) {
  781. qb_log(LOG_NOTICE, "%s[%d] %s is now %s to corosync", nodename, nodeid, app_name, state);
  782. } else {
  783. qb_log(LOG_NOTICE, "%s[%d] %s is now %s from corosync", nodename, nodeid, app_name, state);
  784. }
  785. }
  786. static void
  787. _cs_syslog_rrp_faulty_event(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state)
  788. {
  789. qb_log(LOG_NOTICE, "%s[%d] interface %u is now %s", nodename, nodeid, iface_no, state);
  790. }
  791. static void
  792. _cs_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip)
  793. {
  794. int i;
  795. for (i = 0; i < num_notifiers; i++) {
  796. if (notifiers[i].node_membership_fn) {
  797. notifiers[i].node_membership_fn(nodename, nodeid, state, ip);
  798. }
  799. }
  800. }
  801. static void
  802. _cs_local_node_info_get(char **nodename, uint32_t *nodeid)
  803. {
  804. cs_error_t rc;
  805. corosync_cfg_handle_t cfg_handle;
  806. if (g_local_nodeid == 0) {
  807. rc = corosync_cfg_initialize(&cfg_handle, NULL);
  808. if (rc != CS_OK) {
  809. syslog (LOG_ERR, "Failed to initialize the cfg API. Error %d\n", rc);
  810. exit (EXIT_FAILURE);
  811. }
  812. rc = corosync_cfg_local_get (cfg_handle, &g_local_nodeid);
  813. corosync_cfg_finalize(cfg_handle);
  814. if (rc != CS_OK) {
  815. g_local_nodeid = 0;
  816. strncpy(local_nodename, "localhost", sizeof (local_nodename));
  817. local_nodename[sizeof (local_nodename) - 1] = '\0';
  818. } else {
  819. gethostname(local_nodename, CS_MAX_NAME_LENGTH);
  820. }
  821. }
  822. *nodeid = g_local_nodeid;
  823. *nodename = local_nodename;
  824. }
  825. static void
  826. _cs_node_quorum_event(const char *state)
  827. {
  828. int i;
  829. char *nodename;
  830. uint32_t nodeid;
  831. _cs_local_node_info_get(&nodename, &nodeid);
  832. for (i = 0; i < num_notifiers; i++) {
  833. if (notifiers[i].node_quorum_fn) {
  834. notifiers[i].node_quorum_fn(nodename, nodeid, state);
  835. }
  836. }
  837. }
  838. static void
  839. _cs_application_connection_event(char *app_name, const char *state)
  840. {
  841. int i;
  842. char *nodename;
  843. uint32_t nodeid;
  844. _cs_local_node_info_get(&nodename, &nodeid);
  845. for (i = 0; i < num_notifiers; i++) {
  846. if (notifiers[i].application_connection_fn) {
  847. notifiers[i].application_connection_fn(nodename, nodeid, app_name, state);
  848. }
  849. }
  850. }
  851. static void
  852. _cs_rrp_faulty_event(uint32_t iface_no, const char *state)
  853. {
  854. int i;
  855. char *nodename;
  856. uint32_t nodeid;
  857. _cs_local_node_info_get(&nodename, &nodeid);
  858. for (i = 0; i < num_notifiers; i++) {
  859. if (notifiers[i].rrp_faulty_fn) {
  860. notifiers[i].rrp_faulty_fn(nodename, nodeid, iface_no, state);
  861. }
  862. }
  863. }
  864. static int32_t
  865. sig_exit_handler(int32_t num, void *data)
  866. {
  867. qb_loop_stop(main_loop);
  868. return 0;
  869. }
  870. static void
  871. _cs_cmap_init(void)
  872. {
  873. cs_error_t rc;
  874. int cmap_fd = 0;
  875. cmap_track_handle_t track_handle;
  876. rc = cmap_initialize (&cmap_handle);
  877. if (rc != CS_OK) {
  878. qb_log(LOG_ERR, "Failed to initialize the cmap API. Error %d", rc);
  879. exit (EXIT_FAILURE);
  880. }
  881. cmap_fd_get(cmap_handle, &cmap_fd);
  882. qb_loop_poll_add(main_loop, QB_LOOP_MED, cmap_fd, POLLIN|POLLNVAL, NULL,
  883. _cs_cmap_dispatch);
  884. rc = cmap_track_add(cmap_handle, "runtime.connections.",
  885. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX,
  886. _cs_cmap_connections_key_changed,
  887. NULL,
  888. &track_handle);
  889. if (rc != CS_OK) {
  890. qb_log(LOG_ERR,
  891. "Failed to track the connections key. Error %d", rc);
  892. exit (EXIT_FAILURE);
  893. }
  894. rc = cmap_track_add(cmap_handle, "runtime.totem.pg.mrp.srp.members.",
  895. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  896. _cs_cmap_members_key_changed,
  897. NULL,
  898. &track_handle);
  899. if (rc != CS_OK) {
  900. qb_log(LOG_ERR,
  901. "Failed to track the members key. Error %d", rc);
  902. exit (EXIT_FAILURE);
  903. }
  904. rc = cmap_track_add(cmap_handle, "runtime.totem.pg.mrp.rrp.",
  905. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  906. _cs_cmap_rrp_faulty_key_changed,
  907. NULL,
  908. &track_handle);
  909. if (rc != CS_OK) {
  910. qb_log(LOG_ERR,
  911. "Failed to track the rrp key. Error %d", rc);
  912. exit (EXIT_FAILURE);
  913. }
  914. }
  915. static void
  916. _cs_cmap_finalize(void)
  917. {
  918. cmap_finalize (cmap_handle);
  919. }
  920. static void
  921. _cs_check_config(void)
  922. {
  923. if (conf[CS_NTF_LOG] == QB_FALSE &&
  924. conf[CS_NTF_STDOUT] == QB_FALSE &&
  925. conf[CS_NTF_SNMP] == QB_FALSE &&
  926. conf[CS_NTF_DBUS] == QB_FALSE) {
  927. qb_log(LOG_ERR, "no event type enabled, see corosync-notifyd -h, exiting.");
  928. exit(EXIT_FAILURE);
  929. }
  930. #ifndef ENABLE_SNMP
  931. if (conf[CS_NTF_SNMP]) {
  932. qb_log(LOG_ERR, "Not compiled with SNMP support enabled, exiting.");
  933. exit(EXIT_FAILURE);
  934. }
  935. #endif
  936. #ifndef HAVE_DBUS
  937. if (conf[CS_NTF_DBUS]) {
  938. qb_log(LOG_ERR, "Not compiled with DBus support enabled, exiting.");
  939. exit(EXIT_FAILURE);
  940. }
  941. #endif
  942. if (conf[CS_NTF_STDOUT] && !conf[CS_NTF_FG]) {
  943. qb_log(LOG_ERR, "configured to print to stdout and run in the background, exiting");
  944. exit(EXIT_FAILURE);
  945. }
  946. if (conf[CS_NTF_SNMP] && conf[CS_NTF_DBUS]) {
  947. qb_log(LOG_ERR, "configured to send snmp traps and dbus signals - are you sure?.");
  948. }
  949. }
  950. static void
  951. _cs_usage(void)
  952. {
  953. fprintf(stderr, "usage:\n"\
  954. " -c : SNMP Community name.\n"\
  955. " -f : Start application in foreground.\n"\
  956. " -l : Log all events.\n"\
  957. " -o : Print events to stdout (turns on -l).\n"\
  958. " -s : Send SNMP traps on all events.\n"\
  959. " -m : Set the SNMP Manager IP address (defaults to localhost).\n"\
  960. " -d : Send DBUS signals on all events.\n"\
  961. " -h : Print this help.\n\n");
  962. }
  963. int
  964. main(int argc, char *argv[])
  965. {
  966. int ch;
  967. conf[CS_NTF_FG] = QB_FALSE;
  968. conf[CS_NTF_LOG] = QB_FALSE;
  969. conf[CS_NTF_STDOUT] = QB_FALSE;
  970. conf[CS_NTF_SNMP] = QB_FALSE;
  971. conf[CS_NTF_DBUS] = QB_FALSE;
  972. while ((ch = getopt (argc, argv, "c:floshdm:")) != EOF) {
  973. switch (ch) {
  974. case 'c':
  975. strncpy(snmp_community_buf, optarg, sizeof (snmp_community_buf));
  976. snmp_community_buf[sizeof (snmp_community_buf) - 1] = '\0';
  977. snmp_community = snmp_community_buf;
  978. break;
  979. case 'f':
  980. conf[CS_NTF_FG] = QB_TRUE;
  981. break;
  982. case 'l':
  983. conf[CS_NTF_LOG] = QB_TRUE;
  984. break;
  985. case 'm':
  986. conf[CS_NTF_SNMP] = QB_TRUE;
  987. strncpy(snmp_manager_buf, optarg, sizeof (snmp_manager_buf));
  988. snmp_manager_buf[sizeof (snmp_manager_buf) - 1] = '\0';
  989. snmp_manager = snmp_manager_buf;
  990. break;
  991. case 'o':
  992. conf[CS_NTF_LOG] = QB_TRUE;
  993. conf[CS_NTF_STDOUT] = QB_TRUE;
  994. break;
  995. case 's':
  996. conf[CS_NTF_SNMP] = QB_TRUE;
  997. break;
  998. case 'd':
  999. conf[CS_NTF_DBUS] = QB_TRUE;
  1000. break;
  1001. case 'h':
  1002. default:
  1003. _cs_usage();
  1004. return EXIT_FAILURE;
  1005. }
  1006. }
  1007. qb_log_init("notifyd", LOG_DAEMON, LOG_INFO);
  1008. if (conf[CS_NTF_STDOUT]) {
  1009. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  1010. QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
  1011. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, conf[CS_NTF_STDOUT]);
  1012. }
  1013. _cs_check_config();
  1014. if (!conf[CS_NTF_FG]) {
  1015. if (daemon(0, 0) < 0)
  1016. {
  1017. perror("daemon() failed");
  1018. return EXIT_FAILURE;
  1019. }
  1020. }
  1021. num_notifiers = 0;
  1022. if (conf[CS_NTF_LOG]) {
  1023. notifiers[num_notifiers].node_membership_fn =
  1024. _cs_syslog_node_membership_event;
  1025. notifiers[num_notifiers].node_quorum_fn =
  1026. _cs_syslog_node_quorum_event;
  1027. notifiers[num_notifiers].application_connection_fn =
  1028. _cs_syslog_application_connection_event;
  1029. notifiers[num_notifiers].rrp_faulty_fn =
  1030. _cs_syslog_rrp_faulty_event;
  1031. num_notifiers++;
  1032. }
  1033. main_loop = qb_loop_create();
  1034. _cs_cmap_init();
  1035. _cs_quorum_init();
  1036. #ifdef HAVE_DBUS
  1037. if (conf[CS_NTF_DBUS]) {
  1038. _cs_dbus_init();
  1039. }
  1040. #endif /* HAVE_DBUS */
  1041. #ifdef ENABLE_SNMP
  1042. if (conf[CS_NTF_SNMP]) {
  1043. _cs_snmp_init();
  1044. }
  1045. #endif /* ENABLE_SNMP */
  1046. qb_loop_signal_add(main_loop,
  1047. QB_LOOP_HIGH,
  1048. SIGINT,
  1049. NULL,
  1050. sig_exit_handler,
  1051. NULL);
  1052. qb_loop_signal_add(main_loop,
  1053. QB_LOOP_HIGH,
  1054. SIGQUIT,
  1055. NULL,
  1056. sig_exit_handler,
  1057. NULL);
  1058. qb_loop_signal_add(main_loop,
  1059. QB_LOOP_HIGH,
  1060. SIGTERM,
  1061. NULL,
  1062. sig_exit_handler,
  1063. NULL);
  1064. qb_loop_run(main_loop);
  1065. #ifdef HAVE_DBUS
  1066. if (conf[CS_NTF_DBUS]) {
  1067. _cs_dbus_release();
  1068. }
  1069. #endif /* HAVE_DBUS */
  1070. _cs_quorum_finalize();
  1071. _cs_cmap_finalize();
  1072. return (exit_code);
  1073. }