Framework for exporting system header to user space (#59)
[lunaix-os.git] / lunaix-os / usr / init / init.c
index 0a2f5418a9de84eca57dcd9af649b7360ed167c7..67a73f995835e71442612ce38f7f63fcc721f058 100644 (file)
@@ -1,11 +1,13 @@
 #include <errno.h>
 #include <fcntl.h>
-#include <lunaix/lunaix.h>
-#include <lunaix/mount.h>
+#include <sys/wait.h>
+#include <sys/mount.h>
 #include <termios.h>
 #include <stdio.h>
 #include <unistd.h>
 
+#include <sys/lunaix.h>
+
 #define must_mount(src, target, fs, opts)                                      \
     do {                                                                       \
         int err = 0;                                                           \
         }                                                                      \
     } while (0)
 
-#define check(statement)                                      \
+#define maybe_mount(src, target, fs, opts)                                      \
     do {                                                                       \
         int err = 0;                                                           \
-        if ((err = (statement))) {                            \
-            syslog(2, #statement " failed: %d", err);   \
-            return err;                                                        \
+        if ((err = mount(src, target, fs, opts))) {                            \
+            syslog(2, "mount fs %s to %s failed (%d)\n", fs, target, errno);   \
         }                                                                      \
     } 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_lflag = ICANON | IEXTEN | ISIG | ECHO | ECHOE;
     term.c_iflag = ICRNL | IGNBRK;
     term.c_oflag = ONLCR | OPOST;
     term.c_cflag = CREAD | CLOCAL | CS8 | CPARENB;
-    term.c_cc[VERASE] = 0x08;
+    term.c_cc[VERASE] = 0x7f;
+    
+    cfsetispeed(&term, B9600);
+    cfsetospeed(&term, B9600);
 
     check(tcsetattr(fd, 0, &term));
 
     return 0;
 }
 
+const char* sh_argv[] = { "/bin/sh", 0  };
+const char* sh_envp[] = {  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 err;
-    }
-
-    check(init_termios(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));
 
     pid_t pid;
+    int err = 0;
     if (!(pid = fork())) {
-        err = execve("/usr/bin/sh", NULL, NULL);
+        err = execve(sh_argv[0], sh_argv, sh_envp);
         printf("fail to execute (%d)\n", errno);
         _exit(err);
     }
@@ -81,8 +82,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