X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/2bcb7a074fa1b63e5954092bdcb8752915d3e9e6..0765e7c133eb393d8cd0292af444543c2edf8ccc:/lunaix-os/usr/init/init.c?ds=sidebyside diff --git a/lunaix-os/usr/init/init.c b/lunaix-os/usr/init/init.c index 0ed0d0b..0a2f541 100644 --- a/lunaix-os/usr/init/init.c +++ b/lunaix-os/usr/init/init.c @@ -1,29 +1,74 @@ #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) \ + do { \ + int err = 0; \ + if ((err = (statement))) { \ + syslog(2, #statement " failed: %d", err); \ + return err; \ + } \ + } while (0) + +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] = 0x08; + + check(tcsetattr(fd, 0, &term)); + + return 0; +} + int main(int argc, const char** argv) { int err = 0; + mkdir("/dev"); + mkdir("/sys"); + mkdir("/task"); + + must_mount(NULL, "/dev", "devfs", 0); + must_mount(NULL, "/sys", "twifs", MNT_RO); + must_mount(NULL, "/task", "taskfs", MNT_RO); + if ((err = open("/dev/tty", 0)) < 0) { syslog(2, "fail to open tty (%d)\n", errno); - return 0; + return err; } + check(init_termios(err)); + if ((err = dup(err)) < 0) { syslog(2, "fail to setup tty i/o (%d)\n", errno); - return 0; + return err; } - printf("(%p) user space!\n", (void*)main); - if ((err = symlink("/usr", "/mnt/lunaix-os/usr"))) { syslog(2, "symlink /usr:/mnt/lunaix-os/usr (%d)\n", errno); - return 0; + return err; } pid_t pid; @@ -35,9 +80,9 @@ main(int argc, const char** argv) waitpid(pid, &err, 0); - if (err) { + if (WEXITSTATUS(err)) { printf("shell exit abnormally (%d)", err); } - return 0; + return err; } \ No newline at end of file