-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssp.c
335 lines (287 loc) · 9.39 KB
/
ssp.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>
#define INITIAL_SIZE 10
struct ssp_process {
int status;
int ssp_pid;
pid_t pid;
char *name;
};
struct ssp_process *ssp_processes = NULL;
int ssp_process_count = 0;
int maxSize = INITIAL_SIZE;
struct ssp_process *ssp_orphans = NULL;
int ssp_orphans_count = 0;
int maxSizeOrphans = INITIAL_SIZE;
void handle_signal(int signum) {
if (signum == SIGCHLD) {
int status;
pid_t result;
// Loop to handle multiple children terminating at the same time
while ((result = waitpid(-1, &status, WNOHANG)) > 0) {
// Check if the process is already tracked
bool found = false;
for (int i = 0; i < ssp_process_count; i++) {
if (ssp_processes[i].pid == result) {
found = true;
// Update the status for tracked processes
if (WIFEXITED(status)) {
ssp_processes[i].status = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
ssp_processes[i].status = WTERMSIG(status) + 128;
}
break;
}
}
// If the process is not found, it's an orphan
if (!found) {
// Check if the orphan list is full
if (ssp_orphans_count == maxSizeOrphans) {
maxSizeOrphans *= 2;
ssp_orphans = realloc(ssp_orphans, maxSizeOrphans * sizeof(struct ssp_process));
if (ssp_orphans == NULL) {
int err = errno;
perror("realloc failed");
exit(err);
}
}
// Add the orphan process to the list with name "<unknown>"
ssp_orphans[ssp_orphans_count].pid = result;
ssp_orphans[ssp_orphans_count].ssp_pid = ssp_orphans_count;
ssp_orphans[ssp_orphans_count].name = strdup("<unknown>");
if (WIFEXITED(status)) {
ssp_orphans[ssp_orphans_count].status = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
ssp_orphans[ssp_orphans_count].status = WTERMSIG(status) + 128;
}
ssp_orphans_count++;
}
}
// Handle waitpid failure
if (result == -1 && errno != ECHILD) {
int err = errno;
perror("waitpid failed");
exit(err);
}
}
}
void register_signal(int signum) {
struct sigaction new_action = {0};
sigemptyset(&new_action.sa_mask);
new_action.sa_handler = handle_signal;
new_action.sa_flags = SA_RESTART | SA_NOCLDSTOP; // Restart syscalls and ignore stopped child processes
if (sigaction(signum, &new_action, NULL) == -1) {
int err = errno;
perror("sigaction failed");
exit(err);
}
}
void ssp_init() {
// Allocate memory for the process list
ssp_processes = malloc(maxSize * sizeof(struct ssp_process));
if (ssp_processes == NULL) {
int err = errno;
perror("malloc failed");
exit(err);
}
// Allocate memory for the orphan list
ssp_orphans = malloc(maxSizeOrphans * sizeof(struct ssp_process));
if (ssp_orphans == NULL) {
int err = errno;
perror("malloc failed");
exit(err);
}
if (prctl(PR_SET_CHILD_SUBREAPER, 1) == -1) {
int err = errno;
perror("prctl failed");
exit(err);
}
ssp_process_count = 0;
ssp_orphans_count = 0;
// Register the SIGCHLD signal handler
register_signal(SIGCHLD);
}
int ssp_create(char *const *argv, int fd0, int fd1, int fd2) {
// Check if the process list is full
if (ssp_process_count == maxSize) {
maxSize *= 2;
ssp_processes = realloc(ssp_processes, maxSize * sizeof(struct ssp_process));
if (ssp_processes == NULL) {
int err = errno;
perror("realloc failed");
exit(err);
}
}
// Create a new process
pid_t pid = fork();
// If fork fails
if (pid == -1) {
int err = errno;
perror("fork failed");
exit(err);
}
// If fork succeeds
// In child process
if (pid == 0) {
// Redirect stdin, stdout, stderr
if (dup2(fd0, 0) == -1 || dup2(fd1, 1) == -1 || dup2(fd2, 2) == -1) {
int err = errno;
perror("dup2 failed");
exit(err);
}
// Open /proc/self/fd
DIR *dir = opendir("/proc/self/fd");
if (dir == NULL) {
int err = errno;
perror("opendir failed");
exit(err);
}
// Close all file descriptors except stdin, stdout, stderr
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_LNK) {
int fd = atoi(entry->d_name);
if (fd > 2 && fd != dirfd(dir)) {
close(fd);
}
}
}
// Close /proc/self/fd
if (closedir(dir) == -1) {
int err = errno;
perror("closedir failed");
exit(err);
}
// Execute the program
execvp(argv[0], argv);
// If execvp fails
int err = errno;
perror("execvp failed");
exit(err);
}
// In parent process
// Add the process to the list
ssp_processes[ssp_process_count].status = -1; // set the status to running
ssp_processes[ssp_process_count].pid = pid; // copy the pid
ssp_processes[ssp_process_count].ssp_pid = ssp_process_count; // set the ssp_pid
ssp_processes[ssp_process_count].name = strdup(argv[0]); // copy the string
ssp_process_count++;
return ssp_process_count - 1;
}
/*Returns -2 if the ssp_id is not valid*/
int ssp_get_status(int ssp_id) {
// Check if the ssp_id is valid
if (ssp_id < 0 || ssp_id >= ssp_process_count) {
return -2;
}
struct ssp_process *proc = &ssp_processes[ssp_id];
// Check if the status is already known
if (proc->status != -1) {
return proc->status;
}
// Check if the procces has terminated
int status;
pid_t result = waitpid(proc->pid, &status, WNOHANG);
if (result == 0) {
// Process is still running
return -1;
} else if (result == -1) {
// Error in waitpid
int err = errno;
perror("waitpid failed");
exit(err);
}
// Process has terminated
if (WIFEXITED(status)) {
proc->status = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
proc->status = WTERMSIG(status) + 128;
}
return proc->status;
}
void ssp_send_signal(int ssp_id, int signum) {
// Check if the ssp_id is valid
if (ssp_id < 0 || ssp_id >= ssp_process_count) {
return;
}
struct ssp_process *proc = &ssp_processes[ssp_id];
// Check if the process is still running
if(proc->status != -1) {
return;
}
// Send the signal
if(kill(proc->pid, signum) == -1) {
// If the process has already terminated
if(errno == ESRCH) {
return;
} else {
// If kill fails
int err = errno;
perror("kill failed");
exit(err);
}
}
// Otherwise the signal has been sent
return;
}
void ssp_wait() {
for(int i = 0; i < ssp_process_count; i++) {
struct ssp_process *proc = &ssp_processes[i];
// Check if the process is still running
if(proc->status == -1) {
int status;
pid_t result = waitpid(proc->pid, &status, 0);
// If there's an error in waitpid
if (result == -1) {
int err = errno;
perror("waitpid failed");
exit(err);
}
// Process has terminated
if (WIFEXITED(status)) {
proc->status = WEXITSTATUS(status); // exit code if process exited normally
} else if (WIFSIGNALED(status)) {
proc->status = WTERMSIG(status) + 128; // signal number if process was terminated by a signal
}
}
}
}
void ssp_print() {
// Find the longest name
int max_len = strlen("CMD");
for (int i = 0; i < ssp_process_count; i++) {
int len = strlen(ssp_processes[i].name);
if (len > max_len) {
max_len = len;
}
}
for(int i = 0; i < ssp_orphans_count; i++) {
int len = strlen(ssp_orphans[i].name);
if (len > max_len) {
max_len = len;
}
}
// Print the header
printf("%7s %-*s %s\n", "PID", max_len, "CMD", "STATUS");
// Print the processes
for (int i = 0; i < ssp_process_count; i++) {
struct ssp_process *proc = &ssp_processes[i];
printf("%7d %-*s %d\n", proc->pid, max_len, proc->name, proc->status);
}
// Print the orphans
for (int i = 0; i < ssp_orphans_count; i++) {
struct ssp_process *proc = &ssp_orphans[i];
printf("%7d %-*s %d\n", proc->pid, max_len, proc->name, proc->status);
}
}