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