corosync-notifyd.c 31 KB

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