refactor: make pci device driver loading passive, pci bus scanner will not load them...
[lunaix-os.git] / lunaix-os / usr / init / init.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <lunaix/lunaix.h>
4 #include <lunaix/mount.h>
5 #include <stdio.h>
6 #include <unistd.h>
7
8 #define must_mount(src, target, fs, opts)                                      \
9     do {                                                                       \
10         int err = 0;                                                           \
11         if ((err = mount(src, target, fs, opts))) {                            \
12             syslog(2, "mount fs %s to %s failed (%d)\n", fs, target, errno);   \
13             return err;                                                        \
14         }                                                                      \
15     } while (0)
16
17 int
18 main(int argc, const char** argv)
19 {
20     int err = 0;
21
22     mkdir("/dev");
23     mkdir("/sys");
24     mkdir("/task");
25
26     must_mount(NULL, "/dev", "devfs", 0);
27     must_mount(NULL, "/sys", "twifs", MNT_RO);
28     must_mount(NULL, "/task", "taskfs", MNT_RO);
29
30     if ((err = open("/dev/tty", 0)) < 0) {
31         syslog(2, "fail to open tty (%d)\n", errno);
32         return err;
33     }
34
35     if ((err = dup(err)) < 0) {
36         syslog(2, "fail to setup tty i/o (%d)\n", errno);
37         return err;
38     }
39
40     if ((err = symlink("/usr", "/mnt/lunaix-os/usr"))) {
41         syslog(2, "symlink /usr:/mnt/lunaix-os/usr (%d)\n", errno);
42         return err;
43     }
44
45     pid_t pid;
46     if (!(pid = fork())) {
47         err = execve("/usr/bin/sh", NULL, NULL);
48         printf("fail to execute (%d)\n", errno);
49         _exit(err);
50     }
51
52     waitpid(pid, &err, 0);
53
54     if (WEXITSTATUS(err)) {
55         printf("shell exit abnormally (%d)", err);
56     }
57
58     return err;
59 }