-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.c
191 lines (158 loc) · 3.91 KB
/
init.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
/*
* tinyinit - finish kernel setup and get out of the way.
*
* Setup a few directories, mount a few filesystems,
* and then exec the real application.
*
* By writing this as a small C program it avoids the need
* to bring in bash and most of coreutils for smaller
* unikernel images.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <glob.h>
#include <sys/types.h>
#include <sys/wait.h>
// status updates
#include <syslog.h>
#include <sys/sysinfo.h>
static int forkit(const char * filename)
{
int fd = open(filename, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "%s not found; not sure what to do!\n", filename);
return -1;
}
char args[4096];
ssize_t len = read(fd, args, sizeof(args));
//fprintf(stderr, "init: read %zu bytes\n", len);
if (len < 0)
{
perror(filename);
return -1;
}
close(fd);
char *argv[64] = {};
int argc = 0;
argv[argc++] = args;
fprintf(stderr, "%s: execv('%s'", filename, argv[0]);
// stop before the end of the argument data
for(int offset = 0 ; offset < len-1 ; offset++)
{
if (args[offset] != '\0')
continue;
char * arg = &args[offset+1];
argv[argc++] = arg;
fprintf(stderr, ",'%s'", arg);
}
// terminate the list
argv[argc] = NULL;
fprintf(stderr, ")\n");
// invoke it and hope for the best!
int pid = vfork();
if (pid == 0)
{
execv(argv[0], argv);
exit(-1);
}
return 0;
}
// occasionally log some data
static void loginfo(void)
{
while(1)
{
// schedule another wakeup
sleep(60);
struct sysinfo info;
if (sysinfo(&info) < 0)
{
perror("sysinfo");
continue;
}
const float load_scale = 1.0f / (1 << SI_LOAD_SHIFT);
const unsigned long mem_used = info.totalram - info.freeram;
const int mem_percent = (100 * mem_used) / info.totalram;
syslog(mem_percent > 80 ? LOG_ALERT : LOG_DEBUG,
"load %.2f,%.2f,%.2f ram %ld/%ld (%d%%) procs %d",
info.loads[0] * load_scale,
info.loads[1] * load_scale,
info.loads[2] * load_scale,
mem_used,
info.totalram,
mem_percent,
info.procs
);
}
}
int main(void)
{
// these should already exist, but just in case
fprintf(stderr, "init: creating directories\n");
mkdir("/root", 0755);
mkdir("/proc", 0755);
mkdir("/sys", 0755);
mkdir("/tmp", 0755);
mkdir("/dev", 0755);
mkdir("/run", 0755);
mkdir("/var", 0755);
fprintf(stderr, "init: mounting filesystems\n");
mount("none", "/proc", "proc", 0, "");
mount("none", "/dev", "devtmpfs", 0, "");
mount("none", "/sys", "sysfs", 0, "");
mount("none", "/sys/kernel/security", "securityfs", 0, "");
mount("none", "/tmp", "tmpfs", 0, "");
// this breaks things in /var, and we can't mount a separate /var
// since they might have pre-created directories in it.
//fprintf(stderr, "init: remounting root read only\n");
//mount("none", "/", "rootfs", MS_REMOUNT | MS_RDONLY, "");
int fd = open("/dev/console", O_RDWR);
if (fd >= 0)
{
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
}
// if it wasn't one of the stdio fd's, close it now
if (fd > 2)
close(fd);
// set a shell prompt, just in case
setenv("PS1", "\\w# ", 1);
glob_t globbuf;
int rc = glob("/init.d/*", 0, NULL, &globbuf);
const int matches = globbuf.gl_pathc;
if (rc != 0 || matches == 0)
{
fprintf(stderr, "init: no programs to run? we're done here\n");
return -1;
}
fprintf(stderr, "init: %d startup items found\n", matches);
for(int i = 0 ; i < matches ; i++)
{
if (forkit(globbuf.gl_pathv[i]) == 0)
continue;
fprintf(stderr, "init: failed! we're done here\n");
return -1;
}
globfree(&globbuf);
// we do not need stdin, but our children might
close(0);
openlog("init", 0, LOG_DAEMON);
if (fork() == 0)
loginfo();
while(1)
{
int status;
pid_t pid = wait(&status);
if (status != 0)
syslog(LOG_WARNING, "pid %d exited status %08x", pid, status);
else
fprintf(stderr, "init: pid %d exited status %08x\n", (int) pid, status);
}
}