4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/malloc.h>
9 #include <linux/interrupt.h>
10 #include <linux/smp_lock.h>
11 #include <linux/module.h>
12 #ifdef CONFIG_BSD_PROCESS_ACCT
13 #include <linux/acct.h>
16 #include <asm/uaccess.h>
17 #include <asm/pgtable.h>
18 #include <asm/mmu_context.h>
20 extern void sem_exit (void);
21 extern struct task_struct
*child_reaper
;
23 int getrusage(struct task_struct
*, int, struct rusage
*);
25 static void release(struct task_struct
* p
)
32 * Wait to make sure the process isn't on the
33 * runqueue (active on some other CPU still)
36 spin_lock_irq(&runqueue_lock
);
38 spin_unlock_irq(&runqueue_lock
);
45 current
->cmin_flt
+= p
->min_flt
+ p
->cmin_flt
;
46 current
->cmaj_flt
+= p
->maj_flt
+ p
->cmaj_flt
;
47 current
->cnswap
+= p
->nswap
+ p
->cnswap
;
49 * Potentially available timeslices are retrieved
50 * here - this way the parent does not get penalized
51 * for creating too many processes.
53 * (this cannot be used to artificially 'generate'
54 * timeslices, because any timeslice recovered here
55 * was given away by the parent in the first place.)
57 current
->counter
+= p
->counter
;
58 if (current
->counter
>= current
->priority
*2)
59 current
->counter
= current
->priority
*2-1;
62 printk("task releasing itself\n");
67 * This checks not only the pgrp, but falls back on the pid if no
68 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
71 int session_of_pgrp(int pgrp
)
73 struct task_struct
*p
;
77 read_lock(&tasklist_lock
);
81 if (p
->pgrp
== pgrp
) {
82 fallback
= p
->session
;
86 fallback
= p
->session
;
88 read_unlock(&tasklist_lock
);
93 * Determine if a process group is "orphaned", according to the POSIX
94 * definition in 2.2.2.52. Orphaned process groups are not to be affected
95 * by terminal-generated stop signals. Newly orphaned process groups are
96 * to receive a SIGHUP and a SIGCONT.
98 * "I ask you, have you ever known what it is to be an orphan?"
100 static int will_become_orphaned_pgrp(int pgrp
, struct task_struct
* ignored_task
)
102 struct task_struct
*p
;
104 read_lock(&tasklist_lock
);
106 if ((p
== ignored_task
) || (p
->pgrp
!= pgrp
) ||
107 (p
->state
== TASK_ZOMBIE
) ||
108 (p
->p_pptr
->pid
== 1))
110 if ((p
->p_pptr
->pgrp
!= pgrp
) &&
111 (p
->p_pptr
->session
== p
->session
)) {
112 read_unlock(&tasklist_lock
);
116 read_unlock(&tasklist_lock
);
117 return 1; /* (sighing) "Often!" */
120 int is_orphaned_pgrp(int pgrp
)
122 return will_become_orphaned_pgrp(pgrp
, 0);
125 static inline int has_stopped_jobs(int pgrp
)
128 struct task_struct
* p
;
130 read_lock(&tasklist_lock
);
134 if (p
->state
!= TASK_STOPPED
)
139 read_unlock(&tasklist_lock
);
143 static inline void forget_original_parent(struct task_struct
* father
)
145 struct task_struct
* p
;
147 read_lock(&tasklist_lock
);
149 if (p
->p_opptr
== father
) {
150 /* We dont want people slaying init */
151 p
->exit_signal
= SIGCHLD
;
153 p
->p_opptr
= child_reaper
; /* init */
154 if (p
->pdeath_signal
) send_sig(p
->pdeath_signal
, p
, 0);
157 read_unlock(&tasklist_lock
);
160 static inline void close_files(struct files_struct
* files
)
168 if (i
>= files
->max_fdset
|| i
>= files
->max_fds
)
170 set
= files
->open_fds
->fds_bits
[j
++];
173 struct file
* file
= xchg(&files
->fd
[i
], NULL
);
175 filp_close(file
, files
);
183 extern kmem_cache_t
*files_cachep
;
185 void put_files_struct(struct files_struct
*files
)
187 if (atomic_dec_and_test(&files
->count
)) {
190 * Free the fd and fdset arrays if we expanded them.
192 if (files
->fd
!= &files
->fd_array
[0])
193 free_fd_array(files
->fd
, files
->max_fds
);
194 if (files
->max_fdset
> __FD_SETSIZE
) {
195 free_fdset(files
->open_fds
, files
->max_fdset
);
196 free_fdset(files
->close_on_exec
, files
->max_fdset
);
198 kmem_cache_free(files_cachep
, files
);
202 static inline void __exit_files(struct task_struct
*tsk
)
204 struct files_struct
* files
= tsk
->files
;
210 put_files_struct(files
);
214 void exit_files(struct task_struct
*tsk
)
218 static inline void __put_fs_struct(struct fs_struct
*fs
)
220 if (atomic_dec_and_test(&fs
->count
)) {
227 mntput(fs
->altrootmnt
);
233 void put_fs_struct(struct fs_struct
*fs
)
238 static inline void __exit_fs(struct task_struct
*tsk
)
240 struct fs_struct
* fs
= tsk
->fs
;
250 void exit_fs(struct task_struct
*tsk
)
255 static inline void __exit_sighand(struct task_struct
*tsk
)
257 struct signal_struct
* sig
= tsk
->sig
;
260 spin_lock_irq(&tsk
->sigmask_lock
);
262 spin_unlock_irq(&tsk
->sigmask_lock
);
263 if (atomic_dec_and_test(&sig
->count
))
270 void exit_sighand(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
;
310 atomic_inc(&mm
->mm_count
);
312 if (mm
!= tsk
->active_mm
) BUG();
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
;
378 notify_parent(current
, current
->exit_signal
);
381 * This loop does two things:
383 * A. Make init inherit all the child processes
384 * B. Check to see if any process groups have become orphaned
385 * as a result of our exiting, and if they have any stopped
386 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
389 write_lock_irq(&tasklist_lock
);
390 while (current
->p_cptr
!= NULL
) {
392 current
->p_cptr
= p
->p_osptr
;
396 p
->p_pptr
= p
->p_opptr
;
397 p
->p_osptr
= p
->p_pptr
->p_cptr
;
399 p
->p_osptr
->p_ysptr
= p
;
400 p
->p_pptr
->p_cptr
= p
;
401 if (p
->state
== TASK_ZOMBIE
)
402 notify_parent(p
, p
->exit_signal
);
404 * process group orphan check
405 * Case ii: Our child is in a different pgrp
406 * than we are, and it was the only connection
407 * outside, so the child pgrp is now orphaned.
409 if ((p
->pgrp
!= current
->pgrp
) &&
410 (p
->session
== current
->session
)) {
413 write_unlock_irq(&tasklist_lock
);
414 if (is_orphaned_pgrp(pgrp
) && has_stopped_jobs(pgrp
)) {
415 kill_pg(pgrp
,SIGHUP
,1);
416 kill_pg(pgrp
,SIGCONT
,1);
418 write_lock_irq(&tasklist_lock
);
421 write_unlock_irq(&tasklist_lock
);
424 disassociate_ctty(1);
427 NORET_TYPE
void do_exit(long code
)
429 struct task_struct
*tsk
= current
;
432 printk("Aiee, killing interrupt handler\n");
434 panic("Attempted to kill the idle task!");
436 panic("Attempted to kill init!");
437 tsk
->flags
|= PF_EXITING
;
438 del_timer_sync(&tsk
->real_timer
);
442 #ifdef CONFIG_BSD_PROCESS_ACCT
451 tsk
->state
= TASK_ZOMBIE
;
452 tsk
->exit_code
= code
;
454 put_exec_domain(tsk
->exec_domain
);
455 if (tsk
->binfmt
&& tsk
->binfmt
->module
)
456 __MOD_DEC_USE_COUNT(tsk
->binfmt
->module
);
459 * In order to get rid of the "volatile function does return" message
460 * I did this little loop that confuses gcc to think do_exit really
461 * is volatile. In fact it's schedule() that is volatile in some
462 * circumstances: when current->state = ZOMBIE, schedule() never
465 * In fact the natural way to do all this is to have the label and the
466 * goto right after each other, but I put the fake_volatile label at
467 * the start of the function just in case something /really/ bad
468 * happens, and the schedule returns. This way we can try again. I'm
469 * not paranoid: it's just that everybody is out to get me.
474 asmlinkage
long sys_exit(int error_code
)
476 do_exit((error_code
&0xff)<<8);
479 asmlinkage
long sys_wait4(pid_t pid
,unsigned int * stat_addr
, int options
, struct rusage
* ru
)
482 DECLARE_WAITQUEUE(wait
, current
);
483 struct task_struct
*p
;
485 if (options
& ~(WNOHANG
|WUNTRACED
|__WCLONE
|__WALL
))
488 add_wait_queue(¤t
->wait_chldexit
,&wait
);
491 current
->state
= TASK_INTERRUPTIBLE
;
492 read_lock(&tasklist_lock
);
493 for (p
= current
->p_cptr
; p
; p
= p
->p_osptr
) {
498 if (p
->pgrp
!= current
->pgrp
)
500 } else if (pid
!= -1) {
504 /* Wait for all children (clone and not) if __WALL is set;
505 * otherwise, wait for clone children *only* if __WCLONE is
506 * set; otherwise, wait for non-clone children *only*. (Note:
507 * A "clone" child here is one that reports to its parent
508 * using a signal other than SIGCHLD.) */
509 if (((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
510 && !(options
& __WALL
))
517 if (!(options
& WUNTRACED
) && !(p
->ptrace
& PT_PTRACED
))
519 read_unlock(&tasklist_lock
);
520 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
521 if (!retval
&& stat_addr
)
522 retval
= put_user((p
->exit_code
<< 8) | 0x7f, stat_addr
);
529 current
->times
.tms_cutime
+= p
->times
.tms_utime
+ p
->times
.tms_cutime
;
530 current
->times
.tms_cstime
+= p
->times
.tms_stime
+ p
->times
.tms_cstime
;
531 read_unlock(&tasklist_lock
);
532 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
533 if (!retval
&& stat_addr
)
534 retval
= put_user(p
->exit_code
, stat_addr
);
538 if (p
->p_opptr
!= p
->p_pptr
) {
539 write_lock_irq(&tasklist_lock
);
541 p
->p_pptr
= p
->p_opptr
;
543 write_unlock_irq(&tasklist_lock
);
544 notify_parent(p
, SIGCHLD
);
552 read_unlock(&tasklist_lock
);
555 if (options
& WNOHANG
)
557 retval
= -ERESTARTSYS
;
558 if (signal_pending(current
))
565 current
->state
= TASK_RUNNING
;
566 remove_wait_queue(¤t
->wait_chldexit
,&wait
);
570 #if !defined(__alpha__) && !defined(__ia64__)
573 * sys_waitpid() remains for compatibility. waitpid() should be
574 * implemented by calling sys_wait4() from libc.a.
576 asmlinkage
long sys_waitpid(pid_t pid
,unsigned int * stat_addr
, int options
)
578 return sys_wait4(pid
, stat_addr
, options
, NULL
);