]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/exit.c
Freezer: make kernel threads nonfreezable by default
[net-next-2.6.git] / kernel / exit.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/exit.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
1da177e4
LT
7#include <linux/mm.h>
8#include <linux/slab.h>
9#include <linux/interrupt.h>
1da177e4 10#include <linux/module.h>
c59ede7b 11#include <linux/capability.h>
1da177e4
LT
12#include <linux/completion.h>
13#include <linux/personality.h>
14#include <linux/tty.h>
6b3286ed 15#include <linux/mnt_namespace.h>
1da177e4
LT
16#include <linux/key.h>
17#include <linux/security.h>
18#include <linux/cpu.h>
19#include <linux/acct.h>
8f0ab514 20#include <linux/tsacct_kern.h>
1da177e4
LT
21#include <linux/file.h>
22#include <linux/binfmts.h>
ab516013 23#include <linux/nsproxy.h>
84d73786 24#include <linux/pid_namespace.h>
1da177e4
LT
25#include <linux/ptrace.h>
26#include <linux/profile.h>
fba2afaa 27#include <linux/signalfd.h>
1da177e4
LT
28#include <linux/mount.h>
29#include <linux/proc_fs.h>
49d769d5 30#include <linux/kthread.h>
1da177e4 31#include <linux/mempolicy.h>
c757249a 32#include <linux/taskstats_kern.h>
ca74e92b 33#include <linux/delayacct.h>
83144186 34#include <linux/freezer.h>
1da177e4
LT
35#include <linux/cpuset.h>
36#include <linux/syscalls.h>
7ed20e1a 37#include <linux/signal.h>
6a14c5c9 38#include <linux/posix-timers.h>
9f46080c 39#include <linux/cn_proc.h>
de5097c2 40#include <linux/mutex.h>
0771dfef 41#include <linux/futex.h>
34f192c6 42#include <linux/compat.h>
b92ce558 43#include <linux/pipe_fs_i.h>
fa84cb93 44#include <linux/audit.h> /* for audit_free() */
83cc5ed3 45#include <linux/resource.h>
0d67a46d 46#include <linux/blkdev.h>
6eaeeaba 47#include <linux/task_io_accounting_ops.h>
1da177e4
LT
48
49#include <asm/uaccess.h>
50#include <asm/unistd.h>
51#include <asm/pgtable.h>
52#include <asm/mmu_context.h>
53
54extern void sem_exit (void);
1da177e4 55
408b664a
AB
56static void exit_mm(struct task_struct * tsk);
57
1da177e4
LT
58static void __unhash_process(struct task_struct *p)
59{
60 nr_threads--;
61 detach_pid(p, PIDTYPE_PID);
1da177e4
LT
62 if (thread_group_leader(p)) {
63 detach_pid(p, PIDTYPE_PGID);
64 detach_pid(p, PIDTYPE_SID);
c97d9893 65
5e85d4ab 66 list_del_rcu(&p->tasks);
73b9ebfe 67 __get_cpu_var(process_counts)--;
1da177e4 68 }
47e65328 69 list_del_rcu(&p->thread_group);
c97d9893 70 remove_parent(p);
1da177e4
LT
71}
72
6a14c5c9
ON
73/*
74 * This function expects the tasklist_lock write-locked.
75 */
76static void __exit_signal(struct task_struct *tsk)
77{
78 struct signal_struct *sig = tsk->signal;
79 struct sighand_struct *sighand;
80
81 BUG_ON(!sig);
82 BUG_ON(!atomic_read(&sig->count));
83
84 rcu_read_lock();
85 sighand = rcu_dereference(tsk->sighand);
86 spin_lock(&sighand->siglock);
87
fba2afaa
DL
88 /*
89 * Notify that this sighand has been detached. This must
90 * be called with the tsk->sighand lock held. Also, this
91 * access tsk->sighand internally, so it must be called
92 * before tsk->sighand is reset.
93 */
94 signalfd_detach_locked(tsk);
95
6a14c5c9
ON
96 posix_cpu_timers_exit(tsk);
97 if (atomic_dec_and_test(&sig->count))
98 posix_cpu_timers_exit_group(tsk);
99 else {
100 /*
101 * If there is any task waiting for the group exit
102 * then notify it:
103 */
104 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
105 wake_up_process(sig->group_exit_task);
106 sig->group_exit_task = NULL;
107 }
108 if (tsk == sig->curr_target)
109 sig->curr_target = next_thread(tsk);
110 /*
111 * Accumulate here the counters for all threads but the
112 * group leader as they die, so they can be added into
113 * the process-wide totals when those are taken.
114 * The group leader stays around as a zombie as long
115 * as there are other threads. When it gets reaped,
116 * the exit.c code will add its counts into these totals.
117 * We won't ever get here for the group leader, since it
118 * will have been the last reference on the signal_struct.
119 */
120 sig->utime = cputime_add(sig->utime, tsk->utime);
121 sig->stime = cputime_add(sig->stime, tsk->stime);
122 sig->min_flt += tsk->min_flt;
123 sig->maj_flt += tsk->maj_flt;
124 sig->nvcsw += tsk->nvcsw;
125 sig->nivcsw += tsk->nivcsw;
6eaeeaba
ED
126 sig->inblock += task_io_get_inblock(tsk);
127 sig->oublock += task_io_get_oublock(tsk);
172ba844 128 sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
6a14c5c9
ON
129 sig = NULL; /* Marker for below. */
130 }
131
5876700c
ON
132 __unhash_process(tsk);
133
6a14c5c9 134 tsk->signal = NULL;
a7e5328a 135 tsk->sighand = NULL;
6a14c5c9
ON
136 spin_unlock(&sighand->siglock);
137 rcu_read_unlock();
138
a7e5328a 139 __cleanup_sighand(sighand);
6a14c5c9
ON
140 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
141 flush_sigqueue(&tsk->pending);
142 if (sig) {
143 flush_sigqueue(&sig->shared_pending);
093a8e8a 144 taskstats_tgid_free(sig);
6a14c5c9
ON
145 __cleanup_signal(sig);
146 }
147}
148
8c7904a0
EB
149static void delayed_put_task_struct(struct rcu_head *rhp)
150{
151 put_task_struct(container_of(rhp, struct task_struct, rcu));
152}
153
1da177e4
LT
154void release_task(struct task_struct * p)
155{
36c8b586 156 struct task_struct *leader;
1da177e4 157 int zap_leader;
1f09f974 158repeat:
1da177e4 159 atomic_dec(&p->user->processes);
1da177e4 160 write_lock_irq(&tasklist_lock);
1f09f974 161 ptrace_unlink(p);
1da177e4
LT
162 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
163 __exit_signal(p);
35f5cad8 164
1da177e4
LT
165 /*
166 * If we are the last non-leader member of the thread
167 * group, and the leader is zombie, then notify the
168 * group leader's parent process. (if it wants notification.)
169 */
170 zap_leader = 0;
171 leader = p->group_leader;
172 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
173 BUG_ON(leader->exit_signal == -1);
174 do_notify_parent(leader, leader->exit_signal);
175 /*
176 * If we were the last child thread and the leader has
177 * exited already, and the leader's parent ignores SIGCHLD,
178 * then we are the one who should release the leader.
179 *
180 * do_notify_parent() will have marked it self-reaping in
181 * that case.
182 */
183 zap_leader = (leader->exit_signal == -1);
184 }
185
1da177e4 186 write_unlock_irq(&tasklist_lock);
48e6484d 187 proc_flush_task(p);
1da177e4 188 release_thread(p);
8c7904a0 189 call_rcu(&p->rcu, delayed_put_task_struct);
1da177e4
LT
190
191 p = leader;
192 if (unlikely(zap_leader))
193 goto repeat;
194}
195
1da177e4
LT
196/*
197 * This checks not only the pgrp, but falls back on the pid if no
198 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
199 * without this...
04a2e6a5
EB
200 *
201 * The caller must hold rcu lock or the tasklist lock.
1da177e4 202 */
04a2e6a5 203struct pid *session_of_pgrp(struct pid *pgrp)
1da177e4
LT
204{
205 struct task_struct *p;
04a2e6a5 206 struct pid *sid = NULL;
62dfb554 207
04a2e6a5 208 p = pid_task(pgrp, PIDTYPE_PGID);
62dfb554 209 if (p == NULL)
04a2e6a5 210 p = pid_task(pgrp, PIDTYPE_PID);
62dfb554 211 if (p != NULL)
04a2e6a5 212 sid = task_session(p);
62dfb554 213
1da177e4
LT
214 return sid;
215}
216
217/*
218 * Determine if a process group is "orphaned", according to the POSIX
219 * definition in 2.2.2.52. Orphaned process groups are not to be affected
220 * by terminal-generated stop signals. Newly orphaned process groups are
221 * to receive a SIGHUP and a SIGCONT.
222 *
223 * "I ask you, have you ever known what it is to be an orphan?"
224 */
0475ac08 225static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
1da177e4
LT
226{
227 struct task_struct *p;
228 int ret = 1;
229
0475ac08 230 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4
LT
231 if (p == ignored_task
232 || p->exit_state
f400e198 233 || is_init(p->real_parent))
1da177e4 234 continue;
0475ac08
EB
235 if (task_pgrp(p->real_parent) != pgrp &&
236 task_session(p->real_parent) == task_session(p)) {
1da177e4
LT
237 ret = 0;
238 break;
239 }
0475ac08 240 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
241 return ret; /* (sighing) "Often!" */
242}
243
3e7cd6c4 244int is_current_pgrp_orphaned(void)
1da177e4
LT
245{
246 int retval;
247
248 read_lock(&tasklist_lock);
3e7cd6c4 249 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
1da177e4
LT
250 read_unlock(&tasklist_lock);
251
252 return retval;
253}
254
0475ac08 255static int has_stopped_jobs(struct pid *pgrp)
1da177e4
LT
256{
257 int retval = 0;
258 struct task_struct *p;
259
0475ac08 260 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4
LT
261 if (p->state != TASK_STOPPED)
262 continue;
1da177e4
LT
263 retval = 1;
264 break;
0475ac08 265 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
266 return retval;
267}
268
269/**
49d769d5 270 * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
1da177e4
LT
271 *
272 * If a kernel thread is launched as a result of a system call, or if
49d769d5
EB
273 * it ever exits, it should generally reparent itself to kthreadd so it
274 * isn't in the way of other processes and is correctly cleaned up on exit.
1da177e4
LT
275 *
276 * The various task state such as scheduling policy and priority may have
277 * been inherited from a user process, so we reset them to sane values here.
278 *
49d769d5 279 * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
1da177e4 280 */
49d769d5 281static void reparent_to_kthreadd(void)
1da177e4
LT
282{
283 write_lock_irq(&tasklist_lock);
284
285 ptrace_unlink(current);
286 /* Reparent to init */
9b678ece 287 remove_parent(current);
49d769d5 288 current->real_parent = current->parent = kthreadd_task;
9b678ece 289 add_parent(current);
1da177e4
LT
290
291 /* Set the exit signal to SIGCHLD so we signal init on exit */
292 current->exit_signal = SIGCHLD;
293
e05606d3 294 if (task_nice(current) < 0)
1da177e4
LT
295 set_user_nice(current, 0);
296 /* cpus_allowed? */
297 /* rt_priority? */
298 /* signals? */
299 security_task_reparent_to_init(current);
300 memcpy(current->signal->rlim, init_task.signal->rlim,
301 sizeof(current->signal->rlim));
302 atomic_inc(&(INIT_USER->__count));
303 write_unlock_irq(&tasklist_lock);
304 switch_uid(INIT_USER);
305}
306
307void __set_special_pids(pid_t session, pid_t pgrp)
308{
e19f247a 309 struct task_struct *curr = current->group_leader;
1da177e4 310
937949d9 311 if (process_session(curr) != session) {
1da177e4 312 detach_pid(curr, PIDTYPE_SID);
1ec320af 313 set_signal_session(curr->signal, session);
e713d0da 314 attach_pid(curr, PIDTYPE_SID, find_pid(session));
1da177e4
LT
315 }
316 if (process_group(curr) != pgrp) {
317 detach_pid(curr, PIDTYPE_PGID);
318 curr->signal->pgrp = pgrp;
e713d0da 319 attach_pid(curr, PIDTYPE_PGID, find_pid(pgrp));
1da177e4
LT
320 }
321}
322
ae424ae4 323static void set_special_pids(pid_t session, pid_t pgrp)
1da177e4
LT
324{
325 write_lock_irq(&tasklist_lock);
326 __set_special_pids(session, pgrp);
327 write_unlock_irq(&tasklist_lock);
328}
329
330/*
331 * Let kernel threads use this to say that they
332 * allow a certain signal (since daemonize() will
333 * have disabled all of them by default).
334 */
335int allow_signal(int sig)
336{
7ed20e1a 337 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
338 return -EINVAL;
339
340 spin_lock_irq(&current->sighand->siglock);
341 sigdelset(&current->blocked, sig);
342 if (!current->mm) {
343 /* Kernel threads handle their own signals.
344 Let the signal code know it'll be handled, so
345 that they don't get converted to SIGKILL or
346 just silently dropped */
347 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
348 }
349 recalc_sigpending();
350 spin_unlock_irq(&current->sighand->siglock);
351 return 0;
352}
353
354EXPORT_SYMBOL(allow_signal);
355
356int disallow_signal(int sig)
357{
7ed20e1a 358 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
359 return -EINVAL;
360
361 spin_lock_irq(&current->sighand->siglock);
10ab825b 362 current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
1da177e4
LT
363 recalc_sigpending();
364 spin_unlock_irq(&current->sighand->siglock);
365 return 0;
366}
367
368EXPORT_SYMBOL(disallow_signal);
369
370/*
371 * Put all the gunge required to become a kernel thread without
372 * attached user resources in one place where it belongs.
373 */
374
375void daemonize(const char *name, ...)
376{
377 va_list args;
378 struct fs_struct *fs;
379 sigset_t blocked;
380
381 va_start(args, name);
382 vsnprintf(current->comm, sizeof(current->comm), name, args);
383 va_end(args);
384
385 /*
386 * If we were started as result of loading a module, close all of the
387 * user space pages. We don't need them, and if we didn't close them
388 * they would be locked into memory.
389 */
390 exit_mm(current);
83144186
RW
391 /*
392 * We don't want to have TIF_FREEZE set if the system-wide hibernation
393 * or suspend transition begins right now.
394 */
395 current->flags |= PF_NOFREEZE;
1da177e4
LT
396
397 set_special_pids(1, 1);
24ec839c 398 proc_clear_tty(current);
1da177e4
LT
399
400 /* Block and flush all signals */
401 sigfillset(&blocked);
402 sigprocmask(SIG_BLOCK, &blocked, NULL);
403 flush_signals(current);
404
405 /* Become as one with the init task */
406
407 exit_fs(current); /* current->fs->count--; */
408 fs = init_task.fs;
409 current->fs = fs;
410 atomic_inc(&fs->count);
ab516013 411
444f378b 412 exit_task_namespaces(current);
ab516013 413 current->nsproxy = init_task.nsproxy;
ab516013
SH
414 get_task_namespaces(current);
415
1da177e4
LT
416 exit_files(current);
417 current->files = init_task.files;
418 atomic_inc(&current->files->count);
419
49d769d5 420 reparent_to_kthreadd();
1da177e4
LT
421}
422
423EXPORT_SYMBOL(daemonize);
424
858119e1 425static void close_files(struct files_struct * files)
1da177e4
LT
426{
427 int i, j;
badf1662 428 struct fdtable *fdt;
1da177e4
LT
429
430 j = 0;
4fb3a538
DS
431
432 /*
433 * It is safe to dereference the fd table without RCU or
434 * ->file_lock because this is the last reference to the
435 * files structure.
436 */
badf1662 437 fdt = files_fdtable(files);
1da177e4
LT
438 for (;;) {
439 unsigned long set;
440 i = j * __NFDBITS;
bbea9f69 441 if (i >= fdt->max_fds)
1da177e4 442 break;
badf1662 443 set = fdt->open_fds->fds_bits[j++];
1da177e4
LT
444 while (set) {
445 if (set & 1) {
badf1662 446 struct file * file = xchg(&fdt->fd[i], NULL);
944be0b2 447 if (file) {
1da177e4 448 filp_close(file, files);
944be0b2
IM
449 cond_resched();
450 }
1da177e4
LT
451 }
452 i++;
453 set >>= 1;
454 }
455 }
456}
457
458struct files_struct *get_files_struct(struct task_struct *task)
459{
460 struct files_struct *files;
461
462 task_lock(task);
463 files = task->files;
464 if (files)
465 atomic_inc(&files->count);
466 task_unlock(task);
467
468 return files;
469}
470
471void fastcall put_files_struct(struct files_struct *files)
472{
badf1662
DS
473 struct fdtable *fdt;
474
1da177e4
LT
475 if (atomic_dec_and_test(&files->count)) {
476 close_files(files);
477 /*
478 * Free the fd and fdset arrays if we expanded them.
ab2af1f5
DS
479 * If the fdtable was embedded, pass files for freeing
480 * at the end of the RCU grace period. Otherwise,
481 * you can free files immediately.
1da177e4 482 */
badf1662 483 fdt = files_fdtable(files);
4fd45812 484 if (fdt != &files->fdtab)
ab2af1f5 485 kmem_cache_free(files_cachep, files);
01b2d93c 486 free_fdtable(fdt);
1da177e4
LT
487 }
488}
489
490EXPORT_SYMBOL(put_files_struct);
491
3b9b8ab6
KK
492void reset_files_struct(struct task_struct *tsk, struct files_struct *files)
493{
494 struct files_struct *old;
495
496 old = tsk->files;
497 task_lock(tsk);
498 tsk->files = files;
499 task_unlock(tsk);
500 put_files_struct(old);
501}
502EXPORT_SYMBOL(reset_files_struct);
503
1da177e4
LT
504static inline void __exit_files(struct task_struct *tsk)
505{
506 struct files_struct * files = tsk->files;
507
508 if (files) {
509 task_lock(tsk);
510 tsk->files = NULL;
511 task_unlock(tsk);
512 put_files_struct(files);
513 }
514}
515
516void exit_files(struct task_struct *tsk)
517{
518 __exit_files(tsk);
519}
520
521static inline void __put_fs_struct(struct fs_struct *fs)
522{
523 /* No need to hold fs->lock if we are killing it */
524 if (atomic_dec_and_test(&fs->count)) {
525 dput(fs->root);
526 mntput(fs->rootmnt);
527 dput(fs->pwd);
528 mntput(fs->pwdmnt);
529 if (fs->altroot) {
530 dput(fs->altroot);
531 mntput(fs->altrootmnt);
532 }
533 kmem_cache_free(fs_cachep, fs);
534 }
535}
536
537void put_fs_struct(struct fs_struct *fs)
538{
539 __put_fs_struct(fs);
540}
541
542static inline void __exit_fs(struct task_struct *tsk)
543{
544 struct fs_struct * fs = tsk->fs;
545
546 if (fs) {
547 task_lock(tsk);
548 tsk->fs = NULL;
549 task_unlock(tsk);
550 __put_fs_struct(fs);
551 }
552}
553
554void exit_fs(struct task_struct *tsk)
555{
556 __exit_fs(tsk);
557}
558
559EXPORT_SYMBOL_GPL(exit_fs);
560
561/*
562 * Turn us into a lazy TLB process if we
563 * aren't already..
564 */
408b664a 565static void exit_mm(struct task_struct * tsk)
1da177e4
LT
566{
567 struct mm_struct *mm = tsk->mm;
568
569 mm_release(tsk, mm);
570 if (!mm)
571 return;
572 /*
573 * Serialize with any possible pending coredump.
574 * We must hold mmap_sem around checking core_waiters
575 * and clearing tsk->mm. The core-inducing thread
576 * will increment core_waiters for each thread in the
577 * group with ->mm != NULL.
578 */
579 down_read(&mm->mmap_sem);
580 if (mm->core_waiters) {
581 up_read(&mm->mmap_sem);
582 down_write(&mm->mmap_sem);
583 if (!--mm->core_waiters)
584 complete(mm->core_startup_done);
585 up_write(&mm->mmap_sem);
586
587 wait_for_completion(&mm->core_done);
588 down_read(&mm->mmap_sem);
589 }
590 atomic_inc(&mm->mm_count);
125e1874 591 BUG_ON(mm != tsk->active_mm);
1da177e4
LT
592 /* more a memory barrier than a real lock */
593 task_lock(tsk);
594 tsk->mm = NULL;
595 up_read(&mm->mmap_sem);
596 enter_lazy_tlb(mm, current);
597 task_unlock(tsk);
598 mmput(mm);
599}
600
36c8b586
IM
601static inline void
602choose_new_parent(struct task_struct *p, struct task_struct *reaper)
1da177e4
LT
603{
604 /*
605 * Make sure we're not reparenting to ourselves and that
606 * the parent is not a zombie.
607 */
d799f035 608 BUG_ON(p == reaper || reaper->exit_state);
1da177e4 609 p->real_parent = reaper;
1da177e4
LT
610}
611
36c8b586
IM
612static void
613reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
1da177e4 614{
241ceee0
ON
615 if (p->pdeath_signal)
616 /* We already hold the tasklist_lock here. */
617 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
618
1da177e4
LT
619 /* Move the child from its dying parent to the new one. */
620 if (unlikely(traced)) {
621 /* Preserve ptrace links if someone else is tracing this child. */
622 list_del_init(&p->ptrace_list);
623 if (p->parent != p->real_parent)
624 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
625 } else {
626 /* If this child is being traced, then we're the one tracing it
627 * anyway, so let go of it.
628 */
629 p->ptrace = 0;
6ac781b1 630 remove_parent(p);
1da177e4 631 p->parent = p->real_parent;
6ac781b1 632 add_parent(p);
1da177e4 633
b2b2cbc4 634 if (p->state == TASK_TRACED) {
1da177e4
LT
635 /*
636 * If it was at a trace stop, turn it into
637 * a normal stop since it's no longer being
638 * traced.
639 */
640 ptrace_untrace(p);
641 }
642 }
643
b2b2cbc4
EB
644 /* If this is a threaded reparent there is no need to
645 * notify anyone anything has happened.
646 */
647 if (p->real_parent->group_leader == father->group_leader)
648 return;
649
650 /* We don't want people slaying init. */
651 if (p->exit_signal != -1)
652 p->exit_signal = SIGCHLD;
b2b2cbc4
EB
653
654 /* If we'd notified the old parent about this child's death,
655 * also notify the new parent.
656 */
657 if (!traced && p->exit_state == EXIT_ZOMBIE &&
658 p->exit_signal != -1 && thread_group_empty(p))
659 do_notify_parent(p, p->exit_signal);
660
1da177e4
LT
661 /*
662 * process group orphan check
663 * Case ii: Our child is in a different pgrp
664 * than we are, and it was the only connection
665 * outside, so the child pgrp is now orphaned.
666 */
0475ac08
EB
667 if ((task_pgrp(p) != task_pgrp(father)) &&
668 (task_session(p) == task_session(father))) {
669 struct pid *pgrp = task_pgrp(p);
1da177e4 670
937949d9
CLG
671 if (will_become_orphaned_pgrp(pgrp, NULL) &&
672 has_stopped_jobs(pgrp)) {
0475ac08
EB
673 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
674 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
1da177e4
LT
675 }
676 }
677}
678
679/*
680 * When we die, we re-parent all our children.
681 * Try to give them to another thread in our thread
682 * group, and if no such member exists, give it to
84d73786
SB
683 * the child reaper process (ie "init") in our pid
684 * space.
1da177e4 685 */
36c8b586
IM
686static void
687forget_original_parent(struct task_struct *father, struct list_head *to_release)
1da177e4
LT
688{
689 struct task_struct *p, *reaper = father;
690 struct list_head *_p, *_n;
691
692 do {
693 reaper = next_thread(reaper);
694 if (reaper == father) {
84d73786 695 reaper = child_reaper(father);
1da177e4
LT
696 break;
697 }
698 } while (reaper->exit_state);
699
700 /*
701 * There are only two places where our children can be:
702 *
703 * - in our child list
704 * - in our ptraced child list
705 *
706 * Search them and reparent children.
707 */
708 list_for_each_safe(_p, _n, &father->children) {
709 int ptrace;
36c8b586 710 p = list_entry(_p, struct task_struct, sibling);
1da177e4
LT
711
712 ptrace = p->ptrace;
713
714 /* if father isn't the real parent, then ptrace must be enabled */
715 BUG_ON(father != p->real_parent && !ptrace);
716
717 if (father == p->real_parent) {
718 /* reparent with a reaper, real father it's us */
d799f035 719 choose_new_parent(p, reaper);
1da177e4
LT
720 reparent_thread(p, father, 0);
721 } else {
722 /* reparent ptraced task to its real parent */
723 __ptrace_unlink (p);
724 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
725 thread_group_empty(p))
726 do_notify_parent(p, p->exit_signal);
727 }
728
729 /*
730 * if the ptraced child is a zombie with exit_signal == -1
731 * we must collect it before we exit, or it will remain
732 * zombie forever since we prevented it from self-reap itself
733 * while it was being traced by us, to be able to see it in wait4.
734 */
735 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
736 list_add(&p->ptrace_list, to_release);
737 }
738 list_for_each_safe(_p, _n, &father->ptrace_children) {
36c8b586 739 p = list_entry(_p, struct task_struct, ptrace_list);
d799f035 740 choose_new_parent(p, reaper);
1da177e4
LT
741 reparent_thread(p, father, 1);
742 }
743}
744
745/*
746 * Send signals to all our closest relatives so that they know
747 * to properly mourn us..
748 */
749static void exit_notify(struct task_struct *tsk)
750{
751 int state;
752 struct task_struct *t;
753 struct list_head ptrace_dead, *_p, *_n;
0475ac08 754 struct pid *pgrp;
1da177e4
LT
755
756 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
757 && !thread_group_empty(tsk)) {
758 /*
759 * This occurs when there was a race between our exit
760 * syscall and a group signal choosing us as the one to
761 * wake up. It could be that we are the only thread
762 * alerted to check for pending signals, but another thread
763 * should be woken now to take the signal since we will not.
764 * Now we'll wake all the threads in the group just to make
765 * sure someone gets all the pending signals.
766 */
767 read_lock(&tasklist_lock);
768 spin_lock_irq(&tsk->sighand->siglock);
769 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
7bb44ade
RM
770 if (!signal_pending(t) && !(t->flags & PF_EXITING))
771 recalc_sigpending_and_wake(t);
1da177e4
LT
772 spin_unlock_irq(&tsk->sighand->siglock);
773 read_unlock(&tasklist_lock);
774 }
775
776 write_lock_irq(&tasklist_lock);
777
778 /*
779 * This does two things:
780 *
781 * A. Make init inherit all the child processes
782 * B. Check to see if any process groups have become orphaned
783 * as a result of our exiting, and if they have any stopped
784 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
785 */
786
787 INIT_LIST_HEAD(&ptrace_dead);
788 forget_original_parent(tsk, &ptrace_dead);
789 BUG_ON(!list_empty(&tsk->children));
790 BUG_ON(!list_empty(&tsk->ptrace_children));
791
792 /*
793 * Check to see if any process groups have become orphaned
794 * as a result of our exiting, and if they have any stopped
795 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
796 *
797 * Case i: Our father is in a different pgrp than we are
798 * and we were the only connection outside, so our pgrp
799 * is about to become orphaned.
800 */
801
802 t = tsk->real_parent;
803
0475ac08
EB
804 pgrp = task_pgrp(tsk);
805 if ((task_pgrp(t) != pgrp) &&
14e9d573 806 (task_session(t) == task_session(tsk)) &&
0475ac08
EB
807 will_become_orphaned_pgrp(pgrp, tsk) &&
808 has_stopped_jobs(pgrp)) {
809 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
810 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
1da177e4
LT
811 }
812
813 /* Let father know we died
814 *
815 * Thread signals are configurable, but you aren't going to use
816 * that to send signals to arbitary processes.
817 * That stops right now.
818 *
819 * If the parent exec id doesn't match the exec id we saved
820 * when we started then we know the parent has changed security
821 * domain.
822 *
823 * If our self_exec id doesn't match our parent_exec_id then
824 * we have changed execution domain as these two values started
825 * the same after a fork.
826 *
827 */
828
829 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
830 ( tsk->parent_exec_id != t->self_exec_id ||
831 tsk->self_exec_id != tsk->parent_exec_id)
832 && !capable(CAP_KILL))
833 tsk->exit_signal = SIGCHLD;
834
835
836 /* If something other than our normal parent is ptracing us, then
837 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
838 * only has special meaning to our real parent.
839 */
840 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
841 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
842 do_notify_parent(tsk, signal);
843 } else if (tsk->ptrace) {
844 do_notify_parent(tsk, SIGCHLD);
845 }
846
847 state = EXIT_ZOMBIE;
848 if (tsk->exit_signal == -1 &&
849 (likely(tsk->ptrace == 0) ||
850 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
851 state = EXIT_DEAD;
852 tsk->exit_state = state;
853
854 write_unlock_irq(&tasklist_lock);
855
856 list_for_each_safe(_p, _n, &ptrace_dead) {
857 list_del_init(_p);
36c8b586 858 t = list_entry(_p, struct task_struct, ptrace_list);
1da177e4
LT
859 release_task(t);
860 }
861
862 /* If the process is dead, release it - nobody will wait for it */
863 if (state == EXIT_DEAD)
864 release_task(tsk);
1da177e4
LT
865}
866
e18eecb8
JD
867#ifdef CONFIG_DEBUG_STACK_USAGE
868static void check_stack_usage(void)
869{
870 static DEFINE_SPINLOCK(low_water_lock);
871 static int lowest_to_date = THREAD_SIZE;
872 unsigned long *n = end_of_stack(current);
873 unsigned long free;
874
875 while (*n == 0)
876 n++;
877 free = (unsigned long)n - (unsigned long)end_of_stack(current);
878
879 if (free >= lowest_to_date)
880 return;
881
882 spin_lock(&low_water_lock);
883 if (free < lowest_to_date) {
884 printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
885 "left\n",
886 current->comm, free);
887 lowest_to_date = free;
888 }
889 spin_unlock(&low_water_lock);
890}
891#else
892static inline void check_stack_usage(void) {}
893#endif
894
1da177e4
LT
895fastcall NORET_TYPE void do_exit(long code)
896{
897 struct task_struct *tsk = current;
898 int group_dead;
899
900 profile_task_exit(tsk);
901
22e2c507
JA
902 WARN_ON(atomic_read(&tsk->fs_excl));
903
1da177e4
LT
904 if (unlikely(in_interrupt()))
905 panic("Aiee, killing interrupt handler!");
906 if (unlikely(!tsk->pid))
907 panic("Attempted to kill the idle task!");
84d73786
SB
908 if (unlikely(tsk == child_reaper(tsk))) {
909 if (tsk->nsproxy->pid_ns != &init_pid_ns)
910 tsk->nsproxy->pid_ns->child_reaper = init_pid_ns.child_reaper;
911 else
912 panic("Attempted to kill init!");
913 }
914
1da177e4
LT
915
916 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
917 current->ptrace_message = code;
918 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
919 }
920
df164db5
AN
921 /*
922 * We're taking recursive faults here in do_exit. Safest is to just
923 * leave this task alone and wait for reboot.
924 */
925 if (unlikely(tsk->flags & PF_EXITING)) {
926 printk(KERN_ALERT
927 "Fixing recursive fault but reboot is needed!\n");
778e9a9c
AK
928 /*
929 * We can do this unlocked here. The futex code uses
930 * this flag just to verify whether the pi state
931 * cleanup has been done or not. In the worst case it
932 * loops once more. We pretend that the cleanup was
933 * done as there is no way to return. Either the
934 * OWNER_DIED bit is set by now or we push the blocked
935 * task into the wait for ever nirwana as well.
936 */
937 tsk->flags |= PF_EXITPIDONE;
afc847b7
AV
938 if (tsk->io_context)
939 exit_io_context();
df164db5
AN
940 set_current_state(TASK_UNINTERRUPTIBLE);
941 schedule();
942 }
943
778e9a9c
AK
944 /*
945 * tsk->flags are checked in the futex code to protect against
946 * an exiting task cleaning up the robust pi futexes.
947 */
948 spin_lock_irq(&tsk->pi_lock);
1da177e4 949 tsk->flags |= PF_EXITING;
778e9a9c 950 spin_unlock_irq(&tsk->pi_lock);
1da177e4 951
1da177e4
LT
952 if (unlikely(in_atomic()))
953 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
954 current->comm, current->pid,
955 preempt_count());
956
957 acct_update_integrals(tsk);
365e9c87
HD
958 if (tsk->mm) {
959 update_hiwater_rss(tsk->mm);
960 update_hiwater_vm(tsk->mm);
961 }
1da177e4 962 group_dead = atomic_dec_and_test(&tsk->signal->live);
c3068951 963 if (group_dead) {
778e9a9c 964 hrtimer_cancel(&tsk->signal->real_timer);
25f407f0 965 exit_itimers(tsk->signal);
c3068951 966 }
f6ec29a4 967 acct_collect(code, group_dead);
0771dfef
IM
968 if (unlikely(tsk->robust_list))
969 exit_robust_list(tsk);
2aa92581 970#if defined(CONFIG_FUTEX) && defined(CONFIG_COMPAT)
34f192c6
IM
971 if (unlikely(tsk->compat_robust_list))
972 compat_exit_robust_list(tsk);
973#endif
522ed776
MT
974 if (group_dead)
975 tty_audit_exit();
fa84cb93
AV
976 if (unlikely(tsk->audit_context))
977 audit_free(tsk);
115085ea
ON
978
979 taskstats_exit(tsk, group_dead);
c757249a 980
1da177e4
LT
981 exit_mm(tsk);
982
0e464814 983 if (group_dead)
f6ec29a4 984 acct_process();
1da177e4
LT
985 exit_sem(tsk);
986 __exit_files(tsk);
987 __exit_fs(tsk);
e18eecb8 988 check_stack_usage();
1da177e4
LT
989 exit_thread();
990 cpuset_exit(tsk);
991 exit_keys(tsk);
992
993 if (group_dead && tsk->signal->leader)
994 disassociate_ctty(1);
995
a1261f54 996 module_put(task_thread_info(tsk)->exec_domain->module);
1da177e4
LT
997 if (tsk->binfmt)
998 module_put(tsk->binfmt->module);
999
1000 tsk->exit_code = code;
9f46080c 1001 proc_exit_connector(tsk);
444f378b 1002 exit_task_namespaces(tsk);
0f245285 1003 exit_notify(tsk);
1da177e4
LT
1004#ifdef CONFIG_NUMA
1005 mpol_free(tsk->mempolicy);
1006 tsk->mempolicy = NULL;
1007#endif
c87e2837
IM
1008 /*
1009 * This must happen late, after the PID is not
1010 * hashed anymore:
1011 */
1012 if (unlikely(!list_empty(&tsk->pi_state_list)))
1013 exit_pi_state_list(tsk);
1014 if (unlikely(current->pi_state_cache))
1015 kfree(current->pi_state_cache);
de5097c2 1016 /*
9a11b49a 1017 * Make sure we are holding no locks:
de5097c2 1018 */
9a11b49a 1019 debug_check_no_locks_held(tsk);
778e9a9c
AK
1020 /*
1021 * We can do this unlocked here. The futex code uses this flag
1022 * just to verify whether the pi state cleanup has been done
1023 * or not. In the worst case it loops once more.
1024 */
1025 tsk->flags |= PF_EXITPIDONE;
1da177e4 1026
afc847b7
AV
1027 if (tsk->io_context)
1028 exit_io_context();
1029
b92ce558
JA
1030 if (tsk->splice_pipe)
1031 __free_pipe_info(tsk->splice_pipe);
1032
7407251a 1033 preempt_disable();
55a101f8 1034 /* causes final put_task_struct in finish_task_switch(). */
c394cc9f 1035 tsk->state = TASK_DEAD;
7407251a 1036
1da177e4
LT
1037 schedule();
1038 BUG();
1039 /* Avoid "noreturn function does return". */
54306cf0
AC
1040 for (;;)
1041 cpu_relax(); /* For when BUG is null */
1da177e4
LT
1042}
1043
012914da
RA
1044EXPORT_SYMBOL_GPL(do_exit);
1045
1da177e4
LT
1046NORET_TYPE void complete_and_exit(struct completion *comp, long code)
1047{
1048 if (comp)
1049 complete(comp);
55a101f8 1050
1da177e4
LT
1051 do_exit(code);
1052}
1053
1054EXPORT_SYMBOL(complete_and_exit);
1055
1056asmlinkage long sys_exit(int error_code)
1057{
1058 do_exit((error_code&0xff)<<8);
1059}
1060
1da177e4
LT
1061/*
1062 * Take down every thread in the group. This is called by fatal signals
1063 * as well as by sys_exit_group (below).
1064 */
1065NORET_TYPE void
1066do_group_exit(int exit_code)
1067{
1068 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
1069
1070 if (current->signal->flags & SIGNAL_GROUP_EXIT)
1071 exit_code = current->signal->group_exit_code;
1072 else if (!thread_group_empty(current)) {
1073 struct signal_struct *const sig = current->signal;
1074 struct sighand_struct *const sighand = current->sighand;
1da177e4
LT
1075 spin_lock_irq(&sighand->siglock);
1076 if (sig->flags & SIGNAL_GROUP_EXIT)
1077 /* Another thread got here before we took the lock. */
1078 exit_code = sig->group_exit_code;
1079 else {
1da177e4
LT
1080 sig->group_exit_code = exit_code;
1081 zap_other_threads(current);
1082 }
1083 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
1084 }
1085
1086 do_exit(exit_code);
1087 /* NOTREACHED */
1088}
1089
1090/*
1091 * this kills every thread in the thread group. Note that any externally
1092 * wait4()-ing process will get the correct exit code - even if this
1093 * thread is not the thread group leader.
1094 */
1095asmlinkage void sys_exit_group(int error_code)
1096{
1097 do_group_exit((error_code & 0xff) << 8);
1098}
1099
36c8b586 1100static int eligible_child(pid_t pid, int options, struct task_struct *p)
1da177e4 1101{
73243284
RM
1102 int err;
1103
1da177e4
LT
1104 if (pid > 0) {
1105 if (p->pid != pid)
1106 return 0;
1107 } else if (!pid) {
1108 if (process_group(p) != process_group(current))
1109 return 0;
1110 } else if (pid != -1) {
1111 if (process_group(p) != -pid)
1112 return 0;
1113 }
1114
1115 /*
1116 * Do not consider detached threads that are
1117 * not ptraced:
1118 */
1119 if (p->exit_signal == -1 && !p->ptrace)
1120 return 0;
1121
1122 /* Wait for all children (clone and not) if __WALL is set;
1123 * otherwise, wait for clone children *only* if __WCLONE is
1124 * set; otherwise, wait for non-clone children *only*. (Note:
1125 * A "clone" child here is one that reports to its parent
1126 * using a signal other than SIGCHLD.) */
1127 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
1128 && !(options & __WALL))
1129 return 0;
1130 /*
1131 * Do not consider thread group leaders that are
1132 * in a non-empty thread group:
1133 */
3b6362b8 1134 if (delay_group_leader(p))
1da177e4
LT
1135 return 2;
1136
73243284
RM
1137 err = security_task_wait(p);
1138 if (err)
1139 return err;
1da177e4
LT
1140
1141 return 1;
1142}
1143
36c8b586 1144static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1da177e4
LT
1145 int why, int status,
1146 struct siginfo __user *infop,
1147 struct rusage __user *rusagep)
1148{
1149 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
36c8b586 1150
1da177e4
LT
1151 put_task_struct(p);
1152 if (!retval)
1153 retval = put_user(SIGCHLD, &infop->si_signo);
1154 if (!retval)
1155 retval = put_user(0, &infop->si_errno);
1156 if (!retval)
1157 retval = put_user((short)why, &infop->si_code);
1158 if (!retval)
1159 retval = put_user(pid, &infop->si_pid);
1160 if (!retval)
1161 retval = put_user(uid, &infop->si_uid);
1162 if (!retval)
1163 retval = put_user(status, &infop->si_status);
1164 if (!retval)
1165 retval = pid;
1166 return retval;
1167}
1168
1169/*
1170 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1171 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1172 * the lock and this task is uninteresting. If we return nonzero, we have
1173 * released the lock and the system call should return.
1174 */
36c8b586 1175static int wait_task_zombie(struct task_struct *p, int noreap,
1da177e4
LT
1176 struct siginfo __user *infop,
1177 int __user *stat_addr, struct rusage __user *ru)
1178{
1179 unsigned long state;
1180 int retval;
1181 int status;
1182
1183 if (unlikely(noreap)) {
1184 pid_t pid = p->pid;
1185 uid_t uid = p->uid;
1186 int exit_code = p->exit_code;
1187 int why, status;
1188
1189 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1190 return 0;
1191 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1192 return 0;
1193 get_task_struct(p);
1194 read_unlock(&tasklist_lock);
1195 if ((exit_code & 0x7f) == 0) {
1196 why = CLD_EXITED;
1197 status = exit_code >> 8;
1198 } else {
1199 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1200 status = exit_code & 0x7f;
1201 }
1202 return wait_noreap_copyout(p, pid, uid, why,
1203 status, infop, ru);
1204 }
1205
1206 /*
1207 * Try to move the task's state to DEAD
1208 * only one thread is allowed to do this:
1209 */
1210 state = xchg(&p->exit_state, EXIT_DEAD);
1211 if (state != EXIT_ZOMBIE) {
1212 BUG_ON(state != EXIT_DEAD);
1213 return 0;
1214 }
1215 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1216 /*
1217 * This can only happen in a race with a ptraced thread
1218 * dying on another processor.
1219 */
1220 return 0;
1221 }
1222
1223 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
3795e161
JJ
1224 struct signal_struct *psig;
1225 struct signal_struct *sig;
1226
1da177e4
LT
1227 /*
1228 * The resource counters for the group leader are in its
1229 * own task_struct. Those for dead threads in the group
1230 * are in its signal_struct, as are those for the child
1231 * processes it has previously reaped. All these
1232 * accumulate in the parent's signal_struct c* fields.
1233 *
1234 * We don't bother to take a lock here to protect these
1235 * p->signal fields, because they are only touched by
1236 * __exit_signal, which runs with tasklist_lock
1237 * write-locked anyway, and so is excluded here. We do
1238 * need to protect the access to p->parent->signal fields,
1239 * as other threads in the parent group can be right
1240 * here reaping other children at the same time.
1241 */
1242 spin_lock_irq(&p->parent->sighand->siglock);
3795e161
JJ
1243 psig = p->parent->signal;
1244 sig = p->signal;
1245 psig->cutime =
1246 cputime_add(psig->cutime,
1da177e4 1247 cputime_add(p->utime,
3795e161
JJ
1248 cputime_add(sig->utime,
1249 sig->cutime)));
1250 psig->cstime =
1251 cputime_add(psig->cstime,
1da177e4 1252 cputime_add(p->stime,
3795e161
JJ
1253 cputime_add(sig->stime,
1254 sig->cstime)));
1255 psig->cmin_flt +=
1256 p->min_flt + sig->min_flt + sig->cmin_flt;
1257 psig->cmaj_flt +=
1258 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1259 psig->cnvcsw +=
1260 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1261 psig->cnivcsw +=
1262 p->nivcsw + sig->nivcsw + sig->cnivcsw;
6eaeeaba
ED
1263 psig->cinblock +=
1264 task_io_get_inblock(p) +
1265 sig->inblock + sig->cinblock;
1266 psig->coublock +=
1267 task_io_get_oublock(p) +
1268 sig->oublock + sig->coublock;
1da177e4
LT
1269 spin_unlock_irq(&p->parent->sighand->siglock);
1270 }
1271
1272 /*
1273 * Now we are sure this task is interesting, and no other
1274 * thread can reap it because we set its state to EXIT_DEAD.
1275 */
1276 read_unlock(&tasklist_lock);
1277
1278 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1279 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1280 ? p->signal->group_exit_code : p->exit_code;
1281 if (!retval && stat_addr)
1282 retval = put_user(status, stat_addr);
1283 if (!retval && infop)
1284 retval = put_user(SIGCHLD, &infop->si_signo);
1285 if (!retval && infop)
1286 retval = put_user(0, &infop->si_errno);
1287 if (!retval && infop) {
1288 int why;
1289
1290 if ((status & 0x7f) == 0) {
1291 why = CLD_EXITED;
1292 status >>= 8;
1293 } else {
1294 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1295 status &= 0x7f;
1296 }
1297 retval = put_user((short)why, &infop->si_code);
1298 if (!retval)
1299 retval = put_user(status, &infop->si_status);
1300 }
1301 if (!retval && infop)
1302 retval = put_user(p->pid, &infop->si_pid);
1303 if (!retval && infop)
1304 retval = put_user(p->uid, &infop->si_uid);
1305 if (retval) {
1306 // TODO: is this safe?
1307 p->exit_state = EXIT_ZOMBIE;
1308 return retval;
1309 }
1310 retval = p->pid;
1311 if (p->real_parent != p->parent) {
1312 write_lock_irq(&tasklist_lock);
1313 /* Double-check with lock held. */
1314 if (p->real_parent != p->parent) {
1315 __ptrace_unlink(p);
1316 // TODO: is this safe?
1317 p->exit_state = EXIT_ZOMBIE;
1318 /*
1319 * If this is not a detached task, notify the parent.
1320 * If it's still not detached after that, don't release
1321 * it now.
1322 */
1323 if (p->exit_signal != -1) {
1324 do_notify_parent(p, p->exit_signal);
1325 if (p->exit_signal != -1)
1326 p = NULL;
1327 }
1328 }
1329 write_unlock_irq(&tasklist_lock);
1330 }
1331 if (p != NULL)
1332 release_task(p);
1333 BUG_ON(!retval);
1334 return retval;
1335}
1336
1337/*
1338 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1339 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1340 * the lock and this task is uninteresting. If we return nonzero, we have
1341 * released the lock and the system call should return.
1342 */
36c8b586
IM
1343static int wait_task_stopped(struct task_struct *p, int delayed_group_leader,
1344 int noreap, struct siginfo __user *infop,
1da177e4
LT
1345 int __user *stat_addr, struct rusage __user *ru)
1346{
1347 int retval, exit_code;
1348
1349 if (!p->exit_code)
1350 return 0;
1351 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1352 p->signal && p->signal->group_stop_count > 0)
1353 /*
1354 * A group stop is in progress and this is the group leader.
1355 * We won't report until all threads have stopped.
1356 */
1357 return 0;
1358
1359 /*
1360 * Now we are pretty sure this task is interesting.
1361 * Make sure it doesn't get reaped out from under us while we
1362 * give up the lock and then examine it below. We don't want to
1363 * keep holding onto the tasklist_lock while we call getrusage and
1364 * possibly take page faults for user memory.
1365 */
1366 get_task_struct(p);
1367 read_unlock(&tasklist_lock);
1368
1369 if (unlikely(noreap)) {
1370 pid_t pid = p->pid;
1371 uid_t uid = p->uid;
1372 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1373
1374 exit_code = p->exit_code;
1375 if (unlikely(!exit_code) ||
14bf01bb 1376 unlikely(p->state & TASK_TRACED))
1da177e4
LT
1377 goto bail_ref;
1378 return wait_noreap_copyout(p, pid, uid,
1379 why, (exit_code << 8) | 0x7f,
1380 infop, ru);
1381 }
1382
1383 write_lock_irq(&tasklist_lock);
1384
1385 /*
1386 * This uses xchg to be atomic with the thread resuming and setting
1387 * it. It must also be done with the write lock held to prevent a
1388 * race with the EXIT_ZOMBIE case.
1389 */
1390 exit_code = xchg(&p->exit_code, 0);
1391 if (unlikely(p->exit_state)) {
1392 /*
1393 * The task resumed and then died. Let the next iteration
1394 * catch it in EXIT_ZOMBIE. Note that exit_code might
1395 * already be zero here if it resumed and did _exit(0).
1396 * The task itself is dead and won't touch exit_code again;
1397 * other processors in this function are locked out.
1398 */
1399 p->exit_code = exit_code;
1400 exit_code = 0;
1401 }
1402 if (unlikely(exit_code == 0)) {
1403 /*
1404 * Another thread in this function got to it first, or it
1405 * resumed, or it resumed and then died.
1406 */
1407 write_unlock_irq(&tasklist_lock);
1408bail_ref:
1409 put_task_struct(p);
1410 /*
1411 * We are returning to the wait loop without having successfully
1412 * removed the process and having released the lock. We cannot
1413 * continue, since the "p" task pointer is potentially stale.
1414 *
1415 * Return -EAGAIN, and do_wait() will restart the loop from the
1416 * beginning. Do _not_ re-acquire the lock.
1417 */
1418 return -EAGAIN;
1419 }
1420
1421 /* move to end of parent's list to avoid starvation */
1422 remove_parent(p);
8fafabd8 1423 add_parent(p);
1da177e4
LT
1424
1425 write_unlock_irq(&tasklist_lock);
1426
1427 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1428 if (!retval && stat_addr)
1429 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1430 if (!retval && infop)
1431 retval = put_user(SIGCHLD, &infop->si_signo);
1432 if (!retval && infop)
1433 retval = put_user(0, &infop->si_errno);
1434 if (!retval && infop)
1435 retval = put_user((short)((p->ptrace & PT_PTRACED)
1436 ? CLD_TRAPPED : CLD_STOPPED),
1437 &infop->si_code);
1438 if (!retval && infop)
1439 retval = put_user(exit_code, &infop->si_status);
1440 if (!retval && infop)
1441 retval = put_user(p->pid, &infop->si_pid);
1442 if (!retval && infop)
1443 retval = put_user(p->uid, &infop->si_uid);
1444 if (!retval)
1445 retval = p->pid;
1446 put_task_struct(p);
1447
1448 BUG_ON(!retval);
1449 return retval;
1450}
1451
1452/*
1453 * Handle do_wait work for one task in a live, non-stopped state.
1454 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1455 * the lock and this task is uninteresting. If we return nonzero, we have
1456 * released the lock and the system call should return.
1457 */
36c8b586 1458static int wait_task_continued(struct task_struct *p, int noreap,
1da177e4
LT
1459 struct siginfo __user *infop,
1460 int __user *stat_addr, struct rusage __user *ru)
1461{
1462 int retval;
1463 pid_t pid;
1464 uid_t uid;
1465
1466 if (unlikely(!p->signal))
1467 return 0;
1468
1469 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1470 return 0;
1471
1472 spin_lock_irq(&p->sighand->siglock);
1473 /* Re-check with the lock held. */
1474 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1475 spin_unlock_irq(&p->sighand->siglock);
1476 return 0;
1477 }
1478 if (!noreap)
1479 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1480 spin_unlock_irq(&p->sighand->siglock);
1481
1482 pid = p->pid;
1483 uid = p->uid;
1484 get_task_struct(p);
1485 read_unlock(&tasklist_lock);
1486
1487 if (!infop) {
1488 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1489 put_task_struct(p);
1490 if (!retval && stat_addr)
1491 retval = put_user(0xffff, stat_addr);
1492 if (!retval)
1493 retval = p->pid;
1494 } else {
1495 retval = wait_noreap_copyout(p, pid, uid,
1496 CLD_CONTINUED, SIGCONT,
1497 infop, ru);
1498 BUG_ON(retval == 0);
1499 }
1500
1501 return retval;
1502}
1503
1504
1505static inline int my_ptrace_child(struct task_struct *p)
1506{
1507 if (!(p->ptrace & PT_PTRACED))
1508 return 0;
1509 if (!(p->ptrace & PT_ATTACHED))
1510 return 1;
1511 /*
1512 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1513 * we are the attacher. If we are the real parent, this is a race
1514 * inside ptrace_attach. It is waiting for the tasklist_lock,
1515 * which we have to switch the parent links, but has already set
1516 * the flags in p->ptrace.
1517 */
1518 return (p->parent != p->real_parent);
1519}
1520
1521static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1522 int __user *stat_addr, struct rusage __user *ru)
1523{
1524 DECLARE_WAITQUEUE(wait, current);
1525 struct task_struct *tsk;
1526 int flag, retval;
73243284 1527 int allowed, denied;
1da177e4
LT
1528
1529 add_wait_queue(&current->signal->wait_chldexit,&wait);
1530repeat:
1531 /*
1532 * We will set this flag if we see any child that might later
1533 * match our criteria, even if we are not able to reap it yet.
1534 */
1535 flag = 0;
73243284 1536 allowed = denied = 0;
1da177e4
LT
1537 current->state = TASK_INTERRUPTIBLE;
1538 read_lock(&tasklist_lock);
1539 tsk = current;
1540 do {
1541 struct task_struct *p;
1542 struct list_head *_p;
1543 int ret;
1544
1545 list_for_each(_p,&tsk->children) {
36c8b586 1546 p = list_entry(_p, struct task_struct, sibling);
1da177e4
LT
1547
1548 ret = eligible_child(pid, options, p);
1549 if (!ret)
1550 continue;
1551
73243284
RM
1552 if (unlikely(ret < 0)) {
1553 denied = ret;
1554 continue;
1555 }
1556 allowed = 1;
1557
1da177e4
LT
1558 switch (p->state) {
1559 case TASK_TRACED:
7f2a5255
RM
1560 /*
1561 * When we hit the race with PTRACE_ATTACH,
1562 * we will not report this child. But the
1563 * race means it has not yet been moved to
1564 * our ptrace_children list, so we need to
1565 * set the flag here to avoid a spurious ECHILD
1566 * when the race happens with the only child.
1567 */
1568 flag = 1;
1da177e4
LT
1569 if (!my_ptrace_child(p))
1570 continue;
1571 /*FALLTHROUGH*/
1572 case TASK_STOPPED:
1573 /*
1574 * It's stopped now, so it might later
1575 * continue, exit, or stop again.
1576 */
1577 flag = 1;
1578 if (!(options & WUNTRACED) &&
1579 !my_ptrace_child(p))
1580 continue;
1581 retval = wait_task_stopped(p, ret == 2,
1582 (options & WNOWAIT),
1583 infop,
1584 stat_addr, ru);
1585 if (retval == -EAGAIN)
1586 goto repeat;
1587 if (retval != 0) /* He released the lock. */
1588 goto end;
1589 break;
1590 default:
1591 // case EXIT_DEAD:
1592 if (p->exit_state == EXIT_DEAD)
1593 continue;
1594 // case EXIT_ZOMBIE:
1595 if (p->exit_state == EXIT_ZOMBIE) {
1596 /*
1597 * Eligible but we cannot release
1598 * it yet:
1599 */
1600 if (ret == 2)
1601 goto check_continued;
1602 if (!likely(options & WEXITED))
1603 continue;
1604 retval = wait_task_zombie(
1605 p, (options & WNOWAIT),
1606 infop, stat_addr, ru);
1607 /* He released the lock. */
1608 if (retval != 0)
1609 goto end;
1610 break;
1611 }
1612check_continued:
1613 /*
1614 * It's running now, so it might later
1615 * exit, stop, or stop and then continue.
1616 */
1617 flag = 1;
1618 if (!unlikely(options & WCONTINUED))
1619 continue;
1620 retval = wait_task_continued(
1621 p, (options & WNOWAIT),
1622 infop, stat_addr, ru);
1623 if (retval != 0) /* He released the lock. */
1624 goto end;
1625 break;
1626 }
1627 }
1628 if (!flag) {
1629 list_for_each(_p, &tsk->ptrace_children) {
1630 p = list_entry(_p, struct task_struct,
1631 ptrace_list);
1632 if (!eligible_child(pid, options, p))
1633 continue;
1634 flag = 1;
1635 break;
1636 }
1637 }
1638 if (options & __WNOTHREAD)
1639 break;
1640 tsk = next_thread(tsk);
125e1874 1641 BUG_ON(tsk->signal != current->signal);
1da177e4
LT
1642 } while (tsk != current);
1643
1644 read_unlock(&tasklist_lock);
1645 if (flag) {
1646 retval = 0;
1647 if (options & WNOHANG)
1648 goto end;
1649 retval = -ERESTARTSYS;
1650 if (signal_pending(current))
1651 goto end;
1652 schedule();
1653 goto repeat;
1654 }
1655 retval = -ECHILD;
73243284
RM
1656 if (unlikely(denied) && !allowed)
1657 retval = denied;
1da177e4
LT
1658end:
1659 current->state = TASK_RUNNING;
1660 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1661 if (infop) {
1662 if (retval > 0)
1663 retval = 0;
1664 else {
1665 /*
1666 * For a WNOHANG return, clear out all the fields
1667 * we would set so the user can easily tell the
1668 * difference.
1669 */
1670 if (!retval)
1671 retval = put_user(0, &infop->si_signo);
1672 if (!retval)
1673 retval = put_user(0, &infop->si_errno);
1674 if (!retval)
1675 retval = put_user(0, &infop->si_code);
1676 if (!retval)
1677 retval = put_user(0, &infop->si_pid);
1678 if (!retval)
1679 retval = put_user(0, &infop->si_uid);
1680 if (!retval)
1681 retval = put_user(0, &infop->si_status);
1682 }
1683 }
1684 return retval;
1685}
1686
1687asmlinkage long sys_waitid(int which, pid_t pid,
1688 struct siginfo __user *infop, int options,
1689 struct rusage __user *ru)
1690{
1691 long ret;
1692
1693 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1694 return -EINVAL;
1695 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1696 return -EINVAL;
1697
1698 switch (which) {
1699 case P_ALL:
1700 pid = -1;
1701 break;
1702 case P_PID:
1703 if (pid <= 0)
1704 return -EINVAL;
1705 break;
1706 case P_PGID:
1707 if (pid <= 0)
1708 return -EINVAL;
1709 pid = -pid;
1710 break;
1711 default:
1712 return -EINVAL;
1713 }
1714
1715 ret = do_wait(pid, options, infop, NULL, ru);
1716
1717 /* avoid REGPARM breakage on x86: */
1718 prevent_tail_call(ret);
1719 return ret;
1720}
1721
1722asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1723 int options, struct rusage __user *ru)
1724{
1725 long ret;
1726
1727 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1728 __WNOTHREAD|__WCLONE|__WALL))
1729 return -EINVAL;
1730 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1731
1732 /* avoid REGPARM breakage on x86: */
1733 prevent_tail_call(ret);
1734 return ret;
1735}
1736
1737#ifdef __ARCH_WANT_SYS_WAITPID
1738
1739/*
1740 * sys_waitpid() remains for compatibility. waitpid() should be
1741 * implemented by calling sys_wait4() from libc.a.
1742 */
1743asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1744{
1745 return sys_wait4(pid, stat_addr, options, NULL);
1746}
1747
1748#endif