NORET_TYPE void do_exit(long code) { struct task_struct *tsk = current; if (in_interrupt()) printk("Aiee, killing interrupt handler\n"); if (!tsk->pid) panic("Attempted to kill the idle task!"); tsk->flags |= PF_EXITING; start_bh_atomic(); del_timer(&tsk->real_timer); end_bh_atomic(); lock_kernel(); fake_volatile: #ifdef CONFIG_BSD_PROCESS_ACCT acct_process(code); #endif sem_exit(); __exit_mm(tsk); #if CONFIG_AP1000 exit_msc(tsk); #endif __exit_files(tsk); __exit_fs(tsk); __exit_sighand(tsk); exit_thread(); tsk->state = TASK_ZOMBIE; tsk->exit_code = code; exit_notify(); #ifdef DEBUG_PROC_TREE audit_ptree(); #endif if (tsk->exec_domain && tsk->exec_domain->module) __MOD_DEC_USE_COUNT(tsk->exec_domain->module); if (tsk->binfmt && tsk->binfmt->module) __MOD_DEC_USE_COUNT(tsk->binfmt->module); schedule(); /* * In order to get rid of the "volatile function does return" message * I did this little loop that confuses gcc to think do_exit really * is volatile. In fact it's schedule() that is volatile in some * circumstances: when current->state = ZOMBIE, schedule() never * returns. * * In fact the natural way to do all this is to have the label and the * goto right after each other, but I put the fake_volatile label at * the start of the function just in case something /really/ bad * happens, and the schedule returns. This way we can try again. I'm * not paranoid: it's just that everybody is out to get me. */ goto fake_volatile; }
NORET_TYPE void do_exit(long code) { if (intr_count) { printk("Aiee, killing interrupt handler\n"); intr_count = 0; } fake_volatile: acct_process(code); current->flags |= PF_EXITING; del_timer(¤t->real_timer); sem_exit(); kerneld_exit(); __exit_mm(current); __exit_files(current); __exit_fs(current); __exit_sighand(current); exit_thread(); current->state = TASK_ZOMBIE; current->exit_code = code; exit_notify(); #ifdef DEBUG_PROC_TREE audit_ptree(); #endif if (current->exec_domain && current->exec_domain->use_count) (*current->exec_domain->use_count)--; if (current->binfmt && current->binfmt->use_count) (*current->binfmt->use_count)--; schedule(); /* * In order to get rid of the "volatile function does return" message * I did this little loop that confuses gcc to think do_exit really * is volatile. In fact it's schedule() that is volatile in some * circumstances: when current->state = ZOMBIE, schedule() never * returns. * * In fact the natural way to do all this is to have the label and the * goto right after each other, but I put the fake_volatile label at * the start of the function just in case something /really/ bad * happens, and the schedule returns. This way we can try again. I'm * not paranoid: it's just that everybody is out to get me. */ goto fake_volatile; }
int sys_wait4(pid_t pid,unsigned long * stat_addr, int options, struct rusage * ru) { int flag; struct task_struct *p; unsigned long oldblocked; if (stat_addr) { flag = verify_area(VERIFY_WRITE, stat_addr, 4); if (flag) return flag; } repeat: current->signal &= ~(1<<(SIGCHLD-1)); flag=0; for (p = current->p_cptr ; p ; p = p->p_osptr) { if (pid>0) { if (p->pid != pid) continue; } else if (!pid) { if (p->pgrp != current->pgrp) continue; } else if (pid != -1) { if (p->pgrp != -pid) continue; } switch (p->state) { case TASK_STOPPED: if (!p->exit_code) continue; if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED)) continue; if (stat_addr) put_fs_long((p->exit_code << 8) | 0x7f, stat_addr); p->exit_code = 0; if (ru != NULL) getrusage(p, RUSAGE_BOTH, ru); return p->pid; case TASK_ZOMBIE: current->cutime += p->utime + p->cutime; current->cstime += p->stime + p->cstime; current->cmin_flt += p->min_flt + p->cmin_flt; current->cmaj_flt += p->maj_flt + p->cmaj_flt; if (ru != NULL) getrusage(p, RUSAGE_BOTH, ru); flag = p->pid; if (stat_addr) put_fs_long(p->exit_code, stat_addr); if (p->p_opptr != p->p_pptr) { REMOVE_LINKS(p); p->p_pptr = p->p_opptr; SET_LINKS(p); send_sig(SIGCHLD,p->p_pptr,1); } else release(p); #ifdef DEBUG_PROC_TREE audit_ptree(); #endif return flag; default: flag=1; continue; } } if (flag) { if (options & WNOHANG) return 0; current->state=TASK_INTERRUPTIBLE; oldblocked = current->blocked; current->blocked &= ~(1<<(SIGCHLD-1)); schedule(); current->blocked = oldblocked; if (current->signal & ~(current->blocked | (1<<(SIGCHLD-1)))) return -ERESTARTSYS; else goto repeat; } return -ECHILD; }
volatile void do_exit(long code) { struct task_struct *p; int i; fake_volatile: free_page_tables(current); for (i=0 ; i<NR_OPEN ; i++) if (current->filp[i]) sys_close(i); forget_original_parent(current); iput(current->pwd); current->pwd = NULL; iput(current->root); current->root = NULL; iput(current->executable); current->executable = NULL; for (i=0; i < current->numlibraries; i++) { iput(current->libraries[i].library); current->libraries[i].library = NULL; } current->state = TASK_ZOMBIE; current->exit_code = code; current->rss = 0; /* * Check to see if any process groups have become orphaned * as a result of our exiting, and if they have any stopped * jobs, send them a SIGUP and then a SIGCONT. (POSIX 3.2.2.2) * * Case i: Our father is in a different pgrp than we are * and we were the only connection outside, so our pgrp * is about to become orphaned. */ if ((current->p_pptr->pgrp != current->pgrp) && (current->p_pptr->session == current->session) && is_orphaned_pgrp(current->pgrp) && has_stopped_jobs(current->pgrp)) { kill_pg(current->pgrp,SIGHUP,1); kill_pg(current->pgrp,SIGCONT,1); } /* Let father know we died */ send_sig (SIGCHLD, current->p_pptr, 1); /* * This loop does two things: * * A. Make init inherit all the child processes * B. Check to see if any process groups have become orphaned * as a result of our exiting, and if they have any stopped * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) */ while ((p = current->p_cptr) != NULL) { current->p_cptr = p->p_osptr; p->p_ysptr = NULL; p->flags &= ~(PF_PTRACED|PF_TRACESYS); if (task[1]) p->p_pptr = task[1]; else p->p_pptr = task[0]; p->p_osptr = p->p_pptr->p_cptr; p->p_osptr->p_ysptr = p; p->p_pptr->p_cptr = p; if (p->state == TASK_ZOMBIE) send_sig(SIGCHLD,p->p_pptr,1); /* * process group orphan check * Case ii: Our child is in a different pgrp * than we are, and it was the only connection * outside, so the child pgrp is now orphaned. */ if ((p->pgrp != current->pgrp) && (p->session == current->session) && is_orphaned_pgrp(p->pgrp) && has_stopped_jobs(p->pgrp)) { kill_pg(p->pgrp,SIGHUP,1); kill_pg(p->pgrp,SIGCONT,1); } } if (current->leader) { struct task_struct **p; struct tty_struct *tty; if (current->tty >= 0) { tty = TTY_TABLE(current->tty); if (tty) { if (tty->pgrp > 0) kill_pg(tty->pgrp, SIGHUP, 1); tty->pgrp = -1; tty->session = 0; } } for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) if (*p && (*p)->session == current->session) (*p)->tty = -1; } if (last_task_used_math == current) last_task_used_math = NULL; #ifdef DEBUG_PROC_TREE audit_ptree(); #endif schedule(); /* * In order to get rid of the "volatile function does return" message * I did this little loop that confuses gcc to think do_exit really * is volatile. In fact it's schedule() that is volatile in some * circumstances: when current->state = ZOMBIE, schedule() never * returns. * * In fact the natural way to do all this is to have the label and the * goto right after each other, but I put the fake_volatile label at * the start of the function just in case something /really/ bad * happens, and the schedule returns. This way we can try again. I'm * not paranoid: it's just that everybody is out to get me. */ goto fake_volatile; }
asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru) { int flag, retval; struct wait_queue wait = { current, NULL }; struct task_struct *p; if (options & ~(WNOHANG|WUNTRACED|__WCLONE)) return -EINVAL; add_wait_queue(¤t->wait_chldexit,&wait); repeat: flag = 0; /* The interruptible state must be set before looking at the children. This because we want to catch any racy exit from the children as do_exit() may run under us. The following read_lock will enforce SMP ordering at the CPU level. */ current->state = TASK_INTERRUPTIBLE; read_lock(&tasklist_lock); for (p = current->p_cptr ; p ; p = p->p_osptr) { if (pid>0) { if (p->pid != pid) continue; } else if (!pid) { if (p->pgrp != current->pgrp) continue; } else if (pid != -1) { if (p->pgrp != -pid) continue; } /* wait for cloned processes iff the __WCLONE flag is set */ if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0)) continue; flag = 1; switch (p->state) { case TASK_STOPPED: if (!p->exit_code) continue; if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED)) continue; read_unlock(&tasklist_lock); current->state = TASK_RUNNING; /* We *must* do this before touching userspace! */ retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; if (!retval && stat_addr) retval = put_user((p->exit_code << 8) | 0x7f, stat_addr); if (!retval) { p->exit_code = 0; retval = p->pid; } goto end_wait4; case TASK_ZOMBIE: current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime; current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime; read_unlock(&tasklist_lock); current->state = TASK_RUNNING; /* We *must* do this before touching userspace! */ retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; if (!retval && stat_addr) retval = put_user(p->exit_code, stat_addr); if (retval) goto end_wait4; retval = p->pid; if (p->p_opptr != p->p_pptr) { write_lock_irq(&tasklist_lock); REMOVE_LINKS(p); p->p_pptr = p->p_opptr; SET_LINKS(p); write_unlock_irq(&tasklist_lock); notify_parent(p, SIGCHLD); } else release(p); #ifdef DEBUG_PROC_TREE audit_ptree(); #endif goto end_wait4; default: continue; } } read_unlock(&tasklist_lock); if (flag) { retval = 0; if (options & WNOHANG) goto end_wait4; retval = -ERESTARTSYS; if (signal_pending(current)) goto end_wait4; schedule(); goto repeat; } retval = -ECHILD; end_wait4: remove_wait_queue(¤t->wait_chldexit,&wait); current->state = TASK_RUNNING; return retval; }
/* 如果成功则返回0 */ asmlinkage int sys_wait4(pid_t pid,unsigned long * stat_addr, int options, struct rusage * ru) { int flag, retval; struct wait_queue wait = { current, NULL }; struct task_struct *p; if (stat_addr) { flag = verify_area(VERIFY_WRITE, stat_addr, 4); if (flag) return flag; } add_wait_queue(¤t->wait_chldexit,&wait); repeat: flag=0; /* p_cptr表示最小的孩子进程,p_opptr表示老的兄弟进程 * 通过该循环可以知道从当前进程的最小进程开始一次向年长的进程开始扫描 */ for (p = current->p_cptr ; p ; p = p->p_osptr) { /* 如果pid>0表示等待某个具体的进程,等于0则表示进程组,小于0则表示所有子进程 */ if (pid>0) { if (p->pid != pid) continue; } else if (!pid) { if (p->pgrp != current->pgrp) continue; /* 如果是负数的话,则等待进程组号为-pid的所有子进程*/ } else if (pid != -1) { if (p->pgrp != -pid) continue; } /* wait for cloned processes iff the __WCLONE flag is set */ if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0)) continue; flag = 1; switch (p->state) { case TASK_STOPPED: if (!p->exit_code) continue; if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED)) continue; if (stat_addr) put_fs_long((p->exit_code << 8) | 0x7f, stat_addr); p->exit_code = 0; if (ru != NULL) getrusage(p, RUSAGE_BOTH, ru); retval = p->pid; goto end_wait4; case TASK_ZOMBIE: current->cutime += p->utime + p->cutime; current->cstime += p->stime + p->cstime; current->cmin_flt += p->min_flt + p->cmin_flt; current->cmaj_flt += p->maj_flt + p->cmaj_flt; if (ru != NULL) getrusage(p, RUSAGE_BOTH, ru); flag = p->pid; if (stat_addr) put_fs_long(p->exit_code, stat_addr); if (p->p_opptr != p->p_pptr) { REMOVE_LINKS(p); p->p_pptr = p->p_opptr; SET_LINKS(p); notify_parent(p); } else release(p); #ifdef DEBUG_PROC_TREE audit_ptree(); #endif retval = flag; goto end_wait4; default: continue; } } if (flag) { retval = 0; /* 如果子进程还在运行,并且选项标记为WNOHANG, * 则表示不等待,函数直接返回 */ if (options & WNOHANG) goto end_wait4; /* 设置进程为可中断状态,同时调用进程调度函数 */ current->state=TASK_INTERRUPTIBLE; schedule(); /* 设置进程收到SIGCHLD信号 */ current->signal &= ~(1<<(SIGCHLD-1)); retval = -ERESTARTSYS; /* 如果当前进程的所有信号都被阻塞了,也就是子进程退出时 * 给父进程发送的SIGCHLD信号也被阻塞了,则函数直接返回 */ if (current->signal & ~current->blocked) goto end_wait4; goto repeat; } retval = -ECHILD; end_wait4: remove_wait_queue(¤t->wait_chldexit,&wait); return retval; }
NORET_TYPE void do_exit(long code) { struct task_struct *p; int i; fake_volatile: if (current->semun) sem_exit(); if (current->shm) shm_exit(); free_page_tables(current); for (i=0 ; i<NR_OPEN ; i++) if (current->filp[i]) sys_close(i); forget_original_parent(current); iput(current->pwd); current->pwd = NULL; iput(current->root); current->root = NULL; iput(current->executable); current->executable = NULL; /* Release all of the old mmap stuff. */ { struct vm_area_struct * mpnt, *mpnt1; mpnt = current->mmap; current->mmap = NULL; while (mpnt) { mpnt1 = mpnt->vm_next; if (mpnt->vm_ops && mpnt->vm_ops->close) mpnt->vm_ops->close(mpnt); kfree(mpnt); mpnt = mpnt1; } } if (current->ldt) { vfree(current->ldt); current->ldt = NULL; for (i=1 ; i<NR_TASKS ; i++) { if (task[i] == current) { set_ldt_desc(gdt+(i<<1)+FIRST_LDT_ENTRY, &default_ldt, 1); load_ldt(i); } } } current->state = TASK_ZOMBIE; current->exit_code = code; current->rss = 0; /* * Check to see if any process groups have become orphaned * as a result of our exiting, and if they have any stopped * jobs, send them a SIGUP and then a SIGCONT. (POSIX 3.2.2.2) * * Case i: Our father is in a different pgrp than we are * and we were the only connection outside, so our pgrp * is about to become orphaned. */ if ((current->p_pptr->pgrp != current->pgrp) && (current->p_pptr->session == current->session) && is_orphaned_pgrp(current->pgrp) && has_stopped_jobs(current->pgrp)) { kill_pg(current->pgrp,SIGHUP,1); kill_pg(current->pgrp,SIGCONT,1); } /* Let father know we died */ /* 通知父进程 */ notify_parent(current); /* * This loop does two things: * * A. Make init inherit all the child processes * B. Check to see if any process groups have become orphaned * as a result of our exiting, and if they have any stopped * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) */ while ((p = current->p_cptr) != NULL) { current->p_cptr = p->p_osptr; p->p_ysptr = NULL; p->flags &= ~(PF_PTRACED|PF_TRACESYS); if (task[1] && task[1] != current) p->p_pptr = task[1]; else p->p_pptr = task[0]; p->p_osptr = p->p_pptr->p_cptr; p->p_osptr->p_ysptr = p; p->p_pptr->p_cptr = p; if (p->state == TASK_ZOMBIE) notify_parent(p); /* * process group orphan check * Case ii: Our child is in a different pgrp * than we are, and it was the only connection * outside, so the child pgrp is now orphaned. */ if ((p->pgrp != current->pgrp) && (p->session == current->session) && is_orphaned_pgrp(p->pgrp) && has_stopped_jobs(p->pgrp)) { kill_pg(p->pgrp,SIGHUP,1); kill_pg(p->pgrp,SIGCONT,1); } } if (current->leader) disassociate_ctty(1); if (last_task_used_math == current) last_task_used_math = NULL; #ifdef DEBUG_PROC_TREE audit_ptree(); #endif schedule(); /* * In order to get rid of the "volatile function does return" message * I did this little loop that confuses gcc to think do_exit really * is volatile. In fact it's schedule() that is volatile in some * circumstances: when current->state = ZOMBIE, schedule() never * returns. * * In fact the natural way to do all this is to have the label and the * goto right after each other, but I put the fake_volatile label at * the start of the function just in case something /really/ bad * happens, and the schedule returns. This way we can try again. I'm * not paranoid: it's just that everybody is out to get me. */ goto fake_volatile; }
asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru) { int flag, retval; struct wait_queue wait = { current, NULL }; struct task_struct *p; if (stat_addr) { flag = verify_area(VERIFY_WRITE, stat_addr, sizeof(*stat_addr)); if (flag) return flag; } if (ru) { flag = verify_area(VERIFY_WRITE, ru, sizeof(*ru)); if (flag) return flag; } if (options & ~(WNOHANG|WUNTRACED|__WCLONE)) return -EINVAL; add_wait_queue(¤t->wait_chldexit,&wait); repeat: flag=0; for (p = current->p_cptr ; p ; p = p->p_osptr) { if (pid>0) { if (p->pid != pid) continue; } else if (!pid) { if (p->pgrp != current->pgrp) continue; } else if (pid != -1) { if (p->pgrp != -pid) continue; } /* wait for cloned processes iff the __WCLONE flag is set */ if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0)) continue; flag = 1; switch (p->state) { case TASK_STOPPED: if (!p->exit_code) continue; if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED)) continue; if (ru != NULL) getrusage(p, RUSAGE_BOTH, ru); if (stat_addr) put_user((p->exit_code << 8) | 0x7f, stat_addr); p->exit_code = 0; retval = p->pid; goto end_wait4; case TASK_ZOMBIE: current->cutime += p->utime + p->cutime; current->cstime += p->stime + p->cstime; if (ru != NULL) getrusage(p, RUSAGE_BOTH, ru); if (stat_addr) put_user(p->exit_code, stat_addr); retval = p->pid; if (p->p_opptr != p->p_pptr) { REMOVE_LINKS(p); p->p_pptr = p->p_opptr; SET_LINKS(p); notify_parent(p); } else release(p); #ifdef DEBUG_PROC_TREE audit_ptree(); #endif goto end_wait4; default: continue; } } if (flag) { retval = 0; if (options & WNOHANG) goto end_wait4; retval = -ERESTARTSYS; if (current->signal & ~current->blocked) goto end_wait4; current->state=TASK_INTERRUPTIBLE; schedule(); goto repeat; } retval = -ECHILD; end_wait4: remove_wait_queue(¤t->wait_chldexit,&wait); return retval; }