123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "libbb.h"
- #include <linux/types.h> /* for __u32 */
- #include <linux/watchdog.h>
- #ifndef WDIOC_SETOPTIONS
- # define WDIOC_SETOPTIONS 0x5704
- #endif
- #ifndef WDIOC_SETTIMEOUT
- # define WDIOC_SETTIMEOUT 0x5706
- #endif
- #ifndef WDIOC_GETTIMEOUT
- # define WDIOC_GETTIMEOUT 0x5707
- #endif
- #ifndef WDIOS_ENABLECARD
- # define WDIOS_ENABLECARD 2
- #endif
- #define OPT_FOREGROUND (1 << 0)
- #define OPT_STIMER (1 << 1)
- #define OPT_HTIMER (1 << 2)
- static void shutdown_watchdog(void)
- {
- static const char V = 'V';
- write(3, &V, 1);
- close(3);
- }
- static void shutdown_on_signal(int sig UNUSED_PARAM)
- {
- remove_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid");
- shutdown_watchdog();
- _exit(EXIT_SUCCESS);
- }
- static void watchdog_open(const char* device)
- {
-
- xmove_fd(xopen(device, O_WRONLY), 3);
-
- shutdown_watchdog();
- xmove_fd(xopen(device, O_WRONLY), 3);
- }
- int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int watchdog_main(int argc UNUSED_PARAM, char **argv)
- {
- static const int enable = WDIOS_ENABLECARD;
- static const struct suffix_mult suffixes[] = {
- { "ms", 1 },
- { "", 1000 },
- { "", 0 }
- };
- unsigned opts;
- unsigned stimer_duration;
- unsigned htimer_duration = 60000;
- char *st_arg;
- char *ht_arg;
- opts = getopt32(argv, "^" "Ft:T:" "\0" "=1",
- &st_arg, &ht_arg
- );
-
- if (!(opts & OPT_FOREGROUND))
- bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
-
- if (opts & OPT_HTIMER)
- htimer_duration = xatou_sfx(ht_arg, suffixes);
- stimer_duration = htimer_duration / 2;
- if (opts & OPT_STIMER)
- stimer_duration = xatou_sfx(st_arg, suffixes);
- bb_signals(BB_FATAL_SIGS, shutdown_on_signal);
- watchdog_open(argv[optind]);
-
- htimer_duration = htimer_duration / 1000;
- ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
- ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
- #if 0
- ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
- printf("watchdog: SW timer is %dms, HW timer is %ds\n",
- stimer_duration, htimer_duration * 1000);
- #endif
- write_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid");
- while (1) {
-
- write(3, "", 1);
- usleep(stimer_duration * 1000L);
- }
- return EXIT_SUCCESS;
- }
|