wd.c 18 KB

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