contrib/package: add freifunk watchdog daemon
[project/luci.git] / contrib / package / freifunk-watchdog / src / watchdog.c
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 "watchdog.h"
20
21 /* Get BSSID of given interface */
22 static int iw_get_bssid(int iwfd, const char *ifname, char *bssid)
23 {
24 struct iwreq iwrq;
25
26 if( iw_ioctl(iwfd, ifname, SIOCGIWAP, &iwrq) >= 0 )
27 {
28 unsigned char *addr = (unsigned char *)iwrq.u.ap_addr.sa_data;
29
30 sprintf(bssid, "%02X:%02X:%02X:%02X:%02X:%02X",
31 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
32
33 return 0;
34 }
35
36 return -1;
37 }
38
39 /* Get channel of given interface */
40 static int iw_get_channel(int iwfd, const char *ifname, int *channel)
41 {
42 int i;
43 char buffer[sizeof(struct iw_range)];
44 double cur_freq, cmp_freq;
45 struct iwreq iwrq;
46 struct iw_range *range;
47
48 memset(buffer, 0, sizeof(buffer));
49
50 iwrq.u.data.pointer = (char *)buffer;
51 iwrq.u.data.length = sizeof(buffer);
52 iwrq.u.data.flags = 0;
53
54 if( iw_ioctl(iwfd, ifname, SIOCGIWRANGE, &iwrq) < 0)
55 {
56 *channel = -1;
57 return -1;
58 }
59
60 range = (struct iw_range *)buffer;
61
62 if( iw_ioctl(iwfd, ifname, SIOCGIWFREQ, &iwrq) >= 0 )
63 {
64 cur_freq = ((double)iwrq.u.freq.m) * pow(10, iwrq.u.freq.e);
65 if( cur_freq < 1000.00 )
66 {
67 *channel = (int)cur_freq;
68 return 0;
69 }
70
71 for(i = 0; i < range->num_frequency; i++)
72 {
73 cmp_freq = ((double)range->freq[i].m) * pow(10, range->freq[i].e);
74 if( cmp_freq == cur_freq )
75 {
76 *channel = (int)range->freq[i].i;
77 return 0;
78 }
79 }
80 }
81
82 *channel = -1;
83 return -1;
84 }
85
86 /* Get the (first) pid of given process name */
87 static int find_process(const char *name)
88 {
89 int pid = -1;
90 int file;
91 char buffer[128];
92 char cmpname[128];
93 DIR *dir;
94 struct dirent *entry;
95
96 if( (dir = opendir("/proc")) != NULL )
97 {
98 snprintf(cmpname, sizeof(cmpname), "Name:\t%s\n", name);
99
100 while( (entry = readdir(dir)) != NULL )
101 {
102 if( !strcmp(entry->d_name, "..") || !isdigit(*entry->d_name) )
103 continue;
104
105 sprintf(buffer, "/proc/%s/status", entry->d_name);
106 if( (file = open(buffer, O_RDONLY)) > -1 )
107 {
108 read(file, buffer, sizeof(buffer));
109 close(file);
110
111 if( strstr(buffer, cmpname) == buffer )
112 {
113 pid = atoi(entry->d_name);
114 break;
115 }
116 }
117 }
118
119 closedir(dir);
120 return pid;
121 }
122
123 syslog(LOG_CRIT, "Unable to open /proc: %s",
124 strerror(errno));
125
126 return -1;
127 }
128
129 /* Check if given uci file was updated */
130 static int check_uci_update(const char *config, time_t *mtime)
131 {
132 struct stat s;
133 char path[128];
134
135 snprintf(path, sizeof(path), "/etc/config/%s", config);
136 if( stat(path, &s) > -1 )
137 {
138 if( (*mtime == 0) || (s.st_mtime > *mtime) )
139 {
140 *mtime = s.st_mtime;
141 return 1;
142 }
143 else
144 {
145 snprintf(path, sizeof(path), "/var/state/%s", config);
146 if( stat(path, &s) > -1 )
147 {
148 if( (*mtime == 0) || (s.st_mtime > *mtime) )
149 {
150 *mtime = s.st_mtime;
151 return 1;
152 }
153 }
154
155 return 0;
156 }
157 }
158
159 return -1;
160 }
161
162 /* Add tuple */
163 static void load_wifi_uci_add_iface(const char *section, struct uci_itr_ctx *itr)
164 {
165 wifi_tuple_t *t;
166 const char *ucitmp;
167 int val = 0;
168
169 if( (t = (wifi_tuple_t *)malloc(sizeof(wifi_tuple_t))) != NULL )
170 {
171 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "ifname");
172 if(ucitmp)
173 {
174 strncpy(t->ifname, ucitmp, sizeof(t->ifname));
175 val++;
176 }
177
178 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "bssid");
179 if(ucitmp)
180 {
181 strncpy(t->bssid, ucitmp, sizeof(t->bssid));
182 val++;
183 }
184
185 ucitmp = ucix_get_option(itr->ctx, "wireless", section, "device");
186 if(ucitmp)
187 {
188 ucitmp = ucix_get_option(itr->ctx, "wireless", ucitmp, "channel");
189 if(ucitmp)
190 {
191 t->channel = atoi(ucitmp);
192 val++;
193 }
194 }
195
196 if( val == 3 )
197 {
198 syslog(LOG_INFO, "Monitoring %s: bssid=%s channel=%d",
199 t->ifname, t->bssid, t->channel);
200
201 t->next = itr->list;
202 itr->list = t;
203 }
204 else
205 {
206 free(t);
207 }
208 }
209 }
210
211 /* Load config */
212 static wifi_tuple_t * load_wifi_uci(wifi_tuple_t *ifs, time_t *modtime)
213 {
214 struct uci_context *ctx;
215 struct uci_itr_ctx itr;
216 wifi_tuple_t *cur, *next;
217
218 if( check_uci_update("wireless", modtime) )
219 {
220 syslog(LOG_INFO, "Config changed, reloading");
221
222 if( (ctx = ucix_init("wireless")) != NULL )
223 {
224 if( ifs != NULL )
225 {
226 for(cur = ifs; cur; cur = next)
227 {
228 next = cur->next;
229 free(cur);
230 }
231 }
232
233 itr.list = NULL;
234 itr.ctx = ctx;
235
236 ucix_for_each_section_type(ctx, "wireless", "wifi-iface",
237 (void *)load_wifi_uci_add_iface, &itr);
238
239 return itr.list;
240 }
241 }
242
243 return ifs;
244 }
245
246 /* Daemon implementation */
247 static int do_daemon(void)
248 {
249 int iwfd;
250 int channel;
251 char bssid[18];
252
253 wifi_tuple_t *ifs = NULL, *curif;
254 time_t modtime = 0;
255
256 int restart_wifi = 0;
257 int restart_cron = 0;
258
259 openlog(SYSLOG_IDENT, 0, LOG_DAEMON);
260 //daemon(1, 1);
261
262 if( (iwfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 )
263 {
264 perror("Can not open wireless control socket");
265 return 1;
266 }
267
268 while( 1 )
269 {
270 if( (ifs = load_wifi_uci(ifs, &modtime)) == NULL )
271 {
272 printf("Can not load wireless uci. File corrupt?\n");
273 return 1;
274 }
275
276 /* Check crond */
277 if( find_process("crond") < 0 )
278 {
279 syslog(LOG_WARNING, "The crond process died, restarting");
280 restart_cron++;
281 }
282
283 /* Check wireless interfaces */
284 for( curif = ifs; curif; curif = curif->next )
285 {
286 /* Get current channel and bssid */
287 if( (iw_get_bssid(iwfd, curif->ifname, bssid) == 0) &&
288 (iw_get_channel(iwfd, curif->ifname, &channel) == 0) )
289 {
290 /* Check BSSID */
291 if( strcasecmp(bssid, curif->bssid) != 0 )
292 {
293 syslog(LOG_WARNING, "BSSID mismatch on %s: current=%s wanted=%s",
294 curif->ifname, bssid, curif->bssid);
295
296 restart_wifi++;
297 }
298
299 /* Check channel */
300 if( channel != curif->channel )
301 {
302 syslog(LOG_WARNING, "Channel mismatch on %s: current=%d wanted=%d",
303 curif->ifname, channel, curif->channel);
304
305 restart_wifi++;
306 }
307 }
308 else
309 {
310 syslog(LOG_WARNING, "Requested interface %s not present", curif->ifname);
311 }
312 }
313
314
315 /* Wifi restart required? */
316 if( restart_wifi > 0 )
317 {
318 restart_wifi = 0;
319 EXEC(WIFI_ACTION);
320 }
321
322 /* Cron restart required? */
323 if( restart_cron > 0 )
324 {
325 restart_cron = 0;
326 EXEC(CRON_ACTION);
327 }
328
329 sleep(INTERVAL);
330 }
331
332 closelog();
333 return 0;
334 }
335
336
337 int main(int argc, char *argv[])
338 {
339 /* Check if watchdog is running ... */
340 if( (argc > 1) && (strcmp(argv[1], "running") == 0) )
341 {
342 return (find_process(BINARY) >= 0);
343 }
344
345 /* Start daemon */
346 return do_daemon();
347 }