1
2
3
4
5
6
7
8
9
10
11
12#include "libbb.h"
13#include "linux/types.h"
14#include "linux/watchdog.h"
15
16#define OPT_FOREGROUND (1 << 0)
17#define OPT_STIMER (1 << 1)
18#define OPT_HTIMER (1 << 2)
19
20static void watchdog_shutdown(int sig UNUSED_PARAM)
21{
22 static const char V = 'V';
23
24 write(3, &V, 1);
25 if (ENABLE_FEATURE_CLEAN_UP)
26 close(3);
27 exit(EXIT_SUCCESS);
28}
29
30int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int watchdog_main(int argc, char **argv)
32{
33 static const struct suffix_mult suffixes[] = {
34 { "ms", 1 },
35 { "", 1000 },
36 { }
37 };
38
39 unsigned opts;
40 unsigned stimer_duration;
41 unsigned htimer_duration = 60000;
42 char *st_arg;
43 char *ht_arg;
44
45 opt_complementary = "=1";
46 opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
47
48 if (opts & OPT_HTIMER)
49 htimer_duration = xatou_sfx(ht_arg, suffixes);
50 stimer_duration = htimer_duration / 2;
51 if (opts & OPT_STIMER)
52 stimer_duration = xatou_sfx(st_arg, suffixes);
53
54 bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
55
56
57 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
58
59
60 htimer_duration = htimer_duration / 1000;
61#ifndef WDIOC_SETTIMEOUT
62#error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
63#else
64 ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
65#endif
66#if 0
67 ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
68 printf("watchdog: SW timer is %dms, HW timer is %dms\n",
69 stimer_duration, htimer_duration * 1000);
70#endif
71
72 if (!(opts & OPT_FOREGROUND)) {
73 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
74 }
75
76 while (1) {
77
78
79
80
81 write(3, "", 1);
82 usleep(stimer_duration * 1000L);
83 }
84 return EXIT_SUCCESS;
85}
86