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

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