root/kernel/exit.c

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

DEFINITIONS

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

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