4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/smp_lock.h>
11 #include <linux/module.h>
12 #include <linux/completion.h>
13 #include <linux/personality.h>
14 #include <linux/tty.h>
15 #include <linux/namespace.h>
16 #ifdef CONFIG_BSD_PROCESS_ACCT
17 #include <linux/acct.h>
20 #include <asm/uaccess.h>
21 #include <asm/pgtable.h>
22 #include <asm/mmu_context.h>
24 extern void sem_exit (void);
25 extern struct task_struct *child_reaper;
27 int getrusage(struct task_struct *, int, struct rusage *);
29 static void release_task(struct task_struct * p)
34 * Wait to make sure the process isn't on the
35 * runqueue (active on some other CPU still)
45 } while (task_has_cpu(p));
49 atomic_dec(&p->user->processes);
54 current->cmin_flt += p->min_flt + p->cmin_flt;
55 current->cmaj_flt += p->maj_flt + p->cmaj_flt;
56 current->cnswap += p->nswap + p->cnswap;
58 * Potentially available timeslices are retrieved
59 * here - this way the parent does not get penalized
60 * for creating too many processes.
62 * (this cannot be used to artificially 'generate'
63 * timeslices, because any timeslice recovered here
64 * was given away by the parent in the first place.)
66 current->counter += p->counter;
67 if (current->counter >= MAX_COUNTER)
68 current->counter = MAX_COUNTER;
72 printk("task releasing itself\n");
77 * This checks not only the pgrp, but falls back on the pid if no
78 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
81 int session_of_pgrp(int pgrp)
83 struct task_struct *p;
87 read_lock(&tasklist_lock);
91 if (p->pgrp == pgrp) {
92 fallback = p->session;
96 fallback = p->session;
98 read_unlock(&tasklist_lock);
103 * Determine if a process group is "orphaned", according to the POSIX
104 * definition in 2.2.2.52. Orphaned process groups are not to be affected
105 * by terminal-generated stop signals. Newly orphaned process groups are
106 * to receive a SIGHUP and a SIGCONT.
108 * "I ask you, have you ever known what it is to be an orphan?"
110 static int will_become_orphaned_pgrp(int pgrp, struct task_struct * ignored_task)
112 struct task_struct *p;
114 read_lock(&tasklist_lock);
116 if ((p == ignored_task) || (p->pgrp != pgrp) ||
117 (p->state == TASK_ZOMBIE) ||
118 (p->p_pptr->pid == 1))
120 if ((p->p_pptr->pgrp != pgrp) &&
121 (p->p_pptr->session == p->session)) {
122 read_unlock(&tasklist_lock);
126 read_unlock(&tasklist_lock);
127 return 1; /* (sighing) "Often!" */
130 int is_orphaned_pgrp(int pgrp)
132 return will_become_orphaned_pgrp(pgrp, 0);
135 static inline int has_stopped_jobs(int pgrp)
138 struct task_struct * p;
140 read_lock(&tasklist_lock);
144 if (p->state != TASK_STOPPED)
149 read_unlock(&tasklist_lock);
154 * When we die, we re-parent all our children.
155 * Try to give them to another thread in our thread
156 * group, and if no such member exists, give it to
157 * the global child reaper process (ie "init")
159 static inline void forget_original_parent(struct task_struct * father)
161 struct task_struct * p;
163 read_lock(&tasklist_lock);
166 if (p->p_opptr == father) {
167 /* We dont want people slaying init */
168 p->exit_signal = SIGCHLD;
171 /* Make sure we're not reparenting to ourselves */
172 p->p_opptr = child_reaper;
174 if (p->pdeath_signal) send_sig(p->pdeath_signal, p, 0);
177 read_unlock(&tasklist_lock);
180 static inline void close_files(struct files_struct * files)
188 if (i >= files->max_fdset || i >= files->max_fds)
190 set = files->open_fds->fds_bits[j++];
193 struct file * file = xchg(&files->fd[i], NULL);
195 filp_close(file, files);
203 void put_files_struct(struct files_struct *files)
205 if (atomic_dec_and_test(&files->count)) {
208 * Free the fd and fdset arrays if we expanded them.
210 if (files->fd != &files->fd_array[0])
211 free_fd_array(files->fd, files->max_fds);
212 if (files->max_fdset > __FD_SETSIZE) {
213 free_fdset(files->open_fds, files->max_fdset);
214 free_fdset(files->close_on_exec, files->max_fdset);
216 kmem_cache_free(files_cachep, files);
220 static inline void __exit_files(struct task_struct *tsk)
222 struct files_struct * files = tsk->files;
228 put_files_struct(files);
232 void exit_files(struct task_struct *tsk)
237 static inline void __put_fs_struct(struct fs_struct *fs)
239 /* No need to hold fs->lock if we are killing it */
240 if (atomic_dec_and_test(&fs->count)) {
247 mntput(fs->altrootmnt);
249 kmem_cache_free(fs_cachep, fs);
253 void put_fs_struct(struct fs_struct *fs)
258 static inline void __exit_fs(struct task_struct *tsk)
260 struct fs_struct * fs = tsk->fs;
270 void exit_fs(struct task_struct *tsk)
276 * We can use these to temporarily drop into
277 * "lazy TLB" mode and back.
279 struct mm_struct * start_lazy_tlb(void)
281 struct mm_struct *mm = current->mm;
283 /* active_mm is still 'mm' */
284 atomic_inc(&mm->mm_count);
285 enter_lazy_tlb(mm, current, smp_processor_id());
289 void end_lazy_tlb(struct mm_struct *mm)
291 struct mm_struct *active_mm = current->active_mm;
294 if (mm != active_mm) {
295 current->active_mm = mm;
296 activate_mm(active_mm, mm);
302 * Turn us into a lazy TLB process if we
305 static inline void __exit_mm(struct task_struct * tsk)
307 struct mm_struct * mm = tsk->mm;
311 atomic_inc(&mm->mm_count);
312 BUG_ON(mm != tsk->active_mm);
313 /* more a memory barrier than a real lock */
317 enter_lazy_tlb(mm, current, smp_processor_id());
322 void exit_mm(struct task_struct *tsk)
328 * Send signals to all our closest relatives so that they know
329 * to properly mourn us..
331 static void exit_notify(void)
333 struct task_struct * p, *t;
335 forget_original_parent(current);
337 * Check to see if any process groups have become orphaned
338 * as a result of our exiting, and if they have any stopped
339 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
341 * Case i: Our father is in a different pgrp than we are
342 * and we were the only connection outside, so our pgrp
343 * is about to become orphaned.
348 if ((t->pgrp != current->pgrp) &&
349 (t->session == current->session) &&
350 will_become_orphaned_pgrp(current->pgrp, current) &&
351 has_stopped_jobs(current->pgrp)) {
352 kill_pg(current->pgrp,SIGHUP,1);
353 kill_pg(current->pgrp,SIGCONT,1);
356 /* Let father know we died
358 * Thread signals are configurable, but you aren't going to use
359 * that to send signals to arbitary processes.
360 * That stops right now.
362 * If the parent exec id doesn't match the exec id we saved
363 * when we started then we know the parent has changed security
366 * If our self_exec id doesn't match our parent_exec_id then
367 * we have changed execution domain as these two values started
368 * the same after a fork.
372 if(current->exit_signal != SIGCHLD &&
373 ( current->parent_exec_id != t->self_exec_id ||
374 current->self_exec_id != current->parent_exec_id)
375 && !capable(CAP_KILL))
376 current->exit_signal = SIGCHLD;
380 * This loop does two things:
382 * A. Make init inherit all the child processes
383 * B. Check to see if any process groups have become orphaned
384 * as a result of our exiting, and if they have any stopped
385 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
388 write_lock_irq(&tasklist_lock);
389 current->state = TASK_ZOMBIE;
390 do_notify_parent(current, current->exit_signal);
391 while (current->p_cptr != NULL) {
393 current->p_cptr = p->p_osptr;
397 p->p_pptr = p->p_opptr;
398 p->p_osptr = p->p_pptr->p_cptr;
400 p->p_osptr->p_ysptr = p;
401 p->p_pptr->p_cptr = p;
402 if (p->state == TASK_ZOMBIE)
403 do_notify_parent(p, p->exit_signal);
405 * process group orphan check
406 * Case ii: Our child is in a different pgrp
407 * than we are, and it was the only connection
408 * outside, so the child pgrp is now orphaned.
410 if ((p->pgrp != current->pgrp) &&
411 (p->session == current->session)) {
414 write_unlock_irq(&tasklist_lock);
415 if (is_orphaned_pgrp(pgrp) && has_stopped_jobs(pgrp)) {
416 kill_pg(pgrp,SIGHUP,1);
417 kill_pg(pgrp,SIGCONT,1);
419 write_lock_irq(&tasklist_lock);
422 write_unlock_irq(&tasklist_lock);
425 NORET_TYPE void do_exit(long code)
427 struct task_struct *tsk = current;
430 panic("Aiee, killing interrupt handler!");
432 panic("Attempted to kill the idle task!");
434 panic("Attempted to kill init!");
435 tsk->flags |= PF_EXITING;
436 del_timer_sync(&tsk->real_timer);
439 #ifdef CONFIG_BSD_PROCESS_ACCT
453 disassociate_ctty(1);
455 put_exec_domain(tsk->exec_domain);
456 if (tsk->binfmt && tsk->binfmt->module)
457 __MOD_DEC_USE_COUNT(tsk->binfmt->module);
459 tsk->exit_code = code;
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
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.
479 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
487 asmlinkage long sys_exit(int error_code)
489 do_exit((error_code&0xff)<<8);
492 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
495 DECLARE_WAITQUEUE(wait, current);
496 struct task_struct *tsk;
498 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
501 add_wait_queue(¤t->wait_chldexit,&wait);
504 current->state = TASK_INTERRUPTIBLE;
505 read_lock(&tasklist_lock);
508 struct task_struct *p;
509 for (p = tsk->p_cptr ; p ; p = p->p_osptr) {
514 if (p->pgrp != current->pgrp)
516 } else if (pid != -1) {
520 /* Wait for all children (clone and not) if __WALL is set;
521 * otherwise, wait for clone children *only* if __WCLONE is
522 * set; otherwise, wait for non-clone children *only*. (Note:
523 * A "clone" child here is one that reports to its parent
524 * using a signal other than SIGCHLD.) */
525 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
526 && !(options & __WALL))
533 if (!(options & WUNTRACED) && !(p->ptrace & PT_PTRACED))
535 read_unlock(&tasklist_lock);
536 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
537 if (!retval && stat_addr)
538 retval = put_user((p->exit_code << 8) | 0x7f, stat_addr);
545 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
546 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
547 read_unlock(&tasklist_lock);
548 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
549 if (!retval && stat_addr)
550 retval = put_user(p->exit_code, stat_addr);
554 if (p->p_opptr != p->p_pptr) {
555 write_lock_irq(&tasklist_lock);
557 p->p_pptr = p->p_opptr;
559 do_notify_parent(p, SIGCHLD);
560 write_unlock_irq(&tasklist_lock);
568 if (options & __WNOTHREAD)
570 tsk = next_thread(tsk);
571 } while (tsk != current);
572 read_unlock(&tasklist_lock);
575 if (options & WNOHANG)
577 retval = -ERESTARTSYS;
578 if (signal_pending(current))
585 current->state = TASK_RUNNING;
586 remove_wait_queue(¤t->wait_chldexit,&wait);
590 #if !defined(__alpha__) && !defined(__ia64__)
593 * sys_waitpid() remains for compatibility. waitpid() should be
594 * implemented by calling sys_wait4() from libc.a.
596 asmlinkage long sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
598 return sys_wait4(pid, stat_addr, options, NULL);