-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.c
More file actions
133 lines (120 loc) · 3.1 KB
/
init.c
File metadata and controls
133 lines (120 loc) · 3.1 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
// #include "ucode.c"
// int console_list[3];
// int parent()
// {
// int pid, status;
// while (1)
// {
// for (int i = 0; i < 3; i++)
// {
// printf("INIT: wait for ZOMBIE child\n");
// pid = wait(&status);
// if (pid == console_list[i])
// { // if console login process died
// printf("INIT: forks a new console login\n");
// console_list[i] = 0;
// console_list[i] = fork(); //fork another one
// if (console_list[i])
// continue;
// else
// exec(ttys[i]); //new console login process
// }
// printf("INIT: I just buried an orphan child proc %d\n", pid);
// }
// }
// }
// main()
// {
// int in, out; // file descriptors for terminal I/O
// in = open("/dev/tty0", O_RDONLY); // file descriptor 0
// out = open("/dev/tty0", O_WRONLY); // for display to console
// printf("INIT: fork a login proc on console\n");
// console_list[0] = fork();
// if (console_list[0]) // parent
// console_list[1] = fork();
// else
// exec(ttys[0]);
// if (console_list[1])
// console_list[2] = fork();
// else
// exec(ttys[1]);
// if (console_list[2])
// parent();
// else
// exec(ttys[2]);
// }
#include "ucode.c"
//char *ttys[3] = {"login /dev/tty0", "login /dev/ttys0", "login /dev/ttys1"};
int console, s0, s1;
void parent()
{
int pid, status;
printf("INIT : waiting ...\n");
while (1)
{
//wait the process
pid = wait(&status);
if (pid == console)
{
//fork a new console
console = fork();
//if sh at console dies, run login again for console
if (console)
continue;
else
exec("login /dev/tty0");
}
if (pid == s0)
{
//fork new for s0
s0 = fork();
//if sh at s0 dies, run login for s0
if (s0)
continue;
else
exec("login /dev/ttyS0");
}
if (pid == s1)
{
//fork new terminal for s1
s1 = fork();
//if sh at s1 dies, run login again for s1
if (s1)
continue;
else
exec("login /dev/ttyS1");
}
//when a process exits, bury it
printf("INIT: buried an orphan child task %d\n", pid);
}
}
int main(int argc, char *argv[])
{
int in, out;
int pid;
// open read/write in QEMU console
in = open("/dev/tty0", O_RDONLY);
out = open("/dev/tty0", O_WRONLY);
printf("proc %d in Umode ", getpid());
printf("argc=%d %s %s\n", argc, argv[0], argv[1]);
printf("%s\n", "INIT: fork a login task on console");
//fork a console instance
console = fork();
if (console)
{ // parent
//fork login for other terminals
s0 = fork();
if (s0)
{
s1 = fork();
if (s1)
parent();
else
exec("login /dev/ttyS1");
}
else
exec("login /dev/ttyS0");
}
else
exec("login /dev/tty0");
}