corosync-notifyd.c 31 KB

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