wd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright (c) 2010-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 <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/coroapi.h>
  44. #include <corosync/list.h>
  45. #include <corosync/logsys.h>
  46. #include <corosync/icmap.h>
  47. #include "fsm.h"
  48. #include "service.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 char *wd_exec_init_fn (struct corosync_api_v1 *corosync_api);
  70. static int wd_exec_exit_fn (void);
  71. static void wd_resource_check_fn (void* resource_ref);
  72. static struct corosync_api_v1 *api;
  73. #define WD_DEFAULT_TIMEOUT_SEC 6
  74. #define WD_DEFAULT_TIMEOUT_MS (WD_DEFAULT_TIMEOUT_SEC * CS_TIME_MS_IN_SEC)
  75. #define WD_MIN_TIMEOUT_MS 500
  76. #define WD_MAX_TIMEOUT_MS (120 * CS_TIME_MS_IN_SEC)
  77. static uint32_t watchdog_timeout = WD_DEFAULT_TIMEOUT_SEC;
  78. static uint64_t tickle_timeout = (WD_DEFAULT_TIMEOUT_MS / 2);
  79. static int dog = -1;
  80. static corosync_timer_handle_t wd_timer;
  81. static int watchdog_ok = 1;
  82. struct corosync_service_engine wd_service_engine = {
  83. .name = "corosync watchdog service",
  84. .id = WD_SERVICE,
  85. .priority = 1,
  86. .private_data_size = 0,
  87. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  88. .lib_init_fn = NULL,
  89. .lib_exit_fn = NULL,
  90. .lib_engine = NULL,
  91. .lib_engine_count = 0,
  92. .exec_engine = NULL,
  93. .exec_engine_count = 0,
  94. .confchg_fn = NULL,
  95. .exec_init_fn = wd_exec_init_fn,
  96. .exec_exit_fn = wd_exec_exit_fn,
  97. .exec_dump_fn = NULL
  98. };
  99. static DECLARE_LIST_INIT (confchg_notify);
  100. /*
  101. * F S M
  102. */
  103. static void wd_config_changed (struct cs_fsm* fsm, int32_t event, void * data);
  104. static void wd_resource_failed (struct cs_fsm* fsm, int32_t event, void * data);
  105. enum wd_resource_state {
  106. WD_S_RUNNING,
  107. WD_S_FAILED,
  108. WD_S_STOPPED
  109. };
  110. enum wd_resource_event {
  111. WD_E_FAILURE,
  112. WD_E_CONFIG_CHANGED
  113. };
  114. const char * wd_running_str = "running";
  115. const char * wd_failed_str = "failed";
  116. const char * wd_failure_str = "failure";
  117. const char * wd_stopped_str = "stopped";
  118. const char * wd_config_changed_str = "config_changed";
  119. struct cs_fsm_entry wd_fsm_table[] = {
  120. { WD_S_STOPPED, WD_E_CONFIG_CHANGED, wd_config_changed, {WD_S_STOPPED, WD_S_RUNNING, -1} },
  121. { WD_S_STOPPED, WD_E_FAILURE, NULL, {-1} },
  122. { WD_S_RUNNING, WD_E_CONFIG_CHANGED, wd_config_changed, {WD_S_RUNNING, WD_S_STOPPED, -1} },
  123. { WD_S_RUNNING, WD_E_FAILURE, wd_resource_failed, {WD_S_FAILED, -1} },
  124. { WD_S_FAILED, WD_E_CONFIG_CHANGED, wd_config_changed, {WD_S_RUNNING, WD_S_STOPPED, -1} },
  125. { WD_S_FAILED, WD_E_FAILURE, NULL, {-1} },
  126. };
  127. struct corosync_service_engine *wd_get_service_engine_ver0 (void)
  128. {
  129. return (&wd_service_engine);
  130. }
  131. static const char * wd_res_state_to_str(struct cs_fsm* fsm,
  132. int32_t state)
  133. {
  134. switch (state) {
  135. case WD_S_STOPPED:
  136. return wd_stopped_str;
  137. break;
  138. case WD_S_RUNNING:
  139. return wd_running_str;
  140. break;
  141. case WD_S_FAILED:
  142. return wd_failed_str;
  143. break;
  144. }
  145. return NULL;
  146. }
  147. static const char * wd_res_event_to_str(struct cs_fsm* fsm,
  148. int32_t event)
  149. {
  150. switch (event) {
  151. case WD_E_CONFIG_CHANGED:
  152. return wd_config_changed_str;
  153. break;
  154. case WD_E_FAILURE:
  155. return wd_failure_str;
  156. break;
  157. }
  158. return NULL;
  159. }
  160. static void wd_fsm_cb (struct cs_fsm *fsm, int cb_event, int32_t curr_state,
  161. int32_t next_state, int32_t fsm_event, void *data)
  162. {
  163. switch (cb_event) {
  164. case CS_FSM_CB_EVENT_PROCESS_NF:
  165. log_printf (LOGSYS_LEVEL_ERROR, "Fsm:%s could not find event \"%s\" in state \"%s\"",
  166. fsm->name, fsm->event_to_str(fsm, fsm_event), fsm->state_to_str(fsm, curr_state));
  167. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  168. break;
  169. case CS_FSM_CB_EVENT_STATE_SET:
  170. log_printf (LOGSYS_LEVEL_INFO, "Fsm:%s event \"%s\", state \"%s\" --> \"%s\"",
  171. fsm->name,
  172. fsm->event_to_str(fsm, fsm_event),
  173. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  174. fsm->state_to_str(fsm, next_state));
  175. break;
  176. case CS_FSM_CB_EVENT_STATE_SET_NF:
  177. log_printf (LOGSYS_LEVEL_CRIT, "Fsm:%s Can't change state from \"%s\" to \"%s\" (event was \"%s\")",
  178. fsm->name,
  179. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  180. fsm->state_to_str(fsm, next_state),
  181. fsm->event_to_str(fsm, fsm_event));
  182. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  183. break;
  184. default:
  185. log_printf (LOGSYS_LEVEL_CRIT, "Fsm: Unknown callback event!");
  186. corosync_exit_error(COROSYNC_DONE_FATAL_ERR);
  187. break;
  188. }
  189. }
  190. /*
  191. * returns (CS_TRUE == OK, CS_FALSE == failed)
  192. */
  193. static int32_t wd_resource_state_is_ok (struct resource *ref)
  194. {
  195. char* state = NULL;
  196. uint64_t last_updated;
  197. uint64_t my_time;
  198. uint64_t allowed_period;
  199. char key_name[ICMAP_KEYNAME_MAXLEN];
  200. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "last_updated");
  201. if (icmap_get_uint64(key_name, &last_updated) != CS_OK) {
  202. /* key does not exist.
  203. */
  204. return CS_FALSE;
  205. }
  206. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "state");
  207. if (icmap_get_string(key_name, &state) != CS_OK || strcmp(state, "disabled") == 0) {
  208. /* key does not exist.
  209. */
  210. if (state != NULL)
  211. free(state);
  212. return CS_FALSE;
  213. }
  214. if (last_updated == 0) {
  215. /* initial value */
  216. free(state);
  217. return CS_TRUE;
  218. }
  219. my_time = cs_timestamp_get();
  220. /*
  221. * Here we check that the monitor has written a timestamp within the poll_period
  222. * plus a grace factor of (0.5 * poll_period).
  223. */
  224. allowed_period = (ref->check_timeout * MILLI_2_NANO_SECONDS * 3) / 2;
  225. if ((last_updated + allowed_period) < my_time) {
  226. log_printf (LOGSYS_LEVEL_ERROR,
  227. "last_updated %"PRIu64" ms too late, period:%"PRIu64".",
  228. (uint64_t)(my_time/MILLI_2_NANO_SECONDS - ((last_updated + allowed_period) / MILLI_2_NANO_SECONDS)),
  229. ref->check_timeout);
  230. free(state);
  231. return CS_FALSE;
  232. }
  233. if (strcmp (state, wd_failed_str) == 0) {
  234. free(state);
  235. return CS_FALSE;
  236. }
  237. free(state);
  238. return CS_TRUE;
  239. }
  240. static void wd_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
  241. {
  242. char *state;
  243. uint64_t tmp_value;
  244. uint64_t next_timeout;
  245. struct resource *ref = (struct resource*)data;
  246. char key_name[ICMAP_KEYNAME_MAXLEN];
  247. next_timeout = ref->check_timeout;
  248. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "poll_period");
  249. if (icmap_get_uint64(ref->res_path, &tmp_value) == CS_OK) {
  250. if (tmp_value >= WD_MIN_TIMEOUT_MS && tmp_value <= WD_MAX_TIMEOUT_MS) {
  251. log_printf (LOGSYS_LEVEL_DEBUG,
  252. "poll_period changing from:%"PRIu64" to %"PRIu64".",
  253. ref->check_timeout, tmp_value);
  254. /*
  255. * To easy in the transition between poll_period's we are going
  256. * to make the first timeout the bigger of the new and old value.
  257. * This is to give the monitoring system time to adjust.
  258. */
  259. next_timeout = CS_MAX(tmp_value, ref->check_timeout);
  260. ref->check_timeout = tmp_value;
  261. } else {
  262. log_printf (LOGSYS_LEVEL_WARNING,
  263. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  264. tmp_value, ref->name);
  265. }
  266. }
  267. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "recovery");
  268. if (icmap_get_string(key_name, &ref->recovery) != CS_OK) {
  269. /* key does not exist.
  270. */
  271. log_printf (LOGSYS_LEVEL_WARNING,
  272. "resource %s missing a recovery key.", ref->name);
  273. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref, wd_fsm_cb);
  274. return;
  275. }
  276. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "state");
  277. if (icmap_get_string(key_name, &state) != CS_OK) {
  278. /* key does not exist.
  279. */
  280. log_printf (LOGSYS_LEVEL_WARNING,
  281. "resource %s missing a state key.", ref->name);
  282. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref, wd_fsm_cb);
  283. return;
  284. }
  285. if (ref->check_timer) {
  286. api->timer_delete(ref->check_timer);
  287. ref->check_timer = 0;
  288. }
  289. if (strcmp(wd_stopped_str, state) == 0) {
  290. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref, wd_fsm_cb);
  291. } else {
  292. api->timer_add_duration(next_timeout * MILLI_2_NANO_SECONDS,
  293. ref, wd_resource_check_fn, &ref->check_timer);
  294. cs_fsm_state_set(&ref->fsm, WD_S_RUNNING, ref, wd_fsm_cb);
  295. }
  296. free(state);
  297. }
  298. static void wd_resource_failed (struct cs_fsm* fsm, int32_t event, void * data)
  299. {
  300. struct resource* ref = (struct resource*)data;
  301. if (ref->check_timer) {
  302. api->timer_delete(ref->check_timer);
  303. ref->check_timer = 0;
  304. }
  305. log_printf (LOGSYS_LEVEL_CRIT, "%s resource \"%s\" failed!",
  306. ref->recovery, (char*)ref->name);
  307. if (strcmp (ref->recovery, "watchdog") == 0 ||
  308. strcmp (ref->recovery, "quit") == 0) {
  309. watchdog_ok = 0;
  310. }
  311. else if (strcmp (ref->recovery, "reboot") == 0) {
  312. reboot(RB_AUTOBOOT);
  313. }
  314. else if (strcmp (ref->recovery, "shutdown") == 0) {
  315. reboot(RB_POWER_OFF);
  316. }
  317. cs_fsm_state_set(fsm, WD_S_FAILED, data, wd_fsm_cb);
  318. }
  319. static void wd_key_changed(
  320. int32_t event,
  321. const char *key_name,
  322. struct icmap_notify_value new_val,
  323. struct icmap_notify_value old_val,
  324. void *user_data)
  325. {
  326. struct resource* ref = (struct resource*)user_data;
  327. char *last_key_part;
  328. if (ref == NULL) {
  329. return ;
  330. }
  331. last_key_part = strrchr(key_name, '.');
  332. if (last_key_part == NULL) {
  333. return ;
  334. }
  335. last_key_part++;
  336. if (event == ICMAP_TRACK_ADD || event == ICMAP_TRACK_MODIFY) {
  337. if (strcmp(last_key_part, "last_updated") == 0 ||
  338. strcmp(last_key_part, "current") == 0) {
  339. return;
  340. }
  341. cs_fsm_process(&ref->fsm, WD_E_CONFIG_CHANGED, ref, wd_fsm_cb);
  342. }
  343. if (event == ICMAP_TRACK_DELETE && ref != NULL) {
  344. if (strcmp(last_key_part, "state") != 0) {
  345. return ;
  346. }
  347. log_printf (LOGSYS_LEVEL_WARNING,
  348. "resource \"%s\" deleted from cmap!",
  349. ref->name);
  350. api->timer_delete(ref->check_timer);
  351. ref->check_timer = 0;
  352. icmap_track_delete(ref->icmap_track);
  353. free(ref);
  354. }
  355. }
  356. static void wd_resource_check_fn (void* resource_ref)
  357. {
  358. struct resource* ref = (struct resource*)resource_ref;
  359. if (wd_resource_state_is_ok (ref) == CS_FALSE) {
  360. cs_fsm_process(&ref->fsm, WD_E_FAILURE, ref, wd_fsm_cb);
  361. return;
  362. }
  363. api->timer_add_duration(ref->check_timeout*MILLI_2_NANO_SECONDS,
  364. ref, wd_resource_check_fn, &ref->check_timer);
  365. }
  366. /*
  367. * return 0 - fully configured
  368. * return -1 - partially configured
  369. */
  370. static int32_t wd_resource_create (char *res_path, char *res_name)
  371. {
  372. char *state;
  373. uint64_t tmp_value;
  374. struct resource *ref = calloc (1, sizeof (struct resource));
  375. char key_name[ICMAP_KEYNAME_MAXLEN];
  376. strcpy(ref->res_path, res_path);
  377. ref->check_timeout = WD_DEFAULT_TIMEOUT_MS;
  378. ref->check_timer = 0;
  379. strcpy(ref->name, res_name);
  380. ref->fsm.name = ref->name;
  381. ref->fsm.table = wd_fsm_table;
  382. ref->fsm.entries = sizeof(wd_fsm_table) / sizeof(struct cs_fsm_entry);
  383. ref->fsm.curr_entry = 0;
  384. ref->fsm.curr_state = WD_S_STOPPED;
  385. ref->fsm.state_to_str = wd_res_state_to_str;
  386. ref->fsm.event_to_str = wd_res_event_to_str;
  387. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "poll_period");
  388. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  389. icmap_set_uint64(key_name, ref->check_timeout);
  390. } else {
  391. if (tmp_value >= WD_MIN_TIMEOUT_MS && tmp_value <= WD_MAX_TIMEOUT_MS) {
  392. ref->check_timeout = tmp_value;
  393. } else {
  394. log_printf (LOGSYS_LEVEL_WARNING,
  395. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  396. tmp_value, ref->name);
  397. }
  398. }
  399. icmap_track_add(res_path,
  400. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
  401. wd_key_changed,
  402. ref, &ref->icmap_track);
  403. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "recovery");
  404. if (icmap_get_string(key_name, &ref->recovery) != CS_OK) {
  405. /* key does not exist.
  406. */
  407. log_printf (LOGSYS_LEVEL_WARNING,
  408. "resource %s missing a recovery key.", ref->name);
  409. return -1;
  410. }
  411. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "state");
  412. if (icmap_get_string(key_name, &state) != CS_OK) {
  413. /* key does not exist.
  414. */
  415. log_printf (LOGSYS_LEVEL_WARNING,
  416. "resource %s missing a state key.", ref->name);
  417. return -1;
  418. }
  419. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "last_updated");
  420. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  421. /* key does not exist.
  422. */
  423. ref->last_updated = 0;
  424. } else {
  425. ref->last_updated = tmp_value;
  426. }
  427. /*
  428. * delay the first check to give the monitor time to start working.
  429. */
  430. tmp_value = CS_MAX(ref->check_timeout * 2, WD_DEFAULT_TIMEOUT_MS);
  431. api->timer_add_duration(tmp_value * MILLI_2_NANO_SECONDS,
  432. ref,
  433. wd_resource_check_fn, &ref->check_timer);
  434. cs_fsm_state_set(&ref->fsm, WD_S_RUNNING, ref, wd_fsm_cb);
  435. return 0;
  436. }
  437. static void wd_tickle_fn (void* arg)
  438. {
  439. ENTER();
  440. if (watchdog_ok) {
  441. if (dog > 0) {
  442. ioctl(dog, WDIOC_KEEPALIVE, &watchdog_ok);
  443. }
  444. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  445. wd_tickle_fn, &wd_timer);
  446. }
  447. else {
  448. log_printf (LOGSYS_LEVEL_ALERT, "NOT tickling the watchdog!");
  449. }
  450. }
  451. static void wd_resource_created_cb(
  452. int32_t event,
  453. const char *key_name,
  454. struct icmap_notify_value new_val,
  455. struct icmap_notify_value old_val,
  456. void *user_data)
  457. {
  458. char res_name[ICMAP_KEYNAME_MAXLEN];
  459. char res_type[ICMAP_KEYNAME_MAXLEN];
  460. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  461. int res;
  462. if (event != ICMAP_TRACK_ADD) {
  463. return ;
  464. }
  465. res = sscanf(key_name, "resources.%[^.].%[^.].%[^.]", res_type, res_name, tmp_key);
  466. if (res != 3) {
  467. return ;
  468. }
  469. if (strcmp(tmp_key, "state") != 0) {
  470. return ;
  471. }
  472. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "resources.%s.%s.", res_type, res_name);
  473. wd_resource_create (tmp_key, res_name);
  474. }
  475. static void wd_scan_resources (void)
  476. {
  477. int res_count = 0;
  478. icmap_track_t icmap_track = NULL;
  479. icmap_iter_t iter;
  480. const char *key_name;
  481. int res;
  482. char res_name[ICMAP_KEYNAME_MAXLEN];
  483. char res_type[ICMAP_KEYNAME_MAXLEN];
  484. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  485. ENTER();
  486. iter = icmap_iter_init("resources.");
  487. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  488. res = sscanf(key_name, "resources.%[^.].%[^.].%[^.]", res_type, res_name, tmp_key);
  489. if (res != 3) {
  490. continue ;
  491. }
  492. if (strcmp(tmp_key, "state") != 0) {
  493. continue ;
  494. }
  495. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "resources.%s.%s.", res_type, res_name);
  496. if (wd_resource_create (tmp_key, res_name) == 0) {
  497. res_count++;
  498. }
  499. }
  500. icmap_iter_finalize(iter);
  501. icmap_track_add("resources.process.", ICMAP_TRACK_ADD | ICMAP_TRACK_PREFIX,
  502. wd_resource_created_cb, NULL, &icmap_track);
  503. icmap_track_add("resources.system.", ICMAP_TRACK_ADD | ICMAP_TRACK_PREFIX,
  504. wd_resource_created_cb, NULL, &icmap_track);
  505. if (res_count == 0) {
  506. log_printf (LOGSYS_LEVEL_INFO, "no resources configured.");
  507. }
  508. }
  509. static void watchdog_timeout_apply (uint32_t new)
  510. {
  511. struct watchdog_info ident;
  512. uint32_t original_timeout = 0;
  513. if (dog > 0) {
  514. ioctl(dog, WDIOC_GETTIMEOUT, &original_timeout);
  515. }
  516. if (new == original_timeout) {
  517. return;
  518. }
  519. watchdog_timeout = new;
  520. if (dog > 0) {
  521. ioctl(dog, WDIOC_GETSUPPORT, &ident);
  522. if (ident.options & WDIOF_SETTIMEOUT) {
  523. /* yay! the dog is trained.
  524. */
  525. ioctl(dog, WDIOC_SETTIMEOUT, &watchdog_timeout);
  526. }
  527. ioctl(dog, WDIOC_GETTIMEOUT, &watchdog_timeout);
  528. }
  529. if (watchdog_timeout == new) {
  530. tickle_timeout = (watchdog_timeout * CS_TIME_MS_IN_SEC)/ 2;
  531. /* reset the tickle timer in case it was reduced.
  532. */
  533. api->timer_delete (wd_timer);
  534. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  535. wd_tickle_fn, &wd_timer);
  536. log_printf (LOGSYS_LEVEL_DEBUG, "The Watchdog timeout is %d seconds", watchdog_timeout);
  537. log_printf (LOGSYS_LEVEL_DEBUG, "The tickle timeout is %"PRIu64" ms", tickle_timeout);
  538. } else {
  539. log_printf (LOGSYS_LEVEL_WARNING,
  540. "Could not change the Watchdog timeout from %d to %d seconds",
  541. original_timeout, new);
  542. }
  543. }
  544. static int setup_watchdog(void)
  545. {
  546. struct watchdog_info ident;
  547. ENTER();
  548. if (access ("/dev/watchdog", W_OK) != 0) {
  549. log_printf (LOGSYS_LEVEL_WARNING, "No Watchdog, try modprobe <a watchdog>");
  550. dog = -1;
  551. return -1;
  552. }
  553. /* here goes, lets hope they have "Magic Close"
  554. */
  555. dog = open("/dev/watchdog", O_WRONLY);
  556. if (dog == -1) {
  557. log_printf (LOGSYS_LEVEL_WARNING, "Watchdog exists but couldn't be opened.");
  558. dog = -1;
  559. return -1;
  560. }
  561. /* Right we have the dog.
  562. * Lets see what breed it is.
  563. */
  564. ioctl(dog, WDIOC_GETSUPPORT, &ident);
  565. log_printf (LOGSYS_LEVEL_INFO, "Watchdog is now been tickled by corosync.");
  566. log_printf (LOGSYS_LEVEL_DEBUG, "%s", ident.identity);
  567. watchdog_timeout_apply (watchdog_timeout);
  568. ioctl(dog, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
  569. return 0;
  570. }
  571. static void wd_top_level_key_changed(
  572. int32_t event,
  573. const char *key_name,
  574. struct icmap_notify_value new_val,
  575. struct icmap_notify_value old_val,
  576. void *user_data)
  577. {
  578. uint32_t tmp_value_32;
  579. ENTER();
  580. if (icmap_get_uint32("resources.watchdog_timeout", &tmp_value_32) == CS_OK) {
  581. if (tmp_value_32 >= 2 && tmp_value_32 <= 120) {
  582. watchdog_timeout_apply (tmp_value_32);
  583. }
  584. }
  585. else {
  586. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  587. }
  588. }
  589. static void watchdog_timeout_get_initial (void)
  590. {
  591. uint32_t tmp_value_32;
  592. icmap_track_t icmap_track = NULL;
  593. ENTER();
  594. if (icmap_get_uint32("resources.watchdog_timeout", &tmp_value_32) != CS_OK) {
  595. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  596. icmap_set_uint32("resources.watchdog_timeout", watchdog_timeout);
  597. }
  598. else {
  599. if (tmp_value_32 >= 2 && tmp_value_32 <= 120) {
  600. watchdog_timeout_apply (tmp_value_32);
  601. } else {
  602. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  603. }
  604. }
  605. icmap_track_add("resources.watchdog_timeout", ICMAP_TRACK_MODIFY,
  606. wd_top_level_key_changed, NULL, &icmap_track);
  607. }
  608. static char *wd_exec_init_fn (struct corosync_api_v1 *corosync_api)
  609. {
  610. ENTER();
  611. api = corosync_api;
  612. watchdog_timeout_get_initial();
  613. setup_watchdog();
  614. wd_scan_resources();
  615. return NULL;
  616. }
  617. static int wd_exec_exit_fn (void)
  618. {
  619. char magic = 'V';
  620. ENTER();
  621. if (dog > 0) {
  622. log_printf (LOGSYS_LEVEL_INFO, "magically closing the watchdog.");
  623. write (dog, &magic, 1);
  624. }
  625. return 0;
  626. }