root/kernel/exit.c

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

DEFINITIONS

This source file includes following definitions.
  1. generate
  2. send_sig
  3. notify_parent
  4. release
  5. bad_task_ptr
  6. audit_ptree
  7. session_of_pgrp
  8. kill_pg
  9. kill_sl
  10. kill_proc
  11. sys_kill
  12. is_orphaned_pgrp
  13. has_stopped_jobs
  14. forget_original_parent
  15. exit_mm
  16. exit_files
  17. exit_fs
  18. do_exit
  19. sys_exit
  20. sys_wait4
  21. sys_waitpid

   1 /*
   2  *  linux/kernel/exit.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #define DEBUG_PROC_TREE
   8 
   9 #include <linux/wait.h>
  10 #include <linux/errno.h>
  11 #include <linux/signal.h>
  12 #include <linux/sched.h>
  13 #include <linux/kernel.h>
  14 #include <linux/resource.h>
  15 #include <linux/mm.h>
  16 #include <linux/tty.h>
  17 #include <linux/malloc.h>
  18 
  19 #include <asm/segment.h>
  20 extern void sem_exit (void);
  21 
  22 int getrusage(struct task_struct *, int, struct rusage *);
  23 
  24 static int generate(unsigned long sig, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         unsigned long mask = 1 << (sig-1);
  27         struct sigaction * sa = sig + p->sigaction - 1;
  28 
  29         /* always generate signals for traced processes ??? */
  30         if (p->flags & PF_PTRACED) {
  31                 p->signal |= mask;
  32                 return 1;
  33         }
  34         /* don't bother with ignored signals (but SIGCHLD is special) */
  35         if (sa->sa_handler == SIG_IGN && sig != SIGCHLD)
  36                 return 0;
  37         /* some signals are ignored by default.. (but SIGCONT already did its deed) */
  38         if ((sa->sa_handler == SIG_DFL) &&
  39             (sig == SIGCONT || sig == SIGCHLD || sig == SIGWINCH))
  40                 return 0;
  41         p->signal |= mask;
  42         return 1;
  43 }
  44 
  45 int send_sig(unsigned long sig,struct task_struct * p,int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47         if (!p || sig > 32)
  48                 return -EINVAL;
  49         if (!priv && ((sig != SIGCONT) || (current->session != p->session)) &&
  50             (current->euid != p->euid) && (current->uid != p->uid) && !suser())
  51                 return -EPERM;
  52         if (!sig)
  53                 return 0;
  54         /*
  55          * Forget it if the process is already zombie'd.
  56          */
  57         if (p->state == TASK_ZOMBIE)
  58                 return 0;
  59         if ((sig == SIGKILL) || (sig == SIGCONT)) {
  60                 if (p->state == TASK_STOPPED)
  61                         p->state = TASK_RUNNING;
  62                 p->exit_code = 0;
  63                 p->signal &= ~( (1<<(SIGSTOP-1)) | (1<<(SIGTSTP-1)) |
  64                                 (1<<(SIGTTIN-1)) | (1<<(SIGTTOU-1)) );
  65         }
  66         /* Depends on order SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU */
  67         if ((sig >= SIGSTOP) && (sig <= SIGTTOU)) 
  68                 p->signal &= ~(1<<(SIGCONT-1));
  69         /* Actually generate the signal */
  70         generate(sig,p);
  71         return 0;
  72 }
  73 
  74 void notify_parent(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         if (tsk->p_pptr == task[1])
  77                 tsk->exit_signal = SIGCHLD;
  78         send_sig(tsk->exit_signal, tsk->p_pptr, 1);
  79         wake_up_interruptible(&tsk->p_pptr->wait_chldexit);
  80 }
  81 
  82 void release(struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84         int i;
  85 
  86         if (!p)
  87                 return;
  88         if (p == current) {
  89                 printk("task releasing itself\n");
  90                 return;
  91         }
  92         for (i=1 ; i<NR_TASKS ; i++)
  93                 if (task[i] == p) {
  94                         task[i] = NULL;
  95                         REMOVE_LINKS(p);
  96                         if (STACK_MAGIC != *(unsigned long *)p->kernel_stack_page)
  97                                 printk(KERN_ALERT "release: %s kernel stack corruption. Aiee\n", p->comm);
  98                         free_page(p->kernel_stack_page);
  99                         free_page((long) p);
 100                         return;
 101                 }
 102         panic("trying to release non-existent task");
 103 }
 104 
 105 #ifdef DEBUG_PROC_TREE
 106 /*
 107  * Check to see if a task_struct pointer is present in the task[] array
 108  * Return 0 if found, and 1 if not found.
 109  */
 110 int bad_task_ptr(struct task_struct *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112         int     i;
 113 
 114         if (!p)
 115                 return 0;
 116         for (i=0 ; i<NR_TASKS ; i++)
 117                 if (task[i] == p)
 118                         return 0;
 119         return 1;
 120 }
 121         
 122 /*
 123  * This routine scans the pid tree and makes sure the rep invariant still
 124  * holds.  Used for debugging only, since it's very slow....
 125  *
 126  * It looks a lot scarier than it really is.... we're doing nothing more
 127  * than verifying the doubly-linked list found in p_ysptr and p_osptr, 
 128  * and checking it corresponds with the process tree defined by p_cptr and 
 129  * p_pptr;
 130  */
 131 void audit_ptree(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         int     i;
 134 
 135         for (i=1 ; i<NR_TASKS ; i++) {
 136                 if (!task[i])
 137                         continue;
 138                 if (bad_task_ptr(task[i]->p_pptr))
 139                         printk("Warning, pid %d's parent link is bad\n",
 140                                 task[i]->pid);
 141                 if (bad_task_ptr(task[i]->p_cptr))
 142                         printk("Warning, pid %d's child link is bad\n",
 143                                 task[i]->pid);
 144                 if (bad_task_ptr(task[i]->p_ysptr))
 145                         printk("Warning, pid %d's ys link is bad\n",
 146                                 task[i]->pid);
 147                 if (bad_task_ptr(task[i]->p_osptr))
 148                         printk("Warning, pid %d's os link is bad\n",
 149                                 task[i]->pid);
 150                 if (task[i]->p_pptr == task[i])
 151                         printk("Warning, pid %d parent link points to self\n",
 152                                 task[i]->pid);
 153                 if (task[i]->p_cptr == task[i])
 154                         printk("Warning, pid %d child link points to self\n",
 155                                 task[i]->pid);
 156                 if (task[i]->p_ysptr == task[i])
 157                         printk("Warning, pid %d ys link points to self\n",
 158                                 task[i]->pid);
 159                 if (task[i]->p_osptr == task[i])
 160                         printk("Warning, pid %d os link points to self\n",
 161                                 task[i]->pid);
 162                 if (task[i]->p_osptr) {
 163                         if (task[i]->p_pptr != task[i]->p_osptr->p_pptr)
 164                                 printk(
 165                         "Warning, pid %d older sibling %d parent is %d\n",
 166                                 task[i]->pid, task[i]->p_osptr->pid,
 167                                 task[i]->p_osptr->p_pptr->pid);
 168                         if (task[i]->p_osptr->p_ysptr != task[i])
 169                                 printk(
 170                 "Warning, pid %d older sibling %d has mismatched ys link\n",
 171                                 task[i]->pid, task[i]->p_osptr->pid);
 172                 }
 173                 if (task[i]->p_ysptr) {
 174                         if (task[i]->p_pptr != task[i]->p_ysptr->p_pptr)
 175                                 printk(
 176                         "Warning, pid %d younger sibling %d parent is %d\n",
 177                                 task[i]->pid, task[i]->p_osptr->pid,
 178                                 task[i]->p_osptr->p_pptr->pid);
 179                         if (task[i]->p_ysptr->p_osptr != task[i])
 180                                 printk(
 181                 "Warning, pid %d younger sibling %d has mismatched os link\n",
 182                                 task[i]->pid, task[i]->p_ysptr->pid);
 183                 }
 184                 if (task[i]->p_cptr) {
 185                         if (task[i]->p_cptr->p_pptr != task[i])
 186                                 printk(
 187                         "Warning, pid %d youngest child %d has mismatched parent link\n",
 188                                 task[i]->pid, task[i]->p_cptr->pid);
 189                         if (task[i]->p_cptr->p_ysptr)
 190                                 printk(
 191                         "Warning, pid %d youngest child %d has non-NULL ys link\n",
 192                                 task[i]->pid, task[i]->p_cptr->pid);
 193                 }
 194         }
 195 }
 196 #endif /* DEBUG_PROC_TREE */
 197 
 198 /*
 199  * This checks not only the pgrp, but falls back on the pid if no
 200  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
 201  * without this...
 202  */
 203 int session_of_pgrp(int pgrp)
     /* [previous][next][first][last][top][bottom][index][help] */
 204 {
 205         struct task_struct *p;
 206         int fallback;
 207 
 208         fallback = -1;
 209         for_each_task(p) {
 210                 if (p->session <= 0)
 211                         continue;
 212                 if (p->pgrp == pgrp)
 213                         return p->session;
 214                 if (p->pid == pgrp)
 215                         fallback = p->session;
 216         }
 217         return fallback;
 218 }
 219 
 220 /*
 221  * kill_pg() sends a signal to a process group: this is what the tty
 222  * control characters do (^C, ^Z etc)
 223  */
 224 int kill_pg(int pgrp, int sig, int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226         struct task_struct *p;
 227         int err,retval = -ESRCH;
 228         int found = 0;
 229 
 230         if (sig<0 || sig>32 || pgrp<=0)
 231                 return -EINVAL;
 232         for_each_task(p) {
 233                 if (p->pgrp == pgrp) {
 234                         if ((err = send_sig(sig,p,priv)) != 0)
 235                                 retval = err;
 236                         else
 237                                 found++;
 238                 }
 239         }
 240         return(found ? 0 : retval);
 241 }
 242 
 243 /*
 244  * kill_sl() sends a signal to the session leader: this is used
 245  * to send SIGHUP to the controlling process of a terminal when
 246  * the connection is lost.
 247  */
 248 int kill_sl(int sess, int sig, int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 249 {
 250         struct task_struct *p;
 251         int err,retval = -ESRCH;
 252         int found = 0;
 253 
 254         if (sig<0 || sig>32 || sess<=0)
 255                 return -EINVAL;
 256         for_each_task(p) {
 257                 if (p->session == sess && p->leader) {
 258                         if ((err = send_sig(sig,p,priv)) != 0)
 259                                 retval = err;
 260                         else
 261                                 found++;
 262                 }
 263         }
 264         return(found ? 0 : retval);
 265 }
 266 
 267 int kill_proc(int pid, int sig, int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         struct task_struct *p;
 270 
 271         if (sig<0 || sig>32)
 272                 return -EINVAL;
 273         for_each_task(p) {
 274                 if (p && p->pid == pid)
 275                         return send_sig(sig,p,priv);
 276         }
 277         return(-ESRCH);
 278 }
 279 
 280 /*
 281  * POSIX specifies that kill(-1,sig) is unspecified, but what we have
 282  * is probably wrong.  Should make it like BSD or SYSV.
 283  */
 284 asmlinkage int sys_kill(int pid,int sig)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286         int err, retval = 0, count = 0;
 287 
 288         if (!pid)
 289                 return(kill_pg(current->pgrp,sig,0));
 290         if (pid == -1) {
 291                 struct task_struct * p;
 292                 for_each_task(p) {
 293                         if (p->pid > 1 && p != current) {
 294                                 ++count;
 295                                 if ((err = send_sig(sig,p,0)) != -EPERM)
 296                                         retval = err;
 297                         }
 298                 }
 299                 return(count ? retval : -ESRCH);
 300         }
 301         if (pid < 0) 
 302                 return(kill_pg(-pid,sig,0));
 303         /* Normal kill */
 304         return(kill_proc(pid,sig,0));
 305 }
 306 
 307 /*
 308  * Determine if a process group is "orphaned", according to the POSIX
 309  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
 310  * by terminal-generated stop signals.  Newly orphaned process groups are 
 311  * to receive a SIGHUP and a SIGCONT.
 312  * 
 313  * "I ask you, have you ever known what it is to be an orphan?"
 314  */
 315 int is_orphaned_pgrp(int pgrp)
     /* [previous][next][first][last][top][bottom][index][help] */
 316 {
 317         struct task_struct *p;
 318 
 319         for_each_task(p) {
 320                 if ((p->pgrp != pgrp) || 
 321                     (p->state == TASK_ZOMBIE) ||
 322                     (p->p_pptr->pid == 1))
 323                         continue;
 324                 if ((p->p_pptr->pgrp != pgrp) &&
 325                     (p->p_pptr->session == p->session))
 326                         return 0;
 327         }
 328         return(1);      /* (sighing) "Often!" */
 329 }
 330 
 331 static int has_stopped_jobs(int pgrp)
     /* [previous][next][first][last][top][bottom][index][help] */
 332 {
 333         struct task_struct * p;
 334 
 335         for_each_task(p) {
 336                 if (p->pgrp != pgrp)
 337                         continue;
 338                 if (p->state == TASK_STOPPED)
 339                         return(1);
 340         }
 341         return(0);
 342 }
 343 
 344 static void forget_original_parent(struct task_struct * father)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         struct task_struct * p;
 347 
 348         for_each_task(p) {
 349                 if (p->p_opptr == father)
 350                         if (task[1])
 351                                 p->p_opptr = task[1];
 352                         else
 353                                 p->p_opptr = task[0];
 354         }
 355 }
 356 
 357 static void exit_mm(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359         struct vm_area_struct * mpnt;
 360 
 361         mpnt = current->mm->mmap;
 362         current->mm->mmap = NULL;
 363         while (mpnt) {
 364                 struct vm_area_struct * next = mpnt->vm_next;
 365                 if (mpnt->vm_ops && mpnt->vm_ops->close)
 366                         mpnt->vm_ops->close(mpnt);
 367                 if (mpnt->vm_inode)
 368                         iput(mpnt->vm_inode);
 369                 kfree(mpnt);
 370                 mpnt = next;
 371         }
 372 
 373         /* forget local segments */
 374         __asm__ __volatile__("mov %w0,%%fs ; mov %w0,%%gs ; lldt %w0"
 375                 : /* no outputs */
 376                 : "r" (0));
 377         current->tss.ldt = 0;
 378         if (current->ldt) {
 379                 void * ldt = current->ldt;
 380                 current->ldt = NULL;
 381                 vfree(ldt);
 382         }
 383 
 384         free_page_tables(current);
 385 }
 386 
 387 static void exit_files(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 388 {
 389         int i;
 390 
 391         for (i=0 ; i<NR_OPEN ; i++)
 392                 if (current->files->fd[i])
 393                         sys_close(i);
 394 }
 395 
 396 static void exit_fs(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 397 {
 398         iput(current->fs->pwd);
 399         current->fs->pwd = NULL;
 400         iput(current->fs->root);
 401         current->fs->root = NULL;
 402 }
 403 
 404 NORET_TYPE void do_exit(long code)
     /* [previous][next][first][last][top][bottom][index][help] */
 405 {
 406         struct task_struct *p;
 407 
 408         if (intr_count) {
 409                 printk("Aiee, killing interrupt handler\n");
 410                 intr_count = 0;
 411         }
 412 fake_volatile:
 413         if (current->semundo)
 414                 sem_exit();
 415         exit_mm();
 416         exit_files();
 417         exit_fs();
 418         forget_original_parent(current);
 419         /* 
 420          * Check to see if any process groups have become orphaned
 421          * as a result of our exiting, and if they have any stopped
 422          * jobs, send them a SIGUP and then a SIGCONT.  (POSIX 3.2.2.2)
 423          *
 424          * Case i: Our father is in a different pgrp than we are
 425          * and we were the only connection outside, so our pgrp
 426          * is about to become orphaned.
 427          */
 428         if ((current->p_pptr->pgrp != current->pgrp) &&
 429             (current->p_pptr->session == current->session) &&
 430             is_orphaned_pgrp(current->pgrp) &&
 431             has_stopped_jobs(current->pgrp)) {
 432                 kill_pg(current->pgrp,SIGHUP,1);
 433                 kill_pg(current->pgrp,SIGCONT,1);
 434         }
 435         /* Let father know we died */
 436         notify_parent(current);
 437         
 438         /*
 439          * This loop does two things:
 440          * 
 441          * A.  Make init inherit all the child processes
 442          * B.  Check to see if any process groups have become orphaned
 443          *      as a result of our exiting, and if they have any stopped
 444          *      jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
 445          */
 446         while ((p = current->p_cptr) != NULL) {
 447                 current->p_cptr = p->p_osptr;
 448                 p->p_ysptr = NULL;
 449                 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 450                 if (task[1] && task[1] != current)
 451                         p->p_pptr = task[1];
 452                 else
 453                         p->p_pptr = task[0];
 454                 p->p_osptr = p->p_pptr->p_cptr;
 455                 p->p_osptr->p_ysptr = p;
 456                 p->p_pptr->p_cptr = p;
 457                 if (p->state == TASK_ZOMBIE)
 458                         notify_parent(p);
 459                 /*
 460                  * process group orphan check
 461                  * Case ii: Our child is in a different pgrp 
 462                  * than we are, and it was the only connection
 463                  * outside, so the child pgrp is now orphaned.
 464                  */
 465                 if ((p->pgrp != current->pgrp) &&
 466                     (p->session == current->session) &&
 467                     is_orphaned_pgrp(p->pgrp) &&
 468                     has_stopped_jobs(p->pgrp)) {
 469                         kill_pg(p->pgrp,SIGHUP,1);
 470                         kill_pg(p->pgrp,SIGCONT,1);
 471                 }
 472         }
 473         if (current->leader)
 474                 disassociate_ctty(1);
 475         if (last_task_used_math == current)
 476                 last_task_used_math = NULL;
 477         current->state = TASK_ZOMBIE;
 478         current->exit_code = code;
 479         current->mm->rss = 0;
 480 #ifdef DEBUG_PROC_TREE
 481         audit_ptree();
 482 #endif
 483         if (current->exec_domain && current->exec_domain->use_count)
 484                 (*current->exec_domain->use_count)--;
 485         if (current->binfmt && current->binfmt->use_count)
 486                 (*current->binfmt->use_count)--;
 487         schedule();
 488 /*
 489  * In order to get rid of the "volatile function does return" message
 490  * I did this little loop that confuses gcc to think do_exit really
 491  * is volatile. In fact it's schedule() that is volatile in some
 492  * circumstances: when current->state = ZOMBIE, schedule() never
 493  * returns.
 494  *
 495  * In fact the natural way to do all this is to have the label and the
 496  * goto right after each other, but I put the fake_volatile label at
 497  * the start of the function just in case something /really/ bad
 498  * happens, and the schedule returns. This way we can try again. I'm
 499  * not paranoid: it's just that everybody is out to get me.
 500  */
 501         goto fake_volatile;
 502 }
 503 
 504 asmlinkage int sys_exit(int error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 505 {
 506         do_exit((error_code&0xff)<<8);
 507 }
 508 
 509 asmlinkage int sys_wait4(pid_t pid,unsigned long * stat_addr, int options, struct rusage * ru)
     /* [previous][next][first][last][top][bottom][index][help] */
 510 {
 511         int flag, retval;
 512         struct wait_queue wait = { current, NULL };
 513         struct task_struct *p;
 514 
 515         if (stat_addr) {
 516                 flag = verify_area(VERIFY_WRITE, stat_addr, 4);
 517                 if (flag)
 518                         return flag;
 519         }
 520         add_wait_queue(&current->wait_chldexit,&wait);
 521 repeat:
 522         flag=0;
 523         for (p = current->p_cptr ; p ; p = p->p_osptr) {
 524                 if (pid>0) {
 525                         if (p->pid != pid)
 526                                 continue;
 527                 } else if (!pid) {
 528                         if (p->pgrp != current->pgrp)
 529                                 continue;
 530                 } else if (pid != -1) {
 531                         if (p->pgrp != -pid)
 532                                 continue;
 533                 }
 534                 /* wait for cloned processes iff the __WCLONE flag is set */
 535                 if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
 536                         continue;
 537                 flag = 1;
 538                 switch (p->state) {
 539                         case TASK_STOPPED:
 540                                 if (!p->exit_code)
 541                                         continue;
 542                                 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
 543                                         continue;
 544                                 if (stat_addr)
 545                                         put_fs_long((p->exit_code << 8) | 0x7f,
 546                                                 stat_addr);
 547                                 p->exit_code = 0;
 548                                 if (ru != NULL)
 549                                         getrusage(p, RUSAGE_BOTH, ru);
 550                                 retval = p->pid;
 551                                 goto end_wait4;
 552                         case TASK_ZOMBIE:
 553                                 current->cutime += p->utime + p->cutime;
 554                                 current->cstime += p->stime + p->cstime;
 555                                 current->mm->cmin_flt += p->mm->min_flt + p->mm->cmin_flt;
 556                                 current->mm->cmaj_flt += p->mm->maj_flt + p->mm->cmaj_flt;
 557                                 if (ru != NULL)
 558                                         getrusage(p, RUSAGE_BOTH, ru);
 559                                 flag = p->pid;
 560                                 if (stat_addr)
 561                                         put_fs_long(p->exit_code, stat_addr);
 562                                 if (p->p_opptr != p->p_pptr) {
 563                                         REMOVE_LINKS(p);
 564                                         p->p_pptr = p->p_opptr;
 565                                         SET_LINKS(p);
 566                                         notify_parent(p);
 567                                 } else
 568                                         release(p);
 569 #ifdef DEBUG_PROC_TREE
 570                                 audit_ptree();
 571 #endif
 572                                 retval = flag;
 573                                 goto end_wait4;
 574                         default:
 575                                 continue;
 576                 }
 577         }
 578         if (flag) {
 579                 retval = 0;
 580                 if (options & WNOHANG)
 581                         goto end_wait4;
 582                 current->state=TASK_INTERRUPTIBLE;
 583                 schedule();
 584                 current->signal &= ~(1<<(SIGCHLD-1));
 585                 retval = -ERESTARTSYS;
 586                 if (current->signal & ~current->blocked)
 587                         goto end_wait4;
 588                 goto repeat;
 589         }
 590         retval = -ECHILD;
 591 end_wait4:
 592         remove_wait_queue(&current->wait_chldexit,&wait);
 593         return retval;
 594 }
 595 
 596 /*
 597  * sys_waitpid() remains for compatibility. waitpid() should be
 598  * implemented by calling sys_wait4() from libc.a.
 599  */
 600 asmlinkage int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options)
     /* [previous][next][first][last][top][bottom][index][help] */
 601 {
 602         return sys_wait4(pid, stat_addr, options, NULL);
 603 }

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