feat: kill(2) implementation
[lunaix-os.git] / lunaix-os / kernel / lxinit.c
1 #include <hal/cpu.h>
2 #include <lunaix/clock.h>
3 #include <lunaix/keyboard.h>
4 #include <lunaix/lunistd.h>
5 #include <lunaix/mm/kalloc.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/proc.h>
8 #include <lunaix/signal.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/syslog.h>
11 #include <lunaix/timer.h>
12 #include <lunaix/tty/tty.h>
13
14 extern uint8_t __kernel_start;
15
16 LOG_MODULE("INIT")
17
18 // #define FORK_BOMB_DEMO
19 #define WAIT_DEMO
20 #define IN_USER_MODE
21
22 void __USER__
23 sigchild_handler(int signum)
24 {
25     kprintf(KINFO "SIGCHLD received\n");
26 }
27
28 void __USER__
29 sigsegv_handler(int signum)
30 {
31     pid_t pid = getpid();
32     kprintf(KWARN "SIGSEGV received on process %d\n", pid);
33     _exit(signum);
34 }
35
36 void __USER__
37 _lxinit_main()
38 {
39 #ifdef FORK_BOMB_DEMO
40     // fork炸弹
41     for (;;) {
42         pid_t p;
43         if ((p = fork())) {
44             kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p);
45         }
46     }
47 #endif
48
49     signal(_SIGCHLD, sigchild_handler);
50     signal(_SIGSEGV, sigsegv_handler);
51
52     int status;
53 #ifdef WAIT_DEMO
54     // 测试wait
55     kprintf("I am parent, going to fork my child and wait.\n");
56     if (!fork()) {
57         kprintf("I am child, going to sleep for 2 seconds\n");
58         sleep(2);
59         kprintf("I am child, I am about to terminated\n");
60         _exit(1);
61     }
62     pause();
63     pid_t child = wait(&status);
64     kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
65             child,
66             WEXITSTATUS(status));
67 #endif
68
69     pid_t p = 0;
70
71     if (!fork()) {
72         kprintf("Test no hang!\n");
73         sleep(6);
74         _exit(0);
75     }
76
77     waitpid(-1, &status, WNOHANG);
78
79     for (size_t i = 0; i < 5; i++) {
80         pid_t pid = 0;
81         if (!(pid = fork())) {
82             signal(_SIGSEGV, sigsegv_handler);
83             sleep(i);
84             if (i == 3) {
85                 i = *(int*)0xdeadc0de; // seg fault!
86             }
87             tty_put_char('0' + i);
88             tty_put_char('\n');
89             _exit(0);
90         }
91         kprintf(KINFO "Forked %d\n", pid);
92     }
93
94     while ((p = wait(&status)) >= 0) {
95         short code = WEXITSTATUS(status);
96         if (WIFSIGNALED(status)) {
97             kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
98                     p,
99                     code);
100         } else if (WIFEXITED(status)) {
101             kprintf(KINFO "Process %d exited with code %d\n", p, code);
102         } else {
103             kprintf(KWARN "Process %d aborted with code %d\n", p, code);
104         }
105     }
106
107     char buf[64];
108
109     kprintf(KINFO "Hello processes!\n");
110
111     cpu_get_brand(buf);
112     kprintf("CPU: %s\n\n", buf);
113
114     // no lxmalloc here! This can only be used within kernel, but here, we are
115     // in a dedicated process! any access to kernel method must be done via
116     // syscall
117
118     struct kdb_keyinfo_pkt keyevent;
119     while (1) {
120         if (!kbd_recv_key(&keyevent)) {
121             yield();
122             continue;
123         }
124         if ((keyevent.state & KBD_KEY_FPRESSED) &&
125             (keyevent.keycode & 0xff00) <= KEYPAD) {
126             tty_put_char((char)(keyevent.keycode & 0x00ff));
127             // FIXME: io to vga port is privileged and cause #GP in user mode
128             // tty_sync_cursor();
129         }
130     }
131     spin();
132 }