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