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);
35 /* Older Kamikaze versions are compiled with
36 * CONFIG_WATCHDOG_NOWAYOUT=y, this can be
37 * harmful if we're in the middle of an upgrade.
38 * Increase the watchdog timeout to 3600 seconds
39 * here to avoid unplanned reboots. */
40 ioctl(wdfd
, WDIOC_SETTIMEOUT
, &wdelay
);
49 /* Get BSSID of given interface */
50 static int iw_get_bssid(int iwfd
, const char *ifname
, char *bssid
)
54 if( iw_ioctl(iwfd
, ifname
, SIOCGIWAP
, &iwrq
) >= 0 )
56 unsigned char *addr
= (unsigned char *)iwrq
.u
.ap_addr
.sa_data
;
58 sprintf(bssid
, "%02X:%02X:%02X:%02X:%02X:%02X",
59 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
67 /* Get channel of given interface */
68 static int iw_get_channel(int iwfd
, const char *ifname
, int *channel
)
71 char buffer
[sizeof(struct iw_range
)];
72 double cur_freq
, cmp_freq
;
74 struct iw_range
*range
;
76 memset(buffer
, 0, sizeof(buffer
));
78 iwrq
.u
.data
.pointer
= (char *)buffer
;
79 iwrq
.u
.data
.length
= sizeof(buffer
);
80 iwrq
.u
.data
.flags
= 0;
82 if( iw_ioctl(iwfd
, ifname
, SIOCGIWRANGE
, &iwrq
) < 0)
88 range
= (struct iw_range
*)buffer
;
90 if( iw_ioctl(iwfd
, ifname
, SIOCGIWFREQ
, &iwrq
) >= 0 )
92 cur_freq
= ((double)iwrq
.u
.freq
.m
) * pow(10, iwrq
.u
.freq
.e
);
93 if( cur_freq
< 1000.00 )
95 *channel
= (int)cur_freq
;
99 for(i
= 0; i
< range
->num_frequency
; i
++)
101 cmp_freq
= ((double)range
->freq
[i
].m
) * pow(10, range
->freq
[i
].e
);
102 if( cmp_freq
== cur_freq
)
104 *channel
= (int)range
->freq
[i
].i
;
114 /* Get the (first) pid of given process name */
115 static int find_process(const char *name
)
122 struct dirent
*entry
;
124 if( (dir
= opendir("/proc")) != NULL
)
126 snprintf(cmpname
, sizeof(cmpname
), "Name:\t%s\n", name
);
128 while( (entry
= readdir(dir
)) != NULL
)
130 if( !strcmp(entry
->d_name
, "..") || !isdigit(*entry
->d_name
) )
133 sprintf(buffer
, "/proc/%s/status", entry
->d_name
);
134 if( (file
= open(buffer
, O_RDONLY
)) > -1 )
136 read(file
, buffer
, sizeof(buffer
));
139 if( strstr(buffer
, cmpname
) == buffer
)
141 pid
= atoi(entry
->d_name
);
143 /* Skip myself ... */
144 if( pid
== getpid() )
156 syslog(LOG_CRIT
, "Unable to open /proc: %s",
162 /* Get the 5 minute load average */
163 static double find_loadavg(void)
169 if( (fd
= open("/proc/loadavg", O_RDONLY
)) > -1 )
171 if( read(fd
, buffer
, sizeof(buffer
)) == sizeof(buffer
) )
172 load
= atof(&buffer
[5]);
180 /* Check if given uci file was updated */
181 static int check_uci_update(const char *config
, time_t *mtime
)
186 snprintf(path
, sizeof(path
), "/var/state/%s", config
);
187 if( stat(path
, &s
) > -1 )
189 if( (*mtime
== 0) || (s
.st_mtime
> *mtime
) )
202 static void load_wifi_uci_add_iface(const char *section
, struct uci_itr_ctx
*itr
)
208 ucitmp
= ucix_get_option(itr
->ctx
, "wireless", section
, "mode");
209 if( ucitmp
&& !strncmp(ucitmp
, "adhoc", 5) )
211 if( (t
= (wifi_tuple_t
*)malloc(sizeof(wifi_tuple_t
))) != NULL
)
213 ucitmp
= ucix_get_option(itr
->ctx
, "wireless", section
, "ifname");
216 strncpy(t
->ifname
, ucitmp
, sizeof(t
->ifname
));
220 ucitmp
= ucix_get_option(itr
->ctx
, "wireless", section
, "bssid");
223 strncpy(t
->bssid
, ucitmp
, sizeof(t
->bssid
));
227 ucitmp
= ucix_get_option(itr
->ctx
, "wireless", section
, "device");
230 ucitmp
= ucix_get_option(itr
->ctx
, "wireless", ucitmp
, "channel");
233 t
->channel
= atoi(ucitmp
);
240 syslog(LOG_INFO
, "Monitoring %s: bssid=%s channel=%d",
241 t
->ifname
, t
->bssid
, t
->channel
);
255 static wifi_tuple_t
* load_wifi_uci(wifi_tuple_t
*ifs
, time_t *modtime
)
257 struct uci_context
*ctx
;
258 struct uci_itr_ctx itr
;
259 wifi_tuple_t
*cur
, *next
;
261 if( check_uci_update("wireless", modtime
) )
263 syslog(LOG_INFO
, "Config changed, reloading");
265 if( (ctx
= ucix_init("wireless")) != NULL
)
269 for(cur
= ifs
; cur
; cur
= next
)
279 ucix_for_each_section_type(ctx
, "wireless", "wifi-iface",
280 (void *)load_wifi_uci_add_iface
, &itr
);
289 /* Daemon implementation */
290 static int do_daemon(void)
292 static int wdtrigger
= 1;
293 static int wdtimeout
= INTERVAL
* 2;
294 static const char wdkeepalive
= WATCH_KEEPALIVE
;
301 wifi_tuple_t
*ifs
= NULL
, *curif
;
304 int restart_wifi
= 0;
305 int restart_cron
= 0;
306 int restart_sshd
= 0;
307 int loadavg_panic
= 0;
309 openlog(SYSLOG_IDENT
, 0, LOG_DAEMON
);
312 if( (iwfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1 )
314 syslog(LOG_ERR
, "Can not open wireless control socket: %s",
320 if( (wdfd
= open(WATCH_DEVICE
, O_WRONLY
)) > -1 )
322 syslog(LOG_INFO
, "Opened %s - polling every %i seconds",
323 WATCH_DEVICE
, INTERVAL
);
325 /* Install signal handler to halt watchdog on shutdown */
326 sa
.sa_handler
= shutdown_watchdog
;
327 sa
.sa_flags
= SA_NOCLDWAIT
| SA_RESTART
;
328 sigaction(SIGHUP
, &sa
, NULL
);
329 sigaction(SIGINT
, &sa
, NULL
);
330 sigaction(SIGPIPE
, &sa
, NULL
);
331 sigaction(SIGTERM
, &sa
, NULL
);
332 sigaction(SIGUSR1
, &sa
, NULL
);
333 sigaction(SIGUSR2
, &sa
, NULL
);
335 /* Set watchdog timeout to twice the interval */
336 ioctl(wdfd
, WDIOC_SETTIMEOUT
, &wdtimeout
);
341 /* Check average load */
342 if( find_loadavg() >= LOAD_TRESHOLD
)
348 if( find_process("crond") < 0 )
352 if( find_process("dropbear") < 0 )
355 /* Check wireless interfaces */
356 ifs
= load_wifi_uci(ifs
, &modtime
);
357 for( curif
= ifs
; curif
; curif
= curif
->next
)
359 /* Get current channel and bssid */
360 if( (iw_get_bssid(iwfd
, curif
->ifname
, bssid
) == 0) &&
361 (iw_get_channel(iwfd
, curif
->ifname
, &channel
) == 0) )
364 if( strcasecmp(bssid
, curif
->bssid
) != 0 )
366 syslog(LOG_WARNING
, "BSSID mismatch on %s: current=%s wanted=%s",
367 curif
->ifname
, bssid
, curif
->bssid
);
373 else if( channel
!= curif
->channel
)
375 syslog(LOG_WARNING
, "Channel mismatch on %s: current=%d wanted=%d",
376 curif
->ifname
, channel
, curif
->channel
);
383 syslog(LOG_WARNING
, "Requested interface %s not present", curif
->ifname
);
388 /* Wifi restart required? */
389 if( restart_wifi
>= HYSTERESIS
)
392 syslog(LOG_WARNING
, "Channel or BSSID mismatch on wireless interface, restarting");
396 /* Cron restart required? */
397 if( restart_cron
>= HYSTERESIS
)
400 syslog(LOG_WARNING
, "The cron process died, restarting");
404 /* SSHd restart required? */
405 if( restart_sshd
>= HYSTERESIS
)
408 syslog(LOG_WARNING
, "The ssh process died, restarting");
412 /* Is there a load problem? */
413 if( loadavg_panic
>= HYSTERESIS
)
415 syslog(LOG_EMERG
, "Critical system load level, triggering reset!");
417 /* Try watchdog, fall back to reboot */
419 ioctl(wdfd
, WDIOC_SETTIMEOUT
, &wdtrigger
);
424 /* Reset watchdog timer */
426 write(wdfd
, &wdkeepalive
, 1);
431 shutdown_watchdog(0);
438 int main(int argc
, char *argv
[])
440 /* Check if watchdog is running ... */
441 if( (argc
> 1) && (strcmp(argv
[1], "running") == 0) )
443 return (find_process(BINARY
) == -1);