-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.c
71 lines (61 loc) · 1.13 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
#include "type.h"
#include "ucode.c"
int pid, status, child[3];
int stdin, stdout;
int main(int argc, char* argv[])
{
int i;
stdin = open("/dev/tty0", 0);
stdout = open("/dev/tty0", 1);
printf("IFMinit: fork a login task on console...\n");
for(i=0; i<3; i++) {
child[i] = fork();
if(!child[i]) // child
login(i);
}
parent();
}
int login(int i)
{
switch (i) {
case 0:
exec("login /dev/tty0");
printf("login on tty0 failed\n");
exit(-1);
case 1:
printf("ttyS0\n");
exec("login /dev/ttyS0");
printf("login on ttyS0 failed\n");
exit(-1);
case 2:
exec("login /dev/ttyS1");
printf("login on ttyS1 failed\n");
exit(-1);
}
}
int get_tty(int pid) {
if(pid == child[0])
return 0;
if(pid == child[1])
return 1;
if(pid == child[2])
return 2;
return -1;
}
int parent()
{
int tty;
while(1)
{
pid = wait(&status);
tty = get_tty(pid);
if(-1 == tty)
printf("IFMinit: buried an orphan child: pid=%d\n", pid);
else {
printf("IFMinit: fork a login task on console...\n");
child[tty] = fork();
if(!child[tty])
login(tty);
}
}
}