-
Notifications
You must be signed in to change notification settings - Fork 2
/
keepalive.cpp
73 lines (52 loc) · 1.49 KB
/
keepalive.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
#include <string>
#include <stdlib.h>
#include <rtx/signal.h>
#include <rtx/error.h>
#include <rtx/message.h>
#include <rtx/main.h>
#include <rtx/thread.h>
#include <rtx/time.h>
static std::string command;
void * working_thread(void *)
{
while (1) {
int r;
r = system(command.c_str());
sleep(1);
}
return NULL;
}
// Error handling for C functions (return 0 on success)
#define DOC(c) {int ret = c;if (ret != 0) {rtx_error("Command "#c" failed with value %d",ret);return -1;}}
// Error handling for C++ function (return true on success)
#define DOB(c) if (!(c)) {rtx_error("Command "#c" failed");return -1;}
// Error handling for pointer-returning function (return NULL on failure)
#define DOP(c) if ((c)==NULL) {rtx_error("Command "#c" failed");return -1;}
int main(int argc,char *argv[])
{
RtxThread * th = NULL;
int i;
for (i=1;i<argc-1;i++) {
command+=argv[i];
command+=" ";
}
command += argv[i];
DOC(rtx_main_init("keepalive",0));
rtx_signal_block_realtime();
// Start the working thread
DOP(th = rtx_thread_create ("thread", 0,
RTX_THREAD_SCHED_OTHER, RTX_THREAD_PRIO_MIN, 0,
RTX_THREAD_CANCEL_DEFERRED,
working_thread, NULL,
NULL, NULL));
// Wait for Ctrl-C
DOC (rtx_main_wait_shutdown (0));
rtx_message ("Caught SIGINT/SIGQUIT, exiting ...");
// Terminating the thread
rtx_thread_destroy_sync (th);
// std::string killit("killall");
// system((killit + " " + argv[1]).c_str());
// sleep(1);
// system((killit + " -9 " + argv[1]).c_str());
return 0;
}