Merge branch 'master' of github.com:Minep/lunaix-os
[lunaix-os.git] / lunaix-os / kernel / signal.c
index 3edb2f4fea2cba2fb820d2b700e825bf9f4e9fcd..dd16e2461c558df3cf534735a43dbda7cf135570 100644 (file)
@@ -1,43 +1,58 @@
+#include <lunaix/lunistd.h>
+#include <lunaix/lxsignal.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/signal.h>
+#include <lunaix/spike.h>
+#include <lunaix/status.h>
 #include <lunaix/syscall.h>
 
 extern struct scheduler sched_ctx; /* kernel/sched.c */
 
+void __USER__
+default_sighandler_term(int signum)
+{
+    _exit(signum);
+}
+
 void* default_handlers[_SIG_NUM] = {
     // TODO: 添加默认handler
+    [_SIGINT] = default_sighandler_term,  [_SIGTERM] = default_sighandler_term,
+    [_SIGKILL] = default_sighandler_term, [_SIGSEGV] = default_sighandler_term,
+    [_SIGINT] = default_sighandler_term,
 };
 
-void
+// Referenced in kernel/asm/x86/interrupt.S
+void*
 signal_dispatch()
 {
-    // if (!(SEL_RPL(__current->intr_ctx.cs))) {
-    //     // 同特权级间调度不进行信号处理
-    //     return;
-    // }
-
     if (!__current->sig_pending) {
         // 没有待处理信号
-        return;
+        return 0;
     }
 
     int sig_selected =
-      31 - __builtin_clz(__current->sig_pending & ~__current->sig_mask);
+      31 - __builtin_clz(__current->sig_pending &
+                         ~(__current->sig_mask | __current->sig_inprogress));
 
-    __current->sig_pending = __current->sig_pending & ~(1 << sig_selected);
+    __SIGCLEAR(__current->sig_pending, sig_selected);
+
+    if (sig_selected == 0) {
+        // SIG0 is reserved
+        return 0;
+    }
 
     if (!__current->sig_handler[sig_selected] &&
         !default_handlers[sig_selected]) {
         // 如果该信号没有handler,则忽略
-        return;
+        return 0;
     }
 
-    uintptr_t ustack = __current->ustack_top;
+    uintptr_t ustack = __current->ustack_top & ~0xf;
 
     if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
         // 用户栈没有空间存放信号上下文
-        return;
+        return 0;
     }
 
     struct proc_sig* sig_ctx =
@@ -52,13 +67,58 @@ signal_dispatch()
         sig_ctx->signal_handler = default_handlers[sig_selected];
     }
 
-    asm volatile("pushl %0\n"
-                 "jmp handle_signal" ::"r"(sig_ctx));
+    __SIGSET(__current->sig_inprogress, sig_selected);
+
+    return sig_ctx;
+}
+
+int
+signal_send(pid_t pid, int signum)
+{
+    if (signum < 0 || signum >= _SIG_NUM) {
+        __current->k_status = LXINVL;
+        return -1;
+    }
+
+    struct proc_info* proc;
+    if (pid > 0) {
+        proc = get_process(pid);
+        goto send_single;
+    } else if (!pid) {
+        proc = __current;
+        goto send_grp;
+    } else if (pid < -1) {
+        proc = get_process(-pid);
+        goto send_grp;
+    } else {
+        // TODO: send to all process.
+        //  But I don't want to support it yet.
+        __current->k_status = LXINVL;
+        return -1;
+    }
+
+send_grp:
+    struct proc_info *pos, *n;
+    llist_for_each(pos, n, &proc->grp_member, grp_member)
+    {
+        __SIGSET(pos->sig_pending, signum);
+    }
+    return 0;
+
+send_single:
+    if (PROC_TERMINATED(proc->state)) {
+        __current->k_status = LXINVL;
+        return -1;
+    }
+    __SIGSET(proc->sig_pending, signum);
+    return 0;
 }
 
 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
 {
     __current->intr_ctx = sig_ctx->prev_context;
+    __current->flags &= ~PROC_FINPAUSE;
+    __SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
     schedule();
 }
 
@@ -87,15 +147,33 @@ __DEFINE_LXSYSCALL3(int,
 
 __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler)
 {
-    if (signum < 0 || signum >= _SIG_NUM) {
+    if (signum <= 0 || signum >= _SIG_NUM) {
         return -1;
     }
 
-    if (((1 << signum) & _SIGNAL_UNMASKABLE)) {
+    if ((__SIGNAL(signum) & _SIGNAL_UNMASKABLE)) {
         return -1;
     }
 
     __current->sig_handler[signum] = (void*)handler;
 
     return 0;
+}
+
+__DEFINE_LXSYSCALL(int, pause)
+{
+    __current->flags |= PROC_FINPAUSE;
+
+    __SYSCALL_INTERRUPTIBLE({
+        while ((__current->flags & PROC_FINPAUSE)) {
+            sched_yield();
+        }
+    })
+    __current->k_status = EINTR;
+    return -1;
+}
+
+__DEFINE_LXSYSCALL2(int, kill, pid_t, pid, int, signum)
+{
+    return signal_send(pid, signum);
 }
\ No newline at end of file