-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmonitor.cpp
More file actions
214 lines (181 loc) · 6.54 KB
/
Copy pathmonitor.cpp
File metadata and controls
214 lines (181 loc) · 6.54 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
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
#include "monitor.h"
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
Monitor::Monitor() {
p_status = new Status();
pid = 0;
p_args = NULL;
p_full = NULL;
}
// ----------------------------------------------------------------------------
// Get the command name from the first argument. The result will be used as
// the second argument for exec...()
//
// Returns:
// - Pointer to the original string if no "/" character found in string
// - NULL if the original string was NULL or was shorter than 1 byte
//
// Otherwise, returns the command name extracted from the original string.
//
// ----------------------------------------------------------------------------
char *Monitor::getCommandName(char *str) {
if (str == NULL || strlen(str) < 1) return(NULL);
if (strchr(str, 0x2F) == NULL) return(str);
char *temp = NULL;
char *t = strtok(str, "/");
if (t != NULL) temp = t;
while(t) {
t = strtok(NULL, "/");
if (t != NULL) temp = t;
}
return(temp);
}
// ----------------------------------------------------------------------------
// Parse a command line where arguments are separated by space. After parsing
// an array of string pointers is returned where each item in the array points
// to an argument string. The last item in the array is always NULL.
//
// Returns:
//
// - NULL if the original string is NULL or the length of the string is less
// than 1.
//
// Otherwise, it returns an array of string pointers.
//
// ----------------------------------------------------------------------------
char **Monitor::parseArgs(char *str) {
if (str == NULL || strlen(str) < 1) return(NULL);
char **res = NULL;
int n_spaces = 0;
int i = 0;
char *p = strtok(str, " ");
while(p) {
res = (char **)realloc(res, sizeof(char*) * ++n_spaces);
if (res == NULL) exit(-1);
res[n_spaces-1] = p;
p = strtok(NULL, " ");
}
res = (char **)realloc(res, sizeof(char*) * (n_spaces+1));
res[n_spaces] = 0;
return(res);
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
int Monitor::setTarget(char *cmd_line) {
if (p_args != NULL) free(p_args);
p_full = NULL;
p_args = Monitor::parseArgs((char *)cmd_line);
if (p_args == NULL) return(1);
if (p_args != NULL && p_args[0] != NULL) {
p_full = (char *)malloc(strlen((char *)p_args[0]) + 1);
memset(p_full, 0x00, strlen((char *)p_args[0]) + 1);
strcpy(p_full, p_args[0]);
p_args[0] = getCommandName(p_args[0]);
}
if (p_args[0] == NULL) return(1);
return(0);
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
int Monitor::terminate() {
if (pid < 1) return 1;
int rc = 0;
int error;
if (kill(pid, SIGKILL) == -1) {
error = errno;
if (error == EINVAL || error == EPERM) rc = 0;
if (error == ESRCH) rc = 1; // Even if it is considered as
// an error that the process
// does not exist, this is how
// we report that we got rid
// of it anyway. So, this is
// good for us.
} else {
rc = 1;
}
p_status->reset();
return(rc);
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
Status *Monitor::status() {
return p_status;
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void Monitor::stop() {
running = 0;
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
int Monitor::isRunning() {
return(running);
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
struct user_regs_struct Monitor::getRegisters() {
return regs;
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
int Monitor::start() {
running = 1;
int status = 0;
pid_t child = 0;
p_status->reset();
syslog(LOG_INFO, "starting process: %s (%s)", p_full, p_args[0]);
child = fork();
if(child == 0) {
if (p_full != NULL && p_args != NULL &&
p_args[0] != NULL) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
p_status->setState(P_RUNNING);
execv(p_full, p_args);
p_status->setPid(-1);
p_status->setState(P_ERROR);
syslog(LOG_ERR, "failed to start process: %d", errno);
} else {
p_status->setPid(-1);
p_status->setState(P_ERROR);
}
} else {
wait(NULL);
ptrace(PTRACE_CONT, child, NULL, NULL);
syslog(LOG_INFO, "process started with pid: %d", child);
p_status->setPid(child);
while(running) {
wait(&status);
// ptrace(PTRACE_GETREGS, child, NULL, ®s);
if (WIFEXITED(status)) {
p_status->setState(P_TERM);
p_status->setExitCode(WEXITSTATUS(status));
syslog(LOG_INFO, "process exited with exit code: %d",
WEXITSTATUS(status));
break;
}
if (WIFSIGNALED(status)) {
p_status->setState(P_SIGTERM);
p_status->setSignal(WTERMSIG(status));
syslog(LOG_INFO, "process terminated by signal: %d",
WTERMSIG(status));
break;
}
if (WIFSTOPPED(status)) {
p_status->setState(P_TERM);
p_status->setSignal(WSTOPSIG(status));
syslog(LOG_INFO, "process stopped by signal: %d",
WSTOPSIG(status));
break;
}
}
syslog(LOG_INFO, "monitor for %s stopped", p_full);
}
}