forked from kernelslacker/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trinity.c
186 lines (135 loc) · 3.56 KB
/
trinity.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
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "arch.h"
#include "config.h" // for VERSION
#include "fd.h"
#include "files.h"
#include "ioctls.h"
#include "log.h"
#include "maps.h"
#include "pids.h"
#include "params.h"
#include "domains.h"
#include "random.h"
#include "signals.h"
#include "shm.h"
#include "tables.h"
#include "taint.h"
#include "trinity.h"
#include "uid.h"
#include "version.h"
char *progname = NULL;
unsigned int page_size;
unsigned int num_online_cpus;
unsigned int max_children;
/*
* just in case we're not using the test.sh harness, we
* change to the tmp dir if it exists.
*/
static void change_tmp_dir(void)
{
struct stat sb;
const char tmpdir[]="tmp/";
int ret;
/* Check if it exists, bail early if it doesn't */
ret = (lstat(tmpdir, &sb));
if (ret == -1)
return;
/* Just in case a previous run screwed the perms. */
ret = chmod(tmpdir, 0777);
if (ret == -1)
output(0, "Couldn't chmod %s to 0777.\n", tmpdir);
ret = chdir(tmpdir);
if (ret == -1)
output(0, "Couldn't change to %s\n", tmpdir);
}
int main(int argc, char* argv[])
{
int ret = EXIT_SUCCESS;
int childstatus;
pid_t pid;
const char taskname[13]="trinity-main";
outputstd("Trinity " VERSION " Dave Jones <davej@codemonkey.org.uk>\n");
progname = argv[0];
initpid = getpid();
page_size = getpagesize();
num_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
max_children = num_online_cpus; /* possibly overridden in params. */
set_seed(0);
select_syscall_tables();
create_shm();
/* We do this before the parse_args because --fds will need to
* operate on it when implemented.
*/
setup_fd_providers();
parse_args(argc, argv);
init_uids();
change_tmp_dir();
if (logging == TRUE)
open_main_logfile();
init_shm();
kernel_taint_initial = check_tainted();
if (kernel_taint_initial != 0)
output(0, "Kernel was tainted on startup. Will ignore flags that are already set.\n");
if (munge_tables() == FALSE) {
ret = EXIT_FAILURE;
goto out;
}
if (show_syscall_list == TRUE) {
dump_syscall_tables();
goto out;
}
init_syscalls();
if (show_ioctl_list == TRUE) {
dump_ioctls();
goto out;
}
do_uid0_check();
if (do_specific_domain == TRUE)
find_specific_domain(specific_domain_optarg);
setup_initial_mappings();
parse_devices();
pids_init();
setup_main_signals();
/* check if we ctrl'c or something went wrong during init. */
if (shm->exit_reason != STILL_RUNNING)
goto cleanup_fds;
init_watchdog();
/* do an extra fork so that the watchdog and the children don't share a common parent */
fflush(stdout);
pid = fork();
if (pid == 0) {
shm->mainpid = getpid();
setup_main_signals();
output(0, "Main thread is alive.\n");
prctl(PR_SET_NAME, (unsigned long) &taskname);
set_seed(0);
if (open_fds() == FALSE) {
if (shm->exit_reason != STILL_RUNNING)
panic(EXIT_FD_INIT_FAILURE); // FIXME: Later, push this down to multiple EXIT's.
exit_main_fail();
}
if (dropprivs == TRUE) //FIXME: Push down into child processes later.
drop_privs();
main_loop();
shm->mainpid = 0;
_exit(EXIT_SUCCESS);
}
/* wait for main loop process to exit. */
(void)waitpid(pid, &childstatus, 0);
/* wait for watchdog to exit. */
waitpid(watchdog_pid, &childstatus, 0);
output(0, "Ran %ld syscalls. Successes: %ld Failures: %ld\n",
shm->stats.total_syscalls_done - 1, shm->stats.successes, shm->stats.failures);
cleanup_fds:
close_sockets();
destroy_initial_mappings();
if (logging == TRUE)
close_logfile(&mainlogfile);
out:
exit(ret);
}