wd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 "../exec/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. /*
  161. * returns (CS_TRUE == OK, CS_FALSE == failed)
  162. */
  163. static int32_t wd_resource_state_is_ok (struct resource *ref)
  164. {
  165. char* state;
  166. uint64_t last_updated;
  167. uint64_t my_time;
  168. uint64_t allowed_period;
  169. char key_name[ICMAP_KEYNAME_MAXLEN];
  170. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "last_updated");
  171. if (icmap_get_uint64(key_name, &last_updated) != CS_OK) {
  172. /* key does not exist.
  173. */
  174. return CS_FALSE;
  175. }
  176. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "state");
  177. if (icmap_get_string(key_name, &state) != CS_OK || strcmp(state, "disabled") == 0) {
  178. /* key does not exist.
  179. */
  180. return CS_FALSE;
  181. }
  182. free (state);
  183. if (last_updated == 0) {
  184. /* initial value */
  185. free(state);
  186. return CS_TRUE;
  187. }
  188. my_time = cs_timestamp_get();
  189. /*
  190. * Here we check that the monitor has written a timestamp within the poll_period
  191. * plus a grace factor of (0.5 * poll_period).
  192. */
  193. allowed_period = (ref->check_timeout * MILLI_2_NANO_SECONDS * 3) / 2;
  194. if ((last_updated + allowed_period) < my_time) {
  195. log_printf (LOGSYS_LEVEL_ERROR,
  196. "last_updated %"PRIu64" ms too late, period:%"PRIu64".",
  197. (uint64_t)(my_time/MILLI_2_NANO_SECONDS - ((last_updated + allowed_period) / MILLI_2_NANO_SECONDS)),
  198. ref->check_timeout);
  199. free(state);
  200. return CS_FALSE;
  201. }
  202. if (strcmp (state, wd_failed_str) == 0) {
  203. free(state);
  204. return CS_FALSE;
  205. }
  206. free(state);
  207. return CS_TRUE;
  208. }
  209. static void wd_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
  210. {
  211. char *state;
  212. uint64_t tmp_value;
  213. uint64_t next_timeout;
  214. struct resource *ref = (struct resource*)data;
  215. char key_name[ICMAP_KEYNAME_MAXLEN];
  216. next_timeout = ref->check_timeout;
  217. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "poll_period");
  218. if (icmap_get_uint64(ref->res_path, &tmp_value) == CS_OK) {
  219. if (tmp_value >= WD_MIN_TIMEOUT_MS && tmp_value <= WD_MAX_TIMEOUT_MS) {
  220. log_printf (LOGSYS_LEVEL_DEBUG,
  221. "poll_period changing from:%"PRIu64" to %"PRIu64".",
  222. ref->check_timeout, tmp_value);
  223. /*
  224. * To easy in the transition between poll_period's we are going
  225. * to make the first timeout the bigger of the new and old value.
  226. * This is to give the monitoring system time to adjust.
  227. */
  228. next_timeout = CS_MAX(tmp_value, ref->check_timeout);
  229. ref->check_timeout = tmp_value;
  230. } else {
  231. log_printf (LOGSYS_LEVEL_WARNING,
  232. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  233. tmp_value, ref->name);
  234. }
  235. }
  236. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "recovery");
  237. if (icmap_get_string(key_name, &ref->recovery) != CS_OK) {
  238. /* key does not exist.
  239. */
  240. log_printf (LOGSYS_LEVEL_WARNING,
  241. "resource %s missing a recovery key.", ref->name);
  242. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref);
  243. return;
  244. }
  245. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", ref->res_path, "state");
  246. if (icmap_get_string(key_name, &state) != CS_OK) {
  247. /* key does not exist.
  248. */
  249. log_printf (LOGSYS_LEVEL_WARNING,
  250. "resource %s missing a state key.", ref->name);
  251. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref);
  252. return;
  253. }
  254. if (ref->check_timer) {
  255. api->timer_delete(ref->check_timer);
  256. ref->check_timer = 0;
  257. }
  258. if (strcmp(wd_stopped_str, state) == 0) {
  259. cs_fsm_state_set(&ref->fsm, WD_S_STOPPED, ref);
  260. } else {
  261. api->timer_add_duration(next_timeout * MILLI_2_NANO_SECONDS,
  262. ref, wd_resource_check_fn, &ref->check_timer);
  263. cs_fsm_state_set(&ref->fsm, WD_S_RUNNING, ref);
  264. }
  265. free(state);
  266. }
  267. static void wd_resource_failed (struct cs_fsm* fsm, int32_t event, void * data)
  268. {
  269. struct resource* ref = (struct resource*)data;
  270. if (ref->check_timer) {
  271. api->timer_delete(ref->check_timer);
  272. ref->check_timer = 0;
  273. }
  274. log_printf (LOGSYS_LEVEL_CRIT, "%s resource \"%s\" failed!",
  275. ref->recovery, (char*)ref->name);
  276. if (strcmp (ref->recovery, "watchdog") == 0 ||
  277. strcmp (ref->recovery, "quit") == 0) {
  278. watchdog_ok = 0;
  279. }
  280. else if (strcmp (ref->recovery, "reboot") == 0) {
  281. reboot(RB_AUTOBOOT);
  282. }
  283. else if (strcmp (ref->recovery, "shutdown") == 0) {
  284. reboot(RB_POWER_OFF);
  285. }
  286. cs_fsm_state_set(fsm, WD_S_FAILED, data);
  287. }
  288. static void wd_key_changed(
  289. int32_t event,
  290. const char *key_name,
  291. struct icmap_notify_value new_val,
  292. struct icmap_notify_value old_val,
  293. void *user_data)
  294. {
  295. struct resource* ref = (struct resource*)user_data;
  296. char *last_key_part;
  297. if (ref == NULL) {
  298. return ;
  299. }
  300. last_key_part = strrchr(key_name, '.');
  301. if (last_key_part == NULL) {
  302. return ;
  303. }
  304. last_key_part++;
  305. if (event == ICMAP_TRACK_ADD || event == ICMAP_TRACK_MODIFY) {
  306. if (strcmp(last_key_part, "last_updated") == 0 ||
  307. strcmp(last_key_part, "current") == 0) {
  308. return;
  309. }
  310. cs_fsm_process(&ref->fsm, WD_E_CONFIG_CHANGED, ref);
  311. }
  312. if (event == ICMAP_TRACK_DELETE && ref != NULL) {
  313. if (strcmp(last_key_part, "state") != 0) {
  314. return ;
  315. }
  316. log_printf (LOGSYS_LEVEL_WARNING,
  317. "resource \"%s\" deleted from cmap!",
  318. ref->name);
  319. api->timer_delete(ref->check_timer);
  320. ref->check_timer = 0;
  321. icmap_track_delete(ref->icmap_track);
  322. free(ref);
  323. }
  324. }
  325. static void wd_resource_check_fn (void* resource_ref)
  326. {
  327. struct resource* ref = (struct resource*)resource_ref;
  328. if (wd_resource_state_is_ok (ref) == CS_FALSE) {
  329. cs_fsm_process(&ref->fsm, WD_E_FAILURE, ref);
  330. return;
  331. }
  332. api->timer_add_duration(ref->check_timeout*MILLI_2_NANO_SECONDS,
  333. ref, wd_resource_check_fn, &ref->check_timer);
  334. }
  335. /*
  336. * return 0 - fully configured
  337. * return -1 - partially configured
  338. */
  339. static int32_t wd_resource_create (char *res_path, char *res_name)
  340. {
  341. char *state;
  342. uint64_t tmp_value;
  343. struct resource *ref = calloc (1, sizeof (struct resource));
  344. char key_name[ICMAP_KEYNAME_MAXLEN];
  345. strcpy(ref->res_path, res_path);
  346. ref->check_timeout = WD_DEFAULT_TIMEOUT_MS;
  347. ref->check_timer = 0;
  348. strcpy(ref->name, res_name);
  349. ref->fsm.name = ref->name;
  350. ref->fsm.table = wd_fsm_table;
  351. ref->fsm.entries = sizeof(wd_fsm_table) / sizeof(struct cs_fsm_entry);
  352. ref->fsm.curr_entry = 0;
  353. ref->fsm.curr_state = WD_S_STOPPED;
  354. ref->fsm.state_to_str = wd_res_state_to_str;
  355. ref->fsm.event_to_str = wd_res_event_to_str;
  356. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "poll_period");
  357. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  358. icmap_set_uint64(key_name, ref->check_timeout);
  359. } else {
  360. if (tmp_value >= WD_MIN_TIMEOUT_MS && tmp_value <= WD_MAX_TIMEOUT_MS) {
  361. ref->check_timeout = tmp_value;
  362. } else {
  363. log_printf (LOGSYS_LEVEL_WARNING,
  364. "Could NOT use poll_period:%"PRIu64" ms for resource %s",
  365. tmp_value, ref->name);
  366. }
  367. }
  368. icmap_track_add(res_path,
  369. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
  370. wd_key_changed,
  371. ref, &ref->icmap_track);
  372. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "recovery");
  373. if (icmap_get_string(key_name, &ref->recovery) != CS_OK) {
  374. /* key does not exist.
  375. */
  376. log_printf (LOGSYS_LEVEL_WARNING,
  377. "resource %s missing a recovery key.", ref->name);
  378. return -1;
  379. }
  380. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "state");
  381. if (icmap_get_string(key_name, &state) != CS_OK) {
  382. /* key does not exist.
  383. */
  384. log_printf (LOGSYS_LEVEL_WARNING,
  385. "resource %s missing a state key.", ref->name);
  386. return -1;
  387. }
  388. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", res_path, "last_updated");
  389. if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
  390. /* key does not exist.
  391. */
  392. ref->last_updated = 0;
  393. } else {
  394. ref->last_updated = tmp_value;
  395. }
  396. /*
  397. * delay the first check to give the monitor time to start working.
  398. */
  399. tmp_value = CS_MAX(ref->check_timeout * 2, WD_DEFAULT_TIMEOUT_MS);
  400. api->timer_add_duration(tmp_value * MILLI_2_NANO_SECONDS,
  401. ref,
  402. wd_resource_check_fn, &ref->check_timer);
  403. cs_fsm_state_set(&ref->fsm, WD_S_RUNNING, ref);
  404. return 0;
  405. }
  406. static void wd_tickle_fn (void* arg)
  407. {
  408. ENTER();
  409. if (watchdog_ok) {
  410. if (dog > 0) {
  411. ioctl(dog, WDIOC_KEEPALIVE, &watchdog_ok);
  412. }
  413. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  414. wd_tickle_fn, &wd_timer);
  415. }
  416. else {
  417. log_printf (LOGSYS_LEVEL_ALERT, "NOT tickling the watchdog!");
  418. }
  419. }
  420. static void wd_resource_created_cb(
  421. int32_t event,
  422. const char *key_name,
  423. struct icmap_notify_value new_val,
  424. struct icmap_notify_value old_val,
  425. void *user_data)
  426. {
  427. char res_name[ICMAP_KEYNAME_MAXLEN];
  428. char res_type[ICMAP_KEYNAME_MAXLEN];
  429. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  430. int res;
  431. if (event != ICMAP_TRACK_ADD) {
  432. return ;
  433. }
  434. res = sscanf(key_name, "resources.%[^.].%[^.].%[^.]", res_type, res_name, tmp_key);
  435. if (res != 3) {
  436. return ;
  437. }
  438. if (strcmp(tmp_key, "state") != 0) {
  439. return ;
  440. }
  441. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "resources.%s.%s.", res_type, res_name);
  442. wd_resource_create (tmp_key, res_name);
  443. }
  444. static void wd_scan_resources (void)
  445. {
  446. int res_count = 0;
  447. icmap_track_t icmap_track = NULL;
  448. icmap_iter_t iter;
  449. const char *key_name;
  450. int res;
  451. char res_name[ICMAP_KEYNAME_MAXLEN];
  452. char res_type[ICMAP_KEYNAME_MAXLEN];
  453. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  454. ENTER();
  455. iter = icmap_iter_init("resources.");
  456. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  457. res = sscanf(key_name, "resources.%[^.].%[^.].%[^.]", res_type, res_name, tmp_key);
  458. if (res != 3) {
  459. continue ;
  460. }
  461. if (strcmp(tmp_key, "state") != 0) {
  462. continue ;
  463. }
  464. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "resources.%s.%s.", res_type, res_name);
  465. if (wd_resource_create (tmp_key, res_name) == 0) {
  466. res_count++;
  467. }
  468. }
  469. icmap_iter_finalize(iter);
  470. icmap_track_add("resources.process.", ICMAP_TRACK_ADD | ICMAP_TRACK_PREFIX,
  471. wd_resource_created_cb, NULL, &icmap_track);
  472. icmap_track_add("resources.system.", ICMAP_TRACK_ADD | ICMAP_TRACK_PREFIX,
  473. wd_resource_created_cb, NULL, &icmap_track);
  474. if (res_count == 0) {
  475. log_printf (LOGSYS_LEVEL_INFO, "no resources configured.");
  476. }
  477. }
  478. static void watchdog_timeout_apply (uint32_t new)
  479. {
  480. struct watchdog_info ident;
  481. uint32_t original_timeout = watchdog_timeout;
  482. if (new == original_timeout) {
  483. return;
  484. }
  485. watchdog_timeout = new;
  486. if (dog > 0) {
  487. ioctl(dog, WDIOC_GETSUPPORT, &ident);
  488. if (ident.options & WDIOF_SETTIMEOUT) {
  489. /* yay! the dog is trained.
  490. */
  491. ioctl(dog, WDIOC_SETTIMEOUT, &watchdog_timeout);
  492. }
  493. ioctl(dog, WDIOC_GETTIMEOUT, &watchdog_timeout);
  494. }
  495. if (watchdog_timeout == new) {
  496. tickle_timeout = (watchdog_timeout * CS_TIME_MS_IN_SEC)/ 2;
  497. /* reset the tickle timer in case it was reduced.
  498. */
  499. api->timer_delete (wd_timer);
  500. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  501. wd_tickle_fn, &wd_timer);
  502. log_printf (LOGSYS_LEVEL_DEBUG, "The Watchdog timeout is %d seconds", watchdog_timeout);
  503. log_printf (LOGSYS_LEVEL_DEBUG, "The tickle timeout is %"PRIu64" ms", tickle_timeout);
  504. } else {
  505. log_printf (LOGSYS_LEVEL_WARNING,
  506. "Could not change the Watchdog timeout from %d to %d seconds",
  507. original_timeout, new);
  508. }
  509. }
  510. static int setup_watchdog(void)
  511. {
  512. struct watchdog_info ident;
  513. ENTER();
  514. if (access ("/dev/watchdog", W_OK) != 0) {
  515. log_printf (LOGSYS_LEVEL_WARNING, "No Watchdog, try modprobe <a watchdog>");
  516. dog = -1;
  517. return -1;
  518. }
  519. /* here goes, lets hope they have "Magic Close"
  520. */
  521. dog = open("/dev/watchdog", O_WRONLY);
  522. if (dog == -1) {
  523. log_printf (LOGSYS_LEVEL_WARNING, "Watchdog exists but couldn't be opened.");
  524. dog = -1;
  525. return -1;
  526. }
  527. /* Right we have the dog.
  528. * Lets see what breed it is.
  529. */
  530. ioctl(dog, WDIOC_GETSUPPORT, &ident);
  531. log_printf (LOGSYS_LEVEL_INFO, "Watchdog is now been tickled by corosync.");
  532. log_printf (LOGSYS_LEVEL_DEBUG, "%s", ident.identity);
  533. watchdog_timeout_apply (watchdog_timeout);
  534. ioctl(dog, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
  535. return 0;
  536. }
  537. static void wd_top_level_key_changed(
  538. int32_t event,
  539. const char *key_name,
  540. struct icmap_notify_value new_val,
  541. struct icmap_notify_value old_val,
  542. void *user_data)
  543. {
  544. uint32_t tmp_value_32;
  545. ENTER();
  546. if (icmap_get_uint32("resources.watchdog_timeout", &tmp_value_32) != CS_OK) {
  547. if (tmp_value_32 >= 2 && tmp_value_32 <= 120) {
  548. watchdog_timeout_apply (tmp_value_32);
  549. }
  550. }
  551. else {
  552. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  553. }
  554. }
  555. static void watchdog_timeout_get_initial (void)
  556. {
  557. uint32_t tmp_value_32;
  558. icmap_track_t icmap_track = NULL;
  559. ENTER();
  560. if (icmap_get_uint32("resources.watchdog_timeout", &tmp_value_32) != CS_OK) {
  561. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  562. icmap_set_uint32("resources.watchdog_timeout", watchdog_timeout);
  563. }
  564. else {
  565. if (tmp_value_32 >= 2 && tmp_value_32 <= 120) {
  566. watchdog_timeout_apply (tmp_value_32);
  567. } else {
  568. watchdog_timeout_apply (WD_DEFAULT_TIMEOUT_SEC);
  569. }
  570. }
  571. icmap_track_add("resources.watchdog_timeout", ICMAP_TRACK_MODIFY,
  572. wd_top_level_key_changed, NULL, &icmap_track);
  573. }
  574. static char *wd_exec_init_fn (struct corosync_api_v1 *corosync_api)
  575. {
  576. ENTER();
  577. #ifdef COROSYNC_SOLARIS
  578. logsys_subsys_init();
  579. #endif
  580. api = corosync_api;
  581. watchdog_timeout_get_initial();
  582. setup_watchdog();
  583. wd_scan_resources();
  584. api->timer_add_duration(tickle_timeout*MILLI_2_NANO_SECONDS, NULL,
  585. wd_tickle_fn, &wd_timer);
  586. return NULL;
  587. }
  588. static int wd_exec_exit_fn (void)
  589. {
  590. char magic = 'V';
  591. ENTER();
  592. if (dog > 0) {
  593. log_printf (LOGSYS_LEVEL_INFO, "magically closing the watchdog.");
  594. write (dog, &magic, 1);
  595. }
  596. return 0;
  597. }