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)
 139                 return -EINVAL;
 140         if (signum==SIGKILL || signum==SIGSTOP)
 141                 return -EINVAL;
 142         if (handler >= TASK_SIZE)
 143                 return -EFAULT;
 144         tmp.sa_handler = (void (*)(int)) handler;
 145         tmp.sa_mask = 0;
 146         tmp.sa_flags = SA_ONESHOT | SA_NOMASK;
 147         tmp.sa_restorer = NULL;
 148         handler = (long) current->sigaction[signum-1].sa_handler;
 149         current->sigaction[signum-1] = tmp;
 150         check_pending(signum);
 151         return handler;
 152 }
 153 
 154 asmlinkage int sys_sigaction(int signum, const struct sigaction * action,
     /* [previous][next][first][last][top][bottom][index][help] */
 155         struct sigaction * oldaction)
 156 {
 157         struct sigaction new_sa, *p;
 158 
 159         if (signum<1 || signum>32)
 160                 return -EINVAL;
 161         if (signum==SIGKILL || signum==SIGSTOP)
 162                 return -EINVAL;
 163         p = signum - 1 + current->sigaction;
 164         if (action) {
 165                 int err = verify_area(VERIFY_READ, action, sizeof(*action));
 166                 if (err)
 167                         return err;
 168                 memcpy_fromfs(&new_sa, action, sizeof(struct sigaction));
 169                 if (new_sa.sa_flags & SA_NOMASK)
 170                         new_sa.sa_mask = 0;
 171                 else {
 172                         new_sa.sa_mask |= _S(signum);
 173                         new_sa.sa_mask &= _BLOCKABLE;
 174                 }
 175                 if (TASK_SIZE <= (unsigned long) new_sa.sa_handler)
 176                         return -EFAULT;
 177         }
 178         if (oldaction) {
 179                 int err = verify_area(VERIFY_WRITE, oldaction, sizeof(*oldaction));
 180                 if (err)
 181                         return err;
 182                 memcpy_tofs(oldaction, p, sizeof(struct sigaction));
 183         }
 184         if (action) {
 185                 *p = new_sa;
 186                 check_pending(signum);
 187         }
 188         return 0;
 189 }
 190 
 191 asmlinkage int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options);
 192 
 193 /*
 194  * This sets regs->esp even though we don't actually use sigstacks yet..
 195  */
 196 asmlinkage int sys_sigreturn(unsigned long __unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198 #define COPY(x) regs->x = context.x
 199 #define COPY_SEG(x) \
 200 if ((context.x & 0xfffc) && (context.x & 3) != 3) goto badframe; COPY(x);
 201 #define COPY_SEG_STRICT(x) \
 202 if (!(context.x & 0xfffc) || (context.x & 3) != 3) goto badframe; COPY(x);
 203         struct sigcontext_struct context;
 204         struct pt_regs * regs;
 205 
 206         regs = (struct pt_regs *) &__unused;
 207         if (verify_area(VERIFY_READ, (void *) regs->esp, sizeof(context)))
 208                 goto badframe;
 209         memcpy_fromfs(&context,(void *) regs->esp, sizeof(context));
 210         current->blocked = context.oldmask & _BLOCKABLE;
 211         COPY_SEG(ds);
 212         COPY_SEG(es);
 213         COPY_SEG(fs);
 214         COPY_SEG(gs);
 215         COPY_SEG_STRICT(ss);
 216         COPY_SEG_STRICT(cs);
 217         COPY(eip);
 218         COPY(ecx); COPY(edx);
 219         COPY(ebx);
 220         COPY(esp); COPY(ebp);
 221         COPY(edi); COPY(esi);
 222         regs->eflags &= ~0xCD5;
 223         regs->eflags |= context.eflags & 0xCD5;
 224         regs->orig_eax = -1;            /* disable syscall checks */
 225         return context.eax;
 226 badframe:
 227         do_exit(SIGSEGV);
 228 }
 229 
 230 /*
 231  * Set up a signal frame... Make the stack look the way iBCS2 expects
 232  * it to look.
 233  */
 234 static void setup_frame(struct sigaction * sa, unsigned long ** fp, unsigned long eip,
     /* [previous][next][first][last][top][bottom][index][help] */
 235         struct pt_regs * regs, int signr, unsigned long oldmask)
 236 {
 237         unsigned long * frame;
 238 
 239 #define __CODE ((unsigned long)(frame+24))
 240 #define CODE(x) ((unsigned long *) ((x)+__CODE))
 241         frame = *fp;
 242         if (regs->ss != USER_DS)
 243                 frame = (unsigned long *) sa->sa_restorer;
 244         frame -= 32;
 245         if (verify_area(VERIFY_WRITE,frame,32*4))
 246                 do_exit(SIGSEGV);
 247 /* set up the "normal" stack seen by the signal handler (iBCS2) */
 248         put_fs_long(__CODE,frame);
 249         put_fs_long(current->signal_invmap[signr], frame+1);
 250         put_fs_long(regs->gs, frame+2);
 251         put_fs_long(regs->fs, frame+3);
 252         put_fs_long(regs->es, frame+4);
 253         put_fs_long(regs->ds, frame+5);
 254         put_fs_long(regs->edi, frame+6);
 255         put_fs_long(regs->esi, frame+7);
 256         put_fs_long(regs->ebp, frame+8);
 257         put_fs_long((long)*fp, frame+9);
 258         put_fs_long(regs->ebx, frame+10);
 259         put_fs_long(regs->edx, frame+11);
 260         put_fs_long(regs->ecx, frame+12);
 261         put_fs_long(regs->eax, frame+13);
 262         put_fs_long(current->tss.trap_no, frame+14);
 263         put_fs_long(current->tss.error_code, frame+15);
 264         put_fs_long(eip, frame+16);
 265         put_fs_long(regs->cs, frame+17);
 266         put_fs_long(regs->eflags, frame+18);
 267         put_fs_long(regs->esp, frame+19);
 268         put_fs_long(regs->ss, frame+20);
 269         put_fs_long(0,frame+21);                /* 387 state pointer - not implemented*/
 270 /* non-iBCS2 extensions.. */
 271         put_fs_long(oldmask, frame+22);
 272         put_fs_long(current->tss.cr2, frame+23);
 273 /* set up the return code... */
 274         put_fs_long(0x0000b858, CODE(0));       /* popl %eax ; movl $,%eax */
 275         put_fs_long(0x80cd0000, CODE(4));       /* int $0x80 */
 276         put_fs_long(__NR_sigreturn, CODE(2));
 277         *fp = frame;
 278 #undef __CODE
 279 #undef CODE
 280 }
 281 
 282 /*
 283  * Note that 'init' is a special process: it doesn't get signals it doesn't
 284  * want to handle. Thus you cannot kill init even with a SIGKILL even by
 285  * mistake.
 286  *
 287  * Note that we go through the signals twice: once to check the signals that
 288  * the kernel can handle, and then we build all the user-level signal handling
 289  * stack-frames in one go after that.
 290  */
 291 asmlinkage int do_signal(unsigned long oldmask, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 292 {
 293         unsigned long mask = ~current->blocked;
 294         unsigned long handler_signal = 0;
 295         unsigned long *frame = NULL;
 296         unsigned long eip = 0;
 297         unsigned long signr;
 298         struct sigaction * sa;
 299 
 300         while ((signr = current->signal & mask)) {
 301                 __asm__("bsf %2,%1\n\t"
 302                         "btrl %1,%0"
 303                         :"=m" (current->signal),"=r" (signr)
 304                         :"1" (signr));
 305                 sa = current->sigaction + signr;
 306                 signr++;
 307                 if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
 308                         current->exit_code = signr;
 309                         current->state = TASK_STOPPED;
 310                         notify_parent(current);
 311                         schedule();
 312                         if (!(signr = current->exit_code))
 313                                 continue;
 314                         current->exit_code = 0;
 315                         if (signr == SIGSTOP)
 316                                 continue;
 317                         if (_S(signr) & current->blocked) {
 318                                 current->signal |= _S(signr);
 319                                 continue;
 320                         }
 321                         sa = current->sigaction + signr - 1;
 322                 }
 323                 if (sa->sa_handler == SIG_IGN) {
 324                         if (signr != SIGCHLD)
 325                                 continue;
 326                         /* check for SIGCHLD: it's special */
 327                         while (sys_waitpid(-1,NULL,WNOHANG) > 0)
 328                                 /* nothing */;
 329                         continue;
 330                 }
 331                 if (sa->sa_handler == SIG_DFL) {
 332                         if (current->pid == 1)
 333                                 continue;
 334                         switch (signr) {
 335                         case SIGCONT: case SIGCHLD: case SIGWINCH:
 336                                 continue;
 337 
 338                         case SIGSTOP: case SIGTSTP: case SIGTTIN: case SIGTTOU:
 339                                 if (current->flags & PF_PTRACED)
 340                                         continue;
 341                                 current->state = TASK_STOPPED;
 342                                 current->exit_code = signr;
 343                                 if (!(current->p_pptr->sigaction[SIGCHLD-1].sa_flags & 
 344                                                 SA_NOCLDSTOP))
 345                                         notify_parent(current);
 346                                 schedule();
 347                                 continue;
 348 
 349                         case SIGQUIT: case SIGILL: case SIGTRAP:
 350                         case SIGIOT: case SIGFPE: case SIGSEGV:
 351                                 if (core_dump(signr,regs))
 352                                         signr |= 0x80;
 353                                 /* fall through */
 354                         default:
 355                                 current->signal |= _S(signr & 0x7f);
 356                                 do_exit(signr);
 357                         }
 358                 }
 359                 /*
 360                  * OK, we're invoking a handler
 361                  */
 362                 if (regs->orig_eax >= 0) {
 363                         if (regs->eax == -ERESTARTNOHAND ||
 364                            (regs->eax == -ERESTARTSYS && !(sa->sa_flags & SA_RESTART)))
 365                                 regs->eax = -EINTR;
 366                 }
 367                 handler_signal |= 1 << (signr-1);
 368                 mask &= ~sa->sa_mask;
 369         }
 370         if (regs->orig_eax >= 0 &&
 371             (regs->eax == -ERESTARTNOHAND ||
 372              regs->eax == -ERESTARTSYS ||
 373              regs->eax == -ERESTARTNOINTR)) {
 374                 regs->eax = regs->orig_eax;
 375                 regs->eip -= 2;
 376         }
 377         if (!handler_signal)            /* no handler will be called - return 0 */
 378                 return 0;
 379         eip = regs->eip;
 380         frame = (unsigned long *) regs->esp;
 381         signr = 1;
 382         sa = current->sigaction;
 383         for (mask = 1 ; mask ; sa++,signr++,mask += mask) {
 384                 if (mask > handler_signal)
 385                         break;
 386                 if (!(mask & handler_signal))
 387                         continue;
 388                 setup_frame(sa,&frame,eip,regs,signr,oldmask);
 389                 eip = (unsigned long) sa->sa_handler;
 390                 if (sa->sa_flags & SA_ONESHOT)
 391                         sa->sa_handler = NULL;
 392 /* force a supervisor-mode page-in of the signal handler to reduce races */
 393                 __asm__("testb $0,%%fs:%0": :"m" (*(char *) eip));
 394                 regs->cs = USER_CS; regs->ss = USER_DS;
 395                 regs->ds = USER_DS; regs->es = USER_DS;
 396                 regs->gs = USER_DS; regs->fs = USER_DS;
 397                 current->blocked |= sa->sa_mask;
 398                 oldmask |= sa->sa_mask;
 399         }
 400         regs->esp = (unsigned long) frame;
 401         regs->eip = eip;                /* "return" to the first handler */
 402         current->tss.trap_no = current->tss.error_code = 0;
 403         return 1;
 404 }

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