wd.c 18 KB

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