]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.sbin/watchdogd/watchdogd.c
MFC r302371:
[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 <strings.h>
52 #include <sysexits.h>
53 #include <unistd.h>
54
55 static void     parseargs(int, char *[]);
56 static void     sighandler(int);
57 static void     watchdog_loop(void);
58 static int      watchdog_init(void);
59 static int      watchdog_onoff(int onoff);
60 static int      watchdog_patpat(u_int timeout);
61 static void     usage(void);
62
63 int debugging = 0;
64 int end_program = 0;
65 const char *pidfile = _PATH_VARRUN "watchdogd.pid";
66 int reset_mib[3];
67 size_t reset_miblen = 3;
68 u_int timeout = WD_TO_16SEC;
69 u_int passive = 0;
70 int is_daemon = 0;
71 int fd = -1;
72 int nap = 1;
73 char *test_cmd = NULL;
74
75 /*
76  * Periodically pat the watchdog, preventing it from firing.
77  */
78 int
79 main(int argc, char *argv[])
80 {
81         struct rtprio rtp;
82         struct pidfh *pfh;
83         pid_t otherpid;
84
85         if (getuid() != 0)
86                 errx(EX_SOFTWARE, "not super user");
87                 
88         parseargs(argc, argv);
89
90         rtp.type = RTP_PRIO_REALTIME;
91         rtp.prio = 0;
92         if (rtprio(RTP_SET, 0, &rtp) == -1)
93                 err(EX_OSERR, "rtprio");
94
95         if (watchdog_init() == -1)
96                 errx(EX_SOFTWARE, "unable to initialize watchdog");
97
98         if (is_daemon) {
99                 if (watchdog_onoff(1) == -1)
100                         exit(EX_SOFTWARE);
101
102                 pfh = pidfile_open(pidfile, 0600, &otherpid);
103                 if (pfh == NULL) {
104                         if (errno == EEXIST) {
105                                 errx(EX_SOFTWARE, "%s already running, pid: %d",
106                                     getprogname(), otherpid);
107                         }
108                         warn("Cannot open or create pidfile");
109                 }
110
111                 if (debugging == 0 && daemon(0, 0) == -1) {
112                         watchdog_onoff(0);
113                         pidfile_remove(pfh);
114                         err(EX_OSERR, "daemon");
115                 }
116
117                 signal(SIGHUP, SIG_IGN);
118                 signal(SIGINT, sighandler);
119                 signal(SIGTERM, sighandler);
120
121                 pidfile_write(pfh);
122                 if (madvise(0, 0, MADV_PROTECT) != 0)
123                         warn("madvise failed");
124                 if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
125                         warn("mlockall failed");
126
127                 watchdog_loop();
128
129                 /* exiting */
130                 pidfile_remove(pfh);
131                 return (EX_OK);
132         } else {
133                 if (passive)
134                         timeout |= WD_PASSIVE;
135                 else
136                         timeout |= WD_ACTIVE;
137                 if (watchdog_patpat(timeout) < 0)
138                         err(EX_OSERR, "patting the dog");
139                 return (EX_OK);
140         }
141 }
142
143 /*
144  * Catch signals and begin shutdown process.
145  */
146 static void
147 sighandler(int signum)
148 {
149
150         if (signum == SIGINT || signum == SIGTERM)
151                 end_program = 1;
152 }
153
154 /*
155  * Open the watchdog device.
156  */
157 static int
158 watchdog_init()
159 {
160
161         fd = open("/dev/" _PATH_WATCHDOG, O_RDWR);
162         if (fd >= 0)
163                 return (0);
164         warn("Could not open watchdog device");
165         return (-1);
166 }
167
168 /*
169  * Main program loop which is iterated every second.
170  */
171 static void
172 watchdog_loop(void)
173 {
174         struct stat sb;
175         int failed;
176
177         while (end_program != 2) {
178                 failed = 0;
179
180                 if (test_cmd != NULL)
181                         failed = system(test_cmd);
182                 else
183                         failed = stat("/etc", &sb);
184
185                 if (failed == 0)
186                         watchdog_patpat(timeout|WD_ACTIVE);
187                 sleep(nap);
188
189                 if (end_program != 0) {
190                         if (watchdog_onoff(0) == 0) {
191                                 end_program = 2;
192                         } else {
193                                 warnx("Could not stop the watchdog, not exitting");
194                                 end_program = 0;
195                         }
196                 }
197         }
198 }
199
200 /*
201  * Reset the watchdog timer. This function must be called periodically
202  * to keep the watchdog from firing.
203  */
204 int
205 watchdog_patpat(u_int t)
206 {
207
208         return ioctl(fd, WDIOCPATPAT, &t);
209 }
210
211 /*
212  * Toggle the kernel's watchdog. This routine is used to enable and
213  * disable the watchdog.
214  */
215 static int
216 watchdog_onoff(int onoff)
217 {
218
219         if (onoff)
220                 return watchdog_patpat((timeout|WD_ACTIVE));
221         else
222                 return watchdog_patpat(0);
223 }
224
225 /*
226  * Tell user how to use the program.
227  */
228 static void
229 usage()
230 {
231         if (is_daemon)
232                 fprintf(stderr, "usage: watchdogd [-d] [-e cmd] [-I file] [-s sleep] [-t timeout]\n");
233         else
234                 fprintf(stderr, "usage: watchdog [-d] [-t timeout]\n");
235         exit(EX_USAGE);
236 }
237
238 /*
239  * Handle the few command line arguments supported.
240  */
241 static void
242 parseargs(int argc, char *argv[])
243 {
244         int c;
245         char *p;
246         double a;
247
248         c = strlen(argv[0]);
249         if (argv[0][c - 1] == 'd')
250                 is_daemon = 1;
251         while ((c = getopt(argc, argv,
252             is_daemon ? "I:de:s:t:?" : "dt:?")) != -1) {
253                 switch (c) {
254                 case 'I':
255                         pidfile = optarg;
256                         break;
257                 case 'd':
258                         debugging = 1;
259                         break;
260                 case 'e':
261                         test_cmd = strdup(optarg);
262                         break;
263 #ifdef notyet
264                 case 'p':
265                         passive = 1;
266                         break;
267 #endif
268                 case 's':
269                         p = NULL;
270                         errno = 0;
271                         nap = strtol(optarg, &p, 0);
272                         if ((p != NULL && *p != '\0') || errno != 0)
273                                 errx(EX_USAGE, "-s argument is not a number");
274                         break;
275                 case 't':
276                         p = NULL;
277                         errno = 0;
278                         a = strtod(optarg, &p);
279                         if ((p != NULL && *p != '\0') || errno != 0)
280                                 errx(EX_USAGE, "-t argument is not a number");
281                         if (a < 0)
282                                 errx(EX_USAGE, "-t argument must be positive");
283                         if (a == 0)
284                                 timeout = WD_TO_NEVER;
285                         else
286                                 timeout = flsll(a * 1e9);
287                         if (debugging)
288                                 printf("Timeout is 2^%d nanoseconds\n",
289                                     timeout);
290                         break;
291                 case '?':
292                 default:
293                         usage();
294                         /* NOTREACHED */
295                 }
296         }
297         if (argc != optind)
298                 errx(EX_USAGE, "extra arguments.");
299         if (is_daemon && timeout < WD_TO_1SEC)
300                 errx(EX_USAGE, "-t argument is less than one second.");
301 }