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

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