2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
21 /* Global watchdog fd, required by signal handler */
24 /* Watchdog shutdown helper */
25 static void shutdown_watchdog(int sig)
27 static int wdelay = 3600;
28 static const char wshutdown = WATCH_SHUTDOWN;
32 syslog(LOG_INFO, "Stopping watchdog timer");
33 write(wdfd, &wshutdown, 1);
41 /* Get BSSID of given interface */
42 static int iw_get_bssid(int iwfd, const char *ifname, char *bssid)
46 if( iw_ioctl(iwfd, ifname, SIOCGIWAP, &iwrq) >= 0 )
48 unsigned char *addr = (unsigned char *)iwrq.u.ap_addr.sa_data;
50 sprintf(bssid, "%02X:%02X:%02X:%02X:%02X:%02X",
51 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
59 /* Get channel of given interface */
60 static int iw_get_channel(int iwfd, const char *ifname, int *channel)
63 char buffer[sizeof(struct iw_range)];
64 double cur_freq, cmp_freq;
66 struct iw_range *range;
68 memset(buffer, 0, sizeof(buffer));
70 iwrq.u.data.pointer = (char *)buffer;
71 iwrq.u.data.length = sizeof(buffer);
72 iwrq.u.data.flags = 0;
74 if( iw_ioctl(iwfd, ifname, SIOCGIWRANGE, &iwrq) < 0)
80 range = (struct iw_range *)buffer;
82 if( iw_ioctl(iwfd, ifname, SIOCGIWFREQ, &iwrq) >= 0 )
84 cur_freq = ((double)iwrq.u.freq.m) * pow(10, iwrq.u.freq.e);
85 if( cur_freq < 1000.00 )
87 *channel = (int)cur_freq;
91 for(i = 0; i < range->num_frequency; i++)
93 cmp_freq = ((double)range->freq[i].m) * pow(10, range->freq[i].e);
94 if( cmp_freq == cur_freq )
96 *channel = (int)range->freq[i].i;
106 /* Get the (first) pid of given process name */
107 static int find_process(const char *name)
114 struct dirent *entry;
116 if( (dir = opendir("/proc")) != NULL )
118 snprintf(cmpname, sizeof(cmpname), "Name:\t%s\n", name);
120 while( (entry = readdir(dir)) != NULL )
122 if( !strcmp(entry->d_name, "..") || !isdigit(*entry->d_name) )
125 sprintf(buffer, "/proc/%s/status", entry->d_name);
126 if( (file = open(buffer, O_RDONLY)) > -1 )
128 read(file, buffer, sizeof(buffer));
131 if( strstr(buffer, cmpname) == buffer )
133 pid = atoi(entry->d_name);
135 /* Skip myself ... */
136 if( pid == getpid() )
148 syslog(LOG_CRIT, "Unable to open /proc: %s",
154 /* Get the 5 minute load average */
155 static double find_loadavg(void)
161 if( (fd = open("/proc/loadavg", O_RDONLY)) > -1 )
163 if( read(fd, buffer, sizeof(buffer)) == sizeof(buffer) )
164 load = atof(&buffer[5]);
172 /* Check if given uci file was updated */
173 static int check_uci_update(const char *config, time_t *mtime)
178 snprintf(path, sizeof(path), "/var/state/%s", config);
179 if( stat(path, &s) > -1 )
181 if( (*mtime == 0) || (s.st_mtime > *mtime) )
192 static void load_wifi_uci_add_iface(const char *section, struct uci_itr_ctx *itr)
198 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "mode");
199 if( ucitmp && !strncmp(ucitmp, "adhoc", 5) )
201 if( (t = (wifi_tuple_t *)malloc(sizeof(wifi_tuple_t))) != NULL )
203 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "ifname");
206 strncpy(t->ifname, ucitmp, sizeof(t->ifname));
210 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "bssid");
213 strncpy(t->bssid, ucitmp, sizeof(t->bssid));
217 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "device");
220 ucitmp = ucix_get_option(itr->ctx, "wireless", ucitmp, "channel");
223 t->channel = atoi(ucitmp);
230 syslog(LOG_INFO, "Monitoring %s: bssid=%s channel=%d",
231 t->ifname, t->bssid, t->channel);
245 static wifi_tuple_t * load_wifi_uci(wifi_tuple_t *ifs, time_t *modtime)
247 struct uci_context *ctx;
248 struct uci_itr_ctx itr;
249 wifi_tuple_t *cur, *next;
251 if( check_uci_update("wireless", modtime) )
253 syslog(LOG_INFO, "Config changed, reloading");
255 if( (ctx = ucix_init("wireless")) != NULL )
259 for(cur = ifs; cur; cur = next)
269 ucix_for_each_section_type(ctx, "wireless", "wifi-iface",
270 (void *)load_wifi_uci_add_iface, &itr);
279 /* Daemon implementation */
280 static int do_daemon(void)
282 static int wdtrigger = 1;
283 static int wdtimeout = BASE_INTERVAL * 2;
284 static const char wdkeepalive = WATCH_KEEPALIVE;
291 wifi_tuple_t *ifs = NULL, *curif;
295 int restart_wifi = 0;
296 int restart_cron = 0;
297 int restart_sshd = 0;
298 int loadavg_panic = 0;
300 openlog(SYSLOG_IDENT, 0, LOG_DAEMON);
303 if( (iwfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 )
305 syslog(LOG_ERR, "Can not open wireless control socket: %s",
311 if( (wdfd = open(WATCH_DEVICE, O_WRONLY)) > -1 )
313 syslog(LOG_INFO, "Opened %s - polling every %i seconds",
314 WATCH_DEVICE, BASE_INTERVAL);
316 /* Install signal handler to halt watchdog on shutdown */
317 sa.sa_handler = shutdown_watchdog;
318 sa.sa_flags = SA_NOCLDWAIT | SA_RESTART;
319 sigaction(SIGHUP, &sa, NULL);
320 sigaction(SIGINT, &sa, NULL);
321 sigaction(SIGPIPE, &sa, NULL);
322 sigaction(SIGTERM, &sa, NULL);
323 sigaction(SIGUSR1, &sa, NULL);
324 sigaction(SIGUSR2, &sa, NULL);
326 /* Set watchdog timeout to twice the interval */
327 ioctl(wdfd, WDIOC_SETTIMEOUT, &wdtimeout);
332 /* Check/increment action interval */
333 if( ++action_intv >= ACTION_INTERVAL )
335 /* Reset action interval */
338 /* Check average load */
339 if( find_loadavg() >= LOAD_TRESHOLD )
345 if( find_process("crond") < 0 )
351 if( find_process("dropbear") < 0 )
356 /* Check wireless interfaces */
357 ifs = load_wifi_uci(ifs, &modtime);
358 for( curif = ifs; curif; curif = curif->next )
360 /* Get current channel and bssid */
361 if( (iw_get_bssid(iwfd, curif->ifname, bssid) == 0) &&
362 (iw_get_channel(iwfd, curif->ifname, &channel) == 0) )
365 if( strcasecmp(bssid, curif->bssid) != 0 )
367 syslog(LOG_WARNING, "BSSID mismatch on %s: current=%s wanted=%s",
368 curif->ifname, bssid, curif->bssid);
374 else if( channel != curif->channel )
376 syslog(LOG_WARNING, "Channel mismatch on %s: current=%d wanted=%d",
377 curif->ifname, channel, curif->channel);
384 syslog(LOG_WARNING, "Requested interface %s not present", curif->ifname);
389 /* Wifi restart required? */
390 if( restart_wifi >= HYSTERESIS )
393 syslog(LOG_WARNING, "Channel or BSSID mismatch on wireless interface, restarting");
397 /* Cron restart required? */
398 if( restart_cron >= HYSTERESIS )
401 syslog(LOG_WARNING, "The cron process died, restarting");
405 /* SSHd restart required? */
406 if( restart_sshd >= HYSTERESIS )
409 syslog(LOG_WARNING, "The ssh process died, restarting");
413 /* Is there a load problem? */
414 if( loadavg_panic >= HYSTERESIS )
416 syslog(LOG_EMERG, "Critical system load level, triggering reset!");
418 /* Try watchdog, fall back to reboot */
420 ioctl(wdfd, WDIOC_SETTIMEOUT, &wdtrigger);
427 /* Reset watchdog timer */
429 write(wdfd, &wdkeepalive, 1);
431 sleep(BASE_INTERVAL);
434 shutdown_watchdog(0);
441 int main(int argc, char *argv[])
443 /* Check if watchdog is running ... */
444 if( (argc > 1) && (strcmp(argv[1], "running") == 0) )
446 return (find_process(BINARY) == -1);