summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Huwald <jh@sotun.de>2011-07-10 15:08:09 (GMT)
committerJan Huwald <jh@sotun.de>2011-07-10 15:08:09 (GMT)
commitc4d129be9ced486e4938d6bcf945b89ee4978e5a (patch)
tree4428e9109592013d122caca67ead8f9c9300ac34
Initial commitHEADmaster
-rw-r--r--Makefile6
-rw-r--r--keepalive.c44
2 files changed, 50 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7217860
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
+keepalive: keepalive.c
+ $(CC) $< -O2 -fwhole-program -static -o $@
+
+clean:
+ -rm keepalive *~
+
diff --git a/keepalive.c b/keepalive.c
new file mode 100644
index 0000000..380be52
--- /dev/null
+++ b/keepalive.c
@@ -0,0 +1,44 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <time.h>
+
+const useconds_t
+ minSleepTime = 1,
+ maxSleepTime = 1000000;
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ fprintf(stderr, "supply a program name\n");
+ return -1;
+ }
+
+ useconds_t sleepTime = minSleepTime;
+ time_t td = time(NULL);
+ while (1) {
+ pid_t pid = fork();
+ switch (pid) {
+ case 0: // child
+ execvp(argv[1], &(argv[1]));
+ return -1; // exec failed, stop
+ case -1: // fork failed, assume temporary failure
+ break;
+ default: // parent
+ wait(NULL);
+ }
+
+ fprintf(stderr, "child exit, restarting\n");
+
+ // apply current and calc new delay
+ td = time(NULL) - td;
+ if (td > 2) {
+ sleepTime = minSleepTime;
+ }
+ usleep(sleepTime);
+ if (td <= 2) {
+ sleepTime *= 2;
+ if (sleepTime > maxSleepTime)
+ sleepTime = maxSleepTime;
+ }
+ td = time(NULL);
+ }
+}
contact: Jan Huwald // Impressum