summaryrefslogtreecommitdiffhomepage
path: root/contrib/package/freifunk-watchdog/src/watchdog.h
blob: 71c10e9c1fcbbffb33950417fac8de0b5a315a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <syslog.h>
#include <ctype.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/watchdog.h>

#include "ucix.h"
#include "wireless.22.h"


/* Watchdog poll interval */
#define BASE_INTERVAL	5

/* Action interval (N * BASE_INTERVAL) */
#define ACTION_INTERVAL	6

/* Hysteresis */
#define HYSTERESIS		3

/* How to call myself in the logs */
#define SYSLOG_IDENT	"Freifunk Watchdog"

/* Wifi error action */
#define WIFI_ACTION		"/sbin/wifi", "/sbin/wifi"

/* Crond error action */
#define CRON_ACTION		"/etc/init.d/cron", "/etc/init.d/cron", "restart"

/* SSHd error action */
#define SSHD_ACTION		"/etc/init.d/dropbear", "/etc/init.d/dropbear", "restart"

/* Watchdog device */
#define WATCH_DEVICE	"/dev/watchdog"
#define WATCH_SHUTDOWN	'V'
#define WATCH_KEEPALIVE	'\0'

/* System load error action and treshold */
#define LOAD_TRESHOLD	15.00
#define LOAD_ACTION		"/sbin/reboot", "/sbin/reboot"

/* Fallback binary name (passed by makefile) */
#ifndef BINARY
#define BINARY "ffwatchd"
#endif


/* ifname/bssid/channel tuples */
struct wifi_tuple {
	char ifname[16];
	char bssid[18];
	int channel;
	struct wifi_tuple *next;
};

/* structure to hold tuple-list and uci context during iteration */
struct uci_itr_ctx {
	struct wifi_tuple *list;
	struct uci_context *ctx;
};

typedef struct wifi_tuple wifi_tuple_t;


/* ioctl() helper (stolen from iwlib) */
static inline int
iw_ioctl(int                  skfd,           /* Socket to the kernel */
         const char *         ifname,         /* Device name */
         int                  request,        /* WE ID */
         struct iwreq *       pwrq)           /* Fixed part of the request */
{
  /* Set device name */
  strncpy(pwrq->ifr_ifrn.ifrn_name, ifname, 16);

  /* Do the request */
  return(ioctl(skfd, request, pwrq));
}

/* fork() & execl() helper */
#define EXEC(x)														\
	do {															\
		switch(fork())												\
		{															\
			case -1:												\
				syslog(LOG_CRIT, "Unable to fork child: %s",		\
					strerror(errno));								\
				break;												\
																	\
			case 0:													\
				execl(x, NULL);										\
				syslog(LOG_CRIT, "Unable to execute action: %s",	\
					strerror(errno));								\
				return 1;											\
		}															\
	} while(0)