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
)
30 * Wait to make sure the process isn't on the
31 * runqueue (active on some other CPU still)
34 spin_lock_irq(&runqueue_lock
);
37 spin_unlock_irq(&runqueue_lock
);
42 spin_unlock_irq(&runqueue_lock
);
44 atomic_dec(&p
->user
->processes
);
49 current
->cmin_flt
+= p
->min_flt
+ p
->cmin_flt
;
50 current
->cmaj_flt
+= p
->maj_flt
+ p
->cmaj_flt
;
51 current
->cnswap
+= p
->nswap
+ p
->cnswap
;
53 * Potentially available timeslices are retrieved
54 * here - this way the parent does not get penalized
55 * for creating too many processes.
57 * (this cannot be used to artificially 'generate'
58 * timeslices, because any timeslice recovered here
59 * was given away by the parent in the first place.)
61 current
->counter
+= p
->counter
;
62 if (current
->counter
>= MAX_COUNTER
)
63 current
->counter
= MAX_COUNTER
;
66 printk("task releasing itself\n");
71 * This checks not only the pgrp, but falls back on the pid if no
72 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
75 int session_of_pgrp(int pgrp
)
77 struct task_struct
*p
;
81 read_lock(&tasklist_lock
);
85 if (p
->pgrp
== pgrp
) {
86 fallback
= p
->session
;
90 fallback
= p
->session
;
92 read_unlock(&tasklist_lock
);
97 * Determine if a process group is "orphaned", according to the POSIX
98 * definition in 2.2.2.52. Orphaned process groups are not to be affected
99 * by terminal-generated stop signals. Newly orphaned process groups are
100 * to receive a SIGHUP and a SIGCONT.
102 * "I ask you, have you ever known what it is to be an orphan?"
104 static int will_become_orphaned_pgrp(int pgrp
, struct task_struct
* ignored_task
)
106 struct task_struct
*p
;
108 read_lock(&tasklist_lock
);
110 if ((p
== ignored_task
) || (p
->pgrp
!= pgrp
) ||
111 (p
->state
== TASK_ZOMBIE
) ||
112 (p
->p_pptr
->pid
== 1))
114 if ((p
->p_pptr
->pgrp
!= pgrp
) &&
115 (p
->p_pptr
->session
== p
->session
)) {
116 read_unlock(&tasklist_lock
);
120 read_unlock(&tasklist_lock
);
121 return 1; /* (sighing) "Often!" */
124 int is_orphaned_pgrp(int pgrp
)
126 return will_become_orphaned_pgrp(pgrp
, 0);
129 static inline int has_stopped_jobs(int pgrp
)
132 struct task_struct
* p
;
134 read_lock(&tasklist_lock
);
138 if (p
->state
!= TASK_STOPPED
)
143 read_unlock(&tasklist_lock
);
148 * When we die, we re-parent all our children.
149 * Try to give them to another thread in our process
150 * group, and if no such member exists, give it to
151 * the global child reaper process (ie "init")
153 static inline void forget_original_parent(struct task_struct
* father
)
155 struct task_struct
* p
, *reaper
;
157 read_lock(&tasklist_lock
);
159 /* Next in our thread group */
160 reaper
= next_thread(father
);
161 if (reaper
== father
)
162 reaper
= child_reaper
;
165 if (p
->p_opptr
== father
) {
166 /* We dont want people slaying init */
167 p
->exit_signal
= SIGCHLD
;
170 if (p
->pdeath_signal
) send_sig(p
->pdeath_signal
, p
, 0);
173 read_unlock(&tasklist_lock
);
176 static inline void close_files(struct files_struct
* files
)
184 if (i
>= files
->max_fdset
|| i
>= files
->max_fds
)
186 set
= files
->open_fds
->fds_bits
[j
++];
189 struct file
* file
= xchg(&files
->fd
[i
], NULL
);
191 filp_close(file
, files
);
199 void put_files_struct(struct files_struct
*files
)
201 if (atomic_dec_and_test(&files
->count
)) {
204 * Free the fd and fdset arrays if we expanded them.
206 if (files
->fd
!= &files
->fd_array
[0])
207 free_fd_array(files
->fd
, files
->max_fds
);
208 if (files
->max_fdset
> __FD_SETSIZE
) {
209 free_fdset(files
->open_fds
, files
->max_fdset
);
210 free_fdset(files
->close_on_exec
, files
->max_fdset
);
212 kmem_cache_free(files_cachep
, files
);
216 static inline void __exit_files(struct task_struct
*tsk
)
218 struct files_struct
* files
= tsk
->files
;
224 put_files_struct(files
);
228 void exit_files(struct task_struct
*tsk
)
232 static inline void __put_fs_struct(struct fs_struct
*fs
)
234 /* No need to hold fs->lock if we are killing it */
235 if (atomic_dec_and_test(&fs
->count
)) {
242 mntput(fs
->altrootmnt
);
244 kmem_cache_free(fs_cachep
, fs
);
248 void put_fs_struct(struct fs_struct
*fs
)
253 static inline void __exit_fs(struct task_struct
*tsk
)
255 struct fs_struct
* fs
= tsk
->fs
;
265 void exit_fs(struct task_struct
*tsk
)
271 * We can use these to temporarily drop into
272 * "lazy TLB" mode and back.
274 struct mm_struct
* start_lazy_tlb(void)
276 struct mm_struct
*mm
= current
->mm
;
278 /* active_mm is still 'mm' */
279 atomic_inc(&mm
->mm_count
);
280 enter_lazy_tlb(mm
, current
, smp_processor_id());
284 void end_lazy_tlb(struct mm_struct
*mm
)
286 struct mm_struct
*active_mm
= current
->active_mm
;
289 if (mm
!= active_mm
) {
290 current
->active_mm
= mm
;
291 activate_mm(active_mm
, mm
);
297 * Turn us into a lazy TLB process if we
300 static inline void __exit_mm(struct task_struct
* tsk
)
302 struct mm_struct
* mm
= tsk
->mm
;
305 atomic_inc(&mm
->mm_count
);
307 if (mm
!= tsk
->active_mm
) BUG();
308 /* more a memory barrier than a real lock */
312 enter_lazy_tlb(mm
, current
, smp_processor_id());
317 void exit_mm(struct task_struct
*tsk
)
323 * Send signals to all our closest relatives so that they know
324 * to properly mourn us..
326 static void exit_notify(void)
328 struct task_struct
* p
, *t
;
330 forget_original_parent(current
);
332 * Check to see if any process groups have become orphaned
333 * as a result of our exiting, and if they have any stopped
334 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
336 * Case i: Our father is in a different pgrp than we are
337 * and we were the only connection outside, so our pgrp
338 * is about to become orphaned.
343 if ((t
->pgrp
!= current
->pgrp
) &&
344 (t
->session
== current
->session
) &&
345 will_become_orphaned_pgrp(current
->pgrp
, current
) &&
346 has_stopped_jobs(current
->pgrp
)) {
347 kill_pg(current
->pgrp
,SIGHUP
,1);
348 kill_pg(current
->pgrp
,SIGCONT
,1);
351 /* Let father know we died
353 * Thread signals are configurable, but you aren't going to use
354 * that to send signals to arbitary processes.
355 * That stops right now.
357 * If the parent exec id doesn't match the exec id we saved
358 * when we started then we know the parent has changed security
361 * If our self_exec id doesn't match our parent_exec_id then
362 * we have changed execution domain as these two values started
363 * the same after a fork.
367 if(current
->exit_signal
!= SIGCHLD
&&
368 ( current
->parent_exec_id
!= t
->self_exec_id
||
369 current
->self_exec_id
!= current
->parent_exec_id
)
370 && !capable(CAP_KILL
))
371 current
->exit_signal
= SIGCHLD
;
375 * This loop does two things:
377 * A. Make init inherit all the child processes
378 * B. Check to see if any process groups have become orphaned
379 * as a result of our exiting, and if they have any stopped
380 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
383 write_lock_irq(&tasklist_lock
);
384 do_notify_parent(current
, current
->exit_signal
);
385 while (current
->p_cptr
!= NULL
) {
387 current
->p_cptr
= p
->p_osptr
;
391 p
->p_pptr
= p
->p_opptr
;
392 p
->p_osptr
= p
->p_pptr
->p_cptr
;
394 p
->p_osptr
->p_ysptr
= p
;
395 p
->p_pptr
->p_cptr
= p
;
396 if (p
->state
== TASK_ZOMBIE
)
397 do_notify_parent(p
, p
->exit_signal
);
399 * process group orphan check
400 * Case ii: Our child is in a different pgrp
401 * than we are, and it was the only connection
402 * outside, so the child pgrp is now orphaned.
404 if ((p
->pgrp
!= current
->pgrp
) &&
405 (p
->session
== current
->session
)) {
408 write_unlock_irq(&tasklist_lock
);
409 if (is_orphaned_pgrp(pgrp
) && has_stopped_jobs(pgrp
)) {
410 kill_pg(pgrp
,SIGHUP
,1);
411 kill_pg(pgrp
,SIGCONT
,1);
413 write_lock_irq(&tasklist_lock
);
416 write_unlock_irq(&tasklist_lock
);
419 disassociate_ctty(1);
422 NORET_TYPE
void do_exit(long code
)
424 struct task_struct
*tsk
= current
;
427 printk("Aiee, killing interrupt handler\n");
429 panic("Attempted to kill the idle task!");
431 panic("Attempted to kill init!");
432 tsk
->flags
|= PF_EXITING
;
433 del_timer_sync(&tsk
->real_timer
);
436 #ifdef CONFIG_BSD_PROCESS_ACCT
446 tsk
->state
= TASK_ZOMBIE
;
447 tsk
->exit_code
= code
;
449 put_exec_domain(tsk
->exec_domain
);
450 if (tsk
->binfmt
&& tsk
->binfmt
->module
)
451 __MOD_DEC_USE_COUNT(tsk
->binfmt
->module
);
454 * In order to get rid of the "volatile function does return" message
455 * I did this little loop that confuses gcc to think do_exit really
456 * is volatile. In fact it's schedule() that is volatile in some
457 * circumstances: when current->state = ZOMBIE, schedule() never
460 * In fact the natural way to do all this is to have the label and the
461 * goto right after each other, but I put the fake_volatile label at
462 * the start of the function just in case something /really/ bad
463 * happens, and the schedule returns. This way we can try again. I'm
464 * not paranoid: it's just that everybody is out to get me.
469 asmlinkage
long sys_exit(int error_code
)
471 do_exit((error_code
&0xff)<<8);
474 asmlinkage
long sys_wait4(pid_t pid
,unsigned int * stat_addr
, int options
, struct rusage
* ru
)
477 DECLARE_WAITQUEUE(wait
, current
);
478 struct task_struct
*tsk
;
480 if (options
& ~(WNOHANG
|WUNTRACED
|__WNOTHREAD
|__WCLONE
|__WALL
))
483 add_wait_queue(¤t
->wait_chldexit
,&wait
);
486 current
->state
= TASK_INTERRUPTIBLE
;
487 read_lock(&tasklist_lock
);
490 struct task_struct
*p
;
491 for (p
= tsk
->p_cptr
; p
; p
= p
->p_osptr
) {
496 if (p
->pgrp
!= current
->pgrp
)
498 } else if (pid
!= -1) {
502 /* Wait for all children (clone and not) if __WALL is set;
503 * otherwise, wait for clone children *only* if __WCLONE is
504 * set; otherwise, wait for non-clone children *only*. (Note:
505 * A "clone" child here is one that reports to its parent
506 * using a signal other than SIGCHLD.) */
507 if (((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
508 && !(options
& __WALL
))
515 if (!(options
& WUNTRACED
) && !(p
->ptrace
& PT_PTRACED
))
517 read_unlock(&tasklist_lock
);
518 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
519 if (!retval
&& stat_addr
)
520 retval
= put_user((p
->exit_code
<< 8) | 0x7f, stat_addr
);
527 current
->times
.tms_cutime
+= p
->times
.tms_utime
+ p
->times
.tms_cutime
;
528 current
->times
.tms_cstime
+= p
->times
.tms_stime
+ p
->times
.tms_cstime
;
529 read_unlock(&tasklist_lock
);
530 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
531 if (!retval
&& stat_addr
)
532 retval
= put_user(p
->exit_code
, stat_addr
);
536 if (p
->p_opptr
!= p
->p_pptr
) {
537 write_lock_irq(&tasklist_lock
);
539 p
->p_pptr
= p
->p_opptr
;
541 do_notify_parent(p
, SIGCHLD
);
542 write_unlock_irq(&tasklist_lock
);
550 if (options
& __WNOTHREAD
)
552 tsk
= next_thread(tsk
);
553 } while (tsk
!= current
);
554 read_unlock(&tasklist_lock
);
557 if (options
& WNOHANG
)
559 retval
= -ERESTARTSYS
;
560 if (signal_pending(current
))
567 current
->state
= TASK_RUNNING
;
568 remove_wait_queue(¤t
->wait_chldexit
,&wait
);
572 #if !defined(__alpha__) && !defined(__ia64__)
575 * sys_waitpid() remains for compatibility. waitpid() should be
576 * implemented by calling sys_wait4() from libc.a.
578 asmlinkage
long sys_waitpid(pid_t pid
,unsigned int * stat_addr
, int options
)
580 return sys_wait4(pid
, stat_addr
, options
, NULL
);