summaryrefslogtreecommitdiff
path: root/watchdog.c
blob: 39ae9ff9aa5823ab05c9b1af3e650a11e93cf7d3 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * 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.
 */

#include <linux/watchdog.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <unistd.h>

#include <libubox/uloop.h>

#include "procd.h"
#include "watchdog.h"

#define WDT_PATH	"/dev/watchdog"

static struct uloop_timeout wdt_timeout;
static int wdt_fd = -1;
static int wdt_drv_timeout = 30;
static int wdt_frequency = 5;
static bool wdt_magicclose = false;

void watchdog_ping(void)
{
	DEBUG(4, "Ping\n");
	if (wdt_fd >= 0 && write(wdt_fd, "X", 1) < 0)
		ERROR("WDT failed to write: %m\n");
}

static void watchdog_timeout_cb(struct uloop_timeout *t)
{
	watchdog_ping();
	uloop_timeout_set(t, wdt_frequency * 1000);
}

static int watchdog_open(bool cloexec)
{
	char *env = getenv("WDTFD");

	if (wdt_fd >= 0)
		return wdt_fd;

	if (env) {
		DEBUG(2, "Watchdog handover: fd=%s\n", env);
		wdt_fd = atoi(env);
		unsetenv("WDTFD");
	} else {
		wdt_fd = open(WDT_PATH, O_WRONLY);
	}

	if (wdt_fd < 0)
		return wdt_fd;

	if (cloexec)
		fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC);

	return wdt_fd;
}

static void watchdog_close(void)
{
	if (wdt_fd < 0)
		return;

	if (write(wdt_fd, "V", 1) < 0)
		ERROR("WDT failed to write release: %m\n");

	if (close(wdt_fd) == -1)
		ERROR("WDT failed to close watchdog: %m\n");

	wdt_fd = -1;
}

static int watchdog_set_drv_timeout(void)
{
	if (wdt_fd < 0)
		return -1;

	return ioctl(wdt_fd, WDIOC_SETTIMEOUT, &wdt_drv_timeout);
}

static void watchdog_print_status(void)
{
	struct watchdog_info wdt_info;
	int bootstatus;

	if (wdt_fd < 0)
		return;

	if (ioctl(wdt_fd, WDIOC_GETSUPPORT, &wdt_info)) {
		DEBUG(2, "Watchdog GETSUPPORT failed\n");
		return;
	}

	if (!(wdt_info.options & WDIOF_CARDRESET)) {
		DEBUG(2, "Watchdog does not have CARDRESET support\n");
		return;
	}

	if (ioctl(wdt_fd, WDIOC_GETBOOTSTATUS, &bootstatus)) {
		DEBUG(2, "Watchdog GETBOOTSTATUS failed\n");
		return;
	}

	if (bootstatus & WDIOF_CARDRESET)
		LOG("Watchdog has previously reset the system\n");
	else
		DEBUG(2, "Watchdog did not previously reset the system\n");
}

void watchdog_set_magicclose(bool val)
{
	wdt_magicclose = val;
}

bool watchdog_get_magicclose(void)
{
	return wdt_magicclose;
}

void watchdog_set_stopped(bool val)
{
	if (val) {
		uloop_timeout_cancel(&wdt_timeout);

		if (wdt_magicclose)
			watchdog_close();
	}
	else {
		watchdog_open(true);
		watchdog_set_drv_timeout();
		watchdog_timeout_cb(&wdt_timeout);
	}
}

bool watchdog_get_stopped(void)
{
	return !wdt_timeout.pending;
}

int watchdog_timeout(int timeout)
{
	if (timeout) {
		DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
		wdt_drv_timeout = timeout;

		if (wdt_fd >= 0)
			watchdog_set_drv_timeout();
	}

	return wdt_drv_timeout;
}

int watchdog_frequency(int frequency)
{
	if (frequency) {
		DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
		wdt_frequency = frequency;
	}

	return wdt_frequency;
}

char* watchdog_fd(void)
{
	static char fd_buf[12];

	if (wdt_fd < 0)
		return NULL;

	snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);

	return fd_buf;
}

void watchdog_init(int preinit)
{
	wdt_timeout.cb = watchdog_timeout_cb;

	if (watchdog_open(!preinit) < 0)
		return;

	LOG("- watchdog -\n");
	watchdog_set_drv_timeout();
	watchdog_timeout_cb(&wdt_timeout);

	DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));

	watchdog_print_status();
}


void watchdog_set_cloexec(bool val)
{
	if (wdt_fd < 0)
		return;

	int flags = fcntl(wdt_fd, F_GETFD);
	if (val)
		flags |= FD_CLOEXEC;
	else
		flags &= ~FD_CLOEXEC;
	fcntl(wdt_fd, F_SETFD,  flags);
}