Second Extended Filesystem (ext2) and other improvements (#33)
[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 <termios.h>
6 #include <stdio.h>
7 #include <unistd.h>
8
9 #define must_mount(src, target, fs, opts)                                      \
10     do {                                                                       \
11         int err = 0;                                                           \
12         if ((err = mount(src, target, fs, opts))) {                            \
13             syslog(2, "mount fs %s to %s failed (%d)\n", fs, target, errno);   \
14             return err;                                                        \
15         }                                                                      \
16     } while (0)
17
18 #define maybe_mount(src, target, fs, opts)                                      \
19     do {                                                                       \
20         int err = 0;                                                           \
21         if ((err = mount(src, target, fs, opts))) {                            \
22             syslog(2, "mount fs %s to %s failed (%d)\n", fs, target, errno);   \
23         }                                                                      \
24     } while (0)
25
26 #define check(statement)                                                       \
27     ({                                                                         \
28         int err = 0;                                                           \
29         if ((err = (statement)) < 0) {                                         \
30             syslog(2, #statement " failed: %d", err);                          \
31             _exit(1);                                                          \
32         }                                                                      \
33         err;                                                                   \
34     })
35
36 int
37 init_termios(int fd) {
38     struct termios term;
39
40     check(tcgetattr(fd, &term));
41
42     term.c_lflag = ICANON | IEXTEN | ISIG | ECHO | ECHOE | ECHONL;
43     term.c_iflag = ICRNL | IGNBRK;
44     term.c_oflag = ONLCR | OPOST;
45     term.c_cflag = CREAD | CLOCAL | CS8 | CPARENB;
46     term.c_cc[VERASE] = 0x7f;
47     
48     cfsetispeed(&term, B9600);
49     cfsetospeed(&term, B9600);
50
51     check(tcsetattr(fd, 0, &term));
52
53     return 0;
54 }
55
56 const char* sh_argv[] = { "/usr/bin/sh", 0  };
57 const char* sh_envp[] = {  0  };
58
59 int
60 main(int argc, const char** argv)
61 {
62     mkdir("/dev");
63     mkdir("/sys");
64     mkdir("/task");
65     mkdir("/mnt/disk");
66
67     must_mount(NULL, "/dev", "devfs", 0);
68     must_mount(NULL, "/sys", "twifs", MNT_RO);
69     must_mount(NULL, "/task", "taskfs", MNT_RO);
70     maybe_mount("/dev/block/sdb", "/mnt/disk", "ext2", 0);
71
72     int fd = check(open("/dev/tty", 0));
73
74     check(init_termios(fd));
75
76     check(dup(fd));
77
78     check(symlink("/usr", "/mnt/lunaix-os/usr"));
79
80     pid_t pid;
81     int err = 0;
82     if (!(pid = fork())) {
83         err = execve(sh_argv[0], sh_argv, sh_envp);
84         printf("fail to execute (%d)\n", errno);
85         _exit(err);
86     }
87
88     waitpid(pid, &err, 0);
89
90     if (WEXITSTATUS(err)) {
91         printf("shell exit abnormally (%d)\n", err);
92     }
93
94     printf("init exiting\n");
95
96     return err;
97 }