-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulture.c
106 lines (91 loc) · 2.35 KB
/
vulture.c
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
/*
* Agent for attempting to avoid detection from
* hunter by bisecting its processing when signaled
* and splitting to a child process while killing the
* parent
*/
#define _POSIX_SOURCE
#include "vmonitor.h"
#include "task.h"
#include "vulture.h"
#include "vtime.h"
static unsigned int FORK_FLAG = 0;
static pid_t VMONITOR_PID;
void vulture_handler(int sig)
{
if (sig == SIGINT)
{
FORK_FLAG = 1;
}
else if (sig == SIGTERM)
{
kill(VMONITOR_PID, SIGTERM);
_exit(0);
}
}
void write_pid(char* pidfile, pid_t pid_val)
{
FILE* vulture_pid = fopen(pidfile, "w");
if (vulture_pid != NULL)
{
fprintf(vulture_pid, "%d", (int)pid_val);
fclose(vulture_pid);
}
}
void circle(FILE* vulture_log, char* vulture_pid, int vsleep)
{
while(1)
{
long hash_val = hash();
fprintf(vulture_log, "%ld %ld\n", hash_val, (long)getpid());
msleep(vsleep);
if (FORK_FLAG == 1)
{
FORK_FLAG = 0;
pid_t fresh_proc;
switch (fork())
{
case -1:
fprintf(vulture_log, "Fork failure\n");
return;
case 0:
fresh_proc = getpid();
write_pid(vulture_pid, fresh_proc);
break;
default:
return;
}
}
fflush(vulture_log);
}
fclose(vulture_log);
}
void vulture(const int vsleep, const char* vulture_log_path)
{
FILE* vulture_log = fopen(vulture_log_path, "w");
const char* monitor_proc = "vmonitor";
char* vulture_pid = "./pid/vulture.pid";
struct sigaction sa;
sa.sa_flags = 0;
sa.sa_handler = vulture_handler;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
switch (VMONITOR_PID = fork())
{
case -1:
fprintf(vulture_log, "Fork failure\n");
return;
case 0:
if (prctl(PR_SET_NAME, (unsigned long) monitor_proc) < 0)
{
fprintf(vulture_log, "Unable to set child name to %s", monitor_proc);
return;
}
vwatch(vulture_pid);
break;
default:
write_pid(vulture_pid, getpid());
circle(vulture_log, vulture_pid, vsleep);
break;
}
}