contrib/package/freifunk-watchdog:
[project/luci.git] / contrib / package / freifunk-watchdog / src / watchdog.c
index 5caf6d5..b87bed0 100644 (file)
 
 #include "watchdog.h"
 
+/* Global watchdog fd, required by signal handler */
+int wdfd = -1;
+
+/* Watchdog shutdown helper */
+static void shutdown_watchdog(int sig)
+{
+       static int wdelay = 3600;
+       static const char wshutdown = WATCH_SHUTDOWN;
+
+       if( wdfd > -1 )
+       {
+               syslog(LOG_INFO, "Stopping watchdog timer");
+               write(wdfd, &wshutdown, 1);
+               close(wdfd);
+               wdfd = -1;
+       }
+
+       exit(0);
+}
+
 /* Get BSSID of given interface */
 static int iw_get_bssid(int iwfd, const char *ifname, char *bssid)
 {
@@ -163,11 +183,9 @@ static int check_uci_update(const char *config, time_t *mtime)
                        *mtime = s.st_mtime;
                        return 1;
                }
-
-               return 0;
        }
 
-       return -1;
+       return 0;
 }
 
 /* Add tuple */
@@ -177,44 +195,48 @@ static void load_wifi_uci_add_iface(const char *section, struct uci_itr_ctx *itr
        const char *ucitmp;
        int val = 0;
 
-       if( (t = (wifi_tuple_t *)malloc(sizeof(wifi_tuple_t))) != NULL )
+       ucitmp = ucix_get_option(itr->ctx, "wireless", section, "mode");
+       if( ucitmp && !strncmp(ucitmp, "adhoc", 5) )
        {
-               ucitmp = ucix_get_option(itr->ctx, "wireless", section, "ifname");
-               if(ucitmp)
+               if( (t = (wifi_tuple_t *)malloc(sizeof(wifi_tuple_t))) != NULL )
                {
-                       strncpy(t->ifname, ucitmp, sizeof(t->ifname));
-                       val++;
-               }
-
-               ucitmp = ucix_get_option(itr->ctx, "wireless", section, "bssid");
-               if(ucitmp)
-               {
-                       strncpy(t->bssid, ucitmp, sizeof(t->bssid));
-                       val++;
-               }
+                       ucitmp = ucix_get_option(itr->ctx, "wireless", section, "ifname");
+                       if(ucitmp)
+                       {
+                               strncpy(t->ifname, ucitmp, sizeof(t->ifname));
+                               val++;
+                       }
 
-               ucitmp = ucix_get_option(itr->ctx, "wireless", section, "device");
-               if(ucitmp)
-               {
-                       ucitmp = ucix_get_option(itr->ctx, "wireless", ucitmp, "channel");
+                       ucitmp = ucix_get_option(itr->ctx, "wireless", section, "bssid");
                        if(ucitmp)
                        {
-                               t->channel = atoi(ucitmp);
+                               strncpy(t->bssid, ucitmp, sizeof(t->bssid));
                                val++;
                        }
-               }
 
-               if( val == 3 )
-               {
-                       syslog(LOG_INFO, "Monitoring %s: bssid=%s channel=%d",
-                               t->ifname, t->bssid, t->channel);
+                       ucitmp = ucix_get_option(itr->ctx, "wireless", section, "device");
+                       if(ucitmp)
+                       {
+                               ucitmp = ucix_get_option(itr->ctx, "wireless", ucitmp, "channel");
+                               if(ucitmp)
+                               {
+                                       t->channel = atoi(ucitmp);
+                                       val++;
+                               }
+                       }
 
-                       t->next = itr->list;
-                       itr->list = t;
-               }
-               else
-               {
-                       free(t);
+                       if( val == 3 )
+                       {
+                               syslog(LOG_INFO, "Monitoring %s: bssid=%s channel=%d",
+                                       t->ifname, t->bssid, t->channel);
+
+                               t->next = itr->list;
+                               itr->list = t;
+                       }
+                       else
+                       {
+                               free(t);
+                       }
                }
        }
 }
@@ -257,15 +279,19 @@ static wifi_tuple_t * load_wifi_uci(wifi_tuple_t *ifs, time_t *modtime)
 /* Daemon implementation */
 static int do_daemon(void)
 {
+       static int wdtrigger = 1;
+       static int wdtimeout = BASE_INTERVAL * 2;
+       static const char wdkeepalive = WATCH_KEEPALIVE;
+
        int iwfd;
-       int wdfd;
-       int wdtrigger = 1;
        int channel;
        char bssid[18];
+       struct sigaction sa;
 
        wifi_tuple_t *ifs = NULL, *curif;
        time_t modtime = 0;
 
+       int action_intv = 0;
        int restart_wifi = 0;
        int restart_cron = 0;
        int restart_sshd = 0;
@@ -284,110 +310,130 @@ static int do_daemon(void)
 
        if( (wdfd = open(WATCH_DEVICE, O_WRONLY)) > -1 )
        {
-               syslog(LOG_INFO, "Opened %s - polling each %i seconds",
-                       WATCH_DEVICE, INTERVAL);
+               syslog(LOG_INFO, "Opened %s - polling every %i seconds",
+                       WATCH_DEVICE, BASE_INTERVAL);
+
+               /* Install signal handler to halt watchdog on shutdown */
+               sa.sa_handler = shutdown_watchdog;
+               sa.sa_flags = SA_NOCLDWAIT | SA_RESTART;
+               sigaction(SIGHUP,  &sa, NULL);
+               sigaction(SIGINT,  &sa, NULL);
+               sigaction(SIGPIPE, &sa, NULL);
+               sigaction(SIGTERM, &sa, NULL);
+               sigaction(SIGUSR1, &sa, NULL);
+               sigaction(SIGUSR2, &sa, NULL);
+
+               /* Set watchdog timeout to twice the interval */
+               ioctl(wdfd, WDIOC_SETTIMEOUT, &wdtimeout);
        }
 
        while( 1 )
        {
-               /* Check average load */
-               if( find_loadavg() >= LOAD_TRESHOLD )
-                       loadavg_panic++;
-               else
-                       loadavg_panic = 0;
-
-               /* Check crond */
-               if( find_process("crond") < 0 )
-                       restart_cron++;
-
-               /* Check SSHd */
-               if( find_process("dropbear") < 0 )
-                       restart_sshd++;
-
-               /* Check wireless interfaces */
-               ifs = load_wifi_uci(ifs, &modtime);
-               for( curif = ifs; curif; curif = curif->next )
+               /* Check/increment action interval */
+               if( ++action_intv >= ACTION_INTERVAL )
                {
-                       /* Get current channel and bssid */
-                       if( (iw_get_bssid(iwfd, curif->ifname, bssid) == 0) &&
-                           (iw_get_channel(iwfd, curif->ifname, &channel) == 0) )
+                       /* Reset action interval */
+                       action_intv = 0;
+
+                       /* Check average load */
+                       if( find_loadavg() >= LOAD_TRESHOLD )
+                               loadavg_panic++;
+                       else
+                               loadavg_panic = 0;
+
+                       /* Check crond */
+                       if( find_process("crond") < 0 )
+                               restart_cron++;
+                       else
+                               restart_cron = 0;
+
+                       /* Check SSHd */
+                       if( find_process("dropbear") < 0 )
+                               restart_sshd++;
+                       else
+                               restart_sshd = 0;
+
+                       /* Check wireless interfaces */
+                       ifs = load_wifi_uci(ifs, &modtime);
+                       for( curif = ifs; curif; curif = curif->next )
                        {
-                               /* Check BSSID */
-                               if( strcasecmp(bssid, curif->bssid) != 0 )
+                               /* Get current channel and bssid */
+                               if( (iw_get_bssid(iwfd, curif->ifname, bssid) == 0) &&
+                           (iw_get_channel(iwfd, curif->ifname, &channel) == 0) )
                                {
-                                       syslog(LOG_WARNING, "BSSID mismatch on %s: current=%s wanted=%s",
-                                               curif->ifname, bssid, curif->bssid);
-
-                                       restart_wifi++;
+                                       /* Check BSSID */
+                                       if( strcasecmp(bssid, curif->bssid) != 0 )
+                                       {
+                                               syslog(LOG_WARNING, "BSSID mismatch on %s: current=%s wanted=%s",
+                                                       curif->ifname, bssid, curif->bssid);
+
+                                               restart_wifi++;
+                                       }
+
+                                       /* Check channel */
+                                       else if( channel != curif->channel )
+                                       {
+                                               syslog(LOG_WARNING, "Channel mismatch on %s: current=%d wanted=%d",
+                                                       curif->ifname, channel, curif->channel);
+
+                                               restart_wifi++;
+                                       }
                                }
-
-                               /* Check channel */
-                               else if( channel != curif->channel )
+                               else
                                {
-                                       syslog(LOG_WARNING, "Channel mismatch on %s: current=%d wanted=%d",
-                                               curif->ifname, channel, curif->channel);
-
-                                       restart_wifi++;
+                                       syslog(LOG_WARNING, "Requested interface %s not present", curif->ifname);
                                }
                        }
-                       else
+
+
+                       /* Wifi restart required? */
+                       if( restart_wifi >= HYSTERESIS )
                        {
-                               syslog(LOG_WARNING, "Requested interface %s not present", curif->ifname);
+                               restart_wifi = 0;
+                               syslog(LOG_WARNING, "Channel or BSSID mismatch on wireless interface, restarting");
+                               EXEC(WIFI_ACTION);
                        }
-               }
 
+                       /* Cron restart required? */
+                       if( restart_cron >= HYSTERESIS )
+                       {
+                               restart_cron = 0;
+                               syslog(LOG_WARNING, "The cron process died, restarting");
+                               EXEC(CRON_ACTION);
+                       }
 
-               /* Wifi restart required? */
-               if( restart_wifi >= HYSTERESIS )
-               {
-                       restart_wifi = 0;
-                       syslog(LOG_WARNING, "Channel or BSSID mismatch on wireless interface, restarting");
-                       EXEC(WIFI_ACTION);
-               }
+                       /* SSHd restart required? */
+                       if( restart_sshd >= HYSTERESIS )
+                       {
+                               restart_sshd = 0;
+                               syslog(LOG_WARNING, "The ssh process died, restarting");
+                               EXEC(SSHD_ACTION);
+                       }
 
-               /* Cron restart required? */
-               if( restart_cron >= HYSTERESIS )
-               {
-                       restart_cron = 0;
-                       syslog(LOG_WARNING, "The cron process died, restarting");
-                       EXEC(CRON_ACTION);
-               }
+                       /* Is there a load problem? */
+                       if( loadavg_panic >= HYSTERESIS )
+                       {
+                               syslog(LOG_EMERG, "Critical system load level, triggering reset!");
 
-               /* SSHd restart required? */
-               if( restart_sshd >= HYSTERESIS )
-               {
-                       restart_sshd = 0;
-                       syslog(LOG_WARNING, "The ssh process died, restarting");
-                       EXEC(SSHD_ACTION);
+                               /* Try watchdog, fall back to reboot */
+                               if( wdfd > -1 )
+                                       ioctl(wdfd, WDIOC_SETTIMEOUT, &wdtrigger);
+                               else
+                                       EXEC(LOAD_ACTION);
+                       }
                }
 
-               /* Is there a load problem? */
-               if( loadavg_panic >= HYSTERESIS )
-               {
-                       syslog(LOG_EMERG, "Critical system load level, triggering reset!");
-
-                       /* Try watchdog, fall back to reboot */
-                       if( wdfd > -1 )
-                               ioctl(wdfd, WDIOC_SETTIMEOUT, &wdtrigger);
-                       else
-                               EXEC(LOAD_ACTION);
-               }
 
                /* Reset watchdog timer */
                if( wdfd > -1 )
-                       write(wdfd, '\0', 1);
+                       write(wdfd, &wdkeepalive, 1);
 
-               sleep(INTERVAL);
-       }
-
-       if( wdfd > -1 )
-       {
-               syslog(LOG_INFO, "Stopping watchdog timer");
-               write(wdfd, WATCH_SHUTDOWN, 1);
-               close(wdfd);
+               sleep(BASE_INTERVAL);
        }
 
+       shutdown_watchdog(0);
        closelog();
+
        return 0;
 }