1 #include <lunaix/lunistd.h>
2 #include <lunaix/lxsignal.h>
3 #include <lunaix/process.h>
4 #include <lunaix/sched.h>
5 #include <lunaix/signal.h>
6 #include <lunaix/spike.h>
7 #include <lunaix/status.h>
8 #include <lunaix/syscall.h>
10 extern struct scheduler sched_ctx; /* kernel/sched.c */
13 default_sighandler_term(int signum)
18 void* default_handlers[_SIG_NUM] = {
20 [_SIGINT] = default_sighandler_term,
21 [_SIGTERM] = default_sighandler_term,
24 // Referenced in kernel/asm/x86/interrupt.S
28 if (!__current->sig_pending) {
34 31 - __builtin_clz(__current->sig_pending &
35 ~(__current->sig_mask | __current->sig_inprogress));
37 __SIGCLEAR(__current->sig_pending, sig_selected);
39 if (sig_selected == 0) {
44 if (!__current->sig_handler[sig_selected] &&
45 !default_handlers[sig_selected]) {
50 uintptr_t ustack = __current->ustack_top & ~0xf;
52 if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
57 struct proc_sig* sig_ctx =
58 (struct proc_sig*)(ustack - sizeof(struct proc_sig));
60 sig_ctx->prev_context = __current->intr_ctx;
61 sig_ctx->sig_num = sig_selected;
62 sig_ctx->signal_handler = __current->sig_handler[sig_selected];
64 if (!sig_ctx->signal_handler) {
65 // 如果没有用户自定义的Handler,则使用系统默认Handler。
66 sig_ctx->signal_handler = default_handlers[sig_selected];
69 __SIGSET(__current->sig_inprogress, sig_selected);
75 signal_send(pid_t pid, int signum)
77 if (signum < 0 || signum >= _SIG_NUM) {
78 __current->k_status = LXINVL;
82 struct proc_info* proc;
84 proc = get_process(pid);
89 } else if (pid < -1) {
90 proc = get_process(-pid);
93 // TODO: send to all process.
94 // But I don't want to support it yet.
95 __current->k_status = LXINVL;
100 struct proc_info *pos, *n;
101 llist_for_each(pos, n, &proc->grp_member, grp_member)
103 __SIGSET(pos->sig_pending, signum);
108 if ((proc->state & PROC_TERMMASK)) {
109 __current->k_status = LXINVL;
112 __SIGSET(proc->sig_pending, signum);
116 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
118 __current->intr_ctx = sig_ctx->prev_context;
119 __current->flags &= ~PROC_FINPAUSE;
120 __SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
124 __DEFINE_LXSYSCALL3(int,
133 *oldset = __current->sig_mask;
134 if (how == _SIG_BLOCK) {
135 __current->sig_mask |= *set;
136 } else if (how == _SIG_UNBLOCK) {
137 __current->sig_mask &= ~(*set);
138 } else if (how == _SIG_SETMASK) {
139 __current->sig_mask = *set;
143 __current->sig_mask &= ~_SIGNAL_UNMASKABLE;
147 __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler)
149 if (signum <= 0 || signum >= _SIG_NUM) {
153 if ((__SIGNAL(signum) & _SIGNAL_UNMASKABLE)) {
157 __current->sig_handler[signum] = (void*)handler;
162 __DEFINE_LXSYSCALL(int, pause)
164 __current->flags |= PROC_FINPAUSE;
166 __SYSCALL_INTERRUPTIBLE({
167 while ((__current->flags & PROC_FINPAUSE)) {
171 __current->k_status = EINTR;
175 __DEFINE_LXSYSCALL2(int, kill, pid_t, pid, int, signum)
177 return signal_send(pid, signum);