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