Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / usr / init / init.c
index 1b80f495d05770b7dca832831a5b625f6f8733d5..45bbf929de7fbafcc082ccd86fad4eabf6fdaecb 100644 (file)
@@ -2,6 +2,7 @@
 #include <fcntl.h>
 #include <lunaix/lunaix.h>
 #include <lunaix/mount.h>
+#include <termios.h>
 #include <stdio.h>
 #include <unistd.h>
 
         }                                                                      \
     } 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");
@@ -27,22 +53,16 @@ main(int argc, const char** argv)
     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 err;
-    }
+    int fd = check(open("/dev/tty", 0));
 
-    if ((err = dup(err)) < 0) {
-        syslog(2, "fail to setup tty i/o (%d)\n", errno);
-        return err;
-    }
+    check(init_termios(fd));
 
-    if ((err = symlink("/usr", "/mnt/lunaix-os/usr"))) {
-        syslog(2, "symlink /usr:/mnt/lunaix-os/usr (%d)\n", errno);
-        return err;
-    }
+    check(dup(fd));
+
+    check(symlink("/usr", "/mnt/lunaix-os/usr"));
 
     pid_t pid;
+    int err = 0;
     if (!(pid = fork())) {
         err = execve("/usr/bin/sh", NULL, NULL);
         printf("fail to execute (%d)\n", errno);
@@ -52,8 +72,10 @@ main(int argc, const char** argv)
     waitpid(pid, &err, 0);
 
     if (WEXITSTATUS(err)) {
-        printf("shell exit abnormally (%d)", err);
+        printf("shell exit abnormally (%d)\n", err);
     }
 
+    printf("init exiting\n");
+
     return err;
 }
\ No newline at end of file