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