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 CHECK_SEG(x) if (x) x |= 3
 215 #define COPY(x) regs->x = context.x
 216         struct sigcontext_struct context;
 217         struct pt_regs * regs;
 218 
 219         regs = (struct pt_regs *) &__unused;
 220         memcpy_fromfs(&context,(void *) regs->esp, sizeof(context));
 221         current->blocked = context.oldmask & _BLOCKABLE;
 222         CHECK_SEG(context.ss);
 223         CHECK_SEG(context.cs);
 224         CHECK_SEG(context.ds);
 225         CHECK_SEG(context.es);
 226         CHECK_SEG(context.fs);
 227         CHECK_SEG(context.gs);
 228         COPY(eip); COPY(eflags);
 229         COPY(ecx); COPY(edx);
 230         COPY(ebx);
 231         COPY(esp); COPY(ebp);
 232         COPY(edi); COPY(esi);
 233         COPY(cs); COPY(ss);
 234         COPY(ds); COPY(es);
 235         COPY(fs); COPY(gs);
 236         regs->orig_eax = -1;            /* disable syscall checks */
 237         return context.eax;
 238 }
 239 
 240 /*
 241  * Set up a signal frame... Make the stack look the way iBCS2 expects
 242  * it to look.
 243  */
 244 static void setup_frame(struct sigaction * sa, unsigned long ** fp, unsigned long eip,
     /* [previous][next][first][last][top][bottom][index][help] */
 245         struct pt_regs * regs, int signr, unsigned long oldmask)
 246 {
 247         unsigned long * frame;
 248 
 249 #define __CODE ((unsigned long)(frame+24))
 250 #define CODE(x) ((unsigned long *) ((x)+__CODE))
 251         frame = *fp;
 252         if (regs->ss != USER_DS)
 253                 frame = (unsigned long *) sa->sa_restorer;
 254         frame -= 32;
 255         verify_area(VERIFY_WRITE,frame,32*4);
 256 /* set up the "normal" stack seen by the signal handler (iBCS2) */
 257         put_fs_long(__CODE,frame);
 258         put_fs_long(signr, frame+1);
 259         put_fs_long(regs->gs, frame+2);
 260         put_fs_long(regs->fs, frame+3);
 261         put_fs_long(regs->es, frame+4);
 262         put_fs_long(regs->ds, frame+5);
 263         put_fs_long(regs->edi, frame+6);
 264         put_fs_long(regs->esi, frame+7);
 265         put_fs_long(regs->ebp, frame+8);
 266         put_fs_long((long)*fp, frame+9);
 267         put_fs_long(regs->ebx, frame+10);
 268         put_fs_long(regs->edx, frame+11);
 269         put_fs_long(regs->ecx, frame+12);
 270         put_fs_long(regs->eax, frame+13);
 271         put_fs_long(0, frame+14);               /* trapno - not implemented */
 272         put_fs_long(0, frame+15);               /* err - not implemented */
 273         put_fs_long(eip, frame+16);
 274         put_fs_long(regs->cs, frame+17);
 275         put_fs_long(regs->eflags, frame+18);
 276         put_fs_long(regs->esp, frame+19);
 277         put_fs_long(regs->ss, frame+20);
 278         put_fs_long(0,frame+21);                /* 387 state pointer - not implemented*/
 279 /* non-iBCS2 extensions.. */
 280         put_fs_long(oldmask, frame+22);
 281         put_fs_long(current->tss.cr2, frame+23);
 282 /* set up the return code... */
 283         put_fs_long(0x0000b858, CODE(0));       /* popl %eax ; movl $,%eax */
 284         put_fs_long(0x80cd0000, CODE(4));       /* int $0x80 */
 285         put_fs_long(__NR_sigreturn, CODE(2));
 286         *fp = frame;
 287 #undef __CODE
 288 #undef CODE
 289 }
 290 
 291 /*
 292  * Note that 'init' is a special process: it doesn't get signals it doesn't
 293  * want to handle. Thus you cannot kill init even with a SIGKILL even by
 294  * mistake.
 295  *
 296  * Note that we go through the signals twice: once to check the signals that
 297  * the kernel can handle, and then we build all the user-level signal handling
 298  * stack-frames in one go after that.
 299  */
 300 asmlinkage int do_signal(unsigned long oldmask, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 301 {
 302         unsigned long mask = ~current->blocked;
 303         unsigned long handler_signal = 0;
 304         unsigned long *frame = NULL;
 305         unsigned long eip = 0;
 306         unsigned long signr;
 307         struct sigaction * sa;
 308 
 309         while ((signr = current->signal & mask)) {
 310                 __asm__("bsf %2,%1\n\t"
 311                         "btrl %1,%0"
 312                         :"=m" (current->signal),"=r" (signr)
 313                         :"1" (signr));
 314                 sa = current->sigaction + signr;
 315                 signr++;
 316                 if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
 317                         current->exit_code = signr;
 318                         current->state = TASK_STOPPED;
 319                         notify_parent(current);
 320                         schedule();
 321                         if (!(signr = current->exit_code))
 322                                 continue;
 323                         current->exit_code = 0;
 324                         if (signr == SIGSTOP)
 325                                 continue;
 326                         if (_S(signr) & current->blocked) {
 327                                 current->signal |= _S(signr);
 328                                 continue;
 329                         }
 330                         sa = current->sigaction + signr - 1;
 331                 }
 332                 if (sa->sa_handler == SIG_IGN) {
 333                         if (signr != SIGCHLD)
 334                                 continue;
 335                         /* check for SIGCHLD: it's special */
 336                         while (sys_waitpid(-1,NULL,WNOHANG) > 0)
 337                                 /* nothing */;
 338                         continue;
 339                 }
 340                 if (sa->sa_handler == SIG_DFL) {
 341                         if (current->pid == 1)
 342                                 continue;
 343                         switch (signr) {
 344                         case SIGCONT: case SIGCHLD: case SIGWINCH:
 345                                 continue;
 346 
 347                         case SIGSTOP: case SIGTSTP: case SIGTTIN: case SIGTTOU:
 348                                 if (current->flags & PF_PTRACED)
 349                                         continue;
 350                                 current->state = TASK_STOPPED;
 351                                 current->exit_code = signr;
 352                                 if (!(current->p_pptr->sigaction[SIGCHLD-1].sa_flags & 
 353                                                 SA_NOCLDSTOP))
 354                                         notify_parent(current);
 355                                 schedule();
 356                                 continue;
 357 
 358                         case SIGQUIT: case SIGILL: case SIGTRAP:
 359                         case SIGIOT: case SIGFPE: case SIGSEGV:
 360                                 if (core_dump(signr,regs))
 361                                         signr |= 0x80;
 362                                 /* fall through */
 363                         default:
 364                                 current->signal |= _S(signr & 0x7f);
 365                                 do_exit(signr);
 366                         }
 367                 }
 368                 /*
 369                  * OK, we're invoking a handler
 370                  */
 371                 if (regs->orig_eax >= 0) {
 372                         if (regs->eax == -ERESTARTNOHAND ||
 373                            (regs->eax == -ERESTARTSYS && !(sa->sa_flags & SA_RESTART)))
 374                                 regs->eax = -EINTR;
 375                 }
 376                 handler_signal |= 1 << (signr-1);
 377                 mask &= ~sa->sa_mask;
 378         }
 379         if (regs->orig_eax >= 0 &&
 380             (regs->eax == -ERESTARTNOHAND ||
 381              regs->eax == -ERESTARTSYS ||
 382              regs->eax == -ERESTARTNOINTR)) {
 383                 regs->eax = regs->orig_eax;
 384                 regs->eip -= 2;
 385         }
 386         if (!handler_signal)            /* no handler will be called - return 0 */
 387                 return 0;
 388         eip = regs->eip;
 389         frame = (unsigned long *) regs->esp;
 390         signr = 1;
 391         sa = current->sigaction;
 392         for (mask = 1 ; mask ; sa++,signr++,mask += mask) {
 393                 if (mask > handler_signal)
 394                         break;
 395                 if (!(mask & handler_signal))
 396                         continue;
 397                 setup_frame(sa,&frame,eip,regs,signr,oldmask);
 398                 eip = (unsigned long) sa->sa_handler;
 399                 if (sa->sa_flags & SA_ONESHOT)
 400                         sa->sa_handler = NULL;
 401 /* force a supervisor-mode page-in of the signal handler to reduce races */
 402                 __asm__("testb $0,%%fs:%0": :"m" (*(char *) eip));
 403                 regs->cs = USER_CS; regs->ss = USER_DS;
 404                 regs->ds = USER_DS; regs->es = USER_DS;
 405                 regs->gs = USER_DS; regs->fs = USER_DS;
 406                 current->blocked |= sa->sa_mask;
 407                 oldmask |= sa->sa_mask;
 408         }
 409         regs->esp = (unsigned long) frame;
 410         regs->eip = eip;                        /* "return" to the first handler */
 411         return 1;
 412 }

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