-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzombiebox.c
More file actions
77 lines (63 loc) · 1.46 KB
/
zombiebox.c
File metadata and controls
77 lines (63 loc) · 1.46 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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#define MAX_PROCESSES 16
#define MAIN_LOOP_SLEEP 2
#define PROCESS_SLEEP 10
#define SUBPROCESS_SLEEP 15
#define NAME_SIZE 32
/* set process name */
void setname(char *argv[], int argl, const char *name) {
prctl(PR_SET_NAME, name);
memset(argv[0], 0, argl + 1);
strncpy(argv[0], name, argl);
}
int main(int argc, char *argv[]) {
pid_t children[MAX_PROCESSES] = {};
char name[NAME_SIZE];
/* exit on signal 15 */
signal(SIGTERM, exit);
/* maximum length of process name */
int argl = strlen(argv[0]);
setname(argv, argl, "main");
/* main loop */
for (int n = 0;; n++) {
pid_t pid = fork();
if (!pid) {
/* child */
if (fork()) {
snprintf(name, NAME_SIZE, "process(%d)", n);
setname(argv, argl, name);
sleep(PROCESS_SLEEP);
return EXIT_SUCCESS;
}
/* grandchild */
else {
snprintf(name, NAME_SIZE, "subprocess(%d)", n);
setname(argv, argl, name);
sleep(SUBPROCESS_SLEEP);
return EXIT_SUCCESS;
}
}
/* Add child to process list */
for (int i = 0; i < MAX_PROCESSES; i++) {
if (!children[i]) {
children[i] = pid;
break;
}
}
sleep(MAIN_LOOP_SLEEP);
/* check process list for finished processes */
for (int i = 0; i < MAX_PROCESSES; i++) {
if (children[i]) {
if (waitpid(children[i], NULL, WNOHANG)) {
children[i] = 0;
}
}
}
}
}