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