4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/wait.h>
9 #include <linux/errno.h>
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/resource.h>
15 #include <linux/tty.h>
16 #include <linux/malloc.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #ifdef CONFIG_BSD_PROCESS_ACCT
24 #include <linux/acct.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/mmu_context.h>
31 extern void sem_exit (void);
33 int getrusage(struct task_struct
*, int, struct rusage
*);
35 static void release(struct task_struct
* p
)
40 * Wait to make sure the process isn't active on any
45 spin_lock(&scheduler_lock
);
47 spin_unlock(&scheduler_lock
);
57 add_free_taskslot(p
->tarray_ptr
);
59 write_lock_irq(&tasklist_lock
);
62 write_unlock_irq(&tasklist_lock
);
65 current
->cmin_flt
+= p
->min_flt
+ p
->cmin_flt
;
66 current
->cmaj_flt
+= p
->maj_flt
+ p
->cmaj_flt
;
67 current
->cnswap
+= p
->nswap
+ p
->cnswap
;
70 printk("task releasing itself\n");
75 * This checks not only the pgrp, but falls back on the pid if no
76 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
79 int session_of_pgrp(int pgrp
)
81 struct task_struct
*p
;
85 read_lock(&tasklist_lock
);
89 if (p
->pgrp
== pgrp
) {
90 fallback
= p
->session
;
94 fallback
= p
->session
;
96 read_unlock(&tasklist_lock
);
101 * Determine if a process group is "orphaned", according to the POSIX
102 * definition in 2.2.2.52. Orphaned process groups are not to be affected
103 * by terminal-generated stop signals. Newly orphaned process groups are
104 * to receive a SIGHUP and a SIGCONT.
106 * "I ask you, have you ever known what it is to be an orphan?"
108 static int will_become_orphaned_pgrp(int pgrp
, struct task_struct
* ignored_task
)
110 struct task_struct
*p
;
112 read_lock(&tasklist_lock
);
114 if ((p
== ignored_task
) || (p
->pgrp
!= pgrp
) ||
115 (p
->state
== TASK_ZOMBIE
) ||
116 (p
->p_pptr
->pid
== 1))
118 if ((p
->p_pptr
->pgrp
!= pgrp
) &&
119 (p
->p_pptr
->session
== p
->session
)) {
120 read_unlock(&tasklist_lock
);
124 read_unlock(&tasklist_lock
);
125 return 1; /* (sighing) "Often!" */
128 int is_orphaned_pgrp(int pgrp
)
130 return will_become_orphaned_pgrp(pgrp
, 0);
133 static inline int has_stopped_jobs(int pgrp
)
136 struct task_struct
* p
;
138 read_lock(&tasklist_lock
);
142 if (p
->state
!= TASK_STOPPED
)
147 read_unlock(&tasklist_lock
);
151 static inline void forget_original_parent(struct task_struct
* father
)
153 struct task_struct
* p
;
155 read_lock(&tasklist_lock
);
157 if (p
->p_opptr
== father
) {
158 p
->exit_signal
= SIGCHLD
;
159 p
->p_opptr
= task
[smp_num_cpus
] ? : task
[0]; /* init */
160 if (p
->pdeath_signal
) send_sig(p
->pdeath_signal
, p
, 0);
163 read_unlock(&tasklist_lock
);
166 static inline void close_files(struct files_struct
* files
)
172 unsigned long set
= files
->open_fds
.fds_bits
[j
];
175 if (i
>= files
->max_fds
)
179 struct file
* file
= files
->fd
[i
];
182 close_fp(file
, files
);
191 extern kmem_cache_t
*files_cachep
;
193 static inline void __exit_files(struct task_struct
*tsk
)
195 struct files_struct
* files
= tsk
->files
;
199 if (atomic_dec_and_test(&files
->count
)) {
202 * Free the fd array as appropriate ...
204 if (NR_OPEN
* sizeof(struct file
*) == PAGE_SIZE
)
205 free_page((unsigned long) files
->fd
);
208 kmem_cache_free(files_cachep
, files
);
213 void exit_files(struct task_struct
*tsk
)
218 static inline void __exit_fs(struct task_struct
*tsk
)
220 struct fs_struct
* fs
= tsk
->fs
;
224 if (atomic_dec_and_test(&fs
->count
)) {
232 void exit_fs(struct task_struct
*tsk
)
237 static inline void __exit_sighand(struct task_struct
*tsk
)
239 struct signal_struct
* sig
= tsk
->sig
;
244 spin_lock_irqsave(&tsk
->sigmask_lock
, flags
);
246 spin_unlock_irqrestore(&tsk
->sigmask_lock
, flags
);
247 if (atomic_dec_and_test(&sig
->count
))
254 void exit_sighand(struct task_struct
*tsk
)
259 static inline void __exit_mm(struct task_struct
* tsk
)
261 struct mm_struct
* mm
= tsk
->mm
;
263 /* Set us up to use the kernel mm state */
264 if (mm
!= &init_mm
) {
270 SET_PAGE_DIR(tsk
, swapper_pg_dir
);
275 void exit_mm(struct task_struct
*tsk
)
281 * Send signals to all our closest relatives so that they know
282 * to properly mourn us..
284 static void exit_notify(void)
286 struct task_struct
* p
;
288 forget_original_parent(current
);
290 * Check to see if any process groups have become orphaned
291 * as a result of our exiting, and if they have any stopped
292 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
294 * Case i: Our father is in a different pgrp than we are
295 * and we were the only connection outside, so our pgrp
296 * is about to become orphaned.
298 if ((current
->p_pptr
->pgrp
!= current
->pgrp
) &&
299 (current
->p_pptr
->session
== current
->session
) &&
300 will_become_orphaned_pgrp(current
->pgrp
, current
) &&
301 has_stopped_jobs(current
->pgrp
)) {
302 kill_pg(current
->pgrp
,SIGHUP
,1);
303 kill_pg(current
->pgrp
,SIGCONT
,1);
305 /* Let father know we died */
306 notify_parent(current
, current
->exit_signal
);
309 * This loop does two things:
311 * A. Make init inherit all the child processes
312 * B. Check to see if any process groups have become orphaned
313 * as a result of our exiting, and if they have any stopped
314 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
317 write_lock_irq(&tasklist_lock
);
318 while (current
->p_cptr
!= NULL
) {
320 current
->p_cptr
= p
->p_osptr
;
322 p
->flags
&= ~(PF_PTRACED
|PF_TRACESYS
);
324 p
->p_pptr
= p
->p_opptr
;
325 p
->p_osptr
= p
->p_pptr
->p_cptr
;
327 p
->p_osptr
->p_ysptr
= p
;
328 p
->p_pptr
->p_cptr
= p
;
329 if (p
->state
== TASK_ZOMBIE
)
330 notify_parent(p
, p
->exit_signal
);
332 * process group orphan check
333 * Case ii: Our child is in a different pgrp
334 * than we are, and it was the only connection
335 * outside, so the child pgrp is now orphaned.
337 if ((p
->pgrp
!= current
->pgrp
) &&
338 (p
->session
== current
->session
)) {
341 write_unlock_irq(&tasklist_lock
);
342 if (is_orphaned_pgrp(pgrp
) && has_stopped_jobs(pgrp
)) {
343 kill_pg(pgrp
,SIGHUP
,1);
344 kill_pg(pgrp
,SIGCONT
,1);
346 write_lock_irq(&tasklist_lock
);
349 write_unlock_irq(&tasklist_lock
);
352 disassociate_ctty(1);
355 NORET_TYPE
void do_exit(long code
)
357 struct task_struct
*tsk
= current
;
360 printk("Aiee, killing interrupt handler\n");
362 panic("Attempted to kill the idle task!");
363 tsk
->flags
|= PF_EXITING
;
364 del_timer(&tsk
->real_timer
);
368 #ifdef CONFIG_BSD_PROCESS_ACCT
380 tsk
->state
= TASK_ZOMBIE
;
381 tsk
->exit_code
= code
;
383 #ifdef DEBUG_PROC_TREE
386 if (tsk
->exec_domain
&& tsk
->exec_domain
->module
)
387 __MOD_DEC_USE_COUNT(tsk
->exec_domain
->module
);
388 if (tsk
->binfmt
&& tsk
->binfmt
->module
)
389 __MOD_DEC_USE_COUNT(tsk
->binfmt
->module
);
392 * In order to get rid of the "volatile function does return" message
393 * I did this little loop that confuses gcc to think do_exit really
394 * is volatile. In fact it's schedule() that is volatile in some
395 * circumstances: when current->state = ZOMBIE, schedule() never
398 * In fact the natural way to do all this is to have the label and the
399 * goto right after each other, but I put the fake_volatile label at
400 * the start of the function just in case something /really/ bad
401 * happens, and the schedule returns. This way we can try again. I'm
402 * not paranoid: it's just that everybody is out to get me.
407 asmlinkage
int sys_exit(int error_code
)
409 do_exit((error_code
&0xff)<<8);
412 asmlinkage
int sys_wait4(pid_t pid
,unsigned int * stat_addr
, int options
, struct rusage
* ru
)
415 struct wait_queue wait
= { current
, NULL
};
416 struct task_struct
*p
;
419 if(verify_area(VERIFY_WRITE
, stat_addr
, sizeof(*stat_addr
)))
423 if(verify_area(VERIFY_WRITE
, ru
, sizeof(*ru
)))
427 if (options
& ~(WNOHANG
|WUNTRACED
|__WCLONE
))
430 add_wait_queue(¤t
->wait_chldexit
,&wait
);
433 read_lock(&tasklist_lock
);
434 for (p
= current
->p_cptr
; p
; p
= p
->p_osptr
) {
439 if (p
->pgrp
!= current
->pgrp
)
441 } else if (pid
!= -1) {
445 /* wait for cloned processes iff the __WCLONE flag is set */
446 if ((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
453 if (!(options
& WUNTRACED
) && !(p
->flags
& PF_PTRACED
))
455 read_unlock(&tasklist_lock
);
457 getrusage(p
, RUSAGE_BOTH
, ru
);
459 __put_user((p
->exit_code
<< 8) | 0x7f, stat_addr
);
464 current
->times
.tms_cutime
+= p
->times
.tms_utime
+ p
->times
.tms_cutime
;
465 current
->times
.tms_cstime
+= p
->times
.tms_stime
+ p
->times
.tms_cstime
;
466 read_unlock(&tasklist_lock
);
468 getrusage(p
, RUSAGE_BOTH
, ru
);
470 __put_user(p
->exit_code
, stat_addr
);
472 if (p
->p_opptr
!= p
->p_pptr
) {
473 write_lock_irq(&tasklist_lock
);
475 p
->p_pptr
= p
->p_opptr
;
477 write_unlock_irq(&tasklist_lock
);
478 notify_parent(p
, SIGCHLD
);
481 #ifdef DEBUG_PROC_TREE
489 read_unlock(&tasklist_lock
);
492 if (options
& WNOHANG
)
494 retval
= -ERESTARTSYS
;
495 if (signal_pending(current
))
497 current
->state
=TASK_INTERRUPTIBLE
;
503 remove_wait_queue(¤t
->wait_chldexit
,&wait
);
510 * sys_waitpid() remains for compatibility. waitpid() should be
511 * implemented by calling sys_wait4() from libc.a.
513 asmlinkage
int sys_waitpid(pid_t pid
,unsigned int * stat_addr
, int options
)
515 return sys_wait4(pid
, stat_addr
, options
, NULL
);