contrib/package/freifunk-watchdog: workaround CONFIG_WATCHDOG_NOWAYOUT, trap signals
[project/luci.git] / contrib / package / freifunk-watchdog / src / watchdog.h
1 /*
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.
6  *
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.
11  *
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.
15  *
16  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <syslog.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <math.h>
30 #include <time.h>
31 #include <signal.h>
32 #include <sys/stat.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <linux/types.h>
36 #include <linux/watchdog.h>
37
38 #include "ucix.h"
39 #include "wireless.22.h"
40
41
42 /* Check interval */
43 #define INTERVAL                30
44
45 /* Hysteresis */
46 #define HYSTERESIS              3
47
48 /* How to call myself in the logs */
49 #define SYSLOG_IDENT    "Freifunk Watchdog"
50
51 /* Wifi error action */
52 #define WIFI_ACTION             "/sbin/wifi", "/sbin/wifi"
53
54 /* Crond error action */
55 #define CRON_ACTION             "/etc/init.d/cron", "/etc/init.d/cron", "restart"
56
57 /* SSHd error action */
58 #define SSHD_ACTION             "/etc/init.d/dropbear", "/etc/init.d/dropbear", "restart"
59
60 /* Watchdog device */
61 #define WATCH_DEVICE    "/dev/watchdog"
62 #define WATCH_SHUTDOWN  'V'
63 #define WATCH_KEEPALIVE '\0'
64
65 /* System load error action and treshold */
66 #define LOAD_TRESHOLD   5.00
67 #define LOAD_ACTION             "/sbin/reboot"
68
69 /* Fallback binary name (passed by makefile) */
70 #ifndef BINARY
71 #define BINARY "ffwatchd"
72 #endif
73
74
75 /* ifname/bssid/channel tuples */
76 struct wifi_tuple {
77         char ifname[16];
78         char bssid[18];
79         int channel;
80         struct wifi_tuple *next;
81 };
82
83 /* structure to hold tuple-list and uci context during iteration */
84 struct uci_itr_ctx {
85         struct wifi_tuple *list;
86         struct uci_context *ctx;
87 };
88
89 typedef struct wifi_tuple wifi_tuple_t;
90
91
92 /* ioctl() helper (stolen from iwlib) */
93 static inline int
94 iw_ioctl(int                  skfd,           /* Socket to the kernel */
95          const char *         ifname,         /* Device name */
96          int                  request,        /* WE ID */
97          struct iwreq *       pwrq)           /* Fixed part of the request */
98 {
99   /* Set device name */
100   strncpy(pwrq->ifr_ifrn.ifrn_name, ifname, 16);
101
102   /* Do the request */
103   return(ioctl(skfd, request, pwrq));
104 }
105
106 /* fork() & execl() helper */
107 #define EXEC(x)                                                                                                                 \
108         do {                                                                                                                            \
109                 switch(fork())                                                                                                  \
110                 {                                                                                                                               \
111                         case -1:                                                                                                        \
112                                 syslog(LOG_CRIT, "Unable to fork child: %s",                    \
113                                         strerror(errno));                                                                       \
114                                                                                                                                                 \
115                                         break;                                                                                          \
116                                                                                                                                                 \
117                                 case 0:                                                                                                 \
118                                         execl(x, NULL);                                                                         \
119                                         syslog(LOG_CRIT, "Unable to execute action: %s",        \
120                                                 strerror(errno));                                                               \
121                                                                                                                                                 \
122                                         return 1;                                                                                       \
123                 }                                                                                                                               \
124         } while(0)
125