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

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