6f2382de38b54717b8d90e86af40594188b958a6
[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 <jow@openwrt.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 <limits.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <linux/types.h>
38 #include <linux/watchdog.h>
39
40 #include "ucix.h"
41 #include "wireless.22.h"
42
43
44 /* Watchdog poll interval */
45 #define BASE_INTERVAL 5
46
47 /* Action interval (N * BASE_INTERVAL) */
48 #define ACTION_INTERVAL 6
49
50 /* Hysteresis */
51 #define HYSTERESIS 3
52
53 /* How to call myself in the logs */
54 #define SYSLOG_IDENT "Freifunk Watchdog"
55
56 /* Process error action */
57 #define PROC_ACTION curr_proc->initscript, curr_proc->initscript, "restart"
58
59 /* Wifi error action */
60 #define WIFI_ACTION "/sbin/wifi", "/sbin/wifi"
61
62 /* Watchdog device */
63 #define WATCH_DEVICE "/dev/watchdog"
64 #define WATCH_SHUTDOWN 'V'
65 #define WATCH_KEEPALIVE '\0'
66
67 /* System load error action and treshold */
68 #define LOAD_TRESHOLD 15.00
69 #define LOAD_ACTION "/sbin/reboot", "/sbin/reboot"
70
71 /* Fallback binary name (passed by makefile) */
72 #ifndef BINARY
73 #define BINARY "ffwatchd"
74 #endif
75
76
77 /* ifname/bssid/channel tuples */
78 struct wifi_tuple {
79 char ifname[16];
80 char bssid[18];
81 int channel;
82 struct wifi_tuple *next;
83 };
84
85 /* structure to hold tuple-list and uci context during iteration */
86 struct uci_wifi_iface_itr_ctx {
87 struct wifi_tuple *list;
88 struct uci_context *ctx;
89 };
90
91 typedef struct wifi_tuple wifi_tuple_t;
92
93
94 /* process name/exec tuples */
95 struct process_tuple {
96 char process[PATH_MAX + 1];
97 char initscript[PATH_MAX + 1];
98 int restart;
99 struct process_tuple *next;
100 };
101
102 /* structure to hold tuple-list and uci context during iteration */
103 struct uci_process_itr_ctx {
104 struct process_tuple *list;
105 struct uci_context *ctx;
106 };
107
108 typedef struct process_tuple process_tuple_t;
109
110
111 /* ioctl() helper (stolen from iwlib) */
112 static inline int
113 iw_ioctl(int skfd, /* Socket to the kernel */
114 const char * ifname, /* Device name */
115 int request, /* WE ID */
116 struct iwreq * pwrq) /* Fixed part of the request */
117 {
118 /* Set device name */
119 strncpy(pwrq->ifr_ifrn.ifrn_name, ifname, 16);
120
121 /* Do the request */
122 return(ioctl(skfd, request, pwrq));
123 }
124
125 /* fork() & execl() helper */
126 #define EXEC(x) \
127 do { \
128 switch(fork()) \
129 { \
130 case -1: \
131 syslog(LOG_CRIT, "Unable to fork child: %s", \
132 strerror(errno)); \
133 break; \
134 \
135 case 0: \
136 execl(x, NULL); \
137 syslog(LOG_CRIT, "Unable to execute action: %s", \
138 strerror(errno)); \
139 return 1; \
140 } \
141 } while(0)
142