-
Notifications
You must be signed in to change notification settings - Fork 5
/
Watchdog.cpp
225 lines (190 loc) · 5.02 KB
/
Watchdog.cpp
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
219
220
221
222
223
224
225
//
// Created by ebassetti on 16/09/15.
//
#ifndef NOWATCHDOG
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <dirent.h>
#include <string.h>
#include <vendor_specific.h>
#include "Watchdog.h"
#include "Utils.h"
#include "Log.h"
#include "generic.h"
#include "Config.h"
unsigned long Watchdog::lastBeat = 0;
void Watchdog::launch() {
pid_t pid, sid, parentpid = getpid();
int fd;
// already a daemon
if ( getppid() == 1 ) return;
// Fork off the parent process
pid = fork();
if (pid < 0) {
platformReboot();
}
if (pid > 0) {
// Parent process can proceed
signal(SIGCHLD, SIG_IGN);
heartBeat();
return;
}
// At this point we are executing as the intermediate parent process
// Create a new SID for the intermediate parent process
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
signal(SIGCHLD, SIG_IGN);
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
// Intermediate parent can be killed
exit(EXIT_SUCCESS);
}
Log::setLogFile(WATCHDOG_LOG_PATH);
Log::setLogLevel(LEVEL_DEBUG);
Log::d("Watchdog PID is %i", getpid());
// Change the current working directory.
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
fd = open("/dev/null",O_RDWR, 0);
if (fd != -1) {
dup2 (fd, STDIN_FILENO);
dup2 (fd, STDOUT_FILENO);
dup2 (fd, STDERR_FILENO);
if (fd > 2) {
close (fd);
}
}
// resettign File Creation Mask
umask(027);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while(true) {
sleep(15);
bool sketchPidRunning = Watchdog::checkSketchPid(parentpid);
struct stat fileinfo;
bool heartbeat = true;
int rp = stat(WATCHDOG_FILE, &fileinfo);
if(rp == 0) {
#ifdef __linux__
if(fileinfo.st_mtim.tv_sec < time(NULL) - WATCHDOG_TIMER/1000) {
heartbeat = false;
}
#else
#if defined(OPENBSD) || defined(FREEBSD) || defined(__APPLE__) || defined(__darwin__)
if (fileinfo.st_mtimespec.tv_sec < time(NULL) - 15) {
heartbeat = false;
}
#else
#error No definition for file modify time (watchdog)
#endif
#endif
} else {
heartbeat = true;
}
if (sketchPidRunning == 0 || !heartbeat) {
std::string reason = "";
if(!heartbeat) {
reason.append("Heartbeat missed;");
}
if(sketchPidRunning) {
reason.append("Sketch is not running");
}
Log::i("Reboot reason: %s", reason.c_str());
Log::close();
#ifdef DEBUG
storeCrashInfos(reason);
#endif
platformReboot();
}
}
#pragma clang diagnostic pop
}
bool Watchdog::checkSketchPid(pid_t pid) {
char path[1024];
snprintf(path, 1024, "/proc/%i/cmdline", pid);
return Utils::fileExists(path);
}
void Watchdog::heartBeat() {
if(Utils::millis() - lastBeat > WATCHDOG_TIMER/3) {
FILE* fp = fopen(WATCHDOG_FILE, "w");
if(fp == NULL) {
unlink(WATCHDOG_FILE);
platformReboot();
return;
}
char buf[10];
memset(buf, 0xFF, 9);
buf[9] = 0;
fwrite(buf, 1, 10, fp);
fflush(fp);
fclose(fp);
lastBeat = Utils::millis();
}
}
#ifdef DEBUG
void Watchdog::storeCrashInfos(std::string reason) {
reason = "reason:" + reason + "\n";
std::string macstr = "mac:" + Config::getMacAddress() + "\n";
std::string unixtime = "time:" + Utils::toString(time(NULL)) + "\n";
std::string freemem = "freeram:" + Utils::toString(Utils::getFreeRam()) + "\n";
std::string uptime = "uptime:" + Utils::toString(Utils::uptime()) + "\n";
std::string platform = "platform:" + std::string(PLATFORM_TAG) + "\n";
std::string buildversion = "buildversion:" + std::string(BUILD_VERSION) + "\n";
std::string softwareversion = "softwareversion:" + std::string(SOFTWARE_VERSION) + "\n";
std::string path = WATCHDOG_CRASHDIR;
path.append(Utils::toString(time(NULL)));
path.append(".dat");
mkdir(WATCHDOG_CRASHDIR, 0644);
FILE* fp = fopen(path.c_str(), "wb");
if(fp == NULL) {
// Ooops!
return;
}
fwrite(macstr.c_str(), macstr.length(), 1, fp);
fwrite(unixtime.c_str(), unixtime.length(), 1, fp);
fwrite(freemem.c_str(), freemem.length(), 1, fp);
fwrite(uptime.c_str(), uptime.length(), 1, fp);
fwrite(platform.c_str(), platform.length(), 1, fp);
fwrite(buildversion.c_str(), buildversion.length(), 1, fp);
fwrite(softwareversion.c_str(), softwareversion.length(), 1, fp);
fwrite(reason.c_str(), reason.length(), 1, fp);
if(Utils::fileExists(STACKTRACEINFO)) {
fwrite("Crash infos:\n", 13, 1, fp);
char buf[1024];
memset(buf, 0, 1024);
FILE* crashfd = fopen(STACKTRACEINFO, "r");
if(crashfd != NULL) {
fread(buf, 1024, 1, crashfd);
fwrite(buf, strlen(buf), 1, fp);
fclose(crashfd);
}
unlink(STACKTRACEINFO);
}
if(Utils::fileExists("/media/realroot/core")) {
ssize_t fileSize = Utils::fileSize("/media/realroot/core");
fwrite("Core dump:\n", 11, 1, fp);
uint8_t *buf = (uint8_t *)malloc(fileSize);
memset(buf, 0, fileSize);
FILE* corefd = fopen("/media/realroot/core", "rb");
if(corefd != NULL) {
size_t c = fread(buf, 1, fileSize, corefd);
fwrite(buf, 1, c, fp);
fclose(corefd);
}
free(buf);
unlink("/media/realroot/core");
}
fclose(fp);
}
#endif
#endif