forked from 5ec1cff/TrickyStore
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsupervisor.cpp
More file actions
66 lines (54 loc) · 1.49 KB
/
supervisor.cpp
File metadata and controls
66 lines (54 loc) · 1.49 KB
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
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <android/log.h>
#include <sys/resource.h>
#define LOG_TAG "HALSupervisor"
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
// HAL WATCHDOG DAEMON
// Fork-Based Supervisor Daemon
void start_daemon() {
// Daemon logic here
ALOGI("Daemon started");
while(1) {
sleep(10);
}
}
int main() {
// Run at nice=10
setpriority(PRIO_PROCESS, 0, 10);
int backoff_ms = 500;
const int max_backoff_ms = 30000; // 30s
while (1) {
pid_t pid = fork();
if (pid < 0) {
ALOGE("Fork failed");
sleep(1);
continue;
}
if (pid == 0) {
// Child process
start_daemon();
exit(1);
} else {
// Parent supervisor
int status;
waitpid(pid, &status, 0);
ALOGE("Daemon crashed or exited. Restarting with backoff %d ms", backoff_ms);
struct timespec req;
req.tv_sec = backoff_ms / 1000;
req.tv_nsec = (backoff_ms % 1000) * 1000000L;
nanosleep(&req, NULL);
// Exponential backoff
backoff_ms *= 2;
if (backoff_ms > max_backoff_ms) {
backoff_ms = max_backoff_ms;
}
}
}
return 0;
}