wd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <sys/ioctl.h>
  38. #include <linux/types.h>
  39. #include <linux/watchdog.h>
  40. #include <sys/reboot.h>
  41. #include <corosync/corotypes.h>
  42. #include <corosync/corodefs.h>
  43. #include <corosync/lcr/lcr_comp.h>
  44. #include <corosync/coroapi.h>
  45. #include <corosync/list.h>
  46. #include <corosync/logsys.h>
  47. #include <corosync/icmap.h>
  48. #include "../exec/fsm.h"
  49. typedef enum {
  50. WD_RESOURCE_GOOD,
  51. WD_RESOURCE_FAILED,
  52. WD_RESOURCE_STATE_UNKNOWN,
  53. WD_RESOURCE_NOT_MONITORED
  54. } wd_resource_state_t;
  55. struct resource {
  56. char res_path[ICMAP_KEYNAME_MAXLEN];
  57. char *recovery;
  58. char name[CS_MAX_NAME_LENGTH];
  59. time_t last_updated;
  60. struct cs_fsm fsm;
  61. corosync_timer_handle_t check_timer;
  62. uint64_t check_timeout;
  63. icmap_track_t icmap_track;
  64. };
  65. LOGSYS_DECLARE_SUBSYS("WD");
  66. /*
  67. * Service Interfaces required by service_message_handler struct
  68. */
  69. static int wd_exec_init_fn (
  70. struct corosync_api_v1 *corosync_api);
  71. static int wd_exec_exit_fn (void);
  72. static void wd_resource_check_fn (void* resource_ref);
  73. static struct corosync_api_v1 *api;
  74. #define WD_DEFAULT_TIMEOUT_SEC 6
  75. #define WD_DEFAULT_TIMEOUT_MS (WD_DEFAULT_TIMEOUT_SEC * CS_TIME_MS_IN_SEC)
  76. #define WD_MIN_TIMEOUT_MS 500
  77. #define WD_MAX_TIMEOUT_MS (120 * CS_TIME_MS_IN_SEC)
  78. static uint32_t watchdog_timeout = WD_DEFAULT_TIMEOUT_SEC;
  79. static uint64_t tickle_timeout = (WD_DEFAULT_TIMEOUT_MS / 2);
  80. static int dog = -1;
  81. static corosync_timer_handle_t wd_timer;
  82. static int watchdog_ok = 1;
  83. struct corosync_service_engine wd_service_engine = {
  84. .name = "corosync watchdog service",
  85. .id = WD_SERVICE,
  86. .priority = 1,
  87. .private_data_size = 0,
  88. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  89. .lib_init_fn = NULL,
  90. .lib_exit_fn = NULL,
  91. .lib_engine = NULL,
  92. .lib_engine_count = 0,
  93. .exec_engine = NULL,
  94. .exec_engine_count = 0,
  95. .confchg_fn = NULL,
  96. .exec_init_fn = wd_exec_init_fn,
  97. .exec_exit_fn = wd_exec_exit_fn,
  98. .exec_dump_fn = NULL,
  99. .sync_mode = CS_SYNC_V2
  100. };
  101. static DECLARE_LIST_INIT (confchg_notify);
  102. /*
  103. * F S M
  104. */
  105. static void wd_config_changed (struct cs_fsm* fsm, int32_t event, void * data);
  106. static void wd_resource_failed (struct cs_fsm* fsm, int32_t event, void * data);
  107. enum wd_resource_state {
  108. WD_S_RUNNING,
  109. WD_S_FAILED,
  110. WD_S_STOPPED
  111. };
  112. enum wd_resource_event {
  113. WD_E_FAILURE,
  114. WD_E_CONFIG_CHANGED
  115. };
  116. const char * wd_running_str = "running";
  117. const char * wd_failed_str = "failed";
  118. const char * wd_failure_str = "failure";
  119. const char * wd_stopped_str = "stopped";
  120. const char * wd_config_changed_str = "config_changed";
  121. struct cs_fsm_entry wd_fsm_table[] = {
  122. { WD_S_STOPPED, WD_E_CONFIG_CHANGED, wd_config_changed, {WD_S_STOPPED, WD_S_RUNNING, -1} },
  123. { WD_S_STOPPED, WD_E_FAILURE, NULL, {-1} },
  124. { WD_S_RUNNING, WD_E_CONFIG_CHANGED, wd_config_changed, {WD_S_RUNNING, WD_S_STOPPED, -1} },
  125. { WD_S_RUNNING, WD_E_FAILURE, wd_resource_failed, {WD_S_FAILED, -1} },
  126. { WD_S_FAILED, WD_E_CONFIG_CHANGED, wd_config_changed, {WD_S_RUNNING, WD_S_STOPPED, -1} },
  127. { WD_S_FAILED, WD_E_FAILURE, NULL, {-1} },
  128. };
  129. /*
  130. * Dynamic loading descriptor
  131. */
  132. static struct corosync_service_engine *wd_get_service_engine_ver0 (void);
  133. static struct corosync_service_engine_iface_ver0 wd_service_engine_iface = {
  134. .corosync_get_service_engine_ver0 = wd_get_service_engine_ver0
  135. };
  136. static struct lcr_iface corosync_wd_ver0[1] = {
  137. {
  138. .name = "corosync_wd",
  139. .version = 0,
  140. .versions_replace = 0,
  141. .versions_replace_count = 0,
  142. .dependencies = 0,
  143. .dependency_count = 0,
  144. .constructor = NULL,
  145. .destructor = NULL,
  146. .interfaces = NULL,
  147. }
  148. };
  149. static struct lcr_comp wd_comp_ver0 = {
  150. .iface_count = 1,
  151. .ifaces = corosync_wd_ver0
  152. };
  153. static struct corosync_service_engine *wd_get_service_engine_ver0 (void)
  154. {
  155. return (&wd_service_engine);
  156. }
  157. #ifdef COROSYNC_SOLARIS
  158. void corosync_lcr_component_register (void);
  159. void corosync_lcr_component_register (void) {
  160. #else
  161. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  162. #endif
  163. lcr_interfaces_set (&corosync_wd_ver0[0], &wd_service_engine_iface);
  164. lcr_component_register (&wd_comp_ver0);
  165. }
  166. static const char * wd_res_state_to_str(struct cs_fsm* fsm,
  167. int32_t state)
  168. {
  169. switch (state) {
  170. case WD_S_STOPPED:
  171. return wd_stopped_str;
  172. break;
  173. case WD_S_RUNNING:
  174. return wd_running_str;
  175. break;
  176. case WD_S_FAILED:
  177. return wd_failed_str;
  178. break;
  179. }
  180. return NULL;
  181. }
  182. static const char * wd_res_event_to_str(struct cs_fsm* fsm,
  183. int32_t event)
  184. {
  185. switch (event) {
  186. case WD_E_CONFIG_CHANGED:
  187. return wd_config_changed_str;
  188. break;
  189. case WD_E_FAILURE:
  190. return wd_failure_str;
  191. break;
  192. }
  193. return NULL;
  194. }
  195. /*
  196. * returns (CS_TRUE == OK, CS_FALSE == failed)
  197. */
  198. static int32_t wd_resource_state_is_ok (struct resource *ref)
  199. {
  200. char* state;
  201. uint64_t last_updated;
  202. uint64_t my_time;
  203. uint64_t allowed_period;
  204. char key_name[ICMAP_KEYNAME_MAXLEN];
  205. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "last_updated");
  206. if (icmap_get_uint64(key_name, &last_updated) != CS_OK) {
  207. /* key does not exist.
  208. */
  209. return CS_FALSE;
  210. }
  211. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "state");
  212. if (icmap_get_string(key_name, &state) != CS_OK || strcmp(state, "disabled") == 0) {
  213. /* key does not exist.
  214. */
  215. return CS_FALSE;
  216. }
  217. if (last_updated == 0) {
  218. /* initial value */
  219. free(state);
  220. return CS_TRUE;
  221. }
  222. my_time = cs_timestamp_get();
  223. /*
  224. * Here we check that the monitor has written a timestamp within the poll_period
  225. * plus a grace factor of (0.5 * poll_period).
  226. */
  227. allowed_period = (ref->check_timeout * MILLI_2_NANO_SECONDS * 3) / 2;
  228. if ((last_updated + allowed_period) < my_time) {
  229. log_printf (LOGSYS_LEVEL_ERROR,
  230. "last_updated %"PRIu64" ms too late, period:%"PRIu64".",
  231. (uint64_t)(my_time/MILLI_2_NANO_SECONDS - ((last_updated + allowed_period) / MILLI_2_NANO_SECONDS)),
  232. ref->check_timeout);
  233. return CS_FALSE;
  234. }
  235. if (strcmp (state, wd_failed_str) == 0) {
  236. free(state);
  237. return CS_FALSE;
  238. }
  239. free(state);
  240. return CS_TRUE;
  241. }
  242. static void wd_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
  243. {
  244. char *state;
  245. uint64_t tmp_value;
  246. uint64_t next_timeout;
  247. struct resource *ref = (struct resource*)data;
  248. char key_name[ICMAP_KEYNAME_MAXLEN];
  249. next_timeout = ref->check_timeout;
  250. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "poll_period");
  251. if (icmap_get_uint64(ref->res_path, &tmp_value) == CS_OK) {
  252. if (tmp_value >= WD_MIN_TIMEOUT_MS && tmp_value <= WD_MAX_TIMEOUT_MS) {
  253. log_printf (LOGSYS_LEVEL_DEBUG,
  254. "poll_period changing from:%"PRIu64" to %"PRIu64".",
  255. ref->check_timeout, tmp_value);
  256. /*
  257. * To easy in the transition between poll_period's we are going
  258. * to make the first timeout the bigger of the new and old value.
  259. * This is to give the monitoring system time to adjust.
  260. */
  261. next_timeout = CS_MAX(tmp_value, ref->check_timeout);
  262. ref->check_timeout = tmp_value;
  263. } else {
  264. log_printf (LOGSYS_LEVEL_WARNING,
  265. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  266. tmp_value, ref->name);
  267. }
  268. }
  269. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "recovery");
  270. if (icmap_get_string(key_name, &ref->recovery) != CS_OK) {
  271. /* key does not exist.
  272. */
  273. log_printf (LOGSYS_LEVEL_WARNING,
  274. "resource %s missing a recovery key.", ref->name);
  275. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref);
  276. return;
  277. }
  278. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "state");
  279. if (icmap_get_string(key_name, &state) != CS_OK) {
  280. /* key does not exist.
  281. */
  282. log_printf (LOGSYS_LEVEL_WARNING,
  283. "resource %s missing a state key.", ref->name);
  284. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref);
  285. return;
  286. }
  287. if (ref->check_timer) {
  288. api->timer_delete(ref->check_timer);
  289. ref->check_timer = 0;
  290. }
  291. if (strcmp(wd_stopped_str, state) == 0) {
  292. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref);
  293. } else {
  294. api->timer_add_duration(next_timeout * MILLI_2_NANO_SECONDS,
  295. ref, wd_resource_check_fn, &ref->check_timer);
  296. cs_fsm_state_set(&ref->fsm, WD_S_RUNNING, ref);
  297. }
  298. free(state);
  299. }
  300. static void wd_resource_failed (struct cs_fsm* fsm, int32_t event, void * data)
  301. {
  302. struct resource* ref = (struct resource*)data;
  303. if (ref->check_timer) {
  304. api->timer_delete(ref->check_timer);
  305. ref->check_timer = 0;
  306. }
  307. log_printf (LOGSYS_LEVEL_CRIT, "%s resource \"%s\" failed!",
  308. ref->recovery, (char*)ref->name);
  309. if (strcmp (ref->recovery, "watchdog") == 0 ||
  310. strcmp (ref->recovery, "quit") == 0) {
  311. watchdog_ok = 0;
  312. }
  313. else if (strcmp (ref->recovery, "reboot") == 0) {
  314. reboot(RB_AUTOBOOT);
  315. }
  316. else if (strcmp (ref->recovery, "shutdown") == 0) {
  317. reboot(RB_POWER_OFF);
  318. }
  319. cs_fsm_state_set(fsm, WD_S_FAILED, data);
  320. }
  321. static void wd_key_changed(
  322. int32_t event,
  323. const char *key_name,
  324. struct icmap_notify_value new_val,
  325. struct icmap_notify_value old_val,
  326. void *user_data)
  327. {
  328. struct resource* ref = (struct resource*)user_data;
  329. char *last_key_part;
  330. if (ref == NULL) {
  331. return ;
  332. }
  333. last_key_part = strrchr(key_name, '.');
  334. if (last_key_part == NULL) {
  335. return ;
  336. }
  337. last_key_part++;
  338. if (event == ICMAP_TRACK_ADD || event == ICMAP_TRACK_MODIFY) {
  339. if (strcmp(last_key_part, "last_updated") == 0 ||
  340. strcmp(last_key_part, "current") == 0) {
  341. return;
  342. }
  343. cs_fsm_process(&ref->fsm, WD_E_CONFIG_CHANGED, ref);
  344. }
  345. if (event == ICMAP_TRACK_DELETE && ref != NULL) {
  346. if (strcmp(last_key_part, "state") != 0) {
  347. return ;
  348. }
  349. log_printf (LOGSYS_LEVEL_WARNING,
  350. "resource \"%s\" deleted from cmap!",
  351. ref->name);
  352. api->timer_delete(ref->check_timer);
  353. ref->check_timer = 0;
  354. icmap_track_delete(ref->icmap_track);
  355. free(ref);
  356. }
  357. }
  358. static void wd_resource_check_fn (void* resource_ref)
  359. {
  360. struct resource* ref = (struct resource*)resource_ref;
  361. if (wd_resource_state_is_ok (ref) == CS_FALSE) {
  362. cs_fsm_process(&ref->fsm, WD_E_FAILURE, ref);
  363. return;
  364. }
  365. api->timer_add_duration(ref->check_timeout*MILLI_2_NANO_SECONDS,
  366. ref, wd_resource_check_fn, &ref->check_timer);
  367. }
  368. /*
  369. * return 0 - fully configured
  370. * return -1 - partially configured
  371. */
  372. static int32_t wd_resource_create (char *res_path, char *res_name)
  373. {
  374. char *state;
  375. uint64_t tmp_value;
  376. struct resource *ref = malloc (sizeof (struct resource));
  377. char key_name[ICMAP_KEYNAME_MAXLEN];
  378. strcpy(ref->res_path, res_path);
  379. ref->check_timeout = WD_DEFAULT_TIMEOUT_MS;
  380. ref->check_timer = 0;
  381. strcpy(ref->name, res_name);
  382. ref->fsm.name = ref->name;
  383. ref->fsm.table = wd_fsm_table;
  384. ref->fsm.entries = sizeof(wd_fsm_table) / sizeof(struct cs_fsm_entry);
  385. ref->fsm.curr_entry = 0;
  386. ref->fsm.curr_state = WD_S_STOPPED;
  387. ref->fsm.state_to_str = wd_res_state_to_str;
  388. ref->fsm.event_to_str = wd_res_event_to_str;
  389. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "poll_period");
  390. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  391. icmap_set_uint64(key_name, ref->check_timeout);
  392. } else {
  393. if (tmp_value >= WD_MIN_TIMEOUT_MS && tmp_value <= WD_MAX_TIMEOUT_MS) {
  394. ref->check_timeout = tmp_value;
  395. } else {
  396. log_printf (LOGSYS_LEVEL_WARNING,
  397. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  398. tmp_value, ref->name);
  399. }
  400. }
  401. icmap_track_add(res_path,
  402. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
  403. wd_key_changed,
  404. ref, &ref->icmap_track);
  405. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "recovery");
  406. if (icmap_get_string(key_name, &ref->recovery) != CS_OK) {
  407. /* key does not exist.
  408. */
  409. log_printf (LOGSYS_LEVEL_WARNING,
  410. "resource %s missing a recovery key.", ref->name);
  411. return -1;
  412. }
  413. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "state");
  414. if (icmap_get_string(key_name, &state) != CS_OK) {
  415. /* key does not exist.
  416. */
  417. log_printf (LOGSYS_LEVEL_WARNING,
  418. "resource %s missing a state key.", ref->name);
  419. return -1;
  420. }
  421. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "last_updated");
  422. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  423. /* key does not exist.
  424. */
  425. ref->last_updated = 0;
  426. } else {
  427. ref->last_updated = tmp_value;
  428. }
  429. /*
  430. * delay the first check to give the monitor time to start working.
  431. */
  432. tmp_value = CS_MAX(ref->check_timeout * 2, WD_DEFAULT_TIMEOUT_MS);
  433. api->timer_add_duration(tmp_value * MILLI_2_NANO_SECONDS,
  434. ref,
  435. wd_resource_check_fn, &ref->check_timer);
  436. cs_fsm_state_set(&ref->fsm, WD_S_RUNNING, ref);
  437. return 0;
  438. }
  439. static void wd_tickle_fn (void* arg)
  440. {
  441. ENTER();
  442. if (watchdog_ok) {
  443. if (dog > 0) {
  444. ioctl(dog, WDIOC_KEEPALIVE, &watchdog_ok);
  445. }
  446. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  447. wd_tickle_fn, &wd_timer);
  448. }
  449. else {
  450. log_printf (LOGSYS_LEVEL_ALERT, "NOT tickling the watchdog!");
  451. }
  452. }
  453. static void wd_resource_created_cb(
  454. int32_t event,
  455. const char *key_name,
  456. struct icmap_notify_value new_val,
  457. struct icmap_notify_value old_val,
  458. void *user_data)
  459. {
  460. char res_name[ICMAP_KEYNAME_MAXLEN];
  461. char res_type[ICMAP_KEYNAME_MAXLEN];
  462. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  463. int res;
  464. if (event != ICMAP_TRACK_ADD) {
  465. return ;
  466. }
  467. res = sscanf(key_name, "resources.%[^.].%[^.].%[^.]", res_type, res_name, tmp_key);
  468. if (res != 3) {
  469. return ;
  470. }
  471. if (strcmp(tmp_key, "state") != 0) {
  472. return ;
  473. }
  474. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "resources.%s.%s.", res_type, res_name);
  475. wd_resource_create (tmp_key, res_name);
  476. }
  477. static void wd_scan_resources (void)
  478. {
  479. int res_count = 0;
  480. icmap_track_t icmap_track;
  481. icmap_iter_t iter;
  482. const char *key_name;
  483. int res;
  484. char res_name[ICMAP_KEYNAME_MAXLEN];
  485. char res_type[ICMAP_KEYNAME_MAXLEN];
  486. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  487. ENTER();
  488. iter = icmap_iter_init("resources.");
  489. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  490. res = sscanf(key_name, "resources.%[^.].%[^.].%[^.]", res_type, res_name, tmp_key);
  491. if (res != 3) {
  492. continue ;
  493. }
  494. if (strcmp(tmp_key, "state") != 0) {
  495. continue ;
  496. }
  497. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "resources.%s.%s.", res_type, res_name);
  498. if (wd_resource_create (tmp_key, res_name) == 0) {
  499. res_count++;
  500. }
  501. }
  502. icmap_iter_finalize(iter);
  503. icmap_track_add("resources.process.", ICMAP_TRACK_ADD | ICMAP_TRACK_PREFIX,
  504. wd_resource_created_cb, NULL, &icmap_track);
  505. icmap_track_add("resources.system.", ICMAP_TRACK_ADD | ICMAP_TRACK_PREFIX,
  506. wd_resource_created_cb, NULL, &icmap_track);
  507. if (res_count == 0) {
  508. log_printf (LOGSYS_LEVEL_INFO, "no resources configured.");
  509. }
  510. }
  511. static void watchdog_timeout_apply (uint32_t new)
  512. {
  513. struct watchdog_info ident;
  514. uint32_t original_timeout = watchdog_timeout;
  515. if (new == original_timeout) {
  516. return;
  517. }
  518. watchdog_timeout = new;
  519. if (dog > 0) {
  520. ioctl(dog, WDIOC_GETSUPPORT, &ident);
  521. if (ident.options & WDIOF_SETTIMEOUT) {
  522. /* yay! the dog is trained.
  523. */
  524. ioctl(dog, WDIOC_SETTIMEOUT, &watchdog_timeout);
  525. }
  526. ioctl(dog, WDIOC_GETTIMEOUT, &watchdog_timeout);
  527. }
  528. if (watchdog_timeout == new) {
  529. tickle_timeout = (watchdog_timeout * CS_TIME_MS_IN_SEC)/ 2;
  530. /* reset the tickle timer in case it was reduced.
  531. */
  532. api->timer_delete (wd_timer);
  533. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  534. wd_tickle_fn, &wd_timer);
  535. log_printf (LOGSYS_LEVEL_DEBUG, "The Watchdog timeout is %d seconds\n", watchdog_timeout);
  536. log_printf (LOGSYS_LEVEL_DEBUG, "The tickle timeout is %"PRIu64" ms\n", tickle_timeout);
  537. } else {
  538. log_printf (LOGSYS_LEVEL_WARNING,
  539. "Could not change the Watchdog timeout from %d to %d seconds\n",
  540. original_timeout, new);
  541. }
  542. }
  543. static int setup_watchdog(void)
  544. {
  545. struct watchdog_info ident;
  546. ENTER();
  547. if (access ("/dev/watchdog", W_OK) != 0) {
  548. log_printf (LOGSYS_LEVEL_WARNING, "No Watchdog, try modprobe <a watchdog>");
  549. dog = -1;
  550. return -1;
  551. }
  552. /* here goes, lets hope they have "Magic Close"
  553. */
  554. dog = open("/dev/watchdog", O_WRONLY);
  555. if (dog == -1) {
  556. log_printf (LOGSYS_LEVEL_WARNING, "Watchdog exists but couldn't be opened.");
  557. dog = -1;
  558. return -1;
  559. }
  560. /* Right we have the dog.
  561. * Lets see what breed it is.
  562. */
  563. ioctl(dog, WDIOC_GETSUPPORT, &ident);
  564. log_printf (LOGSYS_LEVEL_INFO, "Watchdog is now been tickled by corosync.");
  565. log_printf (LOGSYS_LEVEL_DEBUG, "%s", ident.identity);
  566. watchdog_timeout_apply (watchdog_timeout);
  567. ioctl(dog, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
  568. return 0;
  569. }
  570. static void wd_top_level_key_changed(
  571. int32_t event,
  572. const char *key_name,
  573. struct icmap_notify_value new_val,
  574. struct icmap_notify_value old_val,
  575. void *user_data)
  576. {
  577. uint32_t tmp_value_32;
  578. ENTER();
  579. if (icmap_get_uint32("resources.watchdog_timeout", &tmp_value_32) != CS_OK) {
  580. if (tmp_value_32 >= 2 && tmp_value_32 <= 120) {
  581. watchdog_timeout_apply (tmp_value_32);
  582. }
  583. }
  584. else {
  585. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  586. }
  587. }
  588. static void watchdog_timeout_get_initial (void)
  589. {
  590. uint32_t tmp_value_32;
  591. icmap_track_t icmap_track;
  592. ENTER();
  593. if (icmap_get_uint32("resources.watchdog_timeout", &tmp_value_32) != CS_OK) {
  594. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  595. icmap_set_uint32("resources.watchdog_timeout", watchdog_timeout);
  596. }
  597. else {
  598. if (tmp_value_32 >= 2 && tmp_value_32 <= 120) {
  599. watchdog_timeout_apply (tmp_value_32);
  600. } else {
  601. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  602. }
  603. }
  604. icmap_track_add("resources.watchdog_timeout", ICMAP_TRACK_MODIFY,
  605. wd_top_level_key_changed, NULL, &icmap_track);
  606. }
  607. static int wd_exec_init_fn (
  608. struct corosync_api_v1 *corosync_api)
  609. {
  610. ENTER();
  611. #ifdef COROSYNC_SOLARIS
  612. logsys_subsys_init();
  613. #endif
  614. api = corosync_api;
  615. watchdog_timeout_get_initial();
  616. setup_watchdog();
  617. wd_scan_resources();
  618. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  619. wd_tickle_fn, &wd_timer);
  620. return 0;
  621. }
  622. static int wd_exec_exit_fn (void)
  623. {
  624. char magic = 'V';
  625. ENTER();
  626. if (dog > 0) {
  627. log_printf (LOGSYS_LEVEL_INFO, "magically closing the watchdog.");
  628. write (dog, &magic, 1);
  629. }
  630. return 0;
  631. }