X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/2a49908006b177c4d6354309333d06b1b96e4887..b60166b327a9108b07e3069fa6568a451529ffd9:/lunaix-os/usr/init/init.c diff --git a/lunaix-os/usr/init/init.c b/lunaix-os/usr/init/init.c index 7e1785a..45bbf92 100644 --- a/lunaix-os/usr/init/init.c +++ b/lunaix-os/usr/init/init.c @@ -1,39 +1,81 @@ #include #include #include +#include +#include #include #include +#define must_mount(src, target, fs, opts) \ + do { \ + int err = 0; \ + if ((err = mount(src, target, fs, opts))) { \ + syslog(2, "mount fs %s to %s failed (%d)\n", fs, target, errno); \ + return err; \ + } \ + } while (0) + +#define check(statement) \ + ({ \ + int err = 0; \ + if ((err = (statement)) < 0) { \ + syslog(2, #statement " failed: %d", err); \ + _exit(1); \ + } \ + err; \ + }) + +int +init_termios(int fd) { + struct termios term; + + check(tcgetattr(fd, &term)); + + term.c_lflag = ICANON | IEXTEN | ISIG | ECHO | ECHOE | ECHONL; + term.c_iflag = ICRNL | IGNBRK; + term.c_oflag = ONLCR | OPOST; + term.c_cflag = CREAD | CLOCAL | CS8 | CPARENB; + term.c_cc[VERASE] = 0x7f; + + check(tcsetattr(fd, 0, &term)); + + return 0; +} + int main(int argc, const char** argv) { - int err = 0; + mkdir("/dev"); + mkdir("/sys"); + mkdir("/task"); - if ((err = open("/dev/tty", 0)) < 0) { - syslog(2, "fail to open tty (%d)\n", errno); - return 0; - } + must_mount(NULL, "/dev", "devfs", 0); + must_mount(NULL, "/sys", "twifs", MNT_RO); + must_mount(NULL, "/task", "taskfs", MNT_RO); - if ((err = dup(err)) < 0) { - syslog(2, "fail to setup tty i/o (%d)\n", errno); - return 0; - } + int fd = check(open("/dev/tty", 0)); - printf("(%p) user space!\n", (void*)main); + check(init_termios(fd)); - if ((err = symlink("/usr", "/mnt/lunaix-os/usr"))) { - syslog(2, "symlink /usr:/mnt/lunaix-os/usr (%d)\n", errno); - return 0; - } + check(dup(fd)); + + check(symlink("/usr", "/mnt/lunaix-os/usr")); pid_t pid; + int err = 0; if (!(pid = fork())) { - err = execve("/usr/sh", NULL, NULL); + err = execve("/usr/bin/sh", NULL, NULL); printf("fail to execute (%d)\n", errno); _exit(err); } - waitpid(pid, NULL, 0); + waitpid(pid, &err, 0); - return 0; + if (WEXITSTATUS(err)) { + printf("shell exit abnormally (%d)\n", err); + } + + printf("init exiting\n"); + + return err; } \ No newline at end of file