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