]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.sbin/watchdogd/watchdogd.c
MFC r239769:
[FreeBSD/stable/8.git] / usr.sbin / watchdogd / watchdogd.c
1 /*
2  * Copyright (c) 2003-2004  Sean M. Kelly <smkelly@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Software watchdog daemon.
29  */
30
31 #include <sys/types.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/mman.h>
35 #include <sys/param.h>
36 #include <sys/rtprio.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/watchdog.h>
40
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <libutil.h>
45 #include <math.h>
46 #include <paths.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sysexits.h>
52 #include <unistd.h>
53
54 static void     parseargs(int, char *[]);
55 static void     sighandler(int);
56 static void     watchdog_loop(void);
57 static int      watchdog_init(void);
58 static int      watchdog_onoff(int onoff);
59 static int      watchdog_patpat(u_int timeout);
60 static void     usage(void);
61
62 int debugging = 0;
63 int end_program = 0;
64 const char *pidfile = _PATH_VARRUN "watchdogd.pid";
65 int reset_mib[3];
66 size_t reset_miblen = 3;
67 u_int timeout = WD_TO_16SEC;
68 u_int passive = 0;
69 int is_daemon = 0;
70 int fd = -1;
71 int nap = 1;
72 char *test_cmd = NULL;
73
74 /*
75  * Periodically pat the watchdog, preventing it from firing.
76  */
77 int
78 main(int argc, char *argv[])
79 {
80         struct rtprio rtp;
81         struct pidfh *pfh;
82         pid_t otherpid;
83
84         if (getuid() != 0)
85                 errx(EX_SOFTWARE, "not super user");
86                 
87         parseargs(argc, argv);
88
89         rtp.type = RTP_PRIO_REALTIME;
90         rtp.prio = 0;
91         if (rtprio(RTP_SET, 0, &rtp) == -1)
92                 err(EX_OSERR, "rtprio");
93
94         if (watchdog_init() == -1)
95                 errx(EX_SOFTWARE, "unable to initialize watchdog");
96
97         if (is_daemon) {
98                 if (watchdog_onoff(1) == -1)
99                         exit(EX_SOFTWARE);
100
101                 pfh = pidfile_open(pidfile, 0600, &otherpid);
102                 if (pfh == NULL) {
103                         if (errno == EEXIST) {
104                                 errx(EX_SOFTWARE, "%s already running, pid: %d",
105                                     getprogname(), otherpid);
106                         }
107                         warn("Cannot open or create pidfile");
108                 }
109
110                 if (debugging == 0 && daemon(0, 0) == -1) {
111                         watchdog_onoff(0);
112                         pidfile_remove(pfh);
113                         err(EX_OSERR, "daemon");
114                 }
115
116                 signal(SIGHUP, SIG_IGN);
117                 signal(SIGINT, sighandler);
118                 signal(SIGTERM, sighandler);
119
120                 pidfile_write(pfh);
121                 if (madvise(0, 0, MADV_PROTECT) != 0)
122                         warn("madvise failed");
123                 if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
124                         warn("mlockall failed");
125
126                 watchdog_loop();
127
128                 /* exiting */
129                 pidfile_remove(pfh);
130                 return (EX_OK);
131         } else {
132                 if (passive)
133                         timeout |= WD_PASSIVE;
134                 else
135                         timeout |= WD_ACTIVE;
136                 if (watchdog_patpat(timeout) < 0)
137                         err(EX_OSERR, "patting the dog");
138                 return (EX_OK);
139         }
140 }
141
142 /*
143  * Catch signals and begin shutdown process.
144  */
145 static void
146 sighandler(int signum)
147 {
148
149         if (signum == SIGINT || signum == SIGTERM)
150                 end_program = 1;
151 }
152
153 /*
154  * Open the watchdog device.
155  */
156 static int
157 watchdog_init()
158 {
159
160         fd = open("/dev/" _PATH_WATCHDOG, O_RDWR);
161         if (fd >= 0)
162                 return (0);
163         warn("Could not open watchdog device");
164         return (-1);
165 }
166
167 /*
168  * Main program loop which is iterated every second.
169  */
170 static void
171 watchdog_loop(void)
172 {
173         struct stat sb;
174         int failed;
175
176         while (end_program != 2) {
177                 failed = 0;
178
179                 if (test_cmd != NULL)
180                         failed = system(test_cmd);
181                 else
182                         failed = stat("/etc", &sb);
183
184                 if (failed == 0)
185                         watchdog_patpat(timeout|WD_ACTIVE);
186                 sleep(nap);
187
188                 if (end_program != 0) {
189                         if (watchdog_onoff(0) == 0) {
190                                 end_program = 2;
191                         } else {
192                                 warnx("Could not stop the watchdog, not exitting");
193                                 end_program = 0;
194                         }
195                 }
196         }
197 }
198
199 /*
200  * Reset the watchdog timer. This function must be called periodically
201  * to keep the watchdog from firing.
202  */
203 int
204 watchdog_patpat(u_int t)
205 {
206
207         return ioctl(fd, WDIOCPATPAT, &t);
208 }
209
210 /*
211  * Toggle the kernel's watchdog. This routine is used to enable and
212  * disable the watchdog.
213  */
214 static int
215 watchdog_onoff(int onoff)
216 {
217
218         if (onoff)
219                 return watchdog_patpat((timeout|WD_ACTIVE));
220         else
221                 return watchdog_patpat(0);
222 }
223
224 /*
225  * Tell user how to use the program.
226  */
227 static void
228 usage()
229 {
230         if (is_daemon)
231                 fprintf(stderr, "usage: watchdogd [-d] [-e cmd] [-I file] [-s sleep] [-t timeout]\n");
232         else
233                 fprintf(stderr, "usage: watchdog [-d] [-t timeout]\n");
234         exit(EX_USAGE);
235 }
236
237 /*
238  * Handle the few command line arguments supported.
239  */
240 static void
241 parseargs(int argc, char *argv[])
242 {
243         int c;
244         char *p;
245         double a;
246
247         c = strlen(argv[0]);
248         if (argv[0][c - 1] == 'd')
249                 is_daemon = 1;
250         while ((c = getopt(argc, argv,
251             is_daemon ? "I:de:s:t:?" : "dt:?")) != -1) {
252                 switch (c) {
253                 case 'I':
254                         pidfile = optarg;
255                         break;
256                 case 'd':
257                         debugging = 1;
258                         break;
259                 case 'e':
260                         test_cmd = strdup(optarg);
261                         break;
262 #ifdef notyet
263                 case 'p':
264                         passive = 1;
265                         break;
266 #endif
267                 case 's':
268                         p = NULL;
269                         errno = 0;
270                         nap = strtol(optarg, &p, 0);
271                         if ((p != NULL && *p != '\0') || errno != 0)
272                                 errx(EX_USAGE, "-s argument is not a number");
273                         break;
274                 case 't':
275                         p = NULL;
276                         errno = 0;
277                         a = strtod(optarg, &p);
278                         if ((p != NULL && *p != '\0') || errno != 0)
279                                 errx(EX_USAGE, "-t argument is not a number");
280                         if (a < 0)
281                                 errx(EX_USAGE, "-t argument must be positive");
282                         if (a == 0)
283                                 timeout = WD_TO_NEVER;
284                         else
285                                 timeout = 1.0 + log(a * 1e9) / log(2.0);
286                         if (debugging)
287                                 printf("Timeout is 2^%d nanoseconds\n",
288                                     timeout);
289                         break;
290                 case '?':
291                 default:
292                         usage();
293                         /* NOTREACHED */
294                 }
295         }
296         if (argc != optind)
297                 errx(EX_USAGE, "extra arguments.");
298         if (is_daemon && timeout < WD_TO_1SEC)
299                 errx(EX_USAGE, "-t argument is less than one second.");
300 }