root/kernel/signal.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_sigprocmask
  2. sys_sgetmask
  3. sys_ssetmask
  4. sys_sigpending
  5. sys_sigsuspend
  6. check_pending
  7. sys_signal
  8. sys_sigaction
  9. sys_sigreturn
  10. setup_frame
  11. do_signal

   1 /*
   2  *  linux/kernel/signal.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/sched.h>
   8 #include <linux/kernel.h>
   9 #include <linux/signal.h>
  10 #include <linux/errno.h>
  11 #include <linux/wait.h>
  12 #include <linux/ptrace.h>
  13 #include <linux/unistd.h>
  14 
  15 #include <asm/segment.h>
  16 
  17 #define _S(nr) (1<<((nr)-1))
  18 
  19 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
  20 
  21 extern int core_dump(long signr,struct pt_regs * regs);
  22 
  23 asmlinkage int do_signal(unsigned long oldmask, struct pt_regs * regs);
  24 
  25 struct sigcontext_struct {
  26         unsigned short gs, __gsh;
  27         unsigned short fs, __fsh;
  28         unsigned short es, __esh;
  29         unsigned short ds, __dsh;
  30         unsigned long edi;
  31         unsigned long esi;
  32         unsigned long ebp;
  33         unsigned long esp;
  34         unsigned long ebx;
  35         unsigned long edx;
  36         unsigned long ecx;
  37         unsigned long eax;
  38         unsigned long trapno;
  39         unsigned long err;
  40         unsigned long eip;
  41         unsigned short cs, __csh;
  42         unsigned long eflags;
  43         unsigned long esp_at_signal;
  44         unsigned short ss, __ssh;
  45         unsigned long i387;
  46         unsigned long oldmask;
  47         unsigned long cr2;
  48 };
  49 
  50 asmlinkage int sys_sigprocmask(int how, sigset_t *set, sigset_t *oset)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52         sigset_t new_set, old_set = current->blocked;
  53         int error;
  54 
  55         if (set) {
  56                 error = verify_area(VERIFY_READ, set, sizeof(sigset_t));
  57                 if (error)
  58                         return error;
  59                 new_set = get_fs_long((unsigned long *) set) & _BLOCKABLE;
  60                 switch (how) {
  61                 case SIG_BLOCK:
  62                         current->blocked |= new_set;
  63                         break;
  64                 case SIG_UNBLOCK:
  65                         current->blocked &= ~new_set;
  66                         break;
  67                 case SIG_SETMASK:
  68                         current->blocked = new_set;
  69                         break;
  70                 default:
  71                         return -EINVAL;
  72                 }
  73         }
  74         if (oset) {
  75                 error = verify_area(VERIFY_WRITE, oset, sizeof(sigset_t));
  76                 if (error)
  77                         return error;
  78                 put_fs_long(old_set, (unsigned long *) oset);
  79         }
  80         return 0;
  81 }
  82 
  83 asmlinkage int sys_sgetmask(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85         return current->blocked;
  86 }
  87 
  88 asmlinkage int sys_ssetmask(int newmask)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90         int old=current->blocked;
  91 
  92         current->blocked = newmask & _BLOCKABLE;
  93         return old;
  94 }
  95 
  96 asmlinkage int sys_sigpending(sigset_t *set)
     /* [previous][next][first][last][top][bottom][index][help] */
  97 {
  98         int error;
  99         /* fill in "set" with signals pending but blocked. */
 100         error = verify_area(VERIFY_WRITE, set, 4);
 101         if (!error)
 102                 put_fs_long(current->blocked & current->signal, (unsigned long *)set);
 103         return error;
 104 }
 105 
 106 /*
 107  * atomically swap in the new signal mask, and wait for a signal.
 108  */
 109 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, unsigned long set)
     /* [previous][next][first][last][top][bottom][index][help] */
 110 {
 111         unsigned long mask;
 112         struct pt_regs * regs = (struct pt_regs *) &restart;
 113 
 114         mask = current->blocked;
 115         current->blocked = set & _BLOCKABLE;
 116         regs->eax = -EINTR;
 117         while (1) {
 118                 current->state = TASK_INTERRUPTIBLE;
 119                 schedule();
 120                 if (do_signal(mask,regs))
 121                         return -EINTR;
 122         }
 123 }
 124 
 125 /*
 126  * POSIX 3.3.1.3:
 127  *  "Setting a signal action to SIG_IGN for a signal that is pending
 128  *   shall cause the pending signal to be discarded, whether or not
 129  *   it is blocked" (but SIGCHLD is unspecified: linux leaves it alone).
 130  *
 131  *  "Setting a signal action to SIG_DFL for a signal that is pending
 132  *   and whose default action is to ignore the signal (for example,
 133  *   SIGCHLD), shall cause the pending signal to be discarded, whether
 134  *   or not it is blocked"
 135  *
 136  * Note the silly behaviour of SIGCHLD: SIG_IGN means that the signal
 137  * isn't actually ignored, but does automatic child reaping, while
 138  * SIG_DFL is explicitly said by POSIX to force the signal to be ignored..
 139  */
 140 static void check_pending(int signum)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142         struct sigaction *p;
 143 
 144         p = signum - 1 + current->sigaction;
 145         if (p->sa_handler == SIG_IGN) {
 146                 if (signum == SIGCHLD)
 147                         return;
 148                 current->signal &= ~_S(signum);
 149                 return;
 150         }
 151         if (p->sa_handler == SIG_DFL) {
 152                 if (signum != SIGCONT && signum != SIGCHLD && signum != SIGWINCH)
 153                         return;
 154                 current->signal &= ~_S(signum);
 155                 return;
 156         }       
 157 }
 158 
 159 asmlinkage int sys_signal(int signum, unsigned long handler)
     /* [previous][next][first][last][top][bottom][index][help] */
 160 {
 161         struct sigaction tmp;
 162 
 163         if (signum<1 || signum>32 || signum==SIGKILL || signum==SIGSTOP)
 164                 return -EINVAL;
 165         if (handler >= TASK_SIZE)
 166                 return -EFAULT;
 167         tmp.sa_handler = (void (*)(int)) handler;
 168         tmp.sa_mask = 0;
 169         tmp.sa_flags = SA_ONESHOT | SA_NOMASK;
 170         tmp.sa_restorer = NULL;
 171         handler = (long) current->sigaction[signum-1].sa_handler;
 172         current->sigaction[signum-1] = tmp;
 173         check_pending(signum);
 174         return handler;
 175 }
 176 
 177 asmlinkage int sys_sigaction(int signum, const struct sigaction * action,
     /* [previous][next][first][last][top][bottom][index][help] */
 178         struct sigaction * oldaction)
 179 {
 180         struct sigaction new_sa, *p;
 181 
 182         if (signum<1 || signum>32 || signum==SIGKILL || signum==SIGSTOP)
 183                 return -EINVAL;
 184         p = signum - 1 + current->sigaction;
 185         if (action) {
 186                 memcpy_fromfs(&new_sa, action, sizeof(struct sigaction));
 187                 if (new_sa.sa_flags & SA_NOMASK)
 188                         new_sa.sa_mask = 0;
 189                 else {
 190                         new_sa.sa_mask |= _S(signum);
 191                         new_sa.sa_mask &= _BLOCKABLE;
 192                 }
 193                 if (TASK_SIZE <= (unsigned long) new_sa.sa_handler)
 194                         return -EFAULT;
 195         }
 196         if (oldaction) {
 197                 if (!verify_area(VERIFY_WRITE,oldaction, sizeof(struct sigaction)))
 198                         memcpy_tofs(oldaction, p, sizeof(struct sigaction));
 199         }
 200         if (action) {
 201                 *p = new_sa;
 202                 check_pending(signum);
 203         }
 204         return 0;
 205 }
 206 
 207 asmlinkage int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options);
 208 
 209 /*
 210  * This sets regs->esp even though we don't actually use sigstacks yet..
 211  */
 212 asmlinkage int sys_sigreturn(unsigned long __unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214 #define COPY(x) regs->x = context.x
 215 #define COPY_SEG(x) \
 216 if ((context.x & 0xfffc) && (context.x & 3) != 3) goto badframe; COPY(x);
 217 #define COPY_SEG_STRICT(x) \
 218 if (!(context.x & 0xfffc) || (context.x & 3) != 3) goto badframe; COPY(x);
 219         struct sigcontext_struct context;
 220         struct pt_regs * regs;
 221 
 222         regs = (struct pt_regs *) &__unused;
 223         if (verify_area(VERIFY_READ, (void *) regs->esp, sizeof(context)))
 224                 goto badframe;
 225         memcpy_fromfs(&context,(void *) regs->esp, sizeof(context));
 226         current->blocked = context.oldmask & _BLOCKABLE;
 227         COPY_SEG(ds);
 228         COPY_SEG(es);
 229         COPY_SEG(fs);
 230         COPY_SEG(gs);
 231         COPY_SEG_STRICT(ss);
 232         COPY_SEG_STRICT(cs);
 233         COPY(eip); COPY(eflags);
 234         COPY(ecx); COPY(edx);
 235         COPY(ebx);
 236         COPY(esp); COPY(ebp);
 237         COPY(edi); COPY(esi);
 238         regs->orig_eax = -1;            /* disable syscall checks */
 239         return context.eax;
 240 badframe:
 241         do_exit(SIGSEGV);
 242 }
 243 
 244 /*
 245  * Set up a signal frame... Make the stack look the way iBCS2 expects
 246  * it to look.
 247  */
 248 static void setup_frame(struct sigaction * sa, unsigned long ** fp, unsigned long eip,
     /* [previous][next][first][last][top][bottom][index][help] */
 249         struct pt_regs * regs, int signr, unsigned long oldmask)
 250 {
 251         unsigned long * frame;
 252 
 253 #define __CODE ((unsigned long)(frame+24))
 254 #define CODE(x) ((unsigned long *) ((x)+__CODE))
 255         frame = *fp;
 256         if (regs->ss != USER_DS)
 257                 frame = (unsigned long *) sa->sa_restorer;
 258         frame -= 32;
 259         if (verify_area(VERIFY_WRITE,frame,32*4))
 260                 do_exit(SIGSEGV);
 261 /* set up the "normal" stack seen by the signal handler (iBCS2) */
 262         put_fs_long(__CODE,frame);
 263         put_fs_long(signr, frame+1);
 264         put_fs_long(regs->gs, frame+2);
 265         put_fs_long(regs->fs, frame+3);
 266         put_fs_long(regs->es, frame+4);
 267         put_fs_long(regs->ds, frame+5);
 268         put_fs_long(regs->edi, frame+6);
 269         put_fs_long(regs->esi, frame+7);
 270         put_fs_long(regs->ebp, frame+8);
 271         put_fs_long((long)*fp, frame+9);
 272         put_fs_long(regs->ebx, frame+10);
 273         put_fs_long(regs->edx, frame+11);
 274         put_fs_long(regs->ecx, frame+12);
 275         put_fs_long(regs->eax, frame+13);
 276         put_fs_long(current->tss.trap_no, frame+14);
 277         put_fs_long(current->tss.error_code, frame+15);
 278         put_fs_long(eip, frame+16);
 279         put_fs_long(regs->cs, frame+17);
 280         put_fs_long(regs->eflags, frame+18);
 281         put_fs_long(regs->esp, frame+19);
 282         put_fs_long(regs->ss, frame+20);
 283         put_fs_long(0,frame+21);                /* 387 state pointer - not implemented*/
 284 /* non-iBCS2 extensions.. */
 285         put_fs_long(oldmask, frame+22);
 286         put_fs_long(current->tss.cr2, frame+23);
 287 /* set up the return code... */
 288         put_fs_long(0x0000b858, CODE(0));       /* popl %eax ; movl $,%eax */
 289         put_fs_long(0x80cd0000, CODE(4));       /* int $0x80 */
 290         put_fs_long(__NR_sigreturn, CODE(2));
 291         *fp = frame;
 292 #undef __CODE
 293 #undef CODE
 294 }
 295 
 296 /*
 297  * Note that 'init' is a special process: it doesn't get signals it doesn't
 298  * want to handle. Thus you cannot kill init even with a SIGKILL even by
 299  * mistake.
 300  *
 301  * Note that we go through the signals twice: once to check the signals that
 302  * the kernel can handle, and then we build all the user-level signal handling
 303  * stack-frames in one go after that.
 304  */
 305 asmlinkage int do_signal(unsigned long oldmask, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         unsigned long mask = ~current->blocked;
 308         unsigned long handler_signal = 0;
 309         unsigned long *frame = NULL;
 310         unsigned long eip = 0;
 311         unsigned long signr;
 312         struct sigaction * sa;
 313 
 314         while ((signr = current->signal & mask)) {
 315                 __asm__("bsf %2,%1\n\t"
 316                         "btrl %1,%0"
 317                         :"=m" (current->signal),"=r" (signr)
 318                         :"1" (signr));
 319                 sa = current->sigaction + signr;
 320                 signr++;
 321                 if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
 322                         current->exit_code = signr;
 323                         current->state = TASK_STOPPED;
 324                         notify_parent(current);
 325                         schedule();
 326                         if (!(signr = current->exit_code))
 327                                 continue;
 328                         current->exit_code = 0;
 329                         if (signr == SIGSTOP)
 330                                 continue;
 331                         if (_S(signr) & current->blocked) {
 332                                 current->signal |= _S(signr);
 333                                 continue;
 334                         }
 335                         sa = current->sigaction + signr - 1;
 336                 }
 337                 if (sa->sa_handler == SIG_IGN) {
 338                         if (signr != SIGCHLD)
 339                                 continue;
 340                         /* check for SIGCHLD: it's special */
 341                         while (sys_waitpid(-1,NULL,WNOHANG) > 0)
 342                                 /* nothing */;
 343                         continue;
 344                 }
 345                 if (sa->sa_handler == SIG_DFL) {
 346                         if (current->pid == 1)
 347                                 continue;
 348                         switch (signr) {
 349                         case SIGCONT: case SIGCHLD: case SIGWINCH:
 350                                 continue;
 351 
 352                         case SIGSTOP: case SIGTSTP: case SIGTTIN: case SIGTTOU:
 353                                 if (current->flags & PF_PTRACED)
 354                                         continue;
 355                                 current->state = TASK_STOPPED;
 356                                 current->exit_code = signr;
 357                                 if (!(current->p_pptr->sigaction[SIGCHLD-1].sa_flags & 
 358                                                 SA_NOCLDSTOP))
 359                                         notify_parent(current);
 360                                 schedule();
 361                                 continue;
 362 
 363                         case SIGQUIT: case SIGILL: case SIGTRAP:
 364                         case SIGIOT: case SIGFPE: case SIGSEGV:
 365                                 if (core_dump(signr,regs))
 366                                         signr |= 0x80;
 367                                 /* fall through */
 368                         default:
 369                                 current->signal |= _S(signr & 0x7f);
 370                                 do_exit(signr);
 371                         }
 372                 }
 373                 /*
 374                  * OK, we're invoking a handler
 375                  */
 376                 if (regs->orig_eax >= 0) {
 377                         if (regs->eax == -ERESTARTNOHAND ||
 378                            (regs->eax == -ERESTARTSYS && !(sa->sa_flags & SA_RESTART)))
 379                                 regs->eax = -EINTR;
 380                 }
 381                 handler_signal |= 1 << (signr-1);
 382                 mask &= ~sa->sa_mask;
 383         }
 384         if (regs->orig_eax >= 0 &&
 385             (regs->eax == -ERESTARTNOHAND ||
 386              regs->eax == -ERESTARTSYS ||
 387              regs->eax == -ERESTARTNOINTR)) {
 388                 regs->eax = regs->orig_eax;
 389                 regs->eip -= 2;
 390         }
 391         if (!handler_signal)            /* no handler will be called - return 0 */
 392                 return 0;
 393         eip = regs->eip;
 394         frame = (unsigned long *) regs->esp;
 395         signr = 1;
 396         sa = current->sigaction;
 397         for (mask = 1 ; mask ; sa++,signr++,mask += mask) {
 398                 if (mask > handler_signal)
 399                         break;
 400                 if (!(mask & handler_signal))
 401                         continue;
 402                 setup_frame(sa,&frame,eip,regs,signr,oldmask);
 403                 eip = (unsigned long) sa->sa_handler;
 404                 if (sa->sa_flags & SA_ONESHOT)
 405                         sa->sa_handler = NULL;
 406 /* force a supervisor-mode page-in of the signal handler to reduce races */
 407                 __asm__("testb $0,%%fs:%0": :"m" (*(char *) eip));
 408                 regs->cs = USER_CS; regs->ss = USER_DS;
 409                 regs->ds = USER_DS; regs->es = USER_DS;
 410                 regs->gs = USER_DS; regs->fs = USER_DS;
 411                 current->blocked |= sa->sa_mask;
 412                 oldmask |= sa->sa_mask;
 413         }
 414         regs->esp = (unsigned long) frame;
 415         regs->eip = eip;                /* "return" to the first handler */
 416         current->tss.trap_no = current->tss.error_code = 0;
 417         return 1;
 418 }

/* [previous][next][first][last][top][bottom][index][help] */